Files
crypto/keccak/keccak_test.go
T
Hanzo AI ecaca10cdb canonical Go entry: backend selector + batch GPU paths via lux/accel
luxfi/crypto becomes the single Go entry point for ALL Lux-family crypto.
Every public function in this module now dispatches between three
implementations through a runtime-selectable backend:

  - vanilla: pure-Go reference (always available)
  - cgo:     native binding (blst, libsecp256k1, ckzg) where present
  - gpu:     batch acceleration via github.com/luxfi/accel

The dispatcher reads LUX_CRYPTO_BACKEND (auto|vanilla|cgo|gpu); auto
picks the most capable backend the binary was compiled and linked with.

New canonical packages:
  backend/             runtime backend selector (env + programmatic)
  internal/gpuhost/    accel session lifecycle, single per-process
  keccak/              Keccak-256 with batch GPU dispatch
  sha256/              SHA-256 with batch GPU dispatch
  sha3/                SHA3 / SHAKE family
  ripemd160/           RIPEMD-160 (Bitcoin/Lux address derivation)
  ed25519/             Ed25519 with batch GPU verify
  bn254/               canonical alias for bn256 (matches FIPS naming)
  modexp/              canonical alias for bigmodexp
  evm256/              EIP-196/197 precompile ABI wrappers
  poseidon/            Poseidon2 hash via gnark-crypto
  pedersen/            Pedersen commitments over BN254
  ntt/                 Number-Theoretic Transform reference
  polymul/             negacyclic polynomial multiplication

Extended existing packages with batch GPU paths:
  bls/batch.go         BatchVerify routes through accel.BLSVerifyBatch
  mldsa/batch.go       BatchVerify (ML-DSA-65) via accel.DilithiumVerifyBatch
  mlkem/batch.go       BatchEncapsulate / BatchDecapsulate via Kyber kernels
  secp256k1/batch.go   BatchVerifySignature via accel.ECDSAVerifyBatch

GPU dispatch is gated on (a) backend.Default(), (b) batch size threshold,
and (c) accel.Available(). When any gate fails the call falls through to
the vanilla CPU path; output is byte-identical.

The legacy gpu/ stub is replaced with a thin probe surface (Available,
Backend, Devices, Version) that delegates to the same gpuhost session.

Tests show vanilla and gpu backends produce identical outputs across all
batch entry points (-race clean).

See AUDIT.md for the per-algorithm state matrix and honest gaps.
2025-12-27 19:30:33 -08:00

116 lines
2.7 KiB
Go

package keccak
import (
"encoding/hex"
"testing"
)
// Vectors from Ethereum / NIST KAT for Keccak-256 (the original, not SHA3-256).
var vectors = []struct {
in, want string
}{
{"", "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"},
{"abc", "4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45"},
{"hello", "1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8"},
{"The quick brown fox jumps over the lazy dog", "4d741b6f1eb29cb2a9b9911c82f56fa8d73b04959d3d9d222895df6c0b28aa15"},
}
func mustHex(t *testing.T, s string) []byte {
t.Helper()
b, err := hex.DecodeString(s)
if err != nil {
t.Fatalf("hex decode: %v", err)
}
return b
}
func TestSum256Vectors(t *testing.T) {
for _, v := range vectors {
got := Sum256([]byte(v.in))
want := mustHex(t, v.want)
if string(got[:]) != string(want) {
t.Errorf("Sum256(%q) = %x; want %s", v.in, got, v.want)
}
}
}
func TestSum256BatchMatchesScalar(t *testing.T) {
inputs := make([][]byte, 16)
for i, v := range vectors {
inputs[i] = []byte(v.in)
}
for i := len(vectors); i < 16; i++ {
inputs[i] = []byte("padding")
}
got := Sum256Batch(inputs)
for i, in := range inputs {
want := Sum256(in)
if got[i] != want {
t.Errorf("batch[%d] mismatch: got %x want %x", i, got[i], want)
}
}
}
func TestSum256BatchLargeMatchesScalar(t *testing.T) {
// Cross batch threshold to exercise GPU path when present.
inputs := make([][]byte, BatchThreshold+8)
for i := range inputs {
inputs[i] = []byte("input-" + string(rune('a'+(i%26))))
}
got := Sum256Batch(inputs)
for i, in := range inputs {
want := Sum256(in)
if got[i] != want {
t.Errorf("batch[%d] mismatch", i)
}
}
}
func TestNewIncrementalEqualsSum256(t *testing.T) {
in := []byte("The quick brown fox jumps over the lazy dog")
h := New()
h.Write(in[:10])
h.Write(in[10:])
got := h.Sum(nil)
want := Sum256(in)
if string(got) != string(want[:]) {
t.Errorf("incremental %x != contiguous %x", got, want)
}
}
func TestConcat(t *testing.T) {
got := Concat([]byte("hello"), []byte(" "), []byte("world"))
want := Sum256([]byte("hello world"))
if got != want {
t.Errorf("Concat = %x; want %x", got, want)
}
}
func TestSum256Hex(t *testing.T) {
got := Sum256Hex([]byte("abc"))
if got != "4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45" {
t.Errorf("Sum256Hex(abc) = %s", got)
}
}
func BenchmarkSum256(b *testing.B) {
in := make([]byte, 1024)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = Sum256(in)
}
}
func BenchmarkSum256Batch(b *testing.B) {
inputs := make([][]byte, BatchThreshold)
for i := range inputs {
inputs[i] = make([]byte, 256)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = Sum256Batch(inputs)
}
}