"# 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",