From 51694b75fad50bf607c967b069b0095dc6ec9fc0 Mon Sep 17 00:00:00 2001 From: fishseabowl Date: Thu, 30 May 2024 08:19:34 -0700 Subject: [PATCH 01/10] Add default impl --- crates/primitives/hashers/src/lib.rs | 18 ++++++++++++++++-- crates/primitives/hashers/src/pedersen.rs | 9 ++++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/crates/primitives/hashers/src/lib.rs b/crates/primitives/hashers/src/lib.rs index f82a98cb68..bd099c2054 100644 --- a/crates/primitives/hashers/src/lib.rs +++ b/crates/primitives/hashers/src/lib.rs @@ -19,7 +19,19 @@ pub trait HasherT { /// * `data` - The data to hash. /// # Returns /// The hash of the data. - fn compute_hash_on_wrappers(data: &[Felt252Wrapper]) -> Felt252Wrapper; + fn compute_hash_on_wrappers(data: I) -> Felt252Wrapper + where + I: IntoIterator, + { + // Default implementation + let hash = Self::compute_hash_on_elements( + data.into_iter() // Convert the data into an iterator + .map(|x| x.0) // Map each Felt252Wrapper to its inner FieldElement + ); + + // Wrap the computed hash in a Felt252Wrapper and return it + Felt252Wrapper(hash) + } /// Hashes the 2 felts sent. /// @@ -46,5 +58,7 @@ pub trait HasherT { /// # Returns /// /// The hash of the array. - fn compute_hash_on_elements(elements: &[FieldElement]) -> FieldElement; + fn compute_hash_on_elements(elements: I) -> FieldElement + where + I: IntoIterator; } diff --git a/crates/primitives/hashers/src/pedersen.rs b/crates/primitives/hashers/src/pedersen.rs index 2667cb9975..d6e5d5965c 100644 --- a/crates/primitives/hashers/src/pedersen.rs +++ b/crates/primitives/hashers/src/pedersen.rs @@ -52,11 +52,14 @@ impl HasherT for PedersenHasher { /// # Returns /// /// The hash of the data. - fn compute_hash_on_wrappers(data: &[Felt252Wrapper]) -> Felt252Wrapper { - let hash = compute_hash_on_elements(&data.iter().map(|x| x.0).collect::>()); + fn compute_hash_on_wrappers(data: I) -> Felt252Wrapper + where + I: IntoIterator, + { + let hash = compute_hash_on_elements(&data.into_iter().map(|x| x.0).collect::>()); Felt252Wrapper(hash) } - + #[inline(always)] fn hash_elements(a: FieldElement, b: FieldElement) -> FieldElement { pedersen_hash(&a, &b) From e976be234f34bc0ae52aea42e8bd4cd7fba0c9d0 Mon Sep 17 00:00:00 2001 From: fishseabowl Date: Fri, 31 May 2024 09:40:43 -0700 Subject: [PATCH 02/10] Make HasherT methods accept iterator --- crates/pallets/starknet/src/tests/utils.rs | 2 +- crates/primitives/block/src/header.rs | 4 +- crates/primitives/block/src/tests.rs | 6 +-- crates/primitives/hashers/src/lib.rs | 4 +- crates/primitives/hashers/src/pedersen.rs | 26 ++++-------- crates/primitives/hashers/src/poseidon.rs | 23 ++++------- .../transactions/src/compute_hash.rs | 40 +++++++++---------- 7 files changed, 42 insertions(+), 63 deletions(-) diff --git a/crates/pallets/starknet/src/tests/utils.rs b/crates/pallets/starknet/src/tests/utils.rs index e2c83b1f63..bd10ad6b8e 100644 --- a/crates/pallets/starknet/src/tests/utils.rs +++ b/crates/pallets/starknet/src/tests/utils.rs @@ -52,7 +52,7 @@ pub fn sign_message_hash_braavos( elements.extend_from_slice( &signer_model.iter().map(|e| Felt252Wrapper::from(*e).into()).collect::>(), ); - let braavos_hash = PedersenHasher::compute_hash_on_elements(&elements); + let braavos_hash = PedersenHasher::compute_hash_on_elements(elements.into_iter()); let mut signatures = sign_message_hash(Felt252Wrapper(braavos_hash).into()); signatures.0.push(actual_impl_hash); diff --git a/crates/primitives/block/src/header.rs b/crates/primitives/block/src/header.rs index fd7d76aa66..1f7b0b1aa7 100644 --- a/crates/primitives/block/src/header.rs +++ b/crates/primitives/block/src/header.rs @@ -62,7 +62,7 @@ impl Header { /// Compute the hash using the Pedersen hasher according to [the Starknet protocol specification](https://docs.starknet.io/documentation/architecture_and_concepts/Network_Architecture/header/#block_hash). pub fn hash(&self) -> Felt252Wrapper { - let data: &[Felt252Wrapper] = &[ + let data = [ self.block_number.into(), self.sequencer_address.0.0.into(), self.block_timestamp.into(), @@ -73,6 +73,6 @@ impl Header { self.parent_block_hash.into(), ]; - PedersenHasher::compute_hash_on_wrappers(data) + PedersenHasher::compute_hash_on_wrappers(data.into_iter()) } } diff --git a/crates/primitives/block/src/tests.rs b/crates/primitives/block/src/tests.rs index 9c430006cc..15662571d7 100644 --- a/crates/primitives/block/src/tests.rs +++ b/crates/primitives/block/src/tests.rs @@ -24,7 +24,7 @@ fn generate_dummy_header() -> Vec { #[test] fn test_header_hash() { - let hash = ::compute_hash_on_wrappers(&generate_dummy_header()); + let hash = ::compute_hash_on_wrappers(generate_dummy_header().into_iter()); let expected_hash = Felt252Wrapper::from_hex_be("0x001bef5f78bfd9122370a6bf9e3365b96362bef2bfd2b44b67707d8fbbf27bdc").unwrap(); @@ -52,7 +52,7 @@ fn test_real_header_hash() { StarkFelt::try_from("0x2043ba1ef46882ce1dbb17b501fffa4b71f87f618e8f394e9605959d92efdf6").unwrap(); let protocol_version = 0u32; - let header: &[Felt252Wrapper] = &[ + let header = [ block_number.into(), global_state_root.into(), sequencer_address.into(), @@ -68,7 +68,7 @@ fn test_real_header_hash() { let expected_hash = Felt252Wrapper::from_hex_be("0x001d126ca058c7e546d59cf4e10728e4b023ca0fb368e8abcabf0b5335f4487a").unwrap(); - let hash = ::compute_hash_on_wrappers(header); + let hash = ::compute_hash_on_wrappers(header.into_iter()); assert_eq!(hash, expected_hash); } diff --git a/crates/primitives/hashers/src/lib.rs b/crates/primitives/hashers/src/lib.rs index bd099c2054..9ed3547f02 100644 --- a/crates/primitives/hashers/src/lib.rs +++ b/crates/primitives/hashers/src/lib.rs @@ -14,7 +14,7 @@ pub trait HasherT { /// The hash of the data. fn hash_bytes(data: &[u8]) -> Felt252Wrapper; - // Hashes the given data. + /// Hashes the given data, including the default implementation /// # Arguments /// * `data` - The data to hash. /// # Returns @@ -53,7 +53,7 @@ pub trait HasherT { /// /// # Arguments /// - /// * `elements` - The array to hash. + /// * `elements` - A generic type that implements the Iterator trait. /// /// # Returns /// diff --git a/crates/primitives/hashers/src/pedersen.rs b/crates/primitives/hashers/src/pedersen.rs index d6e5d5965c..1a41cb63be 100644 --- a/crates/primitives/hashers/src/pedersen.rs +++ b/crates/primitives/hashers/src/pedersen.rs @@ -42,23 +42,6 @@ impl HasherT for PedersenHasher { Felt252Wrapper(hash_value) } - - /// Hashes a slice of field elements using the Pedersen hash function. - /// - /// # Arguments - /// - /// * `data` - The data to hash. - /// - /// # Returns - /// - /// The hash of the data. - fn compute_hash_on_wrappers(data: I) -> Felt252Wrapper - where - I: IntoIterator, - { - let hash = compute_hash_on_elements(&data.into_iter().map(|x| x.0).collect::>()); - Felt252Wrapper(hash) - } #[inline(always)] fn hash_elements(a: FieldElement, b: FieldElement) -> FieldElement { @@ -75,9 +58,14 @@ impl HasherT for PedersenHasher { /// /// h(h(h(h(0, data\[0\]), data\[1\]), ...), data\[n-1\]), n). #[inline] - fn compute_hash_on_elements(elements: &[FieldElement]) -> FieldElement { - compute_hash_on_elements(elements) + fn compute_hash_on_elements(elements: I) -> FieldElement + where + I: IntoIterator + { + let elements_vec: Vec = elements.into_iter().collect(); + compute_hash_on_elements(&elements_vec) } + } #[test] diff --git a/crates/primitives/hashers/src/poseidon.rs b/crates/primitives/hashers/src/poseidon.rs index bbf1287b17..8a7d1d5cc4 100644 --- a/crates/primitives/hashers/src/poseidon.rs +++ b/crates/primitives/hashers/src/poseidon.rs @@ -40,25 +40,16 @@ impl HasherT for PoseidonHasher { Felt252Wrapper(poseidon_hash_many(&field_element_vector)) } - /// Hashes a slice of field elements using the Poseidon hash function. - /// - /// # Arguments - /// - /// * `data` - The data to hash. - /// - /// # Returns - /// - /// The hash of the data. - fn compute_hash_on_wrappers(data: &[Felt252Wrapper]) -> Felt252Wrapper { - let data = data.iter().map(|x| x.0).collect::>(); - Felt252Wrapper(poseidon_hash_many(&data)) - } - fn hash_elements(a: FieldElement, b: FieldElement) -> FieldElement { poseidon_hash(a, b) } - fn compute_hash_on_elements(elements: &[FieldElement]) -> FieldElement { - poseidon_hash_many(elements) + + fn compute_hash_on_elements(elements: I) -> FieldElement + where + I: IntoIterator + { + let elements_vec: Vec = elements.into_iter().collect(); + poseidon_hash_many(&elements_vec) } } diff --git a/crates/primitives/transactions/src/compute_hash.rs b/crates/primitives/transactions/src/compute_hash.rs index aaa549e4ad..a1cdf291be 100644 --- a/crates/primitives/transactions/src/compute_hash.rs +++ b/crates/primitives/transactions/src/compute_hash.rs @@ -66,7 +66,7 @@ impl ComputeTransactionHash for InvokeTransactionV0 { let calldata_hash = compute_hash_on_elements(&convert_calldata(self.calldata.clone())); let max_fee = FieldElement::from(self.max_fee.0); - Felt252Wrapper(PedersenHasher::compute_hash_on_elements(&[ + Felt252Wrapper(PedersenHasher::compute_hash_on_elements([ prefix, version, contract_address, @@ -74,7 +74,7 @@ impl ComputeTransactionHash for InvokeTransactionV0 { calldata_hash, max_fee, chain_id.into(), - ])) + ].into_iter())) .into() } } @@ -89,7 +89,7 @@ impl ComputeTransactionHash for InvokeTransactionV1 { let max_fee = FieldElement::from(self.max_fee.0); let nonce = Felt252Wrapper::from(self.nonce.0).into(); - Felt252Wrapper(PedersenHasher::compute_hash_on_elements(&[ + Felt252Wrapper(PedersenHasher::compute_hash_on_elements([ prefix, version, sender_address, @@ -98,7 +98,7 @@ impl ComputeTransactionHash for InvokeTransactionV1 { max_fee, chain_id.into(), nonce, - ])) + ].into_iter())) .into() } } @@ -111,10 +111,10 @@ impl ComputeTransactionHash for InvokeTransactionV3 { let sender_address = Felt252Wrapper::from(self.sender_address).into(); let nonce = Felt252Wrapper::from(self.nonce.0).into(); let account_deployment_data_hash = PoseidonHasher::compute_hash_on_elements( - &self.account_deployment_data.0.iter().map(|f| Felt252Wrapper::from(*f).into()).collect::>(), + self.account_deployment_data.0.iter().map(|f| Felt252Wrapper::from(*f).into()), ); let calldata_hash = PoseidonHasher::compute_hash_on_elements( - &self.calldata.0.iter().map(|f| Felt252Wrapper::from(*f).into()).collect::>(), + self.calldata.0.iter().map(|f| Felt252Wrapper::from(*f).into()), ); compute_transaction_hash_common_v3( @@ -166,7 +166,7 @@ fn compute_hash_declare_v0_or_v1( FieldElement::from(version) }; - Felt252Wrapper(PedersenHasher::compute_hash_on_elements(&[ + Felt252Wrapper(PedersenHasher::compute_hash_on_elements([ prefix, version, sender_address, @@ -175,7 +175,7 @@ fn compute_hash_declare_v0_or_v1( max_fee, chain_id.into(), nonce_or_class_hash, - ])) + ].into_iter())) .into() } @@ -190,7 +190,7 @@ impl ComputeTransactionHash for DeclareTransactionV2 { let nonce = Felt252Wrapper::from(self.nonce).into(); let compiled_class_hash = Felt252Wrapper::from(self.compiled_class_hash).into(); - Felt252Wrapper(PedersenHasher::compute_hash_on_elements(&[ + Felt252Wrapper(PedersenHasher::compute_hash_on_elements([ prefix, version, sender_address, @@ -200,7 +200,7 @@ impl ComputeTransactionHash for DeclareTransactionV2 { chain_id.into(), nonce, compiled_class_hash, - ])) + ].into_iter())) .into() } } @@ -213,7 +213,7 @@ impl ComputeTransactionHash for DeclareTransactionV3 { let sender_address = Felt252Wrapper::from(self.sender_address).into(); let nonce = Felt252Wrapper::from(self.nonce.0).into(); let account_deployment_data_hash = PoseidonHasher::compute_hash_on_elements( - &self.account_deployment_data.0.iter().map(|f| Felt252Wrapper::from(*f).into()).collect::>(), + self.account_deployment_data.0.iter().map(|f| Felt252Wrapper::from(*f).into()), ); compute_transaction_hash_common_v3( @@ -280,7 +280,7 @@ impl ComputeTransactionHash for DeployAccountTransactionV1 { let max_fee = FieldElement::from(self.max_fee.0); let nonce = Felt252Wrapper::from(self.nonce).into(); - Felt252Wrapper(PedersenHasher::compute_hash_on_elements(&[ + Felt252Wrapper(PedersenHasher::compute_hash_on_elements([ prefix, version, contract_address, @@ -289,7 +289,7 @@ impl ComputeTransactionHash for DeployAccountTransactionV1 { max_fee, chain_id.into(), nonce, - ])) + ].into_iter())) .into() } } @@ -311,7 +311,7 @@ impl ComputeTransactionHash for DeployAccountTransactionV3 { ) .into(); let nonce = Felt252Wrapper::from(self.nonce.0).into(); - let constructor_calldata_hash = PoseidonHasher::compute_hash_on_elements(&constructor_calldata); + let constructor_calldata_hash = PoseidonHasher::compute_hash_on_elements(constructor_calldata.into_iter()); compute_transaction_hash_common_v3( prefix, @@ -342,7 +342,7 @@ impl ComputeTransactionHash for L1HandlerTransaction { let calldata_hash = compute_hash_on_elements(&convert_calldata(self.calldata.clone())); let nonce = Felt252Wrapper::from(self.nonce).into(); - Felt252Wrapper(PedersenHasher::compute_hash_on_elements(&[ + Felt252Wrapper(PedersenHasher::compute_hash_on_elements([ prefix, version, contract_address, @@ -350,7 +350,7 @@ impl ComputeTransactionHash for L1HandlerTransaction { calldata_hash, chain_id.into(), nonce, - ])) + ].into_iter())) .into() } } @@ -369,13 +369,13 @@ fn compute_transaction_hash_common_v3( resource_bounds: &ResourceBoundsMapping, additional_data: Vec, ) -> TransactionHash { - let gas_hash = PoseidonHasher::compute_hash_on_elements(&[ + let gas_hash = PoseidonHasher::compute_hash_on_elements([ FieldElement::from(tip.0), prepare_resource_bound_value(resource_bounds, Resource::L1Gas), prepare_resource_bound_value(resource_bounds, Resource::L2Gas), - ]); + ].into_iter()); let paymaster_hash = PoseidonHasher::compute_hash_on_elements( - &paymaster_data.0.iter().map(|f| Felt252Wrapper::from(*f).into()).collect::>(), + paymaster_data.0.iter().map(|f| Felt252Wrapper::from(*f).into()), ); let data_availability_modes = prepare_data_availability_modes(nonce_data_availability_mode, fee_data_availability_mode); @@ -390,7 +390,7 @@ fn compute_transaction_hash_common_v3( data_availability_modes, ]; data_to_hash.extend(additional_data); - Felt252Wrapper(PoseidonHasher::compute_hash_on_elements(data_to_hash.as_slice())).into() + Felt252Wrapper(PoseidonHasher::compute_hash_on_elements(data_to_hash.into_iter())).into() } #[cfg(test)] From 15aa29923adbd85e790b9d99e00bbfd62beac764 Mon Sep 17 00:00:00 2001 From: fishseabowl Date: Fri, 31 May 2024 09:54:16 -0700 Subject: [PATCH 03/10] add to Changelog.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 33eabbe4e7..dcc148217d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Next release +- dev: make HasherT methods accept iterator rather than &[Felt] as argument - feat: actual estimate_fee added, brought back l1 messages and refactored simulate tx - dev: impl get_state_updates using get_transaction_re_execution_state_diff From cdc64f72fdefcf50af89507c4f09cbbb9ed99979 Mon Sep 17 00:00:00 2001 From: fishseabowl Date: Fri, 31 May 2024 10:27:06 -0700 Subject: [PATCH 04/10] Fix fmt issues --- crates/primitives/hashers/src/lib.rs | 2 +- crates/primitives/hashers/src/pedersen.rs | 5 +- crates/primitives/hashers/src/poseidon.rs | 2 +- .../transactions/src/compute_hash.rs | 113 +++++++----------- 4 files changed, 48 insertions(+), 74 deletions(-) diff --git a/crates/primitives/hashers/src/lib.rs b/crates/primitives/hashers/src/lib.rs index 9ed3547f02..aa77474317 100644 --- a/crates/primitives/hashers/src/lib.rs +++ b/crates/primitives/hashers/src/lib.rs @@ -26,7 +26,7 @@ pub trait HasherT { // Default implementation let hash = Self::compute_hash_on_elements( data.into_iter() // Convert the data into an iterator - .map(|x| x.0) // Map each Felt252Wrapper to its inner FieldElement + .map(|x| x.0), // Map each Felt252Wrapper to its inner FieldElement ); // Wrap the computed hash in a Felt252Wrapper and return it diff --git a/crates/primitives/hashers/src/pedersen.rs b/crates/primitives/hashers/src/pedersen.rs index 1a41cb63be..6a0e7a4668 100644 --- a/crates/primitives/hashers/src/pedersen.rs +++ b/crates/primitives/hashers/src/pedersen.rs @@ -42,7 +42,7 @@ impl HasherT for PedersenHasher { Felt252Wrapper(hash_value) } - + #[inline(always)] fn hash_elements(a: FieldElement, b: FieldElement) -> FieldElement { pedersen_hash(&a, &b) @@ -60,12 +60,11 @@ impl HasherT for PedersenHasher { #[inline] fn compute_hash_on_elements(elements: I) -> FieldElement where - I: IntoIterator + I: IntoIterator, { let elements_vec: Vec = elements.into_iter().collect(); compute_hash_on_elements(&elements_vec) } - } #[test] diff --git a/crates/primitives/hashers/src/poseidon.rs b/crates/primitives/hashers/src/poseidon.rs index 8a7d1d5cc4..c52f0aa577 100644 --- a/crates/primitives/hashers/src/poseidon.rs +++ b/crates/primitives/hashers/src/poseidon.rs @@ -46,7 +46,7 @@ impl HasherT for PoseidonHasher { fn compute_hash_on_elements(elements: I) -> FieldElement where - I: IntoIterator + I: IntoIterator, { let elements_vec: Vec = elements.into_iter().collect(); poseidon_hash_many(&elements_vec) diff --git a/crates/primitives/transactions/src/compute_hash.rs b/crates/primitives/transactions/src/compute_hash.rs index a1cdf291be..7a03a280be 100644 --- a/crates/primitives/transactions/src/compute_hash.rs +++ b/crates/primitives/transactions/src/compute_hash.rs @@ -66,15 +66,10 @@ impl ComputeTransactionHash for InvokeTransactionV0 { let calldata_hash = compute_hash_on_elements(&convert_calldata(self.calldata.clone())); let max_fee = FieldElement::from(self.max_fee.0); - Felt252Wrapper(PedersenHasher::compute_hash_on_elements([ - prefix, - version, - contract_address, - entrypoint_selector, - calldata_hash, - max_fee, - chain_id.into(), - ].into_iter())) + Felt252Wrapper(PedersenHasher::compute_hash_on_elements( + [prefix, version, contract_address, entrypoint_selector, calldata_hash, max_fee, chain_id.into()] + .into_iter(), + )) .into() } } @@ -89,16 +84,10 @@ impl ComputeTransactionHash for InvokeTransactionV1 { let max_fee = FieldElement::from(self.max_fee.0); let nonce = Felt252Wrapper::from(self.nonce.0).into(); - Felt252Wrapper(PedersenHasher::compute_hash_on_elements([ - prefix, - version, - sender_address, - entrypoint_selector, - calldata_hash, - max_fee, - chain_id.into(), - nonce, - ].into_iter())) + Felt252Wrapper(PedersenHasher::compute_hash_on_elements( + [prefix, version, sender_address, entrypoint_selector, calldata_hash, max_fee, chain_id.into(), nonce] + .into_iter(), + )) .into() } } @@ -113,9 +102,8 @@ impl ComputeTransactionHash for InvokeTransactionV3 { let account_deployment_data_hash = PoseidonHasher::compute_hash_on_elements( self.account_deployment_data.0.iter().map(|f| Felt252Wrapper::from(*f).into()), ); - let calldata_hash = PoseidonHasher::compute_hash_on_elements( - self.calldata.0.iter().map(|f| Felt252Wrapper::from(*f).into()), - ); + let calldata_hash = + PoseidonHasher::compute_hash_on_elements(self.calldata.0.iter().map(|f| Felt252Wrapper::from(*f).into())); compute_transaction_hash_common_v3( prefix, @@ -166,16 +154,10 @@ fn compute_hash_declare_v0_or_v1( FieldElement::from(version) }; - Felt252Wrapper(PedersenHasher::compute_hash_on_elements([ - prefix, - version, - sender_address, - zero, - class_or_nothing_hash, - max_fee, - chain_id.into(), - nonce_or_class_hash, - ].into_iter())) + Felt252Wrapper(PedersenHasher::compute_hash_on_elements( + [prefix, version, sender_address, zero, class_or_nothing_hash, max_fee, chain_id.into(), nonce_or_class_hash] + .into_iter(), + )) .into() } @@ -190,17 +172,20 @@ impl ComputeTransactionHash for DeclareTransactionV2 { let nonce = Felt252Wrapper::from(self.nonce).into(); let compiled_class_hash = Felt252Wrapper::from(self.compiled_class_hash).into(); - Felt252Wrapper(PedersenHasher::compute_hash_on_elements([ - prefix, - version, - sender_address, - entrypoint_selector, - calldata, - max_fee, - chain_id.into(), - nonce, - compiled_class_hash, - ].into_iter())) + Felt252Wrapper(PedersenHasher::compute_hash_on_elements( + [ + prefix, + version, + sender_address, + entrypoint_selector, + calldata, + max_fee, + chain_id.into(), + nonce, + compiled_class_hash, + ] + .into_iter(), + )) .into() } } @@ -280,16 +265,10 @@ impl ComputeTransactionHash for DeployAccountTransactionV1 { let max_fee = FieldElement::from(self.max_fee.0); let nonce = Felt252Wrapper::from(self.nonce).into(); - Felt252Wrapper(PedersenHasher::compute_hash_on_elements([ - prefix, - version, - contract_address, - entrypoint_selector, - calldata_hash, - max_fee, - chain_id.into(), - nonce, - ].into_iter())) + Felt252Wrapper(PedersenHasher::compute_hash_on_elements( + [prefix, version, contract_address, entrypoint_selector, calldata_hash, max_fee, chain_id.into(), nonce] + .into_iter(), + )) .into() } } @@ -342,15 +321,9 @@ impl ComputeTransactionHash for L1HandlerTransaction { let calldata_hash = compute_hash_on_elements(&convert_calldata(self.calldata.clone())); let nonce = Felt252Wrapper::from(self.nonce).into(); - Felt252Wrapper(PedersenHasher::compute_hash_on_elements([ - prefix, - version, - contract_address, - entrypoint_selector, - calldata_hash, - chain_id.into(), - nonce, - ].into_iter())) + Felt252Wrapper(PedersenHasher::compute_hash_on_elements( + [prefix, version, contract_address, entrypoint_selector, calldata_hash, chain_id.into(), nonce].into_iter(), + )) .into() } } @@ -369,14 +342,16 @@ fn compute_transaction_hash_common_v3( resource_bounds: &ResourceBoundsMapping, additional_data: Vec, ) -> TransactionHash { - let gas_hash = PoseidonHasher::compute_hash_on_elements([ - FieldElement::from(tip.0), - prepare_resource_bound_value(resource_bounds, Resource::L1Gas), - prepare_resource_bound_value(resource_bounds, Resource::L2Gas), - ].into_iter()); - let paymaster_hash = PoseidonHasher::compute_hash_on_elements( - paymaster_data.0.iter().map(|f| Felt252Wrapper::from(*f).into()), + let gas_hash = PoseidonHasher::compute_hash_on_elements( + [ + FieldElement::from(tip.0), + prepare_resource_bound_value(resource_bounds, Resource::L1Gas), + prepare_resource_bound_value(resource_bounds, Resource::L2Gas), + ] + .into_iter(), ); + let paymaster_hash = + PoseidonHasher::compute_hash_on_elements(paymaster_data.0.iter().map(|f| Felt252Wrapper::from(*f).into())); let data_availability_modes = prepare_data_availability_modes(nonce_data_availability_mode, fee_data_availability_mode); let mut data_to_hash = vec![ From 90d2dd0935de1343429eb3fba2b8cb47bc2ebf5b Mon Sep 17 00:00:00 2001 From: fishseabowl Date: Fri, 31 May 2024 10:50:39 -0700 Subject: [PATCH 05/10] Fix clippy issues --- .../transactions/src/compute_hash.rs | 109 +++++++++++------- 1 file changed, 66 insertions(+), 43 deletions(-) diff --git a/crates/primitives/transactions/src/compute_hash.rs b/crates/primitives/transactions/src/compute_hash.rs index 7a03a280be..311bbdb508 100644 --- a/crates/primitives/transactions/src/compute_hash.rs +++ b/crates/primitives/transactions/src/compute_hash.rs @@ -66,10 +66,15 @@ impl ComputeTransactionHash for InvokeTransactionV0 { let calldata_hash = compute_hash_on_elements(&convert_calldata(self.calldata.clone())); let max_fee = FieldElement::from(self.max_fee.0); - Felt252Wrapper(PedersenHasher::compute_hash_on_elements( - [prefix, version, contract_address, entrypoint_selector, calldata_hash, max_fee, chain_id.into()] - .into_iter(), - )) + Felt252Wrapper(PedersenHasher::compute_hash_on_elements([ + prefix, + version, + contract_address, + entrypoint_selector, + calldata_hash, + max_fee, + chain_id.into(), + ])) .into() } } @@ -84,10 +89,16 @@ impl ComputeTransactionHash for InvokeTransactionV1 { let max_fee = FieldElement::from(self.max_fee.0); let nonce = Felt252Wrapper::from(self.nonce.0).into(); - Felt252Wrapper(PedersenHasher::compute_hash_on_elements( - [prefix, version, sender_address, entrypoint_selector, calldata_hash, max_fee, chain_id.into(), nonce] - .into_iter(), - )) + Felt252Wrapper(PedersenHasher::compute_hash_on_elements([ + prefix, + version, + sender_address, + entrypoint_selector, + calldata_hash, + max_fee, + chain_id.into(), + nonce, + ])) .into() } } @@ -154,10 +165,16 @@ fn compute_hash_declare_v0_or_v1( FieldElement::from(version) }; - Felt252Wrapper(PedersenHasher::compute_hash_on_elements( - [prefix, version, sender_address, zero, class_or_nothing_hash, max_fee, chain_id.into(), nonce_or_class_hash] - .into_iter(), - )) + Felt252Wrapper(PedersenHasher::compute_hash_on_elements([ + prefix, + version, + sender_address, + zero, + class_or_nothing_hash, + max_fee, + chain_id.into(), + nonce_or_class_hash, + ])) .into() } @@ -172,20 +189,17 @@ impl ComputeTransactionHash for DeclareTransactionV2 { let nonce = Felt252Wrapper::from(self.nonce).into(); let compiled_class_hash = Felt252Wrapper::from(self.compiled_class_hash).into(); - Felt252Wrapper(PedersenHasher::compute_hash_on_elements( - [ - prefix, - version, - sender_address, - entrypoint_selector, - calldata, - max_fee, - chain_id.into(), - nonce, - compiled_class_hash, - ] - .into_iter(), - )) + Felt252Wrapper(PedersenHasher::compute_hash_on_elements([ + prefix, + version, + sender_address, + entrypoint_selector, + calldata, + max_fee, + chain_id.into(), + nonce, + compiled_class_hash, + ])) .into() } } @@ -265,10 +279,16 @@ impl ComputeTransactionHash for DeployAccountTransactionV1 { let max_fee = FieldElement::from(self.max_fee.0); let nonce = Felt252Wrapper::from(self.nonce).into(); - Felt252Wrapper(PedersenHasher::compute_hash_on_elements( - [prefix, version, contract_address, entrypoint_selector, calldata_hash, max_fee, chain_id.into(), nonce] - .into_iter(), - )) + Felt252Wrapper(PedersenHasher::compute_hash_on_elements([ + prefix, + version, + contract_address, + entrypoint_selector, + calldata_hash, + max_fee, + chain_id.into(), + nonce, + ])) .into() } } @@ -290,7 +310,7 @@ impl ComputeTransactionHash for DeployAccountTransactionV3 { ) .into(); let nonce = Felt252Wrapper::from(self.nonce.0).into(); - let constructor_calldata_hash = PoseidonHasher::compute_hash_on_elements(constructor_calldata.into_iter()); + let constructor_calldata_hash = PoseidonHasher::compute_hash_on_elements(constructor_calldata); compute_transaction_hash_common_v3( prefix, @@ -321,9 +341,15 @@ impl ComputeTransactionHash for L1HandlerTransaction { let calldata_hash = compute_hash_on_elements(&convert_calldata(self.calldata.clone())); let nonce = Felt252Wrapper::from(self.nonce).into(); - Felt252Wrapper(PedersenHasher::compute_hash_on_elements( - [prefix, version, contract_address, entrypoint_selector, calldata_hash, chain_id.into(), nonce].into_iter(), - )) + Felt252Wrapper(PedersenHasher::compute_hash_on_elements([ + prefix, + version, + contract_address, + entrypoint_selector, + calldata_hash, + chain_id.into(), + nonce, + ])) .into() } } @@ -342,14 +368,11 @@ fn compute_transaction_hash_common_v3( resource_bounds: &ResourceBoundsMapping, additional_data: Vec, ) -> TransactionHash { - let gas_hash = PoseidonHasher::compute_hash_on_elements( - [ - FieldElement::from(tip.0), - prepare_resource_bound_value(resource_bounds, Resource::L1Gas), - prepare_resource_bound_value(resource_bounds, Resource::L2Gas), - ] - .into_iter(), - ); + let gas_hash = PoseidonHasher::compute_hash_on_elements([ + FieldElement::from(tip.0), + prepare_resource_bound_value(resource_bounds, Resource::L1Gas), + prepare_resource_bound_value(resource_bounds, Resource::L2Gas), + ]); let paymaster_hash = PoseidonHasher::compute_hash_on_elements(paymaster_data.0.iter().map(|f| Felt252Wrapper::from(*f).into())); let data_availability_modes = @@ -365,7 +388,7 @@ fn compute_transaction_hash_common_v3( data_availability_modes, ]; data_to_hash.extend(additional_data); - Felt252Wrapper(PoseidonHasher::compute_hash_on_elements(data_to_hash.into_iter())).into() + Felt252Wrapper(PoseidonHasher::compute_hash_on_elements(data_to_hash)).into() } #[cfg(test)] From b76309c8d6cb03e97cdf54e2fba8f9593b5b8220 Mon Sep 17 00:00:00 2001 From: fishseabowl Date: Fri, 31 May 2024 11:24:03 -0700 Subject: [PATCH 06/10] Fix cargo lint issues --- crates/primitives/block/src/header.rs | 2 +- crates/primitives/block/src/tests.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/primitives/block/src/header.rs b/crates/primitives/block/src/header.rs index 1f7b0b1aa7..e9d18d74fd 100644 --- a/crates/primitives/block/src/header.rs +++ b/crates/primitives/block/src/header.rs @@ -73,6 +73,6 @@ impl Header { self.parent_block_hash.into(), ]; - PedersenHasher::compute_hash_on_wrappers(data.into_iter()) + PedersenHasher::compute_hash_on_wrappers(data) } } diff --git a/crates/primitives/block/src/tests.rs b/crates/primitives/block/src/tests.rs index 15662571d7..770e6710f9 100644 --- a/crates/primitives/block/src/tests.rs +++ b/crates/primitives/block/src/tests.rs @@ -24,7 +24,7 @@ fn generate_dummy_header() -> Vec { #[test] fn test_header_hash() { - let hash = ::compute_hash_on_wrappers(generate_dummy_header().into_iter()); + let hash = ::compute_hash_on_wrappers(generate_dummy_header()); let expected_hash = Felt252Wrapper::from_hex_be("0x001bef5f78bfd9122370a6bf9e3365b96362bef2bfd2b44b67707d8fbbf27bdc").unwrap(); @@ -68,7 +68,7 @@ fn test_real_header_hash() { let expected_hash = Felt252Wrapper::from_hex_be("0x001d126ca058c7e546d59cf4e10728e4b023ca0fb368e8abcabf0b5335f4487a").unwrap(); - let hash = ::compute_hash_on_wrappers(header.into_iter()); + let hash = ::compute_hash_on_wrappers(header); assert_eq!(hash, expected_hash); } From 99259eb75ee711cd38e783159a2be166ab969d09 Mon Sep 17 00:00:00 2001 From: fishseabowl Date: Fri, 31 May 2024 12:42:20 -0700 Subject: [PATCH 07/10] Fix cargo lint issues --- crates/pallets/starknet/src/tests/utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/pallets/starknet/src/tests/utils.rs b/crates/pallets/starknet/src/tests/utils.rs index bd10ad6b8e..cb0250fe24 100644 --- a/crates/pallets/starknet/src/tests/utils.rs +++ b/crates/pallets/starknet/src/tests/utils.rs @@ -52,7 +52,7 @@ pub fn sign_message_hash_braavos( elements.extend_from_slice( &signer_model.iter().map(|e| Felt252Wrapper::from(*e).into()).collect::>(), ); - let braavos_hash = PedersenHasher::compute_hash_on_elements(elements.into_iter()); + let braavos_hash = PedersenHasher::compute_hash_on_elements(elements); let mut signatures = sign_message_hash(Felt252Wrapper(braavos_hash).into()); signatures.0.push(actual_impl_hash); From 317685d5902ac4c0c5fe66f7632b9ffc8dd70a28 Mon Sep 17 00:00:00 2001 From: fishseabowl Date: Tue, 4 Jun 2024 15:13:12 -0700 Subject: [PATCH 08/10] Remove related .collect() --- crates/pallets/starknet/src/tests/utils.rs | 2 +- crates/primitives/hashers/src/lib.rs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/pallets/starknet/src/tests/utils.rs b/crates/pallets/starknet/src/tests/utils.rs index cb0250fe24..c1e7356383 100644 --- a/crates/pallets/starknet/src/tests/utils.rs +++ b/crates/pallets/starknet/src/tests/utils.rs @@ -50,7 +50,7 @@ pub fn sign_message_hash_braavos( let mut elements: Vec = vec![Felt252Wrapper::from(tx_hash).into(), Felt252Wrapper::from(actual_impl_hash).into()]; elements.extend_from_slice( - &signer_model.iter().map(|e| Felt252Wrapper::from(*e).into()).collect::>(), + &signer_model.iter().map(|e| Felt252Wrapper::from(*e).into()), ); let braavos_hash = PedersenHasher::compute_hash_on_elements(elements); diff --git a/crates/primitives/hashers/src/lib.rs b/crates/primitives/hashers/src/lib.rs index aa77474317..ba7829c2ab 100644 --- a/crates/primitives/hashers/src/lib.rs +++ b/crates/primitives/hashers/src/lib.rs @@ -14,11 +14,12 @@ pub trait HasherT { /// The hash of the data. fn hash_bytes(data: &[u8]) -> Felt252Wrapper; - /// Hashes the given data, including the default implementation + /// Hashes the given data /// # Arguments /// * `data` - The data to hash. /// # Returns /// The hash of the data. + // Default implematation fn compute_hash_on_wrappers(data: I) -> Felt252Wrapper where I: IntoIterator, From 20cb3d1e5d041a66f171f492fc0488875167af42 Mon Sep 17 00:00:00 2001 From: fishseabowl Date: Wed, 5 Jun 2024 23:29:04 -0700 Subject: [PATCH 09/10] Modify extend_from_slice to extend --- crates/pallets/starknet/src/tests/utils.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/crates/pallets/starknet/src/tests/utils.rs b/crates/pallets/starknet/src/tests/utils.rs index c1e7356383..8e10abcb6a 100644 --- a/crates/pallets/starknet/src/tests/utils.rs +++ b/crates/pallets/starknet/src/tests/utils.rs @@ -49,9 +49,13 @@ pub fn sign_message_hash_braavos( // } let mut elements: Vec = vec![Felt252Wrapper::from(tx_hash).into(), Felt252Wrapper::from(actual_impl_hash).into()]; - elements.extend_from_slice( - &signer_model.iter().map(|e| Felt252Wrapper::from(*e).into()), - ); + + elements.extend(signer_model.iter().map(|e| { + let wrapper: Felt252Wrapper = Felt252Wrapper::from(*e); + let field_element: FieldElement = wrapper.into(); + field_element + })); + let braavos_hash = PedersenHasher::compute_hash_on_elements(elements); let mut signatures = sign_message_hash(Felt252Wrapper(braavos_hash).into()); From d5bd4d79be0bbbd5387a658bca4cd130e9184233 Mon Sep 17 00:00:00 2001 From: fishseabowl Date: Wed, 5 Jun 2024 23:30:33 -0700 Subject: [PATCH 10/10] Fix fmt issues --- crates/pallets/starknet/src/tests/utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/pallets/starknet/src/tests/utils.rs b/crates/pallets/starknet/src/tests/utils.rs index 8e10abcb6a..113c890528 100644 --- a/crates/pallets/starknet/src/tests/utils.rs +++ b/crates/pallets/starknet/src/tests/utils.rs @@ -55,7 +55,7 @@ pub fn sign_message_hash_braavos( let field_element: FieldElement = wrapper.into(); field_element })); - + let braavos_hash = PedersenHasher::compute_hash_on_elements(elements); let mut signatures = sign_message_hash(Felt252Wrapper(braavos_hash).into());