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: ModInverse doesn't support negative numbers #16984

Closed
stefan-sakalik opened this issue Sep 3, 2016 · 3 comments
Closed

math/big: ModInverse doesn't support negative numbers #16984

stefan-sakalik opened this issue Sep 3, 2016 · 3 comments
Milestone

Comments

@stefan-sakalik
Copy link

What version of Go are you using (go version)?

go version go1.7rc3 linux/amd64

What operating system and processor architecture are you using (go env)?

amd64

What did you do?

I tried computing inverse modulo of a negative number using this snippet:

package main                                                                                                                                                              

import (                                                                                                                                                                  
        "fmt"                                                                                                                                                             
        "math/big"                                                                                                                                                        
)                                                                                                                                                                         

func main() {                                                                                                                                                             
        res := big.NewInt(0).ModInverse(big.NewInt(-10), big.NewInt(13)).Int64()                                                                                          
        fmt.Println(res)                                                                                                                                                  
}            

What did you expect to see?

9

What did you see instead?

0

@randall77
Copy link
Contributor

@griesemer

@ALTree
Copy link
Member

ALTree commented Sep 4, 2016

z.ModInverse(g, n) immediately calls GCD(z, nil, g, n) but GCD says that the 3rd and 4th parameters must be > 0 so with g = -10, GCD sets the receiver to 0 and returns. z.ModInverse(10, -13) fails for the same reason.

We could set g = g mod n before the call to GCD in ModInverse to fix the first, but then the result wouldn't match the buildin % on native types.

@dsnet dsnet changed the title math/big ModInverse doesn't support negative numbers math/big: ModInverse doesn't support negative numbers Sep 4, 2016
@quentinmit quentinmit added this to the Go1.8 milestone Sep 6, 2016
@gopherbot
Copy link

CL https://golang.org/cl/29299 mentions this issue.

@golang golang locked and limited conversation to collaborators Sep 27, 2017
@rsc rsc unassigned agl Jun 23, 2022
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

6 participants