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-03-13 13:59:27 +00:00
|
|
|
from py_ecc.bls import G2ProofOfPossession as bls_pop
|
|
|
|
|
2024-03-14 11:20:46 +00:00
|
|
|
from da.common import Certificate, NodeId, BLSPublickey, Bitfield
|
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]
|
|
|
|
nodes_pubkey: List[BLSPublickey]
|
|
|
|
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-14 11:20:46 +00:00
|
|
|
# sort nodes_ids and related public keys
|
|
|
|
self.settings.nodes_ids, self.settings.nodes_pubkey = zip(
|
|
|
|
*sorted(zip(self.settings.nodes_ids, self.settings.nodes_pubkey), key=lambda x: x[0])
|
|
|
|
)
|
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
|
|
|
|
blobs_data = enumerate(zip(columns, column_commitments, zip(*rows_proofs), aggregated_column_proofs))
|
|
|
|
for index, (column, column_commitment, row_proofs, column_proof) in blobs_data:
|
2024-02-12 14:35:23 +00:00
|
|
|
blob = DABlob(
|
|
|
|
index,
|
|
|
|
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
|
|
|
|
def _verify_attestation(public_key: BLSPublickey, attested_message: bytes, attestation: Attestation) -> bool:
|
|
|
|
return bls_pop.Verify(public_key, attested_message, attestation.signature)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _build_attestation_message(encoded_data: EncodedData) -> bytes:
|
|
|
|
hasher = sha3_256()
|
|
|
|
hasher.update(bytes(encoded_data.aggregated_column_commitment))
|
|
|
|
for c in encoded_data.row_commitments:
|
|
|
|
hasher.update(bytes(c))
|
|
|
|
return hasher.digest()
|
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)
|