Skip to content

Commit

Permalink
Feature: SECP syscall and hints support (#288)
Browse files Browse the repository at this point in the history
This PR handles all the Secp syscalls for both `k1` and `r1` curves. It includes:

- secp_new
- secp_add
- secp_mul
- secp_get_point_from_x
- secp_get_xy

It also implements all the hints related to these syscalls.

---------

Co-authored-by: whichqua <[email protected]>
Co-authored-by: Olivier Desenfans <[email protected]>
  • Loading branch information
3 people authored Aug 29, 2024
1 parent 787470f commit db7810b
Show file tree
Hide file tree
Showing 14 changed files with 817 additions and 383 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ license-file = "LICENSE"

[workspace.dependencies]
anyhow = "1.0.75"
ark-ec = "0.4.2"
ark-ff = "0.4.2"
ark-secp256k1 = "0.4"
ark-secp256r1 = "0.4"
assert_matches = "1.5.0"
base64 = "0.21.3"
bitvec = { version = "1.0.1", features = ["serde"] }
Expand All @@ -30,7 +34,7 @@ cairo-lang-starknet-classes = { version = "=2.7.1" }
cairo-lang-utils = { version = "=2.7.1" }
cairo-lang-casm = { version = "=2.7.1" }
cairo-type-derive = { version = "0.1.0", path = "crates/cairo-type-derive" }
cairo-vm = { version = "=1.0.1", features = ["extensive_hints", "cairo-1-hints"] }
cairo-vm = { git = "https://github.com/Moonsong-Labs/cairo-vm", rev = "40ebe36e64ffa429ff18cba8ef0bd7d2c122ac12", features = ["extensive_hints", "cairo-1-hints"] }
clap = { version = "4.5.4", features = ["derive"] }
env_logger = "0.11.3"
flate2 = "1.0.32"
Expand Down
4 changes: 4 additions & 0 deletions crates/starknet-os/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ license-file.workspace = true

[dependencies]
anyhow = { workspace = true }
ark-ec = { workspace = true }
ark-ff = { workspace = true }
ark-secp256k1 = { workspace = true }
ark-secp256r1 = { workspace = true }
base64 = { workspace = true }
bitvec = { workspace = true }
blockifier = { workspace = true }
Expand Down
29 changes: 29 additions & 0 deletions crates/starknet-os/src/cairo_types/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,32 @@ pub struct GetTxSignature {
#[allow(unused)]
pub response: GetTxSignatureResponse,
}

#[allow(unused)]
#[derive(FieldOffsetGetters)]
pub struct SecpNewResponse {
#[allow(unused)]
pub not_on_curve: Felt252,
#[allow(unused)]
pub ec_point: Relocatable,
}

#[allow(unused)]
#[derive(FieldOffsetGetters)]
pub struct EcPoint {
#[allow(unused)]
pub d0: Felt252,
#[allow(unused)]
pub d1: Felt252,
#[allow(unused)]
pub d3: Felt252,
}

#[allow(unused)]
#[derive(FieldOffsetGetters)]
pub struct EcCoordinate {
#[allow(unused)]
pub x: EcPoint,
#[allow(unused)]
pub y: EcPoint,
}
33 changes: 31 additions & 2 deletions crates/starknet-os/src/execution/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use cairo_vm::Felt252;
use starknet_api::deprecated_contract_class::EntryPointType;
use tokio::sync::RwLock;

use super::secp_handler::SecpSyscallProcessor;
use crate::config::STORED_BLOCK_HASH_BUFFER;
use crate::starknet::starknet_storage::{CommitmentInfo, CommitmentInfoError, PerContractStorage};
use crate::storage::storage::StorageError;
Expand All @@ -21,7 +22,6 @@ use crate::storage::storage::StorageError;
pub type ContractStorageMap<PCS> = HashMap<Felt252, PCS>;

/// Maintains the info for executing txns in the OS
#[derive(Debug)]
pub struct ExecutionHelper<PCS>
where
PCS: PerContractStorage,
Expand Down Expand Up @@ -53,8 +53,11 @@ where
pub execute_code_read_iter: IntoIter<Felt252>,
// Per-contract storage
pub storage_by_address: ContractStorageMap<PCS>,
}

// Secp syscall processors.
pub secp256k1_syscall_processor: SecpSyscallProcessor<ark_secp256k1::Config>,
pub secp256r1_syscall_processor: SecpSyscallProcessor<ark_secp256r1::Config>,
}
/// ExecutionHelper is wrapped in Rc<RefCell<_>> in order
/// to clone the refrence when entering and exiting vm scopes
#[derive(Debug)]
Expand All @@ -71,6 +74,30 @@ where
}
}

impl<PCS> std::fmt::Debug for ExecutionHelper<PCS>
where
PCS: PerContractStorage + std::fmt::Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ExecutionHelper")
.field("_prev_block_context", &self._prev_block_context)
.field("tx_execution_info_iter", &self.tx_execution_info_iter)
.field("tx_execution_info", &self.tx_execution_info)
.field("tx_info_ptr", &self.tx_info_ptr)
.field("call_execution_info_ptr", &self.call_execution_info_ptr)
.field("old_block_number_and_hash", &self.old_block_number_and_hash)
.field("call_iter", &self.call_iter)
.field("call_info", &self.call_info)
.field("result_iter", &self.result_iter)
.field("deployed_contracts_iter", &self.deployed_contracts_iter)
.field("execute_code_read_iter", &self.execute_code_read_iter)
.field("storage_by_address", &self.storage_by_address)
.field("secp256k1_syscall_processor", &"SecpHintProcessor<ark_secp256k1::Config>")
.field("secp256r1_syscall_processor", &"SecpHintProcessor<ark_secp256r1::Config>")
.finish()
}
}

impl<PCS> ExecutionHelperWrapper<PCS>
where
PCS: PerContractStorage + 'static,
Expand Down Expand Up @@ -104,6 +131,8 @@ where
deployed_contracts_iter: vec![].into_iter(),
execute_code_read_iter: vec![].into_iter(),
storage_by_address: contract_storage_map,
secp256k1_syscall_processor: Default::default(),
secp256r1_syscall_processor: Default::default(),
})),
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/starknet-os/src/execution/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ mod constants;
pub mod deprecated_syscall_handler;
pub mod execute_syscalls;
pub mod helper;
pub mod secp_handler;
pub mod syscall_handler;
pub mod syscall_handler_utils;
Loading

0 comments on commit db7810b

Please sign in to comment.