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.
78 lines
2.2 KiB
Go
78 lines
2.2 KiB
Go
// Copyright (C) 2020-2026, Lux Industries Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package ed25519
|
|
|
|
import (
|
|
stded "crypto/ed25519"
|
|
"crypto/rand"
|
|
"errors"
|
|
"io"
|
|
)
|
|
|
|
// Sizes are the same as the standard library; re-exported for convenience.
|
|
const (
|
|
PublicKeySize = stded.PublicKeySize
|
|
PrivateKeySize = stded.PrivateKeySize
|
|
SignatureSize = stded.SignatureSize
|
|
SeedSize = stded.SeedSize
|
|
)
|
|
|
|
// BatchThreshold is the minimum batch length at which BatchVerify will try to
|
|
// route through GPU.
|
|
var BatchThreshold = 64
|
|
|
|
// PublicKey, PrivateKey, and Signature are aliases for the stdlib types so
|
|
// callers can mix this package and crypto/ed25519 freely.
|
|
type (
|
|
PublicKey = stded.PublicKey
|
|
PrivateKey = stded.PrivateKey
|
|
)
|
|
|
|
// GenerateKey generates a fresh keypair using rng. If rng is nil it uses
|
|
// crypto/rand.
|
|
func GenerateKey(rng io.Reader) (PublicKey, PrivateKey, error) {
|
|
if rng == nil {
|
|
rng = rand.Reader
|
|
}
|
|
return stded.GenerateKey(rng)
|
|
}
|
|
|
|
// NewKeyFromSeed deterministically derives a private key from a 32-byte seed.
|
|
func NewKeyFromSeed(seed []byte) (PrivateKey, error) {
|
|
if len(seed) != SeedSize {
|
|
return nil, errors.New("ed25519: seed length must be 32")
|
|
}
|
|
return stded.NewKeyFromSeed(seed), nil
|
|
}
|
|
|
|
// Sign produces a signature for msg.
|
|
func Sign(priv PrivateKey, msg []byte) []byte { return stded.Sign(priv, msg) }
|
|
|
|
// Verify reports whether sig is a valid signature of msg by pub.
|
|
func Verify(pub PublicKey, msg, sig []byte) bool { return stded.Verify(pub, msg, sig) }
|
|
|
|
// BatchVerify verifies a slice of (pub, msg, sig) triples. The result slice
|
|
// has one boolean per input. When the batch is large enough and a GPU
|
|
// backend is available the verification runs on the GPU; otherwise it
|
|
// runs on the CPU. Output is byte-identical to repeated calls to Verify.
|
|
func BatchVerify(pubs []PublicKey, msgs [][]byte, sigs [][]byte) []bool {
|
|
n := len(pubs)
|
|
if n != len(msgs) || n != len(sigs) {
|
|
panic("ed25519.BatchVerify: pubs/msgs/sigs length mismatch")
|
|
}
|
|
out := make([]bool, n)
|
|
if n == 0 {
|
|
return out
|
|
}
|
|
if n >= BatchThreshold {
|
|
if ok, err := batchGPU(pubs, msgs, sigs, out); ok && err == nil {
|
|
return out
|
|
}
|
|
}
|
|
for i := range pubs {
|
|
out[i] = stded.Verify(pubs[i], msgs[i], sigs[i])
|
|
}
|
|
return out
|
|
}
|