Skip to content

Commit

Permalink
chore(blockifier): move shared implementation into separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
xrvdg committed Nov 5, 2024
1 parent 8a3d886 commit 96e2939
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 94 deletions.
2 changes: 2 additions & 0 deletions crates/blockifier/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub mod entry_point_execution;
pub mod errors;
pub mod execution_utils;
pub mod hint_code;
pub mod secp;

#[cfg(feature = "cairo_native")]
pub mod native;
pub mod stack_trace;
Expand Down
89 changes: 5 additions & 84 deletions crates/blockifier/src/execution/native/syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::fmt;
use std::hash::RandomState;

use ark_ec::short_weierstrass::{Affine, Projective, SWCurveConfig};
use ark_ff::{PrimeField, Zero};
use ark_ff::PrimeField;
use cairo_native::starknet::{
ExecutionInfo,
ExecutionInfoV2,
Expand All @@ -22,11 +22,8 @@ use starknet_types_core::felt::Felt;
use crate::execution::call_info::{CallInfo, OrderedEvent, OrderedL2ToL1Message, Retdata};
use crate::execution::entry_point::{CallEntryPoint, EntryPointExecutionContext};
use crate::execution::native::utils::encode_str_as_felts;
use crate::execution::syscalls::hint_processor::{
SyscallCounter,
SyscallExecutionError,
OUT_OF_GAS_ERROR,
};
use crate::execution::secp;
use crate::execution::syscalls::hint_processor::{SyscallCounter, OUT_OF_GAS_ERROR};
use crate::execution::syscalls::SyscallSelector;
use crate::state::state_api::State;

Expand Down Expand Up @@ -345,7 +342,7 @@ where
let x = u256_to_biguint(x);
let y = u256_to_biguint(y);

match new_affine(x, y) {
match secp::new_affine(x, y) {
Ok(None) => Ok(None),
Ok(Some(affine)) => Ok(Some(Secp256Point(affine))),
Err(error) => Err(encode_str_as_felts(&error.to_string())),
Expand All @@ -365,90 +362,14 @@ where
fn get_point_from_x(x: U256, y_parity: bool) -> Result<Option<Self>, Vec<Felt>> {
let x = u256_to_biguint(x);

match get_point_from_x(x, y_parity) {
match secp::get_point_from_x(x, y_parity) {
Ok(None) => Ok(None),
Ok(Some(point)) => Ok(Some(Secp256Point(point))),
Err(error) => Err(encode_str_as_felts(&error.to_string())),
}
}
}

pub fn get_point_from_x<Curve: SWCurveConfig>(
x: num_bigint::BigUint,
y_parity: bool,
) -> Result<Option<Affine<Curve>>, SyscallExecutionError>
where
Curve::BaseField: PrimeField, // constraint for get_point_by_id
{
let _ = modulus_bound_check::<Curve>(&[&x])?;

let x = x.into();
let maybe_ec_point = Affine::<Curve>::get_ys_from_x_unchecked(x)
.map(|(smaller, greater)| {
// Return the correct y coordinate based on the parity.
if ark_ff::BigInteger::is_odd(&smaller.into_bigint()) == y_parity {
smaller
} else {
greater
}
})
.map(|y| Affine::<Curve>::new_unchecked(x, y))
.filter(|p| p.is_in_correct_subgroup_assuming_on_curve());

Ok(maybe_ec_point)
}

pub fn new_affine<Curve: SWCurveConfig>(
x: num_bigint::BigUint,
y: num_bigint::BigUint,
) -> Result<Option<Affine<Curve>>, SyscallExecutionError>
where
Curve::BaseField: PrimeField, // constraint for get_point_by_id
{
let _ = modulus_bound_check::<Curve>(&[&x, &y])?;

Ok(maybe_affine(x.into(), y.into()))
}

fn modulus_bound_check<Curve: SWCurveConfig>(
bounds: &[&num_bigint::BigUint],
) -> Result<(), SyscallExecutionError>
where
Curve::BaseField: PrimeField, // constraint for get_point_by_id
{
let modulos = Curve::BaseField::MODULUS.into();

if bounds.iter().any(|p| **p >= modulos) {
let error =
match Felt::from_hex(crate::execution::syscalls::hint_processor::INVALID_ARGUMENT) {
Ok(err) => SyscallExecutionError::SyscallError { error_data: vec![err] },
Err(err) => SyscallExecutionError::from(err),
};

return Err(error);
}

Ok(())
}

/// Variation on [`Affine<Curve>::new`] that doesn't panic and maps (x,y) = (0,0) -> infinity
fn maybe_affine<Curve: SWCurveConfig>(
x: Curve::BaseField,
y: Curve::BaseField,
) -> Option<Affine<Curve>> {
let ec_point = if x.is_zero() && y.is_zero() {
Affine::<Curve>::identity()
} else {
Affine::<Curve>::new_unchecked(x, y)
};

if ec_point.is_on_curve() && ec_point.is_in_correct_subgroup_assuming_on_curve() {
Some(ec_point)
} else {
None
}
}

/// Data structure to tie together k1 and r1 points to it's corresponding
/// `Affine<Curve>`
#[derive(PartialEq, Clone, Copy)]
Expand Down
80 changes: 80 additions & 0 deletions crates/blockifier/src/execution/secp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use ark_ec::short_weierstrass::{Affine, SWCurveConfig};
use ark_ff::{PrimeField, Zero};
use starknet_types_core::felt::Felt;

use super::syscalls::hint_processor::{SyscallExecutionError, INVALID_ARGUMENT};

pub fn get_point_from_x<Curve: SWCurveConfig>(
x: num_bigint::BigUint,
y_parity: bool,
) -> Result<Option<Affine<Curve>>, SyscallExecutionError>
where
Curve::BaseField: PrimeField, // constraint for get_point_by_id
{
modulus_bound_check::<Curve>(&[&x])?;

let x = x.into();
let maybe_ec_point = Affine::<Curve>::get_ys_from_x_unchecked(x)
.map(|(smaller, greater)| {
// Return the correct y coordinate based on the parity.
if ark_ff::BigInteger::is_odd(&smaller.into_bigint()) == y_parity {
smaller
} else {
greater
}
})
.map(|y| Affine::<Curve>::new_unchecked(x, y))
.filter(|p| p.is_in_correct_subgroup_assuming_on_curve());

Ok(maybe_ec_point)
}

pub fn new_affine<Curve: SWCurveConfig>(
x: num_bigint::BigUint,
y: num_bigint::BigUint,
) -> Result<Option<Affine<Curve>>, SyscallExecutionError>
where
Curve::BaseField: PrimeField, // constraint for get_point_by_id
{
modulus_bound_check::<Curve>(&[&x, &y])?;

Ok(maybe_affine(x.into(), y.into()))
}

fn modulus_bound_check<Curve: SWCurveConfig>(
bounds: &[&num_bigint::BigUint],
) -> Result<(), SyscallExecutionError>
where
Curve::BaseField: PrimeField, // constraint for get_point_by_id
{
let modulos = Curve::BaseField::MODULUS.into();

if bounds.iter().any(|p| **p >= modulos) {
let error = match Felt::from_hex(INVALID_ARGUMENT) {
Ok(err) => SyscallExecutionError::SyscallError { error_data: vec![err] },
Err(err) => SyscallExecutionError::from(err),
};

return Err(error);
}

Ok(())
}

/// Variation on [`Affine<Curve>::new`] that doesn't panic and maps (x,y) = (0,0) -> infinity
fn maybe_affine<Curve: SWCurveConfig>(
x: Curve::BaseField,
y: Curve::BaseField,
) -> Option<Affine<Curve>> {
let ec_point = if x.is_zero() && y.is_zero() {
Affine::<Curve>::identity()
} else {
Affine::<Curve>::new_unchecked(x, y)
};

if ec_point.is_on_curve() && ec_point.is_in_correct_subgroup_assuming_on_curve() {
Some(ec_point)
} else {
None
}
}
15 changes: 5 additions & 10 deletions crates/blockifier/src/execution/syscalls/secp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use ark_ec::short_weierstrass;
use ark_ec::short_weierstrass::SWCurveConfig;
use ark_ff::{BigInteger, PrimeField};
use ark_ec::short_weierstrass::{self, SWCurveConfig};
use ark_ff::PrimeField;
use cairo_vm::types::relocatable::Relocatable;
use cairo_vm::vm::vm_core::VirtualMachine;
use num_bigint::BigUint;
Expand All @@ -9,12 +8,8 @@ use starknet_types_core::felt::Felt;

use crate::abi::sierra_types::{SierraType, SierraU256};
use crate::execution::execution_utils::{felt_from_ptr, write_maybe_relocatable, write_u256};
use crate::execution::native::syscall_handler::{get_point_from_x, new_affine};
use crate::execution::syscalls::hint_processor::{
felt_to_bool,
SyscallHintProcessor,
INVALID_ARGUMENT,
};
use crate::execution::secp::new_affine;
use crate::execution::syscalls::hint_processor::{felt_to_bool, SyscallHintProcessor};
use crate::execution::syscalls::{
SyscallExecutionError,
SyscallRequest,
Expand Down Expand Up @@ -51,7 +46,7 @@ where
&mut self,
request: SecpGetPointFromXRequest,
) -> SyscallResult<SecpGetPointFromXResponse> {
let affine = get_point_from_x(request.x, request.y_parity);
let affine = crate::execution::secp::get_point_from_x(request.x, request.y_parity);

affine.map(|maybe_ec_point| SecpGetPointFromXResponse {
optional_ec_point_id: maybe_ec_point.map(|ec_point| self.allocate_point(ec_point)),
Expand Down

0 comments on commit 96e2939

Please sign in to comment.