[libmath-prime-util-perl] 08/16: Fix bignum / Calc issues (things turning into BigFloats)
Partha P. Mukherjee
ppm-guest at moszumanska.debian.org
Thu May 21 18:46:51 UTC 2015
This is an automated email from the git hooks/post-receive script.
ppm-guest pushed a commit to annotated tag v0.19
in repository libmath-prime-util-perl.
commit 8f56bbd1e9c6622886729368b418ff6c582079b0
Author: Dana Jacobsen <dana at acm.org>
Date: Thu Jan 17 23:29:38 2013 -0800
Fix bignum / Calc issues (things turning into BigFloats)
---
Changes | 2 ++
lib/Math/Prime/Util.pm | 22 +++++++++++++---------
lib/Math/Prime/Util/PP.pm | 12 ++++++------
3 files changed, 21 insertions(+), 15 deletions(-)
diff --git a/Changes b/Changes
index f4973f4..2830592 100644
--- a/Changes
+++ b/Changes
@@ -4,6 +4,8 @@ Revision history for Perl extension Math::Prime::Util.
- Update MR bases.
+ - Fixed some issues when using bignum and Calc BigInt backend.
+
0.18 14 January 2012
- Add random_strong_prime.
diff --git a/lib/Math/Prime/Util.pm b/lib/Math/Prime/Util.pm
index ba75b62..fa632fc 100644
--- a/lib/Math/Prime/Util.pm
+++ b/lib/Math/Prime/Util.pm
@@ -447,10 +447,10 @@ sub primes {
my $p1 = primorial(Math::BigInt->new(2052));
my $p2 = primorial(Math::BigInt->new(6028));
my $p3 = primorial(Math::BigInt->new($_big_gcd_top));
- $_big_gcd[0] = $p0 / 223092870;
- $_big_gcd[1] = $p1 / $p0;
- $_big_gcd[2] = $p2 / $p1;
- $_big_gcd[3] = $p3 / $p2;
+ $_big_gcd[0] = int( $p0 / 223092870 );
+ $_big_gcd[1] = int( $p1 / $p0 );
+ $_big_gcd[2] = int( $p2 / $p1 );
+ $_big_gcd[3] = int( $p3 / $p2 );
}
# Returns a function that will get a uniform random number between 0 and
@@ -636,9 +636,11 @@ sub primes {
my($nbins, $rem);
($nbins, $rem) = $oddrange->copy->bdiv( "$rand_part_size" );
$nbins++ if $rem > 0;
+ $nbins = $nbins->as_int();
($binsize,$rem) = $oddrange->copy->bdiv($nbins);
$binsize++ if $rem > 0;
- $nparts = $oddrange->copy->bdiv($binsize);
+ $binsize = $binsize->as_int();
+ $nparts = $oddrange->copy->bdiv($binsize)->as_int();
$low = $high->copy->bzero->badd($low) if ref($low) ne 'Math::BigInt';
} else {
my $nbins = int($oddrange / $rand_part_size);
@@ -857,7 +859,7 @@ sub primes {
# I've seen +0, +1, and +2 here. Maurer uses +0. Menezes uses +1.
my $q = random_maurer_prime( ($r * $k)->bfloor + 1 );
$q = Math::BigInt->new("$q") unless ref($q) eq 'Math::BigInt';
- my $I = Math::BigInt->new(2)->bpow($k-2)->bdiv($q)->bfloor;
+ my $I = Math::BigInt->new(2)->bpow($k-2)->bdiv($q)->bfloor->as_int();
print "B = $B r = $r k = $k q = $q I = $I\n" if $verbose && $verbose != 3;
# Big GCD's are hugely fast with GMP or Pari, but super slow with Calc.
@@ -961,7 +963,8 @@ sub primes {
$qpp = Math::BigInt->new("$qpp") unless ref($qpp) eq 'Math::BigInt';
my ($il, $rem) = Math::BigInt->new(2)->bpow($l-1)->bsub(1)->bdiv(2*$qpp);
$il++ if $rem > 0;
- my $iu = Math::BigInt->new(2)->bpow($l)->bsub(2)->bdiv(2*$qpp);
+ $il = $il->as_int();
+ my $iu = Math::BigInt->new(2)->bpow($l)->bsub(2)->bdiv(2*$qpp)->as_int();
my $istart = $il + $irandf->($iu - $il);
for (my $i = $istart; $i <= $iu; $i++) { # Search for q
my $q = 2 * $i * $qpp + 1;
@@ -969,7 +972,8 @@ sub primes {
my $pp = $qp->copy->bmodpow($q-2, $q)->bmul(2)->bmul($qp)->bsub(1);
my ($jl, $rem) = Math::BigInt->new(2)->bpow($t-1)->bsub($pp)->bdiv(2*$q*$qp);
$jl++ if $rem > 0;
- my $ju = Math::BigInt->new(2)->bpow($t)->bsub(1)->bsub($pp)->bdiv(2*$q*$qp);
+ $jl = $jl->as_int();
+ my $ju = Math::BigInt->new(2)->bpow($t)->bsub(1)->bsub($pp)->bdiv(2*$q*$qp)->as_int();
my $jstart = $jl + $irandf->($ju - $jl);
for (my $j = $jstart; $j <= $ju; $j++) { # Search for p
my $p = $pp + 2 * $j * $q * $qp;
@@ -1431,7 +1435,7 @@ sub is_provable_prime {
next if $ap->copy->bmodpow($nm1, $n) != 1;
# 2. a^((n-1)/f) != 1 mod n for all f.
next if (scalar grep { $_ == 1 }
- map { $ap->copy->bmodpow($nm1/$_,$n); }
+ map { $ap->copy->bmodpow(int($nm1/$_),$n); }
@factors) > 0;
return 2;
}
diff --git a/lib/Math/Prime/Util/PP.pm b/lib/Math/Prime/Util/PP.pm
index 57b2050..ade5de9 100644
--- a/lib/Math/Prime/Util/PP.pm
+++ b/lib/Math/Prime/Util/PP.pm
@@ -1210,7 +1210,7 @@ sub prho_factor {
if ($f == $n) {
last if $inloop++; # We've been here before
} elsif ($f != 1) {
- my $f2 = $n->copy->bdiv($f);
+ my $f2 = $n->copy->bdiv($f)->as_int;
push @factors, $f;
push @factors, $f2;
croak "internal error in prho" unless ($f * $f2) == $n;
@@ -1283,7 +1283,7 @@ sub pbrent_factor {
$Xi->bmul($Xi); $Xi->badd($a); $Xi->bmod($n);
my $f = Math::BigInt::bgcd( ($Xi > $Xm) ? $Xi-$Xm : $Xm-$Xi, $n);
if ( ($f != 1) && ($f != $n) ) {
- my $f2 = $n->copy->bdiv($f);
+ my $f2 = $n->copy->bdiv($f)->as_int;
push @factors, $f;
push @factors, $f2;
croak "internal error in pbrent" unless ($f * $f2) == $n;
@@ -1343,7 +1343,7 @@ sub pminus1_factor {
$kf = $n if $kf == 0;
my $f = Math::BigInt::bgcd( $kf-1, $n );
if ( ($f != 1) && ($f != $n) ) {
- my $f2 = $n->copy->bdiv($f);
+ my $f2 = $n->copy->bdiv($f)->as_int;
push @factors, $f;
push @factors, $f2;
croak "internal error in pminus1" unless ($f * $f2) == $n;
@@ -1380,18 +1380,18 @@ sub holf_factor {
if ( ref($n) eq 'Math::BigInt' ) {
for my $i ($startrounds .. $rounds) {
my $ni = $n->copy->bmul($i);
- my $s = $ni->copy->bsqrt->bfloor;
+ my $s = $ni->copy->bsqrt->bfloor->as_int;
$s->binc if ($s * $s) != $ni;
my $m = $s->copy->bmul($s)->bmod($n);
# Check for perfect square
my $mcheck = int(($m & 127)->bstr);
next if (($mcheck*0x8bc40d7d) & ($mcheck*0xa1e2f5d1) & 0x14020a);
# ... 82% of non-squares were rejected by the bloom filter
- my $f = $m->copy->bsqrt->bfloor;
+ my $f = $m->copy->bsqrt->bfloor->as_int;
next unless ($f*$f) == $m;
$f = Math::BigInt::bgcd( ($s > $f) ? $s-$f : $f-$s, $n);
last if $f == 1 || $f == $n; # Should never happen
- my $f2 = $n->copy->bdiv($f);
+ my $f2 = $n->copy->bdiv($f)->as_int;
push @factors, $f;
push @factors, $f2;
croak "internal error in HOLF" unless ($f * $f2) == $n;
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libmath-prime-util-perl.git
More information about the Pkg-perl-cvs-commits
mailing list