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

Commit

Permalink
Make HasherT methods accept iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
fishseabowl committed May 31, 2024
1 parent 51694b7 commit e976be2
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 63 deletions.
2 changes: 1 addition & 1 deletion crates/pallets/starknet/src/tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub fn sign_message_hash_braavos(
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);
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);
Expand Down
4 changes: 2 additions & 2 deletions 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 All @@ -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())
}
}
6 changes: 3 additions & 3 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().into_iter());

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 All @@ -68,7 +68,7 @@ fn test_real_header_hash() {

let expected_hash =
Felt252Wrapper::from_hex_be("0x001d126ca058c7e546d59cf4e10728e4b023ca0fb368e8abcabf0b5335f4487a").unwrap();
let hash = <PedersenHasher as HasherT>::compute_hash_on_wrappers(header);
let hash = <PedersenHasher as HasherT>::compute_hash_on_wrappers(header.into_iter());

assert_eq!(hash, expected_hash);
}
4 changes: 2 additions & 2 deletions crates/primitives/hashers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -53,7 +53,7 @@ pub trait HasherT {
///
/// # Arguments
///
/// * `elements` - The array to hash.
/// * `elements` - A generic type that implements the Iterator trait.
///
/// # Returns
///
Expand Down
26 changes: 7 additions & 19 deletions crates/primitives/hashers/src/pedersen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<I>(data: I) -> Felt252Wrapper
where
I: IntoIterator<Item = Felt252Wrapper>,
{
let hash = compute_hash_on_elements(&data.into_iter().map(|x| x.0).collect::<Vec<FieldElement>>());
Felt252Wrapper(hash)
}

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

}

#[test]
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
40 changes: 20 additions & 20 deletions crates/primitives/transactions/src/compute_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +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(&[
Felt252Wrapper(PedersenHasher::compute_hash_on_elements([
prefix,
version,
contract_address,
entrypoint_selector,
calldata_hash,
max_fee,
chain_id.into(),
]))
].into_iter()))
.into()
}
}
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 @@ -98,7 +98,7 @@ impl ComputeTransactionHash for InvokeTransactionV1 {
max_fee,
chain_id.into(),
nonce,
]))
].into_iter()))
.into()
}
}
Expand All @@ -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::<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()).collect::<Vec<_>>(),
self.calldata.0.iter().map(|f| Felt252Wrapper::from(*f).into()),
);

compute_transaction_hash_common_v3(
Expand Down Expand Up @@ -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,
Expand All @@ -175,7 +175,7 @@ fn compute_hash_declare_v0_or_v1(
max_fee,
chain_id.into(),
nonce_or_class_hash,
]))
].into_iter()))
.into()
}

Expand All @@ -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,
Expand All @@ -200,7 +200,7 @@ impl ComputeTransactionHash for DeclareTransactionV2 {
chain_id.into(),
nonce,
compiled_class_hash,
]))
].into_iter()))
.into()
}
}
Expand All @@ -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::<Vec<_>>(),
self.account_deployment_data.0.iter().map(|f| Felt252Wrapper::from(*f).into()),
);

compute_transaction_hash_common_v3(
Expand Down Expand Up @@ -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,
Expand All @@ -289,7 +289,7 @@ impl ComputeTransactionHash for DeployAccountTransactionV1 {
max_fee,
chain_id.into(),
nonce,
]))
].into_iter()))
.into()
}
}
Expand All @@ -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,
Expand Down Expand Up @@ -342,15 +342,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(&[
Felt252Wrapper(PedersenHasher::compute_hash_on_elements([
prefix,
version,
contract_address,
entrypoint_selector,
calldata_hash,
chain_id.into(),
nonce,
]))
].into_iter()))
.into()
}
}
Expand All @@ -369,13 +369,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),
]);
].into_iter());
let paymaster_hash = PoseidonHasher::compute_hash_on_elements(
&paymaster_data.0.iter().map(|f| Felt252Wrapper::from(*f).into()).collect::<Vec<_>>(),
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);
Expand All @@ -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)]
Expand Down

0 comments on commit e976be2

Please sign in to comment.