Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Are BN.mont and BN.red interchangable? #193

Open
mattdean-digicatapult opened this issue Jul 16, 2018 · 1 comment
Open

Are BN.mont and BN.red interchangable? #193

mattdean-digicatapult opened this issue Jul 16, 2018 · 1 comment

Comments

@mattdean-digicatapult
Copy link

I'm having some issues using BN.mont in a project I'm working on, whereby under certain conditions it gives me different answers compared to when I use BN.red (documentation implies they're interchangable?). For example:

const BN = require('bn.js')

const a = new BN(0)
const b = new BN(0)
const m = new BN(100)

const redRed = BN.red(m)
const redMont = BN.mont(m)

const resMont = a.toRed(redMont).redPow(b).fromRed()
const resRed = a.toRed(redRed).redPow(b).fromRed()

console.log(resMont, resRed)

which outputs <BN: 18> <BN: 1>

I don't know if this is just a documentation issue (for example maybe BN.mont only produces the correct answer under certain undocumented conditions?) or if this is a genuine bug. It may also be that I've completely misunderstood how this works 😜

@s1na
Copy link

s1na commented Mar 11, 2019

I faced the same issue here. As @cdetrio pointed out, BN.mont is choosing r as a power of 2, which means the modulus should be odd and greater than 3 to produce correct results.

In other words (if I haven't misunderstood), if modulus n is even, because r is a power of 2, gcd(r, n) != 1, which is a condition for the Montgomery multiplication.

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

No branches or pull requests

2 participants