2024-04-15 10:27:48 +02:00

29 lines
748 B
Python

from util import Object
import numpy as np
CURVE_LINSPACE = np.linspace(-10, 10, num=2000)
DEFAULT_CURVE = (3, 3, 11)
NSA_CURVE = (3, 0, 2**384 - 2**128 - 2**96 + 2**32 - 1)
BTC_CURVE = (0, 7, 2**256 - 2**32 - 2**9 - 2**8 - 2**7 - 2**6 - 2**4 - 1)
class EllipticCurve(Object):
def __init__(self, a, b, mod):
self.a = a
self.b = b
self.mod = mod
self.xl = CURVE_LINSPACE
self.yl = self._x2y()
def _x2y(self):
r = np.empty((self.xl.size, 2))
for i, xi in enumerate(self.xl):
r[i][0] = np.sqrt((xi**3) + (self.a * xi**2) + self.b)
for i in range(self.xl.size):
r[i][1] = -r[i][0]
return r
def yfromx(self, x, is_top = True):
r = np.sqrt((x**3) + (self.a * x**2) + self.b)
r = +r if is_top else -r
return r