-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip" use diff testin bump katana revert starknet py
- Loading branch information
Showing
11 changed files
with
246 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule forge-std
added at
066ff1
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.