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

chore: Add correct type casts to eip7594 functions #3697

Closed
wants to merge 26 commits into from
Closed
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
8ad8b69
add eip7594 to pylint checks
kevaundray Apr 18, 2024
006cfe6
fix pylint errors
kevaundray Apr 18, 2024
9c9f8f1
temp fix
kevaundray Apr 18, 2024
3554d79
Merge branch 'dev' into pr3697
hwwhww Apr 19, 2024
d5b4e6c
workaround: add a new config `MAX_EXTENDED_MATRIX_SIZE` to avoid `cla…
hwwhww Apr 19, 2024
ec0dc1f
ensure that value is in the correct range
kevaundray Apr 19, 2024
2a49ce2
Merge branch 'dev' into kw/add-eip7594-to-pylint
kevaundray Apr 19, 2024
2c32877
fix new lint errors
kevaundray Apr 19, 2024
b031a80
try: adding back line
kevaundray Apr 22, 2024
0dc414b
try: type ignore
kevaundray Apr 22, 2024
864e45a
Merge branch 'dev' into kw/add-eip7594-to-pylint
kevaundray Apr 22, 2024
0f8b0b4
add copy
kevaundray Apr 22, 2024
d2a8cb5
fix lint
kevaundray Apr 22, 2024
13d73ff
Merge branch 'dev' into kw/add-eip7594-to-pylint
kevaundray Apr 22, 2024
783899c
fix new linter errors
kevaundray Apr 22, 2024
47ef8ff
remove check from CI
kevaundray Apr 22, 2024
ac26ed2
Merge branch 'dev' into kw/add-eip7594-to-pylint
kevaundray Apr 23, 2024
31be7bf
Merge branch 'dev' into kw/add-eip7594-to-pylint
kevaundray Apr 25, 2024
97ce095
remove extra modulus reduction
kevaundray Apr 25, 2024
62ce140
append and reverse list
kevaundray Apr 25, 2024
8c10508
fix new linter error
kevaundray Apr 25, 2024
e5ad8b7
re-arrange BLS_MODULUS to be before b[i]
kevaundray Apr 25, 2024
1cc7e1a
Avoid underflow
asn-d6 Apr 29, 2024
6eb5756
Correctly handle CosetEvals in compute_cells()
asn-d6 Apr 29, 2024
92b25e6
lint
kevaundray Apr 29, 2024
4de4625
Merge branch 'dev' into kw/add-eip7594-to-pylint
kevaundray Apr 29, 2024
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
52 changes: 30 additions & 22 deletions specs/_features/eip7594/polynomial-commitments-sampling.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def polynomial_eval_to_coeff(polynomial: Polynomial) -> PolynomialCoeff:
roots_of_unity = compute_roots_of_unity(FIELD_ELEMENTS_PER_BLOB)
polynomial_coeff = fft_field(bit_reversal_permutation(list(polynomial)), roots_of_unity, inv=True)

return polynomial_coeff
return PolynomialCoeff(polynomial_coeff)
```

#### `add_polynomialcoeff`
Expand All @@ -206,7 +206,7 @@ def add_polynomialcoeff(a: PolynomialCoeff, b: PolynomialCoeff) -> PolynomialCoe
a, b = (a, b) if len(a) >= len(b) else (b, a)
length_a = len(a)
length_b = len(b)
return [(a[i] + (b[i] if i < length_b else 0)) % BLS_MODULUS for i in range(length_a)]
return PolynomialCoeff([(a[i] + (b[i] if i < length_b else 0)) % BLS_MODULUS for i in range(length_a)])
```

#### `neg_polynomialcoeff`
Expand All @@ -216,7 +216,7 @@ def neg_polynomialcoeff(a: PolynomialCoeff) -> PolynomialCoeff:
"""
Negative of coefficient form polynomial ``a``
"""
return [(BLS_MODULUS - x) % BLS_MODULUS for x in a]
return PolynomialCoeff([(BLS_MODULUS - x) % BLS_MODULUS for x in a])
```

#### `multiply_polynomialcoeff`
Expand All @@ -228,10 +228,10 @@ def multiply_polynomialcoeff(a: PolynomialCoeff, b: PolynomialCoeff) -> Polynomi
"""
assert len(a) + len(b) <= FIELD_ELEMENTS_PER_EXT_BLOB

r = [0]
r = PolynomialCoeff([0])
for power, coef in enumerate(a):
summand = [0] * power + [int(coef) * int(x) % BLS_MODULUS for x in b]
r = add_polynomialcoeff(r, summand)
r = add_polynomialcoeff(r, PolynomialCoeff(summand))
return r
```

Expand All @@ -249,12 +249,14 @@ def divide_polynomialcoeff(a: PolynomialCoeff, b: PolynomialCoeff) -> Polynomial
diff = apos - bpos
while diff >= 0:
quot = div(a[apos], b[bpos])
o.insert(0, quot)
o.append(quot)
for i in range(bpos, -1, -1):
a[diff + i] = (int(a[diff + i]) - int(b[i] + BLS_MODULUS) * int(quot)) % BLS_MODULUS
a[diff + i] = (int(a[diff + i]) + (BLS_MODULUS - int(b[i])) * int(quot)) % BLS_MODULUS
apos -= 1
diff -= 1
return [x % BLS_MODULUS for x in o]

o.reverse()
return PolynomialCoeff(o)
```

#### `shift_polynomialcoeff`
Expand All @@ -271,7 +273,7 @@ def shift_polynomialcoeff(polynomial_coeff: PolynomialCoeff, factor: BLSFieldEle
for p in polynomial_coeff:
o.append(int(p) * factor_power % BLS_MODULUS)
factor_power = factor_power * inv_factor % BLS_MODULUS
return o
return PolynomialCoeff(o)
```

#### `interpolate_polynomialcoeff`
Expand All @@ -284,19 +286,22 @@ def interpolate_polynomialcoeff(xs: Sequence[BLSFieldElement], ys: Sequence[BLSF
Outputs a coefficient form polynomial. Leading coefficients may be zero.
"""
assert len(xs) == len(ys)
r = [0]
r = PolynomialCoeff([0])

