Skip to content

Commit ceb52cb

Browse files
test: tighten 2 CodeRabbit nitpicks on PCPS coverage tests
Addresses the 2 nitpick suggestions from CodeRabbit's latest review on PR #670. Both are valid quality improvements that make existing tests more deterministic / observable. **mod.rs: trunk_query min_depth privacy-path assertion** The previous `test_trunk_query_with_min_depth_engages_privacy_path_for_pcps` only checked that `trunk_query` returned `Ok` — but the non-privacy path also returns `Ok`, so a regression that silently fell back to `calculate_chunk_depths` would still pass the test. Tightened to: - Use 25 keys (AVL tree depth ≥ 6) with `max_depth=4`, `min_depth=4` — parameters where the two depth-split functions return *different* vectors: `calculate_chunk_depths(6, 4)` → `[3, 3]` (natural even split) vs `calculate_chunk_depths_with_minimum(6, 4, 4)` → `[4, 2]` (front chunk clamped up to min_depth). - Assert `result.chunk_depths == calculate_chunk_depths_with_minimum(tree_depth, max_depth, min_depth)` — pins the privacy-path output as the headline check. - Assert the non-privacy output differs — guards against test-vacuity if a future change accidentally makes the two functions return the same output for these inputs. A regression where the PCPS arm in `is_provable_count_tree` is dropped would now fail the privacy-path equality assertion. **aggregate_count/tests.rs: narrow shape_walk_rejects_own_count_underflow** The previous version of this test zeroed *every* `KVDigestCount` in the proof op stream, which could trip an earlier shape error before the verifier ever reached the `checked_sub` underflow arm — making the test non-deterministic (the rejection message might come from an unrelated arm). Per CodeRabbit suggestion: iterate `ops.iter_mut().rev()` and `break` on the first match, mutating only the *last* `KVDigestCount` op (the deepest parent boundary node in the walk, whose children are already on the proof stack). This makes the `checked_sub` underflow the deterministic target of the rejection. All 555 merk lib tests pass; cargo fmt clean. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 7b045cf commit ceb52cb

2 files changed

Lines changed: 58 additions & 16 deletions

File tree

