Skip to content

Commit e256071

Browse files
fmolettaOppen
authored andcommitted
feat(hints): Implement NewHint#59 (lambdaclass#1053)
* Add hint * Add integration test * Add changelog entry * Typo * Fix invoked hint * Update src/tests/cairo_run_test.rs Co-authored-by: Mario Rugiero <[email protected]> * Fix ec_double_assign_new_y * fixes * Fix integration test * Clippy --------- Co-authored-by: Mario Rugiero <[email protected]>
1 parent 450ca87 commit e256071

File tree

6 files changed

+140
-9
lines changed

6 files changed

+140
-9
lines changed

CHANGELOG.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,33 @@
22

33
#### Upcoming Changes
44

5+
Add missing hint on vrf.json lib [#1053](https://github.com/lambdaclass/cairo-rs/pull/1053):
6+
7+
`BuiltinHintProcessor` now supports the following hint:
8+
9+
```python
10+
%{
11+
from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack
12+
SECP_P = 2**255-19
13+
14+
slope = pack(ids.slope, PRIME)
15+
x = pack(ids.point.x, PRIME)
16+
y = pack(ids.point.y, PRIME)
17+
18+
value = new_x = (pow(slope, 2, SECP_P) - 2 * x) % SECP_P
19+
%}
20+
```
21+
522
* Implement hint on 0.6.0.json whitelist [#1044](https://github.com/lambdaclass/cairo-rs/pull/1044):
623

724
`BuiltinHintProcessor` now supports the following hints:
825

26+
```
927
%{
1028
ids.a_lsb = ids.a & 1
1129
ids.b_lsb = ids.b & 1
1230
%}
31+
```
1332
1433
* Implement hint for `starkware.cairo.common.cairo_keccak.keccak._block_permutation` as described by whitelist `starknet/security/whitelists/cairo_keccak.json` [#1046](https://github.com/lambdaclass/cairo-rs/pull/1046)
1534
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
from starkware.cairo.common.cairo_secp.bigint import BigInt3, nondet_bigint3, UnreducedBigInt3
2+
from cairo_programs.compute_slope_v2 import verify_zero, unreduced_mul
3+
from cairo_programs.compute_doubling_slope_v2 import compute_doubling_slope, EcPoint, unreduced_sqr
4+
5+
// Computes the addition of a given point to itself.
6+
//
7+
// Arguments:
8+
// point - the point to operate on.
9+
//
10+
// Returns:
11+
// res - a point representing point + point.
12+
func ec_double{range_check_ptr}(point: EcPoint) -> (res: EcPoint) {
13+
// The zero point.
14+
if (point.x.d0 == 0) {
15+
if (point.x.d1 == 0) {
16+
if (point.x.d2 == 0) {
17+
return (res=point);
18+
}
19+
}
20+
}
21+
22+
let (slope: BigInt3) = compute_doubling_slope(point);
23+
let (slope_sqr: UnreducedBigInt3) = unreduced_sqr(slope);
24+
25+
%{
26+
from starkware.cairo.common.cairo_secp.secp_utils import pack
27+
SECP_P = 2**255-19
28+
29+
slope = pack(ids.slope, PRIME)
30+
x = pack(ids.point.x, PRIME)
31+
y = pack(ids.point.y, PRIME)
32+
33+
value = new_x = (pow(slope, 2, SECP_P) - 2 * x) % SECP_P
34+
%}
35+
36+
let (new_x: BigInt3) = nondet_bigint3();
37+
38+
%{ value = new_y = (slope * (x - new_x) - y) % SECP_P %}
39+
let (new_y: BigInt3) = nondet_bigint3();
40+
41+
verify_zero(
42+
UnreducedBigInt3(
43+
d0=slope_sqr.d0 - new_x.d0 - 2 * point.x.d0,
44+
d1=slope_sqr.d1 - new_x.d1 - 2 * point.x.d1,
45+
d2=slope_sqr.d2 - new_x.d2 - 2 * point.x.d2,
46+
),
47+
);
48+
49+
let (x_diff_slope: UnreducedBigInt3) = unreduced_mul(
50+
BigInt3(d0=point.x.d0 - new_x.d0, d1=point.x.d1 - new_x.d1, d2=point.x.d2 - new_x.d2), slope
51+
);
52+
53+
verify_zero(
54+
UnreducedBigInt3(
55+
d0=x_diff_slope.d0 - point.y.d0 - new_y.d0,
56+
d1=x_diff_slope.d1 - point.y.d1 - new_y.d1,
57+
d2=x_diff_slope.d2 - point.y.d2 - new_y.d2,
58+
),
59+
);
60+
61+
return (res=EcPoint(new_x, new_y));
62+
}
63+
64+
func main{range_check_ptr}() {
65+
let x = BigInt3(1,2,3);
66+
let y = BigInt3(4,5,6);
67+
let p = EcPoint(x, y);
68+
69+
let (r) = ec_double(p);
70+
71+
assert r.x.d0 = 15463639180909693576579425;
72+
assert r.x.d1 = 18412232947780787290771221;
73+
assert r.x.d2 = 2302636566907525872042731;
74+
75+
assert r.y.d0 = 62720835442754730087165024;
76+
assert r.y.d1 = 51587896485732116326812460;
77+
assert r.y.d2 = 1463255073263285938516131;
78+
79+
return ();
80+
}

src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,8 +519,21 @@ impl HintProcessor for BuiltinHintProcessor {
519519
&SECP_P,
520520
),
521521
hint_code::EC_DOUBLE_ASSIGN_NEW_X_V1 | hint_code::EC_DOUBLE_ASSIGN_NEW_X_V2 => {
522-
ec_double_assign_new_x(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking)
522+
ec_double_assign_new_x(
523+
vm,
524+
exec_scopes,
525+
&hint_data.ids_data,
526+
&hint_data.ap_tracking,
527+
&SECP_P,
528+
)
523529
}
530+
hint_code::EC_DOUBLE_ASSIGN_NEW_X_V3 => ec_double_assign_new_x(
531+
vm,
532+
exec_scopes,
533+
&hint_data.ids_data,
534+
&hint_data.ap_tracking,
535+
&SECP_P_V2,
536+
),
524537
hint_code::EC_DOUBLE_ASSIGN_NEW_Y => ec_double_assign_new_y(exec_scopes),
525538
hint_code::KECCAK_WRITE_ARGS => {
526539
keccak_write_args(vm, &hint_data.ids_data, &hint_data.ap_tracking)

src/hint_processor/builtin_hint_processor/hint_code.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,15 @@ y = pack(ids.point.y, PRIME)
696696
697697
value = new_x = (pow(slope, 2, SECP_P) - 2 * x) % SECP_P"#;
698698

699+
pub const EC_DOUBLE_ASSIGN_NEW_X_V3: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import pack
700+
SECP_P = 2**255-19
701+
702+
slope = pack(ids.slope, PRIME)
703+
x = pack(ids.point.x, PRIME)
704+
y = pack(ids.point.y, PRIME)
705+
706+
value = new_x = (pow(slope, 2, SECP_P) - 2 * x) % SECP_P"#;
707+
699708
pub const EC_DOUBLE_ASSIGN_NEW_Y: &str = r#"value = new_y = (slope * (x - new_x) - y) % SECP_P"#;
700709

701710
pub const SHA256_INPUT: &str = r#"ids.full_word = int(ids.n_bytes >= 4)"#;

src/hint_processor/builtin_hint_processor/secp/ec_utils.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -210,18 +210,19 @@ pub fn ec_double_assign_new_x(
210210
exec_scopes: &mut ExecutionScopes,
211211
ids_data: &HashMap<String, HintReference>,
212212
ap_tracking: &ApTracking,
213+
secp_p: &BigInt,
213214
) -> Result<(), HintError> {
214-
exec_scopes.insert_value("SECP_P", SECP_P.clone());
215+
exec_scopes.insert_value("SECP_P", secp_p.clone());
215216
//ids.slope
216217
let slope = BigInt3::from_var_name("slope", vm, ids_data, ap_tracking)?;
217218
//ids.point
218219
let point = EcPoint::from_var_name("point", vm, ids_data, ap_tracking)?;
219220

220-
let slope = slope.pack86();
221-
let x = point.x.pack86();
222-
let y = point.y.pack86();
221+
let slope = slope.pack86().mod_floor(secp_p);
222+
let x = point.x.pack86().mod_floor(secp_p);
223+
let y = point.y.pack86().mod_floor(secp_p);
223224

224-
let value = (slope.pow(2) - (&x << 1u32)).mod_floor(&SECP_P);
225+
let value = (slope.pow(2) - (&x << 1u32)).mod_floor(secp_p);
225226

226227
//Assign variables to vm scope
227228
exec_scopes.insert_value("slope", slope);
@@ -238,14 +239,15 @@ Implements hint:
238239
*/
239240
pub fn ec_double_assign_new_y(exec_scopes: &mut ExecutionScopes) -> Result<(), HintError> {
240241
//Get variables from vm scope
241-
let (slope, x, new_x, y) = (
242+
let (slope, x, new_x, y, secp_p) = (
242243
exec_scopes.get::<BigInt>("slope")?,
243244
exec_scopes.get::<BigInt>("x")?,
244245
exec_scopes.get::<BigInt>("new_x")?,
245246
exec_scopes.get::<BigInt>("y")?,
247+
exec_scopes.get::<BigInt>("SECP_P")?,
246248
);
247249

248-
let value = (slope * (x - new_x) - y).mod_floor(&SECP_P);
250+
let value = (slope * (x - new_x) - y).mod_floor(&secp_p);
249251
exec_scopes.insert_value("value", value.clone());
250252
exec_scopes.insert_value("new_y", value);
251253
Ok(())
@@ -855,7 +857,8 @@ mod tests {
855857
(
856858
"y",
857859
bigint_str!("4310143708685312414132851373791311001152018708061750480")
858-
)
860+
),
861+
("SECP_P", (*SECP_P).clone())
859862
];
860863
//Execute the hint
861864
assert_matches!(

src/tests/cairo_run_test.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,3 +883,10 @@ fn cairo_run_compute_doubling_slope_v2_test() {
883883
let program_data = include_bytes!("../../cairo_programs/compute_doubling_slope_v2.json");
884884
run_program_simple(program_data.as_slice());
885885
}
886+
887+
#[test]
888+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
889+
fn ec_double_assign_new_x_v3() {
890+
let program_data = include_bytes!("../../cairo_programs/ec_double_assign_new_x_v3.json");
891+
run_program_simple(program_data.as_slice());
892+
}

0 commit comments

Comments
 (0)