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

Newhint#20 and Newhint#16: add COMPUTE_SLOPE_SECP256R1 and IMPORT_SECP256R1_P #1014

Merged
merged 15 commits into from
Apr 21, 2023
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,23 @@
* BugFix: Add missing `\n` character after traceback lines when the filename is missing ("Unknown Location")

* 0.11 Support
* Add missing hints [#1014](https://github.com/lambdaclass/cairo-rs/pull/1014):
`BuiltinHintProcessor` now supports the following hints:
```python
from starkware.cairo.common.cairo_secp.secp256r1_utils import SECP256R1_P as SECP_P
```
and:
```python
from starkware.cairo.common.cairo_secp.secp_utils import pack
from starkware.python.math_utils import line_slope

# Compute the slope.
x0 = pack(ids.point0.x, PRIME)
y0 = pack(ids.point0.y, PRIME)
x1 = pack(ids.point1.x, PRIME)
y1 = pack(ids.point1.y, PRIME)
value = slope = line_slope(point1=(x0, y0), point2=(x1, y1), p=SECP_P)
```
* Add missing hints on cairo_secp lib [#991](https://github.com/lambdaclass/cairo-rs/pull/991):
`BuiltinHintProcessor` now supports the following hints:
```python
Expand Down
60 changes: 60 additions & 0 deletions cairo_programs/secp256r1_slope.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
%builtins range_check

// Source: https://github.com/myBraavos/efficient-secp256r1/blob/main/src/secp256r1/ec.cairo

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


// Returns the slope of the line connecting the two given points.
// The slope is used to compute pt0 + pt1.
// Assumption: pt0.x != pt1.x (mod secp256k1_prime).
func compute_slope{range_check_ptr: felt}(point0: EcPoint, point1: EcPoint) -> (slope: BigInt3) {
%{ from starkware.cairo.common.cairo_secp.secp256r1_utils import SECP256R1_P as SECP_P %}
%{
from starkware.cairo.common.cairo_secp.secp_utils import pack
from starkware.python.math_utils import line_slope

# Compute the slope.
x0 = pack(ids.point0.x, PRIME)
y0 = pack(ids.point0.y, PRIME)
x1 = pack(ids.point1.x, PRIME)
y1 = pack(ids.point1.y, PRIME)
value = slope = line_slope(point1=(x0, y0), point2=(x1, y1), p=SECP_P)
%}
let (slope) = nondet_bigint3();

return (slope=slope);
}


func test_compute_slope{range_check_ptr: felt}() {
let x0 = BigInt3(d0=1, d1=5, d2=10);
let y0 = BigInt3(d0=2, d1=4, d2=20);

let pt0 = EcPoint(x=x0, y=y0);

let x1 = BigInt3(d0=3, d1=3, d2=3);
let y1 = BigInt3(d0=3, d1=5, d2=22);

let pt1 = EcPoint(x=x1, y=y1);

// Compute slope
let (slope) = compute_slope(pt0, pt1);

assert slope = slope;
return ();
}

func main{range_check_ptr: felt}(){
test_compute_slope();

return ();
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ use felt::Felt252;
#[cfg(feature = "skip_next_instruction_hint")]
use crate::hint_processor::builtin_hint_processor::skip_next_instruction::skip_next_instruction;

use super::secp::ec_utils::{compute_slope_local_secp_p, import_secp256r1_p};
use super::uint384_extension::unsigned_div_rem_uint768_by_uint384;

pub struct HintProcessorData {
Expand Down Expand Up @@ -408,6 +409,15 @@ impl HintProcessor for BuiltinHintProcessor {
"point0",
"point1",
),
hint_code::COMPUTE_SLOPE_SECP256R1 => compute_slope_local_secp_p(
vm,
exec_scopes,
&hint_data.ids_data,
&hint_data.ap_tracking,
"point0",
"point1",
),
hint_code::IMPORT_SECP256R1_P => import_secp256r1_p(exec_scopes),
hint_code::COMPUTE_SLOPE_WHITELIST => compute_slope(
vm,
exec_scopes,
Expand Down
12 changes: 12 additions & 0 deletions src/hint_processor/builtin_hint_processor/hint_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,18 @@ x1 = pack(ids.point1.x, PRIME)
y1 = pack(ids.point1.y, PRIME)
value = slope = line_slope(point1=(x0, y0), point2=(x1, y1), p=SECP_P)"#;

pub const COMPUTE_SLOPE_SECP256R1: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import pack
from starkware.python.math_utils import line_slope

# Compute the slope.
x0 = pack(ids.point0.x, PRIME)
y0 = pack(ids.point0.y, PRIME)
x1 = pack(ids.point1.x, PRIME)
y1 = pack(ids.point1.y, PRIME)
value = slope = line_slope(point1=(x0, y0), point2=(x1, y1), p=SECP_P)"#;
pub const IMPORT_SECP256R1_P: &str =
"from starkware.cairo.common.cairo_secp.secp256r1_utils import SECP256R1_P as SECP_P";

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

Expand Down
36 changes: 36 additions & 0 deletions src/hint_processor/builtin_hint_processor/secp/ec_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ use num_bigint::BigInt;
use num_integer::Integer;
use num_traits::{One, ToPrimitive, Zero};

use super::secp_utils::SECP256R1_P;

#[derive(Debug, PartialEq)]
struct EcPoint<'a> {
x: BigInt3<'a>,
Expand Down Expand Up @@ -136,6 +138,30 @@ pub fn compute_slope(
exec_scopes.insert_value("slope", value);
Ok(())
}
pub fn compute_slope_local_secp_p(
mfachal marked this conversation as resolved.
Show resolved Hide resolved
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
point0_alias: &str,
point1_alias: &str,
) -> Result<(), HintError> {
//ids.point0
let point0 = EcPoint::from_var_name(point0_alias, vm, ids_data, ap_tracking)?;
//ids.point1
let point1 = EcPoint::from_var_name(point1_alias, vm, ids_data, ap_tracking)?;

let secp_p: BigInt = exec_scopes.get("SECP_P")?;

let value = line_slope(
&(pack(point0.x), pack(point0.y)),
&(pack(point1.x), pack(point1.y)),
&secp_p,
);
exec_scopes.insert_value("value", value.clone());
exec_scopes.insert_value("slope", value);
Ok(())
}

/*
Implements hint:
Expand Down Expand Up @@ -273,6 +299,16 @@ pub fn ec_mul_inner(
insert_value_into_ap(vm, scalar)
}

/*
Implements hint:
%{
from starkware.cairo.common.cairo_secp.secp256r1_utils import SECP256R1_P as SECP_P
%}
*/
pub fn import_secp256r1_p(exec_scopes: &mut ExecutionScopes) -> Result<(), HintError> {
exec_scopes.insert_value("SECP_P", SECP256R1_P.clone());
Ok(())
}
/*
Implements hint:
%{
Expand Down
18 changes: 16 additions & 2 deletions src/hint_processor/builtin_hint_processor/secp/secp_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,22 @@ lazy_static! {
// N = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
pub(crate) static ref N: BigInt = BigInt::from_str(
"115792089237316195423570985008687907852837564279074904382605163141518161494337"
)
.unwrap();
).unwrap();
}
// Constants in package "starkware.cairo.common.cairo_secp.secp256r1_utils"
lazy_static! {
//SECP256R1_P = 2**256 - 2**224 + 2**192 + 2**96 - 1
pub(crate) static ref SECP256R1_P: BigInt = BigInt::from_str(
Oppen marked this conversation as resolved.
Show resolved Hide resolved
"115792089210356248762697446949407573530086143415290314195533631308867097853951"
).unwrap();
//SECP256R1_N = 0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551
pub(crate) static ref SECP256R1_N: BigUint = BigUint::from_str(
"115792089210356248762697446949407573529996955224135760342422259061068512044369"
).unwrap();
//SECP256R1_ALPHA = 0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC
pub(crate) static ref SECP256R1_ALPHA: BigUint = BigUint::from_str(
"115792089210356248762697446949407573530086143415290314195533631308867097853948"
).unwrap();
}

/*
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 @@ -1330,6 +1330,13 @@ fn cairo_run_efficient_secp256r1_ec() {
run_program_simple(program_data.as_slice());
}

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

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