def normalize(self): if int(self.z) in (0, 1): return self z = GField(modular_inverse(int(self.z), self.ecc_params.P), self.ecc_params) tmp = z.sqr() x = self.x.mul(tmp) y = self.y.mul(tmp).mul(z) z = 1 return Jacobian(x, y, z, self.ecc_params)
def parse(s, ecc_params=_ecc_params): """Takes a 192 bit integer in hex with a + or - sign on the front and returns a Jacobian object.""" assert s[0] in ("-", "+") ybit, s = s[0] == "-", s[1:] x = GField(int(s, 16), ecc_params) _ = x.mul2().add(x) y2 = x.pow(3).sub(x.mul(3)).add(ecc_params.B) y = y2.legendre_sqrt() if y is None: y = 0 if bit_test(y, 0) != ybit: y = -y z = 1 return Jacobian(x, y, z, ecc_params)
def generate_challenge(public_key, ecc_params=ecc_params): if isinstance(public_key, str): public_key = parse_public_key(public_key, ecc_params) assert isinstance(public_key, Jacobian) secret = GField(get_random_number(24), ecc_params) challenge = Jacobian.base(ecc_params).mul(secret).normalize() answer = public_key.mul(secret).normalize().x return (challenge, answer)
def __init__(self, x, y, z, ecc_params=_ecc_params): self.ecc_params = ecc_params self._x = GField(x) self._y = GField(y) self._z = GField(z)
def generate_key_pair(ecc_params=ecc_params): private_key = GField(get_random_number(24), ecc_params) public_key = get_public_key(private_key) return (private_key, public_key)
def parse_private_key(private_key_str, ecc_params=ecc_params): private_key = int(private_key_str, 16) return GField(private_key, ecc_params)