def add_signature_annotations(annotations, signature_blob, signature_for_hash_type_f, output_script): sig_pair, sig_type = parse_signature_blob(signature_blob) annotations.append("r: {0:#066x}".format(sig_pair[0])) annotations.append("s: {0:#066x}".format(sig_pair[1])) sig_hash = signature_for_hash_type_f(sig_type, output_script) annotations.append("z: {0:#066x}".format(sig_hash)) annotations.append("signature type %s" % sighash_type_to_string(sig_type)) addresses = [] pairs = possible_public_pairs_for_signature(generator_secp256k1, sig_hash, sig_pair) for pair in pairs: for comp in (True, False): address = public_pair_to_bitcoin_address(pair, compressed=comp, address_prefix=b'\0') addresses.append(address) annotations.append(" sig for %s" % " ".join(addresses))
def test_sign(self): for se in ["47f7616ea6f9b923076625b4488115de1ef1187f760e65f89eb6f4f7ff04b012"] + [x * 64 for x in "123456789abcde"]: secret_exponent = int(se, 16) val = int.from_bytes(b"foo bar", byteorder="big") sig = sign(generator_secp256k1, secret_exponent, val) public_pair = public_pair_for_secret_exponent(generator_secp256k1, secret_exponent) v = verify(generator_secp256k1, public_pair, val, sig) self.assertTrue(v) sig1 = (sig[0] + 1, sig[1]) v = verify(generator_secp256k1, public_pair, val, sig1) self.assertFalse(v) public_pairs = possible_public_pairs_for_signature(generator_secp256k1, val, sig) self.assertIn(public_pair, public_pairs) print(se)
def test_sign(self): for se in ["47f7616ea6f9b923076625b4488115de1ef1187f760e65f89eb6f4f7ff04b012"] + [x * 64 for x in "123456789abcde"]: secret_exponent = int(se, 16) val = 28832970699858290 #int.from_bytes(b"foo bar", byteorder="big") sig = sign(generator_secp256k1, secret_exponent, val) public_pair = public_pair_for_secret_exponent(generator_secp256k1, secret_exponent) v = verify(generator_secp256k1, public_pair, val, sig) self.assertTrue(v) sig1 = (sig[0] + 1, sig[1]) v = verify(generator_secp256k1, public_pair, val, sig1) self.assertFalse(v) public_pairs = possible_public_pairs_for_signature(generator_secp256k1, val, sig) self.assertIn(public_pair, public_pairs) print(se)
def verify(self, h, sig): """ Return whether a signature is valid for hash h using this key. """ val = intbytes.from_bytes(h) pubkey = self.public_pair() rs = sigdecode_der(sig) if self.public_pair() is None: # find the pubkey from the signature and see if it matches # our key possible_pubkeys = ecdsa.possible_public_pairs_for_signature( ecdsa.generator_secp256k1, val, rs) hash160 = self.hash160() for candidate in possible_pubkeys: if hash160 == public_pair_to_hash160_sec(candidate, True): pubkey = candidate break if hash160 == public_pair_to_hash160_sec(candidate, False): pubkey = candidate break else: # signature is using a pubkey that's not this key return False return ecdsa.verify(ecdsa.generator_secp256k1, pubkey, val, rs)
def verify(self, h, sig): """ Return whether a signature is valid for hash h using this key. """ val = from_bytes_32(h) pubkey = self.public_pair() rs = sigdecode_der(sig) if self.public_pair() is None: # find the pubkey from the signature and see if it matches # our key possible_pubkeys = ecdsa.possible_public_pairs_for_signature( ecdsa.generator_secp256k1, val, rs) hash160 = self.hash160() for candidate in possible_pubkeys: if hash160 == public_pair_to_hash160_sec(candidate, True): pubkey = candidate break if hash160 == public_pair_to_hash160_sec(candidate, False): pubkey = candidate break else: # signature is using a pubkey that's not this key return False return ecdsa.verify(ecdsa.generator_secp256k1, pubkey, val, rs)
def recoverPubKeyFromSignature(msg, signature): msgHash = sha3_256Hash(msg) recoveredPubKeys = possible_public_pairs_for_signature( generator_secp256k1, msgHash, signature) return recoveredPubKeys