From c7896d0f23cb752f02d93743152e597d57086426 Mon Sep 17 00:00:00 2001 From: Mathieu <60658558+enitrat@users.noreply.github.com> Date: Fri, 20 Sep 2024 16:00:45 +0200 Subject: [PATCH] feat: disable modexp (#1429) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Time spent on this PR: ## Pull request type Please check the type of change your PR introduces: - [ ] Bugfix - [ ] Feature - [ ] Code style update (formatting, renaming) - [ ] Refactoring (no functional changes, no api changes) - [ ] Build related changes - [ ] Documentation content changes - [ ] Other (please describe): ## What is the current behavior? Resolves #1422 ## What is the new behavior? - Disables the external call to the cairo1 class for modexp operations - Removes the legacy modexp files. - - - - This change is [Reviewable](https://reviewable.io/reviews/kkrt-labs/kakarot/1429) --- blockchain-tests-skip.yml | 272 ++++++++++++++ src/kakarot/precompiles/modexp.cairo | 61 ---- src/kakarot/precompiles/precompiles.cairo | 2 +- src/utils/modexp/modexp_utils.cairo | 338 ------------------ .../src/kakarot/precompiles/test_modexp.cairo | 29 -- tests/src/kakarot/precompiles/test_modexp.py | 32 -- 6 files changed, 273 insertions(+), 461 deletions(-) delete mode 100644 src/kakarot/precompiles/modexp.cairo delete mode 100644 src/utils/modexp/modexp_utils.cairo delete mode 100644 tests/src/kakarot/precompiles/test_modexp.cairo delete mode 100644 tests/src/kakarot/precompiles/test_modexp.py diff --git a/blockchain-tests-skip.yml b/blockchain-tests-skip.yml index 416134393..740befc51 100644 --- a/blockchain-tests-skip.yml +++ b/blockchain-tests-skip.yml @@ -95,6 +95,7 @@ testname: - create2collisionStorageParis_d0g0v0_Cancun - create2collisionStorageParis_d1g0v0_Cancun - create2collisionStorageParis_d2g0v0_Cancun + - create2callPrecompiles_d4g0v0_Cancun stCallCreateCallCodeTest: - Call1024PreCalls_d0g0v0_Cancun #RunResources error - Call1024PreCalls_d0g1v0_Cancun #RunResources error @@ -113,6 +114,23 @@ testname: stEIP150singleCodeGasPrices: - gasCostExp_d7g0v0_Cancun #RunResources error stPreCompiledContracts2: + - modexpRandomInput_d0g0v0_Cancun + - modexpRandomInput_d0g1v0_Cancun + - modexp_0_0_0_20500_d0g1v0_Cancun + - modexp_0_0_0_20500_d0g2v0_Cancun + - modexp_0_0_0_20500_d0g3v0_Cancun + - modexp_0_0_0_22000_d0g0v0_Cancun + - modexp_0_0_0_22000_d0g1v0_Cancun + - modexp_0_0_0_22000_d0g2v0_Cancun + - modexp_0_0_0_22000_d0g3v0_Cancun + - modexp_0_0_0_25000_d0g0v0_Cancun + - modexp_0_0_0_25000_d0g1v0_Cancun + - modexp_0_0_0_25000_d0g2v0_Cancun + - modexp_0_0_0_25000_d0g3v0_Cancun + - modexp_0_0_0_35000_d0g0v0_Cancun + - modexp_0_0_0_35000_d0g1v0_Cancun + - modexp_0_0_0_35000_d0g2v0_Cancun + - modexp_0_0_0_35000_d0g3v0_Cancun - CALLBlake2f_d9g0v0_Cancun #RunResources error - CALLBlake2f_d9g1v0_Cancun #RunResources error - CALLBlake2f_d9g2v0_Cancun #RunResources error @@ -149,6 +167,247 @@ testname: - modexpRandomInput_d2g0v0_Cancun #RunResources error - modexpRandomInput_d2g1v0_Cancun #RunResources error stPreCompiledContracts: + - modexpTests_d0g0v0_Cancun + - modexpTests_d100g0v0_Cancun + - modexpTests_d101g0v0_Cancun + - modexpTests_d102g0v0_Cancun + - modexpTests_d103g0v0_Cancun + - modexpTests_d104g0v0_Cancun + - modexpTests_d105g0v0_Cancun + - modexpTests_d106g0v0_Cancun + - modexpTests_d107g0v0_Cancun + - modexpTests_d108g0v0_Cancun + - modexpTests_d109g0v0_Cancun + - modexpTests_d10g0v0_Cancun + - modexpTests_d110g0v0_Cancun + - modexpTests_d111g0v0_Cancun + - modexpTests_d112g0v0_Cancun + - modexpTests_d113g0v0_Cancun + - modexpTests_d114g0v0_Cancun + - modexpTests_d115g0v0_Cancun + - modexpTests_d116g0v0_Cancun + - modexpTests_d117g0v0_Cancun + - modexpTests_d118g0v0_Cancun + - modexpTests_d119g0v0_Cancun + - modexpTests_d11g0v0_Cancun + - modexpTests_d120g0v0_Cancun + - modexpTests_d121g0v0_Cancun + - modexpTests_d122g0v0_Cancun + - modexpTests_d123g0v0_Cancun + - modexpTests_d124g0v0_Cancun + - modexpTests_d125g0v0_Cancun + - modexpTests_d126g0v0_Cancun + - modexpTests_d12g0v0_Cancun + - modexpTests_d13g0v0_Cancun + - modexpTests_d14g0v0_Cancun + - modexpTests_d15g0v0_Cancun + - modexpTests_d16g0v0_Cancun + - modexpTests_d17g0v0_Cancun + - modexpTests_d18g0v0_Cancun + - modexpTests_d19g0v0_Cancun + - modexpTests_d1g0v0_Cancun + - modexpTests_d20g0v0_Cancun + - modexpTests_d21g0v0_Cancun + - modexpTests_d22g0v0_Cancun + - modexpTests_d23g0v0_Cancun + - modexpTests_d24g0v0_Cancun + - modexpTests_d25g0v0_Cancun + - modexpTests_d26g0v0_Cancun + - modexpTests_d27g0v0_Cancun + - modexpTests_d28g0v0_Cancun + - modexpTests_d29g0v0_Cancun + - modexpTests_d2g0v0_Cancun + - modexpTests_d30g0v0_Cancun + - modexpTests_d31g0v0_Cancun + - modexpTests_d32g0v0_Cancun + - modexpTests_d33g0v0_Cancun + - modexpTests_d34g0v0_Cancun + - modexpTests_d35g0v0_Cancun + - modexpTests_d36g0v0_Cancun + - modexpTests_d37g0v0_Cancun + - modexpTests_d38g0v0_Cancun + - modexpTests_d39g0v0_Cancun + - modexpTests_d3g0v0_Cancun + - modexpTests_d40g0v0_Cancun + - modexpTests_d41g0v0_Cancun + - modexpTests_d42g0v0_Cancun + - modexpTests_d43g0v0_Cancun + - modexpTests_d44g0v0_Cancun + - modexpTests_d45g0v0_Cancun + - modexpTests_d46g0v0_Cancun + - modexpTests_d47g0v0_Cancun + - modexpTests_d48g0v0_Cancun + - modexpTests_d49g0v0_Cancun + - modexpTests_d4g0v0_Cancun + - modexpTests_d50g0v0_Cancun + - modexpTests_d51g0v0_Cancun + - modexpTests_d52g0v0_Cancun + - modexpTests_d53g0v0_Cancun + - modexpTests_d54g0v0_Cancun + - modexpTests_d55g0v0_Cancun + - modexpTests_d56g0v0_Cancun + - modexpTests_d57g0v0_Cancun + - modexpTests_d58g0v0_Cancun + - modexpTests_d59g0v0_Cancun + - modexpTests_d5g0v0_Cancun + - modexpTests_d60g0v0_Cancun + - modexpTests_d61g0v0_Cancun + - modexpTests_d62g0v0_Cancun + - modexpTests_d63g0v0_Cancun + - modexpTests_d64g0v0_Cancun + - modexpTests_d65g0v0_Cancun + - modexpTests_d66g0v0_Cancun + - modexpTests_d67g0v0_Cancun + - modexpTests_d68g0v0_Cancun + - modexpTests_d69g0v0_Cancun + - modexpTests_d6g0v0_Cancun + - modexpTests_d70g0v0_Cancun + - modexpTests_d71g0v0_Cancun + - modexpTests_d72g0v0_Cancun + - modexpTests_d73g0v0_Cancun + - modexpTests_d74g0v0_Cancun + - modexpTests_d75g0v0_Cancun + - modexpTests_d76g0v0_Cancun + - modexpTests_d77g0v0_Cancun + - modexpTests_d78g0v0_Cancun + - modexpTests_d79g0v0_Cancun + - modexpTests_d7g0v0_Cancun + - modexpTests_d80g0v0_Cancun + - modexpTests_d81g0v0_Cancun + - modexpTests_d82g0v0_Cancun + - modexpTests_d83g0v0_Cancun + - modexpTests_d84g0v0_Cancun + - modexpTests_d85g0v0_Cancun + - modexpTests_d86g0v0_Cancun + - modexpTests_d87g0v0_Cancun + - modexpTests_d88g0v0_Cancun + - modexpTests_d89g0v0_Cancun + - modexpTests_d8g0v0_Cancun + - modexpTests_d90g0v0_Cancun + - modexpTests_d91g0v0_Cancun + - modexpTests_d92g0v0_Cancun + - modexpTests_d93g0v0_Cancun + - modexpTests_d94g0v0_Cancun + - modexpTests_d95g0v0_Cancun + - modexpTests_d96g0v0_Cancun + - modexpTests_d97g0v0_Cancun + - modexpTests_d98g0v0_Cancun + - modexpTests_d99g0v0_Cancun + - modexpTests_d9g0v0_Cancun + - modexp_d0g0v0_Cancun + - modexp_d0g1v0_Cancun + - modexp_d0g2v0_Cancun + - modexp_d0g3v0_Cancun + - modexp_d10g0v0_Cancun + - modexp_d10g1v0_Cancun + - modexp_d10g2v0_Cancun + - modexp_d10g3v0_Cancun + - modexp_d12g0v0_Cancun + - modexp_d12g1v0_Cancun + - modexp_d12g2v0_Cancun + - modexp_d12g3v0_Cancun + - modexp_d13g0v0_Cancun + - modexp_d13g1v0_Cancun + - modexp_d13g2v0_Cancun + - modexp_d13g3v0_Cancun + - modexp_d15g0v0_Cancun + - modexp_d15g1v0_Cancun + - modexp_d15g2v0_Cancun + - modexp_d15g3v0_Cancun + - modexp_d18g0v0_Cancun + - modexp_d18g1v0_Cancun + - modexp_d18g2v0_Cancun + - modexp_d18g3v0_Cancun + - modexp_d19g0v0_Cancun + - modexp_d19g1v0_Cancun + - modexp_d19g2v0_Cancun + - modexp_d19g3v0_Cancun + - modexp_d1g0v0_Cancun + - modexp_d1g1v0_Cancun + - modexp_d1g2v0_Cancun + - modexp_d1g3v0_Cancun + - modexp_d20g0v0_Cancun + - modexp_d20g1v0_Cancun + - modexp_d20g2v0_Cancun + - modexp_d20g3v0_Cancun + - modexp_d21g0v0_Cancun + - modexp_d21g1v0_Cancun + - modexp_d21g2v0_Cancun + - modexp_d21g3v0_Cancun + - modexp_d22g0v0_Cancun + - modexp_d22g1v0_Cancun + - modexp_d22g2v0_Cancun + - modexp_d22g3v0_Cancun + - modexp_d23g0v0_Cancun + - modexp_d23g1v0_Cancun + - modexp_d23g2v0_Cancun + - modexp_d23g3v0_Cancun + - modexp_d24g0v0_Cancun + - modexp_d24g1v0_Cancun + - modexp_d24g2v0_Cancun + - modexp_d24g3v0_Cancun + - modexp_d31g0v0_Cancun + - modexp_d31g1v0_Cancun + - modexp_d31g2v0_Cancun + - modexp_d31g3v0_Cancun + - modexp_d32g0v0_Cancun + - modexp_d32g1v0_Cancun + - modexp_d32g2v0_Cancun + - modexp_d32g3v0_Cancun + - modexp_d34g0v0_Cancun + - modexp_d34g1v0_Cancun + - modexp_d34g2v0_Cancun + - modexp_d34g3v0_Cancun + - modexp_d35g0v0_Cancun + - modexp_d35g1v0_Cancun + - modexp_d35g2v0_Cancun + - modexp_d35g3v0_Cancun + - modexp_d3g0v0_Cancun + - modexp_d3g1v0_Cancun + - modexp_d3g2v0_Cancun + - modexp_d3g3v0_Cancun + - modexp_d4g0v0_Cancun + - modexp_d4g1v0_Cancun + - modexp_d4g2v0_Cancun + - modexp_d4g3v0_Cancun + - modexp_d5g0v0_Cancun + - modexp_d5g1v0_Cancun + - modexp_d5g2v0_Cancun + - modexp_d5g3v0_Cancun + - modexp_d6g0v0_Cancun + - modexp_d6g1v0_Cancun + - modexp_d6g2v0_Cancun + - modexp_d6g3v0_Cancun + - modexp_d7g0v0_Cancun + - modexp_d7g1v0_Cancun + - modexp_d7g2v0_Cancun + - modexp_d7g3v0_Cancun + - modexp_d8g0v0_Cancun + - modexp_d8g1v0_Cancun + - modexp_d8g2v0_Cancun + - modexp_d8g3v0_Cancun + - modexp_d9g0v0_Cancun + - modexp_d9g1v0_Cancun + - modexp_d9g2v0_Cancun + - modexp_d9g3v0_Cancun + - precompsEIP2929Cancun_d112g0v0_Cancun + - precompsEIP2929Cancun_d130g0v0_Cancun + - precompsEIP2929Cancun_d148g0v0_Cancun + - precompsEIP2929Cancun_d166g0v0_Cancun + - precompsEIP2929Cancun_d184g0v0_Cancun + - precompsEIP2929Cancun_d202g0v0_Cancun + - precompsEIP2929Cancun_d220g0v0_Cancun + - precompsEIP2929Cancun_d22g0v0_Cancun + - precompsEIP2929Cancun_d238g0v0_Cancun + - precompsEIP2929Cancun_d256g0v0_Cancun + - precompsEIP2929Cancun_d274g0v0_Cancun + - precompsEIP2929Cancun_d292g0v0_Cancun + - precompsEIP2929Cancun_d310g0v0_Cancun + - precompsEIP2929Cancun_d40g0v0_Cancun + - precompsEIP2929Cancun_d58g0v0_Cancun + - precompsEIP2929Cancun_d76g0v0_Cancun + - precompsEIP2929Cancun_d7g0v0_Cancun + - precompsEIP2929Cancun_d94g0v0_Cancun - idPrecomps_d4g0v0_Cancun - idPrecomps_d9g0v0_Cancun - modexp_d11g0v0_Cancun @@ -323,6 +582,14 @@ testname: - randomStatetest64_d0g0v0_Cancun - randomStatetest85_d0g0v0_Cancun stRevertTest: + - RevertPrecompiledTouchExactOOG_Paris_d12g1v0_Cancun + - RevertPrecompiledTouchExactOOG_Paris_d12g2v0_Cancun + - RevertPrecompiledTouchExactOOG_Paris_d20g1v0_Cancun + - RevertPrecompiledTouchExactOOG_Paris_d20g2v0_Cancun + - RevertPrecompiledTouchExactOOG_Paris_d28g1v0_Cancun + - RevertPrecompiledTouchExactOOG_Paris_d28g2v0_Cancun + - RevertPrecompiledTouchExactOOG_Paris_d4g1v0_Cancun + - RevertPrecompiledTouchExactOOG_Paris_d4g2v0_Cancun - RevertInCreateInInit_Paris_d0g0v0_Cancun - RevertPrecompiledTouchExactOOG_Paris_d13g1v0_Cancun - RevertPrecompiledTouchExactOOG_Paris_d13g2v0_Cancun @@ -350,8 +617,13 @@ testname: - InitCollisionParis_d2g0v0_Cancun - InitCollisionParis_d3g0v0_Cancun stReturnDataTest: + - modexp_modsize0_returndatasize_d0g0v0_Cancun + - modexp_modsize0_returndatasize_d1g0v0_Cancun + - modexp_modsize0_returndatasize_d2g0v0_Cancun + - modexp_modsize0_returndatasize_d3g0v0_Cancun - modexp_modsize0_returndatasize_d4g0v0_Cancun #RunResources error stStaticCall: + - static_CallEcrecover0_0input_d5g0v0_Cancun - StaticcallToPrecompileFromCalledContract_d0g0v0_Cancun - StaticcallToPrecompileFromContractInitialization_d0g0v0_Cancun - StaticcallToPrecompileFromTransaction_d0g0v0_Cancun diff --git a/src/kakarot/precompiles/modexp.cairo b/src/kakarot/precompiles/modexp.cairo deleted file mode 100644 index 219f1449a..000000000 --- a/src/kakarot/precompiles/modexp.cairo +++ /dev/null @@ -1,61 +0,0 @@ -// SPDX-License-Identifier: MIT - -%lang starknet - -// Starkware dependencies -from starkware.cairo.common.cairo_builtins import HashBuiltin, BitwiseBuiltin -from starkware.cairo.common.uint256 import Uint256 -from starkware.cairo.common.alloc import alloc - -// Internal dependencies -from utils.utils import Helpers -from utils.modexp.modexp_utils import ModExpHelpersUint256 -from utils.bytes import uint256_to_bytes - -// @title ModExpUint256 MVP Precompile related functions. -// @notice It is an MVP implementation since it only supports uint256 numbers with m_size<=16 and not bigint which requires bigint library in cairo 0.10. -// @author @dragan2234 -// @custom:namespace PrecompileModExpUint256 -namespace PrecompileModExpUint256 { - const PRECOMPILE_ADDRESS = 0x05; - const MOD_EXP_BYTES_LEN = 32; - - // @notice Run the precompile. - // @param input_len The length of input array. - // @param input The input array. - // @return output_len The output length. - // @return output The output array. - // @return gas_used The gas usage of precompile. - func run{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr, - bitwise_ptr: BitwiseBuiltin*, - }(_address: felt, input_len: felt, input: felt*) -> ( - output_len: felt, output: felt*, gas_used: felt, reverted: felt - ) { - alloc_locals; - - let b_size: Uint256 = Helpers.bytes32_to_uint256(input); - let e_size: Uint256 = Helpers.bytes32_to_uint256(input + MOD_EXP_BYTES_LEN); - let m_size: Uint256 = Helpers.bytes32_to_uint256(input + MOD_EXP_BYTES_LEN * 2); - let b: Uint256 = Helpers.bytes_to_uint256(b_size.low, input + MOD_EXP_BYTES_LEN * 3); - let e: Uint256 = Helpers.bytes_to_uint256( - e_size.low, input + MOD_EXP_BYTES_LEN * 3 + b_size.low - ); - let m: Uint256 = Helpers.bytes_to_uint256( - m_size.low, input + MOD_EXP_BYTES_LEN * 3 + b_size.low + e_size.low - ); - with_attr error_message("Kakarot: modexp failed") { - let (result) = ModExpHelpersUint256.uint256_mod_exp(b, e, m); - } - let bytes: felt* = alloc(); - let bytes_len = uint256_to_bytes(bytes, result); - - let (gas_cost) = ModExpHelpersUint256.calculate_mod_exp_gas( - b_size, m_size, e_size, b, e, m - ); - - return (bytes_len, bytes, gas_cost, 0); - } -} diff --git a/src/kakarot/precompiles/precompiles.cairo b/src/kakarot/precompiles/precompiles.cairo index 42c7191ec..7c9652517 100644 --- a/src/kakarot/precompiles/precompiles.cairo +++ b/src/kakarot/precompiles/precompiles.cairo @@ -114,7 +114,7 @@ namespace Precompiles { ret; call PrecompileDataCopy.run; // 0x4 ret; - call external_precompile; // 0x5 + call not_implemented_precompile; // 0x5 ret; call not_implemented_precompile; // 0x6 ret; diff --git a/src/utils/modexp/modexp_utils.cairo b/src/utils/modexp/modexp_utils.cairo deleted file mode 100644 index 3d7e12992..000000000 --- a/src/utils/modexp/modexp_utils.cairo +++ /dev/null @@ -1,338 +0,0 @@ -from starkware.cairo.common.cairo_builtins import BitwiseBuiltin -from starkware.cairo.common.uint256 import Uint256, uint256_mul, uint256_lt -from starkware.cairo.common.bitwise import bitwise_and -from starkware.cairo.common.registers import get_fp_and_pc -from starkware.cairo.common.bool import FALSE - -from utils.uint256 import ( - uint256_sub, - uint256_add, - uint256_unsigned_div_rem, - uint256_mul_div_mod, - uint256_eq, -) - -// @title ModExpHelpersUint256 Functions -// @notice This file contains a selection of helper functions for modular exponentiation and gas cost calculation. -// @author @dragan2234 -// @custom:namespace ModExpHelpersUint256 -namespace ModExpHelpersUint256 { - const GAS_COST_MOD_EXP = 200; - - // @title Modular exponentiation calculation - // @author dragan2234 - // @dev Computes x ** y % p for Uint256 numbers via fast modular exponentiation algorithm. - // Time complexity is log_2(y). - // Loop is implemented via uint256_mod_exp_recursive_call() function. - func uint256_mod_exp{range_check_ptr: felt}(x: Uint256, y: Uint256, p: Uint256) -> ( - remainder: Uint256 - ) { - alloc_locals; - let res = Uint256(low=1, high=0); - let (r_x, r_y, r_res) = uint256_mod_exp_recursive_call(x, y, res, p); - let (quotient, remainder) = uint256_unsigned_div_rem(r_res, p); - return (remainder=remainder); - } - - func uint256_mod_exp_recursive_call{range_check_ptr: felt}( - x: Uint256, y: Uint256, res: Uint256, p: Uint256 - ) -> (r_x: Uint256, r_y: Uint256, r_res: Uint256) { - alloc_locals; - let (is_greater_than_zero) = uint256_lt(Uint256(low=0, high=0), y); - if (is_greater_than_zero == FALSE) { - return (r_x=x, r_y=y, r_res=res); - } - - let (quotient, remainder) = uint256_unsigned_div_rem(y, Uint256(low=2, high=0)); - let (is_equal_to_one) = uint256_eq(remainder, Uint256(low=1, high=0)); - if (is_equal_to_one == FALSE) { - let (x_res_quotient, x_res_quotient_high, x_res_remainder) = uint256_mul_div_mod( - x, x, p - ); - return uint256_mod_exp_recursive_call(x=x_res_remainder, y=quotient, res=res, p=p); - } else { - let ( - x_res_res_quotient, x_res_res_quotient_high, x_res_res_remainder - ) = uint256_mul_div_mod(res, x, p); - let (x_res_quotient, x_res_quotient_high, x_res_remainder) = uint256_mul_div_mod( - x, x, p - ); - return uint256_mod_exp_recursive_call( - x=x_res_remainder, y=quotient, res=x_res_res_remainder, p=p - ); - } - } - - func calculate_mod_exp_gas{range_check_ptr: felt, bitwise_ptr: BitwiseBuiltin*}( - b_size: Uint256, e_size: Uint256, m_size: Uint256, b: Uint256, e: Uint256, m: Uint256 - ) -> (gas_cost: felt) { - alloc_locals; - - let (is_less_than) = uint256_lt(b_size, m_size); - - if (is_less_than == FALSE) { - tempvar max_length = b_size; - } else { - tempvar max_length = m_size; - } - - let (words_step_1, _) = uint256_add(max_length, Uint256(low=7, high=0)); - let (words, _) = uint256_unsigned_div_rem(words_step_1, Uint256(low=8, high=0)); - let (multiplication_complexity, carry) = uint256_mul(words, words); - assert carry = Uint256(0, 0); - - let (is_less_than_33) = uint256_lt(e_size, Uint256(low=33, high=0)); - if (is_less_than_33 == FALSE) { - let sub_step: Uint256 = uint256_sub(e_size, Uint256(low=32, high=0)); - - let (local result, local carry) = uint256_mul(Uint256(low=8, high=0), sub_step); - assert carry = Uint256(low=0, high=0); - - let (bitwise_high) = bitwise_and(e.high, 2 ** 128 - 1); - let (bitwise_low) = bitwise_and(e.low, 2 ** 128 - 1); - let e_bit_length = get_u256_bitlength(Uint256(low=bitwise_low, high=bitwise_high)); - - let e_bit_length_uint256 = Uint256(low=e_bit_length, high=0); - let (subtracted_e_bit_length) = uint256_sub( - e_bit_length_uint256, Uint256(low=1, high=0) - ); - - let (addition, _) = uint256_add(result, subtracted_e_bit_length); - - tempvar iteration_count_res = addition; - tempvar range_check_ptr = range_check_ptr; - tempvar bitwise_ptr = bitwise_ptr; - } else { - let (is_zero) = uint256_eq(e, Uint256(low=0, high=0)); - if (is_zero == FALSE) { - let u256_l = get_u256_bitlength(e); - let inner_step = u256_l - 1; - tempvar iteration_count = Uint256(low=inner_step, high=0); - tempvar range_check_ptr = range_check_ptr; - tempvar bitwise_ptr = bitwise_ptr; - } else { - tempvar iteration_count = Uint256(low=0, high=0); - tempvar range_check_ptr = range_check_ptr; - tempvar bitwise_ptr = bitwise_ptr; - } - tempvar iteration_count_res = iteration_count; - tempvar range_check_ptr = range_check_ptr; - tempvar bitwise_ptr = bitwise_ptr; - } - tempvar bitwise_ptr = bitwise_ptr; - let another_var = iteration_count_res; - let (mci, carry) = uint256_mul(multiplication_complexity, another_var); - assert carry = Uint256(low=0, high=0); - - let (division_mci, _) = uint256_unsigned_div_rem(mci, Uint256(low=3, high=0)); - - let (gas_is_greater_than) = uint256_lt(Uint256(low=200, high=0), division_mci); - - if (gas_is_greater_than == FALSE) { - tempvar gas_cost = Uint256(low=GAS_COST_MOD_EXP, high=0); - } else { - tempvar gas_cost = division_mci; - } - let res = gas_cost.low; - return (gas_cost=res); - } - // @author feltroidprime - // Returns the number of bits in x. - // Params: - // - x: felt - Input value. - // Assumptions for the caller: - // - 0 <= x < 2^128 - // Returns: - // - bit_length: felt - Number of bits in x. - func get_felt_bitlength{range_check_ptr}(x: felt) -> felt { - if (x == 0) { - return 0; - } - alloc_locals; - local bit_length; - - %{ - x = ids.x - ids.bit_length = x.bit_length() - %} - if (bit_length == 128) { - assert [range_check_ptr] = x - 2 ** 127; - tempvar range_check_ptr = range_check_ptr + 1; - return bit_length; - } else { - // Computes N=2^bit_length and n=2^(bit_length-1) - // x is supposed to verify n = 2^(b-1) <= x < N = 2^bit_length <=> x has bit_length bits - let N = pow2(bit_length); - let n = pow2(bit_length - 1); - assert [range_check_ptr] = bit_length; - assert [range_check_ptr + 1] = 128 - bit_length; - assert [range_check_ptr + 2] = N - x - 1; - assert [range_check_ptr + 3] = x - n; - tempvar range_check_ptr = range_check_ptr + 4; - return bit_length; - } - } - // @author feltroidprime - // Returns 2**i for i in [0, 128] - // Assumptions: i is in [0, 128]. - func pow2(i: felt) -> felt { - let (_, pc) = get_fp_and_pc(); - - pc_label: - let data = pc + (powers - pc_label); - - let res = [data + i]; - - return res; - - powers: - dw 0x1; - dw 0x2; - dw 0x4; - dw 0x8; - dw 0x10; - dw 0x20; - dw 0x40; - dw 0x80; - dw 0x100; - dw 0x200; - dw 0x400; - dw 0x800; - dw 0x1000; - dw 0x2000; - dw 0x4000; - dw 0x8000; - dw 0x10000; - dw 0x20000; - dw 0x40000; - dw 0x80000; - dw 0x100000; - dw 0x200000; - dw 0x400000; - dw 0x800000; - dw 0x1000000; - dw 0x2000000; - dw 0x4000000; - dw 0x8000000; - dw 0x10000000; - dw 0x20000000; - dw 0x40000000; - dw 0x80000000; - dw 0x100000000; - dw 0x200000000; - dw 0x400000000; - dw 0x800000000; - dw 0x1000000000; - dw 0x2000000000; - dw 0x4000000000; - dw 0x8000000000; - dw 0x10000000000; - dw 0x20000000000; - dw 0x40000000000; - dw 0x80000000000; - dw 0x100000000000; - dw 0x200000000000; - dw 0x400000000000; - dw 0x800000000000; - dw 0x1000000000000; - dw 0x2000000000000; - dw 0x4000000000000; - dw 0x8000000000000; - dw 0x10000000000000; - dw 0x20000000000000; - dw 0x40000000000000; - dw 0x80000000000000; - dw 0x100000000000000; - dw 0x200000000000000; - dw 0x400000000000000; - dw 0x800000000000000; - dw 0x1000000000000000; - dw 0x2000000000000000; - dw 0x4000000000000000; - dw 0x8000000000000000; - dw 0x10000000000000000; - dw 0x20000000000000000; - dw 0x40000000000000000; - dw 0x80000000000000000; - dw 0x100000000000000000; - dw 0x200000000000000000; - dw 0x400000000000000000; - dw 0x800000000000000000; - dw 0x1000000000000000000; - dw 0x2000000000000000000; - dw 0x4000000000000000000; - dw 0x8000000000000000000; - dw 0x10000000000000000000; - dw 0x20000000000000000000; - dw 0x40000000000000000000; - dw 0x80000000000000000000; - dw 0x100000000000000000000; - dw 0x200000000000000000000; - dw 0x400000000000000000000; - dw 0x800000000000000000000; - dw 0x1000000000000000000000; - dw 0x2000000000000000000000; - dw 0x4000000000000000000000; - dw 0x8000000000000000000000; - dw 0x10000000000000000000000; - dw 0x20000000000000000000000; - dw 0x40000000000000000000000; - dw 0x80000000000000000000000; - dw 0x100000000000000000000000; - dw 0x200000000000000000000000; - dw 0x400000000000000000000000; - dw 0x800000000000000000000000; - dw 0x1000000000000000000000000; - dw 0x2000000000000000000000000; - dw 0x4000000000000000000000000; - dw 0x8000000000000000000000000; - dw 0x10000000000000000000000000; - dw 0x20000000000000000000000000; - dw 0x40000000000000000000000000; - dw 0x80000000000000000000000000; - dw 0x100000000000000000000000000; - dw 0x200000000000000000000000000; - dw 0x400000000000000000000000000; - dw 0x800000000000000000000000000; - dw 0x1000000000000000000000000000; - dw 0x2000000000000000000000000000; - dw 0x4000000000000000000000000000; - dw 0x8000000000000000000000000000; - dw 0x10000000000000000000000000000; - dw 0x20000000000000000000000000000; - dw 0x40000000000000000000000000000; - dw 0x80000000000000000000000000000; - dw 0x100000000000000000000000000000; - dw 0x200000000000000000000000000000; - dw 0x400000000000000000000000000000; - dw 0x800000000000000000000000000000; - dw 0x1000000000000000000000000000000; - dw 0x2000000000000000000000000000000; - dw 0x4000000000000000000000000000000; - dw 0x8000000000000000000000000000000; - dw 0x10000000000000000000000000000000; - dw 0x20000000000000000000000000000000; - dw 0x40000000000000000000000000000000; - dw 0x80000000000000000000000000000000; - dw 0x100000000000000000000000000000000; - } - // @credits feltroidprime - // Returns the total number of bits in the uint256 number. - // Assumptions : - // - 0 <= x < 2^256 - // Returns: - // - nbits: felt - Total number of bits in the uint256 number. - func get_u256_bitlength{range_check_ptr}(x: Uint256) -> felt { - if (x.high != 0) { - let x_bit_high = get_felt_bitlength(x.high); - return 128 + x_bit_high; - } else { - if (x.low != 0) { - let x_bit_low = get_felt_bitlength(x.low); - return x_bit_low; - } else { - return 0; - } - } - } -} diff --git a/tests/src/kakarot/precompiles/test_modexp.cairo b/tests/src/kakarot/precompiles/test_modexp.cairo deleted file mode 100644 index f9e257d36..000000000 --- a/tests/src/kakarot/precompiles/test_modexp.cairo +++ /dev/null @@ -1,29 +0,0 @@ -%lang starknet - -from starkware.cairo.common.alloc import alloc -from starkware.cairo.common.cairo_builtins import HashBuiltin, BitwiseBuiltin -from starkware.cairo.common.math import split_felt - -from utils.utils import Helpers -from kakarot.precompiles.modexp import PrecompileModExpUint256 - -func test__modexp_impl{ - syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr, bitwise_ptr: BitwiseBuiltin* -}(output_ptr: felt*) { - alloc_locals; - local data_len: felt; - let (data: felt*) = alloc(); - %{ - ids.data_len = len(program_input["data"]); - segments.write_arg(ids.data, program_input["data"]); - %} - - let (output_len, output, gas_used, reverted) = PrecompileModExpUint256.run( - PrecompileModExpUint256.PRECOMPILE_ADDRESS, data_len, data - ); - - let result = Helpers.bytes_to_felt(output_len, output); - assert [output_ptr] = result; - assert [output_ptr + 1] = gas_used; - return (); -} diff --git a/tests/src/kakarot/precompiles/test_modexp.py b/tests/src/kakarot/precompiles/test_modexp.py deleted file mode 100644 index d64652ab2..000000000 --- a/tests/src/kakarot/precompiles/test_modexp.py +++ /dev/null @@ -1,32 +0,0 @@ -import math - -import pytest - - -@pytest.mark.MOD_EXP -class TestModExp: - @pytest.mark.slow - def test_modexp(self, cairo_run): - b = 3 - b_size = math.ceil(math.log(b, 256)) - b_size_bytes = b_size.to_bytes(32, "big") - b_bytes = b.to_bytes(b_size, "big") - - e = 2**256 - 2**32 - 978 - e_size = math.ceil(math.log(e, 256)) - e_size_bytes = e_size.to_bytes(32, "big") - e_bytes = e.to_bytes(e_size, "big") - - m = 2**256 - 2**32 - 977 - m_size = math.ceil(math.log(m, 256)) - m_size_bytes = m_size.to_bytes(32, "big") - m_bytes = m.to_bytes(m_size, "big") - - bytes_array = list( - b_size_bytes + e_size_bytes + m_size_bytes + b_bytes + e_bytes + m_bytes - ) - expected_result = pow(b, e, m) - - cairo_result, gas_cost = cairo_run("test__modexp_impl", data=bytes_array) - assert expected_result == cairo_result - assert 1360 == gas_cost