|
| 1 | +// Copyright 2025 The go-ethereum Authors |
| 2 | +// This file is part of the go-ethereum library. |
| 3 | +// |
| 4 | +// The go-ethereum library is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Lesser General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// The go-ethereum library is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Lesser General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Lesser General Public License |
| 15 | +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +//go:build ziren |
| 18 | + |
| 19 | +package crypto |
| 20 | + |
| 21 | +import ( |
| 22 | + "github.com/ProjectZKM/Ziren/crates/go-runtime/zkvm_runtime" |
| 23 | + "github.com/ethereum/go-ethereum/common" |
| 24 | + "golang.org/x/crypto/sha3" |
| 25 | +) |
| 26 | + |
| 27 | +// NewKeccakState creates a new KeccakState |
| 28 | +// For now, we fallback to the original implementation for the stateful interface. |
| 29 | +// TODO: Implement a stateful wrapper around zkvm_runtime.Keccak256 if needed. |
| 30 | +func NewKeccakState() KeccakState { |
| 31 | + return sha3.NewLegacyKeccak256().(KeccakState) |
| 32 | +} |
| 33 | + |
| 34 | +// Keccak256 calculates and returns the Keccak256 hash using the Ziren zkvm_runtime implementation. |
| 35 | +func Keccak256(data ...[]byte) []byte { |
| 36 | + // For multiple data chunks, concatenate them |
| 37 | + if len(data) == 0 { |
| 38 | + result := zkvm_runtime.Keccak256(nil) |
| 39 | + return result[:] |
| 40 | + } |
| 41 | + if len(data) == 1 { |
| 42 | + result := zkvm_runtime.Keccak256(data[0]) |
| 43 | + return result[:] |
| 44 | + } |
| 45 | + |
| 46 | + // Concatenate multiple data chunks |
| 47 | + var totalLen int |
| 48 | + for _, d := range data { |
| 49 | + totalLen += len(d) |
| 50 | + } |
| 51 | + |
| 52 | + combined := make([]byte, 0, totalLen) |
| 53 | + for _, d := range data { |
| 54 | + combined = append(combined, d...) |
| 55 | + } |
| 56 | + |
| 57 | + result := zkvm_runtime.Keccak256(combined) |
| 58 | + return result[:] |
| 59 | +} |
| 60 | + |
| 61 | +// Keccak256Hash calculates and returns the Keccak256 hash as a Hash using the Ziren zkvm_runtime implementation. |
| 62 | +func Keccak256Hash(data ...[]byte) common.Hash { |
| 63 | + return common.Hash(Keccak256(data...)) |
| 64 | +} |
0 commit comments