2021-04-12 17:13:30 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2018 Status Research & Development GmbH
|
|
|
|
# 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
|
|
|
|
## ===========================
|
|
|
|
##
|
|
|
|
|
|
|
|
import
|
2021-04-21 17:04:54 +00:00
|
|
|
../../../constants,
|
2021-04-12 17:13:30 +00:00
|
|
|
../../../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
|
|
|
|
2023-02-14 14:37:21 +00:00
|
|
|
{.push raises: [CatchableError].} # basically the annotation type of a `Vm2OpFn`
|
|
|
|
|
2021-04-12 17:13:30 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private, op handlers implementation
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const
|
2021-04-20 12:07:01 +00:00
|
|
|
sha3Op: Vm2OpFn = proc (k: var Vm2Ctx) =
|
2021-04-14 09:53:30 +00:00
|
|
|
## 0x20, Compute Keccak-256 hash.
|
2021-04-12 17:13:30 +00:00
|
|
|
let (startPos, length) = k.cpt.stack.popInt(2)
|
|
|
|
|
|
|
|
let (pos, len) = (startPos.safeInt, length.safeInt)
|
|
|
|
if pos < 0 or len < 0 or pos > 2147483648'i64:
|
|
|
|
raise newException(OutOfBoundsRead, "Out of bounds memory access")
|
|
|
|
|
2023-08-27 06:13:37 +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:
|
|
|
|
k.cpt.stack.push:
|
2023-04-24 20:59:38 +00:00
|
|
|
keccakHash k.cpt.memory.bytes.toOpenArray(pos, endRange)
|
2021-04-12 17:13:30 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public, op exec table entries
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const
|
|
|
|
vm2OpExecHash*: seq[Vm2OpExec] = @[
|
|
|
|
|
2021-04-13 16:23:39 +00:00
|
|
|
(opCode: Op.Sha3, ## 0x20, Keccak-256
|
2021-04-12 17:13:30 +00:00
|
|
|
forks: Vm2OpAllForks,
|
2021-04-19 09:15:35 +00:00
|
|
|
name: "sha3",
|
2021-04-12 17:13:30 +00:00
|
|
|
info: "Compute Keccak-256 hash",
|
|
|
|
exec: (prep: vm2OpIgnore,
|
|
|
|
run: sha3Op,
|
|
|
|
post: vm2OpIgnore))]
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|