margelo / react-native-bignumber

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

fix: umod doesn't support red numbers #62

Closed Szymon20000 closed 10 months ago

Szymon20000 commented 10 months ago

bn.js internally keeps red numbers as big numbers modulo certain n. So calling umod on red number in bn.js is doing nothing as the number is already smaller than n. However, in our library we store red numbers in [montgomery form]https://en.wikipedia.org/wiki/Montgomery_modular_multiplication to speed up multiply and power operations. We didn't expect anyone to call umod on a red number as the method is not marked as red. This pr fixes that by changing the result number in the red number what solves the problem.

An alternative approach would be to just make umod noop for red numbers as they are already less than module (because they are red numbers). However, I can think of some examples when we want to check number parity or few optimisations that relay on umod to avoid expensive redDiv calls.

Fixes: https://github.com/margelo/react-native-bignumber/issues/55