Skip to content

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

Open

Description

@FiloSottile

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 dereference 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.

Activity

  1. added
    NeedsFixThe path to resolution is known, but the work has not been done.
    on Aug 9, 2021
  2. added this to the Go1.18 milestone on Aug 9, 2021
  3. gunadhya commented on Aug 10, 2021

    @gunadhya

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

  4. gopherbot commented on Aug 15, 2021

    @gopherbot
    Contributor

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

  5. odeke-em commented on Dec 20, 2021

    @odeke-em
    Member

    Thank you for filing this issue @FiloSottile, and thank you @ok-john for sending the CL https://go-review.googlesource.com/c/go/+/342211/

    This issue is marked for Go1.18, but the CL was marked with "Wait-Release". Shall we roll this over to Go1.19 or should we make the breaking change to return nil if the modulus is zero. Now one reservation here is that the internal code has a comment that its internal implementation of GCD expects the parameters to be > 0. Should we perhaps instead document this issue or just wait until Go1.19. What do you think @griesemer @ianlancetaylor?

  6. odeke-em commented on Jan 6, 2022

    @odeke-em
    Member

    Alright, I shall make the call to move this to the Go1.19 milestone as per the CL labelling and given that it is quite late in the cycle for a breakiing change.

  7. modified the milestones: Go1.18, Go1.19 on Jan 6, 2022
  8. ianlancetaylor commented on Jun 24, 2022

    @ianlancetaylor
    Contributor

    CC @golang/security

    Looks like this didn't make 1.19. Moving to backlog. Please recategorize as appropriate.

  9. modified the milestones: Go1.19, Backlog on Jun 24, 2022
  10. modified the milestones: Backlog, Go1.20 on Jul 18, 2022
  11. 1 remaining item

  12. added this to the Go1.21 milestone on Feb 1, 2023
  13. modified the milestones: Go1.21, Go1.22 on Aug 8, 2023
  14. modified the milestones: Go1.22, Go1.23 on Feb 5, 2024
  15. modified the milestones: Go1.23, Go1.24 on Aug 13, 2024
  16. modified the milestones: Go1.24, Go1.25 on Feb 11, 2025
  17. modified the milestones: Go1.25, Go1.26 on Aug 12, 2025
  18. modified the milestones: Go1.26, Go1.27 on Feb 10, 2026
  19. modified the milestones: Go1.27, Go1.28 on Aug 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

NeedsFixThe path to resolution is known, but the work has not been done.

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions