Skip to content

Commit

Permalink
Merge branch 'main' of github.com:lambdaclass/cairo-rs into fix-bigin…
Browse files Browse the repository at this point in the history
…t-pack-divmod
  • Loading branch information
fmoletta committed May 30, 2023
2 parents 55106e3 + 16db8ee commit d7f6496
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 42 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

* fix: Fix hint `BIGINT_PACK_DIV_MOD` [#1189](https://github.com/lambdaclass/cairo-rs/pull/1189)

* 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 possible subtraction overflow in `QUAD_BIT` & `DI_BIT` hints [#1185](https://github.com/lambdaclass/cairo-rs/pull/1185)

* These hints now return an error when ids.m equals zero

* Add `CairoRunner::run_until_pc_with_steps_limit method` [#1181](https://github.com/lambdaclass/cairo-rs/pull/1181)

* fix: felt_from_number not properly returning parse errors [#1012](https://github.com/lambdaclass/cairo-rs/pull/1012)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ use super::{
pack::*,
},
};
use crate::hint_processor::builtin_hint_processor::secp::ec_utils::ec_double_assign_new_x;
use crate::hint_processor::builtin_hint_processor::secp::ec_utils::{
ec_double_assign_new_x, ec_double_assign_new_x_v2,
};
use crate::{
hint_processor::{
builtin_hint_processor::{
Expand Down Expand Up @@ -552,16 +554,21 @@ impl HintProcessor for BuiltinHintProcessor {
"pt1",
&SECP_P,
),
hint_code::EC_DOUBLE_ASSIGN_NEW_X_V1 | hint_code::EC_DOUBLE_ASSIGN_NEW_X_V2 => {
ec_double_assign_new_x(
vm,
exec_scopes,
&hint_data.ids_data,
&hint_data.ap_tracking,
&SECP_P,
"point",
)
}
hint_code::EC_DOUBLE_ASSIGN_NEW_X_V1 => ec_double_assign_new_x(
vm,
exec_scopes,
&hint_data.ids_data,
&hint_data.ap_tracking,
&SECP_P,
"point",
),
hint_code::EC_DOUBLE_ASSIGN_NEW_X_V2 => ec_double_assign_new_x_v2(
vm,
exec_scopes,
&hint_data.ids_data,
&hint_data.ap_tracking,
"point",
),
hint_code::EC_DOUBLE_ASSIGN_NEW_X_V3 => ec_double_assign_new_x(
vm,
exec_scopes,
Expand Down
162 changes: 131 additions & 31 deletions src/hint_processor/builtin_hint_processor/secp/ec_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,17 @@ pub fn square_slope_minus_xs(
Ok(())
}

pub fn ec_double_assign_new_x_v2(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
point_alias: &str,
) -> Result<(), HintError> {
let secp_p: BigInt = exec_scopes.get("SECP_P")?;
ec_double_assign_new_x(vm, exec_scopes, ids_data, ap_tracking, &secp_p, point_alias)
}

/*
Implements hint:
%{
Expand Down Expand Up @@ -487,6 +498,9 @@ pub fn n_pair_bits(
if m >= 253 {
return insert_value_from_var_name("quad_bit", 0, vm, ids_data, ap_tracking);
}
if m.is_zero() {
return Err(HintError::NPairBitsMZero);
}

let one = &Felt252::one();
let two = &Felt252::from(2);
Expand Down Expand Up @@ -909,36 +923,100 @@ mod tests {
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn run_ec_double_assign_new_x_ok() {
let hint_codes = vec!["from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack\n\nslope = pack(ids.slope, PRIME)\nx = pack(ids.point.x, PRIME)\ny = pack(ids.point.y, PRIME)\n\nvalue = new_x = (pow(slope, 2, SECP_P) - 2 * x) % SECP_P", "from starkware.cairo.common.cairo_secp.secp_utils import pack\n\nslope = pack(ids.slope, PRIME)\nx = pack(ids.point.x, PRIME)\ny = pack(ids.point.y, PRIME)\n\nvalue = new_x = (pow(slope, 2, SECP_P) - 2 * x) % SECP_P"];

for hint_code in hint_codes {
let mut vm = vm_with_range_check!();

//Insert ids.point and ids.slope into memory
vm.segments = segments![
((1, 0), 134),
((1, 1), 5123),
((1, 2), 140),
((1, 3), 1232),
((1, 4), 4652),
((1, 5), 720),
((1, 6), 44186171158942157784255469_i128),
((1, 7), 54173758974262696047492534_i128),
((1, 8), 8106299688661572814170174_i128)
];

//Initialize fp
vm.run_context.fp = 10;
let ids_data = HashMap::from([
("point".to_string(), HintReference::new_simple(-10)),
("slope".to_string(), HintReference::new_simple(-4)),
]);
let mut exec_scopes = ExecutionScopes::new();

//Execute the hint
assert_matches!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(()));

check_scope!(
let hint_code = hint_code::EC_DOUBLE_ASSIGN_NEW_X_V1;

let mut vm = vm_with_range_check!();

//Insert ids.point and ids.slope into memory
vm.segments = segments![
((1, 0), 134),
((1, 1), 5123),
((1, 2), 140),
((1, 3), 1232),
((1, 4), 4652),
((1, 5), 720),
((1, 6), 44186171158942157784255469_i128),
((1, 7), 54173758974262696047492534_i128),
((1, 8), 8106299688661572814170174_i128)
];

//Initialize fp
vm.run_context.fp = 10;
let ids_data = HashMap::from([
("point".to_string(), HintReference::new_simple(-10)),
("slope".to_string(), HintReference::new_simple(-4)),
]);
let mut exec_scopes = ExecutionScopes::new();

//Execute the hint
assert_matches!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(()));

check_scope!(
&exec_scopes,
[
(
"slope",
bigint_str!(
"48526828616392201132917323266456307435009781900148206102108934970258721901549"
)
),
(
"x",
bigint_str!("838083498911032969414721426845751663479194726707495046")
),
(
"y",
bigint_str!("4310143708685312414132851373791311001152018708061750480")
),
(
"value",
bigint_str!(
"59479631769792988345961122678598249997181612138456851058217178025444564264149"
)
),
(
"new_x",
bigint_str!(
"59479631769792988345961122678598249997181612138456851058217178025444564264149"
)
)
]
);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn run_ec_double_assign_new_x_v2_ok() {
let hint_code = hint_code::EC_DOUBLE_ASSIGN_NEW_X_V2;

let mut vm = vm_with_range_check!();

//Insert ids.point and ids.slope into memory
vm.segments = segments![
((1, 0), 134),
((1, 1), 5123),
((1, 2), 140),
((1, 3), 1232),
((1, 4), 4652),
((1, 5), 720),
((1, 6), 44186171158942157784255469_i128),
((1, 7), 54173758974262696047492534_i128),
((1, 8), 8106299688661572814170174_i128)
];

//Initialize fp
vm.run_context.fp = 10;
let ids_data = HashMap::from([
("point".to_string(), HintReference::new_simple(-10)),
("slope".to_string(), HintReference::new_simple(-4)),
]);
let mut exec_scopes = ExecutionScopes::new();
exec_scopes.assign_or_update_variable("SECP_P", any_box!(SECP_P.clone()));

//Execute the hint
assert_matches!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(()));

check_scope!(
&exec_scopes,
[
(
Expand Down Expand Up @@ -969,7 +1047,6 @@ mod tests {
)
]
);
}
}

#[test]
Expand Down Expand Up @@ -1281,6 +1358,29 @@ mod tests {
check_memory![vm.segments.memory, ((1, 3), 2)];
}

#[test]
fn run_di_bit_m_zero() {
let hint_code = hint_code::DI_BIT;
let mut vm = vm_with_range_check!();

let scalar_u = 0b10101111001110000;
let scalar_v = 0b101101000111011111100;
let m = 0;
// Insert ids.scalar into memory
vm.segments = segments![((1, 0), scalar_u), ((1, 1), scalar_v), ((1, 2), m)];

// Initialize RunContext
run_context!(vm, 0, 4, 4);

let ids_data = ids_data!["scalar_u", "scalar_v", "m", "dibit"];

// Execute the hint
assert_matches!(
run_hint!(vm, ids_data, hint_code),
Err(HintError::NPairBitsMZero)
);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn run_import_secp256r1_alpha() {
Expand Down
2 changes: 2 additions & 0 deletions src/vm/errors/hint_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,6 @@ pub enum HintError {
RecoverYPointNotOnCurve(Felt252),
#[error("Invalid value for {0}. Got: {1}. Expected: {2}")]
InvalidValue(&'static str, Felt252, Felt252),
#[error("Attempt to subtract with overflow: ids.m - 1")]
NPairBitsMZero,
}

0 comments on commit d7f6496

Please sign in to comment.