2024-03-13 13:59:27 +00:00
|
|
|
from dataclasses import dataclass
|
|
|
|
from hashlib import sha3_256
|
|
|
|
from typing import List, Optional, Generator, Sequence
|
2024-02-12 14:35:23 +00:00
|
|
|
|
2024-05-27 09:38:18 +00:00
|
|
|
from da.common import Certificate, NodeId, BLSPublicKey, Bitfield, build_attestation_message, NomosDaG2ProofOfPossession as bls_pop
|
2024-02-12 14:35:23 +00:00
|
|
|
from da.encoder import EncodedData
|
|
|
|
from da.verifier import DABlob, Attestation
|
|
|
|
|
|
|
|
|
2024-03-13 13:59:27 +00:00
|
|
|
@dataclass
|
|
|
|
class DispersalSettings:
|
|
|
|
nodes_ids: List[NodeId]
|
2024-03-25 10:30:44 +00:00
|
|
|
nodes_pubkey: List[BLSPublicKey]
|
2024-03-13 13:59:27 +00:00
|
|
|
threshold: int
|
|
|
|
|
|
|
|
|
2024-02-12 14:35:23 +00:00
|
|
|
class Dispersal:
|
2024-03-13 13:59:27 +00:00
|
|
|
def __init__(self, settings: DispersalSettings):
|
|
|
|
self.settings = settings
|
2024-03-15 10:34:43 +00:00
|
|
|
# sort over public keys
|
2024-03-14 11:20:46 +00:00
|
|
|
self.settings.nodes_ids, self.settings.nodes_pubkey = zip(
|
2024-03-15 10:34:43 +00:00
|
|
|
*sorted(zip(self.settings.nodes_ids, self.settings.nodes_pubkey), key=lambda x: x[1])
|
2024-03-14 11:20:46 +00:00
|
|
|
)
|
2024-02-12 14:35:23 +00:00
|
|
|
|
|
|
|
def _prepare_data(self, encoded_data: EncodedData) -> Generator[DABlob, None, None]:
|
2024-03-13 13:59:27 +00:00
|
|
|
assert len(encoded_data.column_commitments) == len(self.settings.nodes_ids)
|
|
|
|
assert len(encoded_data.aggregated_column_proofs) == len(self.settings.nodes_ids)
|
2024-03-08 13:16:14 +00:00
|
|
|
columns = encoded_data.extended_matrix.columns
|
2024-02-12 14:35:23 +00:00
|
|
|
column_commitments = encoded_data.column_commitments
|
|
|
|
row_commitments = encoded_data.row_commitments
|
|
|
|
rows_proofs = encoded_data.row_proofs
|
|
|
|
aggregated_column_commitment = encoded_data.aggregated_column_commitment
|
2024-03-13 13:59:27 +00:00
|
|
|
aggregated_column_proofs = encoded_data.aggregated_column_proofs
|
2024-03-25 10:30:44 +00:00
|
|
|
blobs_data = zip(columns, column_commitments, zip(*rows_proofs), aggregated_column_proofs)
|
|
|
|
for (column, column_commitment, row_proofs, column_proof) in blobs_data:
|
2024-02-12 14:35:23 +00:00
|
|
|
blob = DABlob(
|
|
|
|
column,
|
|
|
|
column_commitment,
|
|
|
|
aggregated_column_commitment,
|
2024-03-13 13:59:27 +00:00
|
|
|
column_proof,
|
2024-02-12 14:35:23 +00:00
|
|
|
row_commitments,
|
|
|
|
row_proofs
|
|
|
|
)
|
|
|
|
yield blob
|
|
|
|
|
2024-03-13 13:59:27 +00:00
|
|
|
def _send_and_await_response(self, node: NodeId, blob: DABlob) -> Optional[Attestation]:
|
2024-02-12 14:35:23 +00:00
|
|
|
pass
|
|
|
|
|
2024-03-14 11:20:46 +00:00
|
|
|
def _build_certificate(
|
|
|
|
self,
|
|
|
|
encoded_data: EncodedData,
|
|
|
|
attestations: Sequence[Attestation],
|
|
|
|
signers: Bitfield
|
|
|
|
) -> Certificate:
|
2024-03-13 13:59:27 +00:00
|
|
|
assert len(attestations) >= self.settings.threshold
|
2024-03-14 11:20:46 +00:00
|
|
|
assert len(attestations) == signers.count(True)
|
2024-03-13 13:59:27 +00:00
|
|
|
aggregated = bls_pop.Aggregate([attestation.signature for attestation in attestations])
|
|
|
|
return Certificate(
|
|
|
|
aggregated_signatures=aggregated,
|
2024-03-14 11:20:46 +00:00
|
|
|
signers=signers,
|
2024-03-13 13:59:27 +00:00
|
|
|
aggregated_column_commitment=encoded_data.aggregated_column_commitment,
|
|
|
|
row_commitments=encoded_data.row_commitments
|
|
|
|
)
|
2024-02-12 14:35:23 +00:00
|
|
|
|
2024-03-13 13:59:27 +00:00
|
|
|
@staticmethod
|
2024-03-25 10:30:44 +00:00
|
|
|
def _verify_attestation(public_key: BLSPublicKey, attested_message: bytes, attestation: Attestation) -> bool:
|
2024-03-13 13:59:27 +00:00
|
|
|
return bls_pop.Verify(public_key, attested_message, attestation.signature)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _build_attestation_message(encoded_data: EncodedData) -> bytes:
|
2024-03-15 10:34:43 +00:00
|
|
|
return build_attestation_message(encoded_data.aggregated_column_commitment, encoded_data.row_commitments)
|
2024-02-12 14:35:23 +00:00
|
|
|
|
|
|
|
def disperse(self, encoded_data: EncodedData) -> Optional[Certificate]:
|
|
|
|
attestations = []
|
2024-03-13 13:59:27 +00:00
|
|
|
attested_message = self._build_attestation_message(encoded_data)
|
2024-03-14 11:20:46 +00:00
|
|
|
signed = Bitfield(False for _ in range(len(self.settings.nodes_ids)))
|
|
|
|
blob_data = zip(
|
|
|
|
range(len(self.settings.nodes_ids)),
|
|
|
|
self.settings.nodes_ids,
|
|
|
|
self.settings.nodes_pubkey,
|
|
|
|
self._prepare_data(encoded_data)
|
|
|
|
)
|
|
|
|
for i, node, pk, blob in blob_data:
|
2024-02-12 14:35:23 +00:00
|
|
|
if attestation := self._send_and_await_response(node, blob):
|
2024-03-13 13:59:27 +00:00
|
|
|
if self._verify_attestation(pk, attested_message, attestation):
|
2024-03-14 11:20:46 +00:00
|
|
|
# mark as received
|
|
|
|
signed[i] = True
|
2024-02-12 14:35:23 +00:00
|
|
|
attestations.append(attestation)
|
2024-03-13 13:59:27 +00:00
|
|
|
if len(attestations) >= self.settings.threshold:
|
2024-03-14 11:20:46 +00:00
|
|
|
return self._build_certificate(encoded_data, attestations, signed)
|