Daniel Sanchez cf899d2384
Da encoding protocol (#76)
* Implement generator polynomial and rs encoding

* Implement encode/decode+test using fft. Non-working

* Use lagrange for interpolation

* Remove fft, use evaluations instead

* Move and rename kzg and rs test modules

* Update docs

* Added columns property to chunks matrix
Added test for columns

* Added chunkify and test

* Added compute row commitments
Added row commitments size test

* Fix poly from evaluations method

* Implement encode rows and test

* Update encode row test

* Implement compute row proofs (not working on extended data)

* Use same polynomials for commitment and proof creation after extend

* Fix polynomial from/to evaluations

* Use chunks for verification

* Refactor interpolate

* Implement chunks matrix transposed method

* Added compute column kzg commitments

* Use square size data for encoder tests

* Add column type to columns method

* Added compute columns aggregated commitment
Added aggregated commitment test
Fixed and expanded encode test

* Use sha3 for hashing
2024-03-08 14:16:14 +01:00

42 lines
1.4 KiB
Python

from typing import Sequence, List
import scipy.interpolate
from eth2spec.deneb.mainnet import BLSFieldElement
from eth2spec.eip7594.mainnet import interpolate_polynomialcoeff
from .common import G1, BLS_MODULUS
from .poly import Polynomial
ExtendedData = Sequence[BLSFieldElement]
def encode(polynomial: Polynomial, factor: int, roots_of_unity: Sequence[int]) -> ExtendedData:
"""
Encode a polynomial extending to the given factor
Parameters:
polynomial: Polynomial to be encoded
factor: Encoding factor
roots_of_unity: Powers of 2 sequence
Returns:
list: Extended data set
"""
assert factor >= 2
assert len(polynomial)*factor <= len(roots_of_unity)
return [polynomial.eval(e) for e in roots_of_unity[:len(polynomial)*factor]]
def decode(encoded: ExtendedData, roots_of_unity: Sequence[BLSFieldElement], original_len: int) -> Polynomial:
"""
Decode a polynomial from an extended data-set and the roots of unity, cap to original length
Parameters:
encoded: Extended data set
roots_of_unity: Powers of 2 sequence
original_len: Original length of the encoded polynomial
Returns:
Polynomial: original polynomial
"""
coefs = Polynomial.interpolate(list(map(int, encoded)), list(map(int, roots_of_unity)))[:original_len]
return Polynomial([int(c) for c in coefs], BLS_MODULUS)