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): Implement NewHint#59 #1053

Merged
merged 14 commits into from
Apr 26, 2023
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,33 @@

#### Upcoming Changes

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

`BuiltinHintProcessor` now supports the following hint:

```python
%{
from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack
SECP_P = 2**255-19

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

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

* Implement hint on 0.6.0.json whitelist [#1044](https://github.com/lambdaclass/cairo-rs/pull/1044):

`BuiltinHintProcessor` now supports the following hints:

```
%{
ids.a_lsb = ids.a & 1
ids.b_lsb = ids.b & 1
%}
```

* 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)

Expand Down
85 changes: 85 additions & 0 deletions cairo_programs/ec_double_assign_new_x_v3.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
%builtins range_check
from starkware.cairo.common.cairo_secp.bigint import BigInt3, nondet_bigint3, UnreducedBigInt3
from starkware.cairo.common.cairo_secp.field import (
is_zero,
unreduced_sqr,
unreduced_mul
)
from starkware.cairo.common.cairo_secp.ec import EcPoint, compute_doubling_slope, 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}(point: EcPoint) -> (res: EcPoint) {
// The zero point.
if (point.x.d0 == 0) {
if (point.x.d1 == 0) {
if (point.x.d2 == 0) {
return (res=point);
}
}
}

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

%{
from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack
Oppen marked this conversation as resolved.
Show resolved Hide resolved
SECP_P = 2**255-19

slope = pack(ids.slope, PRIME)
x = pack(ids.point.x, PRIME)
y = pack(ids.point.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 * point.x.d0,
d1=slope_sqr.d1 - new_x.d1 - 2 * point.x.d1,
d2=slope_sqr.d2 - new_x.d2 - 2 * point.x.d2,
),
);

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

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

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

func main{range_check_ptr}() {
let x = BigInt3(1,2,3);
let y = BigInt3(4,5,6);
let p = EcPoint(x, y);

let (r) = ec_double(p);

assert r.x.d0 = 57832968898037685942927716;
assert r.x.d1 = 27957507593122495312333579;
assert r.x.d2 = 876486538158111111042155;

assert r.y.d0 = 76653617457582133477854326;
assert r.y.d1 = 906421522066442687720656;
assert r.y.d2 = 11165193544924531831122323;

return ();
}
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,21 @@ impl HintProcessor for BuiltinHintProcessor {
&SECP_P,
),
hint_code::EC_DOUBLE_ASSIGN_NEW_X_V1 | hint_code::EC_DOUBLE_ASSIGN_NEW_X_V2 => {
ec_double_assign_new_x(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking)
ec_double_assign_new_x(
vm,
exec_scopes,
&hint_data.ids_data,
&hint_data.ap_tracking,
&SECP_P,
)
}
hint_code::EC_DOUBLE_ASSIGN_NEW_X_V3 => ec_double_assign_new_x(
vm,
exec_scopes,
&hint_data.ids_data,
&hint_data.ap_tracking,
&SECP_P_V2,
),
hint_code::EC_DOUBLE_ASSIGN_NEW_Y => ec_double_assign_new_y(exec_scopes),
hint_code::KECCAK_WRITE_ARGS => {
keccak_write_args(vm, &hint_data.ids_data, &hint_data.ap_tracking)
Expand Down
9 changes: 9 additions & 0 deletions src/hint_processor/builtin_hint_processor/hint_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,15 @@ 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_V3: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import pack
SECP_P = 2**255-19

slope = pack(ids.slope, PRIME)
x = pack(ids.point.x, PRIME)
y = pack(ids.point.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 @@ -210,8 +210,9 @@ pub fn ec_double_assign_new_x(
exec_scopes: &mut ExecutionScopes,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
secp_p: &BigInt,
) -> Result<(), HintError> {
exec_scopes.insert_value("SECP_P", SECP_P.clone());
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
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 @@ -883,3 +883,10 @@ fn cairo_run_compute_doubling_slope_v2_test() {
let program_data = include_bytes!("../../cairo_programs/compute_doubling_slope_v2.json");
run_program_simple(program_data.as_slice());
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn ec_double_assing_new_x_v3() {
fmoletta marked this conversation as resolved.
Show resolved Hide resolved
let program_data = include_bytes!("../../cairo_programs/ec_double_assign_new_x_v3.json");
run_program_simple(program_data.as_slice());
}