Repository navigation
fix: reject off-curve points when decompressing public keys in native backend - #122
Open
haybinalim wants to merge 1 commit into
Open
haybinalim wants to merge 1 commit into
haybinalim wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What I did
Fixes #121
NativeECCBackend.decompress_public_key_bytes(viaPublicKey.from_compressed_bytes) returned a point that is not on secp256k1 whenx**3 + 7is not a quadratic residue mod p, and acceptedx >= pwithout reducing or rejecting it. The coincurve backend rejects both withValueError.How I did it
decompress_public_keynow raisesValueErrorwhenx >= p, and when the computed square root does not square back tox**3 + 7(no point with that x exists).ValueErrormatches what the coincurve backend raises and what the function already raises for a bad length or prefix.tests/backends/test_native_backend_against_coincurve.py:test_public_key_decompression_rejects_invalid_points: both prefixes × non-residue x values (5, 7, 9, 10, 11) and x ≥ p (p,p + 1,2**256 - 1) must raise on both backends.test_public_key_decompression_of_arbitrary_bytes_is_equal: hypothesis over arbitrary 32-byte x; native and coincurve must either both reject or return the same key.test_public_key_decompression_is_equalnow passesbackend=explicitly. Without it,from_compressed_bytes(a classmethod) used the global default backend, which is coincurve when installed, so the native side was never exercised.How to verify it
pytest tests: 116 passed.ruff check .,ruff format --check .,mypy eth_keys,mdformat --check README.md: clean.AI assistance
I used an AI assistant (Hark) to help find this with differential testing against the coincurve backend and to draft the fix and tests. I reviewed the change, reproduced the bug on
main, and ran the test suite and linters locally.