Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ light-merkle-tree-metadata = { path = "program-libs/merkle-tree-metadata", versi
aligned-sized = { path = "program-libs/aligned-sized", version = "1.1.0" }
light-bloom-filter = { path = "program-libs/bloom-filter", version = "0.6.0" }
light-bounded-vec = { version = "2.0.1" }
light-poseidon = { version = "0.3.0" }
light-poseidon = { version = "0.4.0" }
light-test-utils = { path = "program-tests/utils", version = "1.2.1" }
light-indexed-array = { path = "program-libs/indexed-array", version = "0.3.0" }
light-array-map = { path = "program-libs/array-map", version = "0.2.0" }
Expand Down
52 changes: 29 additions & 23 deletions program-libs/compressed-account/src/compressed_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,18 +411,27 @@ mod tests {
};
let merkle_tree_pubkey = Pubkey::new_unique();
let leaf_index: u32 = 1;

// Precompute padded 32-byte arrays matching the hash implementation
let mut leaf_index_bytes = [0u8; 32];
leaf_index_bytes[28..].copy_from_slice(&leaf_index.to_le_bytes());
let mut lamports_bytes = [0u8; 32];
lamports_bytes[24..].copy_from_slice(&lamports.to_le_bytes());
lamports_bytes[23] = 1;
let mut discriminator_bytes = [0u8; 32];
discriminator_bytes[24..].copy_from_slice(&data.discriminator);
discriminator_bytes[23] = 2;

let hash = compressed_account
.hash(&merkle_tree_pubkey, &leaf_index, false)
.unwrap();
let hash_manual = Poseidon::hashv(&[
hash_to_bn254_field_size_be(&owner.to_bytes()).as_slice(),
leaf_index.to_le_bytes().as_slice(),
&leaf_index_bytes,
hash_to_bn254_field_size_be(&merkle_tree_pubkey.to_bytes()).as_slice(),
[&[1u8], lamports.to_le_bytes().as_slice()]
.concat()
.as_slice(),
&lamports_bytes,
address.as_slice(),
[&[2u8], data.discriminator.as_slice()].concat().as_slice(),
&discriminator_bytes,
&data.data_hash,
])
.unwrap();
Expand All @@ -442,11 +451,9 @@ mod tests {

let hash_manual = Poseidon::hashv(&[
hash_to_bn254_field_size_be(&owner.to_bytes()).as_slice(),
leaf_index.to_le_bytes().as_slice(),
&leaf_index_bytes,
hash_to_bn254_field_size_be(&merkle_tree_pubkey.to_bytes()).as_slice(),
[&[1u8], lamports.to_le_bytes().as_slice()]
.concat()
.as_slice(),
&lamports_bytes,
address.as_slice(),
])
.unwrap();
Expand All @@ -465,12 +472,10 @@ mod tests {
.unwrap();
let hash_manual = Poseidon::hashv(&[
hash_to_bn254_field_size_be(&owner.to_bytes()).as_slice(),
leaf_index.to_le_bytes().as_slice(),
&leaf_index_bytes,
hash_to_bn254_field_size_be(&merkle_tree_pubkey.to_bytes()).as_slice(),
[&[1u8], lamports.to_le_bytes().as_slice()]
.concat()
.as_slice(),
[&[2u8], data.discriminator.as_slice()].concat().as_slice(),
&lamports_bytes,
&discriminator_bytes,
&data.data_hash,
])
.unwrap();
Expand All @@ -490,9 +495,9 @@ mod tests {
.unwrap();
let hash_manual = Poseidon::hashv(&[
hash_to_bn254_field_size_be(&owner.to_bytes()).as_slice(),
leaf_index.to_le_bytes().as_slice(),
&leaf_index_bytes,
hash_to_bn254_field_size_be(&merkle_tree_pubkey.to_bytes()).as_slice(),
[&[2u8], data.discriminator.as_slice()].concat().as_slice(),
&discriminator_bytes,
&data.data_hash,
])
.unwrap();
Expand All @@ -513,11 +518,9 @@ mod tests {
.unwrap();
let hash_manual = Poseidon::hashv(&[
hash_to_bn254_field_size_be(&owner.to_bytes()).as_slice(),
leaf_index.to_le_bytes().as_slice(),
&leaf_index_bytes,
hash_to_bn254_field_size_be(&merkle_tree_pubkey.to_bytes()).as_slice(),
[&[1u8], lamports.to_le_bytes().as_slice()]
.concat()
.as_slice(),
&lamports_bytes,
])
.unwrap();
assert_eq!(no_address_no_data_hash, hash_manual);
Expand All @@ -538,7 +541,7 @@ mod tests {
.unwrap();
let hash_manual = Poseidon::hashv(&[
hash_to_bn254_field_size_be(&owner.to_bytes()).as_slice(),
leaf_index.to_le_bytes().as_slice(),
&leaf_index_bytes,
hash_to_bn254_field_size_be(&merkle_tree_pubkey.to_bytes()).as_slice(),
])
.unwrap();
Expand Down Expand Up @@ -752,8 +755,11 @@ mod tests {
Some(CompressedAccountData {
discriminator: rng.gen(),
data: Vec::new(), // not used in hash
data_hash: Poseidon::hash(rng.gen::<u64>().to_be_bytes().as_slice())
.unwrap(),
data_hash: {
let mut random_bytes = [0u8; 32];
random_bytes[24..].copy_from_slice(&rng.gen::<u64>().to_be_bytes());
Poseidon::hash(&random_bytes).unwrap()
},
})
} else {
None
Expand Down
10 changes: 5 additions & 5 deletions program-libs/hasher/src/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ impl Hasher for Poseidon {
use crate::HASH_BYTES;
// TODO: reenable once LightHasher refactor is merged
// solana_program::msg!("remove len check onchain.");
Comment thread
ananas-block marked this conversation as resolved.
// for val in vals {
// if val.len() != 32 {
// return Err(HasherError::InvalidInputLength(val.len()));
// }
// }
for val in _vals {
if val.len() != 32 {
return Err(HasherError::InvalidInputLength(val.len(), 32));
}
}
let mut hash_result = [0; HASH_BYTES];
let result = unsafe {
crate::syscalls::sol_poseidon(
Expand Down
16 changes: 12 additions & 4 deletions program-libs/indexed-merkle-tree/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,11 +557,13 @@ mod test {
indexed_array.find_element(&nullifier1),
Some(&bundle1.new_element),
);
let mut next_index_bytes = [0u8; 32];
next_index_bytes[24..].copy_from_slice(&0_usize.to_be_bytes());
let expected_hash = Poseidon::hashv(&[
bigint_to_be_bytes_array::<32>(&nullifier1)
.unwrap()
.as_ref(),
0_usize.to_be_bytes().as_ref(),
&next_index_bytes,
bigint_to_be_bytes_array::<32>(&(0.to_biguint().unwrap()))
Comment thread
ananas-block marked this conversation as resolved.
.unwrap()
.as_ref(),
Expand Down Expand Up @@ -627,11 +629,13 @@ mod test {
indexed_array.find_element(&nullifier2),
Some(&bundle2.new_element),
);
let mut next_index_bytes = [0u8; 32];
next_index_bytes[24..].copy_from_slice(&1_usize.to_be_bytes());
let expected_hash = Poseidon::hashv(&[
bigint_to_be_bytes_array::<32>(&nullifier2)
.unwrap()
.as_ref(),
1_usize.to_be_bytes().as_ref(),
&next_index_bytes,
bigint_to_be_bytes_array::<32>(&(30.to_biguint().unwrap()))
.unwrap()
.as_ref(),
Expand Down Expand Up @@ -707,11 +711,13 @@ mod test {
indexed_array.find_element(&nullifier3),
Some(&bundle3.new_element),
);
let mut next_index_bytes = [0u8; 32];
next_index_bytes[24..].copy_from_slice(&1_usize.to_be_bytes());
let expected_hash = Poseidon::hashv(&[
bigint_to_be_bytes_array::<32>(&nullifier3)
.unwrap()
.as_ref(),
1_usize.to_be_bytes().as_ref(),
&next_index_bytes,
bigint_to_be_bytes_array::<32>(&(30.to_biguint().unwrap()))
.unwrap()
.as_ref(),
Expand Down Expand Up @@ -802,11 +808,13 @@ mod test {
indexed_array.find_element(&nullifier4),
Some(&bundle4.new_element),
);
let mut next_index_bytes = [0u8; 32];
next_index_bytes[24..].copy_from_slice(&0_usize.to_be_bytes());
let expected_hash = Poseidon::hashv(&[
bigint_to_be_bytes_array::<32>(&nullifier4)
.unwrap()
.as_ref(),
0_usize.to_be_bytes().as_ref(),
&next_index_bytes,
bigint_to_be_bytes_array::<32>(&(0.to_biguint().unwrap()))
.unwrap()
.as_ref(),
Expand Down
24 changes: 18 additions & 6 deletions program-libs/indexed-merkle-tree/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,18 +639,30 @@ pub fn functional_non_inclusion_test() {
assert_eq!(
leaf_0,
Poseidon::hashv(&[
&0_u32.to_biguint().unwrap().to_bytes_be(),
&1_u32.to_biguint().unwrap().to_bytes_be(),
&30_u32.to_biguint().unwrap().to_bytes_be()
bigint_to_be_bytes_array::<32>(&0_u32.to_biguint().unwrap())
.unwrap()
.as_ref(),
bigint_to_be_bytes_array::<32>(&1_u32.to_biguint().unwrap())
.unwrap()
.as_ref(),
bigint_to_be_bytes_array::<32>(&30_u32.to_biguint().unwrap())
.unwrap()
.as_ref(),
])
.unwrap()
);
assert_eq!(
leaf_1,
Poseidon::hashv(&[
&30_u32.to_biguint().unwrap().to_bytes_be(),
&0_u32.to_biguint().unwrap().to_bytes_be(),
&0_u32.to_biguint().unwrap().to_bytes_be()
bigint_to_be_bytes_array::<32>(&30_u32.to_biguint().unwrap())
.unwrap()
.as_ref(),
bigint_to_be_bytes_array::<32>(&0_u32.to_biguint().unwrap())
.unwrap()
.as_ref(),
bigint_to_be_bytes_array::<32>(&0_u32.to_biguint().unwrap())
.unwrap()
.as_ref(),
])
.unwrap()
);
Expand Down
5 changes: 3 additions & 2 deletions program-tests/batched-merkle-tree-test/tests/merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ pub fn assert_nullifier_queue_insert(
) -> Result<(), BatchedMerkleTreeError> {
let mut leaf_hash_chain_insert_values = vec![];
for (insert_value, leaf_index) in bloom_filter_insert_values.iter().zip(leaf_indices.iter()) {
let mut leaf_index_bytes = [0u8; 32];
leaf_index_bytes[24..].copy_from_slice(&leaf_index.to_be_bytes());
let nullifier =
Poseidon::hashv(&[insert_value.as_slice(), &leaf_index.to_be_bytes(), &tx_hash])
.unwrap();
Poseidon::hashv(&[insert_value.as_slice(), &leaf_index_bytes, &tx_hash]).unwrap();
leaf_hash_chain_insert_values.push(nullifier);
}
assert_input_queue_insert(
Expand Down
4 changes: 3 additions & 1 deletion program-tests/create-address-test-program/src/create_pda.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ impl light_hasher::DataHasher for RegisteredUser {
fn hash<H: light_hasher::Hasher>(&self) -> std::result::Result<[u8; 32], HasherError> {
let truncated_user_pubkey = hash_to_bn254_field_size_be(&self.user_pubkey.to_bytes());

H::hashv(&[truncated_user_pubkey.as_slice(), self.data.as_slice()])
let mut data_bytes = [0u8; 32];
data_bytes[1..].copy_from_slice(&self.data);
H::hashv(&[truncated_user_pubkey.as_slice(), &data_bytes])
}
}

Expand Down
4 changes: 3 additions & 1 deletion program-tests/system-cpi-test/src/create_pda.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,9 @@ pub struct RegisteredUser {
impl light_hasher::DataHasher for RegisteredUser {
fn hash<H: light_hasher::Hasher>(&self) -> std::result::Result<[u8; 32], HasherError> {
let truncated_user_pubkey = hash_to_bn254_field_size_be(&self.user_pubkey.to_bytes());
H::hashv(&[truncated_user_pubkey.as_slice(), self.data.as_slice()])
let mut data_bytes = [0u8; 32];
data_bytes[1..].copy_from_slice(&self.data);
H::hashv(&[truncated_user_pubkey.as_slice(), &data_bytes])
}
}

Expand Down
5 changes: 3 additions & 2 deletions program-tests/system-cpi-test/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1906,10 +1906,11 @@ pub async fn assert_created_pda<R: Rpc, I: Indexer + TestIndexerExtensions>(
);
let truncated_user_pubkey =
hash_to_bn254_field_size_be(&compressed_escrow_pda_data.user_pubkey.to_bytes());

let mut data_bytes = [0u8; 32];
data_bytes[1..].copy_from_slice(data);
assert_eq!(
compressed_escrow_pda_deserialized.data_hash,
Poseidon::hashv(&[truncated_user_pubkey.as_slice(), data.as_slice()]).unwrap(),
Poseidon::hashv(&[truncated_user_pubkey.as_slice(), &data_bytes]).unwrap(),
);
}

Expand Down
3 changes: 2 additions & 1 deletion program-tests/utils/src/mock_batched_forester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ impl<const HEIGHT: usize> MockBatchedForester<HEIGHT> {
.iter()
.find(|tx_event| tx_event.inputs.contains(leaf))
.expect("No event for leaf found.");
let index_bytes = index.to_be_bytes();
let mut index_bytes = [0u8; 32];
index_bytes[24..].copy_from_slice(&(index as u64).to_be_bytes());
let nullifier = Poseidon::hashv(&[leaf, &index_bytes, &event.tx_hash]).unwrap();
tx_hashes.push(event.tx_hash);
nullifiers.push(nullifier);
Expand Down
3 changes: 2 additions & 1 deletion program-tests/utils/src/test_batch_forester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ pub async fn get_batched_nullify_ix_data<R: Rpc>(
let proof = bundle.merkle_tree.get_proof_of_leaf(index, true).unwrap();
merkle_proofs.push(proof.to_vec());
bundle.input_leaf_indices.remove(0);
let index_bytes = index.to_be_bytes();
let mut index_bytes = [0u8; 32];
index_bytes[24..].copy_from_slice(&(index as u64).to_be_bytes());
use light_hasher::Hasher;
let nullifier = Poseidon::hashv(&[&leaf, &index_bytes, &leaf_info.tx_hash]).unwrap();

Expand Down
3 changes: 2 additions & 1 deletion prover/client/src/proof_types/batch_update/proof_inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ pub fn get_batch_update_inputs<const HEIGHT: usize>(
let merkle_proof_array = merkle_proof.try_into().unwrap();

// Use the adjusted index bytes for computing the nullifier.
let index_bytes = (*index).to_be_bytes();
let mut index_bytes = [0u8; 32];
index_bytes[28..].copy_from_slice(&(*index).to_be_bytes());
let nullifier = Poseidon::hashv(&[leaf, &index_bytes, &tx_hashes[i]]).unwrap();
Comment on lines +174 to 176
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major

Centralize this nullifier index encoding.

This 32-byte/right-aligned layout is now open-coded in the prover, tests, and the on-chain nullifier path. In this codepath, any serialization drift changes every nullifier and derived root, so please move the index-buffer construction behind a single shared helper and call that here instead of rebuilding it inline.

♻️ Proposed refactor
+fn nullifier_index_bytes(index: u32) -> [u8; 32] {
+    let mut bytes = [0u8; 32];
+    bytes[28..].copy_from_slice(&index.to_be_bytes());
+    bytes
+}
+
 ...
-        let mut index_bytes = [0u8; 32];
-        index_bytes[28..].copy_from_slice(&(*index).to_be_bytes());
+        let index_bytes = nullifier_index_bytes(*index);
         let nullifier = Poseidon::hashv(&[leaf, &index_bytes, &tx_hashes[i]]).unwrap();
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@prover/client/src/proof_types/batch_update/proof_inputs.rs` around lines 174
- 176, Extract the 32-byte right-aligned index serialization into a single
shared helper (e.g., encode_index_to_32_bytes or encode_nullifier_index) and
replace the inline creation in proof_inputs.rs (the code that currently creates
index_bytes, copies (*index).to_be_bytes() into index_bytes[28..], then passes
it to Poseidon::hashv) with a call to that helper; ensure the helper returns
[u8;32] and is imported/used here so tests, prover code, and the on-chain
nullifier path all call the same function to avoid serialization drift.

let (root, changelog_entry) =
compute_root_from_merkle_proof(nullifier, &merkle_proof_array, *index);
Expand Down
6 changes: 3 additions & 3 deletions prover/client/tests/batch_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ async fn prove_batch_update() {
old_leaves.push(leaf);
merkle_tree.append(&leaf).unwrap();

#[allow(clippy::unnecessary_cast)]
let nullifier =
Poseidon::hashv(&[&leaf, &(i as usize).to_be_bytes(), &tx_hash]).unwrap();
let mut index_bytes = [0u8; 32];
index_bytes[28..].copy_from_slice(&(i as u32).to_be_bytes());
let nullifier = Poseidon::hashv(&[&leaf, &index_bytes, &tx_hash]).unwrap();
nullifiers.push(nullifier);
}

Expand Down
5 changes: 2 additions & 3 deletions xtask/src/zero_indexed_leaf.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fs::File, io::prelude::*, mem, path::PathBuf};
use std::{fs::File, io::prelude::*, path::PathBuf};

use clap::Parser;
use light_hasher::{Hasher, Keccak, Poseidon, Sha256};
Expand Down Expand Up @@ -26,8 +26,7 @@ fn generate_zero_indexed_leaf_for_hasher<H>(opts: Options) -> anyhow::Result<()>
where
H: Hasher,
{
let zero_indexed_leaf =
H::hashv(&[&[0u8; 32], &[0u8; mem::size_of::<usize>()], &[0u8; 32]]).unwrap();
let zero_indexed_leaf = H::hashv(&[&[0u8; 32], &[0u8; 32], &[0u8; 32]]).unwrap();

let code = quote! {
pub const ZERO_INDEXED_LEAF: [u8; 32] = [ #(#zero_indexed_leaf),* ];
Expand Down
Loading