mirror of
https://github.com/luxfi/sdk.git
synced 2026-10-30 15:19:23 +00:00
* wallet: PQAccount + ML-DSA-65 HD derivation + SLH-DSA-192 recovery
ML-DSA-65 native wallet accounts with 48-byte SHAKE256-384 AccountID
derived via cSHAKE-256 domain separation. KMAC/cSHAKE child seeds use
"LUX/WALLET/{IDENTITY,TX,SESSION,RECOVERY}/V1" customization strings.
HD path: m/44'/9000'/<chain>'/0'/<role>'/<account>', mirroring the F35
hardening landed in genesis/pkg/genesis/keys.go.
Recovery accounts use SLH-DSA-SHAKE-192s (FIPS 205, NIST Cat 3) for
stateless cold-key recovery on a separate hardened branch
m/44'/9000'/<chain>'/2'/<account>' so a leaked wallet branch cannot
derive recovery keys.
Keystore at rest: Argon2id (RFC 9106) → AES-256-GCM with AAD bound to
(scheme, role, chain, path). Private keys never persisted in plaintext.
WalletSchemeID/RecoverySchemeID declared locally (consensus/config has
not committed its WalletSchemeID byte yet); byte values match the raw
ML-DSA block (0x40..) so wire compatibility is preserved when the
consensus type lands.
AccountID KAT cross-checked against @noble/hashes/sha3-addons cshake256
(JS) — Go and TS implementations produce identical 48-byte AccountIDs
for the canonical test vector.
37 unit tests in wallet/account/, 7 in wallet/ (PQWallet facade).
Patch-bump.
* wallet/account: drop unused branchClassical constant
staticcheck U1000 catches it. The comment on branchPQ already
documents that branches 1' (secp256k1) and 2' (recovery) live in
sibling derivations, so the unused constant is dead documentation.
Patch-bump.
* sdk/wallet/account: use luxfi/crypto/pq/mldsa/mldsa65 wrapper
Switches PQAccount ML-DSA-65 key derivation, signing, and verification
from the upstream cloudflare/circl entry points to the luxfi/crypto
wrapper exposed at github.com/luxfi/crypto/pq/mldsa/mldsa65 (v1.18.4
adds the canonical NewKeyFromSeed/[]byte entry point we want).
- NewKeyFromSeed accepts []byte (any length) and internally cSHAKEs
to the 32-byte ξ FIPS 204 §5.1 consumes. The wallet already pre-
domain-separates by role through cSHAKE before handing the seed
off; the wrapper's per-parameter-set "LUX/FIPS204/MLDSA65/seed"
customization compounds with the wallet role customization without
overlap.
- Sign returns ([]byte, error) directly — replaces the SignTo(...)
out-parameter form.
- go.mod: bump luxfi/crypto v1.18.3 → v1.18.4.
Tests rebound to the luxfi/crypto package; sizes (PublicKeySize,
PrivateKeySize, SignatureSize) are re-exported aliases so existing
KAT and roundtrip tests continue to verify byte-identical values.
All 37 wallet/account tests pass under GOWORK=off.
* sdk/wallet/account: gofmt — unstuck CI lint job
Pure gofmt -w pass on four files flagged by golangci-lint in the
previous PR run. Doc-comment list indentation only — no code changes,
all tests green.
- wallet/account/cshake.go
- wallet/account/keystore.go
- wallet/account/scheme.go
- wallet/account/sign_tx_test.go
* sdk/wallet/keychain: hybrid AND-verifies (closes F112)
Adds VerifyHybridSecp256k1MLDSA and VerifyHybridSecp256k1SLHDSA — the
AND-verifying hybrid signature verifiers that close red-team F112.
Previously the hybrid signature format produced by PQSigner.Sign
(length-prefixed classical || PQ concatenation) had no verifier in
this package, leaving every consumer to interpret it. The implicit
"either half verifies" OR semantics is as strong as the WEAKER of the
two halves (ECDSA in the production hybrids) — a forged hybrid
carrying a valid ECDSA half plus random "PQ" bytes would pass any OR
verifier.
The new verifiers require BOTH halves to verify under their respective
public keys. Failure on either half returns a typed sentinel
(ErrHybridSigClassicalFailed / ErrHybridSigPQFailed) without short-
circuiting on the classical refusal — both halves are checked before
the AND result is computed so timing-side channels do not leak which
half failed.
SplitHybridSignature is exported so callers can dispatch verification
through their own keying setup without re-implementing the wire-format
parsing.
Adds 9 regression tests:
- happy-path AND accepts (MLDSA + SLHDSA)
- tampered classical → ErrHybridSigClassicalFailed
- tampered PQ → ErrHybridSigPQFailed
- ECDSA-valid || random PQ bytes → ErrHybridSigPQFailed (the
canonical F112 attack)
- wrong message → either-half-fail
- truncated blob → ErrHybridSigTruncated
- wire-format round-trip
Closes red-team F112.
172 lines
4.5 KiB
Go
172 lines
4.5 KiB
Go
// Copyright (C) 2019-2025, Lux Industries, Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package wallet
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/luxfi/sdk/wallet/account"
|
|
)
|
|
|
|
// pqTestSeed is a local synthetic seed for the pq_wallet tests; the
|
|
// account package has its own testMasterSeed but it isn't exported.
|
|
var pqTestSeed = func() []byte {
|
|
h := sha256.Sum256([]byte("LUX/SDK/WALLET/PQWALLET/TEST/V1"))
|
|
h2 := sha256.Sum256(append(h[:], 0x02))
|
|
out := make([]byte, 0, 64)
|
|
out = append(out, h[:]...)
|
|
out = append(out, h2[:]...)
|
|
return out
|
|
}()
|
|
|
|
func mustAccount(t *testing.T, chainID, accountIdx, roleIdx uint32, role account.AccountRole) *account.PQAccount {
|
|
t.Helper()
|
|
a, err := account.NewPQAccount(pqTestSeed, chainID, accountIdx, roleIdx, role)
|
|
if err != nil {
|
|
t.Fatalf("NewPQAccount: %v", err)
|
|
}
|
|
return a
|
|
}
|
|
|
|
func TestPQWallet_AddAccount(t *testing.T) {
|
|
t.Parallel()
|
|
w := NewPQWallet()
|
|
a := mustAccount(t, 1, 0, 0, account.AccountRoleIdentity)
|
|
|
|
if err := w.AddAccount(a); err != nil {
|
|
t.Fatalf("AddAccount: %v", err)
|
|
}
|
|
got, ok := w.Account(a.AccountID)
|
|
if !ok {
|
|
t.Fatalf("Account(%x) not found after AddAccount", a.AccountID[:6])
|
|
}
|
|
if got != a {
|
|
t.Errorf("Account returned a different pointer than AddAccount stored")
|
|
}
|
|
}
|
|
|
|
func TestPQWallet_AddAccount_RejectsDuplicate(t *testing.T) {
|
|
t.Parallel()
|
|
w := NewPQWallet()
|
|
a := mustAccount(t, 1, 0, 0, account.AccountRoleIdentity)
|
|
|
|
if err := w.AddAccount(a); err != nil {
|
|
t.Fatalf("AddAccount: %v", err)
|
|
}
|
|
if err := w.AddAccount(a); err == nil {
|
|
t.Fatalf("AddAccount(duplicate) succeeded, expected error")
|
|
}
|
|
}
|
|
|
|
func TestPQWallet_AddAccount_RejectsNil(t *testing.T) {
|
|
t.Parallel()
|
|
if err := NewPQWallet().AddAccount(nil); err == nil {
|
|
t.Fatalf("AddAccount(nil) succeeded, expected error")
|
|
}
|
|
}
|
|
|
|
func TestPQWallet_RemoveAccount(t *testing.T) {
|
|
t.Parallel()
|
|
w := NewPQWallet()
|
|
a := mustAccount(t, 1, 0, 0, account.AccountRoleIdentity)
|
|
if err := w.AddAccount(a); err != nil {
|
|
t.Fatalf("AddAccount: %v", err)
|
|
}
|
|
if !w.RemoveAccount(a.AccountID) {
|
|
t.Fatalf("RemoveAccount returned false for present account")
|
|
}
|
|
if _, ok := w.Account(a.AccountID); ok {
|
|
t.Fatalf("Account still present after RemoveAccount")
|
|
}
|
|
// Second remove is idempotent-false.
|
|
if w.RemoveAccount(a.AccountID) {
|
|
t.Fatalf("RemoveAccount returned true for already-removed account")
|
|
}
|
|
}
|
|
|
|
func TestPQWallet_SignTx(t *testing.T) {
|
|
t.Parallel()
|
|
w := NewPQWallet()
|
|
a := mustAccount(t, 1, 0, 0, account.AccountRoleIdentity)
|
|
if err := w.AddAccount(a); err != nil {
|
|
t.Fatalf("AddAccount: %v", err)
|
|
}
|
|
|
|
env := &pqWalletEnvelope{}
|
|
copy(env.digest[:], []byte("pq-wallet-sign-test-digest-48-bytes-exact-len--!"))
|
|
|
|
if err := w.SignTx(a.AccountID, env); err != nil {
|
|
t.Fatalf("SignTx: %v", err)
|
|
}
|
|
if !a.Verify(env.digest, env.sig) {
|
|
t.Errorf("PQWallet.SignTx produced a signature that does not verify under the account")
|
|
}
|
|
}
|
|
|
|
func TestPQWallet_SignTx_UnknownAccount(t *testing.T) {
|
|
t.Parallel()
|
|
w := NewPQWallet()
|
|
var bogus account.AccountID
|
|
err := w.SignTx(bogus, &pqWalletEnvelope{})
|
|
if !errors.Is(err, ErrAccountNotFound) {
|
|
t.Fatalf("SignTx(unknown) = %v, want ErrAccountNotFound", err)
|
|
}
|
|
}
|
|
|
|
func TestPQWallet_AccountIDs(t *testing.T) {
|
|
t.Parallel()
|
|
w := NewPQWallet()
|
|
if len(w.AccountIDs()) != 0 {
|
|
t.Errorf("empty wallet should have 0 AccountIDs")
|
|
}
|
|
a := mustAccount(t, 1, 0, 0, account.AccountRoleIdentity)
|
|
b := mustAccount(t, 1, 1, 0, account.AccountRoleIdentity)
|
|
if err := w.AddAccount(a); err != nil {
|
|
t.Fatalf("AddAccount a: %v", err)
|
|
}
|
|
if err := w.AddAccount(b); err != nil {
|
|
t.Fatalf("AddAccount b: %v", err)
|
|
}
|
|
got := w.AccountIDs()
|
|
if len(got) != 2 {
|
|
t.Errorf("AccountIDs len = %d, want 2", len(got))
|
|
}
|
|
}
|
|
|
|
func TestPQWallet_RecoveryAccount(t *testing.T) {
|
|
t.Parallel()
|
|
w := NewPQWallet()
|
|
r, err := account.NewSLHDSARecoveryAccount(pqTestSeed, 1, 0)
|
|
if err != nil {
|
|
t.Fatalf("NewSLHDSARecoveryAccount: %v", err)
|
|
}
|
|
if err := w.AddRecovery(r); err != nil {
|
|
t.Fatalf("AddRecovery: %v", err)
|
|
}
|
|
got, ok := w.Recovery(r.AccountID)
|
|
if !ok {
|
|
t.Fatalf("Recovery not found after AddRecovery")
|
|
}
|
|
if got != r {
|
|
t.Errorf("Recovery returned a different pointer than AddRecovery stored")
|
|
}
|
|
}
|
|
|
|
// pqWalletEnvelope is a local TxAuthEnvelope test helper.
|
|
type pqWalletEnvelope struct {
|
|
digest [account.AccountIDSize]byte
|
|
sig []byte
|
|
}
|
|
|
|
func (e *pqWalletEnvelope) SigningDigest() ([account.AccountIDSize]byte, error) {
|
|
return e.digest, nil
|
|
}
|
|
|
|
func (e *pqWalletEnvelope) AttachSignature(_ account.WalletSchemeID, _, sig []byte) error {
|
|
e.sig = append([]byte(nil), sig...)
|
|
return nil
|
|
}
|