Skip to content
This repository has been archived by the owner on Aug 2, 2024. It is now read-only.

dev: Make HasherT methods accept iterator #1621

Closed
wants to merge 14 commits into from
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 8 additions & 4 deletions crates/pallets/starknet/src/tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,14 @@ pub fn sign_message_hash_braavos(
// }
let mut elements: Vec<FieldElement> =
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::<Vec<FieldElement>>(),
);
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);
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives/block/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions crates/primitives/block/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn generate_dummy_header() -> Vec<Felt252Wrapper> {

#[test]
fn test_header_hash() {
let hash = <PedersenHasher as HasherT>::compute_hash_on_wrappers(&generate_dummy_header());
let hash = <PedersenHasher as HasherT>::compute_hash_on_wrappers(generate_dummy_header());

let expected_hash =
Felt252Wrapper::from_hex_be("0x001bef5f78bfd9122370a6bf9e3365b96362bef2bfd2b44b67707d8fbbf27bdc").unwrap();
Expand Down Expand Up @@ -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(),
Expand Down
23 changes: 19 additions & 4 deletions crates/primitives/hashers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<I>(data: I) -> Felt252Wrapper
where
I: IntoIterator<Item = Felt252Wrapper>,
{
// 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.
///
Expand All @@ -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<I>(elements: I) -> FieldElement
where
I: IntoIterator<Item = FieldElement>;
}
22 changes: 6 additions & 16 deletions crates/primitives/hashers/src/pedersen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<FieldElement>>());
Felt252Wrapper(hash)
}

#[inline(always)]
fn hash_elements(a: FieldElement, b: FieldElement) -> FieldElement {
pedersen_hash(&a, &b)
Expand All @@ -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<I>(elements: I) -> FieldElement
where
I: IntoIterator<Item = FieldElement>,
{
let elements_vec: Vec<FieldElement> = elements.into_iter().collect();
compute_hash_on_elements(&elements_vec)
}
}

Expand Down
23 changes: 7 additions & 16 deletions crates/primitives/hashers/src/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>();
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<I>(elements: I) -> FieldElement
where
I: IntoIterator<Item = FieldElement>,
{
let elements_vec: Vec<FieldElement> = elements.into_iter().collect();
poseidon_hash_many(&elements_vec)
}
}

Expand Down
32 changes: 15 additions & 17 deletions crates/primitives/transactions/src/compute_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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::<Vec<_>>(),
);
let calldata_hash = PoseidonHasher::compute_hash_on_elements(
&self.calldata.0.iter().map(|f| Felt252Wrapper::from(*f).into()).collect::<Vec<_>>(),
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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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::<Vec<_>>(),
self.account_deployment_data.0.iter().map(|f| Felt252Wrapper::from(*f).into()),
);

compute_transaction_hash_common_v3(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -369,14 +368,13 @@ fn compute_transaction_hash_common_v3(
resource_bounds: &ResourceBoundsMapping,
additional_data: Vec<FieldElement>,
) -> 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::<Vec<_>>(),
);
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![
Expand All @@ -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)]
Expand Down
Loading