From c71fb00c8e228228eaa76160a9efb1000cedfe42 Mon Sep 17 00:00:00 2001 From: George Kadianakis Date: Fri, 8 Mar 2024 18:04:55 +0200 Subject: [PATCH] PeerDAS: Check for degree overflow in multiply_polynomialcoeff() --- .../eip7594/polynomial-commitments-sampling.md | 3 +++ .../test_polynomial_commitments.py | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/specs/_features/eip7594/polynomial-commitments-sampling.md b/specs/_features/eip7594/polynomial-commitments-sampling.md index 9cd862589b..0a90443e9c 100644 --- a/specs/_features/eip7594/polynomial-commitments-sampling.md +++ b/specs/_features/eip7594/polynomial-commitments-sampling.md @@ -196,12 +196,15 @@ def multiply_polynomialcoeff(a: PolynomialCoeff, b: PolynomialCoeff) -> Polynomi """ Multiplies the coefficient form polynomials ``a`` and ``b`` """ + assert len(a) + len(b) <= FIELD_ELEMENTS_PER_EXT_BLOB + r = [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) return r ``` + #### `divide_polynomialcoeff` ```python diff --git a/tests/core/pyspec/eth2spec/test/eip7594/unittests/polynomial_commitments/test_polynomial_commitments.py b/tests/core/pyspec/eth2spec/test/eip7594/unittests/polynomial_commitments/test_polynomial_commitments.py index 0b60cfd287..f8a879e468 100644 --- a/tests/core/pyspec/eth2spec/test/eip7594/unittests/polynomial_commitments/test_polynomial_commitments.py +++ b/tests/core/pyspec/eth2spec/test/eip7594/unittests/polynomial_commitments/test_polynomial_commitments.py @@ -2,6 +2,7 @@ from eth2spec.test.context import ( spec_test, single_phase, + expect_assertion_error, with_eip7594_and_later, ) from eth2spec.test.helpers.sharding import ( @@ -105,3 +106,19 @@ def test_recover_polynomial(spec): # Now flatten the cells and check that they match the entirety of the recovered data flattened_cells = [x for xs in cells for x in xs] assert flattened_cells == recovered_data + + +@with_eip7594_and_later +@spec_test +@single_phase +def test_multiply_polynomial_degree_overflow(spec): + rng = random.Random(5566) + + # Perform a legitimate-but-maxed-out polynomial multiplication + poly1_coeff = [rng.randint(0, BLS_MODULUS - 1) for _ in range(spec.FIELD_ELEMENTS_PER_BLOB)] + poly2_coeff = [rng.randint(0, BLS_MODULUS - 1) for _ in range(spec.FIELD_ELEMENTS_PER_BLOB)] + _ = spec.multiply_polynomialcoeff(poly1_coeff, poly2_coeff) + + # Now overflow the degree by pumping the degree of one of the inputs by one + poly2_coeff = [rng.randint(0, BLS_MODULUS - 1) for _ in range(spec.FIELD_ELEMENTS_PER_BLOB + 1)] + expect_assertion_error(lambda: spec.multiply_polynomialcoeff(poly1_coeff, poly2_coeff))