Skip to content

Commit e048182

Browse files
committed
add validate_attestation_aggregate_signature. Need tests
1 parent 7786c3a commit e048182

File tree

5 files changed

+65
-7
lines changed

5 files changed

+65
-7
lines changed

eth/beacon/state_machines/validation.py

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,21 @@
88
ZERO_HASH32,
99
)
1010

11+
import rlp
1112

13+
from eth.beacon.enums import (
14+
SignatureDomain,
15+
)
1216
from eth.beacon.helpers import (
17+
get_attestation_participants,
1318
get_block_hash,
19+
get_domain,
20+
)
21+
from eth.utils import (
22+
bls
23+
)
24+
from eth.beacon.utils.hash import (
25+
hash_,
1426
)
1527
from eth.beacon.types.states import BeaconState # noqa: F401
1628
from eth.beacon.types.attestations import Attestation # noqa: F401
@@ -192,5 +204,47 @@ def validate_attestation_shard_block_root(attestation_data: AttestationData) ->
192204

193205

194206
def validate_attestation_aggregate_signature(state: BeaconState,
195-
attestation: Attestation) -> None:
196-
pass
207+
attestation: Attestation,
208+
epoch_length: int) -> None:
209+
"""
210+
Validate ``aggregate_signature`` field of ``attestation``.
211+
Raise ``ValidationError`` if it's invalid.
212+
213+
Note: This is the phase 0 version of `aggregate_signature`` validation.
214+
All proof of custody bits are assumed to be 0 within the signed data.
215+
This will change to reflect real proof of custody bits in the Phase 1.
216+
"""
217+
participant_indices = get_attestation_participants(
218+
state=state,
219+
slot=attestation.slot,
220+
shard=attestation.shard,
221+
participation_bitfield=attestation.participation_bitfield,
222+
epoch_length=epoch_length,
223+
)
224+
225+
pubkeys = [
226+
state.validator_registry[validator_index].pubkey
227+
for validator_index in participant_indices
228+
]
229+
group_public_key = bls.aggregate_pubkeys(pubkeys)
230+
231+
message = hash_(
232+
rlp.encode(attestation.data) +
233+
(0).to_bytes(1, "big")
234+
)
235+
236+
is_valid_signature = bls.verify(
237+
message=message,
238+
pubkey=group_public_key,
239+
signature=attestation.aggregate_signature,
240+
domain=get_domain(
241+
fork_data=state.fork_data,
242+
slot=attestation.data.slot,
243+
domain_type=SignatureDomain.DOMAIN_ATTESTATION,
244+
),
245+
246+
)
247+
if not is_valid_signature:
248+
raise ValidationError(
249+
"Attestation ``aggregate_signature`` is invalid."
250+
)

eth/beacon/types/attestations.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@ class Attestation(rlp.Serializable):
2929
# Proof of custody bitfield
3030
('custody_bitfield', binary),
3131
# BLS aggregate signature
32-
('aggregate_sig', CountableList(uint384)),
32+
('aggregate_signature', CountableList(uint384)),
3333
]
3434

3535
def __init__(self,
3636
data: AttestationData,
3737
participation_bitfield: bytes,
3838
custody_bitfield: bytes,
39-
aggregate_sig: Sequence[int]=(0, 0)) -> None:
39+
aggregate_signature: Sequence[int]=(0, 0)) -> None:
4040
super().__init__(
4141
data,
4242
participation_bitfield,
4343
custody_bitfield,
44-
aggregate_sig,
44+
aggregate_signature,
4545
)

tests/beacon/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def sample_attestation_params(sample_attestation_data_params):
8282
'data': AttestationData(**sample_attestation_data_params),
8383
'participation_bitfield': b'\12' * 16,
8484
'custody_bitfield': b'\34' * 16,
85-
'aggregate_sig': [0, 0],
85+
'aggregate_signature': [0, 0],
8686
}
8787

8888

tests/beacon/state_machines/test_attestation_validation.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,7 @@ def test_validate_attestation_shard_block_root(sample_attestation_data_params,
203203
validate_attestation_shard_block_root(
204204
attestation_data,
205205
)
206+
207+
208+
def test_validate_attestation_aggregate_signature():
209+
pass

tests/beacon/types/test_attestation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
@pytest.mark.parametrize(
99
'param,default_value',
1010
[
11-
('aggregate_sig', (0, 0)),
11+
('aggregate_signature', (0, 0)),
1212
]
1313
)
1414
def test_defaults(param, default_value, sample_attestation_params):

0 commit comments

Comments
 (0)