Skip to content

Commit

Permalink
fix(blockifier): rebase and tests - wip
Browse files Browse the repository at this point in the history
  • Loading branch information
PearsonWhite committed Nov 8, 2024
1 parent 6558eea commit c24bf02
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 103 deletions.
102 changes: 0 additions & 102 deletions crates/blockifier/src/execution/native/syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@ use cairo_native::starknet::{
};
use cairo_native::starknet_stub::{big4int_to_u256, encode_str_as_felts, u256_to_biguint};
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use cairo_vm::with_std::fmt;
use num_traits::Zero;
use starknet_api::state::StorageKey;
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::secp;
use crate::execution::syscalls::hint_processor::{SyscallCounter, OUT_OF_GAS_ERROR};
use crate::execution::syscalls::SyscallSelector;
Expand Down Expand Up @@ -376,17 +373,6 @@ impl<'state> StarknetSyscallHandler for &mut NativeSyscallHandler<'state> {
}
}

// From cairo_native/src/starknet_stub.rs

#[derive(PartialEq, Clone, Copy)]
struct Secp256Point<Curve: SWCurveConfig>(Affine<Curve>);

impl<Curve: SWCurveConfig> fmt::Debug for Secp256Point<Curve> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Secp256Point").field(&self.0).finish()
}
}

impl From<Secp256Point<ark_secp256k1::Config>> for Secp256k1Point {
fn from(Secp256Point(Affine { x, y, infinity }): Secp256Point<ark_secp256k1::Config>) -> Self {
Secp256k1Point {
Expand Down Expand Up @@ -427,94 +413,6 @@ impl From<Secp256r1Point> for Secp256Point<ark_secp256r1::Config> {
}
}

// Implementation from: cairo_native/src/starknet_stub.rs

use ark_ff::PrimeField;

impl<Curve: SWCurveConfig> Secp256Point<Curve>
where
Curve::BaseField: PrimeField, // constraint for get_point_by_id
{
// Given a (x,y) pair it will
// - return the point at infinity for (0,0)
// - Err if either x or y is outside of the modulus
// - Ok(None) if (x,y) are within the modules but not on the curve
// - Ok(Some(Point)) if (x,y) are on the curve
fn new(x: U256, y: U256) -> Result<Option<Self>, Vec<Felt>> {
let x = u256_to_biguint(x);
let y = u256_to_biguint(y);
let modulos = Curve::BaseField::MODULUS.into();

if x >= modulos || y >= modulos {
let error = Felt::from_hex(
"0x00000000000000000000000000000000496e76616c696420617267756d656e74",
) // INVALID_ARGUMENT
.map_err(|err| encode_str_as_felts(&err.to_string()))?;

return Err(vec![error]);
}

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

fn add(p0: Self, p1: Self) -> Self {
let result: Projective<Curve> = p0.0 + p1.0;
Secp256Point(result.into())
}

fn mul(p: Self, m: U256) -> Self {
let result = p.0 * Curve::ScalarField::from(u256_to_biguint(m));
Secp256Point(result.into())
}

fn get_point_from_x(x: U256, y_parity: bool) -> Result<Option<Self>, Vec<Felt>> {
let modulos = Curve::BaseField::MODULUS.into();
let x = u256_to_biguint(x);

if x >= modulos {
let error = Felt::from_hex(
"0x00000000000000000000000000000000496e76616c696420617267756d656e74",
) // INVALID_ARGUMENT
.map_err(|err| encode_str_as_felts(&err.to_string()))?;

return Err(vec![error]);
}

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.map(Secp256Point))
}
}

/// 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<Secp256Point<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(Secp256Point(ec_point))
} else {
None
}
}

// todo(xrvdg) remove dead_code annotation after adding syscalls
#[allow(dead_code)]
impl<Curve: SWCurveConfig> Secp256Point<Curve>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ fn test_secp256k1(test_contract: FeatureContract, expected_gas: u64) {
);
}

#[test_case(FeatureContract::TestContract(CairoVersion::Native), 674500; "Native")]
#[cfg_attr(
feature = "cairo_native",
test_case(FeatureContract::TestContract(CairoVersion::Native), 339380; "Native")
)]
#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1), 27565680; "VM")]
fn test_secp256r1(test_contract: FeatureContract, expected_gas: u64) {
let chain_info = &ChainInfo::create_for_testing();
Expand Down

0 comments on commit c24bf02

Please sign in to comment.