This is an automated email from the git hooks/post-receive script.

ppm-guest pushed a commit to annotated tag v0.41
in repository libmath-prime-util-perl.

commit a72818085a3a3b1a9d1f27c694e377037e24ca11
Author: Dana Jacobsen <d...@acm.org>
Date:   Fri May 16 16:03:30 2014 -0700

    BM and BBS examples
---
 MANIFEST           |  1 +
 examples/csrand.pl | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+)

diff --git a/MANIFEST b/MANIFEST
index 3d83c15..8199863 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -58,6 +58,7 @@ bench/bench-mp-psrp.pl
 bench/bench-mp-prime_count.pl
 bench/factor-gnufactor.pl
 examples/README
+examples/csrand.pl
 examples/sophie_germain.pl
 examples/twin_primes.pl
 examples/abundant.pl
diff --git a/examples/csrand.pl b/examples/csrand.pl
new file mode 100644
index 0000000..77551b8
--- /dev/null
+++ b/examples/csrand.pl
@@ -0,0 +1,59 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Math::BigInt try => "GMP,Pari";
+use Math::Prime::Util qw/:all/;
+$|=1;
+
+# Example of Blum-Micali and Blum-Blum-Shub CSPRNGs.
+# Not very practical, but works as an example.
+
+my $nbits = shift || 10;
+my $type = shift || 'BBS';  # BM or BBS
+my $bits = shift || 512;
+
+die "Type must be BM or BBS" unless $type =~ /^(BBS|BM)$/;
+
+if ($type eq 'BM') {
+  my($p, $x0);
+  # Select P
+  do { $p = 2*random_nbit_prime($bits-1)+1 } while !is_prime($p);
+  # Get generator
+  my $g = Math::BigInt->new( "" . znprimroot($p) );
+  # Select X0.  This could be done better.
+  do { $x0 = random_nbit_prime($bits) ^ (random_nbit_prime($bits) >> 1) }
+    while $x0 <= 1;
+  # Generate bits
+  my $xn = Math::BigInt->new("$x0");
+  my $thresh = ($p-1) >> 1;
+  while ($nbits-- > 0) {
+    $xn = $g->copy->bmodpow($xn,$p);
+    print 0 + ($xn < $thresh);
+  }
+  print "\n";
+} else {
+  my($M,$x0);
+  # Select M = p*q
+  while (1) {
+    my($p,$q);
+    do { $p = random_nbit_prime($bits); } while ($p % 4) != 3;
+    do { $q = random_nbit_prime($bits); } while ($q % 4) != 3;
+    if ($bits < 200) {
+      my $gcd = gcd(euler_phi($p-1),euler_phi($q-1));
+      next if $gcd > 10000;
+    }
+    $M = $p * $q;
+    last;
+  }
+  # Select X0.  This could be done better.
+  do { $x0 = random_nbit_prime($bits) ^ (random_nbit_prime($bits) >> 1) }
+    while $x0 <= 1 || gcd($x0,$M) != 1;
+  # Generate bits
+  my $xn = Math::BigInt->new("$x0");
+  my $two = Math::BigInt->new(2);
+  while ($nbits-- > 0) {
+    $xn->bmodpow($two,$M);
+    print $xn->is_odd ? 1 : 0;
+  }
+  print "\n";
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on 
/srv/git.debian.org/git/pkg-perl/packages/libmath-prime-util-perl.git

_______________________________________________
Pkg-perl-cvs-commits mailing list
Pkg-perl-cvs-commits@lists.alioth.debian.org
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/pkg-perl-cvs-commits

Reply via email to