Skip to content

fix:modified utils.py to remove merkle sibling#47

Open
Arpitsh7 wants to merge 1 commit intoAOSSIE-Org:mainfrom
Arpitsh7:bug/merkle-sibling-index
Open

fix:modified utils.py to remove merkle sibling#47
Arpitsh7 wants to merge 1 commit intoAOSSIE-Org:mainfrom
Arpitsh7:bug/merkle-sibling-index

Conversation

@Arpitsh7
Copy link
Contributor

@Arpitsh7 Arpitsh7 commented Mar 7, 2026

Fix: Replace XOR Sibling Index with Explicit Parity Check in generate_merkle_proof()

fixes #45

What this PR does

As described in the issue, generate_merkle_proof() and compute_merkle_root() used two different strategies for handling odd-length tree levels:

Function Old odd-node strategy New odd-node strategy
compute_merkle_root() Inline: right = left if i+1 >= len(leaves) Unchanged
generate_merkle_proof() Pre-mutates: leaves.append(leaves[-1]) Now matches compute_merkle_root()

Changes Made

# BEFORE — fragile XOR dependent on prior array mutation
if len(leaves) % 2 == 1:
    leaves.append(leaves[-1])

sibling_index = index ^ 1
sibling = leaves[sibling_index]
is_left = sibling_index < index

next_level = []
for i in range(0, len(leaves), 2):
    combined = leaves[i] + leaves[i + 1]
    ...

# AFTER — explicit parity check, no hidden dependencies
if index % 2 == 0:
    sibling_index = index + 1
    if sibling_index >= len(leaves):
        sibling_index = index  # odd level — no real sibling, duplicate self
    is_left = False
else:
    sibling_index = index - 1
    is_left = True

next_level = []
for i in range(0, len(leaves), 2):
    left = leaves[i]
    right = leaves[i + 1] if (i + 1) < len(leaves) else left  # unified with compute_merkle_root()
    combined = left + right
    ...

Why this is safe

  • Both functions now use identical odd-node logic
  • verify_merkle_proof() is unchanged — existing proofs remain verifiable
  • No other functions were modified

Testing Checklist

  • Proof generates correctly for even number of chunks
  • Proof generates correctly for odd number of chunks
  • verify_merkle_proof() returns True for valid proof against root
  • verify_merkle_proof() returns False for tampered chunk

Labels: bug, merkle-tree, cryptography

Checklist

  • My code follows the project's code style and conventions
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings or errors
  • I have joined the Discord server and I will share a link to this PR with the project maintainers there
  • I have read the Contributing Guidelines

Summary by CodeRabbit

  • Refactor
    • Improved the internal verification tree computation algorithm for more reliable and consistent handling of validation operations, with better support for edge cases.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 7, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 3210a78d-bcf4-403a-bb61-5c65e3b6271f

📥 Commits

Reviewing files that changed from the base of the PR and between c258e31 and 5dcd7c2.

📒 Files selected for processing (1)
  • openverifiablellm/utils.py

Walkthrough

Refactored generate_merkle_proof() to replace XOR-based sibling index calculation with explicit parity checks, and replaced leaf pre-padding with inline odd-node handling. This aligns the sibling selection logic with compute_merkle_root() for improved maintainability and eliminates fragile dependencies on array mutations.

Changes

Cohort / File(s) Summary
Merkle Proof Logic Refactor
openverifiablellm/utils.py
Replaced XOR sibling selection (index ^ 1) with explicit parity-based approach; removed pre-padding logic that duplicated the last leaf; updated is_left flag derivation to use parity instead of sibling_index; modified next-level hash construction to handle odd leaves inline (right defaults to left when missing).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested labels

Python Lang

Poem

Hoppity hoppity, leaf by leaf we go,
Parity paints the siblings' flow,
No fragile XOR tricks remain,
Merkle trees now march in sync's domain,
🐰 Consistency blooms, the proof is sound!

🚥 Pre-merge checks | ✅ 3 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Title check ❓ Inconclusive The title is partially related to the changeset, referring to a real aspect of the change (modifying utils.py and Merkle sibling logic) but is vague and imprecise with unclear phrasing like 'remove merkle sibling'. Clarify the title to better describe the specific change: consider 'fix: unify odd-node handling in Merkle proof generation' or 'fix: replace XOR sibling calculation with explicit parity check'.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Linked Issues check ✅ Passed The PR successfully addresses all coding requirements from issue #45: unifies odd-node handling strategies, replaces XOR sibling calculation with explicit parity checks, guards against out-of-bounds access, and ensures proofs remain verifiable.
Out of Scope Changes check ✅ Passed All changes in generate_merkle_proof are directly scoped to addressing issue #45; no unrelated modifications or out-of-scope changes detected beyond the stated objectives.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions github-actions bot added size/S and removed size/S labels Mar 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: Merkle Sibling Index Inconsistency in generate_merkle_proof() May Failures

1 participant