Implement generator polynomial and rs encoding

This commit is contained in:
Daniel Sanchez Quiros 2024-02-27 19:48:12 +01:00
parent 09c9b7e4ec
commit 8ea2fb1fa3
1 changed files with 22 additions and 47 deletions

View File

@ -1,55 +1,30 @@
from typing import Sequence, List from eth2spec.utils import bls
import scipy.interpolate from .common import BLS_MODULUS
from eth2spec.deneb.mainnet import BLSFieldElement
from eth2spec.eip7594.mainnet import interpolate_polynomialcoeff
from .common import G1, BLS_MODULUS
from .poly import Polynomial from .poly import Polynomial
from functools import reduce
ExtendedData = Sequence[BLSFieldElement]
def encode(polynomial: Polynomial, factor: int, roots_of_unity: Sequence[BLSFieldElement]) -> ExtendedData: def generator_polynomial(n, k, gen=bls.G1()) -> Polynomial:
""" """
Encode a polynomial extending to the given factor Generate the generator polynomial for RS codes
Parameters: g(x) = (x-α^1)(x-α^2)...(x-α^(n-k))
polynomial: Polynomial to be encoded
factor: Encoding factor
roots_of_unity: Powers of 2 sequence
Returns:
list: Extended data set
""" """
assert factor >= 2 g = Polynomial([bls.Z1()], modulus=BLS_MODULUS)
assert len(polynomial)*factor <= len(roots_of_unity) return reduce(
return [polynomial.eval(e) for e in roots_of_unity[:len(polynomial)*factor]] Polynomial.__mul__,
(Polynomial([bls.Z1(), bls.multiply(gen, alpha)], modulus=BLS_MODULUS) for alpha in range(1, n-k+1)),
initial=g
)
def __interpolate(evaluations: List[int], roots_of_unity: List[int]) -> List[int]: def encode(m: Polynomial, g: Polynomial, n: int, k: int) -> Polynomial:
""" # mprime = q*g + b for some q
Lagrange interpolation xshift = Polynomial([bls.Z1(), *[0 for _ in range(n-k)]], modulus=m.modulus)
mprime = m * xshift
Parameters: _, b = m / g
evaluations: List of evaluations # subtract out b, so now c = q*g
roots_of_unity: Powers of 2 sequence c = mprime - b
# Since c is a multiple of g, it has (at least) n-k roots: α^1 through
Returns: # α^(n-k)
list: Coefficients of the interpolated polynomial return c
"""
return list(map(int, interpolate_polynomialcoeff(roots_of_unity[:len(evaluations)], evaluations)))
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 = __interpolate(list(map(int, encoded)), list(map(int, roots_of_unity)))[:original_len]
return Polynomial([int(c) for c in coefs], BLS_MODULUS)