mirror of
https://github.com/luxfi/crypto.git
synced 2026-07-27 01:54:50 +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.
86 lines
1.9 KiB
Go
86 lines
1.9 KiB
Go
// Copyright (C) 2020-2026, Lux Industries Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package bls
|
|
|
|
import (
|
|
"github.com/luxfi/accel"
|
|
"github.com/luxfi/crypto/backend"
|
|
"github.com/luxfi/crypto/internal/gpuhost"
|
|
)
|
|
|
|
func batchVerifyGPU(pks []*PublicKey, msgs [][]byte, sigs []*Signature, out []bool) (bool, error) {
|
|
if backend.Resolve(gpuhost.Available(), false) != backend.GPU {
|
|
return false, nil
|
|
}
|
|
sess := gpuhost.Session()
|
|
if sess == nil {
|
|
return false, nil
|
|
}
|
|
|
|
n := len(pks)
|
|
width := 0
|
|
for _, m := range msgs {
|
|
if len(m) > width {
|
|
width = len(m)
|
|
}
|
|
}
|
|
if width == 0 {
|
|
width = 1
|
|
}
|
|
|
|
mFlat := make([]uint8, n*width)
|
|
for i, m := range msgs {
|
|
copy(mFlat[i*width:(i+1)*width], m)
|
|
}
|
|
pFlat := make([]uint8, n*PublicKeyLen)
|
|
for i, p := range pks {
|
|
b := PublicKeyToCompressedBytes(p)
|
|
if len(b) != PublicKeyLen {
|
|
return false, nil
|
|
}
|
|
copy(pFlat[i*PublicKeyLen:(i+1)*PublicKeyLen], b)
|
|
}
|
|
sFlat := make([]uint8, n*SignatureLen)
|
|
for i, s := range sigs {
|
|
b := SignatureToBytes(s)
|
|
if len(b) != SignatureLen {
|
|
return false, nil
|
|
}
|
|
copy(sFlat[i*SignatureLen:(i+1)*SignatureLen], b)
|
|
}
|
|
|
|
mT, err := accel.NewTensorWithData[uint8](sess, []int{n, width}, mFlat)
|
|
if err != nil {
|
|
return false, nil
|
|
}
|
|
defer mT.Close()
|
|
sT, err := accel.NewTensorWithData[uint8](sess, []int{n, SignatureLen}, sFlat)
|
|
if err != nil {
|
|
return false, nil
|
|
}
|
|
defer sT.Close()
|
|
pT, err := accel.NewTensorWithData[uint8](sess, []int{n, PublicKeyLen}, pFlat)
|
|
if err != nil {
|
|
return false, nil
|
|
}
|
|
defer pT.Close()
|
|
rT, err := accel.NewTensor[uint8](sess, []int{n})
|
|
if err != nil {
|
|
return false, nil
|
|
}
|
|
defer rT.Close()
|
|
|
|
if err := sess.Crypto().BLSVerifyBatch(mT.Untyped(), sT.Untyped(), pT.Untyped(), rT.Untyped()); err != nil {
|
|
return false, nil
|
|
}
|
|
bytes, err := rT.ToSlice()
|
|
if err != nil {
|
|
return false, nil
|
|
}
|
|
for i, b := range bytes {
|
|
out[i] = b == 1
|
|
}
|
|
return true, nil
|
|
}
|