Files
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

64 lines
1.3 KiB
Go

// Copyright (C) 2020-2026, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package polymul
import (
"errors"
"math/bits"
)
var (
ErrLengthMismatch = errors.New("polymul: a and b must have equal length")
ErrModulusZero = errors.New("polymul: modulus must be non-zero")
)
// MulNegacyclic multiplies two polynomials a and b in Z_q[X] / (X^N + 1).
// a, b, and the returned polynomial all have length N. q must be non-zero.
//
// Schoolbook O(N^2). For N up to 256 this is the simplest correct path.
// For production lattice work use NTT-based multiplication.
func MulNegacyclic(a, b []uint64, q uint64) ([]uint64, error) {
n := len(a)
if n != len(b) {
return nil, ErrLengthMismatch
}
if q == 0 {
return nil, ErrModulusZero
}
c := make([]uint64, n)
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
t := mulMod(a[i], b[j], q)
k := i + j
if k < n {
c[k] = addMod(c[k], t, q)
} else {
c[k-n] = subMod(c[k-n], t, q)
}
}
}
return c, nil
}
func addMod(a, b, q uint64) uint64 {
s := a + b
if s >= q {
s -= q
}
return s
}
func subMod(a, b, q uint64) uint64 {
if a >= b {
return a - b
}
return q - (b - a)
}
func mulMod(a, b, q uint64) uint64 {
hi, lo := bits.Mul64(a, b)
_, rem := bits.Div64(hi, lo, q)
return rem
}