Files
utxo/slhdsafx/fx_test.go
zeekay c6e2a4ce51 fxs: strip linearcodec from tests + drop luxfi/codec direct dep
Completes the second cut of FINAL_RIP.md. The first cut (a0aab0a) dropped
the codec.Registry field from each fx's VM interface and the public API
codec.Manager params. This cut removes the remaining test-side residue:

- Strip `linearcodec.NewDefault()` Codec field literals from every TestVM
  (the field no longer exists on TestVM).
- Drop the obsolete `TestCredentialSerialize`, `TestTransferInputSerialize`,
  and `TestOutputSerialize` tests in secp256k1fx — they asserted the
  legacy linearcodec wire-byte format. ZAP-native round-trips are covered
  in wire/wire_test.go and the per-fx wire_test.go files.
- `go mod edit -droprequire github.com/luxfi/codec` + tidy — codec is now
  indirect-only (transitively required via github.com/luxfi/vm for the
  verify.State interface).

Build + test clean across all 11 packages. ZAP-native at the activation
boundary 2025-12-25T16:20:00-08:00.
2026-06-03 12:33:04 -07:00

464 lines
10 KiB
Go

// Copyright (C) 2019-2025, Lux Industries, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package slhdsafx
import (
"crypto/rand"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/luxfi/crypto/hash"
"github.com/luxfi/crypto/slhdsa"
"github.com/luxfi/ids"
log "github.com/luxfi/log"
)
func newTestFx(t *testing.T) (*Fx, *slhdsa.PrivateKey, []byte) {
t.Helper()
require := require.New(t)
sk, err := slhdsa.GenerateKey(rand.Reader, slhdsa.SHA2_192f)
require.NoError(err)
vm := &TestVM{
Log: log.NewNoOpLogger(),
}
vm.Clk.Set(time.Date(2026, time.January, 1, 0, 0, 0, 0, time.UTC))
fx := &Fx{}
require.NoError(fx.Initialize(vm))
require.NoError(fx.Bootstrapping())
require.NoError(fx.Bootstrapped())
pkBytes := sk.PublicKey.Bytes()
return fx, sk, pkBytes
}
func TestFxInitialize(t *testing.T) {
vm := TestVM{
Log: log.NewNoOpLogger(),
}
fx := Fx{}
require.NoError(t, fx.Initialize(&vm))
}
func TestFxInitializeInvalid(t *testing.T) {
fx := Fx{}
err := fx.Initialize(nil)
require.ErrorIs(t, err, ErrWrongVMType)
}
func TestFxVerifyTransfer(t *testing.T) {
require := require.New(t)
fx, sk, pkBytes := newTestFx(t)
txBytes := []byte{0, 1, 2, 3, 4, 5}
tx := &TestTx{UnsignedBytes: txBytes}
sig, err := sk.SignCtx(rand.Reader, txBytes, utxoSignCtx)
require.NoError(err)
out := &TransferOutput{
Amt: 1,
OutputOwners: OutputOwners{
Level: SecLevelSLH192f,
Locktime: 0,
Threshold: 1,
Addrs: [][]byte{pkBytes},
},
}
in := &TransferInput{
Amt: 1,
Input: Input{
SigIndices: []uint32{0},
},
}
cred := &Credential{
Level: SecLevelSLH192f,
Sigs: [][]byte{sig},
}
require.NoError(fx.VerifyTransfer(tx, in, cred, out))
}
func TestFxVerifyTransferWrongSig(t *testing.T) {
require := require.New(t)
fx, _, pkBytes := newTestFx(t)
txBytes := []byte{0, 1, 2, 3, 4, 5}
tx := &TestTx{UnsignedBytes: txBytes}
badSig := make([]byte, SLH192fSigLen)
out := &TransferOutput{
Amt: 1,
OutputOwners: OutputOwners{
Level: SecLevelSLH192f,
Locktime: 0,
Threshold: 1,
Addrs: [][]byte{pkBytes},
},
}
in := &TransferInput{
Amt: 1,
Input: Input{
SigIndices: []uint32{0},
},
}
cred := &Credential{
Level: SecLevelSLH192f,
Sigs: [][]byte{badSig},
}
err := fx.VerifyTransfer(tx, in, cred, out)
require.ErrorIs(err, ErrWrongSig)
}
func TestFxVerifyTransferMismatchedAmounts(t *testing.T) {
require := require.New(t)
fx, sk, pkBytes := newTestFx(t)
txBytes := []byte{0, 1, 2, 3, 4, 5}
tx := &TestTx{UnsignedBytes: txBytes}
sig, err := sk.SignCtx(rand.Reader, txBytes, utxoSignCtx)
require.NoError(err)
out := &TransferOutput{
Amt: 1,
OutputOwners: OutputOwners{
Level: SecLevelSLH192f,
Locktime: 0,
Threshold: 1,
Addrs: [][]byte{pkBytes},
},
}
in := &TransferInput{
Amt: 2,
Input: Input{
SigIndices: []uint32{0},
},
}
cred := &Credential{
Level: SecLevelSLH192f,
Sigs: [][]byte{sig},
}
err = fx.VerifyTransfer(tx, in, cred, out)
require.ErrorIs(err, ErrMismatchedAmounts)
}
func TestFxVerifyTransferTimelocked(t *testing.T) {
require := require.New(t)
fx, sk, pkBytes := newTestFx(t)
txBytes := []byte{0, 1, 2, 3, 4, 5}
tx := &TestTx{UnsignedBytes: txBytes}
sig, err := sk.SignCtx(rand.Reader, txBytes, utxoSignCtx)
require.NoError(err)
out := &TransferOutput{
Amt: 1,
OutputOwners: OutputOwners{
Level: SecLevelSLH192f,
Locktime: uint64(time.Date(2099, time.January, 1, 0, 0, 0, 0, time.UTC).Unix()),
Threshold: 1,
Addrs: [][]byte{pkBytes},
},
}
in := &TransferInput{
Amt: 1,
Input: Input{
SigIndices: []uint32{0},
},
}
cred := &Credential{
Level: SecLevelSLH192f,
Sigs: [][]byte{sig},
}
err = fx.VerifyTransfer(tx, in, cred, out)
require.ErrorIs(err, ErrTimelocked)
}
func TestFxVerifyCredentials(t *testing.T) {
require := require.New(t)
fx, sk, pkBytes := newTestFx(t)
txBytes := []byte("test transaction")
tx := &TestTx{UnsignedBytes: txBytes}
sig, err := sk.SignCtx(rand.Reader, txBytes, utxoSignCtx)
require.NoError(err)
out := &OutputOwners{
Level: SecLevelSLH192f,
Locktime: 0,
Threshold: 1,
Addrs: [][]byte{pkBytes},
}
in := &Input{
SigIndices: []uint32{0},
}
cred := &Credential{
Level: SecLevelSLH192f,
Sigs: [][]byte{sig},
}
require.NoError(fx.VerifyCredentials(tx, in, cred, out))
}
func TestFxVerifyPermission(t *testing.T) {
require := require.New(t)
fx, sk, pkBytes := newTestFx(t)
txBytes := []byte("permission tx")
tx := &TestTx{UnsignedBytes: txBytes}
sig, err := sk.SignCtx(rand.Reader, txBytes, utxoSignCtx)
require.NoError(err)
owner := &OutputOwners{
Level: SecLevelSLH192f,
Locktime: 0,
Threshold: 1,
Addrs: [][]byte{pkBytes},
}
in := &Input{
SigIndices: []uint32{0},
}
cred := &Credential{
Level: SecLevelSLH192f,
Sigs: [][]byte{sig},
}
require.NoError(fx.VerifyPermission(tx, in, cred, owner))
}
func TestFxVerifyPermissionWrongTypes(t *testing.T) {
fx, _, _ := newTestFx(t)
require.ErrorIs(t, fx.VerifyPermission("bad", nil, nil, nil), ErrWrongTxType)
tx := &TestTx{}
require.ErrorIs(t, fx.VerifyPermission(tx, "bad", nil, nil), ErrWrongInputType)
require.ErrorIs(t, fx.VerifyPermission(tx, &Input{}, "bad", nil), ErrWrongCredentialType)
require.ErrorIs(t, fx.VerifyPermission(tx, &Input{}, &Credential{Level: SecLevelSLH192f}, "bad"), ErrWrongOwnerType)
}
func TestFxVerifyTransferBootstrapping(t *testing.T) {
require := require.New(t)
sk, err := slhdsa.GenerateKey(rand.Reader, slhdsa.SHA2_192f)
require.NoError(err)
pkBytes := sk.PublicKey.Bytes()
vm := &TestVM{
Log: log.NewNoOpLogger(),
}
vm.Clk.Set(time.Date(2026, time.January, 1, 0, 0, 0, 0, time.UTC))
fx := &Fx{}
require.NoError(fx.Initialize(vm))
require.NoError(fx.Bootstrapping())
txBytes := []byte{0, 1, 2, 3, 4, 5}
tx := &TestTx{UnsignedBytes: txBytes}
badSig := make([]byte, SLH192fSigLen)
out := &TransferOutput{
Amt: 1,
OutputOwners: OutputOwners{
Level: SecLevelSLH192f,
Locktime: 0,
Threshold: 1,
Addrs: [][]byte{pkBytes},
},
}
in := &TransferInput{
Amt: 1,
Input: Input{
SigIndices: []uint32{0},
},
}
cred := &Credential{
Level: SecLevelSLH192f,
Sigs: [][]byte{badSig},
}
require.NoError(fx.VerifyTransfer(tx, in, cred, out))
}
func TestFxCreateOutput(t *testing.T) {
require := require.New(t)
fx, _, pkBytes := newTestFx(t)
owner := &OutputOwners{
Level: SecLevelSLH192f,
Locktime: 0,
Threshold: 1,
Addrs: [][]byte{pkBytes},
}
result, err := fx.CreateOutput(100, owner)
require.NoError(err)
out, ok := result.(*TransferOutput)
require.True(ok)
require.Equal(uint64(100), out.Amt)
}
func TestFxVerifyOperation(t *testing.T) {
require := require.New(t)
fx, sk, pkBytes := newTestFx(t)
txBytes := []byte("mint operation tx")
tx := &TestTx{UnsignedBytes: txBytes}
sig, err := sk.SignCtx(rand.Reader, txBytes, utxoSignCtx)
require.NoError(err)
owners := OutputOwners{
Level: SecLevelSLH192f,
Locktime: 0,
Threshold: 1,
Addrs: [][]byte{pkBytes},
}
utxo := &MintOutput{OutputOwners: owners}
op := &MintOperation{
MintInput: Input{SigIndices: []uint32{0}},
MintOutput: MintOutput{
OutputOwners: owners,
},
TransferOutput: TransferOutput{
Amt: 1,
OutputOwners: owners,
},
}
cred := &Credential{
Level: SecLevelSLH192f,
Sigs: [][]byte{sig},
}
require.NoError(fx.VerifyOperation(tx, op, cred, []interface{}{utxo}))
}
func TestFxAddressDerivation(t *testing.T) {
require := require.New(t)
sk, err := slhdsa.GenerateKey(rand.Reader, slhdsa.SHA2_192f)
require.NoError(err)
pkBytes := sk.PublicKey.Bytes()
addressBytes := hash.PubkeyBytesToAddress(pkBytes)
addr, err := ids.ToShortID(addressBytes)
require.NoError(err)
require.NotEqual(ids.ShortEmpty, addr)
}
// Regression: Finding 1 -- Threshold==0 with addresses must be rejected
func TestOutputOwners_RejectThresholdZero(t *testing.T) {
require := require.New(t)
sk, err := slhdsa.GenerateKey(rand.Reader, slhdsa.SHA2_192f)
require.NoError(err)
out := &OutputOwners{
Level: SecLevelSLH192f,
Locktime: 0,
Threshold: 0,
Addrs: [][]byte{sk.PublicKey.Bytes()},
}
require.ErrorIs(out.Verify(), ErrOutputUnoptimized)
}
// Regression: Finding 3 -- Addresses() must return hashed ShortIDs
func TestOutputOwners_Addresses_UsesHash(t *testing.T) {
require := require.New(t)
sk, err := slhdsa.GenerateKey(rand.Reader, slhdsa.SHA2_192f)
require.NoError(err)
pkBytes := sk.PublicKey.Bytes()
out := &OutputOwners{
Level: SecLevelSLH192f,
Locktime: 0,
Threshold: 1,
Addrs: [][]byte{pkBytes},
}
addrs := out.Addresses()
require.Len(addrs, 1)
require.NotEqual(ids.ShortEmpty, addrs[0])
expectedBytes := hash.PubkeyBytesToAddress(pkBytes)
expected, err := ids.ToShortID(expectedBytes)
require.NoError(err)
require.Equal(expected, addrs[0])
}
// Regression: Finding 6 -- Empty Sigs slice must be rejected
func TestCredential_RejectEmptySigs(t *testing.T) {
cred := &Credential{
Level: SecLevelSLH192f,
Sigs: [][]byte{},
}
require.ErrorIs(t, cred.Verify(), ErrEmptyCredential)
}
// Regression: Finding 5 -- Verify cache
func TestFxVerifyCache(t *testing.T) {
require := require.New(t)
fx, sk, pkBytes := newTestFx(t)
txBytes := []byte("cache test tx")
tx := &TestTx{UnsignedBytes: txBytes}
sig, err := sk.SignCtx(rand.Reader, txBytes, utxoSignCtx)
require.NoError(err)
out := &OutputOwners{
Level: SecLevelSLH192f,
Locktime: 0,
Threshold: 1,
Addrs: [][]byte{pkBytes},
}
in := &Input{SigIndices: []uint32{0}}
cred := &Credential{Level: SecLevelSLH192f, Sigs: [][]byte{sig}}
require.NoError(fx.VerifyCredentials(tx, in, cred, out))
require.Equal(1, fx.verifyCache.Len())
require.NoError(fx.VerifyCredentials(tx, in, cred, out))
require.Equal(1, fx.verifyCache.Len())
}
// Regression: Finding 2 -- Domain separation
func TestFxVerifyCredentials_RejectNilContext(t *testing.T) {
require := require.New(t)
fx, sk, pkBytes := newTestFx(t)
txBytes := []byte("domain sep test")
tx := &TestTx{UnsignedBytes: txBytes}
sig, err := sk.Sign(rand.Reader, txBytes, nil)
require.NoError(err)
out := &OutputOwners{
Level: SecLevelSLH192f,
Locktime: 0,
Threshold: 1,
Addrs: [][]byte{pkBytes},
}
in := &Input{SigIndices: []uint32{0}}
cred := &Credential{Level: SecLevelSLH192f, Sigs: [][]byte{sig}}
err = fx.VerifyCredentials(tx, in, cred, out)
require.ErrorIs(err, ErrWrongSig)
}