Skip to content

Commit

Permalink
some refactoring (lambdaclass#574)
Browse files Browse the repository at this point in the history
* refactor collect instructions

* Update src/libfuncs/enum.rs

* Update benches/util.rs

* Update src/compiler.rs

* Update src/compiler.rs

* Update src/compiler.rs

* Update src/debug_info.rs

* Update src/debug_info.rs

* Update src/debug_info.rs

* Update src/libfuncs/enum.rs

* update

---------

Co-authored-by: Pedro Fontana <fontana.pedro93@gmail.com>
Co-authored-by: Edgar <git@edgarluque.com>
  • Loading branch information
3 people authored and Gerson2102 committed May 15, 2024
1 parent 2f7a0dd commit c2a9049
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 46 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ fn main() {
.execute_contract(
fn_id,
// The calldata
&[JitValue::Felt252(Felt::from(1))],
&[JitValue::Felt252(Felt::ONE)],
u64::MAX.into(),
)
.expect("failed to execute the given contract");
Expand Down Expand Up @@ -601,7 +601,7 @@ impl StarkNetSyscallHandler for SyscallHandler {
println!("Called `deploy({class_hash}, {contract_address_salt}, {calldata:?}, {deploy_from_zero})` from MLIR.");
Ok((
class_hash + contract_address_salt,
calldata.iter().map(|x| x + &Felt::from(1)).collect(),
calldata.iter().map(|x| x + &Felt::ONE).collect(),
))
}

Expand Down
8 changes: 4 additions & 4 deletions benches/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ fn criterion_benchmark(c: &mut Criterion) {
let fibonacci = load_contract("programs/benches/fib_2M.cairo");
let logistic_map = load_contract("programs/benches/logistic_map.cairo");

let aot_factorial = aot_cache.compile_and_insert(Felt::from(0), &factorial, OptLevel::None);
let aot_fibonacci = aot_cache.compile_and_insert(Felt::from(1), &fibonacci, OptLevel::None);
let aot_factorial = aot_cache.compile_and_insert(Felt::ZERO, &factorial, OptLevel::None);
let aot_fibonacci = aot_cache.compile_and_insert(Felt::ONE, &fibonacci, OptLevel::None);
let aot_logistic_map =
aot_cache.compile_and_insert(Felt::from(2), &logistic_map, OptLevel::None);

let jit_factorial = jit_cache.compile_and_insert(Felt::from(0), &factorial, OptLevel::None);
let jit_fibonacci = jit_cache.compile_and_insert(Felt::from(1), &fibonacci, OptLevel::None);
let jit_factorial = jit_cache.compile_and_insert(Felt::ZERO, &factorial, OptLevel::None);
let jit_fibonacci = jit_cache.compile_and_insert(Felt::ONE, &fibonacci, OptLevel::None);
let jit_logistic_map =
jit_cache.compile_and_insert(Felt::from(2), &logistic_map, OptLevel::None);

Expand Down
4 changes: 2 additions & 2 deletions examples/erc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl StarknetSyscallHandler for SyscallHandler {
println!("Called `deploy({class_hash}, {contract_address_salt}, {calldata:?}, {deploy_from_zero})` from MLIR.");
Ok((
class_hash + contract_address_salt,
calldata.iter().map(|x| x + Felt::from(1)).collect(),
calldata.iter().map(|x| x + Felt::ONE).collect(),
))
}

Expand Down Expand Up @@ -310,7 +310,7 @@ fn main() {
&[
Felt::from_bytes_be_slice(b"name"),
Felt::from_bytes_be_slice(b"symbol"),
Felt::from(0),
Felt::ZERO,
Felt::from(i64::MAX),
Felt::from(4),
Felt::from(6),
Expand Down
4 changes: 2 additions & 2 deletions examples/starknet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl StarknetSyscallHandler for SyscallHandler {
println!("Called `deploy({class_hash}, {contract_address_salt}, {calldata:?}, {deploy_from_zero})` from MLIR.");
Ok((
class_hash + contract_address_salt,
calldata.iter().map(|x| x + Felt::from(1)).collect(),
calldata.iter().map(|x| x + Felt::ONE).collect(),
))
}

Expand Down Expand Up @@ -305,7 +305,7 @@ fn main() {
let native_executor = JitNativeExecutor::from_native_module(native_program, Default::default());

let result = native_executor
.invoke_contract_dynamic(fn_id, &[Felt::from(1)], Some(u128::MAX), SyscallHandler)
.invoke_contract_dynamic(fn_id, &[Felt::ONE], Some(u128::MAX), SyscallHandler)
.expect("failed to execute the given contract");

println!();
Expand Down
2 changes: 1 addition & 1 deletion src/libfuncs/ec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ mod test {
Felt::from_dec_str("3151312365169595090315724863753927489909436624354740709748557281394568342450").unwrap(),
Felt::from_dec_str("2835232394579952276045648147338966184268723952674536708929458753792035266179").unwrap()
),
Felt::from(1).into(), // scalar
Felt::ONE.into(), // scalar
JitValue::EcPoint(
Felt::from_dec_str("1234").unwrap(),
Felt::from_dec_str("1301976514684871091717790968549291947487646995000837413367950573852273027507").unwrap()
Expand Down
54 changes: 27 additions & 27 deletions src/libfuncs/starknet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4607,7 +4607,7 @@ mod test {

#[test]
fn class_hash_const() {
run_program_assert_output(&CLASS_HASH_CONST, "run_program", &[], Felt::from(0).into())
run_program_assert_output(&CLASS_HASH_CONST, "run_program", &[], Felt::ZERO.into())
}

#[test]
Expand All @@ -4616,14 +4616,14 @@ mod test {
run_program_assert_output(
&STORAGE_BASE_ADDRESS_FROM_FELT252,
"run_program",
&[Felt::from(0).into()],
Felt::from(0).into(),
&[Felt::ZERO.into()],
Felt::ZERO.into(),
);
run_program_assert_output(
&STORAGE_BASE_ADDRESS_FROM_FELT252,
"run_program",
&[Felt::from(1).into()],
Felt::from(1).into(),
&[Felt::ONE.into()],
Felt::ONE.into(),
);
run_program_assert_output(
&STORAGE_BASE_ADDRESS_FROM_FELT252,
Expand All @@ -4641,7 +4641,7 @@ mod test {
)
.unwrap()
.into()],
Felt::from(0).into(),
Felt::ZERO.into(),
);
}

Expand All @@ -4650,14 +4650,14 @@ mod test {
run_program_assert_output(
&STORAGE_ADDRESS_FROM_BASE,
"run_program",
&[Felt::from(0).into()],
Felt::from(0).into(),
&[Felt::ZERO.into()],
Felt::ZERO.into(),
);
run_program_assert_output(
&STORAGE_ADDRESS_FROM_BASE,
"run_program",
&[Felt::from(1).into()],
Felt::from(1).into(),
&[Felt::ONE.into()],
Felt::ONE.into(),
);
run_program_assert_output(
&STORAGE_ADDRESS_FROM_BASE,
Expand All @@ -4678,14 +4678,14 @@ mod test {
run_program_assert_output(
&STORAGE_ADDRESS_FROM_BASE_AND_OFFSET,
"run_program",
&[Felt::from(0).into(), 0u8.into()],
Felt::from(0).into(),
&[Felt::ZERO.into(), 0u8.into()],
Felt::ZERO.into(),
);
run_program_assert_output(
&STORAGE_ADDRESS_FROM_BASE_AND_OFFSET,
"run_program",
&[Felt::from(1).into(), 0u8.into()],
Felt::from(1).into(),
&[Felt::ONE.into(), 0u8.into()],
Felt::ONE.into(),
);
run_program_assert_output(
&STORAGE_ADDRESS_FROM_BASE_AND_OFFSET,
Expand All @@ -4704,13 +4704,13 @@ mod test {
run_program_assert_output(
&STORAGE_ADDRESS_FROM_BASE_AND_OFFSET,
"run_program",
&[Felt::from(0).into(), 1u8.into()],
Felt::from(1).into(),
&[Felt::ZERO.into(), 1u8.into()],
Felt::ONE.into(),
);
run_program_assert_output(
&STORAGE_ADDRESS_FROM_BASE_AND_OFFSET,
"run_program",
&[Felt::from(1).into(), 1u8.into()],
&[Felt::ONE.into(), 1u8.into()],
Felt::from(2).into(),
);
run_program_assert_output(
Expand All @@ -4730,13 +4730,13 @@ mod test {
run_program_assert_output(
&STORAGE_ADDRESS_FROM_BASE_AND_OFFSET,
"run_program",
&[Felt::from(0).into(), 255u8.into()],
&[Felt::ZERO.into(), 255u8.into()],
Felt::from(255).into(),
);
run_program_assert_output(
&STORAGE_ADDRESS_FROM_BASE_AND_OFFSET,
"run_program",
&[Felt::from(1).into(), 255u8.into()],
&[Felt::ONE.into(), 255u8.into()],
Felt::from(256).into(),
);

Expand All @@ -4760,14 +4760,14 @@ mod test {
run_program_assert_output(
&STORAGE_ADDRESS_TO_FELT252,
"run_program",
&[Felt::from(0).into()],
Felt::from(0).into(),
&[Felt::ZERO.into()],
Felt::ZERO.into(),
);
run_program_assert_output(
&STORAGE_ADDRESS_TO_FELT252,
"run_program",
&[Felt::from(1).into()],
Felt::from(1).into(),
&[Felt::ONE.into()],
Felt::ONE.into(),
);
run_program_assert_output(
&STORAGE_ADDRESS_TO_FELT252,
Expand All @@ -4788,14 +4788,14 @@ mod test {
run_program_assert_output(
&STORAGE_ADDRESS_TRY_FROM_FELT252,
"run_program",
&[Felt::from(0).into()],
jit_enum!(0, Felt::from(0).into()),
&[Felt::ZERO.into()],
jit_enum!(0, Felt::ZERO.into()),
);
run_program_assert_output(
&STORAGE_ADDRESS_TRY_FROM_FELT252,
"run_program",
&[Felt::from(1).into()],
jit_enum!(0, Felt::from(1).into()),
&[Felt::ONE.into()],
jit_enum!(0, Felt::ONE.into()),
);
run_program_assert_output(
&STORAGE_ADDRESS_TRY_FROM_FELT252,
Expand Down
8 changes: 4 additions & 4 deletions src/libfuncs/uint128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -943,14 +943,14 @@ mod test {
run_program_assert_output(
&U128_FROM_FELT252,
"run_test",
&[Felt::from(0).into()],
&[Felt::ZERO.into()],
jit_enum!(0, 0u128.into()),
);

run_program_assert_output(
&U128_FROM_FELT252,
"run_test",
&[Felt::from(1).into()],
&[Felt::ONE.into()],
jit_enum!(0, 1u128.into()),
);

Expand Down Expand Up @@ -1097,8 +1097,8 @@ mod test {
fn u128_to_felt252() {
let program = &U128_TO_FELT252;

run_program_assert_output(program, "run_test", &[0u128.into()], Felt::from(0).into());
run_program_assert_output(program, "run_test", &[1u128.into()], Felt::from(1).into());
run_program_assert_output(program, "run_test", &[0u128.into()], Felt::ZERO.into());
run_program_assert_output(program, "run_test", &[1u128.into()], Felt::ONE.into());
run_program_assert_output(
program,
"run_test",
Expand Down
2 changes: 1 addition & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1165,7 +1165,7 @@ pub mod test {
) -> SyscallResult<(Felt, Vec<Felt>)> {
Ok((
class_hash + contract_address_salt,
calldata.iter().map(|x| x + Felt::from(1)).collect(),
calldata.iter().map(|x| x + Felt::ONE).collect(),
))
}

Expand Down
5 changes: 3 additions & 2 deletions tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ pub const fn casm_variant_to_sierra(idx: i64, num_variants: i64) -> i64 {

pub fn get_run_result(r: &RunResultValue) -> Vec<String> {
match r {
RunResultValue::Success(x) => x.iter().map(|x| x.to_string()).collect::<Vec<_>>(),
RunResultValue::Panic(x) => x.iter().map(|x| x.to_string()).collect::<Vec<_>>(),
RunResultValue::Success(x) | RunResultValue::Panic(x) => {
x.iter().map(ToString::to_string).collect()
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/tests/starknet/keccak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl StarknetSyscallHandler for SyscallHandler {
println!("Called `deploy({class_hash}, {contract_address_salt}, {calldata:?}, {deploy_from_zero})` from MLIR.");
Ok((
class_hash + contract_address_salt,
calldata.iter().map(|x| x + Felt::from(1)).collect(),
calldata.iter().map(|x| x + Felt::ONE).collect(),
))
}

Expand Down

0 comments on commit c2a9049

Please sign in to comment.