In [ ]:
# Some builtin functions for number theory
In [13]:
is_prime(2^603-1) # tests whether a number is prime
# much more efficient than trial division
Out[13]:
False
In [14]:
next_prime(21) # returns next prime bigger than n
Out[14]:
23
In [15]:
next_prime(23) # returns first prime bigger than n
Out[15]:
29
In [16]:
next_prime(2^100)
Out[16]:
1267650600228229401496703205653
In [17]:
is_prime(1267650600228229401496703205653)
Out[17]:
True
In [19]:
is_prime(next_prime(2^1000)) # takes a few seconds
Out[19]:
True
In [21]:
power_mod(2,5,29) # power_mod(a,e,n) computes a^e % n (same thing as exp_mod) 
Out[21]:
3
In [25]:
randint(1,1000) # NOT cryptographically secure 
Out[25]:
489
In [ ]:
# Diffie-Helmann algorithm for key-exchange
In [31]:
# Step 1: generate p prime 
# Alice, Bob will know this (so will Eve)
p = next_prime(2^10);p
Out[31]:
1031
In [32]:
# Step 2: Alice and Bob each pick secret numbers (best to use random)
# Alice: 
a = randint(1,2^10); print(a)
# Bob:
b = randint(1,2^10); print(b)
686
557
In [33]:
# Step 3: Alice and Bob each do a computation using the secret number
# Alice:
c = power_mod(3,a,p); print(c)
# Bob:
d = power_mod(3,b,p); print(d)
993
868
In [ ]:
# Step 4: Alice sends Bob c, 
# and Bob sends Alice d 
# Eve (the eavesdropper) can see these 
In [34]:
# Step 5: Both Alice and Bob can compute the same number, which is 3^(ab) mod p 
# Alice:
KA = power_mod(d,a,p); print(KA)
# Bob: 
KB = power_mod(c,b,p); print(KB)
271
271
In [ ]:
# Why can't Eve also find K = KA = KB?  
# She knows p, c = 3^a mod p, and d = 3^b mod p, since she sees the communication between Alice, Bob
# She also knows that that 3 is the base being used.  
# But it is not easy to compute 3^(ab) mod p from this info. 
# One approach would be to use 'brute-force' to find a, b by going through all the possiblilites and 
# testing if 3^a mod p equals c for that guess of a.  But there are so many possibilities 
# for a that this would not be feasible.  
In [35]:
# Fermat's little theorem
# a^(p-1) = 1 mod p, for any prime p, and any a not divisible by p 
power_mod(3,16,17)
Out[35]:
1
In [36]:
power_mod(4,28,29)
Out[36]:
1
In [37]:
# Euler's theorem 
# a^phi(n) = 1 mod n, for any n, a relatively prime to n 
power_mod(4, euler_phi(9), 9) 
Out[37]:
1
In [38]:
power_mod(5, euler_phi(99), 99) 
Out[38]:
1
In [40]:
# phi(n) is the number of integers between 1,n that are relatively prime to n
euler_phi(9)
Out[40]:
6