scclib
Stable Cloud Computing C++ Library
bignum.cc
Go to the documentation of this file.
1 /*
2 BSD 3-Clause License
3 
4 Copyright (c) 2022, Stable Cloud Computing, Inc.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9 1. Redistributions of source code must retain the above copyright notice, this
10  list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright notice,
13  this list of conditions and the following disclaimer in the documentation
14  and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include <crypto/bignum.h>
32 #include <gtest/gtest.h>
33 #include <iostream>
34 #include <string>
35 #include <encode/hex.h>
36 
43 using std::cout;
44 using std::endl;
45 using std::vector;
46 using std::runtime_error;
47 using scc::encode::Hex;
49 
50 TEST(bignum_test, init)
51 {
52  Bignum n;
53  cout << "Init: " << n << endl;
54  ASSERT_EQ(n.str(), "0");
55 }
56 
57 TEST(bignum_test, set_zero)
58 {
59  Bignum n;
60  n = 0;
61  cout << "Zero: " << n << endl;
62  ASSERT_EQ(n.str(), "0");
63 }
64 
65 TEST(bignum_test, set_one)
66 {
67  Bignum n;
68  n = 1;
69  cout << "One: " << n << endl;
70  ASSERT_EQ(n.str(), "1");
71 }
72 
73 TEST(bignum_test, init_ten)
74 {
75  Bignum n(10);
76  cout << "Init ten: " << n << endl;
77  ASSERT_EQ(n.str(), "10");
78 }
79 
80 TEST(bignum_test, copy_construct)
81 {
82  Bignum a(10);
83  Bignum b(a);
84  cout << "copy_construct a=" << a << " b=" << b << endl;
85  ASSERT_TRUE(a == 10);
86  ASSERT_TRUE(b == 10);
87 }
88 
89 TEST(bignum_test, copy_op)
90 {
91  Bignum a(10);
92  Bignum b;
93  b = a;
94  cout << "copy_op a=" << a << " b=" << b << endl;
95  ASSERT_TRUE(a == 10);
96  ASSERT_TRUE(b == 10);
97 }
98 
99 TEST(bignum_test, move_construct)
100 {
101  Bignum a(10);
102  Bignum b = std::move(a);
103  cout << "move_construct a=" << a << " b=" << b << endl;
104  ASSERT_TRUE(b == 10);
105  ASSERT_FALSE(a == 10);
106 }
107 
108 TEST(bignum_test, move_opt)
109 {
110  Bignum a(10);
111  Bignum b;
112  b = std::move(a);
113  cout << "move_op a=" << a << " b=" << b << endl;
114  ASSERT_TRUE(b == 10);
115  ASSERT_FALSE(a == 10);
116 }
117 
118 TEST(bignum_test, print_hex)
119 {
120  Bignum a(1 << 20);
121  a += 15*16;
122  a = -a;
123  cout << "print_hex " << std::hex << a << std::dec << endl;
124  ASSERT_EQ(a.str(true), "-1000f0");
125 
126 }
127 
128 TEST(bignum_test, print_dec)
129 {
130  Bignum a(1 << 20);
131  a = -a;
132  cout << "print_dec " << a << endl;
133  ASSERT_EQ(a.str(), "-1048576");
134 }
135 
136 TEST(bignum_test, compare)
137 {
138  Bignum one(1), ten(10);
139  ASSERT_TRUE(one == one);
140  ASSERT_TRUE(one >= one);
141  ASSERT_TRUE(one <= one);
142  ASSERT_FALSE(one < one);
143  ASSERT_FALSE(one > one);
144  ASSERT_TRUE(one != ten);
145  ASSERT_TRUE(one < ten);
146  ASSERT_TRUE(one <= ten);
147  ASSERT_TRUE(ten > one);
148  ASSERT_TRUE(ten >= one);
149  ASSERT_TRUE(one == 1);
150  ASSERT_TRUE(one >= 1);
151  ASSERT_TRUE(one <= 1);
152  ASSERT_FALSE(one < 1);
153  ASSERT_FALSE(one > 1);
154  ASSERT_TRUE(one != 10);
155  ASSERT_TRUE(one < 10);
156  ASSERT_TRUE(one <= 10);
157  ASSERT_TRUE(ten > 1);
158  ASSERT_TRUE(ten >= 1);
159 }
160 
161 
162 TEST(bignum_test, shift)
163 {
164  Bignum one(1);
165  Bignum sft(1<<7);
166  Bignum a(1);
167 
168  a <<= 7;
169  ASSERT_EQ(a, sft);
170  Bignum b = one << 7;
171  ASSERT_EQ(b, sft);
172  Bignum c(sft);
173  c >>= 7;
174  ASSERT_EQ(c, one);
175  Bignum d = sft >> 7;
176  ASSERT_EQ(d, one);
177  Bignum shiftaway(123456);
178  shiftaway >>= shiftaway.width();
179  ASSERT_EQ(shiftaway, 0);
180 }
181 
182 TEST(bignum_test, big_shift)
183 {
184  Bignum n(0xffff0000);
185  cout << "bigshift in : " << std::hex << n << std::dec << " width: " << n.width() << endl;
186  n <<= 47;
187  auto hx = n.str(true);
188  cout << "bigshift <<47: " << std::hex << hx << std::dec << " width: " << n.width() << endl;
189  ASSERT_EQ(n.width(), 32+47);
190  ASSERT_EQ(hx, "7fff8000000000000000");
191  n >>= 9;
192  hx = n.str(true);
193  cout << "bigshift >>9: " << std::hex << hx << std::dec << " width: " << n.width() << endl;
194  ASSERT_EQ(hx, "3fffc0000000000000");
195  ASSERT_EQ(n.width(), 32+47-9);
196 }
197 
198 TEST(bignum_test, incdec)
199 {
200  Bignum a;
201  a = 1;
202  ASSERT_EQ(++a, 2);
203  ASSERT_EQ(a, 2);
204  ASSERT_EQ(a++, 2);
205  ASSERT_EQ(a, 3);
206  ASSERT_EQ(--a, 2);
207  ASSERT_EQ(a, 2);
208  ASSERT_EQ(a--, 2);
209  ASSERT_EQ(a, 1);
210 }
211 
212 TEST(bignum_test, arith)
213 {
214  Bignum zero, one(1), two(2), three(3), nine(9), ten(10), eleven(11), twelve(12), hund(100);
215 
216  Bignum a = one * ten;
217  ASSERT_EQ(a, ten);
218  a /= ten;
219  ASSERT_EQ(a, one);
220 
221  a = nine / three;
222  ASSERT_EQ(a, three);
223  a *= three;
224  ASSERT_EQ(a, nine);
225 
226  a = nine + one;
227  ASSERT_EQ(a, ten);
228  a -= one;
229  ASSERT_EQ(a, nine);
230 
231  a = ten - one;
232  ASSERT_EQ(a, nine);
233  a += one;
234  ASSERT_EQ(a, ten);
235 
236  a = hund % nine;
237  ASSERT_EQ(a, one);
238 
239  a = hund;
240  Bignum rem;
241  a.div(nine, &rem);
242  ASSERT_EQ(a, eleven);
243  ASSERT_EQ(rem, one);
244 
245  a = exp(nine, zero);
246  ASSERT_EQ(a, one);
247 
248  a = exp(ten, two);
249  ASSERT_EQ(a, hund);
250  a = ten;
251  a.exp(two);
252  ASSERT_EQ(a, hund);
253 
254  a = gcd(nine, twelve);
255  ASSERT_EQ(a, three);
256  a = nine;
257  a.gcd(twelve);
258  ASSERT_EQ(a, three);
259 }
260 
261 TEST(bignum_test, arith_num)
262 {
263  Bignum one(1), two(2), three(3), nine(9), ten(10), eleven(11), twelve(12), hund(100);
264 
265  Bignum a = one * 10;
266  ASSERT_EQ(a, 10);
267  a /= 10;
268  ASSERT_EQ(a, 1);
269 
270  a = nine / 3;
271  ASSERT_EQ(a, 3);
272  a *= 3;
273  ASSERT_EQ(a, 9);
274 
275  a = nine + 1;
276  ASSERT_EQ(a, 10);
277  a -= 1;
278  ASSERT_EQ(a, 9);
279 
280  a = ten - 1;
281  ASSERT_EQ(a, 9);
282  a += 1;
283  ASSERT_EQ(a, 10);
284 
285  a = hund % 9;
286  ASSERT_EQ(a, 1);
287 
288  a = hund;
289  Bignum rem;
290  a.div(nine, &rem);
291  ASSERT_EQ(a, 11);
292  ASSERT_EQ(rem, 1);
293 
294  a = exp(nine, 0);
295  ASSERT_EQ(a, 1);
296 
297  a = exp(ten, 2);
298  ASSERT_EQ(a, 100);
299  a = ten;
300  a.exp(2);
301  ASSERT_EQ(a, 100);
302 
303  a = gcd(nine, 12);
304  ASSERT_EQ(a, 3);
305  a = nine;
306  a.gcd(12);
307  ASSERT_EQ(a, 3);
308 }
309 
310 TEST(bignum_test, prime)
311 {
312  Bignum p1(217645177);
313  Bignum p2(236887691);
314  ASSERT_TRUE(p1.is_prime());
315  ASSERT_TRUE(p2.is_prime());
316  Bignum c = p1*p2;
317  ASSERT_FALSE(c.is_prime());
318  cout << "Prime 1: " << p1 << endl;
319  cout << "Prime 2: " << p2 << endl;
320  cout << "Composite: " << c << endl;
321 
322  // find the first hundred primes (see primes.utm.edu)
323  int count = 0;
324  Bignum b;
325  for (int t = 2; t <= 541; t++)
326  {
327  b = t;
328  if (b.is_prime())
329  count++;
330  }
331  cout << "First hundred primes found: " << count << endl;
332  EXPECT_EQ(count, 100);
333 }
334 
335 TEST(bignum_test, gcd)
336 {
337  Bignum p1(2829604451);
338  cout << "p1: " << std::hex << p1 << endl;
339  ASSERT_TRUE(p1.is_prime());
340  Bignum p2(1787494861);
341  cout << "p2: " << std::hex << p2 << endl;
342  ASSERT_TRUE(p2.is_prime());
343  Bignum p3(3954380029);
344  cout << "p3: " << std::hex << p3 << endl;
345  ASSERT_TRUE(p3.is_prime());
346  Bignum p12 = p1 * p2;
347  cout << "p1 * p2: " << std::hex << p12 << endl;
348  Bignum p13 = p1 * p3;
349  cout << "p1 * p3: " << std::hex << p13 << endl;
350  Bignum gcd(p12);
351  gcd.gcd(p13);
352  cout << "gcd: " << std::hex << gcd << endl;
353  ASSERT_EQ(p1, gcd);
354 }
355 
356 TEST(bignum_test, bintest)
357 {
358  uint8_t octval[] = "\x12\x34\x56\x78\x9a\xbc\xde\xf0"
359  "\xcc";
360 
361  Bignum b(octval, sizeof(octval));
362  cout << "bin: " << std::hex << b << " " << std::dec << b << endl;
363  Bignum t;
364  t = 0x12345678;
365  t <<= 32;
366  t += 0x9abcdef0;
367  t <<= 16;
368  t += 0xcc00;
369  cout << "tst: " << std::hex << t << " " << std::dec << t << endl;
370  ASSERT_EQ(b, t);
371 }
372 
373 TEST(bignum_test, generate_rand)
374 {
375  auto gen = [](int w, bool s=false, bool o=false)
376  {
377  Bignum p;
378  p.gen_rand(w, s, o);
379  cout << "genrand request width="<<w<<" strong="<<s<<" odd="<<o<<": " << std::hex << p << std::dec << " width: " << p.width() << endl;
380  ASSERT_LE(p.width(), w);
381  if (s)
382  {
383  ASSERT_TRUE(p.is_bit_set(w-1));
384  ASSERT_TRUE(p.is_bit_set(w-2));
385  }
386  if (o)
387  {
388  // must be odd
389  ASSERT_TRUE(p.is_bit_set(0));
390  }
391 
392  };
393  for (int i = 1; i <= 128; i++) gen(i);
394  for (int i = 1; i <= 128; i++) gen(i, false, true);
395  for (int i = 2; i <= 128; i++) gen(i, true, false);
396  for (int i = 2; i <= 128; i++) gen(i, true, true);
397 }
398 
399 // https://prime-numbers.info/list/first-1000-primes
400 // https://prime-numbers.info/list/safe-primes (this library does not generate them, but will check here)
401 TEST(bignum_test, generate_primes)
402 {
403  auto gen = [](int w)
404  {
405  Bignum p;
406  p.gen_prime(w);
407  cout << "genprime width=" << p.width() << ": " << p;
408  ASSERT_TRUE(p.is_prime());
409  ASSERT_EQ(p.width(), w);
410 
411  p -= 1;
412  p /= 2;
413  if (p.is_prime())
414  {
415  cout << " SAFE";
416  }
417  cout << endl;
418  };
419  for (int i = 2; i <= 128; i++) gen(i);
420 }
421 
422 TEST(bignum_test, bit_test_set_clear)
423 {
424  Bignum w32(3);
425  cout << "init w32 n="<<std::hex<<w32<<std::dec<<" width: "<< w32.width()<<std::endl;
426  ASSERT_EQ(w32.width(), 2);
427  ASSERT_TRUE(w32.is_bit_set(0));
428  ASSERT_TRUE(w32.is_bit_set(1));
429  ASSERT_FALSE(w32.is_bit_set(2));
430  w32.set_bit(30);
431  w32.set_bit(31);
432  cout << "set bit 30-31 n="<<std::hex<<w32<<std::dec<<" width: "<< w32.width()<<std::endl;
433  ASSERT_EQ(w32, 0xc0000003);
434  ASSERT_EQ(w32.width(), 32);
435  ASSERT_TRUE(w32.is_bit_set(0));
436  ASSERT_TRUE(w32.is_bit_set(1));
437  ASSERT_FALSE(w32.is_bit_set(2));
438  ASSERT_TRUE(w32.is_bit_set(30));
439  ASSERT_TRUE(w32.is_bit_set(31));
440  ASSERT_FALSE(w32.is_bit_set(32));
441  w32.clear_bit(30);
442  w32.clear_bit(31);
443  cout << "clr w32 30-31 n="<<std::hex<<w32<<std::dec<<" width: "<< w32.width()<<std::endl;
444  ASSERT_EQ(w32, 0x3);
445  ASSERT_EQ(w32.width(), 2);
446  ASSERT_TRUE(w32.is_bit_set(0));
447  ASSERT_TRUE(w32.is_bit_set(1));
448  ASSERT_FALSE(w32.is_bit_set(2));
449  ASSERT_FALSE(w32.is_bit_set(30));
450  ASSERT_FALSE(w32.is_bit_set(31));
451  ASSERT_FALSE(w32.is_bit_set(32));
452 
453  Bignum w64(3);
454  w64 <<= 32;
455  cout << "init w64 n="<<std::hex<<w64<<std::dec<<" width: "<< w64.width()<<std::endl;
456  ASSERT_EQ(w64.width(), 34);
457  ASSERT_TRUE(w64.is_bit_set(32));
458  ASSERT_TRUE(w64.is_bit_set(33));
459  ASSERT_FALSE(w64.is_bit_set(34));
460  w64.set_bit(62);
461  w64.set_bit(63);
462  cout << "set bit 62-63 n="<<std::hex<<w64<<std::dec<<" width: "<< w64.width()<<std::endl;
463  ASSERT_EQ(w64.width(), 64);
464  ASSERT_TRUE(w64.is_bit_set(32));
465  ASSERT_TRUE(w64.is_bit_set(33));
466  ASSERT_FALSE(w64.is_bit_set(34));
467  ASSERT_TRUE(w64.is_bit_set(62));
468  ASSERT_TRUE(w64.is_bit_set(63));
469  ASSERT_FALSE(w64.is_bit_set(64));
470  w64.clear_bit(62);
471  w64.clear_bit(63);
472  cout << "clr bit 62-63 n="<<std::hex<<w64<<std::dec<<" width: "<< w64.width()<<std::endl;
473  ASSERT_EQ(w64.width(), 34);
474  ASSERT_TRUE(w64.is_bit_set(32));
475  ASSERT_TRUE(w64.is_bit_set(33));
476  ASSERT_FALSE(w64.is_bit_set(34));
477  ASSERT_FALSE(w64.is_bit_set(62));
478  ASSERT_FALSE(w64.is_bit_set(63));
479  ASSERT_FALSE(w64.is_bit_set(64));
480 }
481 
482 TEST(bignum_test, bit_ops_and)
483 {
484  auto doit = [](int wa, int wb){
485  Bignum a(1);
486  a <<= wa-1;
487  cout << "bit ops a="<<a.str(true)<<" width: "<< a.width()<<std::endl;
488  Bignum b(1);
489  b <<= wb-1;
490  cout << "bit ops b="<<b.str(true)<<" width: "<< b.width()<<std::endl;
491  Bignum c;
492  c = a + b;
493  cout << "bit ops c="<<c.str(true)<<" width: "<< c.width()<<std::endl;
494  Bignum x;
495  x = a & b;
496  cout << "bit ops a&b="<<x.str(true)<<" width: "<< x.width()<<std::endl;
497  ASSERT_EQ(x, 0);
498  x = a & c;
499  cout << "bit ops a&c="<<x.str(true)<<" width: "<< x.width()<<std::endl;
500  ASSERT_EQ(x, a);
501  x = b & c;
502  cout << "bit ops b&c="<<x.str(true)<<" width: "<< x.width()<<std::endl;
503  ASSERT_EQ(x, b);
504  x = c;
505  x &= a;
506  ASSERT_EQ(x, a);
507  x = c;
508  x &= b;
509  ASSERT_EQ(x, b);
510  };
511  doit(1, 2);
512  doit(30, 31);
513  doit(31, 32);
514  doit(32, 33);
515 
516  Bignum a(1), b(2);
517  ASSERT_EQ(a&3, 1);
518  ASSERT_EQ(b&3, 2);
519  Bignum x = a&3;
520  ASSERT_EQ(x, 1);
521  x = b&3;
522  ASSERT_EQ(x, 2);
523 }
524 
525 TEST(bignum_test, bit_ops_or)
526 {
527  auto doit = [](int wa, int wb){
528  Bignum a(1);
529  a <<= wa-1;
530  Bignum b(1);
531  b <<= wb-1;
532  Bignum c;
533  c = a + b;
534  Bignum x;
535  x = a | b;
536  ASSERT_EQ(x, c);
537  x = a | c;
538  ASSERT_EQ(x, c);
539  x = b | c;
540  ASSERT_EQ(x, c);
541  x = c;
542  x |= a;
543  ASSERT_EQ(x, c);
544  x = c;
545  x |= b;
546  ASSERT_EQ(x, c);
547  };
548  doit(1, 2);
549  doit(30, 31);
550  doit(31, 32);
551  doit(32, 33);
552 
553  Bignum a(1), b(2);
554  ASSERT_EQ(a|3, 3);
555  ASSERT_EQ(b|3, 3);
556  Bignum x = a|3;
557  ASSERT_EQ(x, 3);
558  x = b|3;
559  ASSERT_EQ(x, 3);
560 }
561 
562 TEST(bignum_test, bit_ops_xor)
563 {
564  auto doit = [](int wa, int wb){
565  Bignum a(1);
566  a <<= wa-1;
567  Bignum b(1);
568  b <<= wb-1;
569  Bignum c;
570  c = a + b;
571  Bignum x;
572  x = a ^ b;
573  ASSERT_EQ(x, c);
574  x = a ^ c;
575  ASSERT_EQ(x, b);
576  x = b ^ c;
577  ASSERT_EQ(x, a);
578  x = c;
579  x ^= a;
580  ASSERT_EQ(x, b);
581  x = c;
582  x ^= b;
583  ASSERT_EQ(x, a);
584  };
585  doit(1, 2);
586  doit(30, 31);
587  doit(31, 32);
588  doit(32, 33);
589 
590  Bignum a(1), b(2), c(3);
591  ASSERT_EQ(c^1, 2);
592  ASSERT_EQ(c^2, 1);
593  Bignum x = c^1;
594  ASSERT_EQ(x, 2);
595  x = c^2;
596  ASSERT_EQ(x, 1);
597 }
598 
599 TEST(bignum_test, bit_ops_not)
600 {
601  Bignum a=1, b=0;
602  ASSERT_EQ(~a, b);
603  ASSERT_EQ(~b, a);
604  a=0xffff0000;
605  b=0xffff;
606  ASSERT_EQ(~a, b);
607  a <<= 16;
608  b=0xffffffff;
609  ASSERT_EQ(~a, b);
610 }
611 
612 TEST(bignum_test, width)
613 {
614  Bignum a(0xf);
615  ASSERT_EQ(a.width(), 4);
616  auto p = [](int shift)
617  {
618  Bignum b = 1;
619  b <<= shift;
620  cout << "width of " << b.str(true) << ": " << b.width() << endl;
621  ASSERT_EQ(b.width(), shift+1);
622  };
623  Bignum b = 0;
624  cout << "width of " << b.str(true) << ": " << b.width() << endl;
625  ASSERT_EQ(b.width(), 1);
626  p(0);
627  p(1);
628  p(30);
629  p(31);
630  p(32);
631  p(33);
632  p(147);
633 }
634 
635 TEST(bignum_test, negative)
636 {
637  Bignum a;
638 
639  ASSERT_FALSE(a.is_negative());
640  a = 128;
641  ASSERT_FALSE(a.is_negative());
642  a = -a;
643  ASSERT_TRUE(a.is_negative());
644 }
645 
646 TEST(bignum_test, get)
647 {
648  Bignum a;
649  vector<char> v;
650 
651  auto get_test = [&]
652  {
653  v.resize(a.len());
654  a.get((void*)v.data(), v.size());
655  cout << "a = " << a << " get hex " << Hex::bin_to_hexstr(v, " ") << endl;
656  };
657 
658  get_test();
659  a = 127;
660  get_test();
661  ASSERT_EQ(v, vector<char>{'\x7f'});
662  a = -a;
663  ASSERT_THROW(get_test(), runtime_error);
664  a = 128;
665  get_test();
666  ASSERT_EQ(v, vector<char>{'\x80'});
667 }
668 
669 TEST(bignum_test, get_2sc)
670 {
671  Bignum a;
672  vector<char> v;
673 
674  auto get_test = [&](vector<char> chk)
675  {
676  cout << "a = " << a << " len_2sc() = " << a.len_2sc() << endl;
677  v.resize(a.len_2sc());
678  a.get_2sc((void*)v.data(), v.size());
679  cout << "a = " << a << " get hex " << Hex::bin_to_hexstr(v, " ") << endl;
680  ASSERT_EQ(v, chk);
681  };
682 /*
683  128 0000 0000 1000 0000 - 00 80
684  127 0111 1111 - 7f
685  -1 1111 1111 - ff
686  -127 1000 0001 - 81
687  -128 1000 0000 - 80
688  -129 1111 1111 0111 1111 - ff 7f
689 */
690 
691  get_test({'\x00'});
692  a = 128;
693  get_test({'\x00','\x80'});
694  a = 127;
695  get_test({'\x7f'});
696  a = 1;
697  a = -a;
698  get_test({'\xff'});
699  a = 127;
700  a = -a;
701  get_test({'\x81'});
702  a = 128;
703  a = -a;
704  get_test({'\x80'});
705  a = 129;
706  a = -a;
707  get_test({'\xff','\x7f'});
708 }
Big number arithmetic.
scc::crypto::Bignum exp(const scc::crypto::Bignum &, const scc::crypto::Bignum &)
Exponent helper.
scc::crypto::Bignum gcd(const scc::crypto::Bignum &, const scc::crypto::Bignum &)
Greatest common divisor helper.
Big number.
Definition: bignum.h:59
bool is_bit_set(int) const
Is the bit set?
void get(void *, int) const
Octal (byte) output of a positive number.
std::string str(bool=false) const
Return a string representation in decimal or hex.
void exp(const Bignum &)
Exponent a^b.
void gcd(const Bignum &)
Greatest common divisor for a and b.
int len() const
Required length of get() output in bytes.
void get_2sc(void *, int) const
Octal (byte) output, in two's complement form.
int width() const
Number of significant bits.
int len_2sc() const
Get the length of octal output in twos complement form.
void div(const Bignum &, Bignum *)
Divide a/b.
void gen_prime(int)
Generate a random pseudo-prime number of the specified bit width.
bool is_prime(int=-1)
Perform a Miller-Rabin prime test to test primality.
void gen_rand(int, bool=false, bool=false)
Generate a random number of the specified bit width.
bool is_negative() const
Is negative?
Binary to hex string converter.
TEST(inet_example, client_server_stream_test)
[Inet client server]
Definition: inet.cc:521