Skip to content

Commit 19ebc7b

Browse files
committed
Add domain to beacon chain aggregation APIs
1 parent c17926f commit 19ebc7b

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

eth/beacon/aggregation.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
)
1717
from eth.beacon.utils.hash import hash_
1818

19+
from eth.beacon.enums.signature_domain import SignatureDomain
20+
1921

2022
def create_signing_message(slot: int,
2123
parent_hashes: Iterable[Hash32],
@@ -37,7 +39,8 @@ def create_signing_message(slot: int,
3739

3840
def verify_votes(
3941
message: bytes,
40-
votes: Iterable[Tuple[int, bytes, int]]) -> Tuple[Tuple[bytes, ...], Tuple[int, ...]]:
42+
votes: Iterable[Tuple[int, bytes, int]],
43+
domain: SignatureDomain) -> Tuple[Tuple[bytes, ...], Tuple[int, ...]]:
4144
"""
4245
Verify the given votes.
4346
@@ -47,7 +50,7 @@ def verify_votes(
4750
(sig, committee_index)
4851
for (committee_index, sig, public_key)
4952
in votes
50-
if bls.verify(message, public_key, sig)
53+
if bls.verify(message, public_key, sig, domain)
5154
)
5255
try:
5356
sigs, committee_indices = zip(*sigs_with_committe_info)

eth/utils/bls.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import ( # noqa: F401
22
Dict,
3-
Iterable,
3+
Sequence,
44
Tuple,
55
Union,
66
)
@@ -55,7 +55,7 @@ def FQP_point_to_FQ2_point(pt: Tuple[FQP, FQP, FQP]) -> Tuple[FQ2, FQ2, FQ2]:
5555
)
5656

5757

58-
def modular_squareroot(value: int) -> int:
58+
def modular_squareroot(value: int) -> FQP:
5959
"""
6060
``modular_squareroot(x)`` returns the value ``y`` such that ``y**2 % q == x``,
6161
and None if this is not possible. In cases where there are two solutions,
@@ -163,21 +163,24 @@ def verify(m: bytes, pub: int, sig: bytes, domain: int) -> bool:
163163
return final_exponentiation == FQ12.one()
164164

165165

166-
def aggregate_sigs(sigs: Iterable[bytes]) -> Tuple[int, int]:
166+
def aggregate_sigs(sigs: Sequence[bytes]) -> Tuple[int, int]:
167167
o = Z2
168168
for s in sigs:
169169
o = FQP_point_to_FQ2_point(add(o, decompress_G2(s)))
170170
return compress_G2(o)
171171

172172

173-
def aggregate_pubs(pubs: Iterable[int]) -> int:
173+
def aggregate_pubs(pubs: Sequence[int]) -> int:
174174
o = Z1
175175
for p in pubs:
176176
o = add(o, decompress_G1(p))
177177
return compress_G1(o)
178178

179179

180-
def multi_verify(pubs, msgs, sig, domain):
180+
def verify_multiple(pubs: Sequence[int],
181+
msgs: Sequence[bytes],
182+
sig: bytes,
183+
domain: int) -> bool:
181184
len_msgs = len(msgs)
182185
assert len(pubs) == len_msgs
183186

tests/beacon/test_aggregation.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,23 @@ def test_aggregate_votes(votes_count, random, privkeys, pubkeys):
3131
bit_count = 10
3232
pre_bitfield = get_empty_bitfield(bit_count)
3333
pre_sigs = ()
34+
domain = 0
3435

3536
random_votes = random.sample(range(bit_count), votes_count)
3637
message = b'hello'
3738

3839
# Get votes: (committee_index, sig, public_key)
3940
votes = [
40-
(committee_index, bls.sign(message, privkeys[committee_index]), pubkeys[committee_index])
41+
(
42+
committee_index,
43+
bls.sign(message, privkeys[committee_index], domain),
44+
pubkeys[committee_index],
45+
)
4146
for committee_index in random_votes
4247
]
4348

4449
# Verify
45-
sigs, committee_indices = verify_votes(message, votes)
50+
sigs, committee_indices = verify_votes(message, votes, domain)
4651

4752
# Aggregate the votes
4853
bitfield, sigs = aggregate_votes(
@@ -65,4 +70,4 @@ def test_aggregate_votes(votes_count, random, privkeys, pubkeys):
6570
assert len(voted_index) == len(votes)
6671

6772
aggregated_pubs = bls.aggregate_pubs(pubs)
68-
assert bls.verify(message, aggregated_pubs, sigs)
73+
assert bls.verify(message, aggregated_pubs, sigs, domain)

tests/core/bls-utils/test_bls.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
aggregate_sigs,
1616
aggregate_pubs,
1717
verify,
18-
multi_verify,
18+
verify_multiple,
1919
)
2020

2121

@@ -85,7 +85,7 @@ def test_multi_aggregation(msg_1, msg_2, privkeys):
8585
pubs = [aggpub_1, aggpub_2]
8686
aggsig = aggregate_sigs([aggsig_1, aggsig_2])
8787

88-
assert multi_verify(
88+
assert verify_multiple(
8989
pubs=pubs,
9090
msgs=msgs,
9191
sig=aggsig,

0 commit comments

Comments
 (0)