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

feat(hints): Implement NewHint#64 #1044

Merged
merged 6 commits into from
Apr 26, 2023
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -2,6 +2,14 @@

#### Upcoming Changes

* Implement hint on 0.6.0.json whitelist [#1044](https://github.com/lambdaclass/cairo-rs/pull/1044):

`BuiltinHintProcessor` now supports the following hints:

%{
ids.a_lsb = ids.a & 1
ids.b_lsb = ids.b & 1
%}

* Implement hint on `assert_le_felt` for versions 0.6.0 and 0.8.2 [#1047](https://github.com/lambdaclass/cairo-rs/pull/1047):

18 changes: 18 additions & 0 deletions cairo_programs/bitand_hint.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
func main() {
alloc_locals;
let a = 7;
let b = 999;

local a_lsb;
local b_lsb;

%{
ids.a_lsb = ids.a & 1
ids.b_lsb = ids.b & 1
%}

assert a_lsb = 1;
assert b_lsb = 1;

return();
}
Original file line number Diff line number Diff line change
@@ -635,6 +635,9 @@ impl HintProcessor for BuiltinHintProcessor {
hint_code::EC_RECOVER_SUB_A_B => {
ec_recover_sub_a_b(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking)
}
hint_code::A_B_BITAND_1 => {
a_b_bitand_1(vm, &hint_data.ids_data, &hint_data.ap_tracking)
}
hint_code::ASSERT_LE_FELT_V_0_6 => {
assert_le_felt_v_0_6(vm, &hint_data.ids_data, &hint_data.ap_tracking)
}
2 changes: 2 additions & 0 deletions src/hint_processor/builtin_hint_processor/hint_code.rs
Original file line number Diff line number Diff line change
@@ -1094,6 +1094,8 @@ from starkware.python.math_utils import div_mod, safe_div
a = pack(ids.a, PRIME)
b = pack(ids.b, PRIME)
value = res = a - b"#;
pub const A_B_BITAND_1: &str = "ids.a_lsb = ids.a & 1
ids.b_lsb = ids.b & 1";
pub const EC_RECOVER_PRODUCT_MOD: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import pack
from starkware.python.math_utils import div_mod, safe_div

19 changes: 19 additions & 0 deletions src/hint_processor/builtin_hint_processor/math_utils.rs
Original file line number Diff line number Diff line change
@@ -622,6 +622,25 @@ fn div_prime_by_bound(bound: Felt252) -> Result<Felt252, VirtualMachineError> {
Ok(Felt252::new(limit))
}

/* Implements hint:
%{
ids.a_lsb = ids.a & 1
ids.b_lsb = ids.b & 1
%}
*/
pub fn a_b_bitand_1(
vm: &mut VirtualMachine,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
) -> Result<(), HintError> {
let a = get_integer_from_var_name("a", vm, ids_data, ap_tracking)?;
let b = get_integer_from_var_name("b", vm, ids_data, ap_tracking)?;
let a_lsb = a.as_ref() & Felt252::one();
let b_lsb = b.as_ref() & Felt252::one();
insert_value_from_var_name("a_lsb", a_lsb, vm, ids_data, ap_tracking)?;
insert_value_from_var_name("b_lsb", b_lsb, vm, ids_data, ap_tracking)
}

#[cfg(test)]
mod tests {
use super::*;
7 changes: 7 additions & 0 deletions src/tests/cairo_run_test.rs
Original file line number Diff line number Diff line change
@@ -828,6 +828,13 @@ fn cairo_run_inv_mod_p_uint512() {
run_program_simple(program_data.as_slice());
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn bitand_hint() {
let program_data = include_bytes!("../../cairo_programs/bitand_hint.json");
run_program_simple(program_data.as_slice());
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn assert_le_felt_old() {