summaryrefslogtreecommitdiff
path: root/validat0.cpp
diff options
context:
space:
mode:
authorJeffrey Walton <noloader@gmail.com>2018-03-25 01:57:12 -0400
committerJeffrey Walton <noloader@gmail.com>2018-03-25 01:57:12 -0400
commita8d40ee07faa39c8c1c43cc645d9c04a5be02f32 (patch)
tree307b893661466fb27d7f84ad23aff79632a5ac6b /validat0.cpp
parenteb0d040786ffd41e517f51a6878e37b1de8ee334 (diff)
downloadcryptopp-git-a8d40ee07faa39c8c1c43cc645d9c04a5be02f32.tar.gz
Add additional InverseMod tests
This commit adds tests using 'word' moduli
Diffstat (limited to 'validat0.cpp')
-rw-r--r--validat0.cpp28
1 files changed, 26 insertions, 2 deletions
diff --git a/validat0.cpp b/validat0.cpp
index d55f9ee4..32d72bc8 100644
--- a/validat0.cpp
+++ b/validat0.cpp
@@ -2496,7 +2496,6 @@ bool TestHuffmanCodes()
bool TestIntegerBitops()
{
std::cout << "\nTesting Integer bit operations...\n\n";
- bool pass;
struct Bitops_TestTuple
{
@@ -3202,7 +3201,7 @@ bool TestIntegerOps()
Integer a("0x2F0500010000018000000000001C1C000000000000000A000B0000000000000000000000000000FDFFFFFF00000000");
Integer b("0x3D2F050001");
- result = (0x3529E4FEBC == a.InverseMod(b));
+ result = (Integer("0x3529E4FEBC") == a.InverseMod(b));
pass = result && pass;
if (!result)
std::cout << "FAILED: InverseMod operation\n";
@@ -3306,6 +3305,31 @@ bool TestIntegerOps()
std::cout << "FAILED: InverseMod operation\n";
}
+ for (unsigned int i=0; i<128; ++i)
+ {
+ Integer a(prng, 4096);
+ word32 m = prng.GenerateWord32();
+
+ a++; // make non-0
+ if (m == 0) m++;
+
+ // Avoid the conversion from word to long
+ Integer am = a % Integer(Integer::POSITIVE, m);
+
+ Integer x = Integer(Integer::POSITIVE, a.InverseMod(m));
+ Integer y = Integer(Integer::POSITIVE, am.InverseMod(m));
+ Integer z = Integer(Integer::POSITIVE, (a * y).Modulo(m));
+
+ if (GCD(a,m) == 1) // coprime?
+ result = (x == y) && (z == 1) && (a_times_b_mod_c(a, x, m) == 1);
+ else
+ result = (x == y);
+
+ pass = result && pass;
+ if (!result)
+ std::cout << "FAILED: InverseMod operation\n";
+ }
+
if (pass)
std::cout << "passed:";
else