Skip to content

fix(script_number): use arithmetic division instead of signed right shift in encode#2335

Open
deepview-autofix wants to merge 1 commit intobitcoinjs:masterfrom
deepview-autofix:deepview/5c602cc034
Open

fix(script_number): use arithmetic division instead of signed right shift in encode#2335
deepview-autofix wants to merge 1 commit intobitcoinjs:masterfrom
deepview-autofix:deepview/5c602cc034

Conversation

@deepview-autofix
Copy link
Copy Markdown

@deepview-autofix deepview-autofix commented Apr 15, 2026

Replace value >>= 8 with value = Math.floor(value / 256) in the encode function. The signed right shift operator coerces its operand to a 32-bit signed integer via ToInt32, which truncates bits above bit 31.

For 5-byte script numbers with values > 0xFFFFFFFF (e.g. 4294967296), this caused the upper byte information to be completely lost, producing incorrect encoded output.

Math.floor(value / 256) works correctly for all safe JavaScript integer values.

…hift in encode

Replace `value >>= 8` with `value = Math.floor(value / 256)` in the
encode function. The signed right shift operator coerces its operand to
a 32-bit signed integer via ToInt32, which truncates bits above bit 31.
For 5-byte script numbers with values > 0xFFFFFFFF (e.g. 4294967296),
this caused the upper byte information to be completely lost, producing
incorrect encoded output. Math.floor(value / 256) works correctly for
all safe JavaScript integer values.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: DeepView Autofix <276251120+deepview-autofix@users.noreply.github.com>
Co-Authored-By: Nikita Skovoroda <chalkerx@gmail.com>
Signed-off-by: Nikita Skovoroda <chalkerx@gmail.com>
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.

1 participant