From 3d6ff7c8616be578273c7acf5fdd11068e852526 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Miko=C5=82ajczyk?= Date: Thu, 13 Nov 2025 15:17:10 +0100 Subject: [PATCH 1/2] Add keccak_wasm.go --- crypto/keccak.go | 2 +- crypto/keccak_wasm.go | 55 ++++++++++++++++++++++++++++++++++++++++++ crypto/keccak_ziren.go | 2 +- 3 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 crypto/keccak_wasm.go 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..1959d34c4d --- /dev/null +++ b/crypto/keccak_wasm.go @@ -0,0 +1,55 @@ +// 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...) + } + + var inputPtr unsafe.Pointer + if len(flattenedInput) > 0 { + inputPtr = unsafe.Pointer(&flattenedInput[0]) + } else { + inputPtr = unsafe.Pointer(nil) + } + + outsourcedKeccak(inputPtr, uint32(len(flattenedInput)), 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 From 47225f94fba65b196a91fc1ccdf6e4850397f17b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Miko=C5=82ajczyk?= Date: Tue, 18 Nov 2025 13:55:37 +0100 Subject: [PATCH 2/2] minor code adjustment --- crypto/keccak_wasm.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/crypto/keccak_wasm.go b/crypto/keccak_wasm.go index 1959d34c4d..e6e2c706c9 100644 --- a/crypto/keccak_wasm.go +++ b/crypto/keccak_wasm.go @@ -40,15 +40,14 @@ func keccak256Digest(out []byte, data ...[]byte) { for _, b := range data { flattenedInput = append(flattenedInput, b...) } + inputLen := len(flattenedInput) - var inputPtr unsafe.Pointer - if len(flattenedInput) > 0 { + inputPtr := unsafe.Pointer(nil) + if inputLen > 0 { inputPtr = unsafe.Pointer(&flattenedInput[0]) - } else { - inputPtr = unsafe.Pointer(nil) } - outsourcedKeccak(inputPtr, uint32(len(flattenedInput)), unsafe.Pointer(&out[0])) + outsourcedKeccak(inputPtr, uint32(inputLen), unsafe.Pointer(&out[0])) } //go:wasmimport arbkeccak keccak256