Repository navigation
fix(ergo-nipopow): reject m == 0 instead of looping forever in best_arg - #951
Merged
Merged
Conversation
A NipopowProof parsed from bytes carries m straight from the wire, and nothing checked it: NipopowProof::new rejected only k == 0, is_valid() checked connections/heights/proofs but not the parameters, and best_arg's level loop (args.len() >= m) is unconditionally true for m == 0, so is_better_than / NipopowVerifier::process hung forever on a peer-supplied proof with m = 0. prove() also accepted m == 0 and then panicked indexing sub_chain[sub_chain.len() - m]. Mirror the reference implementation (ergo node NipopowAlgos / NipopowProof / PoPowParams): - NipopowProof::new and NipopowAlgos::prove return the new NipopowProofError::ZeroMParameter for m == 0 (mirrors ZeroKParameter; bestArg's require(m >= 1)). - is_valid() checks the parameters first (PoPowParams.isValid: m >= 1 && k >= 1 && m + k <= i32::MAX), so parsed proofs with invalid parameters are never compared. - best_arg's loop also stops when no header reaches the current level, so it terminates for any m when called directly; no change for m >= 1. Regression tests cover all five paths (invalid-and-never-better, verifier process, new, prove, best_arg on an empty chain).
kushti
approved these changes
Oct 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #950. a
NipopowProofwithm == 0hangs proof comparison forever.mcomes straight from the wire inscorex_parse; nothing checked it, andbest_arg's level loop (args.len() >= m) is unconditionally true form == 0.The reference implementation guards this in three places, and this PR mirrors all three:
bestArghasrequire(m >= 1)→NipopowProof::newandNipopowAlgos::provenow return the newNipopowProofError::ZeroMParameterform == 0(mirroring the existingZeroKParameterfork). Theprovepath additionally removes a latent panic: withm == 0it indexedsub_chain[sub_chain.len() - m]on a non-empty sub-chain.NipopowProof.isValidchecksPoPowParams.isValid(m, k)(m >= 1 && k >= 1 && m + k <= Int.MaxValue) first →is_valid()now does the same via a newhas_valid_params(). Parsed proofs with invalid parameters are simply invalid, sois_better_thannever reachesbest_argwith them (it returnsself.is_valid(), matching the reference's fallback).best_argcallers, the level loop also stops when no header reaches the current level: with an emptyargs, every higher level is empty too. This changes nothing form >= 1(thereargs.len() >= malready implies non-empty).Verification
Fail-first: on pristine
developa minimalm = 0proof (single suffix-head header, empty interlinks/proof, so the other validity checks pass) never returns fromis_better_than— killed after a 25 s timeout.New regression tests (ergo-nipopow,
--all-features):zero_m_proof_is_invalid_and_never_better— the hanging call now returnsOk(false); the proof is invalid solely because of its parameters.verifier_processes_zero_m_proofs—NipopowVerifier::processreturns for a secondm = 0proof instead of hanging in the comparison.new_rejects_zero_m_and_zero_k—NipopowProof::newreturnsZeroMParameter/ZeroKParameter.prove_rejects_zero_m—provereturnsZeroMParameterinstead of panicking.best_arg_zero_m_empty_chain—best_arg(&[], 0)returns0(the diverging-chain shape the comparison actually produces).Suites: ergo-nipopow
--all-features6/6 (incl. the existing roundtrip proptest), ergo-lib--all-featuresgreen; clippy-D warningsclean; fmt clean.Submitted by @Kshot9000 (https://x.com/kshot9000) — independent Ergo builder.
Tips: ERG 9fcM5RWnAjmP4vx5bnW6yohB6H9bLq8sJbaPLHtwZLtQPB32Pvy