From bbe11964b1bd1f57bbebc406d1bf7e0e04ac2ac4 Mon Sep 17 00:00:00 2001 From: Krushimir Date: Thu, 13 Feb 2025 20:00:16 +0100 Subject: [PATCH] fix: `subtree8-rand` benchmark panics with debug assertions enabled (#377) (#378) --- CHANGELOG.md | 1 + benches/smt-subtree.rs | 1 + src/merkle/smt/full/concurrent/mod.rs | 12 ++++++++++++ 3 files changed, 14 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b545fef..1804c64 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - Added parallel implementation of `Smt::compute_mutations` with better performance (#365). - Implemented parallel leaf hashing in `Smt::process_sorted_pairs_to_leaves` (#365). - [BREAKING] Updated Winterfell dependency to v0.12 (#374). +- Added debug-only duplicate column check in `build_subtree` (#378). ## 0.13.3 (2025-02-12) diff --git a/benches/smt-subtree.rs b/benches/smt-subtree.rs index 89cd66c..6938a33 100644 --- a/benches/smt-subtree.rs +++ b/benches/smt-subtree.rs @@ -100,6 +100,7 @@ fn smt_subtree_random(c: &mut Criterion) { }) .collect(); leaves.sort(); + leaves.dedup_by_key(|leaf| leaf.col); leaves }, |leaves| { diff --git a/src/merkle/smt/full/concurrent/mod.rs b/src/merkle/smt/full/concurrent/mod.rs index fc5d71e..bf7e636 100644 --- a/src/merkle/smt/full/concurrent/mod.rs +++ b/src/merkle/smt/full/concurrent/mod.rs @@ -478,6 +478,18 @@ fn build_subtree( tree_depth: u8, bottom_depth: u8, ) -> (UnorderedMap, SubtreeLeaf) { + #[cfg(debug_assertions)] + { + // Ensure that all leaves have unique column indices within this subtree. + // In normal usage via public APIs, this should never happen because leaf + // construction enforces uniqueness. However, when testing or benchmarking + // `build_subtree()` in isolation, duplicate columns can appear if input + // constraints are not enforced. + let mut seen_cols = BTreeSet::new(); + for leaf in &leaves { + assert!(seen_cols.insert(leaf.col), "Duplicate column found in subtree: {}", leaf.col); + } + } debug_assert!(bottom_depth <= tree_depth); debug_assert!(Integer::is_multiple_of(&bottom_depth, &SUBTREE_DEPTH)); debug_assert!(leaves.len() <= usize::pow(2, SUBTREE_DEPTH as u32));