Skip to content

Fix panic in gen_indexes when index modulo N equals zero#847

Open
mwaddip wants to merge 1 commit into
ergoplatform:developfrom
mwaddip:fix/gen-indexes-zero-modulo
Open

Fix panic in gen_indexes when index modulo N equals zero#847
mwaddip wants to merge 1 commit into
ergoplatform:developfrom
mwaddip:fix/gen-indexes-zero-modulo

Conversation

@mwaddip
Copy link
Copy Markdown

@mwaddip mwaddip commented Mar 31, 2026

Summary

gen_indexes panics when any of the 32 extracted 4-byte hash windows,
interpreted as an unsigned big-endian integer, is an exact multiple of N.
The modulo result is zero, BigInt::to_u32_digits() returns an empty
digit vector, and .1[0] panics with index out of bounds.

The JVM reference (AutolykosPowScheme.scala) handles this correctly
via BigInt.toInt, which returns 0 for BigInt(0).

Fix

Replace .1[0] with .1.first().copied().unwrap_or(0) — returns 0
when the digit vector is empty, matching JVM behavior.

Risk

With 32 indices per header and N starting at ~67M, there is a non-trivial
cumulative probability that some mainnet block triggers this during
initial chain sync. The fix is a one-line change with no behavioral
difference for non-zero results.

Test plan

  • Added regression test test_gen_indexes_zero_modulo that triggers
    the exact panic condition (seed hash with 4-byte window equal to N)
  • All existing tests pass unchanged
  • cargo clippy clean
  • cargo fmt clean

to_u32_digits() returns an empty vec for BigInt(0). Accessing .1[0]
panics. Replace with .first().copied().unwrap_or(0) to match the JVM
reference behavior (BigInt.toInt returns 0).
@coveralls
Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 23799568905

Details

  • 17 of 17 (100.0%) changed or added relevant lines in 1 file are covered.
  • 4 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.01%) to 86.904%

Files with Coverage Reduction New Missed Lines %
ergo-lib/src/wallet/tx_builder.rs 4 92.98%
Totals Coverage Status
Change from base Build 21705206395: 0.01%
Covered Lines: 27486
Relevant Lines: 31628

💛 - Coveralls

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.

2 participants