// 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/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/crypto/keccak"
)

// NewKeccakState creates a new KeccakState
func NewKeccakState() KeccakState {
	return keccak.NewLegacyKeccak256().(KeccakState)
}

var hasherPool = sync.Pool{
	New: func() any {
		return keccak.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
}
