Guest User

Untitled

a guest
May 3rd, 2024
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.90 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import random
  3. import time
  4. import math
  5. import sys
  6.  
  7. def rabin_miller_deterministic(n, cores=1, worker=0):
  8.     def test_once(a,d,r,n):
  9.         x = pow(a, d, n)
  10.         if x == 1 or x == n-1: return True
  11.         for i in range(1,r):
  12.             x = (x*x) % n
  13.             if x == n-1: return True
  14.         return False
  15.        
  16.     # Take care of special cases
  17.     if n == 2 or n == 3: return True
  18.     if n == 1 or n % 2 == 0: return False
  19.  
  20.     # Find r, d such that n = 2**r * d + 1, with d odd
  21.     d = n - 1
  22.     r = 0
  23.     while d % 2 == 0:
  24.         d = d // 2
  25.         r += 1
  26.  
  27.     ul = min(n-2, math.floor(2 * math.log(n)**2))
  28.     if worker == 0: print("ul =", ul)
  29.  
  30.     for a in range(2, ul+1):
  31.         if cores == 1 or a % cores == worker:
  32.             if test_once(a,d,r,n) == False:
  33.                 return False
  34.     return True
  35.  
  36. def rabin_miller(n, certainty=128):
  37.     """Determine if a positive integer is (probably) a prime.
  38.  
  39.    Positional argument:
  40.  
  41.        n   --      The positive integer to check for primality.
  42.  
  43.    Keyword argument:
  44.  
  45.        certainty   --  How sure you want to be than n really is a prime. See
  46.                        below for details.
  47.  
  48.    Returns:
  49.  
  50.        True        --  n is probably prime. The probability that n is not
  51.                        prime is <= 1/2**certainty.
  52.        False       --  n is *definitely* not prime.
  53.    """
  54.    
  55.    
  56.     rand = random.SystemRandom()
  57.     def test_once(d,r,n):
  58.         a = rand.randint(2, n-2)
  59.         x = pow(a, d, n)
  60.         if x == 1 or x == n-1: return True
  61.         for i in range(1,r):
  62.             x = (x*x) % n
  63.             if x == n-1: return True
  64.         return False
  65.        
  66.     # Take care of special cases
  67.     if n == 2 or n == 3: return True
  68.     if n == 1 or n % 2 == 0: return False
  69.  
  70.     # Find r, d such that n = 2**r * d + 1, with d odd
  71.     d = n - 1
  72.     r = 0
  73.     while d % 2 == 0:
  74.         d = d // 2
  75.         r += 1
  76.     for t in range(certainty//2+1):
  77.         if test_once(d,r,n) == False:
  78.             return False
  79.     return True
  80.  
  81. def find_prime(start=0, bits=0, linear=True):
  82.     """Find a randomly chosen prime of a given number of bits, or find the
  83.    first prime >= a given number.
  84.  
  85.    Keyword arguments:
  86.  
  87.        bits    --  Number of bits if you want a random prime.
  88.  
  89.        start   --  Starting search position if you want a non-random prime.
  90.  
  91.        linear  --  Controls search for random primes. See below.
  92.  
  93.        Note: you should specify exactly one of "bits" and "start".
  94.  
  95.    Returns:
  96.  
  97.        prime   --  A prime number
  98.  
  99.    When searching for a random prime, "linear" specifies what happens if
  100.    the random number it picks is not prime. If linear is True, it will add
  101.    2 and check that number. If linear is False, it will pick another random
  102.    number to check. When linear is True, if adding 2 would take it past
  103.    2**bits, it will wrap to 2**(bits-1)+1.
  104.    """
  105.     rand = random.SystemRandom()
  106.     if bits != 0 and start == 0:
  107.         base = 2**(bits-1)
  108.         wrap = 2*base
  109.         p = rand.randint(base, wrap-1) | 1
  110.     elif bits == 0 and start != 0:
  111.         if start <= 2:
  112.             return 2
  113.         p = start | 1
  114.         linear = True
  115.         base = start ^ 1
  116.         wrap = 2 * p
  117.     else:
  118.         print("Error: find_prime must be given exactly one of bits and start")
  119.         return 0
  120.     while not rabin_miller(p):
  121.         if linear:
  122.             p += 2
  123.             if p >= wrap:
  124.                 p = base + 1
  125.         else:
  126.             p = rand.randint(base, wrap-1) | 1
  127.     return p
  128.  
  129.  
  130. if __name__ == "__main__":
  131.     cores = 1
  132.     worker = 0
  133.     if len(sys.argv) == 3:
  134.         cores = int(sys.argv[1])
  135.         worker = int(sys.argv[2])
  136.     start = 2**1024+1
  137.     p = find_prime(start=start)
  138.     if worker == 0: print("p =", p)
  139.     print(worker, rabin_miller_deterministic(p, cores=cores, worker=worker))
  140.  
Advertisement
Add Comment
Please, Sign In to add comment