Skip to content

Commit

Permalink
converted tabs to spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
nickolaswiebe committed Apr 26, 2017
1 parent 3fcf276 commit 4c97114
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 72 deletions.
24 changes: 12 additions & 12 deletions math/extended_gcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
"""

def extended_gcd(a,b):
old_s, s = 1, 0
old_t, t = 0, 1
old_r, r = a, b
while r != 0:
quotient = old_r / r
old_r, r = r, old_r - quotient * r
old_s, s = s, old_s - quotient * s
old_t, t = t, old_t - quotient * t
return old_s, old_t, old_r
old_s, s = 1, 0
old_t, t = 0, 1
old_r, r = a, b
while r != 0:
quotient = old_r / r
old_r, r = r, old_r - quotient * r
old_s, s = s, old_s - quotient * s
old_t, t = t, old_t - quotient * t
return old_s, old_t, old_r
66 changes: 33 additions & 33 deletions math/rabin_miller.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

# factor n into a power of 2 times an odd number
def pow2_factor(n):
power = 0
while n % 2 == 0:
n /= 2
power += 1
return power, n
power = 0
while n % 2 == 0:
n /= 2
power += 1
return power, n

"""
Rabin-Miller primality test
Expand All @@ -15,31 +15,31 @@ def pow2_factor(n):
with a 4 ** -k chance of being wrong
"""
def is_prime(n, k):
r, d = pow2_factor(n - 1)
"""
returns true if a is a valid 'witness' for n
a valid witness increases chances of n being prime
an invalid witness guarentees n is composite
"""
def valid_witness(a):
x = pow(a, d, n)
if x == 1 or x == n - 1:
return False
for _ in range(r - 1):
x = pow(x, 2, n)
if x == 1:
return True
if x == n - 1:
return False
return True
for _ in xrange(k):
if valid_witness(random.randrange(2, n - 2)):
return False
return True
r, d = pow2_factor(n - 1)
"""
returns true if a is a valid 'witness' for n
a valid witness increases chances of n being prime
an invalid witness guarentees n is composite
"""
def valid_witness(a):
x = pow(a, d, n)
if x == 1 or x == n - 1:
return False
for _ in range(r - 1):
x = pow(x, 2, n)
if x == 1:
return True
if x == n - 1:
return False
return True
for _ in xrange(k):
if valid_witness(random.randrange(2, n - 2)):
return False
return True
54 changes: 27 additions & 27 deletions math/rsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,45 +30,45 @@
generate a prime with k bits
"""
def genprime(k):
while True:
n = random.randrange(2 ** (k - 1),2 ** k)
if is_prime(n,128):
return n
while True:
n = random.randrange(2 ** (k - 1),2 ** k)
if is_prime(n,128):
return n

"""
calculate the inverse of a mod m
that is, find b such that (a * b) % m == 1
"""
def modinv(a, m):
x, y, g = extended_gcd(a,m)
return x % m
x, y, g = extended_gcd(a,m)
return x % m

"""
the RSA key generating algorithm
k is the number of bits in n
"""
def generate_key(k):
# size in bits of p and q need to add up to the size of n
p_size = k / 2
q_size = k - p_size
e = genprime(k) # in many cases, e is also chosen to be a small constant
while True:
p = genprime(k / 2)
if p % e != 1:
break
while True:
q = genprime(k - k / 2)
if q % e != 1:
break
n = p * q
l = (p - 1) * (q - 1) # calculate totient function
d = modinv(e,l)
return n, e, d
# size in bits of p and q need to add up to the size of n
p_size = k / 2
q_size = k - p_size
e = genprime(k) # in many cases, e is also chosen to be a small constant
while True:
p = genprime(k / 2)
if p % e != 1:
break
while True:
q = genprime(k - k / 2)
if q % e != 1:
break
n = p * q
l = (p - 1) * (q - 1) # calculate totient function
d = modinv(e,l)
return n, e, d

"""
sample usage:
Expand Down

0 comments on commit 4c97114

Please sign in to comment.