def verify(msghash, sig, pub): assert isinstance(msghash, bytes) assert isinstance(sig, tuple) assert isinstance(pub, tuple) _, r, s = sig w = ec.inv(s, ec.N) z = convert.bytes_to_int(msghash) u1, u2 = z * w % ec.N, r * w % ec.N x, _ = ec.add(ec.multiply(ec.G, u1), ec.multiply(pub, u2)) return r == x
def derive_xpub(k, i): i = int(i) if i >= 2**31: raise Exception("Can't do private derivation on public key!") pub = k['pub'] pub_ser = ecdsa.serialize_pub(pub) hmacdata = pub_ser + convert.int_to_bytes(i, 4) I = hmac.new(k['chaincode'], hmacdata, hashlib.sha512).digest() return { 'depth': k['depth'] + 1, 'fingerprint': hashes.hash160(pub_ser)[:4], 'i': i, 'chaincode': I[32:], 'pub': ec.add(k['pub'], ecdsa.priv_to_pub(ecdsa.deserialize_priv(I[:32]))) }
def test_all(self): for _ in range(20): x, y = random.randrange(2**256), random.randrange(2**256) self.assertFalse(ec.is_curve_point((x,y))) G_x = ec.multiply(ec.G, x) G_y = ec.multiply(ec.G, y) self.assertTrue(ec.is_curve_point(G_x)) self.assertEqual( ec.multiply(G_x, y)[0], ec.multiply(G_y, x)[0] ) self.assertEqual( ec.add(G_x, G_y)[0], ec.multiply(ec.G, ec.add_scalar(x, y))[0] ) self.assertEqual( ec.substract(G_x, G_y)[0], ec.multiply(ec.G, ec.substract_scalar(x, y))[0] ) self.assertEqual(ec.G[0], ec.multiply(ec.divide(ec.G, x), x)[0])