cairo_vm/hint_processor/builtin_hint_processor/
poseidon_utils.rs1use crate::stdlib::{collections::HashMap, string::String};
2
3use crate::Felt252;
4
5use crate::{
6 hint_processor::hint_processor_definition::HintReference,
7 serde::deserialize_program::ApTracking,
8 vm::{errors::hint_errors::HintError, vm_core::VirtualMachine},
9};
10
11use super::hint_utils::{get_integer_from_var_name, get_ptr_from_var_name, insert_value_into_ap};
12use num_traits::ToPrimitive;
13
14pub fn n_greater_than_10(
16 vm: &mut VirtualMachine,
17 ids_data: &HashMap<String, HintReference>,
18 ap_tracking: &ApTracking,
19) -> Result<(), HintError> {
20 let n = get_integer_from_var_name("n", vm, ids_data, ap_tracking)?
21 .to_usize()
22 .unwrap_or(10); let value = Felt252::from((n >= 10) as usize);
24 insert_value_into_ap(vm, value)
25}
26
27pub fn n_greater_than_2(
29 vm: &mut VirtualMachine,
30 ids_data: &HashMap<String, HintReference>,
31 ap_tracking: &ApTracking,
32) -> Result<(), HintError> {
33 let n = get_integer_from_var_name("n", vm, ids_data, ap_tracking)?
34 .to_usize()
35 .unwrap_or(2);
36 let value = Felt252::from((n >= 2) as usize);
37 insert_value_into_ap(vm, value)
38}
39
40pub fn elements_over_x(
42 vm: &mut VirtualMachine,
43 ids_data: &HashMap<String, HintReference>,
44 ap_tracking: &ApTracking,
45 x: usize,
46) -> Result<(), HintError> {
47 let elements_end = get_ptr_from_var_name("elements_end", vm, ids_data, ap_tracking)?;
48 let elements = get_ptr_from_var_name("elements", vm, ids_data, ap_tracking)?;
49 let value = Felt252::from(((elements_end - elements)? >= x) as usize);
50 insert_value_into_ap(vm, value)
51}
52
53#[cfg(test)]
54mod tests {
55 use crate::any_box;
56 use crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor;
57 use crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData;
58 use crate::hint_processor::hint_processor_definition::HintProcessorLogic;
59 use crate::hint_processor::hint_processor_definition::HintReference;
60
61 use crate::{hint_processor::builtin_hint_processor::hint_code, utils::test_utils::*};
62 use assert_matches::assert_matches;
63
64 #[cfg(target_arch = "wasm32")]
65 use wasm_bindgen_test::*;
66
67 #[test]
68 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
69 fn run_n_greater_than_10_true() {
70 let hint_code = hint_code::NONDET_N_GREATER_THAN_10;
71 let mut vm = vm!();
72 vm.set_ap(3);
73 vm.segments = segments![((1, 0), 21)];
74 vm.set_fp(1);
75 let ids_data = ids_data!("n");
76 assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(()));
77 check_memory![vm.segments.memory, ((1, 3), 1)];
79 }
80
81 #[test]
82 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
83 fn run_n_greater_than_10_false() {
84 let hint_code = hint_code::NONDET_N_GREATER_THAN_10;
85 let mut vm = vm!();
86 vm.set_ap(3);
87 vm.segments = segments![((1, 0), 9)];
88 vm.set_fp(1);
89 let ids_data = ids_data!("n");
90 assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(()));
91 check_memory![vm.segments.memory, ((1, 3), 0)];
93 }
94
95 #[test]
96 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
97 fn run_n_greater_than_2_true() {
98 let hint_code = hint_code::NONDET_N_GREATER_THAN_2;
99 let mut vm = vm!();
100 vm.set_ap(3);
101 vm.segments = segments![((1, 0), 6)];
102 vm.set_fp(1);
103 let ids_data = ids_data!("n");
104 assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(()));
105 check_memory![vm.segments.memory, ((1, 3), 1)];
107 }
108
109 #[test]
110 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
111 fn run_n_greater_than_2_false() {
112 let hint_code = hint_code::NONDET_N_GREATER_THAN_2;
113 let mut vm = vm!();
114 vm.set_ap(3);
115 vm.segments = segments![((1, 0), 1)];
116 vm.set_fp(1);
117 let ids_data = ids_data!("n");
118 assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(()));
119 check_memory![vm.segments.memory, ((1, 3), 0)];
121 }
122}