Skip to content

ACP: isqrt for NonZero<uN> #391

Closed
Closed

Description

@ivan-shrimp

Proposal

Problem statement

Integer primitives now have an integer square root method isqrt. Positive integers always have a positive square root, and it would be natural to directly express this through NonZero.

Motivating examples or use cases

x.pow(2).isqrt() gives x for x: uN, and we can extend that to NonZero<uN>.

Solution sketch

// N = 8/16/32/64/128/size
impl NonZero<uN> {
    const fn isqrt(self) -> Self {
        // should behave as if:
        unsafe { NonZero::new_unchecked(self.get().isqrt()) }
    }
}

Alternatives

  • Do nothing, and require users to use the NonZero constructors manually when necessary. This is either unsafe or requires .unwrap(), which is not ideal.
  • Halve the size of the returned integer. However, this would be inconsistent with the primitive counterparts and violate the round-tripping property as mentioned above.

Links and related work

Motivation for primitive isqrt: rust-lang/rust#89273
Tracking issue for isqrt: rust-lang/rust#116226
Suggestion for this function: rust-lang/rust#116226 (comment)
Implementation: rust-lang/rust#126199

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

  • We think this problem seems worth solving, and the standard library might be the right place to solve it.
  • We think that this probably doesn't belong in the standard library.

Second, if there's a concrete solution:

  • We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
  • We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.

Activity

  1. kennytm commented on Jun 10, 2024

    @kennytm
    Member

    My motivation suggesting NonZero<uN>::isqrt() in rust-lang/rust#116226 (comment) was that, given a natural number $N &gt; 0$, compute the "middle" value $M = \lfloor\sqrt N\rfloor &gt; 0$ to decompose every number $i \in \mathbb Z_N$ into $(a,b) \in \mathbb Z_{\lceil N/M\rceil} \times \mathbb Z_M$ with $i = aM+b$. The $(a,b)$ pair is then feed through a Feistel network in order to perform something like format-preserving encryption.

    With N: NonZero<u64> we can use NonZero::isqrt() to get M: NonZero<u64> so that (a, b) = (i / M, i % M) are panic-free.

  2. scottmcm commented on Jun 10, 2024

    @scottmcm
    Member

    (Disclaimer: I'm not libs-api) This seems entirely reasonable to me, under the same "it's helpful compared to .get().isqrt()" that gave us NonZero::count_ones and NonZero::ilog2.

  3. pitaj commented on Jun 10, 2024

    @pitaj

    For the future, the following operations could have the same function signature fn(NonZero<T>) -> NonZero<T>

    • reverse_bits
    • rotate_left
    • rotate_right
    • swap_bytes
    • to_be
    • to_le
  4. the8472 commented on Jun 11, 2024

    @the8472
    Member

    We discussed this during today's libs-api meeting and adding isqrt was accepted.
    Additional methods should go in a separate ACP.

  5. added
    ACP-acceptedAPI Change Proposal is accepted (seconded with no objections)
    on Jun 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ACP-acceptedAPI Change Proposal is accepted (seconded with no objections)api-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions