In [2]:
# builtin functions for number theory
Out[2]:
True
In [1]:
is_prime(7) # tests whether a number is prime
# much more efficient than trial division
Out[1]:
True
In [3]:
is_prime(23082341083410384)
Out[3]:
False
In [7]:
is_prime(2^4219-1)
Out[7]:
False
In [9]:
next_prime(13) # first prime bigger than n
Out[9]:
17
In [10]:
next_prime(2^100)
Out[10]:
1267650600228229401496703205653
In [15]:
randint(1,100) # generates random integer between 1,100
# NOT Cryptographically secure (but probably good enough for our purposes)
Out[15]:
43
In [18]:
# a^e mod n
power_mod(2,5,29) # same algorithm as exp_mod that we did last class
Out[18]:
3
In [19]:
# Diffie-Helmann key exchange
In [ ]:
p = 1031  # common modulus to be used; this is public 
In [21]:
# Step 1: Alice and Bob each pick secret numbers 
# Alice:
a = 347 # secret, don't share with anyone
# Bob:
b = 943 # secret, don't share with anyone
In [23]:
# Step 2: Alice and Bob each do a computation using the secret number
# Alice:
c=power_mod(3,a,p); print(c) # 3^a mod p 
# Bob:
d=power_mod(3,b,p); print(d) # 3^b mod p 
352
576
In [24]:
# Step 3
# Alice sends c to Bob
# Bob sends d to Alice
# Eve (the eavesdropper) can see these 
In [26]:
# Step 4: 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)
1010
1010
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 [27]:
# Fermat's little theorem:
# a^(p-1) = 1 mod p, if a is not divisible by p 
power_mod(4,16,17)
Out[27]:
1
In [28]:
power_mod(5,22,23)
Out[28]:
1
In [ ]: