mirror of
https://github.com/luxfi/crypto.git
synced 2026-07-27 01:54:50 +00:00
- Fix bn256/gnark imports to use crypto/bitutil instead of standalone bitutil - Remove unused secp256k1 import from crypto.go - Fix mlkem test files to use correct API (GenerateKeyPair returns pub,priv,err) - Fix mlkem constant names (MLKEM512SharedKeySize not SharedSecretSize) - Restore crypto/common types as standalone (not aliases to geth/common) - Update crypto.go and keccak.go to use crypto/common instead of geth/common - Fix secp256k1 fuzz tests to use correct DecompressPubkey/CompressPubkey API - Remove stale test files and go.work files crypto package is now fully independent of geth (geth -> crypto, not reverse)
64 lines
1.7 KiB
Go
64 lines
1.7 KiB
Go
// Copyright 2025 The go-ethereum Authors
|
|
// This file is part of the go-ethereum library.
|
|
//
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//go:build !ziren
|
|
|
|
package crypto
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/luxfi/crypto/common"
|
|
"golang.org/x/crypto/sha3"
|
|
)
|
|
|
|
// NewKeccakState creates a new KeccakState
|
|
func NewKeccakState() KeccakState {
|
|
return sha3.NewLegacyKeccak256().(KeccakState)
|
|
}
|
|
|
|
var hasherPool = sync.Pool{
|
|
New: func() any {
|
|
return sha3.NewLegacyKeccak256().(KeccakState)
|
|
},
|
|
}
|
|
|
|
// Keccak256 calculates and returns the Keccak256 hash of the input data.
|
|
func Keccak256(data ...[]byte) []byte {
|
|
b := make([]byte, 32)
|
|
d := hasherPool.Get().(KeccakState)
|
|
d.Reset()
|
|
for _, b := range data {
|
|
d.Write(b)
|
|
}
|
|
d.Read(b)
|
|
hasherPool.Put(d)
|
|
return b
|
|
}
|
|
|
|
// Keccak256Hash calculates and returns the Keccak256 hash of the input data,
|
|
// converting it to an internal Hash data structure.
|
|
func Keccak256Hash(data ...[]byte) (h common.Hash) {
|
|
d := hasherPool.Get().(KeccakState)
|
|
d.Reset()
|
|
for _, b := range data {
|
|
d.Write(b)
|
|
}
|
|
d.Read(h[:])
|
|
hasherPool.Put(d)
|
|
return h
|
|
}
|