make elliptic curves available to solver.py
This commit is contained in:
46
lib.py
46
lib.py
@@ -7,6 +7,8 @@ import math
|
||||
def is_prime(num):
|
||||
if isinstance(num, str):
|
||||
num = int(num)
|
||||
if num in [2, 3, 5]:
|
||||
return True
|
||||
if num & 1 and num % 5 > 0:
|
||||
for i in range(2, math.floor(math.sqrt(num)) + 1):
|
||||
if i & 1 and (num % i) == 0:
|
||||
@@ -24,3 +26,47 @@ def rev(num): # or int(str(num)[::-1])
|
||||
revs = (revs * 10) + remainder
|
||||
num = num // 10
|
||||
return revs
|
||||
|
||||
|
||||
def power(x, y, p):
|
||||
res = 1
|
||||
x %= p
|
||||
while (y > 0):
|
||||
if (y & 1):
|
||||
res = (res * x) % p
|
||||
y = y >> 1
|
||||
x = (x * x) % p
|
||||
return res
|
||||
|
||||
|
||||
def sqrtNormal(n, p):
|
||||
n %= p
|
||||
for x in range(2, p):
|
||||
if ((x * x) % p == n):
|
||||
return x
|
||||
return None
|
||||
|
||||
|
||||
# Assumption: p is of the form 3*i + 4 where i >= 1
|
||||
def sqrtFast(n, p):
|
||||
if (p % 4 != 3):
|
||||
# raise ValueError('Invalid Input')
|
||||
return sqrtNormal(n, p)
|
||||
# Try "+(n ^ ((p + 1)/4))"
|
||||
n = n % p
|
||||
x = power(n, (p + 1) // 4, p)
|
||||
if ((x * x) % p == n):
|
||||
return x
|
||||
# Try "-(n ^ ((p + 1)/4))"
|
||||
x = p - x
|
||||
if ((x * x) % p == n):
|
||||
return x
|
||||
return None
|
||||
|
||||
|
||||
def elliptic_curve(x, a, b, r):
|
||||
y2 = (x ** 3 + a * x + b) % r
|
||||
y = sqrtFast(y2, r) if y2 > 0 else 0
|
||||
if y is None:
|
||||
return None, None
|
||||
return y, -y % r
|
||||
|
||||
Reference in New Issue
Block a user