diff --git a/.tool-versions b/.tool-versions index 30efda798..aac540e2d 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1,2 +1,2 @@ -scarb 2.7.1 +scarb 2.8.2 starknet-foundry 0.30.0 diff --git a/Scarb.toml b/Scarb.toml index 7a604e5e3..b0d420965 100644 --- a/Scarb.toml +++ b/Scarb.toml @@ -4,14 +4,14 @@ members = ["crates/*"] [workspace.package] description = "Kakarot is an (zk)-Ethereum Virtual Machine implementation written in Cairo." documentation = "https://www.kakarot.org/" -cairo-version = "2.7.1" +cairo-version = "2.8.2" version = "0.1.0" readme = "README.md" repository = "https://github.com/kkrt-labs/kakarot-ssj/" license-file = "LICENSE" [workspace.dependencies] -starknet = "2.7.1" +starknet = "2.8.2" [workspace.tool.fmt] sort-module-level-items = true diff --git a/crates/alexandria_data_structures/Scarb.toml b/crates/alexandria_data_structures/Scarb.toml index 4f8f3c192..0c66cfebb 100644 --- a/crates/alexandria_data_structures/Scarb.toml +++ b/crates/alexandria_data_structures/Scarb.toml @@ -8,6 +8,7 @@ version = "0.1.0" [dev-dependencies] snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry.git", tag = "v0.30.0" } +assert_macros = "2.8.2" [scripts] test = "snforge test --max-n-steps 4294967295" diff --git a/crates/contracts/Scarb.toml b/crates/contracts/Scarb.toml index 4b2416282..3c60f88bf 100644 --- a/crates/contracts/Scarb.toml +++ b/crates/contracts/Scarb.toml @@ -24,7 +24,7 @@ name = "contracts" [dev-dependencies] snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry.git", tag = "v0.30.0" } -assert_macros = "0.1.0" +assert_macros = "2.8.2" snforge_utils = { path = "../snforge_utils" } [scripts] diff --git a/crates/contracts/tests/test_contract_account.cairo b/crates/contracts/tests/test_contract_account.cairo index 1c379610b..ab95aac48 100644 --- a/crates/contracts/tests/test_contract_account.cairo +++ b/crates/contracts/tests/test_contract_account.cairo @@ -19,7 +19,7 @@ fn test_ca_deploy() { let initial_bytecode = contract_account.bytecode(); assert(initial_bytecode.is_empty(), 'bytecode should be empty'); - assert(contract_account.get_evm_address() == ca_address(), 'wrong ca evm address'); + assert(contract_account.get_evm_address() == ca_address.evm, 'wrong ca evm address'); assert(contract_account.get_nonce() == 1, 'wrong nonce'); } diff --git a/crates/evm/Scarb.toml b/crates/evm/Scarb.toml index a88b0d56e..17265e8b4 100644 --- a/crates/evm/Scarb.toml +++ b/crates/evm/Scarb.toml @@ -15,7 +15,7 @@ garaga = { git = "https://github.com/keep-starknet-strange/garaga.git" } [dev-dependencies] snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry.git", tag = "v0.30.0" } snforge_utils = { path = "../snforge_utils" } -assert_macros = "0.1.0" +assert_macros = "2.8.2" [tool] fmt.workspace = true diff --git a/crates/evm/src/precompiles/ec_add.cairo b/crates/evm/src/precompiles/ec_add.cairo index 1bbac18ec..a8ef25764 100644 --- a/crates/evm/src/precompiles/ec_add.cairo +++ b/crates/evm/src/precompiles/ec_add.cairo @@ -7,16 +7,15 @@ use core::circuit::{ circuit_mul, circuit_inverse, EvalCircuitResult, EvalCircuitTrait, CircuitOutputsTrait, CircuitModulus, AddInputResultTrait, CircuitInputs, CircuitInputAccumulator }; +use core::num::traits::Zero; -use core::internal::BoundedInt; use core::option::Option; use core::starknet::SyscallResultTrait; use core::starknet::{EthAddress}; use evm::errors::{EVMError}; use evm::precompiles::Precompile; use garaga::core::circuit::AddInputResultTrait2; -use garaga::utils::u384_eq_zero; use utils::helpers::{U256Trait, ToBytes, FromBytes}; use utils::helpers::{load_word, u256_to_bytes_array}; @@ -106,7 +105,10 @@ fn ec_add(x1: u256, y1: u256, x2: u256, y2: u256) -> Option<(u256, u256)> { Option::Some(( x, y )) => Option::Some( - (u384_circuit_output_to_u256(x), u384_circuit_output_to_u256(y)) + ( + TryInto::::try_into(x).unwrap(), + TryInto::::try_into(y).unwrap() + ) ), Option::None => Option::Some((0, 0)), } @@ -166,7 +168,7 @@ fn is_on_curve(x: u384, y: u384) -> bool { let outputs = circuit_inputs.done_2().eval(modulus).unwrap(); let zero_check: u384 = outputs.get_output(check); - return u384_eq_zero(zero_check); + return zero_check.is_zero(); } @@ -250,7 +252,7 @@ fn eq_mod_p(a: u384, b: u384) -> bool { let outputs = (sub,).new_inputs().next_2(a).next_2(b).done_2().eval(modulus).unwrap(); - return u384_eq_zero(outputs.get_output(sub)); + return outputs.get_output(sub).is_zero(); } // returns true if a == -b mod p_bn254 @@ -266,101 +268,54 @@ fn eq_neg_mod_p(a: u384, b: u384) -> bool { let outputs = (check,).new_inputs().next_2(a).next_2(b).done_2().eval(modulus).unwrap(); - return u384_eq_zero(outputs.get_output(check)); + return outputs.get_output(check).is_zero(); } -type ConstValue = BoundedInt; -const POW64: felt252 = 0x10000000000000000; -const POW32: felt252 = 0x100000000; -const POW96: felt252 = 0x1000000000000000000000000; -const POW32_TYPED: ConstValue = 0x100000000; -const NZ_POW32_TYPED: NonZero> = 0x100000000; - -const NZ_POW64_TYPED: NonZero> = 0x10000000000000000; - - -trait DivRemHelper { - type DivT; - type RemT; -} -impl DivRemU96By64 of DivRemHelper> { - type DivT = BoundedInt<0, { POW32 - 1 }>; - type RemT = BoundedInt<0, { POW64 - 1 }>; -} - -impl DivRemU96By32 of DivRemHelper> { - type DivT = BoundedInt<0, { POW64 - 1 }>; - type RemT = BoundedInt<0, { POW32 - 1 }>; -} - -extern fn bounded_int_div_rem>( - lhs: Lhs, rhs: NonZero, -) -> (H::DivT, H::RemT) implicits(RangeCheck) nopanic; - - -// Cuts a u384 into a u256. -// Must be used on circuit outputs ran with a p <=256 bits -// so that the outputs are guaranteed to be less than p. -fn u384_circuit_output_to_u256(x: u384) -> u256 { - // limb3_96 || limb2_96 || limb1_96 || limb0_96 - let (q_limb1_64, r_limb1_32) = bounded_int_div_rem(x.limb1, NZ_POW32_TYPED); - // limb3_96 || limb2_96 || q_limb1_64 || r_limb1_32 || limb0_96 - let low: felt252 = (r_limb1_32.into() * POW96) + x.limb0.into(); - // limb3_96 || limb2_96 || q_limb1_64 || low_128 - let (_q_limb2_32, r_limb2_64) = bounded_int_div_rem(x.limb2, NZ_POW64_TYPED); - // limb3_96 || q_limb2_32 || r_limb2_64 || q_limb1_64 || low_128 - - let high: felt252 = (r_limb2_64.into() * POW64) + q_limb1_64.into(); - - return u256 { low: low.try_into().unwrap(), high: high.try_into().unwrap() }; -} - #[cfg(test)] mod tests { use super::{ - u384_circuit_output_to_u256, eq_mod_p, eq_neg_mod_p, double_ec_point_unchecked, - add_ec_point_unchecked, is_on_curve, u384, POW32, POW64, POW96 + eq_mod_p, eq_neg_mod_p, double_ec_point_unchecked, add_ec_point_unchecked, is_on_curve, u384 }; use utils::helpers::{U256Trait, ToBytes, FromBytes}; #[test] - fn test_u384_circuit_output_to_u256() { + fn test_u384_to_u256() { let x = u384 { limb0: 0x1, limb1: 0x0, limb2: 0x0, limb3: 0x0 }; - let y = u384_circuit_output_to_u256(x); + let y = TryInto::::try_into(x).unwrap(); assert_eq!(y, u256 { low: 0x1, high: 0x0 }); let x = u384 { limb0: 0x0, limb1: 0x0, limb2: 0x0, limb3: 0x0 }; - let y = u384_circuit_output_to_u256(x); + let y = TryInto::::try_into(x).unwrap(); assert_eq!(y, u256 { low: 0x0, high: 0x0 }); let x = u384 { limb0: 0xc77661, limb1: 0x0, limb2: 0x0, limb3: 0x0 }; - let y = u384_circuit_output_to_u256(x); + let y = TryInto::::try_into(x).unwrap(); assert_eq!(y, u256 { low: 0xc77661, high: 0x0 }); let x = u384 { limb0: 0xa1f1ae97, limb1: 0x0, limb2: 0x0, limb3: 0x0 }; - let y = u384_circuit_output_to_u256(x); + let y = TryInto::::try_into(x).unwrap(); assert_eq!(y, u256 { low: 0xa1f1ae97, high: 0x0 }); let x = u384 { limb0: 0x6dbd0f5925f2ea8792be851d, limb1: 0x60, limb2: 0x0, limb3: 0x0 }; - let y = u384_circuit_output_to_u256(x); + let y = TryInto::::try_into(x).unwrap(); assert_eq!(y, u256 { low: 0x606dbd0f5925f2ea8792be851d, high: 0x0 }); let x = u384 { limb0: 0x288ad273930c8e07bee0b040, limb1: 0x9a80, limb2: 0x0, limb3: 0x0 }; - let y = u384_circuit_output_to_u256(x); + let y = TryInto::::try_into(x).unwrap(); assert_eq!(y, u256 { low: 0x9a80288ad273930c8e07bee0b040, high: 0x0 }); let x = u384 { limb0: 0x79f59cab560d347406f8f978, limb1: 0x32355e68, limb2: 0x0, limb3: 0x0 }; - let y = u384_circuit_output_to_u256(x); + let y = TryInto::::try_into(x).unwrap(); assert_eq!(y, u256 { low: 0x32355e6879f59cab560d347406f8f978, high: 0x0 }); let x = u384 { limb0: 0xf7c12fd7cd43a2091356f287, limb1: 0x5670d3784d, limb2: 0x0, limb3: 0x0 }; - let y = u384_circuit_output_to_u256(x); + let y = TryInto::::try_into(x).unwrap(); assert_eq!(y, u256 { low: 0x70d3784df7c12fd7cd43a2091356f287, high: 0x56 }); let x = u384 { limb0: 0x4def54e61b4eee26c407edc8, limb1: 0x6a3d1d0cac6d, limb2: 0x0, limb3: 0x0 }; - let y = u384_circuit_output_to_u256(x); + let y = TryInto::::try_into(x).unwrap(); assert_eq!(y, u256 { low: 0x1d0cac6d4def54e61b4eee26c407edc8, high: 0x6a3d }); let x = u384 { @@ -369,7 +324,7 @@ mod tests { limb2: 0x0, limb3: 0x0 }; - let y = u384_circuit_output_to_u256(x); + let y = TryInto::::try_into(x).unwrap(); assert_eq!(y, u256 { low: 0x836f45e30a666c4bd0b0f6ac7bfc6697, high: 0x55354b07685a19 }); let x = u384 { @@ -378,7 +333,7 @@ mod tests { limb2: 0x0, limb3: 0x0 }; - let y = u384_circuit_output_to_u256(x); + let y = TryInto::::try_into(x).unwrap(); assert_eq!(y, u256 { low: 0x7a497f6bf99e6e4a89d4c4bf4eeb5764, high: 0xba69422bccfb0bf0 }); let x = u384 { @@ -387,7 +342,7 @@ mod tests { limb2: 0xda, limb3: 0x0 }; - let y = u384_circuit_output_to_u256(x); + let y = TryInto::::try_into(x).unwrap(); assert_eq!(y, u256 { low: 0xf4262ef4a18fd325c835625f53342a9f, high: 0xda3f862f6ff3d3c356 }); let x = u384 { @@ -396,7 +351,7 @@ mod tests { limb2: 0x4bb761b32d048, limb3: 0x0 }; - let y = u384_circuit_output_to_u256(x); + let y = TryInto::::try_into(x).unwrap(); assert_eq!( y, u256 { low: 0x4abd71f14332f4d7188cef59cbdef8db, high: 0x4bb761b32d048bb3e59509bf71bec } diff --git a/crates/evm/src/precompiles/ec_mul.cairo b/crates/evm/src/precompiles/ec_mul.cairo index c3f495982..b77825fde 100644 --- a/crates/evm/src/precompiles/ec_mul.cairo +++ b/crates/evm/src/precompiles/ec_mul.cairo @@ -14,7 +14,7 @@ use evm::precompiles::Precompile; use evm::precompiles::ec_add::{ is_on_curve, eq_mod_p, eq_neg_mod_p, double_ec_point_unchecked, add_ec_point_unchecked, - ec_safe_add, u384_circuit_output_to_u256 + ec_safe_add, }; use garaga::core::circuit::AddInputResultTrait2; use garaga::utils::u384_eq_zero; @@ -83,9 +83,7 @@ fn ec_mul(x1: u256, y1: u256, s: u256) -> Option<(u256, u256)> { match pt { Option::Some(( x, y - )) => Option::Some( - (u384_circuit_output_to_u256(x), u384_circuit_output_to_u256(y)) - ), + )) => Option::Some((x.try_into().unwrap(), y.try_into().unwrap())), Option::None => Option::Some((0, 0)), } } diff --git a/crates/openzeppelin/Scarb.toml b/crates/openzeppelin/Scarb.toml index 5e139c1e0..9ac70ab32 100644 --- a/crates/openzeppelin/Scarb.toml +++ b/crates/openzeppelin/Scarb.toml @@ -25,6 +25,7 @@ fmt.workspace = true [dev-dependencies] snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry.git", tag = "v0.30.0" } +assert_macros = "2.8.2" [scripts] test = "snforge test --max-n-steps 4294967295" diff --git a/crates/snforge_utils/Scarb.toml b/crates/snforge_utils/Scarb.toml index 0c9437456..1c0d12acc 100644 --- a/crates/snforge_utils/Scarb.toml +++ b/crates/snforge_utils/Scarb.toml @@ -6,11 +6,12 @@ edition = "2023_11" # See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html [dependencies] -starknet = "2.7.1" +starknet = "2.8.2" evm = { path = "../evm" } [dev-dependencies] snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v0.30.0" } +assert_macros = "2.8.2" [[target.starknet-contract]] sierra = true diff --git a/crates/utils/Scarb.toml b/crates/utils/Scarb.toml index f637ecf2e..ee3ce2097 100644 --- a/crates/utils/Scarb.toml +++ b/crates/utils/Scarb.toml @@ -18,6 +18,7 @@ fmt.workspace = true [dev-dependencies] snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry.git", tag = "v0.30.0" } +assert_macros = "2.8.2" [scripts] test = "snforge test --max-n-steps 4294967295"