mirror of
https://github.com/luxfi/utxo.git
synced 2026-10-30 15:13:57 +00:00
- SA1019: math.Mul64/Add64 -> generic math.Mul/Add (ed25519fx, mldsafx, schnorrfx inputs; flow_checker; utxo_fetching); prefixdb.NewNested -> New (alias) - unused: DELETE dead codecVersion const (last codec reference in utxo — full ZAP native now), isSortedAndUniqueOrdered (root + secp256k1fx), mldsafx Keychain.get, and all 6 redundant TransferOutput.isState() markers (the embedded verify.IsState already provides State membership) - QF1008: drop embedded-field selectors (utxo_wire, ed25519fx, nftfx, propertyfx + tests); ST1005: lowercase Schnorr error string Behavior-identical; 11/11 packages pass. Co-authored-by: Hanzo Dev <[email protected]>
120 lines
2.6 KiB
Go
120 lines
2.6 KiB
Go
// Copyright (C) 2019-2025, Lux Industries, Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package ed25519fx
|
|
|
|
import (
|
|
"crypto/ed25519"
|
|
"crypto/rand"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/luxfi/crypto/hash"
|
|
"github.com/luxfi/ids"
|
|
log "github.com/luxfi/log"
|
|
)
|
|
|
|
// BenchmarkEd25519Verify measures Ed25519 signature verification cost.
|
|
// Ed25519 verify is faster than secp256k1 ecrecover (~0.5-0.7x cost).
|
|
func BenchmarkEd25519Verify(b *testing.B) {
|
|
pub, sk, err := ed25519.GenerateKey(rand.Reader)
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
|
|
addressBytes := hash.PubkeyBytesToAddress(pub)
|
|
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 Ed25519 verify")
|
|
tx := &TestTx{UnsignedBytes: txBytes}
|
|
|
|
sig := ed25519.Sign(sk, txBytes)
|
|
var sigArr [SigLen]byte
|
|
copy(sigArr[:], sig)
|
|
|
|
out := &OutputOwners{
|
|
Locktime: 0,
|
|
Threshold: 1,
|
|
Addrs: []ids.ShortID{addr},
|
|
}
|
|
in := &Input{SigIndices: []uint32{0}}
|
|
cred := &Credential{Sigs: [][SigLen]byte{sigArr}, PubKeys: [][]byte{pub}}
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
|
|
// BenchmarkEd25519VerifyCached measures cache-hit performance.
|
|
func BenchmarkEd25519VerifyCached(b *testing.B) {
|
|
pub, sk, err := ed25519.GenerateKey(rand.Reader)
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
|
|
addressBytes := hash.PubkeyBytesToAddress(pub)
|
|
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 Ed25519 verify cached")
|
|
tx := &TestTx{UnsignedBytes: txBytes}
|
|
|
|
sig := ed25519.Sign(sk, txBytes)
|
|
var sigArr [SigLen]byte
|
|
copy(sigArr[:], sig)
|
|
|
|
out := &OutputOwners{
|
|
Locktime: 0,
|
|
Threshold: 1,
|
|
Addrs: []ids.ShortID{addr},
|
|
}
|
|
in := &Input{SigIndices: []uint32{0}}
|
|
cred := &Credential{Sigs: [][SigLen]byte{sigArr}, PubKeys: [][]byte{pub}}
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|