margelo / react-native-bignumber

🔢 The fastest Big Number library for React Native
https://margelo.io
MIT License
342 stars 12 forks source link

Different output when using react-native-bignumber and bn.js #55

Closed kantorm closed 10 months ago

kantorm commented 1 year ago

I'm trying to use this package in a project which uses elliptic and one of the functions produce different output when using react-native-bignumber and bn.js. I managed to identify piece of code that returns different output. I tested it in the example app of react-native-bignumber. The code looks pretty much like this.

import bn_elliptic from 'bn_elliptic';
import elliptic from 'elliptic';

const secp256k1 = new elliptic.ec('secp256k1');
const bn_secp256k1 = new bn_elliptic.ec('secp256k1');

const kI =
  '9dc74cbfd383980fb4ae5d2680acddac9dac956dca65a28c80ac9c847c2374e4';
const n = secp256k1.curve.n;
const G = secp256k1.curve.g;
const Q = G.mul(kI);

const n_BN = bn_secp256k1.curve.n;
const G_BN = bn_secp256k1.curve.g;
const Q_BN = G_BN.mul(kI);

console.log(Q.x.umod(n));
// output: e696d0036454d7b7890bd425947329f68d9c7c0e2de44958e5700a30ca98b02c
console.log(Q_BN.x.umod(n_BN));
// output: 54c4a33c6423d689378f160a7ff8b61330444abb58fb470f96ea16d99d4a2fed

It looks like there are some differences in umod implementation but I might be wrong.

mrousavy commented 1 year ago

huh, that's weird. Is that something that's not covered by a BN.js test? We'll take a look....

awojciak commented 1 year ago

Hi @mrousavy , is there any progress on that issue? I'd like to use this library, but i want to be sure that there are no potentially costly bugs

PhungVanHoa commented 1 year ago

any news ?

mrousavy commented 1 year ago

we'll take a look

Szymon20000 commented 1 year ago

Hey! I started working on the issue. Seems like problem with umod indeed. However, I need to take a closer looks as Q.x looks to be a red number. I'm not sure what umod in a field would mean to be honest :D Will do my best to resolve it this week.

XantreDev commented 1 year ago

Same issue. Trying to use tronweb.fromMnemonic (uses elliptic under the hood).

Szymon20000 commented 10 months ago

Sorry guys! I was super busy in December and completely forgot about the issue. I found the issue and opened a pr with the fix.