Skip to content

fix!: Go nkeys parity for curve keys, small-order keys, and known-answer tests - #68

Merged
ricochet merged 7 commits into
mainfrom
fix/go-nkeys-parity
Nov 30, 2026
Merged

ricochet merged 7 commits into
mainfrom
fix/go-nkeys-parity

Conversation

@ricochet

@ricochet ricochet commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Fixes from comparing this crate with nats-io/nkeys (v0.4.17). One commit each, so the changelog lists them separately.

1. KeyPair rejects curve keys (breaking)

KeyPair is an Ed25519 key pair, but it accepted KeyPairType::Curve and the X/SX curve prefixes, which are reserved for X25519 encryption keys (XKey). For example, KeyPair::new(KeyPairType::Curve) produced an X-prefixed key whose public key was derived as Ed25519. Loading the same seed with XKey::from_seed, or with Go nkeys, gives a different public key. KeyPair::from_seed and KeyPair::from_public_key also 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_seed and from_public_key return ErrorKind::IncorrectKeyType for curve keys, with a message pointing to XKey.
  • KeyPair::new now returns Result<KeyPair> and reports the same error for KeyPairType::Curve, instead of panicking. The key type often comes from user input (KeyPairType implements FromStr and accepts "curve"), so a library crash would be the wrong response. This matches new_from_raw and Go's CreatePair. The type-specific constructors (new_user, new_account, ...) can't fail and stay infallible. No callers of KeyPair::new(...) in wascap, wash or wasmCloud/crates.
  • nk gen curve was affected too. It now generates keys with XKey, and the cli feature enables xkeys. I checked that its output now round-trips through XKey::from_seed.

2. Reject small-order public keys when parsing

KeyPair::from_public_key now rejects small-order Ed25519 public keys (VerifyingKey::is_weak), as Go's FromPublicKey does since October 2026. #63 already rejects them in verify via verify_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_raw and a from_seed round trip, and the signature must verify with the public-only key. Go has the same check in TestKeyPairFromKnownSeed. It guards against regressions from Ed25519 library upgrades like the recent ed25519-dalek 2→3.

4. Reject seeds with an unknown key type

Found in code review. decode_seed doesn't check the key type byte, and KeyPairType::from maps unknown values to Operator, so a well-formed seed with an unknown type byte loaded as an Operator key and re-encoded as a different seed. KeyPair::from_seed now returns InvalidPrefix for these, matching Go's DecodeSeed.

5. KeyPairType::from_str rejects unknown names (breaking)

Found in code review. from_str mapped any unrecognized string to Module, so nk gen usr silently generated a Module key. It now returns an IncorrectKeyType error listing the valid names, and nk reports it. Names are still matched case-insensitively.

Also from review: the crate docs now say KeyPair rejects curve keys and point to XKey, and XKey::from_seed reports the raw byte for an unknown key type instead of calling it Operator.

Release

The breaking commits are fix!: with BREAKING CHANGE footers, 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, and wasm32-unknown-unknown builds with and without xkeys.

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
ricochet force-pushed the fix/go-nkeys-parity branch from da82077 to 9abeba3 Compare November 30, 2026 18:02
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>
@ricochet
ricochet merged commit a711a88 into main Nov 30, 2026
10 checks passed
@ricochet
ricochet deleted the fix/go-nkeys-parity branch November 30, 2026 18:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants