Skip to content

Commit 9a4faed

Browse files
committed
fix(fast-inbox): harden the leaf-index guards in append_leaves_to_snapshot (A-1372)
1 parent f961ad2 commit 9a4faed

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

noir-projects/noir-protocol-circuits/crates/types/src/merkle_tree/append_only_tree.nr

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,9 @@ pub fn append_leaves_to_snapshot<let TreeHeight: u32, let MaxLeaves: u32>(
131131
frontier_hint: [Field; TreeHeight],
132132
) -> AppendOnlyTreeSnapshot {
133133
// We cast the leaf index to a u64, so heights above 64 would insert at the wrong index once the tree grows past
134-
// 2^64 leaves.
135-
assert(
134+
// 2^64 leaves. Checking at compile time turns a bad instantiation into a build error rather than a circuit no
135+
// prover can satisfy.
136+
std::static_assert(
136137
TreeHeight <= 64,
137138
"`append_leaves_to_snapshot` does not support trees with height greater than 64",
138139
);
@@ -147,6 +148,10 @@ pub fn append_leaves_to_snapshot<let TreeHeight: u32, let MaxLeaves: u32>(
147148
merkle_hash(zero_subtree_roots[level - 1], zero_subtree_roots[level - 1]);
148149
}
149150

151+
// The cast below truncates, so an index above 2^64 would silently drop its high bits and append at the wrong
152+
// position. Callers keep the index far below that by only ever advancing it by the number of leaves appended, but
153+
// nothing in this function's signature enforces it, so pin it here.
154+
snapshot.next_available_leaf_index.assert_max_bit_size::<64>();
150155
let start_index = snapshot.next_available_leaf_index as u64;
151156
let end_index = start_index + num_leaves as u64;
152157
// Guard against wrapping past the tree capacity, which would otherwise silently produce a wrong root.

0 commit comments

Comments
 (0)