From c24bf02f894fdb9de613f40e6c2872360404e0ba Mon Sep 17 00:00:00 2001 From: Pearson White Date: Thu, 7 Nov 2024 10:37:53 -0500 Subject: [PATCH] fix(blockifier): rebase and tests - wip --- .../src/execution/native/syscall_handler.rs | 102 ------------------ .../execution/syscalls/syscall_tests/secp.rs | 5 +- 2 files changed, 4 insertions(+), 103 deletions(-) diff --git a/crates/blockifier/src/execution/native/syscall_handler.rs b/crates/blockifier/src/execution/native/syscall_handler.rs index 2e7ebaa6eb..753efba27b 100644 --- a/crates/blockifier/src/execution/native/syscall_handler.rs +++ b/crates/blockifier/src/execution/native/syscall_handler.rs @@ -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; @@ -376,17 +373,6 @@ impl<'state> StarknetSyscallHandler for &mut NativeSyscallHandler<'state> { } } -// From cairo_native/src/starknet_stub.rs - -#[derive(PartialEq, Clone, Copy)] -struct Secp256Point(Affine); - -impl fmt::Debug for Secp256Point { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_tuple("Secp256Point").field(&self.0).finish() - } -} - impl From> for Secp256k1Point { fn from(Secp256Point(Affine { x, y, infinity }): Secp256Point) -> Self { Secp256k1Point { @@ -427,94 +413,6 @@ impl From for Secp256Point { } } -// Implementation from: cairo_native/src/starknet_stub.rs - -use ark_ff::PrimeField; - -impl Secp256Point -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, Vec> { - 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 = 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, Vec> { - 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::::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::::new_unchecked(x, y)) - .filter(|p| p.is_in_correct_subgroup_assuming_on_curve()); - - Ok(maybe_ec_point.map(Secp256Point)) - } -} - -/// Variation on [`Affine::new`] that doesn't panic and maps (x,y) = (0,0) -> infinity -fn maybe_affine( - x: Curve::BaseField, - y: Curve::BaseField, -) -> Option> { - let ec_point = if x.is_zero() && y.is_zero() { - Affine::::identity() - } else { - Affine::::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 Secp256Point diff --git a/crates/blockifier/src/execution/syscalls/syscall_tests/secp.rs b/crates/blockifier/src/execution/syscalls/syscall_tests/secp.rs index bdefe68744..97f8b89903 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_tests/secp.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_tests/secp.rs @@ -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();