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

Add KZG tests for invalid length inputs #3282

Merged
merged 11 commits into from
Mar 7, 2023

Conversation

jtraglia
Copy link
Member

@jtraglia jtraglia commented Mar 6, 2023

We would like to test invalid length inputs (blobs/commitments/proofs). This PR adds these tests and updates the spec to accommodate these changes. This required adding explicit length checks; Python's type-hinting does not actually enforce byte lengths. @hwwhww is there a better way to do this? Also, I fixed a couple of nits with parameter names.

blob, z = VALID_BLOBS[1], VALID_ZS[1]
proof = spec.compute_kzg_proof(blob, z)
for y in INVALID_ZS:
blob, z = VALID_BLOBS[4], VALID_ZS[1]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made this change (testing all of the invalid field element values) because I wanted to test invalid length field elements for the y field too. Previously, this was only testing one invalid y value.

tests/generators/kzg_4844/main.py Outdated Show resolved Hide resolved
tests/generators/kzg_4844/main.py Outdated Show resolved Hide resolved
tests/generators/kzg_4844/main.py Outdated Show resolved Hide resolved
tests/generators/kzg_4844/main.py Show resolved Hide resolved
Comment on lines 351 to +363
def verify_kzg_proof(commitment_bytes: Bytes48,
z: Bytes32,
y: Bytes32,
z_bytes: Bytes32,
y_bytes: Bytes32,
proof_bytes: Bytes48) -> bool:
"""
Verify KZG proof that ``p(z) == y`` where ``p(z)`` is the polynomial represented by ``polynomial_kzg``.
Receives inputs as bytes.
Public method.
"""
assert len(commitment_bytes) == BYTES_PER_COMMITMENT
assert len(z_bytes) == BYTES_PER_FIELD_ELEMENT
assert len(y_bytes) == BYTES_PER_FIELD_ELEMENT
assert len(proof_bytes) == BYTES_PER_PROOF
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, typing hint doesn't mandatorily verify the length. You can also assert it is checking with SSZ remerkleable:

  • You can cast: Bytes48(commitment_bytes), it will raise an error if the length is wrong.
  • You can use assert isinstance(commitment_bytes, Bytes48) to force the input to be remerkleable-defined Bytes48. That said, Python's built-in 48-byte object will be an invalid input.

However, I think the assertions you added are more explicit and clear for client devs. 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, thanks! Sounds good 👍

@dankrad
Copy link
Contributor

dankrad commented Mar 7, 2023

I'd also like to merge this before the next release to have the maximum coverage in test vectors

@jtraglia
Copy link
Member Author

jtraglia commented Mar 7, 2023

Great. The latest changes have been merged into this PR & I have tested that all of KZG tests generate successfully.

@dankrad dankrad merged commit ccfe576 into ethereum:dev Mar 7, 2023
@jtraglia jtraglia deleted the invalid-length-inputs branch March 7, 2023 22:00
@jtraglia jtraglia changed the title Add KZG tests for input length inputs Add KZG tests for invalid length inputs Mar 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants