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 alternative string for hint EC_DOUBLE_ASSIGN_NEW_X #1083

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

#### Upcoming Changes

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

`BuiltinHintProcessor` now supports the following hint:

```python
%{
from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack

slope = pack(ids.slope, PRIME)
x = pack(ids.pt.x, PRIME)
y = pack(ids.pt.y, PRIME)

value = new_x = (pow(slope, 2, SECP_P) - 2 * x) % SECP_P
%}
```

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

`BuiltinHintProcessor` now supports the following hint:
Expand Down
84 changes: 84 additions & 0 deletions cairo_programs/ec_double_v4.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
%builtins range_check

from starkware.cairo.common.cairo_secp.bigint import BigInt3, UnreducedBigInt3, nondet_bigint3
from starkware.cairo.common.cairo_secp.ec import compute_doubling_slope, EcPoint
from starkware.cairo.common.cairo_secp.field import (
is_zero,
unreduced_mul,
unreduced_sqr,
verify_zero,
)

// Computes the addition of a given point to itself.
//
// Arguments:
// point - the point to operate on.
//
// Returns:
// res - a point representing point + point.
func ec_double{range_check_ptr}(pt: EcPoint) -> (res: EcPoint) {
// The zero point.
if (pt.x.d0 == 0) {
if (pt.x.d1 == 0) {
if (pt.x.d2 == 0) {
return (res=pt);
}
}
}

let (slope: BigInt3) = compute_doubling_slope(pt);
let (slope_sqr: UnreducedBigInt3) = unreduced_sqr(slope);

%{
from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack

slope = pack(ids.slope, PRIME)
x = pack(ids.pt.x, PRIME)
y = pack(ids.pt.y, PRIME)

value = new_x = (pow(slope, 2, SECP_P) - 2 * x) % SECP_P
%}
let (new_x: BigInt3) = nondet_bigint3();

%{ value = new_y = (slope * (x - new_x) - y) % SECP_P %}
let (new_y: BigInt3) = nondet_bigint3();

verify_zero(
UnreducedBigInt3(
d0=slope_sqr.d0 - new_x.d0 - 2 * pt.x.d0,
d1=slope_sqr.d1 - new_x.d1 - 2 * pt.x.d1,
d2=slope_sqr.d2 - new_x.d2 - 2 * pt.x.d2,
),
);

let (x_diff_slope: UnreducedBigInt3) = unreduced_mul(
BigInt3(d0=pt.x.d0 - new_x.d0, d1=pt.x.d1 - new_x.d1, d2=pt.x.d2 - new_x.d2), slope
);

verify_zero(
UnreducedBigInt3(
d0=x_diff_slope.d0 - pt.y.d0 - new_y.d0,
d1=x_diff_slope.d1 - pt.y.d1 - new_y.d1,
d2=x_diff_slope.d2 - pt.y.d2 - new_y.d2,
),
);

return (res=EcPoint(new_x, new_y));
}

func main{range_check_ptr}() {
let x = BigInt3(7,8,9);
let y = BigInt3(19,29,30);
let p = EcPoint(x, y);
let (r) = ec_double(p);

assert r.x.d0 = 51257743837507631919880152;
assert r.x.d1 = 64460046105241149334147278;
assert r.x.d2 = 12582041431145599112140654;

assert r.y.d0 = 19321524266852839048503535;
assert r.y.d1 = 35591956483215965716767025;
assert r.y.d2 = 12630971731313051616919773;

return();
}
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ impl HintProcessor for BuiltinHintProcessor {
&hint_data.ids_data,
&hint_data.ap_tracking,
&SECP_P,
"point",
)
}
hint_code::EC_DOUBLE_ASSIGN_NEW_X_V3 => ec_double_assign_new_x(
Expand All @@ -540,6 +541,15 @@ impl HintProcessor for BuiltinHintProcessor {
&hint_data.ids_data,
&hint_data.ap_tracking,
&SECP_P_V2,
"point",
),
hint_code::EC_DOUBLE_ASSIGN_NEW_X_V4 => ec_double_assign_new_x(
vm,
exec_scopes,
&hint_data.ids_data,
&hint_data.ap_tracking,
&SECP_P,
"pt",
),
hint_code::EC_DOUBLE_ASSIGN_NEW_Y => ec_double_assign_new_y(exec_scopes),
hint_code::KECCAK_WRITE_ARGS => {
Expand Down
8 changes: 8 additions & 0 deletions src/hint_processor/builtin_hint_processor/hint_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,14 @@ y = pack(ids.point.y, PRIME)

value = new_x = (pow(slope, 2, SECP_P) - 2 * x) % SECP_P"#;

pub const EC_DOUBLE_ASSIGN_NEW_X_V4: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack

slope = pack(ids.slope, PRIME)
x = pack(ids.pt.x, PRIME)
y = pack(ids.pt.y, PRIME)

value = new_x = (pow(slope, 2, SECP_P) - 2 * x) % SECP_P"#;

pub const EC_DOUBLE_ASSIGN_NEW_Y: &str = r#"value = new_y = (slope * (x - new_x) - y) % SECP_P"#;

pub const SHA256_INPUT: &str = r#"ids.full_word = int(ids.n_bytes >= 4)"#;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,13 @@ pub fn ec_double_assign_new_x(
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
secp_p: &BigInt,
point_alias: &str,
) -> Result<(), HintError> {
exec_scopes.insert_value("SECP_P", secp_p.clone());
//ids.slope
let slope = BigInt3::from_var_name("slope", vm, ids_data, ap_tracking)?;
//ids.point
let point = EcPoint::from_var_name("point", vm, ids_data, ap_tracking)?;
let point = EcPoint::from_var_name(point_alias, vm, ids_data, ap_tracking)?;

let slope = slope.pack86().mod_floor(secp_p);
let x = point.x.pack86().mod_floor(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 @@ -912,6 +912,13 @@ fn nondet_bigint3_v2() {
run_program_simple(program_data.as_slice());
}

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

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn cairo_run_ec_double_slope() {
Expand Down