Skip to content

Commit

Permalink
Add NewHint#24
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaRedHand committed Apr 17, 2023
1 parent 2ecda0c commit e3bc05a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 26 deletions.
7 changes: 5 additions & 2 deletions cairo_programs/div_mod_n.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from starkware.cairo.common.cairo_secp.bigint import BigInt3, nondet_bigint3, BASE, bigint_mul
from starkware.cairo.common.cairo_secp.constants import BETA, N0, N1, N2

// Source: https://github.com/myBraavos/efficient-secp256r1/blob/73cca4d53730cb8b2dcf34e36c7b8f34b96b3230/src/secp256r1/signature.cairo

// Computes a * b^(-1) modulo the size of the elliptic curve (N).
//
// Prover assumptions:
Expand Down Expand Up @@ -103,9 +105,10 @@ func test_div_mod_n{range_check_ptr: felt}() {
3413472211745629263979533, 17305268010345238170172332, 11991751872105858217578135
);

// let (res_alt) = div_mod_n_alt(a, b);
// test alternative hint
let (res_alt) = div_mod_n_alt(a, b);

// assert res_alt = res;
assert res_alt = res;

return ();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,14 @@ impl HintProcessor for BuiltinHintProcessor {
}
hint_code::IS_ZERO_NONDET => is_zero_nondet(vm, exec_scopes),
hint_code::IS_ZERO_ASSIGN_SCOPE_VARS => is_zero_assign_scope_variables(exec_scopes),
hint_code::DIV_MOD_N_PACKED_DIVMOD => div_mod_n_packed_divmod(
vm,
exec_scopes,
&hint_data.ids_data,
&hint_data.ap_tracking,
),
hint_code::DIV_MOD_N_PACKED_DIVMOD_V1 | hint_code::DIV_MOD_N_PACKED_DIVMOD_V2 => {
div_mod_n_packed_divmod(
vm,
exec_scopes,
&hint_data.ids_data,
&hint_data.ap_tracking,
)
}
hint_code::DIV_MOD_N_SAFE_DIV => div_mod_n_safe_div(exec_scopes, 0),
hint_code::DIV_MOD_N_SAFE_DIV_PLUS_ONE => div_mod_n_safe_div(exec_scopes, 1),
hint_code::GET_POINT_FROM_X => get_point_from_x(
Expand Down
9 changes: 8 additions & 1 deletion src/hint_processor/builtin_hint_processor/hint_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,14 @@ from starkware.python.math_utils import div_mod
value = x_inv = div_mod(1, x, SECP_P)"#;

pub(crate) const DIV_MOD_N_PACKED_DIVMOD: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import N, pack
pub(crate) const DIV_MOD_N_PACKED_DIVMOD_V1: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import N, pack
from starkware.python.math_utils import div_mod, safe_div
a = pack(ids.a, PRIME)
b = pack(ids.b, PRIME)
value = res = div_mod(a, b, N)"#;

pub(crate) const DIV_MOD_N_PACKED_DIVMOD_V2: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import pack
from starkware.python.math_utils import div_mod, safe_div
a = pack(ids.a, PRIME)
Expand Down
39 changes: 22 additions & 17 deletions src/hint_processor/builtin_hint_processor/secp/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,24 +134,29 @@ mod tests {
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn safe_div_ok() {
let hint_code = hint_code::DIV_MOD_N_PACKED_DIVMOD;
let mut vm = vm!();

vm.segments = segments![
((1, 0), 15),
((1, 1), 3),
((1, 2), 40),
((1, 3), 0),
((1, 4), 10),
((1, 5), 1)
let hint_codes = vec![
hint_code::DIV_MOD_N_PACKED_DIVMOD_V1,
hint_code::DIV_MOD_N_PACKED_DIVMOD_V2,
];
vm.run_context.fp = 3;
let ids_data = non_continuous_ids_data![("a", -3), ("b", 0)];
let mut exec_scopes = ExecutionScopes::new();
assert_matches!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(()));

assert_matches!(div_mod_n_safe_div(&mut exec_scopes, 0), Ok(()));
assert_matches!(div_mod_n_safe_div(&mut exec_scopes, 1), Ok(()));
for hint_code in hint_codes {
let mut vm = vm!();

vm.segments = segments![
((1, 0), 15),
((1, 1), 3),
((1, 2), 40),
((1, 3), 0),
((1, 4), 10),
((1, 5), 1)
];
vm.run_context.fp = 3;
let ids_data = non_continuous_ids_data![("a", -3), ("b", 0)];
let mut exec_scopes = ExecutionScopes::new();
assert_matches!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(()));

assert_matches!(div_mod_n_safe_div(&mut exec_scopes, 0), Ok(()));
assert_matches!(div_mod_n_safe_div(&mut exec_scopes, 1), Ok(()));
}
}

#[test]
Expand Down

0 comments on commit e3bc05a

Please sign in to comment.