Fix two broken key-generation code paths - #20
Draft
ZenulAbidin wants to merge 2 commits into
Draft
ZenulAbidin wants to merge 2 commits into
ZenulAbidin wants to merge 2 commits into
Conversation
Co-authored-by: Ali Sherief <ali@zenulabidin.com>
Co-authored-by: Ali Sherief <ali@zenulabidin.com>
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.
Summary
Fixes two deterministic, reproducible bugs in the key-generation API. Both are exercised by the public surface (
create_keypairis exported fromzpywallet/__init__.py), both fail 100% of the time, and neither was covered by tests.Bug 1 —
PrivateKey.from_random()always returns the zero scalaristarts at0; the guard never rejects0, so the loop body never executes andfrom_int(0)raises:Even if the loop had run,
0would still have been accepted. Fix: reject0as well (while i <= 0 or i >= secp256k1.N).Bug 2 —
create_keypair()passes raw bytes toPrivateKey()PrivateKey.__init__expects acoincurve.PrivateKey(it callsckey.to_hex()), but rawbyteswere passed, so every call raised:Fix: build the key via the (now-correct)
PrivateKey.from_random(network), which also guarantees a valid in-range scalar.Reproduction (before)
Tests
tests/test_12_keys.py::test_008_from_random_returns_valid_scalar— assertsfrom_randomyields a scalar in[1, N-1]and a usable address, across repeated draws.tests/test_05_zpywallet.py::test_010_create_keypair— assertscreate_keypairreturns a consistent(PrivateKey, PublicKey)pair for BTC (base58 address) and ETH (0xhex address).Validation
pytest tests)flake8(repo config)create_keypair+from_randomall OKNo behavioral change to any already-working path; the diff to library code is 2 lines.