diff --git a/da/kzg_rs.py b/da/kzg_rs.py index c92f951..ec47d41 100644 --- a/da/kzg_rs.py +++ b/da/kzg_rs.py @@ -3,7 +3,7 @@ from typing import List, Sequence from eth2spec.eip7594.mainnet import ( bit_reversal_permutation, KZG_SETUP_G1_LAGRANGE, Polynomial, - BYTES_PER_FIELD_ELEMENT, bytes_to_bls_field, BLSFieldElement, + BYTES_PER_FIELD_ELEMENT, bytes_to_bls_field, BLSFieldElement, compute_kzg_proof_impl, KZG_ENDIANNESS, ) from eth2spec.eip7594.mainnet import KZGCommitment as Commitment, KZGProof as Proof from eth2spec.utils import bls @@ -37,4 +37,17 @@ def bytes_to_polynomial(b: bytearray) -> Polynomial: def bytes_to_kzg_commitment(b: bytearray) -> Commitment: return g1_lincomb( bit_reversal_permutation(KZG_SETUP_G1_LAGRANGE), bytes_to_polynomial(b) - ) \ No newline at end of file + ) + + +def compute_kzg_proofs(b: bytearray, commitment: Commitment) -> List[Proof]: + assert len(b) % BYTES_PER_FIELD_ELEMENT == 0 + polynomial = bytes_to_polynomial(b) + return [ + compute_kzg_proof_impl( + polynomial, + bytes_to_bls_field(i.to_bytes(length=BYTES_PER_FIELD_ELEMENT, byteorder=KZG_ENDIANNESS)) + )[0] + for i in range(len(b)//BYTES_PER_FIELD_ELEMENT) + ] + diff --git a/da/test_kzg_rs.py b/da/test_kzg_rs.py index d3daadc..cbce77e 100644 --- a/da/test_kzg_rs.py +++ b/da/test_kzg_rs.py @@ -1,7 +1,7 @@ from itertools import chain from random import randrange from unittest import TestCase -from da.kzg_rs import Polynomial, bytes_to_polynomial, bytes_to_kzg_commitment +from da.kzg_rs import Polynomial, bytes_to_polynomial, bytes_to_kzg_commitment, compute_kzg_proofs from eth2spec.eip7594.mainnet import ( Polynomial as EthPolynomial, blob_to_polynomial, BLS_MODULUS, BYTES_PER_FIELD_ELEMENT, FIELD_ELEMENTS_PER_BLOB, Blob, blob_to_kzg_commitment @@ -37,3 +37,9 @@ class TestKzgRs(TestCase): rand_bytes = self.rand_bytes() commitment2 = bytes_to_kzg_commitment(rand_bytes) self.assertEqual(len(commitment), len(commitment2)) + + def test_compute_kzg_proofs(self): + rand_bytes = self.rand_bytes() + commitment = bytes_to_kzg_commitment(rand_bytes) + proofs = compute_kzg_proofs(rand_bytes, commitment) + self.assertEqual(len(proofs), FIELD_ELEMENTS_PER_BLOB) \ No newline at end of file