Math-Prime-Util icon indicating copy to clipboard operation
Math-Prime-Util copied to clipboard

invmod(p, q) can wrongly fail

Open hvds opened this issue 4 years ago • 1 comments

Hi Dana, how's the release coming along? :)

My code just hit this today under MPU-0.73:

% cat t
use strict;
use warnings;
use Math::GMP;
use Math::Prime::Util qw{ invmod };

my $p = Math::GMP->new(1589) ** 6;
my $q = Math::GMP->new(93) ** 6;
print "p=$p, q=$q\n";

my $i1 = invmod(-$p, $q);
print "i1=@{[ $i1 // '<undef>' ]}\n";

my $i2 = invmod(-$p % $q, $q);
print "i2=@{[ $i2 // '<undef>' ]}\n";

my $prod = (-$p * $i2) % $q;
print +($prod == 1) ? "i2 ok\n" : "i2 not ok\n";
% perl t
p=16096942149150081961, q=646990183449
i1=<undef>
i2=25493952356
i2 ok
% 

I haven't looked into it yet (nor tested it against the latest github code), and it's easy enough to work around, but I assume it shouldn't be doing that - i1 should get the same answer as i2.

Hugo

hvds avatar Jan 24 '22 04:01 hvds

I tested the code against the latest github code and it appears that the issue has been fixed:

p=16096942149150081961, q=646990183449
i1=25493952356
i2=25493952356
i2 ok

trizen avatar Jan 24 '22 06:01 trizen