File: calculate-mr-probs.pl

package info (click to toggle)
libmath-prime-util-gmp-perl 0.27-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,024 kB
  • ctags: 696
  • sloc: ansic: 10,302; perl: 2,855; sh: 158; makefile: 2
file content (48 lines) | stat: -rwxr-xr-x 1,139 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env perl
use warnings;
use strict;
use List::Util qw/min max/;

# From Damgård, Landrock, and Pomerance, 1993.  Page 178.

# k = bits in n
my $k = shift;
if (!defined $k || $k < 2) {
  die "Usage: $0 <k> [<maxtests>]\n k = the number of bits in n\n";
}
my $maxtests = shift || 30;

foreach my $t (1 .. $maxtests) {
  printf("k: %d t:%4d  p < %lg\n", $k, $t, min(mr_prob($k, $t)));
  #my @p = mr_prob($k, $t); print "k: $k t: $t p: @p\n";
}


sub mr_prob {
  my($k, $t) = @_;
  die "k must be >= 2" unless $k >= 2;
  die "t must be >= 1" unless $t >= 1;
  my @probs;

  push @probs, 4**-$t;

  if ($t == 1) {
    push @probs, $k*$k * 4**(2.0-sqrt($k));
  }

  if (($t == 2 && $k >= 88) || ($t >= 3 && $k >= 21 && $t <= $k/9)) {
    push @probs, $k**1.5 * 2**$t * $t**-.5 * 4**(2.0-sqrt($t*$k));
  }

  if ($t >= $k/9 && $k >= 21) {
    push @probs, (   (7.0/20.0) * $k * 2**(-5*$t)
                   + (1.0/7.0) * $k**(15.0/4.0) * 2**(-$k/2 - 2*$t)
                   + 12 * $k * 2**(-$k/4 - 3*$t) );
  }

  if ($t >= $k/4 && $k >= 21) {
    push @probs, (1.0/7.0) * $k**(15.0/4.0) * 2**(-$k/2 - 2*$t);
  }

  return @probs;
}