‎merk/src/merk/mod.rs‎

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1882,13 +1882,29 @@ mod test {
18821882
/// `ProvableCountTree | ProvableCountSumTree`, so PCPS with
18831883
/// `min_depth` would silently fall into the non-privacy path and
18841884
/// leak small-subtree information.
1885+
///
1886+
/// Tightened (per CodeRabbit review): the test now asserts the
1887+
/// returned `chunk_depths` matches the privacy function's output
1888+
/// AND that this differs from the non-privacy function's output,
1889+
/// so a regression that silently falls back to the non-privacy
1890+
/// path would fail this assertion.
18851891
#[test]
18861892
fn test_trunk_query_with_min_depth_engages_privacy_path_for_pcps() {
1887-
use crate::TreeFeatureType::ProvableCountedAndProvableSummedMerkNode;
1893+
use crate::{
1894+
proofs::branch::depth::{calculate_chunk_depths, calculate_chunk_depths_with_minimum},
1895+
TreeFeatureType::ProvableCountedAndProvableSummedMerkNode,
1896+
};
18881897
let grove_version = GroveVersion::latest();
18891898
let mut merk =
18901899
TempMerk::new_with_tree_type(grove_version, TreeType::ProvableCountProvableSumTree);
1891-
let batch: Vec<(Vec<u8>, crate::Op)> = (0u64..15)
1900+
// 25 keys → AVL tree depth ≥ 6 (Fibonacci-minimum
1901+
// 20=N(6) ≤ 25 < N(7)=33). At depth 6 with max_depth=4 and
1902+
// min_depth=4, the two depth-split functions return
1903+
// **different** vectors: non-privacy produces [3, 3] (the
1904+
// natural even split) while privacy produces [4, 2] (front
1905+
// chunk clamped up to min_depth). So a non-engaged privacy
1906+
// path is observable in the output.
1907+
let batch: Vec<(Vec<u8>, crate::Op)> = (0u64..25)
18921908
.map(|n| {
18931909
(
18941910
n.to_be_bytes().to_vec(),
@@ -1903,18 +1919,39 @@ mod test {
19031919
.unwrap()
19041920
.expect("apply");
19051921

1906-
// min_depth = 5; tree_depth of 15 keys with privacy clamping
1907-
// engaged must produce a result whose chunk_depths reflect the
1908-
// minimum-clamped first chunk depth. We only check the call
1909-
// succeeds — the exact chunk_depths layout is an internal
1910-
// detail of `calculate_chunk_depths_with_minimum`. Without the
1911-
// PCPS arm in `is_provable_count_tree`, the privacy path
1912-
// wouldn't be engaged at all (silently fell back to the
1913-
// non-privacy depth calculation).
1922+
let max_depth = 4u8;
1923+
let min_depth = 4u8;
19141924
let result = merk
1915-
.trunk_query(8, Some(5), grove_version)
1925+
.trunk_query(max_depth, Some(min_depth), grove_version)
19161926
.unwrap()
19171927
.expect("trunk_query with min_depth on PCPS must succeed");
19181928
assert!(!result.proof.is_empty());
1929+
1930+
// The returned chunk_depths must equal the privacy function's
1931+
// output for (tree_depth, max_depth, min_depth) — this is the
1932+
// headline assertion: the PCPS arm in `is_provable_count_tree`
1933+
// routes to the privacy depth calculator.
1934+
let expected_privacy =
1935+
calculate_chunk_depths_with_minimum(result.tree_depth, max_depth, min_depth)
1936+
.expect("expected privacy chunk-depth calculation to succeed");
1937+
assert_eq!(
1938+
result.chunk_depths, expected_privacy,
1939+
"trunk_query on PCPS with min_depth must use the privacy depth split"
1940+
);
1941+
1942+
// Sanity: confirm the two depth functions actually return
1943+
// **different** vectors for these inputs so the equality
1944+
// assertion above isn't a coincidence — i.e. if we'd silently
1945+
// fallen into the non-privacy branch, the assertion above
1946+
// would have failed.
1947+
let non_privacy = calculate_chunk_depths(result.tree_depth, max_depth)
1948+
.expect("non-privacy chunk-depth calculation");
1949+
assert_ne!(
1950+
non_privacy, expected_privacy,
1951+
"test setup invariant: at tree_depth={}, max_depth={}, min_depth={}, the two depth \
1952+
functions must produce different vectors — otherwise the privacy-engaged assertion \
1953+
above is vacuous",
1954+
result.tree_depth, max_depth, min_depth,
1955+
);
19191956
}
19201957
}

‎merk/src/proofs/query/aggregate_count/tests.rs‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1764,15 +1764,20 @@ fn shape_walk_rejects_own_count_underflow() {
17641764
.unwrap()
17651765
.expect("prove succeeds");
17661766

1767-
// Find the LAST KVDigestCount op (deeper in the walk, more likely
1768-
// to be a parent boundary with children already pushed). Lower its
1769-
// count to 0 so any non-zero left_struct + right_struct exceeds
1770-
// the parent aggregate.
1767+
// Mutate ONLY the last KVDigestCount op (per CodeRabbit review):
1768+
// that's the parent boundary node whose children are already on
1769+
// the proof stack, so zeroing it specifically triggers the
1770+
// `checked_sub` underflow when the verifier computes
1771+
// `own_count = aggregate - left_struct - right_struct`. Mutating
1772+
// every KVDigestCount in the stream could trip an earlier,
1773+
// unrelated shape error before the verifier ever reaches this
1774+
// arm — making the test non-deterministic.
17711775
let mut rewrote = false;
1772-
for op in ops.iter_mut() {
1776+
for op in ops.iter_mut().rev() {
17731777
if let ProofOp::Push(Node::KVDigestCount(_, _, c)) = op {
17741778
*c = 0;
17751779
rewrote = true;
1780+
break;
17761781
}
17771782
}
17781783
assert!(

0 commit comments

Comments
 (0)