Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implement a more memory-efficient version of is_strong_pseudoprime by…

… using Rmpz_powm

We were multiplying and *then* doing a modulus unnecessarily, instead of letting
GMP do both at the same time. Thanks to danaj@cpan.org for the recommendation:

https://rt.cpan.org/Public/Bug/Display.html?id=78379
  • Loading branch information...
commit 0045721ca93146ea2bacb53e53dcd01d922c76c2 1 parent a1ea2be
Duke Leto authored
Showing with 3 additions and 12 deletions.
  1. +3 −12 lib/Math/Primality.pm
15 lib/Math/Primality.pm
View
@@ -231,18 +231,9 @@ sub is_strong_pseudoprime($;$)
}
map {
- # successively square $residue, $n is a strong psuedoprime
- # if any of these are congruent to -1 (mod $n)
- Rmpz_mul($residue,$residue,$residue); # $residue = $residue * $residue
- debug "$_: r=$residue" if DEBUG;
-
- my $mod = GMP->new();
- Rmpz_mod($mod, $residue, $n); # $mod = $residue mod $n
- debug "$_:$residue % $n = $mod " if DEBUG;
- $mod = Rmpz_cmp($mod, $m);
-
- if ($mod == 0) {
- debug "$_:$mod == $m => spsp!" if DEBUG;
+ Rmpz_powm($residue, $residue, GMP->new(2), $n);
+ if (Rmpz_cmp($residue, $m) == 0) {
+ debug "$_:$residue == $m => spsp!" if DEBUG;
return 1;
}
} ( 1 .. $s-1 );
Please sign in to comment.
Something went wrong with that request. Please try again.