"# Code to Find an integer k such that where a and m are relatively prime."
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n",
"1\n"
]
}
],
"source": [
"# Python3 program to calculate \n",
"# discrete logarithm \n",
"import math; \n",
"\n",
"# Iterative Function to calculate \n",
"# (x ^ y)%p in O(log y) \n",
"def powmod(x, y, p): \n",
"\n",
"\tres = 1; # Initialize result \n",
"\n",
"\tx = x % p; # Update x if it is more \n",
"\t\t\t# than or equal to p \n",
"\n",
"\twhile (y > 0): \n",
"\t\t\n",
"\t\t# If y is odd, multiply x with result \n",
"\t\tif (y & 1): \n",
"\t\t\tres = (res * x) % p; \n",
"\n",
"\t\t# y must be even now \n",
"\t\ty = y >> 1; # y = y/2 \n",
"\t\tx = (x * x) % p; \n",
"\treturn res; \n",
"\n",
"# Function to calculate k for given a, b, m \n",
"def discreteLogarithm(a, b, m): \n",
"\tn = int(math.sqrt(m) + 1); \n",
"\n",
"\tvalue = [0] * m; \n",
"\n",
"\t# Store all values of a^(n*i) of LHS \n",
"\tfor i in range(n, 0, -1): \n",
"\t\tvalue[ powmod (a, i * n, m) ] = i; \n",
"\n",
"\tfor j in range(n): \n",
"\t\t\n",
"\t\t# Calculate (a ^ j) * b and check \n",
"\t\t# for collision \n",
"\t\tcur = (powmod (a, j, m) * b) % m; \n",
"\n",
"\t\t# If collision occurs i.e., LHS = RHS \n",
"\t\tif (value[cur]): \n",
"\t\t\tans = value[cur] * n - j; \n",
"\t\t\t\n",
"\t\t\t# Check whether ans lies below m or not \n",
"\t\t\tif (ans < m): \n",
"\t\t\t\treturn ans; \n",
"\t\n",
"\treturn -1; \n",
"\n",
"# Driver code \n",
"a = 610292520201647; \n",
"b = 12542369; \n",
"m = 5; \n",
"print(discreteLogarithm(a, b, m)); \n",
"\n",
"a = 610292520201647; \n",
"b = 1852642; \n",
"m = 5; \n",
"print(discreteLogarithm(a, b, m)); \n",
"\n",
"# This code is contributed by mits \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 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",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n",
"1\n"
]
}
],
"source": [
"# Python3 program to calculate \n",
"# discrete logarithm \n",
"import math; \n",
"\n",
"# Iterative Function to calculate \n",
"# (x ^ y)%p in O(log y) \n",
"def powmod(x, y, p): \n",
"\n",
"\tres = 1; # Initialize result \n",
"\n",
"\tx = x % p; # Update x if it is more \n",
"\t\t\t# than or equal to p \n",
"\n",
"\twhile (y > 0): \n",
"\t\t\n",
"\t\t# If y is odd, multiply x with result \n",
"\t\tif (y & 1): \n",
"\t\t\tres = (res * x) % p; \n",
"\n",
"\t\t# y must be even now \n",
"\t\ty = y >> 1; # y = y/2 \n",
"\t\tx = (x * x) % p; \n",
"\treturn res; \n",
"\n",
"# Function to calculate k for given a, b, m \n",
"def discreteLogarithm(a, b, m): \n",
"\tn = int(math.sqrt(m) + 1); \n",
"\n",
"\tvalue = [0] * m; \n",
"\n",
"\t# Store all values of a^(n*i) of LHS \n",
"\tfor i in range(n, 0, -1): \n",
"\t\tvalue[ powmod (a, i * n, m) ] = i; \n",
"\n",
"\tfor j in range(n): \n",
"\t\t\n",
"\t\t# Calculate (a ^ j) * b and check \n",
"\t\t# for collision \n",
"\t\tcur = (powmod (a, j, m) * b) % m; \n",
"\n",
"\t\t# If collision occurs i.e., LHS = RHS \n",
"\t\tif (value[cur]): \n",
"\t\t\tans = value[cur] * n - j; \n",
"\t\t\t\n",
"\t\t\t# Check whether ans lies below m or not \n",
"\t\t\tif (ans < m): \n",
"\t\t\t\treturn ans; \n",
"\t\n",
"\treturn -1; \n",
"\n",
"# Driver code \n",
"a = 610292520201647; \n",
"b = 12542369; \n",
"m = 5; \n",
"print(discreteLogarithm(a, b, m)); \n",
"\n",
"a = 610292520201647; \n",
"b = 1852642; \n",
"m = 5; \n",
"print(discreteLogarithm(a, b, m)); \n",
"\n",
"# This code is contributed by mits "
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"54\n",
"49\n"
]
}
],
"source": [
"# Python3 program to calculate \n",
"# discrete logarithm \n",
"import math; \n",
"\n",
"# Iterative Function to calculate \n",
"# (x ^ y)%p in O(log y) \n",
"def powmod(x, y, p): \n",
"\n",
"\tres = 1; # Initialize result \n",
"\n",
"\tx = x % p; # Update x if it is more \n",
"\t\t\t# than or equal to p \n",
"\n",
"\twhile (y > 0): \n",
"\t\t\n",
"\t\t# If y is odd, multiply x with result \n",
"\t\tif (y & 1): \n",
"\t\t\tres = (res * x) % p; \n",
"\n",
"\t\t# y must be even now \n",
"\t\ty = y >> 1; # y = y/2 \n",
"\t\tx = (x * x) % p; \n",
"\treturn res; \n",
"\n",
"# Function to calculate k for given a, b, m \n",
"def discreteLogarithm(a, b, m): \n",
"\tn = int(math.sqrt(m) + 1); \n",
"\n",
"\tvalue = [0] * m; \n",
"\n",
"\t# Store all values of a^(n*i) of LHS \n",
"\tfor i in range(n, 0, -1): \n",
"\t\tvalue[ powmod (a, i * n, m) ] = i; \n",
"\n",
"\tfor j in range(n): \n",
"\t\t\n",
"\t\t# Calculate (a ^ j) * b and check \n",
"\t\t# for collision \n",
"\t\tcur = (powmod (a, j, m) * b) % m; \n",
"\n",
"\t\t# If collision occurs i.e., LHS = RHS \n",
"\t\tif (value[cur]): \n",
"\t\t\tans = value[cur] * n - j; \n",
"\t\t\t\n",
"\t\t\t# Check whether ans lies below m or not \n",
"\t\t\tif (ans < m): \n",
"\t\t\t\treturn ans; \n",
"\t\n",
"\treturn -1; \n",
"\n",
"# Driver code \n",
"a = 610292520201647; \n",
"b = 12542369; \n",
"m = 125; \n",
"print(discreteLogarithm(a, b, m)); \n",
"\n",
"a = 610292520201647; \n",
"b = 1852642; \n",
"m = 125; \n",
"print(discreteLogarithm(a, b, m)); \n",
"\n",
"# This code is contributed by mits "
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-1\n",
"-1\n"
]
}
],
"source": [
"# Python3 program to calculate \n",
"# discrete logarithm \n",
"import math; \n",
"\n",
"# Iterative Function to calculate \n",
"# (x ^ y)%p in O(log y) \n",
"def powmod(x, y, p): \n",
"\n",
"\tres = 1; # Initialize result \n",
"\n",
"\tx = x % p; # Update x if it is more \n",
"\t\t\t# than or equal to p \n",
"\n",
"\twhile (y > 0): \n",
"\t\t\n",
"\t\t# If y is odd, multiply x with result \n",
"\t\tif (y & 1): \n",
"\t\t\tres = (res * x) % p; \n",
"\n",
"\t\t# y must be even now \n",
"\t\ty = y >> 1; # y = y/2 \n",
"\t\tx = (x * x) % p; \n",
"\treturn res; \n",
"\n",
"# Function to calculate k for given a, b, m \n",
"def discreteLogarithm(a, b, m): \n",
"\tn = int(math.sqrt(m) + 1); \n",
"\n",
"\tvalue = [0] * m; \n",
"\n",
"\t# Store all values of a^(n*i) of LHS \n",
"\tfor i in range(n, 0, -1): \n",
"\t\tvalue[ powmod (a, i * n, m) ] = i; \n",
"\n",
"\tfor j in range(n): \n",
"\t\t\n",
"\t\t# Calculate (a ^ j) * b and check \n",
"\t\t# for collision \n",
"\t\tcur = (powmod (a, j, m) * b) % m; \n",
"\n",
"\t\t# If collision occurs i.e., LHS = RHS \n",
"\t\tif (value[cur]): \n",
"\t\t\tans = value[cur] * n - j; \n",
"\t\t\t\n",
"\t\t\t# Check whether ans lies below m or not \n",
# 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
importmath;
# Iterative Function to calculate
# (x ^ y)%p in O(log y)
defpowmod(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;
returnres;
# Function to calculate k for given a, b, m
defdiscreteLogarithm(a,b,m):
n=int(math.sqrt(m)+1);
value=[0]*m;
# Store all values of a^(n*i) of LHS
foriinrange(n,0,-1):
value[powmod (a,i*n,m)]=i;
forjinrange(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):
returnans;
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)