2024-02-12 14:35:23 +00:00
|
|
|
from dataclasses import dataclass
|
2024-03-11 09:01:34 +00:00
|
|
|
from hashlib import sha3_256
|
2024-03-22 09:36:00 +00:00
|
|
|
from typing import List, Optional, Sequence, Set, Dict
|
2024-03-11 09:01:34 +00:00
|
|
|
|
|
|
|
from eth2spec.deneb.mainnet import BLSFieldElement
|
2024-02-12 14:35:23 +00:00
|
|
|
from eth2spec.eip7594.mainnet import (
|
|
|
|
KZGCommitment as Commitment,
|
|
|
|
KZGProof as Proof,
|
|
|
|
)
|
2024-03-13 13:59:27 +00:00
|
|
|
from py_ecc.bls import G2ProofOfPossession as bls_pop
|
2024-02-12 14:35:23 +00:00
|
|
|
|
2024-03-22 09:36:00 +00:00
|
|
|
import da.common
|
2024-03-25 10:30:44 +00:00
|
|
|
from da.common import Column, Chunk, Attestation, BLSPrivateKey, BLSPublicKey
|
2024-03-11 09:01:34 +00:00
|
|
|
from da.encoder import DAEncoder
|
|
|
|
from da.kzg_rs import kzg
|
|
|
|
from da.kzg_rs.common import ROOTS_OF_UNITY, GLOBAL_PARAMETERS, BLS_MODULUS
|
2024-02-12 14:35:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class DABlob:
|
2024-03-11 09:01:34 +00:00
|
|
|
column: Column
|
2024-02-12 14:35:23 +00:00
|
|
|
column_commitment: Commitment
|
|
|
|
aggregated_column_commitment: Commitment
|
|
|
|
aggregated_column_proof: Proof
|
|
|
|
rows_commitments: List[Commitment]
|
|
|
|
rows_proofs: List[Proof]
|
|
|
|
|
2024-03-22 09:36:00 +00:00
|
|
|
def id(self) -> bytes:
|
|
|
|
return da.common.build_attestation_message(self.aggregated_column_commitment, self.rows_commitments)
|
|
|
|
|
|
|
|
def column_id(self) -> bytes:
|
|
|
|
return sha3_256(self.column.as_bytes()).digest()
|
|
|
|
|
2024-02-12 14:35:23 +00:00
|
|
|
|
|
|
|
class DAVerifier:
|
2024-03-25 10:30:44 +00:00
|
|
|
def __init__(self, sk: BLSPrivateKey, nodes_pks: List[BLSPublicKey]):
|
2024-03-22 09:36:00 +00:00
|
|
|
self.attested_blobs: Dict[bytes, (bytes, Attestation)] = dict()
|
2024-03-11 09:01:34 +00:00
|
|
|
self.sk = sk
|
2024-03-25 10:30:44 +00:00
|
|
|
self.index = nodes_pks.index(bls_pop.SkToPk(self.sk))
|
2024-02-12 14:35:23 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _verify_column(
|
|
|
|
column: Column,
|
|
|
|
column_commitment: Commitment,
|
|
|
|
aggregated_column_commitment: Commitment,
|
|
|
|
aggregated_column_proof: Proof,
|
|
|
|
index: int
|
|
|
|
) -> bool:
|
|
|
|
# 1. compute commitment for column
|
2024-03-11 09:01:34 +00:00
|
|
|
_, computed_column_commitment = kzg.bytes_to_commitment(column.as_bytes(), GLOBAL_PARAMETERS)
|
2024-02-12 14:35:23 +00:00
|
|
|
# 2. If computed column commitment != column commitment, fail
|
2024-03-11 09:01:34 +00:00
|
|
|
if column_commitment != computed_column_commitment:
|
|
|
|
return False
|
2024-02-12 14:35:23 +00:00
|
|
|
# 3. compute column hash
|
2024-03-11 09:01:34 +00:00
|
|
|
column_hash = DAEncoder.hash_column_and_commitment(column, column_commitment)
|
2024-02-12 14:35:23 +00:00
|
|
|
# 4. Check proof with commitment and proof over the aggregated column commitment
|
2024-03-11 09:01:34 +00:00
|
|
|
chunk = BLSFieldElement.from_bytes(column_hash)
|
|
|
|
return kzg.verify_element_proof(
|
|
|
|
chunk, aggregated_column_commitment, aggregated_column_proof, index, ROOTS_OF_UNITY
|
|
|
|
)
|
2024-02-12 14:35:23 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2024-03-11 09:01:34 +00:00
|
|
|
def _verify_chunk(chunk: Chunk, commitment: Commitment, proof: Proof, index: int) -> bool:
|
|
|
|
chunk = BLSFieldElement(int.from_bytes(bytes(chunk)) % BLS_MODULUS)
|
|
|
|
return kzg.verify_element_proof(chunk, commitment, proof, index, ROOTS_OF_UNITY)
|
2024-02-12 14:35:23 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _verify_chunks(
|
2024-03-11 09:01:34 +00:00
|
|
|
chunks: Sequence[Chunk],
|
|
|
|
commitments: Sequence[Commitment],
|
|
|
|
proofs: Sequence[Proof],
|
|
|
|
index: int
|
2024-02-12 14:35:23 +00:00
|
|
|
) -> bool:
|
2024-03-11 09:01:34 +00:00
|
|
|
if not (len(chunks) == len(commitments) == len(proofs)):
|
|
|
|
return False
|
2024-02-12 14:35:23 +00:00
|
|
|
for chunk, commitment, proof in zip(chunks, commitments, proofs):
|
2024-03-11 09:01:34 +00:00
|
|
|
if not DAVerifier._verify_chunk(chunk, commitment, proof, index):
|
2024-02-12 14:35:23 +00:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2024-03-13 13:59:27 +00:00
|
|
|
def _build_attestation(self, blob: DABlob) -> Attestation:
|
|
|
|
hasher = sha3_256()
|
|
|
|
hasher.update(bytes(blob.aggregated_column_commitment))
|
|
|
|
for c in blob.rows_commitments:
|
|
|
|
hasher.update(bytes(c))
|
|
|
|
message = hasher.digest()
|
|
|
|
return Attestation(signature=bls_pop.Sign(self.sk, message))
|
2024-02-12 14:35:23 +00:00
|
|
|
|
|
|
|
def verify(self, blob: DABlob) -> Optional[Attestation]:
|
2024-03-22 09:36:00 +00:00
|
|
|
blob_id = blob.id()
|
|
|
|
if previous_attestation := self.attested_blobs.get(blob_id):
|
|
|
|
column_id, attestation = previous_attestation
|
|
|
|
# we already attested, is cached so we return it
|
|
|
|
if column_id == blob.column_id():
|
|
|
|
return attestation
|
|
|
|
# we already attested and they are asking us to attest the same data different column
|
|
|
|
# skip
|
|
|
|
return None
|
2024-02-12 14:35:23 +00:00
|
|
|
is_column_verified = DAVerifier._verify_column(
|
2024-03-11 09:01:34 +00:00
|
|
|
blob.column,
|
|
|
|
blob.column_commitment,
|
|
|
|
blob.aggregated_column_commitment,
|
|
|
|
blob.aggregated_column_proof,
|
2024-03-25 10:30:44 +00:00
|
|
|
self.index
|
2024-02-12 14:35:23 +00:00
|
|
|
)
|
|
|
|
if not is_column_verified:
|
|
|
|
return
|
|
|
|
are_chunks_verified = DAVerifier._verify_chunks(
|
2024-03-25 10:30:44 +00:00
|
|
|
blob.column, blob.rows_commitments, blob.rows_proofs, self.index
|
2024-02-12 14:35:23 +00:00
|
|
|
)
|
|
|
|
if not are_chunks_verified:
|
|
|
|
return
|
2024-03-22 09:36:00 +00:00
|
|
|
attestation = self._build_attestation(blob)
|
|
|
|
self.attested_blobs[blob_id] = (blob.column_id(), attestation)
|
|
|
|
return attestation
|