Repository navigation
fix: clear surplus words in notn so result stays below 2 ** width - #319
Merged
Merged
Conversation
inotn inverts the words covering the requested width but never clears the
words above it. When the value occupies more 26-bit words than the width
requires, those high words survive _strip(), so the result exceeds 2 ** width.
new BN('5991996425021').notn(15).toString()
// was '5991949171906', expected '31938'
Zero the words from the first index past the width up to this.length before
stripping, matching the w-bit complement contract that the result is < 2 ** width.
fanatid
pushed a commit
that referenced
this pull request
Jun 26, 2026
…th (#319) inotn inverts the words covering the requested width but never clears the words above it. When the value occupies more 26-bit words than the width requires, those high words survive _strip(), so the result exceeds 2 ** width. new BN('5991996425021').notn(15).toString() // was '5991949171906', expected '31938' Zero the words from the first index past the width up to this.length before stripping, matching the w-bit complement contract that the result is < 2 ** width.
Collaborator
|
Thanks! published in 4.12.4 and 5.2.4 |
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.
When
widthis smaller than the bit length of the value,notn(width)returns a number larger than2 ** width.Repro
Checked against the native BigInt oracle for the same operation:
Contract
notn(width)is the w-bit bitwise complement: the result must be< 2 ** width. Single-word values are fine; the bug only shows when the value occupies more 26-bit words thanwidthrequires.Root cause
inotncomputesbytesNeeded = Math.ceil(width / 26), inverts the words covering[0, bytesNeeded)plus the residue word, but never clears the words above that range._expandonly growsthis.length, it never shrinks, so when the value already has more words than the width needs, those surplus high words are left untouched and survive_strip().Existing
.notn()tests only usewidth >= bitLength(value), sowidth < bitLengthwas never exercised.Fix
Zero
this.words[j]for everyjfrom the first index past the requested width up tothis.length, before stripping.Tests
Added a case to
test/binary-test.js.notn()that checks multi-word values withwidth < bitLengthagainst the BigInt oracle. It fails on master and passes with the fix. Full suite stays green (175 passing, lint clean).