Skip to content

Commit 210fcfb

Browse files
authored
Merge pull request #3804 from b-wagn/fft-test-extension
EIP7594-Polynomial-Commitments-Tests: Extend Tests for FFT and Coset FFT.
2 parents ca64850 + 2aeddf2 commit 210fcfb

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

tests/core/pyspec/eth2spec/test/eip7594/unittests/polynomial_commitments/test_polynomial_commitments.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,74 @@
1515
@spec_test
1616
@single_phase
1717
def test_fft(spec):
18+
19+
# in this test we sample a random polynomial in coefficient form
20+
# then we apply an FFT to get evaluations over the roots of unity
21+
# we then apply an inverse FFT to the evaluations to get coefficients
22+
23+
# we check two things:
24+
# 1) the original coefficients and the resulting coefficients match
25+
# 2) the evaluations that we got are the same as if we would have evaluated individually
26+
1827
rng = random.Random(5566)
1928

2029
roots_of_unity = spec.compute_roots_of_unity(spec.FIELD_ELEMENTS_PER_BLOB)
2130

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

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

38+
# first check: inverse FFT after FFT results in original coefficients
2739
assert len(poly_eval) == len(poly_coeff) == len(poly_coeff_inversed)
2840
assert poly_coeff_inversed == poly_coeff
2941

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

3148
@with_eip7594_and_later
3249
@spec_test
3350
@single_phase
3451
def test_coset_fft(spec):
52+
53+
# in this test we sample a random polynomial in coefficient form
54+
# then we apply a Coset FFT to get evaluations over the coset of the roots of unity
55+
# we then apply an inverse Coset FFT to the evaluations to get coefficients
56+
57+
# we check two things:
58+
# 1) the original coefficients and the resulting coefficients match
59+
# 2) the evaluations that we got are the same as if we would have evaluated individually
60+
3561
rng = random.Random(5566)
3662

3763
roots_of_unity = spec.compute_roots_of_unity(spec.FIELD_ELEMENTS_PER_BLOB)
3864

65+
# this is the shift that generates the coset
66+
coset_shift = spec.PRIMITIVE_ROOT_OF_UNITY
67+
68+
# sample a random polynomial
3969
poly_coeff = [rng.randint(0, BLS_MODULUS - 1) for _ in range(spec.FIELD_ELEMENTS_PER_BLOB)]
4070

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

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

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

4887
@with_eip7594_and_later
4988
@spec_test

0 commit comments

Comments
 (0)