Skip to content

Commit

Permalink
Merge branch 'main' into deserialize-scientific-notation
Browse files Browse the repository at this point in the history
  • Loading branch information
pefontana committed May 30, 2023
2 parents d7e1fe5 + c06402c commit c12e7fd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
#### Upcoming Changes
* fix: Handle the deserialization of serde_json::Number with scientific notation (e.g.: Number(1e27)) in felt_from_number function [#1188](https://github.com/lambdaclass/cairo-rs/pull/1188)

* fix: Fix hint `BIGINT_PACK_DIV_MOD` [#1189](https://github.com/lambdaclass/cairo-rs/pull/1189)
* bugfix: Use cairo constants in `ASSERT_250_BIT` hint [#1187](https://github.com/lambdaclass/cairo-rs/pull/1187)

* bugfix: Fix `EC_DOUBLE_ASSIGN_NEW_X_V2` hint not taking `SECP_P` value from the current execution scope [#1186](https://github.com/lambdaclass/cairo-rs/pull/1186)

* fix: Fix `EC_DOUBLE_ASSIGN_NEW_X_V2` hint not taking `SECP_P` value from the current execution scope [#1186](https://github.com/lambdaclass/cairo-rs/pull/1186)
* fix: Fix hint `BIGINT_PACK_DIV_MOD` [#1189](https://github.com/lambdaclass/cairo-rs/pull/1189)

* fix: Fix possible subtraction overflow in `QUAD_BIT` & `DI_BIT` hints [#1185](https://github.com/lambdaclass/cairo-rs/pull/1185)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ impl HintProcessor for BuiltinHintProcessor {
hint_code::ASSERT_LE_FELT_EXCLUDED_0 => assert_le_felt_excluded_0(vm, exec_scopes),
hint_code::IS_LE_FELT => is_le_felt(vm, &hint_data.ids_data, &hint_data.ap_tracking),
hint_code::ASSERT_250_BITS => {
assert_250_bit(vm, &hint_data.ids_data, &hint_data.ap_tracking)
assert_250_bit(vm, &hint_data.ids_data, &hint_data.ap_tracking, constants)
}
hint_code::IS_250_BITS => is_250_bits(vm, &hint_data.ids_data, &hint_data.ap_tracking),
hint_code::IS_ADDR_BOUNDED => {
Expand Down
41 changes: 31 additions & 10 deletions src/hint_processor/builtin_hint_processor/math_utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::stdlib::{
collections::HashMap,
ops::{Shl, Shr},
prelude::*,
use crate::{
hint_processor::builtin_hint_processor::hint_utils::get_constant_from_var_name,
stdlib::{
collections::HashMap,
ops::{Shl, Shr},
prelude::*,
},
};
use lazy_static::lazy_static;
use num_traits::{Bounded, Pow};
Expand Down Expand Up @@ -550,17 +553,24 @@ pub fn assert_250_bit(
vm: &mut VirtualMachine,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
const UPPER_BOUND: &str = "starkware.cairo.common.math.assert_250_bit.UPPER_BOUND";
const SHIFT: &str = "starkware.cairo.common.math.assert_250_bit.SHIFT";
//Declare constant values
let upper_bound = Felt252::one().shl(250u32);
let shift = Felt252::one().shl(128u32);
let upper_bound = constants
.get(UPPER_BOUND)
.map_or_else(|| get_constant_from_var_name("UPPER_BOUND", constants), Ok)?;
let shift = constants
.get(SHIFT)
.map_or_else(|| get_constant_from_var_name("SHIFT", constants), Ok)?;
let value = get_integer_from_var_name("value", vm, ids_data, ap_tracking)?;
//Main logic
//can be deleted
if value.as_ref() > &upper_bound {
if value.as_ref() > upper_bound {
return Err(HintError::ValueOutside250BitRange(value.into_owned()));
}
let (high, low) = value.div_rem(&shift);
let (high, low) = value.div_rem(shift);
insert_value_from_var_name("high", high, vm, ids_data, ap_tracking)?;
insert_value_from_var_name("low", low, vm, ids_data, ap_tracking)
}
Expand Down Expand Up @@ -1887,6 +1897,10 @@ mod tests {
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn run_assert_250_bit_valid() {
let hint_code = hint_code::ASSERT_250_BITS;
let constants = HashMap::from([
("UPPER_BOUND".to_string(), Felt252::from(15)),
("SHIFT".to_string(), Felt252::from(5)),
]);
let mut vm = vm!();
//Initialize fp
vm.run_context.fp = 3;
Expand All @@ -1895,7 +1909,10 @@ mod tests {
//Create ids
let ids_data = ids_data!["value", "high", "low"];
//Execute the hint
assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(()));
assert_matches!(
run_hint!(vm, ids_data, hint_code, &mut exec_scopes_ref!(), &constants),
Ok(())
);
//Hint would return an error if the assertion fails
//Check ids.high and ids.low values
check_memory![vm.segments.memory, ((1, 1), 0), ((1, 2), 1)];
Expand All @@ -1905,6 +1922,10 @@ mod tests {
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn run_assert_250_bit_invalid() {
let hint_code = hint_code::ASSERT_250_BITS;
let constants = HashMap::from([
("UPPER_BOUND".to_string(), Felt252::from(15)),
("SHIFT".to_string(), Felt252::from(5)),
]);
let mut vm = vm!();
//Initialize fp
vm.run_context.fp = 3;
Expand All @@ -1921,7 +1942,7 @@ mod tests {
let ids_data = ids_data!["value", "high", "low"];
//Execute the hint
assert_matches!(
run_hint!(vm, ids_data, hint_code),
run_hint!(vm, ids_data, hint_code, &mut exec_scopes_ref!(), &constants),
Err(HintError::ValueOutside250BitRange(x)) if x == Felt252::one().shl(251_u32)
);
}
Expand Down

0 comments on commit c12e7fd

Please sign in to comment.