def verify(self, h, sig, generator=None): """ Return whether a signature is valid for hash h using this key. """ generator = generator or self._generator if not generator: raise ValueError("generator must be specified") 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 = generator.possible_public_pairs_for_signature(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 generator.verify(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() return self._generator.verify(pubkey, val, sigdecode_der(sig))
def verify(self, h, sig, generator=None): """ Return whether a signature is valid for hash h using this key. """ generator = generator or self._generator if not generator: raise ValueError("generator must be specified") 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 = generator.possible_public_pairs_for_signature( 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 generator.verify(pubkey, val, rs)
def sign(key, msg): sig = key.sign(msg) # openssl doesn't return low s, so we have to compute it r, s1 = sigdecode_der(sig) s2 = (secp256k1._r - s1) % secp256k1._r return str(hexlify(sigencode_der(r, s1 if s1 < s2 else s2)), "utf-8")
def sigcheck(a_key, a_hash_for_sig, a_sig): """ Returns True if a_key was used to generate a_sig from a_hash_for_sig; False otherwise. """ r, s = sigdecode_der(a_sig) return secp256k1_generator.verify(a_key.public_pair(), a_hash_for_sig, (r, s))
def f(solved_values, **kwargs): signature_type = kwargs.get("signature_type", DEFAULT_SIGNATURE_TYPE) generator_for_signature_type_f = kwargs[ "generator_for_signature_type_f"] signature_for_hash_type_f = m["signature_for_hash_type_f"] existing_script = kwargs.get("existing_script", b'') existing_signatures, secs_solved = _find_signatures( existing_script, generator_for_signature_type_f, signature_for_hash_type_f, len(m["sig_list"]), m["sec_list"]) sec_keys = m["sec_list"] signature_variables = m["sig_list"] signature_placeholder = kwargs.get("signature_placeholder", DEFAULT_PLACEHOLDER_SIGNATURE) db = kwargs.get("hash160_lookup", {}) # we reverse this enumeration to make the behaviour look like the old signer. BRAIN DAMAGE for signature_order, sec_key in reversed(list(enumerate(sec_keys))): sec_key = solved_values.get(sec_key, sec_key) if sec_key in secs_solved: continue if len(existing_signatures) >= len(signature_variables): break result = db.get(hash160(sec_key)) if result: secret_exponent = result[0] sig_hash = signature_for_hash_type_f(signature_type) generator = result[3] r, s = generator.sign(secret_exponent, sig_hash) else: # try existing signatures generator = generator_for_signature_type_f(signature_type) public_pair = sec_to_public_pair(sec_key, generator=generator) for sig in all_signature_hints(public_pair, signature_for_hash_type_f, **kwargs): sig_hash = signature_for_hash_type_f(indexbytes(sig, -1)) sig_pair = der.sigdecode_der(sig[:-1]) if generator.verify(public_pair, sig_hash, sig_pair): r, s = sig_pair break else: continue order = generator.order() if s + s > order: s = order - s binary_signature = der.sigencode_der(r, s) + int2byte(signature_type) existing_signatures.append((signature_order, binary_signature)) # pad with placeholder signatures if signature_placeholder is not None: while len(existing_signatures) < len(signature_variables): existing_signatures.append((-1, signature_placeholder)) existing_signatures.sort() return dict( zip(signature_variables, (es[-1] for es in existing_signatures)))
def d(): hwif = 'xprvA2nrNbFZABcdryreWet9Ea4LvTJcGsqrMzxHx98MMrotbir7yrKCEXw7nadnHM8Dq38EGfSh6dqA9QWTyefMLEcBYJUuekgW4BYPJcr9E7j' to_sign = unhexlify( '4554813e91f3d5be790c7c608f80b2b00f3ea77512d49039e9e3dc45f89e2f01') priv_key = key_from_text(hwif) sig = priv_key.sign(to_sign) r, s = sigdecode_der(sig) print(r) print(s)
def sigcheck(a_key, a_hash_for_sig, a_sig): """ Returns True if a_key was used to generate a_sig from a_hash_for_sig; False otherwise. """ r, s = sigdecode_der(a_sig) return secp256k1_generator.verify(a_key.public_pair(), a_hash_for_sig, (r, s))
def f(solved_values, **kwargs): signature_type = kwargs.get("signature_type", DEFAULT_SIGNATURE_TYPE) signature_hints = kwargs.get("signature_hints", []) generator_for_signature_type_f = kwargs["generator_for_signature_type_f"] signature_for_hash_type_f = m["signature_for_hash_type_f"] existing_script = kwargs.get("existing_script", b'') existing_signatures, secs_solved = _find_signatures( existing_script, generator_for_signature_type_f, signature_for_hash_type_f, len(m["sig_list"]), m["sec_list"]) sec_keys = m["sec_list"] signature_variables = m["sig_list"] signature_placeholder = kwargs.get("signature_placeholder", DEFAULT_PLACEHOLDER_SIGNATURE) db = kwargs.get("hash160_lookup", {}) # we reverse this enumeration to make the behaviour look like the old signer. BRAIN DAMAGE for signature_order, sec_key in reversed(list(enumerate(sec_keys))): sec_key = solved_values.get(sec_key, sec_key) if sec_key in secs_solved: continue if len(existing_signatures) >= len(signature_variables): break result = db.get(hash160(sec_key)) if result: secret_exponent = result[0] sig_hash = signature_for_hash_type_f(signature_type) generator = result[3] r, s = generator.sign(secret_exponent, sig_hash) else: # try existing signatures for sig in signature_hints: sig_hash = signature_for_hash_type_f(indexbytes(sig, -1)) generator = generator_for_signature_type_f(signature_type) public_pair = sec_to_public_pair(sec_key, generator=generator) sig_pair = der.sigdecode_der(sig[:-1]) if generator.verify(public_pair, sig_hash, sig_pair): r, s = sig_pair break else: continue order = generator.order() if s + s > order: s = order - s binary_signature = der.sigencode_der(r, s) + int2byte(signature_type) existing_signatures.append((signature_order, binary_signature)) # pad with placeholder signatures if signature_placeholder is not None: while len(existing_signatures) < len(signature_variables): existing_signatures.append((-1, signature_placeholder)) existing_signatures.sort() return dict(zip(signature_variables, (es[-1] for es in existing_signatures)))
def detect_crypto_lib(): key = network.keys.bip32_seed(bytearray(range(0, 16))) r, s1 = sigdecode_der(key.sign(bytearray(range(0, 32)))) s2 = (secp256k1._r - s1) % secp256k1._r return "libsecp256k1" if s1 < s2 else "openssl"