Skip to content

Conversation

@alexdupre
Copy link
Contributor

In some circumstances the hex encoding of big numbers is wrong. In addition to a display issue, given that the the hex string if often used as an intermediate representation in transport/conversion scenarios, the re-constructed big number can actually change its value, creating serious issues.

alexdupre added 2 commits May 16, 2022 21:11
The hex encoding of some numbers is wrong. Since the string representation
is also used for interoperability between BigInt libraries, this bug is
causing a number modification when a BN.js number is converted into
another BigInt class, for example the BigNumber of ethers.js.
@ricmoo
Copy link

ricmoo commented May 17, 2022

FYI, I have confirmed this issue.

@indutny Would you have an idea of when you would be able to get to this? I am debating including a hack to fix this temporarily into ethers if you don’t have time to get to it. :)

@fanatid
Copy link
Collaborator

fanatid commented May 18, 2022

@alexdupre thanks for the fix. Can you explain what's wrong with current code and how moving if-block will fix it? Test is a good but it would be great to have explanation for the future.

@alexdupre
Copy link
Contributor Author

The issue is present when these conditions are true:

  1. the code is processing the last word
  2. carry is 0
  3. off is 24 at the start of the iteration

In this case the if doesn't add the full 0-padded 6 characters, because it thinks it's the last iteration, but actually because of the off increment (and consequentially the i decrement) this is the second last iteration, and the full 6 chars have to be added. Does it make sense?

@fanatid
Copy link
Collaborator

fanatid commented May 18, 2022

Make sense, thanks.
I wanted release this as patch but in the same time changes can break something, maybe we should proceed with minor? Any thoughts?

@alexdupre
Copy link
Contributor Author

alexdupre commented May 18, 2022

If you are 100% sure that the fix is correct, then I think it should be released as patch, this is a serious issue and there cannot be external code that relies on such broken behavior. Personally I don't think this fix can break anything.

@fanatid
Copy link
Collaborator

fanatid commented May 18, 2022

I think changes are correct but it would be great if somebody except @ricmoo confirmed that everything is correct.

@alexdupre
Copy link
Contributor Author

FWIW I think this may be a good candidate for a property-based testing methodology (it'd be interesting to know if it would have spotted this issue as well).

@alexdupre
Copy link
Contributor Author

Any update?

@fanatid fanatid merged commit 042ab62 into indutny:master May 24, 2022
@fanatid
Copy link
Collaborator

fanatid commented May 24, 2022

Thank you for the reminder! Published as v5.2.1.

@ricmoo
Copy link

ricmoo commented May 24, 2022

@fanatid Could I bug you to also get the version in elliptic bumped to match this too, please? :)

@fanatid
Copy link
Collaborator

fanatid commented May 24, 2022

Only @indutny can do this :/

legobeat added a commit to legobeat/ethjs-abi that referenced this pull request Jan 10, 2023
legobeat added a commit to legobeat/number-to-bn that referenced this pull request Jan 10, 2023
legobeat added a commit to legobt/ethjs-abi that referenced this pull request Jan 10, 2023
@ChALkeR
Copy link

ChALkeR commented Nov 8, 2024

Could this be backported to v4?

@rawmet1989
Copy link

rawmet1989 commented Nov 8, 2024 via email

ChALkeR pushed a commit to ChALkeR/bn.js that referenced this pull request Nov 11, 2024
* Add test for `.toString(16)`

* Fix serious issue in `.toString(16)`

The hex encoding of some numbers is wrong. Since the string representation
is also used for interoperability between BigInt libraries, this bug is
causing a number modification when a BN.js number is converted into
another BigInt class, for example the BigNumber of ethers.js.
@fanatid
Copy link
Collaborator

fanatid commented Nov 11, 2024

published as 4.12.1, thanks @ChALkeR!

@rawmet1989
Copy link

Hey, so what are you trying to say?

github-merge-queue bot pushed a commit to MetaMask/metamask-mobile that referenced this pull request Mar 13, 2025
## **Description**

- Unpin and dedupe `ethereumjs-util` and `ethereumjs-abi` while staying
on legacy v6
- Replace `import { BN } from 'ethereumjs-util'` with importing `BN`
explicitly from either `bn.js@4` or `bn.js@5`
  - Mixing the two is not safe and can cause breakage.
- [Some on why this is A Bad
Thing](ethereumjs/ethereumjs-util#250)
- We don't get complete and correct typings with importing the old
re-export
- This adds both v4 and v5 as explicit dependencies - ideally v4 will be
removed as transition to v5 concludes
- Add EIP-1191 address checksum algorithm support for
`toChecksumAddress()`
- Unblock relying on the ancient and deprecated `[email protected]`
  
- [`ethereumjs-util`
changelog](https://github.com/ethereumjs/ethereumjs-util/blob/master/CHANGELOG.md)
-
[`[email protected]`](ethereumjs/ethereumjs-util@v6.1.0...v6.2.0)

## **Related issues**

- indutny/bn.js#209
- indutny/bn.js#295
- #12010

#### Blocking
- #11932
  - #12043
  - #11968

## **Manual testing steps**


## **Screenshots/Recordings**

### **Before**

### **After**

## **Pre-merge author checklist**

- [x] I’ve followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile
Coding
Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I’ve included tests if applicable
- [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.

---------

Co-authored-by: Nicolas MASSART <[email protected]>
Co-authored-by: Nico MASSART <[email protected]>
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.

5 participants