Using BigInt in React Native

Viewed 417

I want to use a crypto library sign and verify a signature with Ed25519. When I use https://github.com/paulmillr/noble-ed25519, it fails because of a BigInt issue

Unhandled Rejection (TypeError): can't convert BigInt to number

BigInt Issue

I don't understand where it fails, can someone please explain me the BigInt behaviour in React Native ?

1 Answers

It fail at BigInt(X1) ** BigInt(2).

React Native transform this expression to something like Math.pow(BigInt(X1), BigInt(2)), in which Math.pow(number, number): number only accepts numbers as inputs.

You could wrap Math.pow to make the ** syntax works again:

function bigIntPow() {/* Your implementation */}

function makePow(pow) {
  return function() {
    if (typeof arguments === 'number') {
      return pow.apply(this, arguments);
    }
    return bigIntPow.apply(this, arguments);
  };
}

Math.pow = makePow(Math.pow);
console.log(3n ** 4n); // 81n
Related