diff --git a/Cargo.lock b/Cargo.lock index f91677f9..1a4110fc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1803,9 +1803,9 @@ dependencies = [ [[package]] name = "rustls-webpki" -version = "0.101.5" +version = "0.101.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "45a27e3b59326c16e23d30aeb7a36a24cc0d29e71d68ff611cdfb4a01d013bed" +checksum = "3c7d5dece342910d9ba34d259310cae3e0154b873b35408b787b59bce53d34fe" dependencies = [ "ring", "untrusted", diff --git a/src/business_logic/transaction/types.rs b/src/business_logic/transaction/types.rs index 61def86c..6e0ea0b7 100644 --- a/src/business_logic/transaction/types.rs +++ b/src/business_logic/transaction/types.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; +use cairo_felt::Felt252; use serde::{Deserialize, Serialize}; -use starknet::core::types::FieldElement; // TODO: Complete this module @@ -13,6 +13,6 @@ struct Transaction { #[derive(Serialize, Deserialize)] pub struct InternalTransaction { - hash_value: FieldElement, + hash_value: Felt252, external_to_internal_cls: HashMap, } diff --git a/src/error/mod.rs b/src/error/mod.rs index dde69d99..88501b10 100644 --- a/src/error/mod.rs +++ b/src/error/mod.rs @@ -1,4 +1,4 @@ -use starknet::core::types::FieldElement; +use cairo_felt::Felt252; #[derive(thiserror::Error, Clone, Debug)] pub enum SnOsError { @@ -19,11 +19,11 @@ pub enum CommitmentInfoError { #[error("Inconsistent tree heights : {0} {1}.")] InconsistentTreeHeights(usize, usize), #[error("Inconsistent tree roots : {0} {1}.")] - InconsistentTreeRoots(FieldElement, FieldElement), + InconsistentTreeRoots(Felt252, Felt252), } #[derive(thiserror::Error, Clone, Debug)] pub enum FactTreeError { #[error("Unexpected result on single leaf index : {0}")] - UnexpectedResult(FieldElement), + UnexpectedResult(Felt252), } diff --git a/src/fact_state/contract_state.rs b/src/fact_state/contract_state.rs index 4778f8c6..a4889bb2 100644 --- a/src/fact_state/contract_state.rs +++ b/src/fact_state/contract_state.rs @@ -1,4 +1,4 @@ -use starknet::core::types::FieldElement; +use cairo_felt::Felt252; use crate::{ storage::{DBObject, Fact}, @@ -13,18 +13,16 @@ use crate::{ #[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)] pub struct ContractState { - contract_hash: FieldElement, + contract_hash: Felt252, storage_commitment_tree: PatriciaTree, - nonce: FieldElement, + nonce: Felt252, } impl LeafFact for ContractState { fn is_empty(&self) -> bool { - self.storage_commitment_tree.root - == FieldElement::from_byte_slice_be(EMPTY_NODE_HASH.as_slice()).unwrap() - && self.contract_hash - == FieldElement::from_byte_slice_be(UNINITIALIZED_CLASS_HASH.as_slice()).unwrap() - && self.nonce == FieldElement::ZERO + self.storage_commitment_tree.root == Felt252::from_bytes_be(EMPTY_NODE_HASH.as_slice()) + && self.contract_hash == Felt252::from_bytes_be(UNINITIALIZED_CLASS_HASH.as_slice()) + && self.nonce == Felt252::new(0) } } @@ -36,7 +34,7 @@ impl Fact for ContractState { return EMPTY_NODE_HASH.to_vec(); } - let contract_state_hash_version = FieldElement::ZERO; + let contract_state_hash_version = Felt252::new(0); // Set hash_value = H(H(contract_hash, storage_root), RESERVED) let hash_value = H::hash_elements(self.contract_hash, self.storage_commitment_tree.root); diff --git a/src/os_input.rs b/src/os_input.rs index 4f9c3a66..a2e6105f 100644 --- a/src/os_input.rs +++ b/src/os_input.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; +use cairo_felt::Felt252; use serde::{Deserialize, Serialize}; -use starknet::core::types::FieldElement; use crate::{ business_logic::transaction::types::InternalTransaction, @@ -14,11 +14,11 @@ use crate::{ struct StarknetOsInput { contract_state_commitment_info: CommitmentInfo, contract_class_commitment_info: CommitmentInfo, - deprecated_compiled_classes: HashMap, // TODO: Add contract_class module - compiled_classes: HashMap, // TODO: Add contract_class module - contracts: HashMap, - class_hash_to_compiled_class_hash: HashMap, + deprecated_compiled_classes: HashMap, // TODO: Add contract_class module + compiled_classes: HashMap, // TODO: Add contract_class module + contracts: HashMap, + class_hash_to_compiled_class_hash: HashMap, general_config: StarknetGeneralConfig, transactions: Vec, - block_hash: FieldElement, + block_hash: Felt252, } diff --git a/src/storage/starknet.rs b/src/storage/starknet.rs index ba053d47..918d0ab4 100644 --- a/src/storage/starknet.rs +++ b/src/storage/starknet.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use std::{collections::HashMap, marker::PhantomData}; -use starknet::core::types::FieldElement; +use cairo_felt::Felt252; use crate::{ error::CommitmentInfoError, @@ -15,12 +15,12 @@ use crate::{ use super::{FactCheckingContext, Storage}; -type CommitmentFacts = HashMap>; +type CommitmentFacts = HashMap>; #[derive(Serialize, Deserialize)] pub struct CommitmentInfo { - pub previous_root: FieldElement, - pub updated_root: FieldElement, + pub previous_root: Felt252, + pub updated_root: Felt252, tree_height: usize, commitment_facts: CommitmentFacts, _phantom_data: PhantomData, @@ -35,7 +35,7 @@ impl CommitmentInfo { &mut self, previous_tree: PatriciaTree, expected_updated_tree: PatriciaTree, - expected_accessed_indices: Vec, + expected_accessed_indices: Vec, ffc: FactCheckingContext, ) -> Result, CommitmentInfoError> { if previous_tree.height != expected_updated_tree.height { @@ -45,7 +45,7 @@ impl CommitmentInfo { )); } - let modifications: HashMap = expected_updated_tree + let modifications: HashMap = expected_updated_tree .get_leaves(&ffc, expected_accessed_indices, None) .await; @@ -66,15 +66,15 @@ impl CommitmentInfo { pub async fn create_from_modifications( &mut self, previous_tree: PatriciaTree, - expected_updated_root: FieldElement, - modifications: HashMap, + expected_updated_root: Felt252, + modifications: HashMap, ffc: &FactCheckingContext, ) -> Result, CommitmentInfoError> { let mut commitment_facts = CommitmentFacts::new(); let actual_updated_tree = previous_tree .update(ffc, modifications, Some(&mut commitment_facts)) .await; - let actual_updated_root: FieldElement = actual_updated_tree.root; + let actual_updated_root: Felt252 = actual_updated_tree.root; if actual_updated_root != expected_updated_root { return Err(CommitmentInfoError::InconsistentTreeRoots( diff --git a/src/utils/commitment_tree/binary_fact_tree.rs b/src/utils/commitment_tree/binary_fact_tree.rs index 941885da..2c1bc387 100644 --- a/src/utils/commitment_tree/binary_fact_tree.rs +++ b/src/utils/commitment_tree/binary_fact_tree.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use starknet::core::types::FieldElement; +use cairo_felt::Felt252; use crate::{ error::FactTreeError, @@ -10,7 +10,7 @@ use crate::{ use super::nodes::InnerNodeFact; -pub type BinaryFactDict = HashMap>; +pub type BinaryFactDict = HashMap>; pub trait BinaryFactTree where @@ -27,28 +27,28 @@ where async fn get_leaves( &self, ffc: &FactCheckingContext, - indices: Vec, + indices: Vec, facts: Option, - ) -> HashMap; + ) -> HashMap; async fn _get_leaves( &self, ffc: &FactCheckingContext, - indices: Vec, + indices: Vec, facts: Option, - ) -> HashMap; + ) -> HashMap; async fn update( &self, ffc: &FactCheckingContext, - modifications: HashMap, + modifications: HashMap, facts: Option<&mut BinaryFactDict>, ) -> Self; async fn get_leaf( &self, ffc: &FactCheckingContext, - index: FieldElement, + index: Felt252, ) -> Result { let leaves = self.get_leaves(ffc, vec![index], None).await; if leaves.keys().ne([index].iter()) { diff --git a/src/utils/commitment_tree/nodes.rs b/src/utils/commitment_tree/nodes.rs index 73a68d8c..ef198530 100644 --- a/src/utils/commitment_tree/nodes.rs +++ b/src/utils/commitment_tree/nodes.rs @@ -1,5 +1,5 @@ use bitvec::{prelude::Msb0, vec::BitVec}; -use starknet::core::types::FieldElement; +use cairo_felt::Felt252; use crate::{ storage::{DBObject, Fact, HASH_BYTES}, @@ -10,7 +10,7 @@ use serde::{Deserialize, Serialize}; pub const EMPTY_NODE_HASH: [u8; 4] = HASH_BYTES; -pub const EMPTY_NODE_PREIMAGE_LENGTH: FieldElement = FieldElement::ZERO; +pub const EMPTY_NODE_PREIMAGE_LENGTH: Felt252 = Felt252::new(0); /// A node in a Binary Merkle-Patricia Tree graph. #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] @@ -36,14 +36,14 @@ impl Fact for EmptyNode { #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct BinaryNode { - left: FieldElement, - right: FieldElement, + left: Felt252, + right: Felt252, } impl BinaryNode { #[allow(unused)] - pub(crate) fn preimage_length() -> FieldElement { - FieldElement::TWO * FieldElement::from_byte_slice_be(HASH_BYTES.as_slice()).unwrap() + pub(crate) fn preimage_length() -> Felt252 { + Felt252::new(2) * Felt252::from_bytes_be(HASH_BYTES.as_slice()).unwrap() } } @@ -65,9 +65,8 @@ pub struct EdgeNode { impl EdgeNode { #[allow(unused)] - pub(crate) fn preimage_length() -> FieldElement { - FieldElement::TWO * FieldElement::from_byte_slice_be(HASH_BYTES.as_slice()).unwrap() - + FieldElement::ONE + pub(crate) fn preimage_length() -> Felt252 { + Felt252::new(2) * Felt252::from_bytes_be(HASH_BYTES.as_slice()).unwrap() + Felt252::new(1) } } @@ -80,7 +79,7 @@ impl Fact for EdgeNode { let bottom_path_hash = H::hash_elements( self.bottom_node.hash().unwrap(), - FieldElement::from_byte_slice_be(bvec.as_slice()).unwrap(), + Felt252::from_bytes_be(bvec.as_slice()).unwrap(), ); // Add the edge length. @@ -94,7 +93,7 @@ impl Fact for EdgeNode { impl InnerNodeFact { /// Returns true if the node represents an empty node -- this is defined as a node - /// with the [FieldElement::ZERO]. + /// with the [Felt252::new(0)]. /// /// This can occur for the root node in an empty graph. pub fn is_empty(&self) -> bool { @@ -109,20 +108,17 @@ impl InnerNodeFact { } /// Get the hash of an inner node fact. - pub fn hash(&self) -> Option { + pub fn hash(&self) -> Option { match self { - InnerNodeFact::Empty(empty) => Some( - FieldElement::from_byte_slice_be(empty._hash::().as_slice()) - .unwrap(), - ), - InnerNodeFact::Binary(binary) => Some( - FieldElement::from_byte_slice_be(binary._hash::().as_slice()) - .unwrap(), - ), - InnerNodeFact::Edge(edge) => Some( - FieldElement::from_byte_slice_be(edge._hash::().as_slice()) - .unwrap(), - ), + InnerNodeFact::Empty(empty) => { + Some(Felt252::from_bytes_be(empty._hash::().as_slice()).unwrap()) + } + InnerNodeFact::Binary(binary) => { + Some(Felt252::from_bytes_be(binary._hash::().as_slice()).unwrap()) + } + InnerNodeFact::Edge(edge) => { + Some(Felt252::from_bytes_be(edge._hash::().as_slice()).unwrap()) + } } } } diff --git a/src/utils/commitment_tree/patricia_tree.rs b/src/utils/commitment_tree/patricia_tree.rs index 164578d8..67d58ccd 100644 --- a/src/utils/commitment_tree/patricia_tree.rs +++ b/src/utils/commitment_tree/patricia_tree.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use starknet::core::types::FieldElement; +use cairo_felt::Felt252; use crate::{ error::FactTreeError, @@ -15,7 +15,7 @@ use super::{ #[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)] pub struct PatriciaTree { - pub root: FieldElement, + pub root: Felt252, pub height: usize, } @@ -32,25 +32,25 @@ impl BinaryFactTree for PatriciaTree { async fn get_leaves( &self, _ffc: &FactCheckingContext, - _indices: Vec, + _indices: Vec, _facts: Option, - ) -> HashMap { + ) -> HashMap { todo!() } async fn _get_leaves( &self, _ffc: &FactCheckingContext, - _indices: Vec, + _indices: Vec, _facts: Option, - ) -> HashMap { + ) -> HashMap { todo!() } async fn update( &self, _ffc: &FactCheckingContext, - _modifications: HashMap, + _modifications: HashMap, _facts: Option<&mut BinaryFactDict>, ) -> Self { todo!() @@ -59,7 +59,7 @@ impl BinaryFactTree for PatriciaTree { async fn get_leaf( &self, _ffc: &FactCheckingContext, - _index: FieldElement, + _index: Felt252, ) -> Result { todo!() } diff --git a/src/utils/definitions/general_config.rs b/src/utils/definitions/general_config.rs index c38badf0..840df41b 100644 --- a/src/utils/definitions/general_config.rs +++ b/src/utils/definitions/general_config.rs @@ -1,6 +1,5 @@ +use cairo_felt::Felt252; use serde::{Deserialize, Serialize}; -use starknet::core::chain_id::TESTNET; -use starknet::core::types::FieldElement; use std::collections::HashMap; use std::path::PathBuf; use tokio::sync::OnceCell; @@ -9,7 +8,7 @@ use super::constants; const GENERAL_CONFIG_FILE_NAME: &str = "general_config.yml"; const N_STEPS_RESOURCE: &str = "n_steps"; -const DEFAULT_CHAIN_ID: FieldElement = TESTNET; +const DEFAULT_CHAIN_ID: Felt252 = Felt252::new(0); // Fix this // Default configuration values. @@ -22,15 +21,15 @@ pub const DEFAULT_GAS_PRICE: usize = 10usize.pow(8); #[derive(Debug, Serialize, Deserialize)] struct StarknetOsConfig { - chain_id: FieldElement, - fee_token_address: FieldElement, + chain_id: Felt252, + fee_token_address: Felt252, } impl Default for StarknetOsConfig { fn default() -> Self { StarknetOsConfig { chain_id: DEFAULT_CHAIN_ID, - fee_token_address: FieldElement::ZERO, + fee_token_address: Felt252::new(0), } } } @@ -45,7 +44,7 @@ pub struct StarknetGeneralConfig { validate_max_n_steps: usize, min_gas_price: usize, constant_gas_price: bool, - sequencer_address: FieldElement, + sequencer_address: Felt252, tx_commitment_tree_height: usize, event_commitment_tree_height: usize, cairo_resource_fee_weights: HashMap, @@ -67,7 +66,7 @@ impl Default for StarknetGeneralConfig { validate_max_n_steps: DEFAULT_VALIDATE_MAX_STEPS, min_gas_price: DEFAULT_GAS_PRICE, constant_gas_price: false, - sequencer_address: FieldElement::ZERO, // TODO: Add real value + sequencer_address: Felt252::new(0), // TODO: Add real value tx_commitment_tree_height: constants::TRANSACTION_COMMITMENT_TREE_HEIGHT, event_commitment_tree_height: constants::EVENT_COMMITMENT_TREE_HEIGHT, cairo_resource_fee_weights: HashMap::default(), // TODO: Add builtins module @@ -77,11 +76,11 @@ impl Default for StarknetGeneralConfig { } impl StarknetGeneralConfig { - fn chain_id(&self) -> FieldElement { + fn chain_id(&self) -> Felt252 { self.starknet_os_config.chain_id } - fn fee_token_address(&self) -> FieldElement { + fn fee_token_address(&self) -> Felt252 { self.starknet_os_config.fee_token_address } } diff --git a/src/utils/hasher/mod.rs b/src/utils/hasher/mod.rs index d2182fee..af520d49 100644 --- a/src/utils/hasher/mod.rs +++ b/src/utils/hasher/mod.rs @@ -1,7 +1,7 @@ -pub mod pedersen; - use starknet::core::types::FieldElement; +pub mod pedersen; + /// A trait for hashing. pub trait HasherT { /// Hashes the given data. diff --git a/src/utils/hasher/pedersen.rs b/src/utils/hasher/pedersen.rs index 7801311a..3d5c0b1a 100644 --- a/src/utils/hasher/pedersen.rs +++ b/src/utils/hasher/pedersen.rs @@ -1,10 +1,6 @@ -use starknet::core::{ - crypto::{compute_hash_on_elements, pedersen_hash}, - types::FieldElement, -}; - use super::HasherT; - +use starknet::core::crypto::{compute_hash_on_elements, pedersen_hash}; +use starknet::core::types::FieldElement; /// The Pedersen hasher. #[derive(Clone, Copy)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]