Repository navigation
keys.mjs: keys as group elements (full points, additive tweaks, normalise once, x-only at the edge) - #28
Conversation
…tive tweaks and chains (d + t <-> P + t·G, never lifted between steps), normalise the secret once so a bare did:nostr identifier is exact, x-only only at a taproot output or a BIP 340 signature; tests against the official BIP 340, 341 and 86 vectors, property tests over both parities, keys-vectors.json as known answers (#27)
There was a problem hiding this comment.
Copilot review overview
🔵 Needs a closer look
It is foundational, security-sensitive key-derivation/crypto code (BIP 340/341/86, taproot tweaks, did:nostr) whose curve-dependent vectors and external engine behavior can't be fully executed here and that other components will depend on, warranting human sign-off.
Review effort: Balanced
Findings: None
What changed in this PR
This PR adds siding/lib/keys.mjs, a pure (browser-loadable) module that treats secp256k1 keys as group elements. It keeps full compressed points (02/03 + x) for all tweaking/chaining, applies plain additive arithmetic (d + t ↔ P + t·G), and reduces to x-only form only at the two edges Bitcoin needs it (taproot output and BIP 340 signing). A holder normalizes a secret once to the even-y point its did:nostr identifier denotes, so bare-identifier derivations stay exact without re-lifting at each step. It fits alongside the existing schnorr.mjs (signing) and address.mjs (encoding), and is intended to back #23's peg-in tweak form, Blocktrails-style chains, and per-account deposit addresses.
Changes:
- New
keys.mjsAPI:publicKey,normalize,signingKey,basePoint,xOnly,did,multikey,negate,tweakSecret/tweakPoint,chainSecrets/chainPoints,taggedScalar,tapTweak. - New test
keys-test.mjs(24 checks) against official BIP 340/341/86 vectors, property tests over both parities, and the normalise-once rule. - New
keys-vectors.jsonknown-answer file (regenerable with--regen) pinning did:nostr's additive-tweak case.
| File | Description |
|---|---|
| siding/lib/keys.mjs | New pure module: full-point arithmetic, normalise-once, x-only at the edge, BIP 341 tap tweak, validation and refusals in words. |
| siding/test/keys-test.mjs | Tests the edge against official BIP vectors, property tests for d+t ↔ P+t·G, chains, normalise-once, key reading, and known-answers. |
| siding/test/keys-vectors.json | Committed known-answer vectors (even/odd secret cases) regenerated from the library and checked by the test. |
I verified the module's logic against the established engine API used elsewhere (ckdPubKey, liftX in federation.mjs), confirmed the error/refusal paths and edge cases ([1, n-1] bounds, cancel/infinity, off-curve x, prefix handling), and independently checked the vectors' internal arithmetic (normalized = n−d, cumulative chain tweaks, signingKeys parity rule, outputs = x-only) with pure bigint math — all consistent. I found no concrete issues to raise as inline comments.
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Closes #27.
Adds
siding/lib/keys.mjs, a pure module in the shape ofschnorr.mjs, and its tests.The rule. A did:nostr identifier names the even-y (02) point; a Multikey or compressed point carries its parity. A holder normalises the secret once (n − d if the point is odd-y). Tweaks are plain addition on full points, each step from the actual point of the one before, never lifted again. x-only only at the edge: a taproot output is x(P), and a BIP 340 signature uses the sign it needs inside the signing.
API.
publicKey,normalize,signingKey,basePoint(did:nostr / x / Multikey / compressed),xOnly,did,multikey,negate,tweakSecret,tweakPoint,chainSecrets,chainPoints,taggedScalar,tapTweak.Tests (
siding/test/keys-test.mjs, 24 checks,SCHEMAas the other tests): all 7 BIP 341 output keys from the 02 point (and none from the 03 point: the lift), the 9 valid BIP 340 vectors, the 3 BIP 86 addresses from their child xpubs; d + t ↔ P + t·G on 500 keys of both parities; chains; +1, +2, +3; normalise once over 300 keys and a chain through odd-y points; refusals in words;keys-vectors.jsonknown answers (regenerate with--regen, review the diff).Used next by #23's peg-in tweak form, Blocktrails chains, and a Web Ledgers teller; the vectors are the first to pin did:nostr's additive-tweak case.