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

When computing modular inverses distingush which case we are in #4569

Merged
merged 2 commits into from
Jan 20, 2025

Conversation

randombit
Copy link
Owner

There are several different scenarios where we need to compute a modular inverse

  • A secret value modulo a secret prime (eg RSA-CRT setup)
  • A (potentially) secret value modulo a public prime (eg inversion modulo an DL group order)
  • A secret value modulo a public value that is not prime (RSA blinding setup)
  • RSA secret exponent calculation, where e and phi(n) are relatively prime but phi(n) is not prime, and e is public
  • The general case where we have no idea as to the structure of the modulus, and we don't know if it is public so must treat it as secret

Previously all of these went through inverse_mod which prevented any possible optimizations based on cases.

Add a new (internal) header which directly exposes the various cases and apply them within the codebase.

The only new algorithm implemented (so far) is Arazi's algorithm for inversion of a prime modulo a non-prime. (TIL) Specifically this is a special case for computing the RSA secret exponent when e=65537. (Would also work for e=3/e=17/etc but this is not implemented)

@randombit randombit requested a review from reneme January 18, 2025 16:59
@randombit randombit force-pushed the jack/mod-inv-distinguish-cases branch from dbf8c3f to 541bdbc Compare January 18, 2025 17:01
@randombit randombit force-pushed the jack/mod-inv-distinguish-cases branch 2 times, most recently from fe8e981 to b3da780 Compare January 18, 2025 18:09
@coveralls
Copy link

coveralls commented Jan 18, 2025

Coverage Status

coverage: 91.21% (+0.008%) from 91.202%
when pulling 77338d0 on jack/mod-inv-distinguish-cases
into 35d58e2 on master.

@randombit randombit force-pushed the jack/mod-inv-distinguish-cases branch 5 times, most recently from 4800c83 to 120fe32 Compare January 19, 2025 00:14
Copy link
Collaborator

@reneme reneme left a comment

Choose a reason for hiding this comment

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

Actual change makes sense to us (/cc @FAlbertDev). A few minor style suggestions below.

*/

#ifndef BOTAN_MOD_INV_H_
#define BOTAN_MOD_INV_H_
Copy link
Collaborator

Choose a reason for hiding this comment

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

Proper nit: modular_inverse.h as the name of this file? More typing, but easier to read and more accessible by beginners. Totally fine with just leaving it as is, though.

src/lib/math/numbertheory/mod_inv.cpp Outdated Show resolved Hide resolved
src/tests/test_bigint.cpp Outdated Show resolved Hide resolved
src/tests/tests.cpp Outdated Show resolved Hide resolved
@@ -744,6 +752,58 @@ class Lucas_Primality_Test final : public Test {

BOTAN_REGISTER_TEST("math", "bn_lucas", Lucas_Primality_Test);

class RSA_Compute_Exp_Test : public Test {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is quite some Monte Carlo test. No general objection to that, but wouldn't it make sense to implement that as part of the fuzzing somehow?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Likely yes. In fact we should do this for all of our (many) Monte Carlo tests. And likewise most of the fuzzers we have should also be tests that run as part of the regular test suite. Some of them do already exist in both systems, but only a few. I'll create a ticket describing the situation.

Copy link
Owner Author

Choose a reason for hiding this comment

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

src/lib/math/numbertheory/mod_inv.cpp Outdated Show resolved Hide resolved
@randombit randombit force-pushed the jack/mod-inv-distinguish-cases branch from 120fe32 to 77338d0 Compare January 20, 2025 12:18
There are several different scenarios where we need to compute a modular inverse

* A secret value modulo a secret prime (eg RSA-CRT setup)

* A (potentially) secret value modulo a public prime (eg inversion modulo an DL group order)

* A secret value modulo a public value that is not prime (RSA blinding setup)

* RSA secret exponent calculation, where e and phi(n) are relatively
  prime, phi(n) is secret and not prime, and e is public

* The general case where we have no idea as to the structure of the
  modulus, and we don't know if it is public so must treat it as secret

Previously all of these went through `inverse_mod` which prevented any possible
optimizations based on cases.

Add a new (internal) header which directly exposes the various cases and apply
them within the codebase.

The only new algorithm implemented (so far) is Arazi's algorithm for inversion
of a prime modulo a non-prime. Specifically this is a special case for computing
the RSA secret exponent when `e=65537`. (Would also work for `e=3`/`e=17`/etc but
this is not implemented)
@randombit randombit merged commit 9eae7c1 into master Jan 20, 2025
39 checks passed
@randombit randombit deleted the jack/mod-inv-distinguish-cases branch January 20, 2025 13:40
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.

3 participants