From d9cae56d9da7d3c66d0f333ac393070fe641d6a6 Mon Sep 17 00:00:00 2001 From: enitrat Date: Tue, 24 Sep 2024 10:26:59 +0200 Subject: [PATCH] update from code review --- Makefile | 2 +- .../src/EvmPrecompiles/EvmPrecompiles.sol | 4 +- .../EvmPrecompiles/test_evm_precompiles.py | 75 ++++++++----------- 3 files changed, 34 insertions(+), 47 deletions(-) diff --git a/Makefile b/Makefile index a9edfa766..f29626580 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ endif .PHONY: build test coverage clean -# 173874367 corresponds to release v0.1.13 of Kakarot SSJ. +# 176384150 corresponds to release v0.1.13 of Kakarot SSJ. KKRT_SSJ_RELEASE_ID = 176384150 # Kakarot SSJ artifacts for precompiles. KKRT_SSJ_BUILD_ARTIFACT_URL = $(shell curl -L https://api.github.com/repos/kkrt-labs/kakarot-ssj/releases/${KKRT_SSJ_RELEASE_ID} | jq -r '.assets[0].browser_download_url') diff --git a/solidity_contracts/src/EvmPrecompiles/EvmPrecompiles.sol b/solidity_contracts/src/EvmPrecompiles/EvmPrecompiles.sol index 5fc94c9a2..1bd48ffa8 100644 --- a/solidity_contracts/src/EvmPrecompiles/EvmPrecompiles.sol +++ b/solidity_contracts/src/EvmPrecompiles/EvmPrecompiles.sol @@ -11,9 +11,9 @@ contract EvmPrecompiles { address private constant ECMUL_PRECOMPILE = address(0x07); /// @dev Gas cost for ECADD call is 150 - uint256 private constant ECADD_GAS = 1000000; + uint256 private constant ECADD_GAS = 150; /// @dev Gas cost for ECMUL call is 6000 - uint256 private constant ECMUL_GAS = 1000000; + uint256 private constant ECMUL_GAS = 6000; /*////////////////////////////////////////////////////////////// FUNCTIONS FOR PRECOMPILES diff --git a/tests/end_to_end/EvmPrecompiles/test_evm_precompiles.py b/tests/end_to_end/EvmPrecompiles/test_evm_precompiles.py index 1619c65db..b7c945259 100644 --- a/tests/end_to_end/EvmPrecompiles/test_evm_precompiles.py +++ b/tests/end_to_end/EvmPrecompiles/test_evm_precompiles.py @@ -1,7 +1,6 @@ import pytest import pytest_asyncio from ethereum.base_types import U256, Uint -from ethereum.cancun.vm.exceptions import OutOfGasError from ethereum.crypto.alt_bn128 import ALT_BN128_PRIME, BNF, BNP from hypothesis import given, settings from hypothesis.strategies import integers @@ -10,15 +9,19 @@ @pytest_asyncio.fixture(scope="package") -async def evm_precompiles(owner): +async def evm_precompiles(): return await deploy( "EvmPrecompiles", "EvmPrecompiles", - caller_eoa=owner.starknet_contract, ) def ref_alt_bn128_add(x0, y0, x1, y1): + """ + # ruff: noqa: D401 + Reference implementation of the alt_bn128_add precompile. + Source: https://github.com/ethereum/execution-specs/blob/07f5747a43d62ef7f203d41d77005cb15ca5e434/src/ethereum/cancun/vm/precompiled_contracts/alt_bn128.py#L32-L103. + """ x0_value = U256.from_signed(x0) y0_value = U256.from_signed(y0) x1_value = U256.from_signed(x1) @@ -26,13 +29,13 @@ def ref_alt_bn128_add(x0, y0, x1, y1): for i in (x0_value, y0_value, x1_value, y1_value): if i >= ALT_BN128_PRIME: - raise OutOfGasError + return [False, 0, 0] try: p0 = BNP(BNF(x0_value), BNF(y0_value)) p1 = BNP(BNF(x1_value), BNF(y1_value)) except ValueError: - raise OutOfGasError from None + return [False, 0, 0] p = p0 + p1 @@ -42,24 +45,29 @@ def ref_alt_bn128_add(x0, y0, x1, y1): x = Uint(int.from_bytes(x_bytes, "big")) y = Uint(int.from_bytes(y_bytes, "big")) - return [x, y] + return [True, x, y] def ref_alt_bn128_mul(x0, y0, s): + """ + # ruff: noqa: D401 + Reference implementation of the alt_bn128_mul precompile. + Source: https://github.com/ethereum/execution-specs/blob/07f5747a43d62ef7f203d41d77005cb15ca5e434/src/ethereum/cancun/vm/precompiled_contracts/alt_bn128.py#L32-L103. + """ x0_value = U256.from_signed(x0) y0_value = U256.from_signed(y0) - U256.from_signed(s) + s_value = U256.from_signed(s) for i in (x0_value, y0_value): if i >= ALT_BN128_PRIME: - raise OutOfGasError + return [False, 0, 0] try: p0 = BNP(BNF(x0_value), BNF(y0_value)) except ValueError: - raise OutOfGasError from None + return [False, 0, 0] - p = p0.mul_by(s) + p = p0.mul_by(s_value) x_bytes = p.x.to_be_bytes32() y_bytes = p.y.to_be_bytes32() @@ -67,12 +75,11 @@ def ref_alt_bn128_mul(x0, y0, s): x = Uint(int.from_bytes(x_bytes, "big")) y = Uint(int.from_bytes(y_bytes, "big")) - return [x, y] + return [True, x, y] @pytest.mark.asyncio(scope="package") @pytest.mark.EvmPrecompiles -# @pytest.mark.xfail(reason="Katana doesn't support new builtins") class TestEvmPrecompiles: class TestEcAdd: @given( @@ -85,24 +92,14 @@ class TestEcAdd: async def test_should_return_ec_add_with_coordinates( self, evm_precompiles, x0, y0, x1, y1 ): - try: - expected = ref_alt_bn128_add(x0, y0, x1, y1) - result = await evm_precompiles.ecAdd(x0, y0, x1, y1) - assert result[0] is True - assert result[1:] == expected - except OutOfGasError: - result = await evm_precompiles.ecAdd(x0, y0, x1, y1) - assert result[0] is False + expected = ref_alt_bn128_add(x0, y0, x1, y1) + result = await evm_precompiles.ecAdd(x0, y0, x1, y1) + assert result == expected async def test_should_return_ec_add_point_at_infinity(self, evm_precompiles): - try: - expected = ref_alt_bn128_add(0, 0, 0, 0) - result = await evm_precompiles.ecAdd(0, 0, 0, 0) - assert result[0] is True - assert result[1:] == expected - except OutOfGasError: - result = await evm_precompiles.ecAdd(0, 0, 0, 0) - assert result[0] is False + expected = ref_alt_bn128_add(0, 0, 0, 0) + result = await evm_precompiles.ecAdd(0, 0, 0, 0) + assert result == expected class TestEcMul: @given( @@ -114,21 +111,11 @@ class TestEcMul: async def test_should_return_ec_mul_with_coordinates( self, evm_precompiles, x0, y0, s ): - try: - expected = ref_alt_bn128_mul(x0, y0, s) - result = await evm_precompiles.ecMul(x0, y0, s) - assert result[0] is True - assert result[1:] == expected - except OutOfGasError: - result = await evm_precompiles.ecMul(x0, y0, s) - assert result[0] is False + expected = ref_alt_bn128_mul(x0, y0, s) + result = await evm_precompiles.ecMul(x0, y0, s) + assert result == expected async def test_should_return_ec_mul_point_at_infinity(self, evm_precompiles): - try: - expected = ref_alt_bn128_mul(0, 0, 0) - result = await evm_precompiles.ecMul(0, 0, 0) - assert result[0] is True - assert result[1:] == expected - except OutOfGasError: - result = await evm_precompiles.ecMul(0, 0, 0) - assert result[0] is False + expected = ref_alt_bn128_mul(0, 0, 0) + result = await evm_precompiles.ecMul(0, 0, 0) + assert result == expected