-
Notifications
You must be signed in to change notification settings - Fork 90
chore: hasher and light hasher macro support for sha256 #1892
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,7 +37,14 @@ pub(crate) fn generate_data_hasher_impl( | |
| slices[num_flattned_fields] = element.as_slice(); | ||
| } | ||
|
|
||
| H::hashv(slices.as_slice()) | ||
| let mut result = H::hashv(slices.as_slice())?; | ||
|
|
||
| // Apply field size truncation for non-Poseidon hashers | ||
| if H::ID != ::light_hasher::Poseidon::ID { | ||
| result[0] = 0; | ||
| } | ||
|
|
||
| Ok(result) | ||
| } | ||
|
Comment on lines
+40
to
48
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chainPotentially incorrect slices population before hashing. hashv(slices.as_slice()) assumes slices are fully populated. In the surrounding code, slices are filled via a loop using a constant index (num_flattned_fields) that never increments, so only index 0 is set repeatedly and others remain empty. This will corrupt inputs for flattened structs. Suggested fix outside this hunk: populate slices using enumerate and remove the unused counter. for (i, element) in field_array.iter().enumerate() {
slices[i] = element.as_slice();
}Also consider renaming num_flattned_fields -> num_flattened_fields (and removing it if unused). Search for the current population pattern to patch all occurrences: 🏁 Script executed: #!/bin/bash
rg -n 'for element in field_array\.iter\(\) \{[^}]*slices\[[^\]]+\]\s*=\s*element\.as_slice\(\);[^}]*\}' sdk-libs/macros/src/hasherLength of output: 0 🏁 Script executed: #!/bin/bash
sed -n '1,200p' sdk-libs/macros/src/hasher/data_hasher.rsLength of output: 4363 Use let mut num_flattned_fields = 0;
for element in field_array.iter() {
slices[num_flattned_fields] = element.as_slice();
}never increments the counter, so only for (i, element) in field_array.iter().enumerate() {
slices[i] = element.as_slice();
}and rename 🤖 Prompt for AI Agents |
||
| } | ||
| } | ||
|
|
@@ -59,13 +66,53 @@ pub(crate) fn generate_data_hasher_impl( | |
| println!("DataHasher::hash inputs {:?}", debug_prints); | ||
| } | ||
| } | ||
| H::hashv(&[ | ||
| let mut result = H::hashv(&[ | ||
| #(#data_hasher_assignments.as_slice(),)* | ||
| ]) | ||
| ])?; | ||
|
|
||
| // Apply field size truncation for non-Poseidon hashers | ||
| if H::ID != ::light_hasher::Poseidon::ID { | ||
| result[0] = 0; | ||
| } | ||
|
|
||
| Ok(result) | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| Ok(hasher_impl) | ||
| } | ||
|
|
||
| /// SHA256-specific DataHasher implementation that serializes the whole struct | ||
| pub(crate) fn generate_data_hasher_impl_sha( | ||
| struct_name: &syn::Ident, | ||
| generics: &syn::Generics, | ||
| ) -> Result<TokenStream> { | ||
| let (impl_gen, type_gen, where_clause) = generics.split_for_impl(); | ||
|
|
||
| let hasher_impl = quote! { | ||
| impl #impl_gen ::light_hasher::DataHasher for #struct_name #type_gen #where_clause { | ||
| fn hash<H>(&self) -> ::std::result::Result<[u8; 32], ::light_hasher::HasherError> | ||
| where | ||
| H: ::light_hasher::Hasher | ||
| { | ||
| use ::light_hasher::Hasher; | ||
| use borsh::BorshSerialize; | ||
|
|
||
| // Compile-time assertion that H must be SHA256 (ID = 1) | ||
| use ::light_hasher::sha256::RequireSha256; | ||
| let _ = <H as RequireSha256>::ASSERT; | ||
|
|
||
| // For SHA256, we serialize the whole struct and hash it in one go | ||
| let serialized = self.try_to_vec().map_err(|_| ::light_hasher::HasherError::BorshError)?; | ||
| let mut result = H::hash(&serialized)?; | ||
| // Truncate sha256 to 31 be bytes less than 254 bits bn254 field size. | ||
| result[0] = 0; | ||
| Ok(result) | ||
| } | ||
ananas-block marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| }; | ||
|
|
||
| Ok(hasher_impl) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major
❓ Verification inconclusive
Compile-time gating via RequireSha256 is brittle; blanket impl negates the constraint.
With
impl<T: Hasher> RequireSha256 for T {}, anyHashersatisfies the bound unless the associated const is actually referenced. Theassert!in an associated const may also depend on MSRV/const‑panic semantics and won’t fire unless evaluated. Prefer a simple marker trait implemented only forSha256, or a const‑generic assertion pattern.Minimal, robust fix:
If you need a generic proof, use a const‑generic helper type instead of
assert!in a const.Replace brittle RequireSha256 implementation with a marker trait
In program-libs/hasher/src/sha256.rs remove the
const ASSERTand blanket impl, and instead use:🤖 Prompt for AI Agents