Skip to content

Commit

Permalink
Merge pull request #3804 from b-wagn/fft-test-extension
Browse files Browse the repository at this point in the history
EIP7594-Polynomial-Commitments-Tests: Extend Tests for FFT and Coset FFT.
  • Loading branch information
asn-d6 authored Jun 14, 2024
2 parents ca64850 + 2aeddf2 commit 210fcfb
Showing 1 changed file with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,74 @@
@spec_test
@single_phase
def test_fft(spec):

# in this test we sample a random polynomial in coefficient form
# then we apply an FFT to get evaluations over the roots of unity
# we then apply an inverse FFT to the evaluations to get coefficients

# we check two things:
# 1) the original coefficients and the resulting coefficients match
# 2) the evaluations that we got are the same as if we would have evaluated individually

rng = random.Random(5566)

roots_of_unity = spec.compute_roots_of_unity(spec.FIELD_ELEMENTS_PER_BLOB)

# sample a random polynomial
poly_coeff = [rng.randint(0, BLS_MODULUS - 1) for _ in range(spec.FIELD_ELEMENTS_PER_BLOB)]

# do an FFT and then an inverse FFT
poly_eval = spec.fft_field(poly_coeff, roots_of_unity)
poly_coeff_inversed = spec.fft_field(poly_eval, roots_of_unity, inv=True)

# first check: inverse FFT after FFT results in original coefficients
assert len(poly_eval) == len(poly_coeff) == len(poly_coeff_inversed)
assert poly_coeff_inversed == poly_coeff

# second check: result of FFT are really the evaluations
for i, w in enumerate(roots_of_unity):
individual_evaluation = spec.evaluate_polynomialcoeff(poly_coeff, w)
assert individual_evaluation == poly_eval[i]


@with_eip7594_and_later
@spec_test
@single_phase
def test_coset_fft(spec):

# in this test we sample a random polynomial in coefficient form
# then we apply a Coset FFT to get evaluations over the coset of the roots of unity
# we then apply an inverse Coset FFT to the evaluations to get coefficients

# we check two things:
# 1) the original coefficients and the resulting coefficients match
# 2) the evaluations that we got are the same as if we would have evaluated individually

rng = random.Random(5566)

roots_of_unity = spec.compute_roots_of_unity(spec.FIELD_ELEMENTS_PER_BLOB)

# this is the shift that generates the coset
coset_shift = spec.PRIMITIVE_ROOT_OF_UNITY

# sample a random polynomial
poly_coeff = [rng.randint(0, BLS_MODULUS - 1) for _ in range(spec.FIELD_ELEMENTS_PER_BLOB)]

# do a coset FFT and then an inverse coset FFT
poly_eval = spec.coset_fft_field(poly_coeff, roots_of_unity)
poly_coeff_inversed = spec.coset_fft_field(poly_eval, roots_of_unity, inv=True)

# first check: inverse coset FFT after coset FFT results in original coefficients
assert len(poly_eval) == len(poly_coeff) == len(poly_coeff_inversed)
assert poly_coeff_inversed == poly_coeff

# second check: result of FFT are really the evaluations over the coset
for i, w in enumerate(roots_of_unity):
# the element of the coset is coset_shift * w
shifted_w = spec.BLSFieldElement((coset_shift * int(w)) % BLS_MODULUS)
individual_evaluation = spec.evaluate_polynomialcoeff(poly_coeff, shifted_w)
assert individual_evaluation == poly_eval[i]


@with_eip7594_and_later
@spec_test
Expand Down

0 comments on commit 210fcfb

Please sign in to comment.