fix(serder)!: #147 ample(3) diverges from keripy — port the exact f1/f2 threshold formula#152
Merged
Merged
Conversation
…mula ample() computed the simplified max(1, ceil(2n/3)), which diverges from keripy's ample() at exactly n = 3: keripy returns 3, we returned 2. A 3-witness inception built without an explicit witness threshold emitted "bt":"2" where keripy emits "bt":"3", byte-diverging from the oracle and weakening the intended BFT margin. Port the real formula (eventing.py:69-106 @ de59bc7d, weak defaults): minimize m over both the floor and ceil candidates for the max fault count f subject to n >= 3f+1 and (n+f+1)/2 <= m <= n-f. BREAKING: ample(n) now returns Result<u32, SerderError> — the unwrap_or(u32::MAX) sentinel conversion is replaced with a typed SerderError::Validation on thresholds exceeding u32 (checked arithmetic throughout, per the arithmetic-safety rules). Tests: keripy test_eventing_v1 table (n = 0..13), exhaustive sweep + proptest vs a verbatim keripy oracle over n = 0..=256, 64-bit overflow error paths, and the flipped bug-probes (ample(3) == 3, builder bt "3"). Closes #147 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
3 tasks
Merging this PR will not alter performance
Comparing Footnotes
|
Open
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
serder::amplecomputed the simplifiedmax(1, ceil(2n/3)), which diverges from keripy'sample()at exactly n = 3: keripy returns 3, we returned 2. A 3-witness inception built without an explicit witness threshold emitted"bt":"2"where keripy emits"bt":"3"— byte-divergent from the oracle and a weakened BFT margin.This ports the real formula (keripy
eventing.py:69-106@de59bc7d,f=None, weak=Truedefaults): minimizemover both the floor and ceiling candidates for the maximum fault countfsubject ton >= 3f+1and(n+f+1)/2 <= m <= n-f.Breaking change (0.x MINOR)
ample(n: usize)now returnsResult<u32, SerderError>instead ofu32:unwrap_or(u32::MAX)sentinel conversion (banned pattern) is replaced with a typedSerderError::Validationwhen the threshold exceedsu32;checked_sub/checked_add), no bare ops;InceptionBuilder/DelegatedInceptionBuilderpropagate the error through their existingResult(no builder-surface change).Called out in
CHANGELOG.mdunder Unreleased.Acceptance criteria (from #147)
test_eventing_v1.pyample()expectations for n = 0..13ample_three_witnessesflipped to assert 3 (verified failing against the old formula before the fix)checked_addoverflow atusize::MAX,u32::try_fromfailure at 7·10⁹) have testsVerification
nix flake check— all checks green (clippy, fmt, taplo, audit, deny, 1993 nextest tests, doctests, wasm, no_std, fuzz-replay)ample(3)→ 2, builderbt→ "2"), GREEN after the portamplefrom the localde59bc7dcheckout): zero divergences over n = 0..=100,000; reproduce with:Out of scope (noted, not touched)
majority()inbuilder/icp.rs:72carries the sameunwrap_or(u64::MAX)sentinel pattern — same class of cleanup, separate signature, deserves its own scoped issue.Closes #147
🤖 Generated with Claude Code