def _do_test_crypto(self, message): G = ecc.generator() _r = G.order() pvk = ecdsa.util.randrange(_r) Pub = pvk * G pubkey_c = Pub.get_public_key_bytes(True) #pubkey_u = point_to_ser(Pub,False) addr_c = public_key_to_p2pkh(pubkey_c) #print "Private key ", '%064x'%pvk eck = ecc.ECPrivkey(number_to_string(pvk, _r)) #print "Compressed public key ", pubkey_c.encode('hex') enc = ecc.ECPubkey(pubkey_c).encrypt_message(message) dec = eck.decrypt_message(enc) self.assertEqual(message, dec) #print "Uncompressed public key", pubkey_u.encode('hex') #enc2 = EC_KEY.encrypt_message(message, pubkey_u) dec2 = eck.decrypt_message(enc) self.assertEqual(message, dec2) signature = eck.sign_message(message, True) #print signature eck.verify_message_for_address(signature, message)
def test_ecc_sanity(self): G = ecc.generator() n = G.order() self.assertEqual(ecc.CURVE_ORDER, n) inf = n * G self.assertEqual(ecc.point_at_infinity(), inf) self.assertTrue(inf.is_at_infinity()) self.assertFalse(G.is_at_infinity()) self.assertEqual(11 * G, 7 * G + 4 * G) self.assertEqual((n + 2) * G, 2 * G) self.assertEqual((n - 2) * G, -2 * G) A = (n - 2) * G B = (n - 1) * G C = n * G D = (n + 1) * G self.assertFalse(A.is_at_infinity()) self.assertFalse(B.is_at_infinity()) self.assertTrue(C.is_at_infinity()) self.assertTrue((C * 5).is_at_infinity()) self.assertFalse(D.is_at_infinity()) self.assertEqual(inf, C) self.assertEqual(inf, A + 2 * G) self.assertEqual(inf, D + (-1) * G) self.assertNotEqual(A, B)