Skip to content
Snippets Groups Projects
Commit 12e35d82 authored by ABHIJIT MAGAPU's avatar ABHIJIT MAGAPU
Browse files

Upload New File

parent ec7ba50b
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
# Code for Finding a large prime number (min 20-digit)
%% Cell type:code id: tags:
``` python
from random import randrange, getrandbits
def is_prime(n, k=128):
""" Test if a number is prime
Args:
n -- int -- the number to test
k -- int -- the number of tests to do
return True if n is prime
"""
# Test if n is not even.
# But care, 2 is prime !
if n == 2 or n == 3:
return True
if n <= 1 or n % 2 == 0:
return False
# find r and s
s = 0
r = n - 1
while r & 1 == 0:
s += 1
r //= 2
# do k tests
for _ in range(k):
a = randrange(2, n - 1)
x = pow(a, r, n)
if x != 1 and x != n - 1:
j = 1
while j < s and x != n - 1:
x = pow(x, 2, n)
if x == 1:
return False
j += 1
if x != n - 1:
return False
return True
def generate_prime_candidate(length):
""" Generate an odd integer randomly
Args:
length -- int -- the length of the number to generate, in bits
return a integer
"""
# generate random bits
p = getrandbits(length)
# apply a mask to set MSB and LSB to 1
p |= (1 << length - 1) | 1
return p
def generate_prime_number(length=60):
""" Generate a prime
Args:
length -- int -- length of the prime to generate, in bits
return a prime
"""
p = 4
# keep generating while the primality test fail
while not is_prime(p, 128):
p = generate_prime_candidate(length)
return p
print(generate_prime_number())
```
%% Cell type:markdown id: tags:
610292520201647
%% Cell type:markdown id: tags:
# Code for finding Primitive root
%% Cell type:code id: tags:
``` python
# Python3 program to find primitive root
# of a given number n
from math import sqrt
# Returns True if n is prime
def isPrime( n):
# Corner cases
if (n <= 1):
return False
if (n <= 3):
return True
# This is checked so that we can skip
# middle five numbers in below loop
if (n % 2 == 0 or n % 3 == 0):
return False
i = 5
while(i * i <= n):
if (n % i == 0 or n % (i + 2) == 0) :
return False
i = i + 6
return True
""" Iterative Function to calculate (x^n)%p
in O(logy) */"""
def power( x, y, p):
res = 1 # Initialize result
x = x % p # Update x if it is more
# than or equal to p
while (y > 0):
# If y is odd, multiply x with result
if (y & 1):
res = (res * x) % p
# y must be even now
y = y >> 1 # y = y/2
x = (x * x) % p
return res
# Utility function to store prime
# factors of a number
def findPrimefactors(s, n) :
# Print the number of 2s that divide n
while (n % 2 == 0) :
s.add(2)
n = n // 2
# n must be odd at this po. So we can
# skip one element (Note i = i +2)
for i in range(3, int(sqrt(n)), 2):
# While i divides n, print i and divide n
while (n % i == 0) :
s.add(i)
n = n // i
# This condition is to handle the case
# when n is a prime number greater than 2
if (n > 2) :
s.add(n)
def findPrimitive( n) :
s = set()
# Check if n is prime or not
if (isPrime(n) == False):
return -1
# Find value of Euler Totient function
# of n. Since n is a prime number, the
# value of Euler Totient function is n-1
# as there are n-1 relatively prime numbers.
phi = n - 1
# Find prime factors of phi and store in a set
findPrimefactors(s, phi)
# Check for every number from 2 to phi
for r in range(2, phi + 1):
# Iterate through all prime factors of phi.
# and check if we found a power with value 1
flag = False
for it in s:
# Check if r^((phi)/primefactors)
# mod n is 1 or not
if (power(r, phi // it, n) == 1):
flag = True
break
# If there was no power with value 1.
if (flag == False):
return r
# If no primitive root found
return -1
n = 610292520201647
print("Smallest primitive root of",n, "is", findPrimitive(n))
```
%% Output
Smallest primitive root of 610292520201647 is 5
%% Cell type:markdown id: tags:
# Code for Diffie–Hellman
%% Cell type:code id: tags:
``` python
import random
import hashlib
import sys
g=3
p=610292520201647
a=random.randint(10000,99999)
b=random.randint(10000,99999)
A = (g**a) % p
B = (g**b) % p
print('g: ',g,' (a shared value), n: ',p, ' (a prime number)')
print('\nAlice calculates:')
print('a (Alice random): ',a)
print('Alice value (A): ',A,' (g^a) mod p')
print('\nBob calculates:')
print('b (Bob random): ',b)
print('Bob value (B): ',B,' (g^b) mod p')
print('\nAlice calculates:')
keyA=(B**a) % p
print('Key: ',keyA,' (B^a) mod p')
print('Key: ',hashlib.sha256(str(keyA).encode()).hexdigest())
print('\nBob calculates:')
keyB=(A**b) % p
print('Key: ',keyB,' (A^b) mod p')
print('Key: ',hashlib.sha256(str(keyB).encode()).hexdigest())
```
%% Output
g: 3 (a shared value), n: 610292520201647 (a prime number)
Alice calculates:
a (Alice random): 37856
Alice value (A): 23079533594046 (g^a) mod p
Bob calculates:
b (Bob random): 50018
Bob value (B): 69387698405719 (g^b) mod p
Alice calculates:
Key: 573821206942293 (B^a) mod p
Key: f4cb54ce1c55e40a6482af8ee396da4f565d243f4532ad83d006dcf526499322
Bob calculates:
Key: 573821206942293 (A^b) mod p
Key: f4cb54ce1c55e40a6482af8ee396da4f565d243f4532ad83d006dcf526499322
%% Cell type:markdown id: tags:
# Code to Find an integer k such that where a and m are relatively prime.
%% Cell type:code id: tags:
``` python
# Python3 program to calculate
# discrete logarithm
import math;
# Iterative Function to calculate
# (x ^ y)%p in O(log y)
def powmod(x, y, p):
res = 1; # Initialize result
x = x % p; # Update x if it is more
# than or equal to p
while (y > 0):
# If y is odd, multiply x with result
if (y & 1):
res = (res * x) % p;
# y must be even now
y = y >> 1; # y = y/2
x = (x * x) % p;
return res;
# Function to calculate k for given a, b, m
def discreteLogarithm(a, b, m):
n = int(math.sqrt(m) + 1);
value = [0] * m;
# Store all values of a^(n*i) of LHS
for i in range(n, 0, -1):
value[ powmod (a, i * n, m) ] = i;
for j in range(n):
# Calculate (a ^ j) * b and check
# for collision
cur = (powmod (a, j, m) * b) % m;
# If collision occurs i.e., LHS = RHS
if (value[cur]):
ans = value[cur] * n - j;
# Check whether ans lies below m or not
if (ans < m):
return ans;
return -1;
# Driver code
a = 610292520201647;
b = 12542369;
m = 5;
print(discreteLogarithm(a, b, m));
a = 610292520201647;
b = 1852642;
m = 5;
print(discreteLogarithm(a, b, m));
# This code is contributed by mits
```
%% Output
2
1
%% Cell type:markdown id: tags:
# Applying this code to see how long it takes to break your chosen 3 keys. Record your observation as shown in the table(min 3 (p,g) values and for each (p,g) 3 private keys (weak, medium strong and strong)
%% Cell type:code id: tags:
``` python
# Python3 program to calculate
# discrete logarithm
import math;
# Iterative Function to calculate
# (x ^ y)%p in O(log y)
def powmod(x, y, p):
res = 1; # Initialize result
x = x % p; # Update x if it is more
# than or equal to p
while (y > 0):
# If y is odd, multiply x with result
if (y & 1):
res = (res * x) % p;
# y must be even now
y = y >> 1; # y = y/2
x = (x * x) % p;
return res;
# Function to calculate k for given a, b, m
def discreteLogarithm(a, b, m):
n = int(math.sqrt(m) + 1);
value = [0] * m;
# Store all values of a^(n*i) of LHS
for i in range(n, 0, -1):
value[ powmod (a, i * n, m) ] = i;
for j in range(n):
# Calculate (a ^ j) * b and check
# for collision
cur = (powmod (a, j, m) * b) % m;
# If collision occurs i.e., LHS = RHS
if (value[cur]):
ans = value[cur] * n - j;
# Check whether ans lies below m or not
if (ans < m):
return ans;
return -1;
# Driver code
a = 610292520201647;
b = 12542369;
m = 5;
print(discreteLogarithm(a, b, m));
a = 610292520201647;
b = 1852642;
m = 5;
print(discreteLogarithm(a, b, m));
# This code is contributed by mits
```
%% Output
2
1
%% Cell type:code id: tags:
``` python
# Python3 program to calculate
# discrete logarithm
import math;
# Iterative Function to calculate
# (x ^ y)%p in O(log y)
def powmod(x, y, p):
res = 1; # Initialize result
x = x % p; # Update x if it is more
# than or equal to p
while (y > 0):
# If y is odd, multiply x with result
if (y & 1):
res = (res * x) % p;
# y must be even now
y = y >> 1; # y = y/2
x = (x * x) % p;
return res;
# Function to calculate k for given a, b, m
def discreteLogarithm(a, b, m):
n = int(math.sqrt(m) + 1);
value = [0] * m;
# Store all values of a^(n*i) of LHS
for i in range(n, 0, -1):
value[ powmod (a, i * n, m) ] = i;
for j in range(n):
# Calculate (a ^ j) * b and check
# for collision
cur = (powmod (a, j, m) * b) % m;
# If collision occurs i.e., LHS = RHS
if (value[cur]):
ans = value[cur] * n - j;
# Check whether ans lies below m or not
if (ans < m):
return ans;
return -1;
# Driver code
a = 610292520201647;
b = 12542369;
m = 125;
print(discreteLogarithm(a, b, m));
a = 610292520201647;
b = 1852642;
m = 125;
print(discreteLogarithm(a, b, m));
# This code is contributed by mits
```
%% Output
54
49
%% Cell type:code id: tags:
``` python
# Python3 program to calculate
# discrete logarithm
import math;
# Iterative Function to calculate
# (x ^ y)%p in O(log y)
def powmod(x, y, p):
res = 1; # Initialize result
x = x % p; # Update x if it is more
# than or equal to p
while (y > 0):
# If y is odd, multiply x with result
if (y & 1):
res = (res * x) % p;
# y must be even now
y = y >> 1; # y = y/2
x = (x * x) % p;
return res;
# Function to calculate k for given a, b, m
def discreteLogarithm(a, b, m):
n = int(math.sqrt(m) + 1);
value = [0] * m;
# Store all values of a^(n*i) of LHS
for i in range(n, 0, -1):
value[ powmod (a, i * n, m) ] = i;
for j in range(n):
# Calculate (a ^ j) * b and check
# for collision
cur = (powmod (a, j, m) * b) % m;
# If collision occurs i.e., LHS = RHS
if (value[cur]):
ans = value[cur] * n - j;
# Check whether ans lies below m or not
if (ans < m):
return ans;
return -1;
# Driver code
a = 610292520201647;
b = 12542369;
m = 63456456;
print(discreteLogarithm(a, b, m));
a = 610292520201647;
b = 1852642;
m = 465464;
print(discreteLogarithm(a, b, m));
# This code is contributed by mits
```
%% Output
-1
-1
%% Cell type:code id: tags:
``` python
# Python3 program to calculate
# discrete logarithm
import math;
# Iterative Function to calculate
# (x ^ y)%p in O(log y)
def powmod(x, y, p):
res = 1; # Initialize result
x = x % p; # Update x if it is more
# than or equal to p
while (y > 0):
# If y is odd, multiply x with result
if (y & 1):
res = (res * x) % p;
# y must be even now
y = y >> 1; # y = y/2
x = (x * x) % p;
return res;
# Function to calculate k for given a, b, m
def discreteLogarithm(a, b, m):
n = int(math.sqrt(m) + 1);
value = [0] * m;
# Store all values of a^(n*i) of LHS
for i in range(n, 0, -1):
value[ powmod (a, i * n, m) ] = i;
for j in range(n):
# Calculate (a ^ j) * b and check
# for collision
cur = (powmod (a, j, m) * b) % m;
# If collision occurs i.e., LHS = RHS
if (value[cur]):
ans = value[cur] * n - j;
# Check whether ans lies below m or not
if (ans < m):
return ans;
return -1;
# Driver code
a = 610292520201647;
b = 12542369;
m = 610292520201644;
print(discreteLogarithm(a, b, m));
a = 610292520201647;
b = 1852642;
m = 610292520201644;
print(discreteLogarithm(a, b, m));
# This code is contributed by mits
```
%% Output
---------------------------------------------------------------------------
MemoryError Traceback (most recent call last)
<ipython-input-2-e09ae1952765> in <module>
53 b = 12542369;
54 m = 610292520201644;
---> 55 print(discreteLogarithm(a, b, m));
56
57 a = 610292520201647;
<ipython-input-2-e09ae1952765> in discreteLogarithm(a, b, m)
27 n = int(math.sqrt(m) + 1);
28
---> 29 value = [0] * m;
30
31 # Store all values of a^(n*i) of LHS
MemoryError:
%% Cell type:code id: tags:
``` python
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment