diff --git a/crypto/keccak.go b/crypto/keccak.go index 0ad79a63c1..c31229e47e 100644 --- a/crypto/keccak.go +++ b/crypto/keccak.go @@ -14,7 +14,7 @@ // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . -//go:build !ziren +//go:build !ziren && !wasm package crypto diff --git a/crypto/keccak_wasm.go b/crypto/keccak_wasm.go new file mode 100644 index 0000000000..e6e2c706c9 --- /dev/null +++ b/crypto/keccak_wasm.go @@ -0,0 +1,54 @@ +// Copyright 2025, Offchain Labs, Inc. +// For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE.md + +//go:build wasm && !ziren + +package crypto + +import ( + "unsafe" + + "github.com/ethereum/go-ethereum/common" + "golang.org/x/crypto/sha3" +) + +// NewKeccakState creates a new KeccakState +func NewKeccakState() KeccakState { + return sha3.NewLegacyKeccak256().(KeccakState) +} + +// Keccak256 calculates and returns the Keccak256 hash of the input data. +func Keccak256(data ...[]byte) []byte { + b := make([]byte, 32) + keccak256Digest(b, data...) + 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) { + keccak256Digest(h[:], data...) + return h +} + +func keccak256Digest(out []byte, data ...[]byte) { + if len(out) != 32 { + panic("output buffer must be 32 bytes") + } + + flattenedInput := make([]byte, 0) + for _, b := range data { + flattenedInput = append(flattenedInput, b...) + } + inputLen := len(flattenedInput) + + inputPtr := unsafe.Pointer(nil) + if inputLen > 0 { + inputPtr = unsafe.Pointer(&flattenedInput[0]) + } + + outsourcedKeccak(inputPtr, uint32(inputLen), unsafe.Pointer(&out[0])) +} + +//go:wasmimport arbkeccak keccak256 +func outsourcedKeccak(inBuf unsafe.Pointer, inLen uint32, outBuf unsafe.Pointer) diff --git a/crypto/keccak_ziren.go b/crypto/keccak_ziren.go index 8e967c6dbf..67a9c17487 100644 --- a/crypto/keccak_ziren.go +++ b/crypto/keccak_ziren.go @@ -14,7 +14,7 @@ // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . -//go:build ziren +//go:build ziren && !wasm package crypto