test(nostr-cid-vm): make JWK-y corruption provably off-curve (closes #438)#504
Merged
Merged
Conversation
The "rejects a JWK with the right x but wrong y" test flipped the last base64url char of y from 'A'→'B' (or x→'A'). Because the final base64url char of a 32-byte field carries only 4 high bits — the bottom 2 are padding — 'A' (000000) and 'B' (000001) decode to the SAME bytes whenever y ended in 'A' (≈1/16 of randomly generated keys), turning the "corruption" into a no-op so the test occasionally accepted the JWK as valid. (A second, far rarer path: the flip landed on -y mod p.) Set y to 32 zero bytes instead. (x, 0) is on secp256k1 iff x³ ≡ -7 mod p, which has exactly 3 solutions in ~2²⁵⁶ — provably off-curve for any practical random x. Verified across 30 consecutive runs.
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
jwk.ywith'A'.repeat(43)(32 zero bytes) so the "right x, wrong y" JWK is provably off-curve for any practical random keypair.Root cause
The old corruption was:
The last base64url char of a 32-byte field carries only 4 high bits — the bottom 2 are padding zeroes.
'A'=000000and'B'=000001differ only in those padding bits, so they decode to the same bytes. When y's base64url happened to end in'A'(~1/16 of randomly generated keys), the "corruption" was a no-op and the JWK passed curve validation, so the WebID upgrade succeeded and the test failed.A much rarer second path (the one the issue analysis flagged): the flip happened to land on
-y mod p, which is the other valid y for the same x. Either way, the fix below kills both.Fix
Set y to 32 zero bytes.
(x, 0)is on secp256k1 iffx³ ≡ -7 mod p, which has exactly 3 solutions in ~2²⁵⁶ — vanishingly improbable for any randomly generated x. Keeps option 2 from the issue's fix menu, preserving "random keypair" coverage.Test plan
node --test test/nostr-cid-vm.test.js— all 25 tests pass.did:nostr:.Closes #438.