Skip to content

Commit 59a59a7

Browse files
fix(merk): hash_for_link missing arms for indexed-tree primaries
Pre-existing PCIT bug surfaced by the V1 PCIT subquery test (test_v1_proof_supports_count_indexed_tree_subquery). Root cause: Tree::hash_for_link(tree_type) had arms only for ProvableCountTree / ProvableSumTree / ProvableCountSumTree / ProvableCountProvableSumTree. The three indexed-tree primaries (ProvableSumIndexedTree, ProvableCountIndexedTree, ProvableCountProvableSumIndexedTree) fell through to plain self.hash() which uses node_hash (no aggregate baked in). Meanwhile the proof emitter in merk/proofs/query/mod.rs keyed off the node's feature_type (ProvableCountedMerkNode / etc.) and correctly emitted KVCount-style proof ops, which the verifier reconstructed via node_hash_with_count. Result: Merk's stored root hash for a PCIT primary disagreed with what the proof reconstructed, causing 'V1 mismatch in cidx lower-layer hash' on every V1 PCIT subquery. The bug was masked under the legacy non-provable CountIndexedTree (now removed in Phase 1) because that variant used CountNode (count not in hash) so the proof emitter also emitted plain KV nodes — the two sides agreed by accident. Fix: add arms in hash_for_link that delegate the indexed variants to their plain Provable* counterparts. PCIT -> ProvableCountTree, PSIT -> ProvableSumTree, PCPSIT -> ProvableCountProvableSumTree. Test: un-#[ignore] the V1 PCIT subquery test (now passes), and add a low-level regression test indexed_primaries_match_non_indexed_provable_hashes in merk/tree/test_provable_count.rs asserting that every indexed primary's hash_for_link result is byte-identical to the corresponding non-indexed Provable* arm. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent c781b52 commit 59a59a7

3 files changed

Lines changed: 76 additions & 2 deletions

File tree

‎grovedb/src/tests/v1_proof_tests.rs‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,8 +1311,6 @@ fn test_v0_proof_rejects_dense_tree_subquery() {
13111311
/// PCIT lives in `tests/count_indexed_tree_tests.rs` (currently
13121312
/// gated off for Phase 1; Phase 2 will restore the split suite).
13131313
#[test]
1314-
#[ignore = "Phase 1: latent PCIT V1-subquery hash-mismatch unrelated to this refactor; \
1315-
tracked alongside the count_indexed_tree_tests rewrite"]
13161314
fn test_v1_proof_supports_count_indexed_tree_subquery() {
13171315
let grove_version = GroveVersion::latest();
13181316
let db = make_empty_grovedb();

‎merk/src/tree/mod.rs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,20 @@ impl TreeNode {
768768
);
769769
}
770770
}
771+
// The three indexed-tree primaries mirror their non-indexed
772+
// Provable* counterparts at the Merk-node level: their nodes
773+
// carry the corresponding `Provable*MerkNode` feature_type and
774+
// the proof emitter keys off that feature_type. Delegate to the
775+
// matching Provable* arm so `root_hash_key_and_aggregate_data()`
776+
// returns the same `node_hash_with_*` the proof reconstructs.
777+
// (Pre-existing bug: before this delegation the indexed-tree
778+
// primaries fell through to plain `self.hash()` and produced
779+
// root hashes the proof verifier could not reconstruct.)
780+
TreeType::ProvableSumIndexedTree => self.hash_for_link(TreeType::ProvableSumTree),
781+
TreeType::ProvableCountIndexedTree => self.hash_for_link(TreeType::ProvableCountTree),
782+
TreeType::ProvableCountProvableSumIndexedTree => {
783+
self.hash_for_link(TreeType::ProvableCountProvableSumTree)
784+
}
771785
TreeType::ProvableCountProvableSumTree => {
772786
// For ProvableCountProvableSumTree, include BOTH the
773787
// aggregate count AND the aggregate sum in the hash via

‎merk/src/tree/test_provable_count.rs‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,66 @@ mod tests {
112112
_ => panic!("Expected ProvableCount aggregate data"),
113113
}
114114
}
115+
116+
/// Regression: indexed-tree primaries (PCIT / PSIT / PCPSIT) must produce
117+
/// the same `hash_for_link` as their non-indexed `Provable*` counterparts.
118+
///
119+
/// Prior to the fix, `hash_for_link(ProvableCountIndexedTree)` fell
120+
/// through to plain `self.hash()` (no count baked in) while the proof
121+
/// emitter — which keys off the node's `feature_type` — emitted
122+
/// count-aware proof ops. The two encodings produced different root
123+
/// hashes for the same tree, causing V1 PCIT subquery proofs to fail
124+
/// with "V1 mismatch in cidx lower-layer hash".
125+
#[test]
126+
fn indexed_primaries_match_non_indexed_provable_hashes() {
127+
let pcit_node = TreeNode::new(
128+
vec![1, 2, 3],
129+
vec![4, 5, 6],
130+
None,
131+
TreeFeatureType::ProvableCountedMerkNode(7),
132+
)
133+
.unwrap();
134+
assert_eq!(
135+
pcit_node
136+
.hash_for_link(TreeType::ProvableCountIndexedTree)
137+
.unwrap(),
138+
pcit_node
139+
.hash_for_link(TreeType::ProvableCountTree)
140+
.unwrap(),
141+
"ProvableCountIndexedTree primary must hash identically to ProvableCountTree"
142+
);
143+
144+
let psit_node = TreeNode::new(
145+
vec![9, 9, 9],
146+
vec![8, 8, 8],
147+
None,
148+
TreeFeatureType::ProvableSummedMerkNode(42),
149+
)
150+
.unwrap();
151+
assert_eq!(
152+
psit_node
153+
.hash_for_link(TreeType::ProvableSumIndexedTree)
154+
.unwrap(),
155+
psit_node.hash_for_link(TreeType::ProvableSumTree).unwrap(),
156+
"ProvableSumIndexedTree primary must hash identically to ProvableSumTree"
157+
);
158+
159+
let pcpsit_node = TreeNode::new(
160+
vec![0, 1, 2],
161+
vec![3, 4, 5],
162+
None,
163+
TreeFeatureType::ProvableCountedAndProvableSummedMerkNode(3, -11),
164+
)
165+
.unwrap();
166+
assert_eq!(
167+
pcpsit_node
168+
.hash_for_link(TreeType::ProvableCountProvableSumIndexedTree)
169+
.unwrap(),
170+
pcpsit_node
171+
.hash_for_link(TreeType::ProvableCountProvableSumTree)
172+
.unwrap(),
173+
"ProvableCountProvableSumIndexedTree primary must hash identically to \
174+
ProvableCountProvableSumTree"
175+
);
176+
}
115177
}

0 commit comments

Comments
 (0)