mirror of
https://github.com/luxfi/crypto.git
synced 2026-07-27 01:54:50 +00:00
rename keccak/ → keccak256/ — package name names the variant, like secp256k1
The package is Keccak-256 only (Size=32, all Sum* functions return [32]byte). Naming it 'keccak' was misleading — Keccak is a family (256/384/512). Now matches the secp256k1 precision pattern. Function renames drop redundant '256' suffix: Sum256 → Sum Sum256Hex → SumHex Sum256Batch → SumBatch Call sites read: import "github.com/luxfi/crypto/keccak256" h := keccak256.Sum(data)
This commit is contained in:
@@ -11,7 +11,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/luxfi/crypto/backend"
|
||||
"github.com/luxfi/crypto/keccak"
|
||||
"github.com/luxfi/crypto/keccak256"
|
||||
"github.com/luxfi/crypto/sha256"
|
||||
)
|
||||
|
||||
@@ -27,10 +27,10 @@ func TestKeccak256BatchAcrossBackends(t *testing.T) {
|
||||
t.Cleanup(func() { backend.SetDefault(prev) })
|
||||
|
||||
backend.SetDefault(backend.Vanilla)
|
||||
vanilla := keccak.Sum256Batch(inputs)
|
||||
vanilla := keccak256.SumBatch(inputs)
|
||||
|
||||
backend.SetDefault(backend.GPU)
|
||||
gpu := keccak.Sum256Batch(inputs)
|
||||
gpu := keccak256.SumBatch(inputs)
|
||||
|
||||
for i := range vanilla {
|
||||
if vanilla[i] != gpu[i] {
|
||||
|
||||
@@ -9,4 +9,4 @@
|
||||
// exceed the BatchThreshold cutoff
|
||||
//
|
||||
// The dispatcher honours backend.Default(); see github.com/luxfi/crypto/backend.
|
||||
package keccak
|
||||
package keccak256
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright (C) 2020-2026, Lux Industries Inc. All rights reserved.
|
||||
// See the file LICENSE for licensing terms.
|
||||
|
||||
package keccak
|
||||
package keccak256
|
||||
|
||||
import (
|
||||
"github.com/luxfi/accel"
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright (C) 2020-2026, Lux Industries Inc. All rights reserved.
|
||||
// See the file LICENSE for licensing terms.
|
||||
|
||||
package keccak
|
||||
package keccak256
|
||||
|
||||
import (
|
||||
"hash"
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
// Size is the output size of Keccak-256 in bytes.
|
||||
const Size = 32
|
||||
|
||||
// BatchThreshold is the minimum batch length at which Sum256Batch will try to
|
||||
// BatchThreshold is the minimum batch length at which SumBatch will try to
|
||||
// route through GPU (lux/accel). Below this threshold the vanilla path is
|
||||
// always faster (PCIe round-trip dominates).
|
||||
//
|
||||
@@ -27,7 +27,7 @@ var pool = sync.Pool{
|
||||
}
|
||||
|
||||
// Sum256 returns the Keccak-256 hash of in. Allocations: 1.
|
||||
func Sum256(in []byte) [Size]byte {
|
||||
func Sum(in []byte) [Size]byte {
|
||||
switch backend.Resolve(false, false) {
|
||||
// Single-input keccak: GPU dispatch is uneconomic; cgo path identical to
|
||||
// vanilla today (golang.org/x/crypto/sha3 is asm-accelerated). One path.
|
||||
@@ -36,9 +36,9 @@ func Sum256(in []byte) [Size]byte {
|
||||
}
|
||||
}
|
||||
|
||||
// Sum256Hex is a convenience that returns a hex string.
|
||||
func Sum256Hex(in []byte) string {
|
||||
h := Sum256(in)
|
||||
// SumHex is a convenience that returns a hex string.
|
||||
func SumHex(in []byte) string {
|
||||
h := Sum(in)
|
||||
const hex = "0123456789abcdef"
|
||||
out := make([]byte, 2*Size)
|
||||
for i, b := range h {
|
||||
@@ -70,12 +70,12 @@ func Concat(inputs ...[]byte) [Size]byte {
|
||||
return out
|
||||
}
|
||||
|
||||
// Sum256Batch computes Keccak-256 for a batch of inputs.
|
||||
// SumBatch computes Keccak-256 for a batch of inputs.
|
||||
//
|
||||
// When the batch is large enough and the GPU backend is available the
|
||||
// computation runs on the GPU; otherwise it runs on the CPU. The output is
|
||||
// always byte-identical to repeated calls to Sum256.
|
||||
func Sum256Batch(inputs [][]byte) [][Size]byte {
|
||||
func SumBatch(inputs [][]byte) [][Size]byte {
|
||||
out := make([][Size]byte, len(inputs))
|
||||
if len(inputs) == 0 {
|
||||
return out
|
||||
@@ -1,4 +1,4 @@
|
||||
package keccak
|
||||
package keccak256
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
@@ -24,17 +24,17 @@ func mustHex(t *testing.T, s string) []byte {
|
||||
return b
|
||||
}
|
||||
|
||||
func TestSum256Vectors(t *testing.T) {
|
||||
func TestSumVectors(t *testing.T) {
|
||||
for _, v := range vectors {
|
||||
got := Sum256([]byte(v.in))
|
||||
got := Sum([]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)
|
||||
t.Errorf("Sum(%q) = %x; want %s", v.in, got, v.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSum256BatchMatchesScalar(t *testing.T) {
|
||||
func TestSumBatchMatchesScalar(t *testing.T) {
|
||||
inputs := make([][]byte, 16)
|
||||
for i, v := range vectors {
|
||||
inputs[i] = []byte(v.in)
|
||||
@@ -43,38 +43,38 @@ func TestSum256BatchMatchesScalar(t *testing.T) {
|
||||
inputs[i] = []byte("padding")
|
||||
}
|
||||
|
||||
got := Sum256Batch(inputs)
|
||||
got := SumBatch(inputs)
|
||||
for i, in := range inputs {
|
||||
want := Sum256(in)
|
||||
want := Sum(in)
|
||||
if got[i] != want {
|
||||
t.Errorf("batch[%d] mismatch: got %x want %x", i, got[i], want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSum256BatchLargeMatchesScalar(t *testing.T) {
|
||||
func TestSumBatchLargeMatchesScalar(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)
|
||||
got := SumBatch(inputs)
|
||||
for i, in := range inputs {
|
||||
want := Sum256(in)
|
||||
want := Sum(in)
|
||||
if got[i] != want {
|
||||
t.Errorf("batch[%d] mismatch", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewIncrementalEqualsSum256(t *testing.T) {
|
||||
func TestNewIncrementalEqualsSum(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)
|
||||
want := Sum(in)
|
||||
if string(got) != string(want[:]) {
|
||||
t.Errorf("incremental %x != contiguous %x", got, want)
|
||||
}
|
||||
@@ -82,34 +82,34 @@ func TestNewIncrementalEqualsSum256(t *testing.T) {
|
||||
|
||||
func TestConcat(t *testing.T) {
|
||||
got := Concat([]byte("hello"), []byte(" "), []byte("world"))
|
||||
want := Sum256([]byte("hello world"))
|
||||
want := Sum([]byte("hello world"))
|
||||
if got != want {
|
||||
t.Errorf("Concat = %x; want %x", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSum256Hex(t *testing.T) {
|
||||
got := Sum256Hex([]byte("abc"))
|
||||
func TestSumHex(t *testing.T) {
|
||||
got := SumHex([]byte("abc"))
|
||||
if got != "4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45" {
|
||||
t.Errorf("Sum256Hex(abc) = %s", got)
|
||||
t.Errorf("SumHex(abc) = %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkSum256(b *testing.B) {
|
||||
func BenchmarkSum(b *testing.B) {
|
||||
in := make([]byte, 1024)
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = Sum256(in)
|
||||
_ = Sum(in)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkSum256Batch(b *testing.B) {
|
||||
func BenchmarkSumBatch(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)
|
||||
_ = SumBatch(inputs)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user