Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EIP7594-Polynomial-Commitments-Tests: Extend Tests for FFT and Coset FFT. #3804

Merged
merged 3 commits into from
Jun 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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