Skip to content

Commit

Permalink
Add normalize address hints
Browse files Browse the repository at this point in the history
  • Loading branch information
fmoletta committed Apr 10, 2023
1 parent a206709 commit 86077f2
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 4 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ use felt::Felt252;
use crate::hint_processor::builtin_hint_processor::skip_next_instruction::skip_next_instruction;

use super::ec_utils::{chained_ec_op_random_ec_point_hint, random_ec_point_hint, recover_y_hint};
use super::normalize_address::{normalize_address_set_is_250, normalize_address_set_is_small};

pub struct HintProcessorData {
pub code: String,
Expand Down Expand Up @@ -449,6 +450,18 @@ impl HintProcessor for BuiltinHintProcessor {
chained_ec_op_random_ec_point_hint(vm, &hint_data.ids_data, &hint_data.ap_tracking)
}
hint_code::RECOVER_Y => recover_y_hint(vm, &hint_data.ids_data, &hint_data.ap_tracking),
hint_code::NORMALIZE_ADDRESS_SET_IS_250_HINT => normalize_address_set_is_250(
vm,
&hint_data.ids_data,
&hint_data.ap_tracking,
constants,
),
hint_code::NORMALIZE_ADDRESS_SET_IS_SMALL_HINT => normalize_address_set_is_small(
vm,
&hint_data.ids_data,
&hint_data.ap_tracking,
constants,
),
#[cfg(feature = "skip_next_instruction_hint")]
hint_code::SKIP_NEXT_INSTRUCTION => skip_next_instruction(vm),
code => Err(HintError::UnknownHint(code.to_string())),
Expand Down
8 changes: 8 additions & 0 deletions src/hint_processor/builtin_hint_processor/hint_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,5 +608,13 @@ from starkware.python.math_utils import recover_y
ids.p.x = ids.x
# This raises an exception if `x` is not on the curve.
ids.p.y = recover_y(ids.x, ALPHA, BETA, FIELD_PRIME)";
pub(crate) const NORMALIZE_ADDRESS_SET_IS_250_HINT: &str =
"ids.is_250 = 1 if ids.addr < 2**250 else 0";
pub(crate) const NORMALIZE_ADDRESS_SET_IS_SMALL_HINT: &str = r#"# Verify the assumptions on the relationship between 2**250, ADDR_BOUND and PRIME.
ADDR_BOUND = ids.ADDR_BOUND % PRIME
assert (2**250 < ADDR_BOUND <= 2**251) and (2 * 2**250 < PRIME) and (
ADDR_BOUND * 2 > PRIME), \
'normalize_address() cannot be used with the current constants.'
ids.is_small = 1 if ids.addr < ADDR_BOUND else 0"#;
#[cfg(feature = "skip_next_instruction_hint")]
pub(crate) const SKIP_NEXT_INSTRUCTION: &str = "skip_next_instruction()";
1 change: 1 addition & 0 deletions src/hint_processor/builtin_hint_processor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod keccak_utils;
pub mod math_utils;
pub mod memcpy_hint_utils;
pub mod memset_utils;
pub mod normalize_address;
pub mod poseidon_utils;
pub mod pow_utils;
pub mod secp;
Expand Down
68 changes: 68 additions & 0 deletions src/hint_processor/builtin_hint_processor/normalize_address.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use core::ops::Shl;

use crate::hint_processor::builtin_hint_processor::hint_utils::get_integer_from_var_name;
use crate::stdlib::{collections::HashMap, prelude::*};
use crate::utils::CAIRO_PRIME;
use crate::vm::errors::hint_errors::HintError;
use felt::Felt252;
use lazy_static::lazy_static;
use num_traits::One;

use crate::{
hint_processor::hint_processor_definition::HintReference,
serde::deserialize_program::ApTracking, vm::vm_core::VirtualMachine,
};

use super::hint_utils::insert_value_from_var_name;

const ADDR_BOUND: &str = "starkware.starknet.common.storage.ADDR_BOUND";
lazy_static! {
static ref HALF_PRIME: Felt252 = Felt252::from(&*CAIRO_PRIME / 2_u32);
}

/* Implements hint:
# Verify the assumptions on the relationship between 2**250, ADDR_BOUND and PRIME.
ADDR_BOUND = ids.ADDR_BOUND % PRIME
assert (2**250 < ADDR_BOUND <= 2**251) and (2 * 2**250 < PRIME) and (
ADDR_BOUND * 2 > PRIME), \
'normalize_address() cannot be used with the current constants.'
ids.is_small = 1 if ids.addr < ADDR_BOUND else 0"
*/
pub(crate) fn normalize_address_set_is_small(
vm: &mut VirtualMachine,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let addr_bound = constants
.get(ADDR_BOUND)
.ok_or(HintError::MissingConstant("ADDR_BOUND"))?;
let addr = get_integer_from_var_name("addr", vm, ids_data, ap_tracking)?;
if addr_bound <= &Felt252::one().shl(250_usize)
|| addr_bound > &Felt252::one().shl(251_usize)
|| addr_bound <= &*HALF_PRIME
{
return Err(HintError::AssertionFailed(format!(
"assert (2**250 < {} <= 2**251) and (2 * 2**250 < PRIME) and (
{} * 2 > PRIME); normalize_address() cannot be used with the current constants.",
addr_bound, addr_bound
)));
}

let is_small = Felt252::from((*addr < *addr_bound) as usize);
insert_value_from_var_name("is_small", is_small, vm, ids_data, ap_tracking)
}

/* Implements Hint:
ids.is_250 = 1 if ids.addr < 2**250 else 0
*/
pub(crate) fn normalize_address_set_is_250(
vm: &mut VirtualMachine,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let addr = get_integer_from_var_name("addr", vm, ids_data, ap_tracking)?;
let is_250 = Felt252::from((*addr < Felt252::one().shl(250_usize)) as usize);
insert_value_from_var_name("is_250", is_250, vm, ids_data, ap_tracking)
}

0 comments on commit 86077f2

Please sign in to comment.