Skip to content

Commit

Permalink
feat: enable ecadd and ecmul
Browse files Browse the repository at this point in the history
wip"

use diff testin

bump katana

revert starknet py
  • Loading branch information
enitrat committed Sep 17, 2024
1 parent d0f8832 commit b59ea50
Show file tree
Hide file tree
Showing 11 changed files with 246 additions and 204 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ endif

.PHONY: build test coverage clean

# 154615699 corresponds to release v0.1.7 of Kakarot SSJ.
KKRT_SSJ_RELEASE_ID = 154615699
# 173874367 corresponds to release v0.1.12 of Kakarot SSJ.
KKRT_SSJ_RELEASE_ID = 173874367
# 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')
KATANA_VERSION = v1.0.0-alpha.11
KATANA_VERSION = v1.0.0-alpha.12
ROOT_DIR:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))

BUILD_DIR = build
Expand Down
13 changes: 8 additions & 5 deletions kakarot_scripts/utils/starknet.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from starknet_py.net.client_models import Call, DeclareTransactionResponse
from starknet_py.net.full_node_client import _create_broadcasted_txn
from starknet_py.net.models.transaction import DeclareV1
from starknet_py.net.schemas.rpc import DeclareTransactionResponseSchema
from starknet_py.net.schemas.rpc.transactions import DeclareTransactionResponseSchema
from starknet_py.net.signer.stark_curve_signer import KeyPair
from starkware.starknet.public.abi import get_selector_from_name

Expand Down Expand Up @@ -243,6 +243,12 @@ def get_deployments():

@cache
def get_artifact(contract_name):
# Cairo 0 artifacts
artifacts = list(BUILD_DIR.glob(f"*{contract_name}*.json"))
if artifacts:
return Artifact(sierra=None, casm=artifacts[0])

# Cairo 1 artifacts
artifacts = list(CAIRO_DIR.glob(f"**/*{contract_name}.*.json")) or list(
BUILD_DIR_SSJ.glob(f"**/*{contract_name}.*.json")
)
Expand All @@ -255,10 +261,7 @@ def get_artifact(contract_name):
)
return Artifact(sierra=sierra, casm=casm)

artifacts = list(BUILD_DIR.glob(f"**/*{contract_name}*.json"))
if not artifacts:
raise FileNotFoundError(f"No artifact found for {contract_name}")
return Artifact(sierra=None, casm=artifacts[0])
raise FileNotFoundError(f"No artifact found for {contract_name}")


@cache
Expand Down
1 change: 1 addition & 0 deletions lib/forge-std
Submodule forge-std added at 066ff1
165 changes: 85 additions & 80 deletions poetry.lock

Large diffs are not rendered by default.

52 changes: 52 additions & 0 deletions solidity_contracts/src/EvmPrecompiles/EvmPrecompiles.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.0;

