2019-05-13 21:15:02 +00:00
|
|
|
from py_ecc import bls
|
|
|
|
|
|
|
|
# Flag to make BLS active or not. Used for testing, do not ignore BLS in production unless you know what you are doing.
|
|
|
|
bls_active = True
|
|
|
|
|
2019-05-21 20:28:47 +00:00
|
|
|
STUB_SIGNATURE = b'\x11' * 96
|
|
|
|
STUB_PUBKEY = b'\x22' * 48
|
2019-08-11 17:05:17 +00:00
|
|
|
STUB_COORDINATES = bls.api.signature_to_G2(bls.sign(b"", 0, b"\0" * 8))
|
2019-05-13 21:15:02 +00:00
|
|
|
|
2019-05-21 20:28:47 +00:00
|
|
|
|
|
|
|
def only_with_bls(alt_return=None):
|
|
|
|
"""
|
|
|
|
Decorator factory to make a function only run when BLS is active. Otherwise return the default.
|
|
|
|
"""
|
|
|
|
def runner(fn):
|
|
|
|
def entry(*args, **kw):
|
|
|
|
if bls_active:
|
|
|
|
return fn(*args, **kw)
|
|
|
|
else:
|
|
|
|
return alt_return
|
|
|
|
return entry
|
|
|
|
return runner
|
|
|
|
|
|
|
|
|
|
|
|
@only_with_bls(alt_return=True)
|
2019-05-13 21:15:02 +00:00
|
|
|
def bls_verify(pubkey, message_hash, signature, domain):
|
2019-06-30 20:12:02 +00:00
|
|
|
return bls.verify(message_hash=message_hash, pubkey=pubkey,
|
2019-07-16 06:27:34 +00:00
|
|
|
signature=signature, domain=domain)
|
2019-05-13 21:15:02 +00:00
|
|
|
|
|
|
|
|
2019-05-21 20:28:47 +00:00
|
|
|
@only_with_bls(alt_return=True)
|
2019-05-13 21:15:02 +00:00
|
|
|
def bls_verify_multiple(pubkeys, message_hashes, signature, domain):
|
2019-06-30 22:00:54 +00:00
|
|
|
return bls.verify_multiple(pubkeys=pubkeys, message_hashes=message_hashes,
|
2019-07-16 06:27:34 +00:00
|
|
|
signature=signature, domain=domain)
|
2019-05-13 21:15:02 +00:00
|
|
|
|
|
|
|
|
2019-05-21 20:28:47 +00:00
|
|
|
@only_with_bls(alt_return=STUB_PUBKEY)
|
2019-05-13 21:15:02 +00:00
|
|
|
def bls_aggregate_pubkeys(pubkeys):
|
2019-05-21 20:28:47 +00:00
|
|
|
return bls.aggregate_pubkeys(pubkeys)
|
2019-05-15 16:37:11 +00:00
|
|
|
|
|
|
|
|
2019-05-21 20:28:47 +00:00
|
|
|
@only_with_bls(alt_return=STUB_SIGNATURE)
|
2019-05-15 16:37:11 +00:00
|
|
|
def bls_aggregate_signatures(signatures):
|
2019-05-21 20:28:47 +00:00
|
|
|
return bls.aggregate_signatures(signatures)
|
2019-05-15 16:37:11 +00:00
|
|
|
|
|
|
|
|
2019-05-21 20:28:47 +00:00
|
|
|
@only_with_bls(alt_return=STUB_SIGNATURE)
|
2019-05-15 16:37:11 +00:00
|
|
|
def bls_sign(message_hash, privkey, domain):
|
2019-06-30 20:12:02 +00:00
|
|
|
return bls.sign(message_hash=message_hash, privkey=privkey,
|
2019-07-16 06:27:34 +00:00
|
|
|
domain=domain)
|
2019-08-11 17:05:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
@only_with_bls(alt_return=STUB_COORDINATES)
|
|
|
|
def bls_signature_to_G2(signature):
|
|
|
|
return bls.api.signature_to_G2(signature)
|