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): add NewHint#32 #1010

Merged
merged 8 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

#### Upcoming Changes

* Add missing hint on cairo_secp lib [#1010](https://github.com/lambdaclass/cairo-rs/pull/1010):

`BuiltinHintProcessor` now supports the following hint:

```python
memory[ap] = int(x == 0)
```

* BREAKING CHANGE: refactor `Program` to optimize `Program::clone` [#999](https://github.com/lambdaclass/cairo-rs/pull/999)
* Breaking change: many fields that were (unnecessarily) public become hidden by the refactor.

Expand Down
50 changes: 50 additions & 0 deletions cairo_programs/is_zero.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,34 @@ func is_zero{range_check_ptr}(x: BigInt3) -> (res: felt) {
return (res=0);
}

func is_zero_alt{range_check_ptr}(x: BigInt3) -> (res: felt) {
%{
from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack

x = pack(ids.x, PRIME) % SECP_P
%}
%{ memory[ap] = int(x == 0) %}
tempvar x_is_zero;

if (x_is_zero != 0) {
verify_zero(UnreducedBigInt3(d0=x.d0, d1=x.d1, d2=x.d2));
return (res=1);
}

%{
from starkware.cairo.common.cairo_secp.secp_utils import SECP_P
from starkware.python.math_utils import div_mod

value = x_inv = div_mod(1, x, SECP_P)
%}
let (x_inv) = nondet_bigint3();
let (x_x_inv) = unreduced_mul(x, x_inv);

// Check that x * x_inv = 1 to verify that x != 0.
verify_zero(UnreducedBigInt3(d0=x_x_inv.d0 - 1, d1=x_x_inv.d1, d2=x_x_inv.d2));
return (res=0);
}

func test_is_zero{range_check_ptr}() -> () {
let zero = BigInt3(0, 0, 0);

Expand All @@ -52,8 +80,30 @@ func test_is_zero{range_check_ptr}() -> () {
return ();
}

func test_is_zero_alt{range_check_ptr}() -> () {
let zero = BigInt3(0, 0, 0);

let (res: felt) = is_zero_alt(zero);
assert res = 1;

let one = BigInt3(1, 0, 0);

let (res: felt) = is_zero_alt(one);
assert res = 0;

let secp256k1_prime = BigInt3(
77371252455336262886226991, 77371252455336267181195263, 19342813113834066795298815
);

let (res: felt) = is_zero_alt(secp256k1_prime);
assert res = 1;

return ();
}

func main{range_check_ptr}() -> () {
test_is_zero();
test_is_zero_alt();

return ();
}
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ impl HintProcessor for BuiltinHintProcessor {
hint_code::IS_ZERO_PACK => {
is_zero_pack(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking)
}
hint_code::IS_ZERO_NONDET => is_zero_nondet(vm, exec_scopes),
hint_code::IS_ZERO_NONDET | hint_code::IS_ZERO_INT => is_zero_nondet(vm, exec_scopes),
hint_code::IS_ZERO_ASSIGN_SCOPE_VARS => is_zero_assign_scope_variables(exec_scopes),
hint_code::IS_ZERO_ASSIGN_SCOPE_VARS_EXTERNAL_SECP => {
is_zero_assign_scope_variables_external_const(exec_scopes)
Expand Down
1 change: 1 addition & 0 deletions src/hint_processor/builtin_hint_processor/hint_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ ids.high = int.from_bytes(hashed[:16], 'big')
ids.low = int.from_bytes(hashed[16:32], 'big')"#;

pub const IS_ZERO_NONDET: &str = "memory[ap] = to_felt_or_relocatable(x == 0)";
pub const IS_ZERO_INT: &str = "memory[ap] = int(x == 0)";
pub const IS_ZERO_PACK: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack

x = pack(ids.x, PRIME) % SECP_P"#;
Expand Down
147 changes: 77 additions & 70 deletions src/hint_processor/builtin_hint_processor/secp/field_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,107 +432,114 @@ mod tests {
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn run_is_zero_nondet_ok_true() {
let hint_code = "memory[ap] = to_felt_or_relocatable(x == 0)";
let mut vm = vm_with_range_check!();
let hint_codes = vec![hint_code::IS_ZERO_NONDET, hint_code::IS_ZERO_INT];
for hint_code in hint_codes {
MegaRedHand marked this conversation as resolved.
Show resolved Hide resolved
let mut vm = vm_with_range_check!();

//Initialize memory
add_segments!(vm, 2);
//Initialize memory
add_segments!(vm, 2);

//Initialize ap
vm.run_context.ap = 15;
//Initialize ap
vm.run_context.ap = 15;

let mut exec_scopes = ExecutionScopes::new();
//Initialize vm scope with variable `x`
exec_scopes.assign_or_update_variable("x", any_box!(BigInt::zero()));
//Create hint data
//Execute the hint
assert_matches!(
run_hint!(vm, HashMap::new(), hint_code, &mut exec_scopes),
Ok(())
);
let mut exec_scopes = ExecutionScopes::new();
//Initialize vm scope with variable `x`
exec_scopes.assign_or_update_variable("x", any_box!(BigInt::zero()));
//Create hint data
//Execute the hint
assert_matches!(
run_hint!(vm, HashMap::new(), hint_code, &mut exec_scopes),
Ok(())
);

//Check hint memory insert
//memory[ap] = to_felt_or_relocatable(x == 0)
check_memory!(vm.segments.memory, ((1, 15), 1));
//Check hint memory insert
//memory[ap] = to_felt_or_relocatable(x == 0)
check_memory!(vm.segments.memory, ((1, 15), 1));
}
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn run_is_zero_nondet_ok_false() {
let hint_code = "memory[ap] = to_felt_or_relocatable(x == 0)";
let mut vm = vm_with_range_check!();
let hint_codes = vec![hint_code::IS_ZERO_NONDET, hint_code::IS_ZERO_INT];
for hint_code in hint_codes {
let mut vm = vm_with_range_check!();

//Initialize memory
add_segments!(vm, 2);
//Initialize memory
add_segments!(vm, 2);

//Initialize ap
vm.run_context.ap = 15;
//Initialize ap
vm.run_context.ap = 15;

//Initialize vm scope with variable `x`
let mut exec_scopes = ExecutionScopes::new();
exec_scopes.assign_or_update_variable("x", any_box!(bigint!(123890i32)));
//Initialize vm scope with variable `x`
let mut exec_scopes = ExecutionScopes::new();
exec_scopes.assign_or_update_variable("x", any_box!(bigint!(123890i32)));

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

//Check hint memory insert
//memory[ap] = to_felt_or_relocatable(x == 0)
check_memory!(vm.segments.memory, ((1, 15), 0));
//Check hint memory insert
//memory[ap] = to_felt_or_relocatable(x == 0)
check_memory!(vm.segments.memory, ((1, 15), 0));
}
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn run_is_zero_nondet_scope_error() {
let hint_code = "memory[ap] = to_felt_or_relocatable(x == 0)";
let mut vm = vm_with_range_check!();
let hint_codes = vec![hint_code::IS_ZERO_NONDET, hint_code::IS_ZERO_INT];
for hint_code in hint_codes {
let mut vm = vm_with_range_check!();

//Initialize memory
add_segments!(vm, 2);
//Initialize memory
add_segments!(vm, 2);

//Initialize ap
vm.run_context.ap = 15;
//Initialize ap
vm.run_context.ap = 15;

//Skip `x` assignment
//Skip `x` assignment

//Execute the hint
assert_matches!(
run_hint!(vm, HashMap::new(), hint_code),
Err(HintError::VariableNotInScopeError(x)) if x == *"x".to_string()
);
//Execute the hint
assert_matches!(
run_hint!(vm, HashMap::new(), hint_code),
Err(HintError::VariableNotInScopeError(x)) if x == *"x".to_string()
);
}
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn run_is_zero_nondet_invalid_memory_insert() {
let hint_code = "memory[ap] = to_felt_or_relocatable(x == 0)";
let mut vm = vm_with_range_check!();
let hint_codes = vec![hint_code::IS_ZERO_NONDET, hint_code::IS_ZERO_INT];
for hint_code in hint_codes {
let mut vm = vm_with_range_check!();

//Insert a value in ap before the hint execution, so the hint memory insert fails
vm.segments = segments![((1, 15), 55)];
//Insert a value in ap before the hint execution, so the hint memory insert fails
vm.segments = segments![((1, 15), 55)];

//Initialize ap
vm.run_context.ap = 15;
//Initialize ap
vm.run_context.ap = 15;

//Initialize vm scope with variable `x`
let mut exec_scopes = ExecutionScopes::new();
exec_scopes.assign_or_update_variable("x", any_box!(BigInt::zero()));
//Execute the hint
assert_matches!(
run_hint!(vm, HashMap::new(), hint_code, &mut exec_scopes),
Err(HintError::Memory(
MemoryError::InconsistentMemory(
x,
y,
z
)
)) if x ==
vm.run_context.get_ap() &&
y == MaybeRelocatable::from(Felt252::new(55i32)) &&
z == MaybeRelocatable::from(Felt252::new(1i32))
);
//Initialize vm scope with variable `x`
let mut exec_scopes = ExecutionScopes::new();
exec_scopes.assign_or_update_variable("x", any_box!(BigInt::zero()));
//Execute the hint
assert_matches!(
run_hint!(vm, HashMap::new(), hint_code, &mut exec_scopes),
Err(HintError::Memory(
MemoryError::InconsistentMemory(
x,
y,
z
)
)) if x == vm.run_context.get_ap()
&& y == MaybeRelocatable::from(Felt252::new(55i32))
&& z == MaybeRelocatable::from(Felt252::new(1i32))
);
}
}

#[test]
Expand Down