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

Replace mp_mod_inv with recip #233

Merged
merged 2 commits into from
Jan 30, 2025
Merged

Conversation

RyanGlScott
Copy link
Contributor

Closes #100.

mp_mod_inv has an incorrect type signature, as it is only valid when the modulus is prime, but the type does not reflect this. Moreover, there is now a correct version of this in the Cryptol prelude in the form of recip : {p} (prime p) => Z p -> Z p, so there is really no need to define mp_mod_inv separately anymore. Let's just remove mp_mod_inv.

The only real call site for mp_mod_inv was in Common::EC::PrimeField::PFEC, but it is easily swapped out for recip.

This replaces the `mp_mod_inv` function with `recip`. The latter uses the
`Field` instance for `Z p` and is thus safer than `mp_mod_inv`, as it requires
that `p` is prime.

This is the only real call site of `mp_mod_inv`, so we can remove `mp_mod_inv`
in a follow-up commit.
`mp_mod_inv` has an incorrect type signature, as it is only valid when the
modulus is prime, but the type does not reflect this. Moreover, there is now a
correct version of this in the Cryptol prelude in the form of `recip : {p}
(prime p) => Z p -> Z p`, so there is really no need to define `mp_mod_inv`
separately anymore. Let's just remove `mp_mod_inv`.
@RyanGlScott RyanGlScott requested a review from marsella January 30, 2025 15:25
Copy link
Contributor

@marsella marsella left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, thank you! I peeked into the history and this used to be used in some of the old ECDSA and EC instances that were deleted in #113 and #174. Happy to have it gone.

@weaversa
Copy link
Contributor

When I wrote that function 7 years ago (!) we didn't have a prime type constraint. I'm glad to see it is no longer needed.

@RyanGlScott RyanGlScott merged commit 83066c8 into master Jan 30, 2025
2 checks passed
@RyanGlScott RyanGlScott deleted the 100-replace-mp_mod_inv-with-recip branch January 30, 2025 15:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add prime constraint to inverse function
3 participants