Added g1_linecomb without strict limit assertion

This commit is contained in:
Daniel Sanchez Quiros 2024-02-13 13:06:24 +01:00
parent cc94872505
commit b692a47f7b
2 changed files with 24 additions and 3 deletions

View File

@ -1,17 +1,31 @@
from itertools import batched
from typing import List
from typing import List, Sequence
from eth2spec.eip7594.mainnet import (
blob_to_kzg_commitment, g1_lincomb, 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,
)
from eth2spec.eip7594.mainnet import KZGCommitment as Commitment, KZGProof as Proof
from eth2spec.utils import bls
class Polynomial(List[BLSFieldElement]):
pass
def g1_lincomb(points: Sequence[Commitment], scalars: Sequence[BLSFieldElement]) -> Commitment:
"""
BLS multiscalar multiplication. This function can be optimized using Pippenger's algorithm and variants.
"""
# we assert to have more points available than elements,
# this is dependent on the available kzg setup size
assert len(points) >= len(scalars)
result = bls.Z1()
for x, a in zip(points, scalars):
result = bls.add(result, bls.multiply(bls.bytes48_to_G1(x), a))
return Commitment(bls.G1_to_bytes48(result))
def bytes_to_polynomial(b: bytearray) -> Polynomial:
"""
Convert bytes to list of BLS field scalars.
@ -23,4 +37,4 @@ 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)
)
)

View File

@ -30,3 +30,10 @@ class TestKzgRs(TestCase):
eth_commitment = blob_to_kzg_commitment(Blob(rand_bytes))
commitment = bytes_to_kzg_commitment(rand_bytes)
self.assertEqual(eth_commitment, commitment)
def test_small_bytes_kzg_commitment(self):
rand_bytes = bytearray(int.to_bytes(randrange(BLS_MODULUS), length=BYTES_PER_FIELD_ELEMENT))
commitment = bytes_to_kzg_commitment(rand_bytes)
rand_bytes = self.rand_bytes()
commitment2 = bytes_to_kzg_commitment(rand_bytes)
self.assertEqual(len(commitment), len(commitment2))