diff --git a/py_ecc/bls/constants.py b/py_ecc/bls/constants.py index 7f6f18b9..d9fb9f2a 100644 --- a/py_ecc/bls/constants.py +++ b/py_ecc/bls/constants.py @@ -4,6 +4,10 @@ from py_ecc.optimized_bls12_381 import ( field_modulus as q, ) +from eth_typing import ( + BLSPubkey, + BLSSignature, +) G2_cofactor = 305502333931268344200999753193121504214466019254188142667664032982267604182971884026507427359259977847832272839041616661285803823378372096355777062779109 # noqa: E501 FQ2_order = q ** 2 - 1 @@ -15,3 +19,6 @@ POW_2_381 = 2**381 POW_2_382 = 2**382 POW_2_383 = 2**383 + +EMPTY_SIGNATURE = BLSSignature(b'\xc0' + b'\x00' * 95) +EMPTY_PUBKEY = BLSPubkey(b'\xc0' + b'\x00' * 47) diff --git a/tests/test_bls.py b/tests/test_bls.py index 94173736..970c4185 100644 --- a/tests/test_bls.py +++ b/tests/test_bls.py @@ -44,6 +44,10 @@ normalize, field_modulus as q, ) +from py_ecc.bls.constants import ( + EMPTY_PUBKEY, + EMPTY_SIGNATURE, +) @pytest.mark.parametrize( @@ -78,6 +82,31 @@ def test_decompress_G2_with_no_modular_square_root_found(): signature_to_G2(b'\x11' * 96) +def test_verify_empty_pubkey_and_signature(): + """ + `verify` returns True for an empty public key and an empty signature. + The behavior is accepted for now and should be revisited in the future. + """ + assert verify(b'\x11' * 32, EMPTY_PUBKEY, EMPTY_SIGNATURE, 1000) + assert verify_multiple( + pubkeys=[], + message_hashes=[], + signature=EMPTY_SIGNATURE, + domain=1000, + ) + assert verify_multiple( + pubkeys=[EMPTY_PUBKEY, EMPTY_PUBKEY], + message_hashes=[b'\x11' * 32, b'\x12' * 32], + signature=EMPTY_SIGNATURE, + domain=1000, + ) + + +def test_empty_aggregation(): + assert aggregate_pubkeys([]) == EMPTY_PUBKEY + assert aggregate_signatures([]) == EMPTY_SIGNATURE + + @pytest.mark.parametrize( 'pt,on_curve,is_infinity', [ @@ -213,6 +242,8 @@ def test_signature_aggregation(msg, privkeys): (tuple(range(10)), tuple(range(10))), ((0, 1, 2, 3), (4, 5, 6, 7)), ((0, 1, 2, 3), (2, 3, 4, 5)), + (tuple(), (2, 3, 4, 5)), + ((0, 1, 2, 3), tuple()), ] ) def test_multi_aggregation(msg_1, msg_2, privkeys_1, privkeys_2):