Skip to content

Commit

Permalink
Add evmmax unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
rodiazet committed Dec 5, 2023
1 parent 59ff113 commit b4c0208
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ target_sources(
evm_benchmark_test.cpp
evmmax_bn254_add_test.cpp
evmmax_bn254_mul_test.cpp
evmmax_execution_tests.cpp
evmmax_test.cpp
evmmax_secp256k1_test.cpp
evmone_test.cpp
Expand Down
129 changes: 129 additions & 0 deletions test/unittests/evmmax_execution_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// evmone: Fast Ethereum Virtual Machine implementation
// Copyright 2019-2020 The evmone Authors.
// SPDX-License-Identifier: Apache-2.0

/// This file contains EVMMAX execution tests.

#include "evm_fixture.hpp"

using namespace evmc::literals;
using evmone::test::evm;
using namespace intx;

TEST_P(evm, evmmax_smoke_test)
{
if (is_advanced())
return;

rev = EVMC_PRAGUE; /// TODO: Use EVMC_EVMMAX
// Modulus == 7
auto code = mstore(0, 0x07);
// 3 values slots
// Modulus size in bytes
// Modulus offset in EVM memory
// Modulus ID
code += setupx(3, 32, 0, 1);
// value 3
code += mstore(32, 0x03);
// value 6
code += mstore(64, 0x06);
// num values
// values offset
// store values
code += storex(2, 32, 0);
// ADDMODX for values in slots 0 and 1 save result in slot 2
code += addmodx(2, 1, 0);
// MULMODX for values in slots 1 and 2 save result in slot 2
code += mulmodx(2, 2, 1);
// SUBMODX for values in slots 1 and 2 save result in slot 2
code += submodx(2, 2, 1);
// load values from slot 2 into EVM memory
code += loadx(1, 2, 96);
// return loaded result
code += ret(96, 32);

execute(1000, code);
EXPECT_EQ(result.status_code, EVMC_SUCCESS);
EXPECT_OUTPUT_INT(6);
}

TEST_P(evm, evmmax_smoke_test_1byte_modulus)
{
if (is_advanced())
return;

rev = EVMC_PRAGUE; /// TODO: Use EVMC_EVMMAX
// Modulus == 7
auto code = mstore8(0, 0x07);
// 3 values slots
// Modulus size in bytes
// Modulus offset in EVM memory
// Modulus ID
code += setupx(3, 1, 0, 1);
// value 3
code += mstore8(8, 0x03);
// value 6
code += mstore8(16, 0x06);
// num values
// values offset
// store values
code += storex(2, 1, 0);
// ADDMODX for values in slots 0 and 1 save result in slot 2
code += addmodx(2, 1, 0);
// MULMODX for values in slots 1 and 2 save result in slot 2
code += mulmodx(2, 2, 1);
// SUBMODX for values in slots 1 and 2 save result in slot 2
code += submodx(2, 2, 1);
// load values from slot 2 into EVM memory
code += loadx(1, 2, 17);
// return loaded result
code += ret(17, 8);

execute(1000, code);
EXPECT_EQ(result.status_code, EVMC_SUCCESS);

ASSERT_EQ(result.output_size, 8);
EXPECT_EQ(hex({result.output_data, result.output_size}), "0000000000000006");
}

TEST_P(evm, evmmax_smoke_test_2byte_modulus)
{
if (is_advanced())
return;

rev = EVMC_PRAGUE; /// TODO: Use EVMC_EVMMAX
// Modulus == 263 (0x0107)
auto code = mstore8(0, 0x01);
code += mstore8(1, 0x07);
// 3 values slots
// Modulus size in bytes
// Modulus offset in EVM memory
// Modulus ID
code += setupx(3, 2, 0, 1);
// value 258
code += mstore8(8, 0x01);
code += mstore8(9, 0x02);
// value 254
code += mstore8(16, 0x00);
code += mstore8(17, 0xfe);
// num values
// values offset
// store values
code += storex(2, 2, 0);
// ADDMODX for values in slots 0 and 1 save result in slot 2
code += addmodx(2, 1, 0); // 258 + 254 = 249 mod 263
// MULMODX for values in slots 1 and 2 save result in slot 2
code += mulmodx(2, 2, 1); // 249 * 254 = 126 mod 263
// SUBMODX for values in slots 1 and 2 save result in slot 2
code += submodx(2, 2, 1); // 126 - 254 = 135 mod 263
// load values from slot 2 into EVM memory
code += loadx(1, 2, 18);
// return loaded result
code += ret(18, 8);

execute(1000, code);
EXPECT_EQ(result.status_code, EVMC_SUCCESS);

ASSERT_EQ(result.output_size, 8);
EXPECT_EQ(hex({result.output_data, result.output_size}), "0000000000000087");
}

0 comments on commit b4c0208

Please sign in to comment.