Skip to content

fix: validate child_order and use child positions consistently in Go and Rust - #433

Open
citizen-stig wants to merge 2 commits into
cosmos:masterfrom
citizen-stig:nikolai/pr-423-followup
Open

citizen-stig wants to merge 2 commits into
cosmos:masterfrom
citizen-stig:nikolai/pr-423-followup

Conversation

@citizen-stig

Copy link
Copy Markdown
Contributor

Follow-up to #429, addressing #429 (review). Includes #429's commit until that merges.

Problem

  • Rust get_padding used the matched child_order value as the position (find(|&&x| x == branch) returns branch), while Go getPadding uses getPosition. The two agree only for [0, 1].
  • Checking that child_order is a permutation does not fix that by itself: [1, 0] is a valid permutation and still diverges.
  • leftBranchesAreEmpty / rightBranchesAreEmpty (and the Rust equivalents) assume an identity order in both implementations. For orders like merk's [0, 2, 1], which the proto documents, they return wrong answers, and Go panics with
    slice bounds out of range.

Changes

  • Both Go and Rust now check that child_order is a permutation of 0..n with n >= 2 when an existence proof is checked against the spec. This replaces the empty-only check from fix(rust): reject specs with empty child_order #429.
  • Rust looks up child positions the same way Go's getPosition does.
  • Left and right empty-sibling checks share one helper per language. It looks up each sibling in the op's prefix or suffix by its position. Results for [0, 1] are unchanged.

Breaking

Specs whose child_order is not a permutation of 0..n with n >= 2 (including [0]) are now rejected. All shipped specs use [0, 1].

Tests

  • TestEmptyBranchMerkOrder / check_empty_branch_merk_order cover [0, 2, 1]. Both fail on the old code (Go panics).
  • TestRejectInvalidChildOrder / reject_invalid_child_order cover [], [0], [0,0], [0,2], [1,2] and [-1,0].
  • go test ./..., cargo test (44 passed) and cargo clippy --all-targets all pass.

Not in scope

Other Go/Rust differences found while auditing, to be fixed in separate PRs:

🤖 Generated with Claude Code

citizen-stig and others added 2 commits November 19, 2026 10:36
`child_order.len() - 1` in ensure_inner and ensure_right_most underflowed
when the spec had no child_order, which panics with overflow checks on.
Found by the new fuzz target; verification now fails instead.

Co-Authored-By: Claude Mythos 5.1 <noreply@anthropic.com>
Specs must now have a child_order that is a permutation of 0..n with
n >= 2, checked in both Go and Rust when the existence proof is checked
against the spec.

Rust get_padding used the matched child_order value as the position,
while Go used getPosition; they agreed only for [0, 1]. Rust now looks
up positions the same way Go does.

leftBranchesAreEmpty / rightBranchesAreEmpty (and the Rust equivalents)
assumed an identity order: for orders like merk's [0, 2, 1] they gave
wrong answers and Go panicked with slice bounds out of range. Both now
share a helper that looks up each sibling in the prefix or suffix by
its position. Results for [0, 1] are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@greptile-apps

greptile-apps Bot commented Dec 2, 2026

Copy link
Copy Markdown

This PR does not match any of the 1 configured review trigger rule.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant