Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(hints): Implement NewHint#38 #1036

Merged
merged 43 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
17716c4
Add test
fmoletta Apr 20, 2023
e867c2b
Ignore builtin segments in get_memory_holes
fmoletta Apr 20, 2023
c560497
Improve test
fmoletta Apr 20, 2023
ed5329f
Add memory holes check for cairo_run_test tests
fmoletta Apr 20, 2023
c15445b
Fix broken condition
fmoletta Apr 20, 2023
f252960
Make memory_holes check optional & customizable
fmoletta Apr 20, 2023
063f9c4
Fix bounds
fmoletta Apr 20, 2023
6e3ed8b
Add a test with deliberately created memory holes
fmoletta Apr 20, 2023
9fa2dc8
Fix test value
fmoletta Apr 20, 2023
d36bf3f
Remove duplicated tests + add memory hole value to some tests
fmoletta Apr 20, 2023
ece99eb
Add memory holes value + remove duplicated tests + fix typo
fmoletta Apr 20, 2023
1f867fb
Fix test values
fmoletta Apr 20, 2023
ac9c795
Add changelog entry
fmoletta Apr 20, 2023
356a187
Link PR in Changelog
fmoletta Apr 20, 2023
0f89514
Merge branch 'main' of github.com:lambdaclass/cairo-rs into fix-get-m…
fmoletta Apr 20, 2023
67e2622
Mark breaking change in changelog
fmoletta Apr 21, 2023
4a17d58
Merge branch 'main' of github.com:lambdaclass/cairo-rs into fix-get-m…
fmoletta Apr 21, 2023
e114745
fmt
fmoletta Apr 21, 2023
3ecf882
Fix test value
fmoletta Apr 21, 2023
342efdb
Fix codecov-patch diff
fmoletta Apr 21, 2023
67e3b70
Add hint code
fmoletta Apr 21, 2023
4779307
Add integration test
fmoletta Apr 21, 2023
cbe3b5d
Add untracked file
fmoletta Apr 21, 2023
81a99e5
Merge branch 'main' into div-mod-n-packed-hint
fmoletta Apr 21, 2023
a71552d
Add changelog entry
fmoletta Apr 21, 2023
75bb911
Rename hint
fmoletta Apr 21, 2023
53ade42
Merge branch 'main' into div-mod-n-packed-hint
fmoletta Apr 21, 2023
23c7d8f
Fix test
fmoletta Apr 21, 2023
992c7fe
Fix test
fmoletta Apr 21, 2023
b434052
Add hint code + hint impl + unit test
fmoletta Apr 21, 2023
ad46d43
fmt
fmoletta Apr 21, 2023
c078fc2
Add integration test
fmoletta Apr 21, 2023
ad87b73
Add changelog entry
fmoletta Apr 21, 2023
86f7094
Merge branch 'main' into new-hint-38
fmoletta Apr 21, 2023
d183b6b
Remove indent from hint string
fmoletta Apr 21, 2023
535f9c8
Merge branch 'div-mod-n-packed-hint' into new-hint-38
fmoletta Apr 21, 2023
6cd45c2
Add wasm import
fmoletta Apr 21, 2023
7316d57
Merge branch 'div-mod-n-packed-hint' into new-hint-38
fmoletta Apr 21, 2023
f960abb
Merge branch 'main' of github.com:lambdaclass/cairo-rs into div-mod-n…
fmoletta Apr 21, 2023
3d529a7
Merge branch 'main' of github.com:lambdaclass/cairo-rs into div-mod-n…
fmoletta Apr 24, 2023
b93125c
Merge branch 'main' into div-mod-n-packed-hint
fmoletta Apr 24, 2023
a60f26d
Merge branch 'div-mod-n-packed-hint' into new-hint-38
fmoletta Apr 24, 2023
44fd38e
Merge branch 'main' into new-hint-38
fmoletta Apr 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,39 @@

#### Upcoming Changes

* Implement hint on ec_recover.json whitelist [#1036](https://github.com/lambdaclass/cairo-rs/pull/1036):

`BuiltinHintProcessor` now supports the following hint:

```python

%{
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)
b = pack(ids.b, PRIME)
value = res = a - b
%}

```

* Implement hint on ec_recover.json whitelist [#1032](https://github.com/lambdaclass/cairo-rs/pull/1032):

`BuiltinHintProcessor` now supports the following hint:

```python
%{
from starkware.cairo.common.cairo_secp.secp_utils import pack
from starkware.python.math_utils import div_mod, safe_div

N = pack(ids.n, PRIME)
x = pack(ids.x, PRIME) % N
s = pack(ids.s, PRIME) % N,
value = res = div_mod(x, s, N)
%}
```

* BREAKING CHANGE: Fix `CairoRunner::get_memory_holes` [#1027](https://github.com/lambdaclass/cairo-rs/pull/1027):

* Skip builtin segements when counting memory holes
Expand Down
50 changes: 50 additions & 0 deletions cairo_programs/ec_recover.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
%builtins range_check
from starkware.cairo.common.cairo_secp.bigint import BigInt3, nondet_bigint3

func test_div_mod_n_packed_hint{range_check_ptr: felt}() {

tempvar n = BigInt3(177, 0, 0);
tempvar x = BigInt3(25, 0, 0);
tempvar s = BigInt3(5, 0, 0);

%{
from starkware.cairo.common.cairo_secp.secp_utils import pack
from starkware.python.math_utils import div_mod, safe_div

N = pack(ids.n, PRIME)
x = pack(ids.x, PRIME) % N
s = pack(ids.s, PRIME) % N
value = res = div_mod(x, s, N)
%}

let (res) = nondet_bigint3();
assert res = BigInt3(5,0,0);

return();
}

func test_sub_a_b_hint{range_check_ptr: felt}() {

tempvar a = BigInt3(100, 0, 0);
tempvar b = BigInt3(25, 0, 0);

%{
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)
b = pack(ids.b, PRIME)
value = res = a - b
%}

let (res) = nondet_bigint3();
assert res = BigInt3(75,0,0);

return();
}

func main{range_check_ptr: felt}() {
test_div_mod_n_packed_hint();
test_sub_a_b_hint();
return();
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ use felt::Felt252;
#[cfg(feature = "skip_next_instruction_hint")]
use crate::hint_processor::builtin_hint_processor::skip_next_instruction::skip_next_instruction;

use super::ec_recover::{ec_recover_divmod_n_packed, ec_recover_sub_a_b};

pub struct HintProcessorData {
pub code: String,
pub ap_tracking: ApTracking,
Expand Down Expand Up @@ -568,6 +570,15 @@ impl HintProcessor for BuiltinHintProcessor {
}
hint_code::QUAD_BIT => quad_bit(vm, &hint_data.ids_data, &hint_data.ap_tracking),
hint_code::DI_BIT => di_bit(vm, &hint_data.ids_data, &hint_data.ap_tracking),
hint_code::EC_RECOVER_DIV_MOD_N_PACKED => ec_recover_divmod_n_packed(
vm,
exec_scopes,
&hint_data.ids_data,
&hint_data.ap_tracking,
),
hint_code::EC_RECOVER_SUB_A_B => {
ec_recover_sub_a_b(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking)
}
#[cfg(feature = "skip_next_instruction_hint")]
hint_code::SKIP_NEXT_INSTRUCTION => skip_next_instruction(vm),
code => Err(HintError::UnknownHint(code.to_string())),
Expand Down
161 changes: 161 additions & 0 deletions src/hint_processor/builtin_hint_processor/ec_recover.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
use super::secp::{bigint_utils::BigInt3, secp_utils::pack};
use crate::stdlib::{collections::HashMap, prelude::*};
use crate::{
hint_processor::hint_processor_definition::HintReference,
math_utils::div_mod,
serde::deserialize_program::ApTracking,
types::exec_scope::ExecutionScopes,
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine},
};
use num_integer::Integer;

/* Implements Hint:
%{
from starkware.cairo.common.cairo_secp.secp_utils import pack
from starkware.python.math_utils import div_mod, safe_div

N = pack(ids.n, PRIME)
x = pack(ids.x, PRIME) % N
s = pack(ids.s, PRIME) % N,
value = res = div_mod(x, s, N)
%}
*/
pub fn ec_recover_divmod_n_packed(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
) -> Result<(), HintError> {
let n = pack(BigInt3::from_var_name("n", vm, ids_data, ap_tracking)?);
let x = pack(BigInt3::from_var_name("x", vm, ids_data, ap_tracking)?).mod_floor(&n);
let s = pack(BigInt3::from_var_name("s", vm, ids_data, ap_tracking)?).mod_floor(&n);

let value = div_mod(&x, &s, &n);
exec_scopes.insert_value("value", value.clone());
exec_scopes.insert_value("res", value);
Ok(())
}

/* Implements Hint:
%{
from starkware.cairo.common.cairo_secp.secp_utils import pack
from starkware.python.math_utils import div_mod, safe_div

a = pack(ids.x, PRIME)
b = pack(ids.s, PRIME)
value = res = a - b
%}
*/
pub fn ec_recover_sub_a_b(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
) -> Result<(), HintError> {
let a = pack(BigInt3::from_var_name("a", vm, ids_data, ap_tracking)?);
let b = pack(BigInt3::from_var_name("b", vm, ids_data, ap_tracking)?);

let value = a - b;
exec_scopes.insert_value("value", value.clone());
exec_scopes.insert_value("res", value);
Ok(())
}

#[cfg(test)]
mod tests {
use num_bigint::BigInt;

use super::*;
use crate::hint_processor::builtin_hint_processor::hint_code;
use crate::hint_processor::hint_processor_definition::HintReference;
use crate::utils::test_utils::*;
use crate::vm::errors::memory_errors::MemoryError;
use crate::vm::vm_core::VirtualMachine;
use crate::vm::vm_memory::memory::Memory;
use crate::vm::vm_memory::memory_segments::MemorySegmentManager;
use crate::{
any_box,
hint_processor::{
builtin_hint_processor::builtin_hint_processor_definition::{
BuiltinHintProcessor, HintProcessorData,
},
hint_processor_definition::HintProcessor,
},
types::{exec_scope::ExecutionScopes, relocatable::MaybeRelocatable},
};

#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*;

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn run_ec_recover_divmod_n_packed_ok() {
let mut vm = vm!();
let mut exec_scopes = ExecutionScopes::new();

vm.run_context.fp = 8;
let ids_data = non_continuous_ids_data![("n", -8), ("x", -5), ("s", -2)];

vm.segments = segments![
//n
((1, 0), 177),
((1, 1), 0),
((1, 2), 0),
//x
((1, 3), 25),
((1, 4), 0),
((1, 5), 0),
//s
((1, 6), 5),
((1, 7), 0),
((1, 8), 0)
];

assert!(run_hint!(
vm,
ids_data,
hint_code::EC_RECOVER_DIV_MOD_N_PACKED,
&mut exec_scopes
)
.is_ok());

check_scope!(
&exec_scopes,
[("value", BigInt::from(5)), ("res", BigInt::from(5))]
);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn run_ec_recover_sub_a_b_ok() {
let mut vm = vm!();
let mut exec_scopes = ExecutionScopes::new();

vm.run_context.fp = 8;
let ids_data = non_continuous_ids_data![("a", -8), ("b", -5)];

vm.segments = segments![
//a
((1, 0), 100),
((1, 1), 0),
((1, 2), 0),
//b
((1, 3), 25),
((1, 4), 0),
((1, 5), 0),
];

assert!(run_hint!(
vm,
ids_data,
hint_code::EC_RECOVER_SUB_A_B,
&mut exec_scopes
)
.is_ok());

check_scope!(
&exec_scopes,
[("value", BigInt::from(75)), ("res", BigInt::from(75))]
);
}
}
12 changes: 12 additions & 0 deletions src/hint_processor/builtin_hint_processor/hint_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,18 @@ pub const QUAD_BIT: &str = r#"ids.quad_bit = (

pub const DI_BIT: &str =
r#"ids.dibit = ((ids.scalar_u >> ids.m) & 1) + 2 * ((ids.scalar_v >> ids.m) & 1)"#;
pub const EC_RECOVER_DIV_MOD_N_PACKED: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import pack
from starkware.python.math_utils import div_mod, safe_div

N = pack(ids.n, PRIME)
x = pack(ids.x, PRIME) % N
s = pack(ids.s, PRIME) % N
value = res = div_mod(x, s, N)"#;
pub const EC_RECOVER_SUB_A_B: &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)
b = pack(ids.b, PRIME)
value = res = a - b"#;
#[cfg(feature = "skip_next_instruction_hint")]
pub 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 @@ -4,6 +4,7 @@ pub mod builtin_hint_processor_definition;
pub mod cairo_keccak;
pub mod dict_hint_utils;
pub mod dict_manager;
pub mod ec_recover;
pub mod ec_utils;
pub mod field_arithmetic;
pub mod find_element_hint;
Expand Down
7 changes: 7 additions & 0 deletions src/tests/cairo_run_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -798,3 +798,10 @@ fn memory_holes() {
let program_data = include_bytes!("../../cairo_programs/memory_holes.json");
run_program_simple_with_memory_holes(program_data.as_slice(), 5)
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn ec_recover() {
let program_data = include_bytes!("../../cairo_programs/ec_recover.json");
run_program_simple(program_data.as_slice());
}