Skip to content

Commit

Permalink
Add additional InverseMod tests
Browse files Browse the repository at this point in the history
This commit adds tests using 'word' moduli
  • Loading branch information
noloader committed Mar 25, 2018
1 parent eb0d040 commit a8d40ee
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions validat0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2496,7 +2496,6 @@ bool TestHuffmanCodes()
bool TestIntegerBitops()
{
std::cout << "\nTesting Integer bit operations...\n\n";
bool pass;

struct Bitops_TestTuple
{
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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
Expand Down

1 comment on commit a8d40ee

@noloader
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also see Issue 602.

Please sign in to comment.