Files
utxo/secp256r1fx/fx_bench_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

127 lines
2.8 KiB
Go

// Copyright (C) 2019-2025, Lux Industries, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package secp256r1fx
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"testing"
"time"
"github.com/luxfi/crypto/hash"
"github.com/luxfi/ids"
log "github.com/luxfi/log"
)
// BenchmarkP256Verify measures P-256 ECDSA signature verification cost.
// P-256 verify is roughly on par with secp256k1 (~1x cost).
func BenchmarkP256Verify(b *testing.B) {
sk, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
b.Fatal(err)
}
pkBytes := pubKeyBytes(&sk.PublicKey)
addressBytes := hash.PubkeyBytesToAddress(pkBytes)
addr, err := ids.ToShortID(addressBytes)
if err != nil {
b.Fatal(err)
}
vm := &TestVM{
Log: log.NewNoOpLogger(),
}
vm.Clk.Set(time.Date(2026, time.January, 1, 0, 0, 0, 0, time.UTC))
fx := &Fx{}
if err := fx.Initialize(vm); err != nil {
b.Fatal(err)
}
if err := fx.Bootstrapped(); err != nil {
b.Fatal(err)
}
txBytes := []byte("benchmark tx payload for P-256 verify")
tx := &TestTx{UnsignedBytes: txBytes}
sigBytes, err := signP256(sk, txBytes)
if err != nil {
b.Fatal(err)
}
var sigArr [SigLen]byte
copy(sigArr[:], sigBytes)
out := &OutputOwners{
Locktime: 0,
Threshold: 1,
Addrs: []ids.ShortID{addr},
}
in := &Input{SigIndices: []uint32{0}}
cred := &Credential{Sigs: [][SigLen]byte{sigArr}, PubKeys: [][]byte{pkBytes}}
b.ResetTimer()
for i := 0; i < b.N; i++ {
fx.verifyCache.Flush()
if err := fx.VerifyCredentials(tx, in, cred, out); err != nil {
b.Fatal(err)
}
}
}
// BenchmarkP256VerifyCached measures cache-hit performance.
func BenchmarkP256VerifyCached(b *testing.B) {
sk, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
b.Fatal(err)
}
pkBytes := pubKeyBytes(&sk.PublicKey)
addressBytes := hash.PubkeyBytesToAddress(pkBytes)
addr, err := ids.ToShortID(addressBytes)
if err != nil {
b.Fatal(err)
}
vm := &TestVM{
Log: log.NewNoOpLogger(),
}
vm.Clk.Set(time.Date(2026, time.January, 1, 0, 0, 0, 0, time.UTC))
fx := &Fx{}
if err := fx.Initialize(vm); err != nil {
b.Fatal(err)
}
if err := fx.Bootstrapped(); err != nil {
b.Fatal(err)
}
txBytes := []byte("benchmark tx payload for P-256 verify cached")
tx := &TestTx{UnsignedBytes: txBytes}
sigBytes, err := signP256(sk, txBytes)
if err != nil {
b.Fatal(err)
}
var sigArr [SigLen]byte
copy(sigArr[:], sigBytes)
out := &OutputOwners{
Locktime: 0,
Threshold: 1,
Addrs: []ids.ShortID{addr},
}
in := &Input{SigIndices: []uint32{0}}
cred := &Credential{Sigs: [][SigLen]byte{sigArr}, PubKeys: [][]byte{pkBytes}}
if err := fx.VerifyCredentials(tx, in, cred, out); err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := fx.VerifyCredentials(tx, in, cred, out); err != nil {
b.Fatal(err)
}
}
}