2021-04-12 17:13:30 +00:00
|
|
|
# Nimbus
|
2024-06-07 08:24:32 +00:00
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
2021-04-12 17:13:30 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except
|
|
|
|
# according to those terms.
|
|
|
|
|
|
|
|
## EVM Opcode Handlers: Hashes
|
|
|
|
## ===========================
|
|
|
|
##
|
|
|
|
|
2024-06-07 08:24:32 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2021-04-12 17:13:30 +00:00
|
|
|
import
|
2021-04-21 17:04:54 +00:00
|
|
|
../../../constants,
|
2024-06-07 08:24:32 +00:00
|
|
|
../../evm_errors,
|
2021-04-26 16:00:46 +00:00
|
|
|
../../computation,
|
2021-04-22 16:05:58 +00:00
|
|
|
../../memory,
|
2021-04-21 17:04:54 +00:00
|
|
|
../../stack,
|
|
|
|
../gas_costs,
|
|
|
|
../op_codes,
|
2021-04-22 16:05:58 +00:00
|
|
|
../utils/utils_numeric,
|
2021-04-14 09:53:30 +00:00
|
|
|
./oph_defs,
|
2022-09-03 18:15:35 +00:00
|
|
|
eth/common
|
2021-04-12 17:13:30 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private, op handlers implementation
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const
|
2024-06-15 16:18:53 +00:00
|
|
|
sha3Op: VmOpFn = proc (k: var VmCtx): EvmResultVoid =
|
2021-04-14 09:53:30 +00:00
|
|
|
## 0x20, Compute Keccak-256 hash.
|
2024-06-07 08:24:32 +00:00
|
|
|
let
|
|
|
|
(startPos, length) = ? k.cpt.stack.popInt(2)
|
|
|
|
(pos, len) = (startPos.safeInt, length.safeInt)
|
2021-04-12 17:13:30 +00:00
|
|
|
|
|
|
|
if pos < 0 or len < 0 or pos > 2147483648'i64:
|
2024-06-07 08:24:32 +00:00
|
|
|
return err(opErr(OutOfBounds))
|
2021-04-12 17:13:30 +00:00
|
|
|
|
2024-06-07 08:24:32 +00:00
|
|
|
? k.cpt.opcodeGastCost(Op.Sha3,
|
2021-04-14 16:51:54 +00:00
|
|
|
k.cpt.gasCosts[Op.Sha3].m_handler(k.cpt.memory.len, pos, len),
|
|
|
|
reason = "SHA3: word gas cost")
|
2021-04-12 17:13:30 +00:00
|
|
|
|
|
|
|
k.cpt.memory.extend(pos, len)
|
2021-04-14 16:51:54 +00:00
|
|
|
|
2021-04-12 17:13:30 +00:00
|
|
|
let endRange = min(pos + len, k.cpt.memory.len) - 1
|
|
|
|
if endRange == -1 or pos >= k.cpt.memory.len:
|
|
|
|
k.cpt.stack.push(EMPTY_SHA3)
|
|
|
|
else:
|
2024-06-07 08:24:32 +00:00
|
|
|
k.cpt.stack.push keccakHash k.cpt.memory.bytes.toOpenArray(pos, endRange)
|
2021-04-12 17:13:30 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public, op exec table entries
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const
|
2024-06-15 16:18:53 +00:00
|
|
|
VmOpExecHash*: seq[VmOpExec] = @[
|
2021-04-12 17:13:30 +00:00
|
|
|
|
2021-04-13 16:23:39 +00:00
|
|
|
(opCode: Op.Sha3, ## 0x20, Keccak-256
|
2024-06-15 16:18:53 +00:00
|
|
|
forks: VmOpAllForks,
|
2021-04-19 09:15:35 +00:00
|
|
|
name: "sha3",
|
2021-04-12 17:13:30 +00:00
|
|
|
info: "Compute Keccak-256 hash",
|
2024-06-15 16:18:53 +00:00
|
|
|
exec: (prep: VmOpIgnore,
|
2021-04-12 17:13:30 +00:00
|
|
|
run: sha3Op,
|
2024-06-15 16:18:53 +00:00
|
|
|
post: VmOpIgnore))]
|
2021-04-12 17:13:30 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|