Add additional InverseMod tests

This commit adds tests using 'word' moduli
pull/605/head
Jeffrey Walton 2018-03-25 01:57:12 -04:00
parent eb0d040786
commit a8d40ee07f
No known key found for this signature in database
GPG Key ID: B36AB348921B1838
1 changed files with 26 additions and 2 deletions

View File

@ -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