2020-05-11 17:18:49 +00:00
|
|
|
from py_ecc.bls import G2ProofOfPossession as py_ecc_bls
|
2020-01-07 19:52:20 +00:00
|
|
|
from py_ecc.bls.g2_primatives import signature_to_G2 as _signature_to_G2
|
2020-05-11 17:18:49 +00:00
|
|
|
import milagro_bls_binding as milagro_bls # noqa: F401 for BLS switching option
|
2019-05-13 21:15:02 +00:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2020-05-11 17:18:49 +00:00
|
|
|
# To change bls implementation, default to PyECC for correctness. Milagro is a good faster alternative.
|
|
|
|
bls = py_ecc_bls
|
|
|
|
|
2019-05-21 20:28:47 +00:00
|
|
|
STUB_SIGNATURE = b'\x11' * 96
|
|
|
|
STUB_PUBKEY = b'\x22' * 48
|
2020-05-20 17:35:40 +00:00
|
|
|
Z2_SIGNATURE = b'\xc0' + b'\x00' * 95
|
|
|
|
STUB_COORDINATES = _signature_to_G2(Z2_SIGNATURE)
|
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-12-17 10:04:56 +00:00
|
|
|
def Verify(PK, message, signature):
|
2020-05-14 16:45:26 +00:00
|
|
|
try:
|
|
|
|
result = bls.Verify(PK, message, signature)
|
|
|
|
except Exception:
|
|
|
|
result = False
|
|
|
|
finally:
|
|
|
|
return result
|
2019-12-17 10:04:56 +00:00
|
|
|
|
|
|
|
|
2019-12-17 13:33:37 +00:00
|
|
|
@only_with_bls(alt_return=True)
|
2020-05-09 03:48:48 +00:00
|
|
|
def AggregateVerify(pubkeys, messages, signature):
|
2020-05-14 16:45:26 +00:00
|
|
|
try:
|
2020-05-20 08:26:43 +00:00
|
|
|
result = bls.AggregateVerify(list(pubkeys), list(messages), signature)
|
2020-05-14 16:45:26 +00:00
|
|
|
except Exception:
|
|
|
|
result = False
|
|
|
|
finally:
|
|
|
|
return result
|
2019-05-13 21:15:02 +00:00
|
|
|
|
|
|
|
|
2019-05-21 20:28:47 +00:00
|
|
|
@only_with_bls(alt_return=True)
|
2020-05-09 03:48:48 +00:00
|
|
|
def FastAggregateVerify(pubkeys, message, signature):
|
2020-05-14 16:45:26 +00:00
|
|
|
try:
|
2020-05-20 08:26:43 +00:00
|
|
|
result = bls.FastAggregateVerify(list(pubkeys), message, signature)
|
2020-05-14 16:45:26 +00:00
|
|
|
except Exception:
|
|
|
|
result = False
|
|
|
|
finally:
|
|
|
|
return result
|
2019-05-13 21:15:02 +00:00
|
|
|
|
|
|
|
|
2019-05-21 20:28:47 +00:00
|
|
|
@only_with_bls(alt_return=STUB_SIGNATURE)
|
2019-12-17 10:04:56 +00:00
|
|
|
def Aggregate(signatures):
|
2020-01-07 19:52:20 +00:00
|
|
|
return bls.Aggregate(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-12-17 10:04:56 +00:00
|
|
|
def Sign(SK, message):
|
2020-05-11 17:18:49 +00:00
|
|
|
if bls == py_ecc_bls:
|
|
|
|
return bls.Sign(SK, message)
|
|
|
|
else:
|
2020-05-20 17:35:40 +00:00
|
|
|
return bls.Sign(SK.to_bytes(32, 'big'), message)
|
2019-08-11 17:05:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
@only_with_bls(alt_return=STUB_COORDINATES)
|
2019-12-20 06:41:46 +00:00
|
|
|
def signature_to_G2(signature):
|
2020-01-07 19:52:20 +00:00
|
|
|
return _signature_to_G2(signature)
|
2020-04-23 07:08:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
@only_with_bls(alt_return=STUB_PUBKEY)
|
|
|
|
def AggregatePKs(pubkeys):
|
2020-05-11 17:18:49 +00:00
|
|
|
return bls._AggregatePKs(list(pubkeys))
|
2020-05-14 16:45:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
@only_with_bls(alt_return=STUB_SIGNATURE)
|
|
|
|
def SkToPk(SK):
|
|
|
|
return bls.SkToPk(SK)
|