diff --git a/CHANGELOG.md b/CHANGELOG.md index 194890f5d4..bad8c8ade0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Next release +- dev: make HasherT methods accept iterator rather than &[Felt] as argument - test: Adding txv3 tests - feat: L1 gas price/fix diff --git a/crates/pallets/starknet/src/tests/utils.rs b/crates/pallets/starknet/src/tests/utils.rs index 82f82bd747..afc81fe0d2 100644 --- a/crates/pallets/starknet/src/tests/utils.rs +++ b/crates/pallets/starknet/src/tests/utils.rs @@ -49,10 +49,14 @@ 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::>(), - ); - let braavos_hash = PedersenHasher::compute_hash_on_elements(&elements); + + 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()); signatures.0.push(actual_impl_hash); diff --git a/crates/primitives/block/src/header.rs b/crates/primitives/block/src/header.rs index fd7d76aa66..e9d18d74fd 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(), diff --git a/crates/primitives/block/src/tests.rs b/crates/primitives/block/src/tests.rs index 9c430006cc..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()); + let hash = ::compute_hash_on_wrappers(generate_dummy_header()); 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(), diff --git a/crates/primitives/hashers/src/lib.rs b/crates/primitives/hashers/src/lib.rs index f82a98cb68..ba7829c2ab 100644 --- a/crates/primitives/hashers/src/lib.rs +++ b/crates/primitives/hashers/src/lib.rs @@ -14,12 +14,25 @@ pub trait HasherT { /// The hash of the data. fn hash_bytes(data: &[u8]) -> Felt252Wrapper; - // Hashes the given data. + /// Hashes the given data /// # Arguments /// * `data` - The data to hash. /// # Returns /// The hash of the data. - fn compute_hash_on_wrappers(data: &[Felt252Wrapper]) -> Felt252Wrapper; + // Default implematation + 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. /// @@ -41,10 +54,12 @@ pub trait HasherT { /// /// # Arguments /// - /// * `elements` - The array to hash. + /// * `elements` - A generic type that implements the Iterator trait. /// /// # 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..6a0e7a4668 100644 --- a/crates/primitives/hashers/src/pedersen.rs +++ b/crates/primitives/hashers/src/pedersen.rs @@ -43,20 +43,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: &[Felt252Wrapper]) -> Felt252Wrapper { - let hash = compute_hash_on_elements(&data.iter().map(|x| x.0).collect::>()); - Felt252Wrapper(hash) - } - #[inline(always)] fn hash_elements(a: FieldElement, b: FieldElement) -> FieldElement { pedersen_hash(&a, &b) @@ -72,8 +58,12 @@ 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) } } diff --git a/crates/primitives/hashers/src/poseidon.rs b/crates/primitives/hashers/src/poseidon.rs index bbf1287b17..c52f0aa577 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..311bbdb508 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, @@ -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, @@ -111,11 +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::>(), - ); - let calldata_hash = PoseidonHasher::compute_hash_on_elements( - &self.calldata.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())); compute_transaction_hash_common_v3( prefix, @@ -166,7 +165,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, @@ -190,7 +189,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, @@ -213,7 +212,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 +279,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, @@ -311,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); + let constructor_calldata_hash = PoseidonHasher::compute_hash_on_elements(constructor_calldata); compute_transaction_hash_common_v3( prefix, @@ -342,7 +341,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, @@ -369,14 +368,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), ]); - let paymaster_hash = PoseidonHasher::compute_hash_on_elements( - &paymaster_data.0.iter().map(|f| Felt252Wrapper::from(*f).into()).collect::>(), - ); + 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![ @@ -390,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.as_slice())).into() + Felt252Wrapper(PoseidonHasher::compute_hash_on_elements(data_to_hash)).into() } #[cfg(test)]