Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some refactoring #574

Merged
merged 14 commits into from
May 13, 2024
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
2 changes: 1 addition & 1 deletion benches/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn prepare_programs(path: &str) -> Vec<(Arc<Program>, String)> {
_ => None,
}
})
.collect::<Vec<_>>()
tcoratger marked this conversation as resolved.
Show resolved Hide resolved
.collect()
tcoratger marked this conversation as resolved.
Show resolved Hide resolved
}

#[allow(unused)] // its used but clippy doesn't detect it well
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
6 changes: 3 additions & 3 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ fn generate_function_structure<'c, 'a>(
(
k,
(
v.0.map(|x| (x.0, x.1.into_iter().map(|x| x.0).collect::<Vec<_>>())),
v.0.map(|x| (x.0, x.1.into_iter().map(|x| x.0).collect())),
tcoratger marked this conversation as resolved.
Show resolved Hide resolved
tcoratger marked this conversation as resolved.
Show resolved Hide resolved
v.1,
),
)
Expand Down Expand Up @@ -954,7 +954,7 @@ where
None => BranchArg::External(state[var_id]),
}
})
.collect::<Vec<_>>();
.collect();
tcoratger marked this conversation as resolved.
Show resolved Hide resolved

(landing_block.deref(), target_vars)
}
Expand All @@ -975,7 +975,7 @@ where
None => BranchArg::External(state[var_id]),
}
})
.collect::<Vec<_>>();
.collect();
tcoratger marked this conversation as resolved.
Show resolved Hide resolved

(block.deref(), target_vars)
}
Expand Down
6 changes: 3 additions & 3 deletions src/debug_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl DebugInfo {
.map(|x| x.map(|location| (type_declaration.id.clone(), location)))
.transpose()
})
.collect::<Result<HashMap<_, _>, DiagnosticAdded>>()?;
pefontana marked this conversation as resolved.
Show resolved Hide resolved
.collect::<Result<_, _>>()?;
tcoratger marked this conversation as resolved.
Show resolved Hide resolved

let libfunc_declarations = program
.libfunc_declarations
Expand All @@ -49,7 +49,7 @@ impl DebugInfo {
.map(|x| x.map(|location| (libfunc_declaration.id.clone(), location)))
.transpose()
})
.collect::<Result<HashMap<_, _>, DiagnosticAdded>>()?;
.collect::<Result<HashMap<_, _>, _>>()?;
tcoratger marked this conversation as resolved.
Show resolved Hide resolved
tcoratger marked this conversation as resolved.
Show resolved Hide resolved

let statements =
find_all_statements(db, |id| libfunc_declarations.contains_key(id), program)?;
Expand All @@ -58,7 +58,7 @@ impl DebugInfo {
.funcs
.iter()
.map(|function| Ok((function.id.clone(), find_func(db, function)?)))
.collect::<Result<HashMap<_, _>, DiagnosticAdded>>()?;
.collect::<Result<_, _>>()?;
tcoratger marked this conversation as resolved.
Show resolved Hide resolved

Ok(Self {
type_declarations,
Expand Down
2 changes: 1 addition & 1 deletion src/libfuncs/ec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,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
5 changes: 2 additions & 3 deletions src/libfuncs/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ use melior::{
},
Context,
};
use std::num::TryFromIntError;

/// Select and call the correct libfunc builder function from the selector.
pub fn build<'ctx, 'this>(
Expand Down Expand Up @@ -272,7 +271,7 @@ pub fn build_match<'ctx, 'this>(

let case_values = (0..variant_tys.len())
.map(i64::try_from)
.collect::<std::result::Result<Vec<_>, TryFromIntError>>()?;
.collect::<std::result::Result<Vec<_>, _>>()?;
tcoratger marked this conversation as resolved.
Show resolved Hide resolved

entry.append_operation(cf::switch(
context,
Expand Down Expand Up @@ -428,7 +427,7 @@ pub fn build_snapshot_match<'ctx, 'this>(

let case_values = (0..variant_tys.len())
.map(i64::try_from)
.collect::<std::result::Result<Vec<_>, TryFromIntError>>()?;
.collect::<std::result::Result<Vec<_>, _>>()?;
tcoratger marked this conversation as resolved.
Show resolved Hide resolved

entry.append_operation(cf::switch(
context,
Expand Down
54 changes: 27 additions & 27 deletions src/libfuncs/starknet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4623,7 +4623,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 @@ -4632,14 +4632,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 @@ -4657,7 +4657,7 @@ mod test {
)
.unwrap()
.into()],
Felt::from(0).into(),
Felt::ZERO.into(),
);
}

Expand All @@ -4666,14 +4666,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 @@ -4694,14 +4694,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 @@ -4720,13 +4720,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 @@ -4746,13 +4746,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 @@ -4776,14 +4776,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 @@ -4804,14 +4804,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
Loading