Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if not priv:
if P:
#Construct a pubkey from raw hex
self.P = secp256k1.PublicKey(safe_from_hex(P), raw=True, ctx=ctx)
else:
self.P = None
else:
if P:
raise PoDLEError("Pubkey should not be provided with privkey")
#any other formatting abnormality will just throw in PrivateKey
if len(priv)==66 and priv[-2:]=='01':
priv = priv[:-2]
self.priv = secp256k1.PrivateKey(safe_from_hex(priv), ctx=ctx)
self.P = self.priv.pubkey
if P2:
self.P2 = secp256k1.PublicKey(safe_from_hex(P2), raw=True, ctx=ctx)
else:
self.P2 = None
#These sig values should be passed in hex.
if s:
self.s = safe_from_hex(s)
if e:
self.e = safe_from_hex(e)
#Optionally maintain usage state (boolean)
self.used = used
#the H(P2) value
self.commitment = None
if not all([self.P, self.P2, self.s, self.e]):
raise PoDLEError("Verify called without sufficient data")
if not self.get_commitment() == commitment:
return False
for J in [getNUMS(i) for i in index_range]:
sig_priv = secp256k1.PrivateKey(self.s, raw=True, ctx=ctx)
sG = sig_priv.pubkey
sJ = tweak_mul(J, self.s)
e_int = decode(self.e, 256)
minus_e = encode(-e_int % N, 256, minlen=32)
minus_e_P = tweak_mul(self.P, minus_e)
minus_e_P2 = tweak_mul(self.P2, minus_e)
KG = dummy_pub.combine([sG.public_key, minus_e_P.public_key])
KJ = dummy_pub.combine([sJ.public_key, minus_e_P2.public_key])
KGser = secp256k1.PublicKey(KG, ctx=ctx).serialize()
KJser = secp256k1.PublicKey(KJ, ctx=ctx).serialize()
#check 2: e =?= H(K_G || K_J || P || P2)
e_check = hashlib.sha256(
KGser + KJser + self.P.serialize() + self.P2.serialize()).digest()
if e_check == self.e:
return True
#commitment fails for any NUMS in the provided range
return False
def __init__(self, pubkey=None, raw=False, flags=None, ctx=None):
if USE_SECP:
if flags == None:
flags = secp256k1.FLAG_VERIFY
self.obj = secp256k1.PublicKey(pubkey, raw, flags, ctx)
else:
if not raw:
raise Exception("Non raw init unsupported")
pubkey = pubkey[1:]
x = int.from_bytes(pubkey[0:32], 'big')
y = int.from_bytes(pubkey[32:], 'big')
self.obj = ECPublicKey(Point(x, y, CURVE_SECP256K1))
raise AssertionError()
if not isinstance(signature, bytes):
raise AssertionError()
digest = hashfn(message).digest()
sig = signature[1:]
# TODO: 4 means we use compressed keys.
# Grapehen uses compressed keys by default even though it would still allow
# uncompressed keys to be used. This library so far expects compressed keys
# due to this line:
recoverParameter = bytearray(signature)[0] - 4 - 27 # recover parameter only
if SECP256K1_MODULE == "secp256k1":
ALL_FLAGS = (
secp256k1.lib.SECP256K1_CONTEXT_VERIFY | secp256k1.lib.SECP256K1_CONTEXT_SIGN)
# Placeholder
pub = secp256k1.PublicKey(flags=ALL_FLAGS)
# Recover raw signature
sig = pub.ecdsa_recoverable_deserialize(sig, recoverParameter)
# Recover PublicKey
verifyPub = secp256k1.PublicKey(pub.ecdsa_recover(message, sig))
# Convert recoverable sig to normal sig
normalSig = verifyPub.ecdsa_recoverable_convert(sig)
# Verify
verifyPub.ecdsa_verify(message, normalSig)
phex = verifyPub.serialize(compressed=True)
elif SECP256K1_MODULE == "cryptography":
p = recover_public_key(digest, sig, recoverParameter, message)
order = ecdsa.SECP256k1.order
r, s = ecdsa.util.sigdecode_string(sig, order)
sigder = encode_dss_signature(r, s)
p.verify(sigder, message, ec.ECDSA(hashes.SHA256()))
phex = compressedPubkey(p)
def recoverPubkeyParameter(message, digest, signature, pubkey):
""" Use to derive a number that allows to easily recover the
public key from the signature
"""
if not isinstance(message, bytes):
message = bytes(message, "utf-8") # pragma: no cover
for i in range(0, 4):
if SECP256K1_MODULE == "secp256k1": # pragma: no cover
sig = pubkey.ecdsa_recoverable_deserialize(signature, i)
p = secp256k1.PublicKey(pubkey.ecdsa_recover(message, sig))
if p.serialize() == pubkey.serialize():
return i
elif SECP256K1_MODULE == "cryptography" and not isinstance(pubkey, PublicKey):
p = recover_public_key(digest, signature, i, message)
p_comp = hexlify(compressedPubkey(p))
pubkey_comp = hexlify(compressedPubkey(pubkey))
if p_comp == pubkey_comp:
return i
else: # pragma: no cover
p = recover_public_key(digest, signature, i)
p_comp = hexlify(compressedPubkey(p))
p_string = hexlify(p.to_string())
if isinstance(pubkey, PublicKey): # pragma: no cover
pubkey_string = bytes(repr(pubkey), "ascii")
else: # pragma: no cover
pubkey_string = hexlify(pubkey.to_string())
def verify_request_with_pub(request,pub_key,hex_sig):
""" Verifies a request (in JSON format) was signed in a valid way
Returns either True or False
"""
request = json.loads(canon_request(json.loads(request)))
del request['params']['AuthSig']
del request['params']['AuthKey']
request = canon_request(request)
digest = hashlib.sha256(request.encode('utf-8')).digest()
logger.info('verify_request_with_pub using digest %s' % hexlify(digest))
pub_key = Base58(pub_key)
ecdsa_pub = secp256k1.PublicKey(bytes(pub_key),raw=True)
ecdsa_sig = ecdsa_pub.ecdsa_deserialize(unhexlify(hex_sig))
return ecdsa_pub.ecdsa_verify(digest,ecdsa_sig,raw=True)
def recover_pubkey_parameter(self, digest, signature, pubkey):
for i in range(0, 4):
if USE_SECP256K1:
sig = pubkey.ecdsa_recoverable_deserialize(signature, i)
p = secp256k1.PublicKey(
pubkey.ecdsa_recover(self.message, sig))
if p.serialize() == pubkey.serialize():
return i
else:
p = self.recover_public_key(digest, signature, i)
if (p.to_string() == pubkey.to_string()
or self.compressed_pubkey(p) == pubkey.to_string()):
return i
return None
def _decode_public_key(serialized_public_key, encoding_format='hex'):
if encoding_format == 'hex':
serialized_public_key = binascii.unhexlify(serialized_public_key)
elif encoding_format != 'bytes':
raise ValueError("Unrecognized public_key encoding format")
pub = __PK__.deserialize(serialized_public_key)
return secp256k1.PublicKey(pub, ctx=__CTX__)
def add_pubkeys(pubkeys, usehex):
'''Input a list of binary compressed pubkeys
and return their sum as a binary compressed pubkey.'''
r = secp256k1.PublicKey(ctx=ctx) #dummy holding object
pubkey_list = [secp256k1.PublicKey(x,
raw=True,
ctx=ctx).public_key for x in pubkeys]
r.combine(pubkey_list)
return r.serialize()