Skip to content

Commit ddbeb8a

Browse files
authored
feat(hints): add NewHint#27 (#990)
* Add NewHint#27 * Update changelog * Fix changelog Some of the entries were being repeated when merging changes, and were also originally misplaced in the 0.3.0-rc1 section * Make hint codes public
1 parent 2f557b1 commit ddbeb8a

File tree

6 files changed

+213
-139
lines changed

6 files changed

+213
-139
lines changed

CHANGELOG.md

Lines changed: 52 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,58 @@
1616
* Added dynamic layout [#879](https://github.com/lambdaclass/cairo-rs/pull/879)
1717
* `get_segment_size` was exposed [#934](https://github.com/lambdaclass/cairo-rs/pull/934)
1818

19+
20+
* Add missing hint on cairo_secp lib [#990](https://github.com/lambdaclass/cairo-rs/pull/990):
21+
22+
`BuiltinHintProcessor` now supports the following hint:
23+
```python
24+
from starkware.cairo.common.cairo_secp.secp_utils import pack
25+
26+
slope = pack(ids.slope, PRIME)
27+
x = pack(ids.point.x, PRIME)
28+
y = pack(ids.point.y, PRIME)
29+
30+
value = new_x = (pow(slope, 2, SECP_P) - 2 * x) % SECP_P
31+
```
32+
33+
* Add missing hint on cairo_secp lib [#989](https://github.com/lambdaclass/cairo-rs/pull/989):
34+
35+
`BuiltinHintProcessor` now supports the following hint:
36+
```python
37+
from starkware.cairo.common.cairo_secp.secp_utils import SECP_P
38+
q, r = divmod(pack(ids.val, PRIME), SECP_P)
39+
assert r == 0, f"verify_zero: Invalid input {ids.val.d0, ids.val.d1, ids.val.d2}."
40+
ids.q = q % PRIME
41+
```
42+
43+
* Add missing hint on cairo_secp lib [#986](https://github.com/lambdaclass/cairo-rs/pull/986):
44+
45+
`BuiltinHintProcessor` now supports the following hint:
46+
```python
47+
from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack
48+
from starkware.python.math_utils import div_mod
49+
50+
# Compute the slope.
51+
x = pack(ids.pt.x, PRIME)
52+
y = pack(ids.pt.y, PRIME)
53+
value = slope = div_mod(3 * x ** 2, 2 * y, SECP_P)
54+
```
55+
56+
* Add missing hint on cairo_secp lib [#984](https://github.com/lambdaclass/cairo-rs/pull/984):
57+
58+
`BuiltinHintProcessor` now supports the following hint:
59+
```python
60+
from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack
61+
from starkware.python.math_utils import div_mod
62+
63+
# Compute the slope.
64+
x0 = pack(ids.pt0.x, PRIME)
65+
y0 = pack(ids.pt0.y, PRIME)
66+
x1 = pack(ids.pt1.x, PRIME)
67+
y1 = pack(ids.pt1.y, PRIME)
68+
value = slope = div_mod(y0 - y1, x0 - x1, SECP_P)
69+
```
70+
1971
#### [0.3.0-rc1] - 2023-04-13
2072
* Derive Deserialize for ExecutionResources [#922](https://github.com/lambdaclass/cairo-rs/pull/922)
2173
* Remove builtin names from VirtualMachine.builtin_runners [#921](https://github.com/lambdaclass/cairo-rs/pull/921)
@@ -61,22 +113,6 @@
61113
* after each step
62114
* ExecutionResource operations: add and substract [#774](https://github.com/lambdaclass/cairo-rs/pull/774), multiplication [#908](https://github.com/lambdaclass/cairo-rs/pull/908) , and `AddAssign` [#914](https://github.com/lambdaclass/cairo-rs/pull/914)
63115
64-
65-
* Add missing hint on cairo_secp lib [#984]:
66-
67-
`BuiltinHintProcessor` now supports the following hint:
68-
```python
69-
from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack
70-
from starkware.python.math_utils import div_mod
71-
72-
# Compute the slope.
73-
x0 = pack(ids.pt0.x, PRIME)
74-
y0 = pack(ids.pt0.y, PRIME)
75-
x1 = pack(ids.pt1.x, PRIME)
76-
y1 = pack(ids.pt1.y, PRIME)
77-
value = slope = div_mod(y0 - y1, x0 - x1, SECP_P)
78-
```
79-
80116
* Implement hints on uint384 lib (Part 2) [#971](https://github.com/lambdaclass/cairo-rs/pull/971)
81117
82118
`BuiltinHintProcessor` now supports the following hint:
@@ -228,69 +264,6 @@
228264
229265
Used by the common library function `uint256_mul_div_mod`
230266
231-
* Add missing hint on cairo_secp lib [#986]:
232-
233-
`BuiltinHintProcessor` now supports the following hint:
234-
```python
235-
from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack
236-
from starkware.python.math_utils import div_mod
237-
238-
# Compute the slope.
239-
x = pack(ids.pt.x, PRIME)
240-
y = pack(ids.pt.y, PRIME)
241-
value = slope = div_mod(3 * x ** 2, 2 * y, SECP_P)
242-
```
243-
244-
* Add missing hint on cairo_secp lib [#984]:
245-
`BuiltinHintProcessor` now supports the following hint:
246-
```python
247-
from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack
248-
from starkware.python.math_utils import div_mod
249-
250-
# Compute the slope.
251-
x0 = pack(ids.pt0.x, PRIME)
252-
y0 = pack(ids.pt0.y, PRIME)
253-
x1 = pack(ids.pt1.x, PRIME)
254-
y1 = pack(ids.pt1.y, PRIME)
255-
value = slope = div_mod(y0 - y1, x0 - x1, SECP_P)
256-
```
257-
258-
* Add missing hint on cairo_secp lib [#989]:
259-
260-
`BuiltinHintProcessor` now supports the following hint:
261-
```python
262-
from starkware.cairo.common.cairo_secp.secp_utils import SECP_P
263-
q, r = divmod(pack(ids.val, PRIME), SECP_P)
264-
assert r == 0, f"verify_zero: Invalid input {ids.val.d0, ids.val.d1, ids.val.d2}."
265-
ids.q = q % PRIME
266-
```
267-
268-
* Add missing hint on cairo_secp lib [#986]:
269-
`BuiltinHintProcessor` now supports the following hint:
270-
```python
271-
from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack
272-
from starkware.python.math_utils import div_mod
273-
274-
# Compute the slope.
275-
x = pack(ids.pt.x, PRIME)
276-
y = pack(ids.pt.y, PRIME)
277-
value = slope = div_mod(3 * x ** 2, 2 * y, SECP_P)
278-
```
279-
280-
* Add missing hint on cairo_secp lib [#984]:
281-
`BuiltinHintProcessor` now supports the following hint:
282-
```python
283-
from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack
284-
from starkware.python.math_utils import div_mod
285-
286-
# Compute the slope.
287-
x0 = pack(ids.pt0.x, PRIME)
288-
y0 = pack(ids.pt0.y, PRIME)
289-
x1 = pack(ids.pt1.x, PRIME)
290-
y1 = pack(ids.pt1.y, PRIME)
291-
value = slope = div_mod(y0 - y1, x0 - x1, SECP_P)
292-
```
293-
294267
* Move `Memory` into `MemorySegmentManager` [#830](https://github.com/lambdaclass/cairo-rs/pull/830)
295268
* Structural changes:
296269
* Remove `memory: Memory` field from `VirtualMachine`
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
%builtins range_check
2+
3+
// Source: https://github.com/myBraavos/efficient-secp256r1/blob/73cca4d53730cb8b2dcf34e36c7b8f34b96b3230/src/secp256r1/ec.cairo#L127
4+
5+
from starkware.cairo.common.cairo_secp.bigint import BigInt3, UnreducedBigInt3, nondet_bigint3
6+
from starkware.cairo.common.cairo_secp.ec import EcPoint, compute_doubling_slope
7+
from starkware.cairo.common.cairo_secp.field import (
8+
is_zero,
9+
unreduced_mul,
10+
unreduced_sqr,
11+
verify_zero,
12+
)
13+
14+
// Computes the addition of a given point to itself.
15+
//
16+
// Arguments:
17+
// point - the point to operate on.
18+
//
19+
// Returns:
20+
// res - a point representing point + point.
21+
func ec_double{range_check_ptr}(point: EcPoint) -> (res: EcPoint) {
22+
// The zero point.
23+
if (point.x.d0 == 0) {
24+
if (point.x.d1 == 0) {
25+
if (point.x.d2 == 0) {
26+
return (res=point);
27+
}
28+
}
29+
}
30+
31+
let (slope: BigInt3) = compute_doubling_slope(point);
32+
let (slope_sqr: UnreducedBigInt3) = unreduced_sqr(slope);
33+
34+
%{
35+
from starkware.cairo.common.cairo_secp.secp_utils import pack
36+
37+
slope = pack(ids.slope, PRIME)
38+
x = pack(ids.point.x, PRIME)
39+
y = pack(ids.point.y, PRIME)
40+
41+
value = new_x = (pow(slope, 2, SECP_P) - 2 * x) % SECP_P
42+
%}
43+
let (new_x: BigInt3) = nondet_bigint3();
44+
45+
%{ value = new_y = (slope * (x - new_x) - y) % SECP_P %}
46+
let (new_y: BigInt3) = nondet_bigint3();
47+
verify_zero(
48+
UnreducedBigInt3(
49+
d0=slope_sqr.d0 - new_x.d0 - 2 * point.x.d0,
50+
d1=slope_sqr.d1 - new_x.d1 - 2 * point.x.d1,
51+
d2=slope_sqr.d2 - new_x.d2 - 2 * point.x.d2,
52+
),
53+
);
54+
55+
let (x_diff_slope: UnreducedBigInt3) = unreduced_mul(
56+
BigInt3(d0=point.x.d0 - new_x.d0, d1=point.x.d1 - new_x.d1, d2=point.x.d2 - new_x.d2), slope
57+
);
58+
verify_zero(
59+
UnreducedBigInt3(
60+
d0=x_diff_slope.d0 - point.y.d0 - new_y.d0,
61+
d1=x_diff_slope.d1 - point.y.d1 - new_y.d1,
62+
d2=x_diff_slope.d2 - point.y.d2 - new_y.d2,
63+
),
64+
);
65+
66+
return (res=EcPoint(new_x, new_y));
67+
}
68+
69+
func main{range_check_ptr: felt}() {
70+
let x = BigInt3(235, 522, 111);
71+
let y = BigInt3(1323, 15124, 796759);
72+
73+
let point = EcPoint(x, y);
74+
75+
let (res) = ec_double(point);
76+
77+
assert res = EcPoint(
78+
BigInt3(64960503569511978748964127, 74077005698377320581054215, 17246103581201827820088765),
79+
BigInt3(13476289913106792137931934, 29193128211607101710049068, 18079689234850912663169436),
80+
);
81+
82+
return ();
83+
}

src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ impl HintProcessor for BuiltinHintProcessor {
390390
"pt0",
391391
"pt1",
392392
),
393-
hint_code::EC_DOUBLE_ASSIGN_NEW_X => {
393+
hint_code::EC_DOUBLE_ASSIGN_NEW_X_V1 | hint_code::EC_DOUBLE_ASSIGN_NEW_X_V2 => {
394394
ec_double_assign_new_x(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking)
395395
}
396396
hint_code::EC_DOUBLE_ASSIGN_NEW_Y => ec_double_assign_new_y(exec_scopes),

src/hint_processor/builtin_hint_processor/hint_code.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,15 @@ x1 = pack(ids.pt1.x, PRIME)
497497
y1 = pack(ids.pt1.y, PRIME)
498498
value = slope = div_mod(y0 - y1, x0 - x1, SECP_P)"#;
499499

500-
pub const EC_DOUBLE_ASSIGN_NEW_X: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack
500+
pub const EC_DOUBLE_ASSIGN_NEW_X_V1: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack
501+
502+
slope = pack(ids.slope, PRIME)
503+
x = pack(ids.point.x, PRIME)
504+
y = pack(ids.point.y, PRIME)
505+
506+
value = new_x = (pow(slope, 2, SECP_P) - 2 * x) % SECP_P"#;
507+
508+
pub const EC_DOUBLE_ASSIGN_NEW_X_V2: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import pack
501509
502510
slope = pack(ids.slope, PRIME)
503511
x = pack(ids.point.x, PRIME)

src/hint_processor/builtin_hint_processor/secp/ec_utils.rs

Lines changed: 61 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -504,64 +504,67 @@ mod tests {
504504
#[test]
505505
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
506506
fn run_ec_double_assign_new_x_ok() {
507-
let hint_code = "from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack\n\nslope = pack(ids.slope, PRIME)\nx = pack(ids.point.x, PRIME)\ny = pack(ids.point.y, PRIME)\n\nvalue = new_x = (pow(slope, 2, SECP_P) - 2 * x) % SECP_P";
508-
let mut vm = vm_with_range_check!();
509-
510-
//Insert ids.point and ids.slope into memory
511-
vm.segments = segments![
512-
((1, 0), 134),
513-
((1, 1), 5123),
514-
((1, 2), 140),
515-
((1, 3), 1232),
516-
((1, 4), 4652),
517-
((1, 5), 720),
518-
((1, 6), 44186171158942157784255469_i128),
519-
((1, 7), 54173758974262696047492534_i128),
520-
((1, 8), 8106299688661572814170174_i128)
521-
];
522-
523-
//Initialize fp
524-
vm.run_context.fp = 10;
525-
let ids_data = HashMap::from([
526-
("point".to_string(), HintReference::new_simple(-10)),
527-
("slope".to_string(), HintReference::new_simple(-4)),
528-
]);
529-
let mut exec_scopes = ExecutionScopes::new();
530-
531-
//Execute the hint
532-
assert_matches!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(()));
533-
534-
check_scope!(
535-
&exec_scopes,
536-
[
537-
(
538-
"slope",
539-
bigint_str!(
540-
"48526828616392201132917323266456307435009781900148206102108934970258721901549"
541-
)
542-
),
543-
(
544-
"x",
545-
bigint_str!("838083498911032969414721426845751663479194726707495046")
546-
),
547-
(
548-
"y",
549-
bigint_str!("4310143708685312414132851373791311001152018708061750480")
550-
),
551-
(
552-
"value",
553-
bigint_str!(
554-
"59479631769792988345961122678598249997181612138456851058217178025444564264149"
555-
)
556-
),
557-
(
558-
"new_x",
559-
bigint_str!(
560-
"59479631769792988345961122678598249997181612138456851058217178025444564264149"
561-
)
562-
)
563-
]
564-
);
507+
let hint_codes = vec!["from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack\n\nslope = pack(ids.slope, PRIME)\nx = pack(ids.point.x, PRIME)\ny = pack(ids.point.y, PRIME)\n\nvalue = new_x = (pow(slope, 2, SECP_P) - 2 * x) % SECP_P", "from starkware.cairo.common.cairo_secp.secp_utils import pack\n\nslope = pack(ids.slope, PRIME)\nx = pack(ids.point.x, PRIME)\ny = pack(ids.point.y, PRIME)\n\nvalue = new_x = (pow(slope, 2, SECP_P) - 2 * x) % SECP_P"];
508+
509+
for hint_code in hint_codes {
510+
let mut vm = vm_with_range_check!();
511+
512+
//Insert ids.point and ids.slope into memory
513+
vm.segments = segments![
514+
((1, 0), 134),
515+
((1, 1), 5123),
516+
((1, 2), 140),
517+
((1, 3), 1232),
518+
((1, 4), 4652),
519+
((1, 5), 720),
520+
((1, 6), 44186171158942157784255469_i128),
521+
((1, 7), 54173758974262696047492534_i128),
522+
((1, 8), 8106299688661572814170174_i128)
523+
];
524+
525+
//Initialize fp
526+
vm.run_context.fp = 10;
527+
let ids_data = HashMap::from([
528+
("point".to_string(), HintReference::new_simple(-10)),
529+
("slope".to_string(), HintReference::new_simple(-4)),
530+
]);
531+
let mut exec_scopes = ExecutionScopes::new();
532+
533+
//Execute the hint
534+
assert_matches!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(()));
535+
536+
check_scope!(
537+
&exec_scopes,
538+
[
539+
(
540+
"slope",
541+
bigint_str!(
542+
"48526828616392201132917323266456307435009781900148206102108934970258721901549"
543+
)
544+
),
545+
(
546+
"x",
547+
bigint_str!("838083498911032969414721426845751663479194726707495046")
548+
),
549+
(
550+
"y",
551+
bigint_str!("4310143708685312414132851373791311001152018708061750480")
552+
),
553+
(
554+
"value",
555+
bigint_str!(
556+
"59479631769792988345961122678598249997181612138456851058217178025444564264149"
557+
)
558+
),
559+
(
560+
"new_x",
561+
bigint_str!(
562+
"59479631769792988345961122678598249997181612138456851058217178025444564264149"
563+
)
564+
)
565+
]
566+
);
567+
}
565568
}
566569

567570
#[test]

src/tests/cairo_run_test.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,3 +1308,10 @@ fn cairo_run_ed25519_ec() {
13081308
let program_data = include_bytes!("../../cairo_programs/ed25519_ec.json");
13091309
run_program_simple(program_data.as_slice());
13101310
}
1311+
1312+
#[test]
1313+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
1314+
fn cairo_run_efficient_secp256r1_ec() {
1315+
let program_data = include_bytes!("../../cairo_programs/efficient_secp256r1_ec.json");
1316+
run_program_simple(program_data.as_slice());
1317+
}

0 commit comments

Comments
 (0)