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 with modulus zero is undefined #47606

Open
FiloSottile opened this issue Aug 9, 2021 · 2 comments · May be fixed by #47706
Open

math/big: ModInverse with modulus zero is undefined #47606

FiloSottile opened this issue Aug 9, 2021 · 2 comments · May be fixed by #47706
Labels
Milestone

Comments

@FiloSottile
Copy link
Contributor

@FiloSottile FiloSottile commented Aug 9, 2021

ModInverse is defined for all values of g, and for positive and negative values n, but not for a zero n.

ModInverse sets z to the multiplicative inverse of g in the ring ℤ/nℤ
and returns z. If g and n are not relatively prime, g has no multiplicative
inverse in the ring ℤ/nℤ. In this case, z is unchanged and the return value
is nil.

The ring of numbers modulo zero does not exist, and being coprime with zero is meaningless, so the current docs don't cover it.

Arguably, ModInverse should always panic when the modulo is zero, but currently it returns nil for g > 1, returns 1 for g = 1, returns nil for g = 0, and panics for g < 0. Turning a non-panic into a panic for the sake of consistency is asking for trouble, so we should just make it return nil consistently for n = 0.

This could break programs that try to dereverence the return value of ModInverse(1, 0), but that feels like a specific enough condition that programs won't have come to rely on it #famouslastwords.

@FiloSottile FiloSottile added this to the Go1.18 milestone Aug 9, 2021
@gunadhya
Copy link

@gunadhya gunadhya commented Aug 10, 2021

Hi @FiloSottile, I'd like to work on this issue.

ok-john added a commit to ca-std/go that referenced this issue Aug 15, 2021
ModInverse now explicitly returns nil should n = 0. Prior to this we would compute
g mod n and the GCD  which is pointless should n == 0.
ok-john added a commit to ca-std/go that referenced this issue Aug 15, 2021
math/big: ModInverse explicitly prevent g mod (n=0)  golang#47606
ok-john added a commit to ca-std/go that referenced this issue Aug 15, 2021
Fixes golang#47606

This patch adds a check for n = 0 and makes the behavior of
n = 0 explicit by immediately returning nil.
ok-john added a commit to ca-std/go that referenced this issue Aug 15, 2021
Fixes golang#47606
1) Added an explict check for n = 0 and if true return nil.
2) Updated out-dated documentation

Since we compute everything starting from mod n here it's completely useless to start any computation should n == 0.
@ok-john ok-john linked a pull request that will close this issue Aug 15, 2021
@gopherbot
Copy link

@gopherbot gopherbot commented Aug 15, 2021

Change https://golang.org/cl/342211 mentions this issue: big/int: Explicit checks in ModInverse for g modulo n=0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

3 participants