From 19bd0be16bf403b2615ab8059abd6ff0d5e05458 Mon Sep 17 00:00:00 2001 From: Eric Kilmer Date: Sun, 15 Jan 2023 18:06:28 -0500 Subject: [PATCH] Remove pysha3 and use hashlib for sha3 (keccak) --- manticore/ethereum/abi.py | 4 ++-- manticore/platforms/evm.py | 14 +++++++------- tests/auto_generators/make_VMTests.py | 4 ++-- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/manticore/ethereum/abi.py b/manticore/ethereum/abi.py index 1350b3cef..ebf63e217 100644 --- a/manticore/ethereum/abi.py +++ b/manticore/ethereum/abi.py @@ -3,7 +3,7 @@ import uuid import re -import sha3 +import hashlib from . import abitypes from ..core.smtlib import ( @@ -198,7 +198,7 @@ def function_selector(method_name_and_signature): """ Makes a function hash id from a method signature """ - s = sha3.keccak_256() + s = hashlib.sha3_256() s.update(method_name_and_signature.encode()) return bytes(s.digest()[:4]) diff --git a/manticore/platforms/evm.py b/manticore/platforms/evm.py index 04567cd69..841972a5c 100644 --- a/manticore/platforms/evm.py +++ b/manticore/platforms/evm.py @@ -36,7 +36,7 @@ import pyevmasm as EVMAsm import logging from collections import namedtuple -import sha3 +import hashlib import rlp logger = logging.getLogger(__name__) @@ -60,7 +60,7 @@ def globalsha3(data): if issymbolic(data): return None - return int(sha3.keccak_256(data).hexdigest(), 16) + return int(hashlib.sha3_256(data).hexdigest(), 16) def globalfakesha3(data): @@ -1647,7 +1647,7 @@ def SHA3_gas(self, start, size): @concretized_args(size="ALL") def SHA3(self, start, size): - """Compute Keccak-256 hash + """Compute Keccak-256 (sha3-256) hash If the size is symbolic the potential solutions will be sampled as defined by the default policy and the analysis will be forked. The `size` can be considered concrete in this handler. @@ -2511,7 +2511,7 @@ def symbolic_function(self, func, data): self._publish("will_solve", self.constraints, data, "get_value") data_c = SelectedSolver.instance().get_value(self.constraints, data) self._publish("did_solve", self.constraints, data, "get_value", data_c) - return int(sha3.keccak_256(data_c).hexdigest(), 16) + return int(hashlib.sha3_256(data_c).hexdigest(), 16) @property def PC(self): @@ -3040,7 +3040,7 @@ def block_hash(self, block_number=None, force_recent=True): # We are not maintaining an actual -block-chain- so we just generate # some hashes for each virtual block - value = sha3.keccak_256((repr(block_number) + "NONCE").encode()).hexdigest() + value = hashlib.sha3_256((repr(block_number) + "NONCE").encode()).hexdigest() value = int(value, 16) if force_recent: @@ -3095,7 +3095,7 @@ def calculate_new_address(sender=None, nonce=None): if nonce is None: # assume that the sender is a contract account, which is initialized with a nonce of 1 nonce = 1 - new_address = int(sha3.keccak_256(rlp.encode([sender, nonce])).hexdigest()[24:], 16) + new_address = int(hashlib.sha3_256(rlp.encode([sender, nonce])).hexdigest()[24:], 16) return new_address def execute(self): @@ -3173,7 +3173,7 @@ def create_account(self, address=None, balance=0, code=None, storage=None, nonce # adds hash of new address data = binascii.unhexlify("{:064x}{:064x}".format(address, 0)) - value = sha3.keccak_256(data).hexdigest() + value = hashlib.sha3_256(data).hexdigest() value = int(value, 16) self._publish("on_concrete_sha3", data, value) diff --git a/tests/auto_generators/make_VMTests.py b/tests/auto_generators/make_VMTests.py index 592bb5f5b..2b18a9a78 100644 --- a/tests/auto_generators/make_VMTests.py +++ b/tests/auto_generators/make_VMTests.py @@ -71,7 +71,7 @@ def gen_header(testcases): if any("logs" in testcase for testcase in testcases.values()): body += """ -import sha3 +import hashlib import rlp from rlp.sedes import ( CountableList, @@ -240,7 +240,7 @@ def did_close_transaction_callback(self, state, tx): # check logs logs = [Log(unhexlify('{'{'}:040x{'}'}'.format(l.address)), l.topics, solve(l.memlog)) for l in world.logs] data = rlp.encode(logs) - self.assertEqual(sha3.keccak_256(data).hexdigest(), '{testcase['logs'][2:]}')""" + self.assertEqual(hashlib.sha3_256(data).hexdigest(), '{testcase['logs'][2:]}')""" return body