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

math/big: Exp function returns puzzling results when exponent = 1 #9826

Closed
royalfork opened this issue Feb 10, 2015 · 1 comment
Closed

math/big: Exp function returns puzzling results when exponent = 1 #9826

royalfork opened this issue Feb 10, 2015 · 1 comment
Milestone

Comments

@royalfork
Copy link

I'm using Go 1.4 on 64 bit Debian.

I run the following:
import "math/big"
a := new(big.Int).Exp(big.NewInt(5), big.NewInt(1), big.NewInt(3))

I expect 'a' to be 2, because 5**1 % 3 = 5 % 3 = 2. Instead, Go gives a = 5.

Code can be found here: http://play.golang.org/p/TCWWiXXZhf

@mikioh mikioh changed the title math/big Exp function returns puzzling results when exponent = 1 math/big: Exp function returns puzzling results when exponent = 1 Feb 10, 2015
@griesemer griesemer self-assigned this Feb 10, 2015
@ALTree
Copy link
Member

ALTree commented Apr 6, 2015

In nat.go expNN (here), when y is 1, v is set to 1 (here) and shift on the next line to 64. Then on the line

w := _W - int(shift)

w is set to 0 (64 - 64), so the code inside the loop on line 921 is never ran. Since z (the result) was set to x (on line 895) and never changed the function returns x (5 in the example) instead of x mod m.

Patch: https://go-review.googlesource.com/#/c/8409

@mikioh mikioh added this to the Go1.5 milestone Apr 8, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants