Skip to content

fix(math): preserve signed BN values and formatting - #3982

Open
yinbowen-dc wants to merge 1 commit into
FuelLabs:masterfrom
yinbowen-dc:codex/fix-3981-signed-bn
Open

yinbowen-dc wants to merge 1 commit into
FuelLabs:masterfrom
yinbowen-dc:codex/fix-3981-signed-bn

Conversation

@yinbowen-dc

Copy link
Copy Markdown

Closes #3981.

Release notes

Fixed negative BN values losing their sign during copying and arithmetic, and corrected signed amount formatting.

Summary

Copy BN magnitudes without aliasing their words and restore the sign. Preserve signed operands/results across arithmetic wrappers, clone, two's-complement conversion and extended arithmetic. Format the sign separately from decimal digits and return whole numbers when units=0. Includes a patch changeset.

Validation: four new regression tests fail on main; all 37 math tests pass with the fix. Math and dependency builds, math TypeScript check, ESLint and Prettier pass. Tested on Node 25.9.0 (outside the declared supported range).

Checklist

  • Changes covered by regression tests
  • Changes documented in a changeset
  • No API signature changes

AI assistance: implementation, review and tests prepared with Codex; no human review is claimed.

@fuel-cla-bot

fuel-cla-bot Bot commented Nov 18, 2026

Copy link
Copy Markdown

Thanks for the contribution! Before we can merge this, we need @yinbowen-dc to sign the Fuel Labs Contributor License Agreement.

@cursor

cursor Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Touches core BN construction and amount formatting used across the stack; fixes incorrect signs on negatives but changes behavior for code that relied on the buggy paths.

Overview
Fixes a sign-loss bug in @fuel-ts/math BN when values are copied or wrapped after arithmetic. Constructing from another BN now decodes via the existing array path but forces decimal/big-endian interpretation and re-applies negativity with ineg() when the source was negative, so clones/copies (bn(other), clone(), arithmetic helpers) keep the correct sign instead of treating magnitudes as positive.

Arithmetic wrappers (caller, sqr, neg, abs, two's-complement helpers, mulTo, egcd, divmod) now build results with new BN(superResult) (or decimal string for some low-level paths) rather than toArray(), preserving signs through the constructor.

Display: formatUnits and format now handle units === 0 as plain integers, format negative amounts with an explicit sign (including small fractional values), and treat -0 like 0 in precision trimming. A patch changeset and four regression tests cover negatives, isolated copies, extended arithmetic, and formatting.

Reviewed by Cursor Bugbot for commit 348ff3b. Bugbot is set up for automated code reviews on this repo. Configure here.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@fuel-ts/math: BN loses the sign when wrapping a BN (new BN(bn) uses toArray()), so arithmetic and comparisons with negatives are wrong

1 participant