Skip to content

Commit

Permalink
update from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
enitrat committed Sep 24, 2024
1 parent 9650aca commit d9cae56
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 47 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
4 changes: 2 additions & 2 deletions solidity_contracts/src/EvmPrecompiles/EvmPrecompiles.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
75 changes: 31 additions & 44 deletions tests/end_to_end/EvmPrecompiles/test_evm_precompiles.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,29 +9,33 @@


@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)
y1_value = U256.from_signed(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

Expand All @@ -42,37 +45,41 @@ 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()

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(
Expand All @@ -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(
Expand All @@ -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

0 comments on commit d9cae56

Please sign in to comment.