Botan 3.7.1
Crypto and TLS for C&
mod_inv.h
Go to the documentation of this file.
1/*
2* (C) 2025 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#ifndef BOTAN_MOD_INV_H_
8#define BOTAN_MOD_INV_H_
9
10#include <botan/bigint.h>
11#include <optional>
12
13namespace Botan {
14
15/**
16* Compute the inverse of x modulo some integer m
17*
18* Returns nullopt if no such integer exists eg if gcd(x, m) > 1
19*
20* This algorithm is const time with respect to x, aside from its
21* length. It also avoids leaking information about the modulus m,
22* except that it does leak which of 3 categories the modulus is in:
23*
24* - An odd integer
25* - A power of 2
26* - Some even number not a power of 2
27*
28* And if the modulus is even, it leaks the power of 2 which divides
29* the modulus.
30*
31* @param x a positive integer less than m
32* @param m a positive integer
33*
34* Throws Invalid_Argument if x or m are negative
35*/
36std::optional<BigInt> BOTAN_TEST_API inverse_mod_general(const BigInt& x, const BigInt& m);
37
38/**
39* Compute the inverse of x modulo a secret prime p
40*
41* This algorithm is constant time with respect to x and p, aside from
42* leaking the length of p. (In particular it should not leak the
43* length of x, if x is shorter)
44*
45* @param x a positive integer less than p
46* @param p an odd prime
47* @return y such that (x*y) % p == 1
48*
49* This always returns a result since any integer in [1,p)
50* has an inverse modulo a prime p.
51*
52* This function assumes as a precondition that p truly is prime; the
53* results may not be correct if this does not hold.
54*
55* Throws Invalid_Argument if x is less than or equal to zero,
56* or if p is even or less than 3.
57*/
58BigInt BOTAN_TEST_API inverse_mod_secret_prime(const BigInt& x, const BigInt& p);
59
60/**
61* Compute the inverse of x modulo a public prime p
62*
63* This algorithm is constant time with respect to x. The prime
64* p is assumed to be public.
65*
66* @param x a positive integer less than p
67* @param p an odd prime
68* @return y such that (x*y) % p == 1
69*
70* This always returns a result since any integer in [1,p)
71* has an inverse modulo a prime p.
72*
73* This function assumes as a precondition that p truly is prime; the
74* results may not be correct if this does not hold.
75*
76* Throws Invalid_Argument if x is less than or equal to zero,
77* or if p is even or less than 3.
78*/
79BigInt BOTAN_TEST_API inverse_mod_public_prime(const BigInt& x, const BigInt& p);
80
81/**
82* Compute the inverse of x modulo a public RSA modulus n
83*
84* This algorithm is constant time with respect to x. The RSA
85* modulus is assumed to be public.
86*
87* @param x a positive integer less than n
88* @param n a RSA public modulus
89* @return y such that (x*y) % n == 1
90*
91* This always returns a result since any integer in [1,n) has an inverse modulo
92* a RSA public modulus n, unless you have happened to guess one of the factors
93* at random. In the unlikely event of this occuring, Internal_Error will be thrown.
94*/
95BigInt inverse_mod_rsa_public_modulus(const BigInt& x, const BigInt& n);
96
97/**
98* Compute the RSA private exponent d
99*
100* This algorithm is constant time with respect to phi_n, p, and q,
101* aside from leaking their lengths. It may leak the public exponent e.
102*
103* @param e the public exponent
104* @param phi_n is lcm(p-1, q-1)
105* @param p is the first secret prime
106* @param q is the second secret prime
107* @return d inverse of e modulo phi_n
108*/
109BigInt BOTAN_TEST_API compute_rsa_secret_exponent(const BigInt& e,
110 const BigInt& phi_n,
111 const BigInt& p,
112 const BigInt& q);
113
114} // namespace Botan
115
116#endif
#define BOTAN_TEST_API
Definition api.h:39
BigInt inverse_mod_secret_prime(const BigInt &x, const BigInt &p)
Definition mod_inv.cpp:280
BigInt inverse_mod_public_prime(const BigInt &x, const BigInt &p)
Definition mod_inv.cpp:291
BigInt compute_rsa_secret_exponent(const BigInt &e, const BigInt &phi_n, const BigInt &p, const BigInt &q)
Definition mod_inv.cpp:330
std::optional< BigInt > inverse_mod_general(const BigInt &x, const BigInt &mod)
Definition mod_inv.cpp:177
BigInt inverse_mod_rsa_public_modulus(const BigInt &x, const BigInt &n)
Definition mod_inv.cpp:295