Ejemplo n.º 1
0
 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])
Ejemplo n.º 2
0
def derive_xpriv(k, i):
    i = int(i)

    pub = ecdsa.priv_to_pub(k['priv'])
    pub_ser = ecdsa.serialize_pub(pub)
    priv_ser = ecdsa.serialize_priv(k['priv'])

    if i >= 2**31:
        hmacdata = b'\x00' + priv_ser + convert.int_to_bytes(i, 4)
    else:
        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:],
        'priv': ec.add_scalar(k['priv'], ecdsa.deserialize_priv(I[:32]))
    }