summaryrefslogtreecommitdiff
path: root/validat0.cpp
diff options
context:
space:
mode:
authorJeffrey Walton <noloader@gmail.com>2018-03-25 16:44:51 -0400
committerJeffrey Walton <noloader@gmail.com>2018-03-25 16:44:51 -0400
commitb096401b7ccd44c706c38b3ae8e9d83b783996b6 (patch)
treed6ec954b312ea1256a0bff3ed3bd51aee30b7718 /validat0.cpp
parenta6c9419059c5cb58f90c28616235ea8c4580ebfd (diff)
downloadcryptopp-git-b096401b7ccd44c706c38b3ae8e9d83b783996b6.tar.gz
Add additional Integer class tests
Diffstat (limited to 'validat0.cpp')
-rw-r--r--validat0.cpp125
1 files changed, 116 insertions, 9 deletions
diff --git a/validat0.cpp b/validat0.cpp
index bb22a604..2eaae8dd 100644
--- a/validat0.cpp
+++ b/validat0.cpp
@@ -3136,6 +3136,7 @@ bool TestIntegerOps()
{
std::cout << "\nTesting Integer operations...\n\n";
bool pass=true, result;
+ RandomNumberGenerator& prng = GlobalRNG();
// ****************************** DivideByZero ******************************
@@ -3202,7 +3203,7 @@ bool TestIntegerOps()
if (!pass)
std::cout << "FAILED: Integer Doubled\n";
- // ****************************** Integer::Double ******************************
+ // ****************************** Integer::Square ******************************
try {
Integer x = Integer::Two().Squared();
@@ -3230,7 +3231,33 @@ bool TestIntegerOps()
std::cout << "FAILED:";
std::cout << " Squaring operations\n";
- // ****************************** Integer::Modulo and Integer::InverseMod ******************************
+ // ****************************** Integer::GCD ******************************
+
+ {
+ for (unsigned int i=0; i<128; ++i)
+ {
+ Integer x, y;
+ AlgorithmParameters params =
+ MakeParameters("BitLength", 256)("RandomNumberType", Integer::PRIME);
+
+ x.GenerateRandom(prng, params);
+ y.GenerateRandom(prng, params);
+
+ if (x != y)
+ {
+ result = (Integer::Gcd(x,y) == 1);
+ pass = result && pass;
+ }
+ }
+
+ if (pass)
+ std::cout << "passed:";
+ else
+ std::cout << "FAILED:";
+ std::cout << " GCD operations\n";
+ }
+
+ // ******************** Integer::Modulo and Integer::InverseMod ********************
{
// http://github.com/weidai11/cryptopp/issues/602
@@ -3243,7 +3270,6 @@ bool TestIntegerOps()
std::cout << "FAILED: InverseMod operation\n";
}
- RandomNumberGenerator& prng = GlobalRNG();
for (unsigned int i=0; i<128+64; ++i)
{
Integer a(prng, 1024), m(prng, 1024);
@@ -3387,15 +3413,96 @@ bool TestIntegerOps()
pass = result && pass;
if (!result)
std::cout << "FAILED: Power2 (1) operation\n";
+ }
- if (pass)
- std::cout << "passed:";
- else
- std::cout << "FAILED:";
- std::cout << " Power2 operations\n";
+ for (unsigned int i=0; i<128; i+=2)
+ {
+ Integer b = 2, m(prng, 2048);
+
+ Integer x = EuclideanDomainOf<Integer>().Exponentiate(b, i) % m;
+ Integer y = Integer::Power2(i) % m;
+
+ pass = (x == y) && pass;
+ if (!result)
+ std::cout << "FAILED: Exponentiation operation\n";
}
- // ****************************** Integer::Power2 ******************************
+ if (pass)
+ std::cout << "passed:";
+ else
+ std::cout << "FAILED:";
+ std::cout << " Power2 operations\n";
+
+ // ****************************** Integer Exponentiation ******************************
+
+ // Be careful with EuclideanDomainOf<Integer>().Exponentiate(). It can easily consume
+ // all machine memory because it is an exponentiation without a modular reduction.
+
+ {
+ word32 m = prng.GenerateWord32();
+ Integer z = Integer::Zero();
+ Integer x = a_exp_b_mod_c(z, z, m);
+ Integer y = EuclideanDomainOf<Integer>().Exponentiate(0, 0) % m;
+
+ pass = (x == y) && (x == 1) && pass;
+ if (!result)
+ std::cout << "FAILED: Exponentiation operation\n";
+ }
+
+ for (unsigned int i=0; i<128; i+=2)
+ {
+ Integer b = 0, m(prng, 2048);
+
+ Integer x = a_exp_b_mod_c(b, i, m);
+ Integer y = EuclideanDomainOf<Integer>().Exponentiate(b, i) % m;
+
+ pass = (x == y) && pass;
+ if (!result)
+ std::cout << "FAILED: Exponentiation operation\n";
+ }
+
+ for (unsigned int i=0; i<128; i+=2)
+ {
+ Integer b = 1, m(prng, 2048);
+
+ Integer x = a_exp_b_mod_c(b, i, m);
+ Integer y = EuclideanDomainOf<Integer>().Exponentiate(b, i) % m;
+
+ pass = (x == y) && pass;
+ if (!result)
+ std::cout << "FAILED: Exponentiation operation\n";
+ }
+
+ for (unsigned int i=0; i<128; i+=2)
+ {
+ Integer b = 2, m(prng, 2048);
+
+ Integer x = a_exp_b_mod_c(b, i, m);
+ Integer y = EuclideanDomainOf<Integer>().Exponentiate(b, i) % m;
+
+ pass = (x == y) && pass;
+ if (!result)
+ std::cout << "FAILED: Exponentiation operation\n";
+ }
+
+ // Run the exponent 0 to 24
+ for (unsigned int i=0; i<24; ++i)
+ {
+ Integer b(prng, 32), m(prng, 2048);
+
+ Integer x = a_exp_b_mod_c(b, i, m);
+ Integer y = EuclideanDomainOf<Integer>().Exponentiate(b, i) % m;
+
+ pass = (x == y) && pass;
+ if (!result)
+ std::cout << "FAILED: Exponentiation operation\n";
+ }
+
+ if (pass)
+ std::cout << "passed:";
+ else
+ std::cout << "FAILED:";
+ std::cout << " Exponentiation operations\n";
return pass;
}