-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLarge Prime Generator.py
More file actions
152 lines (114 loc) · 3.77 KB
/
Large Prime Generator.py
File metadata and controls
152 lines (114 loc) · 3.77 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import math
import random
import time
import os
'''
Kipling Gillespie
August 2017
Last Updated: 10/25/2017
I wrote this program to teach myself python and practice
algorithms we were covering in my CS315 class at
the University of Kentucky.
'''
# Calculates the modular exponentiation of (integer^exponant) Mod N
# I need to write a non-recursive version of this method. When we try
# and calculate a 128 byte prime the program crashes due to
# the stack overflowing from recursion.
def modexp(integer, exponant, N):
if exponant == 0:
return 1
z = modexp(integer, exponant//2, N)
if (exponant%2) == 0:
return (z*z)%N
else:
return (integer*z*z)%N
# Calculates the Greatest Common Divisor using Euclid's Algorithm
def gcd(a, b):
if b == 0:
return a
return gcd(b, a%b)
# Calculates the Greatest Common Divisor, and x and y using
# Euclid's Extended Algorithm
def egcd(a, b):
if b == 0:
return (1, 0, a)
xp, yp, d = egcd(b, a%b)
return (yp, xp - (math.floor(a/b)*yp), d)
# Calculates whether p is prime or not b using Fermat's Little Theorem.
# the Accuracy variable tells the program how many random values for
# a the program should run
def primality(p, accuracy = 100):
# we know that two is Prime and all other even values are composite
#values
if p == 2:
return True
elif (not p&1):
return False
for x in range(0, accuracy):
a = random.randint(2, p-1)
res = modexp(a, p-1, p)
if res == 1:
continue
else:
return False
return True
def genprime(binbytes = 8, accuracy = 100):
isPrime = False
tries = 0
while(not isPrime):
tries+=1
a = int.from_bytes(os.urandom(binbytes), 'little')
isPrime = primality(a, accuracy)
# I wanted to track how many probes I had to
# make to find a prime
#if (tries%1000 ==0):
#print(tries)
return (a, tries)
def PiFunc(x):
#input: An Integer value
#output: an estimate on the number of primes less than x
return x/math.log(x)
#sill bunk...
def EstimatedChecks(binBytes):
largest = math.pow(2,binBytes)-1
smallest = math.pow(2, binBytes-1)
ourRange = largest #- smallest;
probability = PiFunc(largest)#(PiFunc(largest)-PiFunc(smallest))
probability /= ourRange
probability *= 100
return probability
# in progress
def RSAKey(binBytes):
p = genprime(binBytes)
q = genprime(binBytes)
N = p*q
e = gcd(p-1, q-1)
(d, toss, temp) = egcd(e, N)
return N, e, d
def main():
random.seed()
avgRunTime = 0
avgTries = 0
# How many times do I want to generate a large random prime
numRuns = 1
# How many bytes should the prime be made up of.
numbytes = 64
# How accurate do I want my primality test. I di
accuracy = 100
print("Estimated ", EstimatedChecks(numbytes), "% chance of picking Prime.")
#now lets try and generate a large number.
for i in range(0, numRuns):
#start stop watch for this run
startTime = time.time()
#calculate a prime number and print it when found
(prime, tries) = genprime(numbytes, accuracy)
print(prime)
#Stop watch for this run
endTime = time.time();
avgTries += tries
avgRunTime += endTime-startTime
avgRunTime /= numRuns
avgTries //= numRuns
print("Average Run Time Per Prime: ", avgRunTime)
print("Average Tries Per Prime: ", avgTries)
main()