Added compute proofs (wip)

This commit is contained in:
Daniel Sanchez Quiros 2024-02-13 16:09:13 +01:00
parent b692a47f7b
commit 906b626e67
2 changed files with 22 additions and 3 deletions

View File

@ -3,7 +3,7 @@ from typing import List, Sequence
from eth2spec.eip7594.mainnet import ( from eth2spec.eip7594.mainnet import (
bit_reversal_permutation, KZG_SETUP_G1_LAGRANGE, Polynomial, 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.eip7594.mainnet import KZGCommitment as Commitment, KZGProof as Proof
from eth2spec.utils import bls from eth2spec.utils import bls
@ -37,4 +37,17 @@ def bytes_to_polynomial(b: bytearray) -> Polynomial:
def bytes_to_kzg_commitment(b: bytearray) -> Commitment: def bytes_to_kzg_commitment(b: bytearray) -> Commitment:
return g1_lincomb( return g1_lincomb(
bit_reversal_permutation(KZG_SETUP_G1_LAGRANGE), bytes_to_polynomial(b) bit_reversal_permutation(KZG_SETUP_G1_LAGRANGE), bytes_to_polynomial(b)
) )
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)
]

View File

@ -1,7 +1,7 @@
from itertools import chain from itertools import chain
from random import randrange from random import randrange
from unittest import TestCase 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 ( from eth2spec.eip7594.mainnet import (
Polynomial as EthPolynomial, blob_to_polynomial, BLS_MODULUS, Polynomial as EthPolynomial, blob_to_polynomial, BLS_MODULUS,
BYTES_PER_FIELD_ELEMENT, FIELD_ELEMENTS_PER_BLOB, Blob, blob_to_kzg_commitment 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() rand_bytes = self.rand_bytes()
commitment2 = bytes_to_kzg_commitment(rand_bytes) commitment2 = bytes_to_kzg_commitment(rand_bytes)
self.assertEqual(len(commitment), len(commitment2)) 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)