2024-02-12 14:35:23 +00:00
|
|
|
from dataclasses import dataclass
|
2024-03-15 10:34:43 +00:00
|
|
|
from hashlib import sha3_256
|
|
|
|
from itertools import chain, zip_longest, compress
|
|
|
|
from typing import List, Generator, Self, Sequence
|
2024-03-13 13:59:27 +00:00
|
|
|
|
|
|
|
from eth2spec.eip7594.mainnet import Bytes32, KZGCommitment as Commitment
|
2024-03-15 10:34:43 +00:00
|
|
|
from py_ecc.bls import G2ProofOfPossession as bls_pop
|
2024-02-12 14:35:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NodeId(Bytes32):
|
|
|
|
pass
|
|
|
|
|
2024-03-13 13:59:27 +00:00
|
|
|
|
2024-03-20 10:03:39 +00:00
|
|
|
class Chunk(bytes):
|
2024-02-12 14:35:23 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2024-03-08 13:16:14 +00:00
|
|
|
class Column(List[Bytes32]):
|
|
|
|
def as_bytes(self) -> bytes:
|
|
|
|
return bytes(chain.from_iterable(self))
|
2024-02-12 14:35:23 +00:00
|
|
|
|
|
|
|
|
2024-03-08 13:16:14 +00:00
|
|
|
class Row(List[Bytes32]):
|
|
|
|
def as_bytes(self) -> bytes:
|
|
|
|
return bytes(chain.from_iterable(self))
|
2024-02-12 14:35:23 +00:00
|
|
|
|
|
|
|
|
2024-03-08 13:16:14 +00:00
|
|
|
class ChunksMatrix(List[Row | Column]):
|
|
|
|
@property
|
2024-02-12 14:35:23 +00:00
|
|
|
def columns(self) -> Generator[List[Chunk], None, None]:
|
2024-03-08 13:16:14 +00:00
|
|
|
yield from map(Column, zip_longest(*self, fillvalue=b""))
|
|
|
|
|
|
|
|
def transposed(self) -> Self:
|
|
|
|
return ChunksMatrix(self.columns)
|
2024-02-12 14:35:23 +00:00
|
|
|
|
|
|
|
|
2024-03-25 10:30:44 +00:00
|
|
|
BLSPublicKey = bytes
|
2024-03-13 13:59:27 +00:00
|
|
|
BLSPrivateKey = int
|
|
|
|
BLSSignature = bytes
|
2024-02-12 14:35:23 +00:00
|
|
|
|
|
|
|
|
2024-03-14 11:20:46 +00:00
|
|
|
class Bitfield(List[bool]):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2024-02-12 14:35:23 +00:00
|
|
|
@dataclass
|
|
|
|
class Attestation:
|
2024-03-13 13:59:27 +00:00
|
|
|
signature: BLSSignature
|
2024-02-12 14:35:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class Certificate:
|
2024-03-13 13:59:27 +00:00
|
|
|
aggregated_signatures: BLSSignature
|
2024-03-14 11:20:46 +00:00
|
|
|
signers: Bitfield
|
2024-03-13 13:59:27 +00:00
|
|
|
aggregated_column_commitment: Commitment
|
|
|
|
row_commitments: List[Commitment]
|
2024-02-12 14:35:23 +00:00
|
|
|
|
2024-03-22 11:01:13 +00:00
|
|
|
def id(self) -> bytes:
|
|
|
|
return build_attestation_message(self.aggregated_column_commitment, self.row_commitments)
|
|
|
|
|
2024-03-25 10:30:44 +00:00
|
|
|
def verify(self, nodes_public_keys: List[BLSPublicKey]) -> bool:
|
2024-03-15 10:34:43 +00:00
|
|
|
"""
|
|
|
|
List of nodes public keys should be a trusted list of verified proof of possession keys.
|
|
|
|
Otherwise, we could fall under the Rogue Key Attack
|
|
|
|
`assert all(bls_pop.PopVerify(pk, proof) for pk, proof in zip(node_public_keys, pops))`
|
|
|
|
"""
|
|
|
|
# we sort them as the signers bitfield is sorted by the public keys as well
|
|
|
|
signers_keys = list(compress(sorted(nodes_public_keys), self.signers))
|
|
|
|
message = build_attestation_message(self.aggregated_column_commitment, self.row_commitments)
|
|
|
|
return bls_pop.AggregateVerify(signers_keys, [message]*len(signers_keys), self.aggregated_signatures)
|
|
|
|
|
|
|
|
|
|
|
|
def build_attestation_message(aggregated_column_commitment: Commitment, row_commitments: Sequence[Commitment]) -> bytes:
|
|
|
|
hasher = sha3_256()
|
|
|
|
hasher.update(bytes(aggregated_column_commitment))
|
|
|
|
for c in row_commitments:
|
|
|
|
hasher.update(bytes(c))
|
2024-03-22 11:01:13 +00:00
|
|
|
return hasher.digest()
|