From 37be1b3ed93701e040a9373569f60111632831bf Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Fri, 4 Dec 2020 18:39:42 +0000 Subject: [PATCH 1/2] Remove dependency on util::160 in yulInterpreter Instead apply explicit truncation or masking for readability. --- .../yulInterpreter/EVMInstructionInterpreter.cpp | 15 ++++++++------- .../yulInterpreter/EwasmBuiltinInterpreter.cpp | 2 +- test/tools/yulInterpreter/Interpreter.h | 8 ++++---- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/test/tools/yulInterpreter/EVMInstructionInterpreter.cpp b/test/tools/yulInterpreter/EVMInstructionInterpreter.cpp index fe647cec4..1bb5d1147 100644 --- a/test/tools/yulInterpreter/EVMInstructionInterpreter.cpp +++ b/test/tools/yulInterpreter/EVMInstructionInterpreter.cpp @@ -35,6 +35,7 @@ using namespace solidity; using namespace solidity::yul; using namespace solidity::yul::test; +using solidity::util::h160; using solidity::util::h256; using solidity::util::keccak256; @@ -181,18 +182,18 @@ u256 EVMInstructionInterpreter::eval( return u256(keccak256(readMemory(offset, size))); } case Instruction::ADDRESS: - return m_state.address; + return h256(m_state.address, h256::AlignRight); case Instruction::BALANCE: - if (arg[0] == m_state.address) + if (arg[0] == h256(m_state.address, h256::AlignRight)) return m_state.selfbalance; else return m_state.balance; case Instruction::SELFBALANCE: return m_state.selfbalance; case Instruction::ORIGIN: - return m_state.origin; + return h256(m_state.origin, h256::AlignRight); case Instruction::CALLER: - return m_state.caller; + return h256(m_state.caller, h256::AlignRight); case Instruction::CALLVALUE: return m_state.callvalue; case Instruction::CALLDATALOAD: @@ -248,7 +249,7 @@ u256 EVMInstructionInterpreter::eval( else return 0xaaaaaaaa + (arg[0] - m_state.blockNumber - 256); case Instruction::COINBASE: - return m_state.coinbase; + return h256(m_state.coinbase, h256::AlignRight); case Instruction::TIMESTAMP: return m_state.timestamp; case Instruction::NUMBER: @@ -304,11 +305,11 @@ u256 EVMInstructionInterpreter::eval( case Instruction::CREATE: accessMemory(arg[1], arg[2]); logTrace(_instruction, arg); - return u160(0xcccccc + arg[1]); + return (0xcccccc + arg[1]) & u256("0xffffffffffffffffffffffffffffffffffffffff"); case Instruction::CREATE2: accessMemory(arg[2], arg[3]); logTrace(_instruction, arg); - return u160(0xdddddd + arg[1]); + return (0xdddddd + arg[1]) & u256("0xffffffffffffffffffffffffffffffffffffffff"); case Instruction::CALL: case Instruction::CALLCODE: // TODO assign returndata diff --git a/test/tools/yulInterpreter/EwasmBuiltinInterpreter.cpp b/test/tools/yulInterpreter/EwasmBuiltinInterpreter.cpp index 84f16cd2a..17921ab03 100644 --- a/test/tools/yulInterpreter/EwasmBuiltinInterpreter.cpp +++ b/test/tools/yulInterpreter/EwasmBuiltinInterpreter.cpp @@ -410,7 +410,7 @@ u256 EwasmBuiltinInterpreter::evalEthBuiltin(string const& _fun, vector storage; - u160 address = 0x11111111; + util::h160 address = util::h160("0x0000000000000000000000000000000011111111"); u256 balance = 0x22222222; u256 selfbalance = 0x22223333; - u160 origin = 0x33333333; - u160 caller = 0x44444444; + util::h160 origin = util::h160("0x0000000000000000000000000000000033333333"); + util::h160 caller = util::h160("0x0000000000000000000000000000000044444444"); u256 callvalue = 0x55555555; /// Deployed code bytes code = util::asBytes("codecodecodecodecode"); u256 gasprice = 0x66666666; - u160 coinbase = 0x77777777; + util::h160 coinbase = util::h160("0x0000000000000000000000000000000077777777"); u256 timestamp = 0x88888888; u256 blockNumber = 1024; u256 difficulty = 0x9999999; From ac192ff4c14fc383e5428eef46bd719e4a6a80fe Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Fri, 4 Dec 2020 18:59:36 +0000 Subject: [PATCH 2/2] Drop the util::u160 type --- libsolutil/Common.h | 1 - libsolutil/CommonData.h | 3 +-- test/libsolutil/FixedHash.cpp | 9 --------- test/libsolutil/StringUtils.cpp | 7 ------- 4 files changed, 1 insertion(+), 19 deletions(-) diff --git a/libsolutil/Common.h b/libsolutil/Common.h index 1945f2074..75de12747 100644 --- a/libsolutil/Common.h +++ b/libsolutil/Common.h @@ -65,7 +65,6 @@ using bytesConstRef = util::vector_ref; using bigint = boost::multiprecision::number>; using u256 = boost::multiprecision::number>; using s256 = boost::multiprecision::number>; -using u160 = boost::multiprecision::number>; // Map types. using StringMap = std::map; diff --git a/libsolutil/CommonData.h b/libsolutil/CommonData.h index ec858777e..d5572d6f8 100644 --- a/libsolutil/CommonData.h +++ b/libsolutil/CommonData.h @@ -373,7 +373,7 @@ inline void toBigEndian(T _val, Out& o_out) /// Converts a big-endian byte-stream represented on a templated collection to a templated integer value. /// @a In will typically be either std::string or bytes. -/// @a T will typically by unsigned, u160, u256 or bigint. +/// @a T will typically by unsigned, u256 or bigint. template inline T fromBigEndian(In const& _bytes) { @@ -383,7 +383,6 @@ inline T fromBigEndian(In const& _bytes) return ret; } inline bytes toBigEndian(u256 _val) { bytes ret(32); toBigEndian(_val, ret); return ret; } -inline bytes toBigEndian(u160 _val) { bytes ret(20); toBigEndian(_val, ret); return ret; } /// Convenience function for toBigEndian. /// @returns a byte array just big enough to represent @a _val. diff --git a/test/libsolutil/FixedHash.cpp b/test/libsolutil/FixedHash.cpp index 4efe0a9e1..67397e9e1 100644 --- a/test/libsolutil/FixedHash.cpp +++ b/test/libsolutil/FixedHash.cpp @@ -173,12 +173,6 @@ BOOST_AUTO_TEST_CASE(converting_constructor) BOOST_AUTO_TEST_CASE(arith_constructor) { - FixedHash<20> a(u160(0x1234)); - BOOST_CHECK_EQUAL( - a.hex(), - "0000000000000000000000000000000000001234" - ); - FixedHash<32> b(u256(0x12340000)); BOOST_CHECK_EQUAL( b.hex(), @@ -190,9 +184,6 @@ BOOST_AUTO_TEST_CASE(arith_constructor) BOOST_AUTO_TEST_CASE(to_arith) { - FixedHash<20> a{}; - BOOST_CHECK_EQUAL(u160(a), 0); - FixedHash<32> b("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"); BOOST_CHECK_EQUAL(u256(b), u256("0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470")); } diff --git a/test/libsolutil/StringUtils.cpp b/test/libsolutil/StringUtils.cpp index f678c0da7..0ef61cb0a 100644 --- a/test/libsolutil/StringUtils.cpp +++ b/test/libsolutil/StringUtils.cpp @@ -125,12 +125,6 @@ BOOST_AUTO_TEST_CASE(test_format_number_readable) BOOST_CHECK_EQUAL(formatNumberReadable(u256(0x100000000)), "2**32"); BOOST_CHECK_EQUAL(formatNumberReadable(u256(0xFFFFffff)), "2**32 - 1"); - u160 a = 0; - for (int i = 0; i < 20; i++) - { - a <<= 8; - a |= 0x55; - } u256 b = 0; for (int i = 0; i < 32; i++) { @@ -144,7 +138,6 @@ BOOST_AUTO_TEST_CASE(test_format_number_readable) u256(0xFFFFffffFFFFffff) << 128 | u256(0xFFFFffffFFFFffff) << 64 | u256(0xFFFFffffFFFFffff); - BOOST_CHECK_EQUAL(formatNumberReadable(a, true), "0x5555...{+32 more}...5555"); BOOST_CHECK_EQUAL(formatNumberReadable(b, true), "0x5555...{+56 more}...5555"); BOOST_CHECK_EQUAL(formatNumberReadable(c, true), "0xABCD...{+56 more}...6789"); BOOST_CHECK_EQUAL(formatNumberReadable(d, true), "0xAAAAaaaaAAAAaaab * 2**192 - 1");