Conversation
SetNumNbBits corrupted the sliding window when called on a running reader
(nr.last != nil) whenever wordNbBits > 1 or the reader had advanced:
- scaled by 2^(n'-n) instead of radix^(n'-n) = 2^((n'-n)·wordNbBits),
- read the appended words from the head of toRead instead of after the
current window,
- truncated toRead, discarding input used by subsequent Next() calls.
Widen the window in place (scale by radix^(delta words), read the appended
words from the correct slice, do not mutate toRead) and add regression tests.
This supersedes Consensys-Incorporated#1609, which was closed for lacking regression tests.
Tests: add TestSetNumNbBitsAfterNext (wordNbBits in {1,2,4,8}) and restore the
randomised TestSetNumNbBits, which previously overwrote its generated data with
words={1,0};increases={1}, skipping the nr.last != nil path entirely.
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.
Description
NumReader.SetNumNbBitswidens the sliding window of an already-running reader. Thecurrent code computed the widened value incorrectly whenever
wordNbBits > 1orthe reader had advanced past the first window, and it also truncated the remaining
input, which silently corrupted all subsequent reads.
This supersedes #1609, whose fix was closed for "Cannot quickly evaluate correctness
of the PR. Lacking regression tests." This PR contains the same core fix plus the
missing regression tests, so the change is now verifiable.
The fix:
radix^(n'-n)instead of2^(n'-n),(
nr.toRead[n:n']) instead of from the head,nr.toRead—next()relies on a stable start index.The change adds no constraints relative to the buggy version (see Benchmarking).
Fixes #1864
Type of change
How has this been tested?
TestSetNumNbBitsAfterNext: a new table-driven test coveringwordNbBits ∈ {1,2,4,8}and several resize schedules, asserting against an independent native oracle
(
numReaderNums). It fails onmasterand passes with the fix.TestSetNumNbBits: removed the leftoverwords = []byte{1,0}; increases = []byte{1}override that disabled the randomised test (and guarded the index into
buf). It nowactually exercises the
nr.last != nilresize path and fails onmaster../std/compress/...suite passes (go test ./std/compress/... -count=1).How has this been benchmarked?
Constraint count is unchanged: the resize path still performs one
api.Mul+ oneapi.Add(plus a secondapi.Mulonly when the window runs past the end of theinput), identical to the original code. The no-resize path (
wordsPerNumunchanged)is now skipped entirely. No benchmark run required;
-shortsafe.Checklist:
golangci-lintdoes not output errors locally (gofmt+go vetclean)Note
Medium Risk
Corrects constraint-circuit number packing used in compress paths; wrong values could have silently broken proofs, but the fix is localized with new regression tests and unchanged constraint shape on the resize path.
Overview
Fixes
NumReader.SetNumNbBitswhen the reader is already running and the bit window is widened. The old path scaled the held value with2^(n'-n)instead ofradix^(n'-n), pulled new words from the wrong offset intoRead, and truncatedtoRead, which broke laterNext()reads wheneverwordNbBits > 1or the head had advanced.The resize logic now widens
nr.lastin place (head unchanged): multiply byr^(n'-n), append words fromtoRead[n:n'], zero-pad past the buffer end, and leavetoReaduntouched sonext()still shifts from index 0.Tests: adds
TestSetNumNbBitsAfterNext(several resize schedules andwordNbBits ∈ {1,2,4,8}) against a native oraclenumReaderNums; restores randomizedTestSetNumNbBits(drops the hard-coded override, boundsincreasesindexing); test circuit takes configurablewordNbBits.Reviewed by Cursor Bugbot for commit a6cc814. Bugbot is set up for automated code reviews on this repo. Configure here.