Skip to content

Commit

Permalink
feat: missing EC hints for Starknet OS 0.13.1 (#1706)
Browse files Browse the repository at this point in the history
* feat: missing EC hints for Starknet OS 0.13.1

* changelog

* fix clippy
  • Loading branch information
odesenfans authored Apr 15, 2024
1 parent 1d23afb commit 95d2c88
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

* refactor: Remove unused code & use constants whenever possible for builtin instance definitions[#1707](https://github.com/lambdaclass/cairo-vm/pull/1707)

* feat: missing EC hints for Starknet OS 0.13.1 [#1706](https://github.com/lambdaclass/cairo-vm/pull/1706)

* fix(BREAKING): Use program builtins in `initialize_main_entrypoint` & `read_return_values`[#1703](https://github.com/lambdaclass/cairo-vm/pull/1703)
* `initialize_main_entrypoint` now iterates over the program builtins when building the stack & inserts 0 for any missing builtin
* `read_return_values` now only computes the final stack of the builtins in the program
Expand Down
6 changes: 4 additions & 2 deletions fuzzer/src/fuzz_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const HEX_SYMBOLS: [&str; 16] = [
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f",
];

const HINTS_CODE: [&str; 184] = [
const HINTS_CODE: [&str; 186] = [
ADD_SEGMENT,
VM_ENTER_SCOPE,
VM_EXIT_SCOPE,
Expand Down Expand Up @@ -139,10 +139,12 @@ const HINTS_CODE: [&str; 184] = [
EC_DOUBLE_SLOPE_V1,
EC_DOUBLE_SLOPE_V2,
EC_DOUBLE_SLOPE_V3,
EC_DOUBLE_SLOPE_V4,
EC_DOUBLE_SLOPE_EXTERNAL_CONSTS,
COMPUTE_SLOPE_V1,
COMPUTE_SLOPE_V2,
COMPUTE_SLOPE_SECP256R1,
COMPUTE_SLOPE_SECP256R1_V1,
COMPUTE_SLOPE_SECP256R1_V2,
IMPORT_SECP256R1_P,
COMPUTE_SLOPE_WHITELIST,
EC_DOUBLE_ASSIGN_NEW_X_V1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ use crate::hint_processor::builtin_hint_processor::skip_next_instruction::skip_n

#[cfg(feature = "print")]
use crate::hint_processor::builtin_hint_processor::print::{print_array, print_dict, print_felt};
use crate::hint_processor::builtin_hint_processor::secp::secp_utils::{
SECP256R1_ALPHA, SECP256R1_P,
};

use super::blake2s_utils::example_blake2s_compress;

Expand Down Expand Up @@ -532,6 +535,15 @@ impl HintProcessorLogic for BuiltinHintProcessor {
&SECP_P,
&ALPHA,
),
hint_code::EC_DOUBLE_SLOPE_V4 => compute_doubling_slope(
vm,
exec_scopes,
&hint_data.ids_data,
&hint_data.ap_tracking,
"point",
&SECP256R1_P,
&SECP256R1_ALPHA,
),
hint_code::EC_DOUBLE_SLOPE_EXTERNAL_CONSTS => compute_doubling_slope_external_consts(
vm,
exec_scopes,
Expand Down Expand Up @@ -559,13 +571,23 @@ impl HintProcessorLogic for BuiltinHintProcessor {
"point1",
&SECP_P_V2,
),
hint_code::COMPUTE_SLOPE_SECP256R1 => compute_slope(
hint_code::COMPUTE_SLOPE_SECP256R1_V1 => compute_slope(
vm,
exec_scopes,
&hint_data.ids_data,
&hint_data.ap_tracking,
"point0",
"point1",
"SECP_P",
),
hint_code::COMPUTE_SLOPE_SECP256R1_V2 => compute_slope(
vm,
exec_scopes,
&hint_data.ids_data,
&hint_data.ap_tracking,
"point0",
"point1",
"SECP256R1_P",
),
hint_code::IMPORT_SECP256R1_P => import_secp256r1_p(exec_scopes),
hint_code::COMPUTE_SLOPE_WHITELIST => compute_slope_and_assing_secp_p(
Expand Down
22 changes: 21 additions & 1 deletion vm/src/hint_processor/builtin_hint_processor/hint_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,15 @@ x = pack(ids.pt.x, PRIME)
y = pack(ids.pt.y, PRIME)
value = slope = div_mod(3 * x ** 2, 2 * y, SECP_P)"#;

pub const EC_DOUBLE_SLOPE_V4: &str = r#"from starkware.cairo.common.cairo_secp.secp256r1_utils import SECP256R1_ALPHA, SECP256R1_P
from starkware.cairo.common.cairo_secp.secp_utils import pack
from starkware.python.math_utils import ec_double_slope
# Compute the slope.
x = pack(ids.point.x, SECP256R1_P)
y = pack(ids.point.y, SECP256R1_P)
value = slope = ec_double_slope(point=(x, y), alpha=SECP256R1_ALPHA, p=SECP256R1_P)"#;

pub const EC_DOUBLE_SLOPE_EXTERNAL_CONSTS: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import pack
from starkware.python.math_utils import ec_double_slope
Expand Down Expand Up @@ -722,7 +731,7 @@ 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
pub const COMPUTE_SLOPE_SECP256R1_V1: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import pack
from starkware.python.math_utils import line_slope
# Compute the slope.
Expand All @@ -732,6 +741,17 @@ 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_V2: &str = r#"from starkware.cairo.common.cairo_secp.secp256r1_utils import SECP256R1_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=SECP256R1_P)"#;

pub const IMPORT_SECP256R1_P: &str =
"from starkware.cairo.common.cairo_secp.secp256r1_utils import SECP256R1_P as SECP_P";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ pub fn compute_slope_and_assing_secp_p(
ap_tracking,
point0_alias,
point1_alias,
"SECP_P",
)
}

Expand All @@ -208,13 +209,14 @@ pub fn compute_slope(
ap_tracking: &ApTracking,
point0_alias: &str,
point1_alias: &str,
secp_p_name: &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 secp_p: BigInt = exec_scopes.get(secp_p_name)?;

let value = line_slope(
&(point0.x.pack86(), point0.y.pack86()),
Expand Down

0 comments on commit 95d2c88

Please sign in to comment.