/// @title EVM Precompiles Integration
/// @notice Contract for integration testing of EVM precompiles.
/// @dev Implements functions for ECADD and ECMUL precompiles.
contract EvmPrecompiles {
/// @dev Address of the ECADD precompile
address private constant ECADD_PRECOMPILE = address(0x06);
/// @dev Address of the ECMUL precompile
address private constant ECMUL_PRECOMPILE = address(0x07);

/// @dev Gas cost for ECADD operation
uint256 private constant ECADD_GAS = 150;
/// @dev Gas cost for ECMUL operation
uint256 private constant ECMUL_GAS = 6000;

/*//////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/
constructor() {}

/*//////////////////////////////////////////////////////////////
FUNCTIONS FOR PRECOMPILES
//////////////////////////////////////////////////////////////*/
/// @notice Performs elliptic curve addition
/// @param x1 X coordinate of the first point
/// @param y1 Y coordinate of the first point
/// @param x2 X coordinate of the second point
/// @param y2 Y coordinate of the second point
/// @return x X coordinate of the result point
/// @return y Y coordinate of the result point
function ecAdd(uint256 x1, uint256 y1, uint256 x2, uint256 y2) public view returns (uint256 x, uint256 y) {
bytes memory input = abi.encodePacked(x1, y1, x2, y2);
(bool success, bytes memory result) = ECADD_PRECOMPILE.staticcall{gas: ECADD_GAS}(input);
require(success, "ECADD precompile call failed");
return abi.decode(result, (uint256, uint256));
}

/// @notice Performs elliptic curve scalar multiplication
/// @param x1 X coordinate of the point
/// @param y1 Y coordinate of the point
/// @param s Scalar for multiplication
/// @return x X coordinate of the result point
/// @return y Y coordinate of the result point
function ecMul(uint256 x1, uint256 y1, uint256 s) public view returns (uint256 x, uint256 y) {
bytes memory input = abi.encodePacked(x1, y1, s);
(bool success, bytes memory result) = ECMUL_PRECOMPILE.staticcall{gas: ECMUL_GAS}(input);
require(success, "ECMUL precompile call failed");
return abi.decode(result, (uint256, uint256));
}
}
4 changes: 2 additions & 2 deletions src/kakarot/precompiles/precompiles.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ namespace Precompiles {
ret;
call external_precompile; // 0x5
ret;
call not_implemented_precompile; // 0x6
call external_precompile; // 0x6
ret;
call not_implemented_precompile; // 0x7
call external_precompile; // 0x7
ret;
call not_implemented_precompile; // 0x8
ret;
Expand Down
95 changes: 95 additions & 0 deletions tests/end_to_end/EvmPrecompiles/test_evm_precompiles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import pytest
import pytest_asyncio
from ethereum.base_types import U256, Uint
from ethereum.crypto.alt_bn128 import ALT_BN128_PRIME, BNF, BNP
from ethereum.exceptions import OutOfGasError

from kakarot_scripts.utils.kakarot import deploy


@pytest_asyncio.fixture(scope="package")
async def evm_precompiles(owner):
return await deploy(
"EvmPrecompiles",
"EvmPrecompiles",
caller_eoa=owner.starknet_contract,
)


def ref_alt_bn128_add(x0, y0, x1, y1):
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

try:
p0 = BNP(BNF(x0_value), BNF(y0_value))
p1 = BNP(BNF(x1_value), BNF(y1_value))
except ValueError:
raise OutOfGasError from None

p = p0 + p1

x_bytes = p.x.to_be_bytes32()
y_bytes = p.y.to_be_bytes32()

x = Uint(x_bytes)
y = Uint(y_bytes)

return x, y


def ref_alt_bn128_mul(x0, y0, s):
x0_value = U256.from_signed(x0)
y0_value = U256.from_signed(y0)
U256.from_signed(s)

for i in (x0_value, y0_value):
if i >= ALT_BN128_PRIME:
raise OutOfGasError

try:
p0 = BNP(BNF(x0_value), BNF(y0_value))
except ValueError:
raise OutOfGasError from None

p = p0.mul_by(s)

x_bytes = p.x.to_be_bytes32()
y_bytes = p.y.to_be_bytes32()

x = Uint(x_bytes)
y = Uint(y_bytes)

return x, y


@pytest.mark.asyncio(scope="package")
@pytest.mark.EvmPrecompiles
@pytest.mark.xfail(reason="Katana doesn't support new builtins")
class TestEvmPrecompiles:
class TestEcAdd:
async def test_should_return_ec_add_point_at_infinity(self, evm_precompiles):
result = await evm_precompiles.ecAdd(0, 0, 0, 0)
expected = ref_alt_bn128_add(0, 0, 0, 0)
assert result == expected

async def test_should_return_ec_add_with_coordinates(self, evm_precompiles):
result = await evm_precompiles.ecAdd(1, 2, 1, 2)
expected = ref_alt_bn128_add(1, 2, 1, 2)
assert result == expected

class TestEcMul:
async def test_should_return_ec_mul_with_coordinates(self, evm_precompiles):
result = await evm_precompiles.ecMul(1, 2, 2)
expected = ref_alt_bn128_mul(1, 2, 2)
assert result == expected

async def test_should_return_ec_mul_point_at_infinity(self, evm_precompiles):
result = await evm_precompiles.ecMul(0, 0, 0)
expected = ref_alt_bn128_mul(0, 0, 0)
assert result == expected
69 changes: 0 additions & 69 deletions tests/src/kakarot/precompiles/test_ec_add.cairo

This file was deleted.

11 changes: 0 additions & 11 deletions tests/src/kakarot/precompiles/test_ec_add.py

This file was deleted.

8 changes: 0 additions & 8 deletions tests/src/kakarot/precompiles/test_ec_mul.py

This file was deleted.

26 changes: 0 additions & 26 deletions tests/src/kakarot/precompiles/test_ec_recover.cairo

This file was deleted.

0 comments on commit b59ea50

Please sign in to comment.