Repository navigation
fix!: Go nkeys parity for curve keys, small-order keys, and known-answer tests - #68
Merged
Merged
Conversation
KeyPair is an Ed25519 key pair, but it accepted KeyPairType::Curve and the curve prefixes. KeyPair::new(Curve) produced an X-prefixed key whose public key was derived as Ed25519, so it didn't match XKey or Go nkeys for the same seed. from_seed and from_public_key likewise treated curve keys as Ed25519. KeyPair now rejects curve keys with IncorrectKeyType (new panics), pointing callers to XKey, matching Go nkeys, which routes curve prefixes to its curve-key code. `nk gen curve` now generates keys with XKey, and the cli feature enables xkeys. BREAKING CHANGE: KeyPair::new(KeyPairType::Curve) panics, and KeyPair::new_from_raw, KeyPair::from_seed and KeyPair::from_public_key return an IncorrectKeyType error for curve keys. Use XKey for curve keys. Signed-off-by: Bailey Hayes <bailey@cosmonic.com>
Small-order Ed25519 public keys can't come from honest key generation, and a crafted signature under one can verify for many messages. verify already rejects them via verify_strict; reject them when parsing too, as Go nkeys has since its small-order check (covering all eight points and their noncanonical encodings). Signed-off-by: Bailey Hayes <bailey@cosmonic.com>
Check that the RFC 8032 TEST 1 seed derives the expected public key and signature for every Ed25519 key type, through new_from_raw and a from_seed round trip, and that the signature verifies with the public-only key. Go nkeys has the same check in TestKeyPairFromKnownSeed. This guards against regressions from Ed25519 library upgrades. Signed-off-by: Bailey Hayes <bailey@cosmonic.com>
ricochet
force-pushed
the
fix/go-nkeys-parity
branch
from
November 30, 2026 18:02
da82077 to
9abeba3
Compare
decode_seed doesn't validate the key type byte, and KeyPairType::from maps any unknown value to Operator. A well-formed seed with an unknown type byte therefore loaded as an Operator key, and re-encoded as a different seed. Go nkeys rejects such seeds in DecodeSeed; KeyPair::from_seed now returns InvalidPrefix for them. Signed-off-by: Bailey Hayes <bailey@cosmonic.com>
KeyPair::new panicked for KeyPairType::Curve. The key type often comes from user input (KeyPairType implements FromStr and accepts "curve"), so a library crash is the wrong response. KeyPair::new now returns Result and reports IncorrectKeyType, matching new_from_raw and Go's CreatePair. The type-specific constructors (new_user, new_account, ...) can't fail and stay infallible. The nk CLI reports errors instead of unwrapping. BREAKING CHANGE: KeyPair::new returns Result<KeyPair>. Callers passing a fixed non-curve type can use the type-specific constructors instead. Signed-off-by: Bailey Hayes <bailey@cosmonic.com>
The crate docs list the X (curve) prefix next to the KeyPair examples. Add a note that KeyPair rejects curve keys and XKey handles them. XKey::from_seed named the seed's key type with KeyPairType::from, which maps unknown bytes to Operator, so a seed with an unknown type byte was reported as an Operator seed. Report the raw byte for unknown types. Signed-off-by: Bailey Hayes <bailey@cosmonic.com>
KeyPairType::from_str mapped any unrecognized string to Module, so user input like `nk gen usr` silently generated a Module key. Return an IncorrectKeyType error listing the valid names instead. BREAKING CHANGE: KeyPairType::from_str returns an error for unknown names instead of KeyPairType::Module. Signed-off-by: Bailey Hayes <bailey@cosmonic.com>
jfleitz
approved these changes
Nov 30, 2026
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.
Fixes from comparing this crate with nats-io/nkeys (v0.4.17). One commit each, so the changelog lists them separately.
1.
KeyPairrejects curve keys (breaking)KeyPairis an Ed25519 key pair, but it acceptedKeyPairType::Curveand theX/SXcurve prefixes, which are reserved for X25519 encryption keys (XKey). For example,KeyPair::new(KeyPairType::Curve)produced anX-prefixed key whose public key was derived as Ed25519. Loading the same seed withXKey::from_seed, or with Go nkeys, gives a different public key.KeyPair::from_seedandKeyPair::from_public_keyalso treated curve keys as Ed25519. Go fixed the same mix-up in 2022 by routing curve prefixes to its curve-key code.new_from_raw,from_seedandfrom_public_keyreturnErrorKind::IncorrectKeyTypefor curve keys, with a message pointing toXKey.KeyPair::newnow returnsResult<KeyPair>and reports the same error forKeyPairType::Curve, instead of panicking. The key type often comes from user input (KeyPairTypeimplementsFromStrand accepts"curve"), so a library crash would be the wrong response. This matchesnew_from_rawand Go'sCreatePair. The type-specific constructors (new_user,new_account, ...) can't fail and stay infallible. No callers ofKeyPair::new(...)inwascap,washorwasmCloud/crates.nk gen curvewas affected too. It now generates keys withXKey, and theclifeature enablesxkeys. I checked that its output now round-trips throughXKey::from_seed.2. Reject small-order public keys when parsing
KeyPair::from_public_keynow rejects small-order Ed25519 public keys (VerifyingKey::is_weak), as Go'sFromPublicKeydoes since October 2026. #63 already rejects them inverifyviaverify_strict; this fails earlier, at parse time. The test covers all 14 encodings Go rejects (libsodium's small-order list, each with and without the sign bit set), and it fails if the check is removed.3. RFC 8032 known-answer test
New test: the RFC 8032 TEST 1 seed must derive the expected public key and empty-message signature for every Ed25519 key type, through
new_from_rawand afrom_seedround trip, and the signature must verify with the public-only key. Go has the same check inTestKeyPairFromKnownSeed. It guards against regressions from Ed25519 library upgrades like the recented25519-dalek2→3.4. Reject seeds with an unknown key type
Found in code review.
decode_seeddoesn't check the key type byte, andKeyPairType::frommaps unknown values toOperator, so a well-formed seed with an unknown type byte loaded as an Operator key and re-encoded as a different seed.KeyPair::from_seednow returnsInvalidPrefixfor these, matching Go'sDecodeSeed.5.
KeyPairType::from_strrejects unknown names (breaking)Found in code review.
from_strmapped any unrecognized string toModule, sonk gen usrsilently generated a Module key. It now returns anIncorrectKeyTypeerror listing the valid names, andnkreports it. Names are still matched case-insensitively.Also from review: the crate docs now say
KeyPairrejects curve keys and point toXKey, andXKey::from_seedreports the raw byte for an unknown key type instead of calling itOperator.Release
The breaking commits are
fix!:withBREAKING CHANGEfooters, so release-plz should propose 0.6.0 rather than a 0.5.x patch.Testing
cargo test --all-features(43 tests, 9 new), clippy and fmt clean, andwasm32-unknown-unknownbuilds with and withoutxkeys.