mirror of
https://github.com/luxfi/crypto.git
synced 2026-10-30 16:59:13 +00:00
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.
69 lines
1.8 KiB
Go
69 lines
1.8 KiB
Go
package ntt
|
|
|
|
import "testing"
|
|
|
|
// Use a tiny prime (97) where 28 is a primitive 8th root of unity:
|
|
// 28^8 mod 97 = 1; for k = 1..7, 28^k mod 97 != 1.
|
|
const (
|
|
q = uint64(97)
|
|
omega = uint64(28)
|
|
)
|
|
|
|
func TestNTTRoundTrip(t *testing.T) {
|
|
want := []uint64{1, 2, 3, 4, 5, 6, 7, 8}
|
|
a := append([]uint64(nil), want...)
|
|
if err := NTT(a, omega, q); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Output is bit-reversed but transformed; we don't compare against
|
|
// hand-computed values here (that's brittle). Instead invert and check
|
|
// we get back the original.
|
|
omegaInv := powMod(omega, q-2, q) // Fermat
|
|
nInv := powMod(uint64(len(a)), q-2, q)
|
|
if err := INTT(a, omegaInv, nInv, q); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for i, v := range a {
|
|
if v != want[i] {
|
|
t.Errorf("round trip[%d] = %d; want %d", i, v, want[i])
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNTTRejectsBadInputs(t *testing.T) {
|
|
if err := NTT([]uint64{1, 2, 3}, omega, q); err != ErrNotPow2 {
|
|
t.Errorf("non-pow2: got %v want %v", err, ErrNotPow2)
|
|
}
|
|
if err := NTT([]uint64{1, 2}, omega, 0); err != ErrModulusZero {
|
|
t.Errorf("modulus 0: got %v want %v", err, ErrModulusZero)
|
|
}
|
|
}
|
|
|
|
func TestNTTLinearity(t *testing.T) {
|
|
// NTT(a + b) should equal NTT(a) + NTT(b) componentwise mod q.
|
|
a := []uint64{1, 0, 0, 0, 0, 0, 0, 0}
|
|
b := []uint64{0, 1, 0, 0, 0, 0, 0, 0}
|
|
sum := make([]uint64, 8)
|
|
for i := range sum {
|
|
sum[i] = (a[i] + b[i]) % q
|
|
}
|
|
|
|
A := append([]uint64(nil), a...)
|
|
B := append([]uint64(nil), b...)
|
|
S := append([]uint64(nil), sum...)
|
|
if err := NTT(A, omega, q); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := NTT(B, omega, q); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := NTT(S, omega, q); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for i := range S {
|
|
if want := (A[i] + B[i]) % q; want != S[i] {
|
|
t.Errorf("linearity fail at %d: got %d want %d", i, S[i], want)
|
|
}
|
|
}
|
|
}
|