32 #include <gtest/gtest.h>
46 using std::runtime_error;
50 TEST(bignum_test, init)
53 cout <<
"Init: " << n << endl;
54 ASSERT_EQ(n.
str(),
"0");
57 TEST(bignum_test, set_zero)
61 cout <<
"Zero: " << n << endl;
62 ASSERT_EQ(n.
str(),
"0");
65 TEST(bignum_test, set_one)
69 cout <<
"One: " << n << endl;
70 ASSERT_EQ(n.
str(),
"1");
73 TEST(bignum_test, init_ten)
76 cout <<
"Init ten: " << n << endl;
77 ASSERT_EQ(n.str(),
"10");
80 TEST(bignum_test, copy_construct)
84 cout <<
"copy_construct a=" << a <<
" b=" << b << endl;
89 TEST(bignum_test, copy_op)
94 cout <<
"copy_op a=" << a <<
" b=" << b << endl;
99 TEST(bignum_test, move_construct)
103 cout <<
"move_construct a=" << a <<
" b=" << b << endl;
104 ASSERT_TRUE(b == 10);
105 ASSERT_FALSE(a == 10);
108 TEST(bignum_test, move_opt)
113 cout <<
"move_op a=" << a <<
" b=" << b << endl;
114 ASSERT_TRUE(b == 10);
115 ASSERT_FALSE(a == 10);
118 TEST(bignum_test, print_hex)
123 cout <<
"print_hex " << std::hex << a << std::dec << endl;
124 ASSERT_EQ(a.str(
true),
"-1000f0");
128 TEST(bignum_test, print_dec)
132 cout <<
"print_dec " << a << endl;
133 ASSERT_EQ(a.str(),
"-1048576");
136 TEST(bignum_test, compare)
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);
162 TEST(bignum_test, shift)
178 shiftaway >>= shiftaway.width();
179 ASSERT_EQ(shiftaway, 0);
182 TEST(bignum_test, big_shift)
185 cout <<
"bigshift in : " << std::hex << n << std::dec <<
" width: " << n.width() << endl;
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");
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);
198 TEST(bignum_test, incdec)
212 TEST(bignum_test, arith)
214 Bignum zero, one(1), two(2), three(3), nine(9), ten(10), eleven(11), twelve(12), hund(100);
242 ASSERT_EQ(a, eleven);
254 a =
gcd(nine, twelve);
261 TEST(bignum_test, arith_num)
263 Bignum one(1), two(2), three(3), nine(9), ten(10), eleven(11), twelve(12), hund(100);
310 TEST(bignum_test, prime)
314 ASSERT_TRUE(p1.is_prime());
315 ASSERT_TRUE(p2.is_prime());
318 cout <<
"Prime 1: " << p1 << endl;
319 cout <<
"Prime 2: " << p2 << endl;
320 cout <<
"Composite: " << c << endl;
325 for (
int t = 2; t <= 541; t++)
331 cout <<
"First hundred primes found: " << count << endl;
332 EXPECT_EQ(count, 100);
338 cout <<
"p1: " << std::hex << p1 << endl;
339 ASSERT_TRUE(p1.is_prime());
341 cout <<
"p2: " << std::hex << p2 << endl;
342 ASSERT_TRUE(p2.is_prime());
344 cout <<
"p3: " << std::hex << p3 << endl;
345 ASSERT_TRUE(p3.is_prime());
347 cout <<
"p1 * p2: " << std::hex << p12 << endl;
349 cout <<
"p1 * p3: " << std::hex << p13 << endl;
352 cout <<
"gcd: " << std::hex <<
gcd << endl;
356 TEST(bignum_test, bintest)
358 uint8_t octval[] =
"\x12\x34\x56\x78\x9a\xbc\xde\xf0"
361 Bignum b(octval,
sizeof(octval));
362 cout <<
"bin: " << std::hex << b <<
" " << std::dec << b << endl;
369 cout <<
"tst: " << std::hex << t <<
" " << std::dec << t << endl;
373 TEST(bignum_test, generate_rand)
375 auto gen = [](
int w,
bool s=
false,
bool o=
false)
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);
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);
401 TEST(bignum_test, generate_primes)
407 cout <<
"genprime width=" << p.
width() <<
": " << p;
409 ASSERT_EQ(p.
width(), w);
419 for (
int i = 2; i <= 128; i++) gen(i);
422 TEST(bignum_test, bit_test_set_clear)
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));
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));
443 cout <<
"clr w32 30-31 n="<<std::hex<<w32<<std::dec<<
" width: "<< w32.width()<<std::endl;
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));
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));
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));
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));
482 TEST(bignum_test, bit_ops_and)
484 auto doit = [](
int wa,
int wb){
487 cout <<
"bit ops a="<<a.
str(
true)<<
" width: "<< a.
width()<<std::endl;
490 cout <<
"bit ops b="<<b.
str(
true)<<
" width: "<< b.
width()<<std::endl;
493 cout <<
"bit ops c="<<c.
str(
true)<<
" width: "<< c.
width()<<std::endl;
496 cout <<
"bit ops a&b="<<x.
str(
true)<<
" width: "<< x.
width()<<std::endl;
499 cout <<
"bit ops a&c="<<x.
str(
true)<<
" width: "<< x.
width()<<std::endl;
502 cout <<
"bit ops b&c="<<x.
str(
true)<<
" width: "<< x.
width()<<std::endl;
525 TEST(bignum_test, bit_ops_or)
527 auto doit = [](
int wa,
int wb){
562 TEST(bignum_test, bit_ops_xor)
564 auto doit = [](
int wa,
int wb){
599 TEST(bignum_test, bit_ops_not)
612 TEST(bignum_test, width)
615 ASSERT_EQ(a.
width(), 4);
616 auto p = [](
int shift)
620 cout <<
"width of " << b.
str(
true) <<
": " << b.
width() << endl;
621 ASSERT_EQ(b.
width(), shift+1);
624 cout <<
"width of " << b.
str(
true) <<
": " << b.
width() << endl;
625 ASSERT_EQ(b.
width(), 1);
635 TEST(bignum_test, negative)
646 TEST(bignum_test, get)
654 a.
get((
void*)v.data(), v.size());
655 cout <<
"a = " << a <<
" get hex " << Hex::bin_to_hexstr(v,
" ") << endl;
661 ASSERT_EQ(v, vector<char>{
'\x7f'});
663 ASSERT_THROW(get_test(), runtime_error);
666 ASSERT_EQ(v, vector<char>{
'\x80'});
669 TEST(bignum_test, get_2sc)
674 auto get_test = [&](vector<char> chk)
676 cout <<
"a = " << a <<
" len_2sc() = " << a.
len_2sc() << endl;
678 a.
get_2sc((
void*)v.data(), v.size());
679 cout <<
"a = " << a <<
" get hex " << Hex::bin_to_hexstr(v,
" ") << endl;
693 get_test({
'\x00',
'\x80'});
707 get_test({
'\xff',
'\x7f'});
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.
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]