From ea1bfa30af464e67ec657271e7de4f862eadf68d Mon Sep 17 00:00:00 2001 From: Roberto Catalan Date: Fri, 12 May 2023 16:42:04 -0300 Subject: [PATCH] Test RandomEcPoint hint & Fix for LinearSplit (#1142) * Implement TryFrom for Program * Add cairo1hintprocessor dependency * Map hints to pc * Add Cairo1HintProcessor * Move cairo-1-hint-processor to cairo-rs crate * fixes * Fix test helper * Remove allow * fix test func * Add builtins to test * Extract builtins from contract_class * Add _builtin to builtin names in etrypoint data * Copy logic from cairo1 contract execution in starknet * Remove unused code * Use lower initial_gas value * Add program segment size argument * Check return values in run_cairo_1_entrypoint fn * Remove debug prints * Add basic fibonacci test * Add another fibonacci case * Always verify secure * Clippy * Compile casm contracts when running test target * Remove unwrap from cell_ref_to_relocatable * Remove paniking macro from extract_buffer * Misc improvements * Misc improvements * Misc improvements * Misc improvements * Remove unwraps & asserts from DictSquashExecScope::pop_current_key * Remove unwraps & asserts from DictManagerExecScope::new_default_dict * Remove expect from get_dict_tracker * Add constants for cairo 1 compiler binaries in makefile * Add cairo 1 compiler to deps target in makefile * Add cairo folder to clean target * Remove todo from execute method * Separate helper functions from Cairo1HintProcessor implementation * Add untracked file * Fix * Add changelog entry * Add a job to compile cairo 1 contracts in ci * Add job dependency * Fix yml syntax * Fix yml syntax * Temporarily extempt cairo-1-hint-processor from codecov * Fix yml syntax * Fix workflow * Remove cache code from new job * Fix yml syntax * Fix wrong path * Fix makefile * Build only compiler binaries * Add cairo-1-contracts-cache * Fetch contracts cache in jobs that need them * Use no-std version of HashMap * Import stdlib::prelude::* * Wrap print in not-wasm block * Remove std path * use slices instead of vec * Make DictSquashExecScope fields private * Import hint processor dependencies without default features * -Clippy * Add type * Compile cairo 1 contracts in build-programs job * Rename cache * Use target dependency instead of explicit $(MAKE) * Fix yml syntax * Check for cairo folder before cloning cairo repo * Ommit folder name * Swap paths * Add cairo-1-hints feature flag * Add compile-hint feature to tests run in workflow * Add cairo-1-hints to test_utils * Add cairo-1-hints to test_utils * Use both paths when fetching compiled test data * Remove cairo-1-hints feature from test_utils feature * Move dependencies to cairo-1-hints feature * Update CHANGELOG.md * Fix cfg directive * Add cairo-1-hints to test workflow * Add Cairo 1 considerations for Gitignore and Makefile (#1144) * Add ignore for casm and sierra files * Add libfunc experimental for cairo 1 compilation * Add new enty to CHANGELOG * Add test for RandomEcPoint hint * Add rust test for Cairo 1 program * Fix Cairo 1 program * Change contract & Add activate feature for Cairo 1 * Add new entry to Changelog * Restore CHANGELOG.md * Restore Cargo.toml * Add eof new line * Add more coverage to the test * Refactor contract test * Fix LinearSplit hint * Add fix entry to the CHANGELOG * Remove outdated comment * Remove CHANGELOG entry --------- Co-authored-by: Federica Co-authored-by: fmoletta <99273364+fmoletta@users.noreply.github.com> Co-authored-by: Pedro Fontana --- .../cairo-1-contracts/random_ec_point.cairo | 27 +++++++++++++++++++ .../cairo_1_hint_processor/hint_processor.rs | 10 +++---- .../cairo_1_run_from_entrypoint_tests.rs | 8 ++++++ 3 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 cairo_programs/cairo-1-contracts/random_ec_point.cairo diff --git a/cairo_programs/cairo-1-contracts/random_ec_point.cairo b/cairo_programs/cairo-1-contracts/random_ec_point.cairo new file mode 100644 index 0000000000..d504c5e3ab --- /dev/null +++ b/cairo_programs/cairo-1-contracts/random_ec_point.cairo @@ -0,0 +1,27 @@ +#[contract] +mod RandomEcPoint{ + use option::OptionTrait; + use ec::ec_state_init; + use ec::ec_state_add; + use ec::ec_state_try_finalize_nz; + use ec::ec_point_from_x; + use ec::ec_point_non_zero; + use ec::ec_point_unwrap; + + // Test taken from https://github.com/starkware-libs/cairo/blob/a0ead7c0b8e297d281c7213151cd43ac11de5042/corelib/src/test/ec_test.cairo#L17 + #[external] + fn random_ec_point() -> felt252{ + let p = ec_point_from_x(1).unwrap(); + let p_nz = ec_point_non_zero(p); + + let mut state = ec_state_init(); + ec_state_add(ref state, p_nz); + + let q = ec_state_try_finalize_nz(state).expect('zero point'); + let (qx, qy) = ec_point_unwrap(q); + + assert(qx == 1, 'bad finalize x'); + qx + } + +} diff --git a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs index 9c2c3c5b37..9698dcd779 100644 --- a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs +++ b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs @@ -465,15 +465,15 @@ impl Cairo1HintProcessor { x: &CellRef, y: &CellRef, ) -> Result<(), HintError> { - let value = res_operand_get_val(vm, value)?; - let scalar = res_operand_get_val(vm, scalar)?; - let max_x = res_operand_get_val(vm, max_x)?; + let value = res_operand_get_val(vm, value)?.to_biguint(); + let scalar = res_operand_get_val(vm, scalar)?.to_biguint(); + let max_x = res_operand_get_val(vm, max_x)?.to_biguint(); let x_value = (&value / &scalar).min(max_x); let y_value = value - &x_value * &scalar; - vm.insert_value(cell_ref_to_relocatable(x, vm)?, x_value) + vm.insert_value(cell_ref_to_relocatable(x, vm)?, Felt252::from(x_value)) .map_err(HintError::from)?; - vm.insert_value(cell_ref_to_relocatable(y, vm)?, y_value) + vm.insert_value(cell_ref_to_relocatable(y, vm)?, Felt252::from(y_value)) .map_err(HintError::from)?; Ok(()) diff --git a/src/tests/cairo_1_run_from_entrypoint_tests.rs b/src/tests/cairo_1_run_from_entrypoint_tests.rs index 6b52159700..d63984a83d 100644 --- a/src/tests/cairo_1_run_from_entrypoint_tests.rs +++ b/src/tests/cairo_1_run_from_entrypoint_tests.rs @@ -459,6 +459,14 @@ fn linear_split() { ); } +#[test] +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] +fn random_ec_point() { + let program_data = + include_bytes!("../../cairo_programs/cairo-1-contracts/random_ec_point.casm"); + run_cairo_1_entrypoint(program_data.as_slice(), 0, &[], &[1.into()]); +} + #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn assert_le_find_small_arcs() {