for i in range(len(xs)):
summand = [ys[i]]
summand = PolynomialCoeff([ys[i]])
for j in range(len(ys)):
if j != i:
weight_adjustment = bls_modular_inverse(int(xs[i]) - int(xs[j]))
weight_adjustment = bls_modular_inverse(
BLSFieldElement((int(xs[i]) - int(xs[j]) + BLS_MODULUS) % BLS_MODULUS))
summand = multiply_polynomialcoeff(
summand, [((BLS_MODULUS - int(weight_adjustment)) * int(xs[j])) % BLS_MODULUS, weight_adjustment]
summand,
PolynomialCoeff([
((BLS_MODULUS - int(weight_adjustment)) * int(xs[j])) % BLS_MODULUS, weight_adjustment])
)
r = add_polynomialcoeff(r, summand)

return r
return PolynomialCoeff(r)
```

#### `vanishing_polynomialcoeff`
Expand All @@ -306,9 +311,9 @@ def vanishing_polynomialcoeff(xs: Sequence[BLSFieldElement]) -> PolynomialCoeff:
"""
Compute the vanishing polynomial on ``xs`` (in coefficient form)
"""
p = [1]
p = PolynomialCoeff([1])
for x in xs:
p = multiply_polynomialcoeff(p, [-int(x) + BLS_MODULUS, 1])
p = multiply_polynomialcoeff(p, PolynomialCoeff([-int(x) + BLS_MODULUS, 1]))
return p
```

Expand Down Expand Up @@ -357,7 +362,7 @@ def compute_kzg_proof_multi_impl(
# Compute the quotient polynomial directly in monomial form
quotient_polynomial = divide_polynomialcoeff(polynomial_coeff, denominator_poly)

return KZGProof(g1_lincomb(KZG_SETUP_G1_MONOMIAL[:len(quotient_polynomial)], quotient_polynomial)), ys
return KZGProof(g1_lincomb(KZG_SETUP_G1_MONOMIAL[:len(quotient_polynomial)], quotient_polynomial)), CosetEvals(ys)
```

#### `verify_kzg_proof_multi_impl`
Expand Down Expand Up @@ -466,6 +471,7 @@ def compute_cells(blob: Blob) -> Vector[Cell, CELLS_PER_EXT_BLOB]:
extended_data = fft_field(polynomial_coeff + [0] * FIELD_ELEMENTS_PER_BLOB,
compute_roots_of_unity(FIELD_ELEMENTS_PER_EXT_BLOB))
extended_data_rbo = bit_reversal_permutation(extended_data)

cells = []
for cell_id in range(CELLS_PER_EXT_BLOB):
start = cell_id * FIELD_ELEMENTS_PER_CELL
Expand Down Expand Up @@ -621,12 +627,13 @@ def recover_shifted_data(cell_ids: Sequence[CellID],
extended_evaluation_times_zero = [BLSFieldElement(int(a) * int(b) % BLS_MODULUS)
for a, b in zip(zero_poly_eval, extended_evaluation)]

extended_evaluations_fft = fft_field(extended_evaluation_times_zero, roots_of_unity_extended, inv=True)
extended_evaluations_fft = PolynomialCoeff(
fft_field(extended_evaluation_times_zero, roots_of_unity_extended, inv=True))

# Compute (E*Z)(k*x)
shifted_extended_evaluation = shift_polynomialcoeff(extended_evaluations_fft, shift_factor)
# Compute Z(k*x)
shifted_zero_poly = shift_polynomialcoeff(zero_poly_coeff, shift_factor)
shifted_zero_poly = shift_polynomialcoeff(PolynomialCoeff(zero_poly_coeff), shift_factor)

eval_shifted_extended_evaluation = fft_field(shifted_extended_evaluation, roots_of_unity_extended)
eval_shifted_zero_poly = fft_field(shifted_zero_poly, roots_of_unity_extended)
Expand All @@ -640,7 +647,7 @@ def recover_shifted_data(cell_ids: Sequence[CellID],
def recover_original_data(eval_shifted_extended_evaluation: Sequence[BLSFieldElement],
eval_shifted_zero_poly: Sequence[BLSFieldElement],
shift_inv: BLSFieldElement,
roots_of_unity_extended: Sequence[BLSFieldElement]) -> Sequence[BLSFieldElement]:
roots_of_unity_extended: Sequence[BLSFieldElement]) -> CosetEvals:
"""
Given Q_1, Q_2 and k^{-1}, compute P(x).
"""
Expand All @@ -650,14 +657,15 @@ def recover_original_data(eval_shifted_extended_evaluation: Sequence[BLSFieldEle
for a, b in zip(eval_shifted_extended_evaluation, eval_shifted_zero_poly)
]

shifted_reconstructed_poly = fft_field(eval_shifted_reconstructed_poly, roots_of_unity_extended, inv=True)
shifted_reconstructed_poly = PolynomialCoeff(
fft_field(eval_shifted_reconstructed_poly, roots_of_unity_extended, inv=True))

# Unshift P(k*x) by k^{-1} to get P(x)
reconstructed_poly = shift_polynomialcoeff(shifted_reconstructed_poly, shift_inv)

reconstructed_data = bit_reversal_permutation(fft_field(reconstructed_poly, roots_of_unity_extended))

return reconstructed_data
return CosetEvals(reconstructed_data)
```

### `recover_all_cells`
Expand Down
Loading