Skip to content

Commit

Permalink
feat(hints): Add alternative hint string for nondet_bigint3 (#1071)
Browse files Browse the repository at this point in the history
* Add alternative hint string for nondet_bigint_3

* Add integration test

* Rename test file

* Add changelog entry

* Fix hint

* Update CHANGELOG.md

Co-authored-by: Tomás <47506558+MegaRedHand@users.noreply.github.com>

---------

Co-authored-by: Mario Rugiero <mario.rugiero@lambdaclass.com>
Co-authored-by: Tomás <47506558+MegaRedHand@users.noreply.github.com>
  • Loading branch information
3 people committed Apr 26, 2023
1 parent 47e7b34 commit 45a43aa
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 3 deletions.
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

#### Upcoming Changes

* Add alternative hint code for nondet_bigint3 hint [#1071](https://github.com/lambdaclass/cairo-rs/pull/1071)

`BuiltinHintProcessor` now supports the following hint:

```python
%{
from starkware.cairo.common.cairo_secp.secp_utils import split
segments.write_arg(ids.res.address_, split(value))
%}
```

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

`BuiltinHintProcessor` now supports the following hint:
Expand All @@ -19,7 +30,7 @@
value = new_x = (pow(slope, 2, SECP_P) - x0 - x1) % SECP_P
```

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

`BuiltinHintProcessor` now supports the following hint:

Expand Down
45 changes: 45 additions & 0 deletions cairo_programs/nondet_bigint3_v2.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
%builtins range_check

from starkware.cairo.common.cairo_secp.bigint import BigInt3, BASE

// Hint arguments: value.
func nondet_bigint3{range_check_ptr}() -> (res: BigInt3) {
// The result should be at the end of the stack after the function returns.
let res: BigInt3 = [cast(ap + 5, BigInt3*)];
%{
from starkware.cairo.common.cairo_secp.secp_utils import split
segments.write_arg(ids.res.address_, split(value))
%}
// The maximal possible sum of the limbs, assuming each of them is in the range [0, BASE).
const MAX_SUM = 3 * (BASE - 1);
assert [range_check_ptr] = MAX_SUM - (res.d0 + res.d1 + res.d2);

// Prepare the result at the end of the stack.
tempvar range_check_ptr = range_check_ptr + 4;
[range_check_ptr - 3] = res.d0, ap++;
[range_check_ptr - 2] = res.d1, ap++;
[range_check_ptr - 1] = res.d2, ap++;
static_assert &res + BigInt3.SIZE == ap;
return (res=res);
}

func main{range_check_ptr}() {
alloc_locals;
// Take these hints from div_mod_n just to have a value in scope
local a: BigInt3 = BigInt3(1,2,3);
local b: BigInt3 = BigInt3(4,5,6);
%{
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)
%}
let (r) = nondet_bigint3();
assert r.d0 = 46511138620617205537268188;
assert r.d1 = 26286155657000021849694253;
assert r.d2 = 3102515549921694024741409;
return();
}

Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ impl HintProcessor for BuiltinHintProcessor {
&hint_data.ids_data,
&hint_data.ap_tracking,
),
hint_code::NONDET_BIGINT3 => {
hint_code::NONDET_BIGINT3_V1 | hint_code::NONDET_BIGINT3_V2 => {
nondet_bigint3(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking)
}
hint_code::REDUCE => {
Expand Down
5 changes: 4 additions & 1 deletion src/hint_processor/builtin_hint_processor/hint_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,10 +471,13 @@ new_state = blake2s_compress(
segments.write_arg(ids.output, new_state)"#;

pub const NONDET_BIGINT3: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import split
pub const NONDET_BIGINT3_V1: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import split
segments.write_arg(ids.res.address_, split(value))"#;

pub const NONDET_BIGINT3_V2: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import split
segments.write_arg(ids.res.address_, split(value))"#;

pub const VERIFY_ZERO_V1: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack
q, r = divmod(pack(ids.val, PRIME), SECP_P)
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 @@ -904,3 +904,10 @@ fn ec_double_assign_new_x_v3() {
let program_data = include_bytes!("../../cairo_programs/ec_double_assign_new_x_v3.json");
run_program_simple(program_data.as_slice());
}

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

1 comment on commit 45a43aa

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.30.

Benchmark suite Current: 45a43aa Previous: 47e7b34 Ratio
parse program 27010691 ns/iter (± 1135108) 18575731 ns/iter (± 178502) 1.45
build runner 3951733 ns/iter (± 149782) 2553314 ns/iter (± 1320) 1.55

This comment was automatically generated by workflow using github-action-benchmark.

CC: @unbalancedparentheses

Please sign in to comment.