|
ADS
Algorithms and Data Structures
|
The public key cryptosystem RSA of the reality check of section 17.3, on integers of 64 bits, a toy that shows the method and is not secure. More...
Files | |
| file | ADS/numeric/RSA.h |
| The public key cryptosystem RSA of the reality check of section 17.3, on integers of 64 bits, a toy that shows the method and is not secure: the keys need some thousand bits. | |
Classes | |
| struct | ADS::RsaKey |
| a key \( ( N , e ) \) More... | |
Functions | |
| template<class R> | |
| U64 | ADS::randomPrime (int bits, R &rng) |
| a random odd prime of exactly \( \mathit{bits} \) bits, by the test of Miller and Rabin on 40 bases | |
| template<class R> | |
| std::pair< RsaKey, RsaKey > | ADS::rsaKeys (R &rng) |
| a pair (public, private) of keys, with \( N \) of about 62 bits | |
| U64 | ADS::rsa (U64 M, const RsaKey &k) |
| the message \( M < N \) under the key, \( M^e \bmod N \): encrypting with a public key, or decrypting and signing with a private one | |
The public key cryptosystem RSA of the reality check of section 17.3, on integers of 64 bits, a toy that shows the method and is not secure.
The definitions, the invariants and the costs of the area are in Theory of numerical algorithms.
| U64 ADS::randomPrime | ( | int | bits, |
| R & | rng ) |
a random odd prime of exactly \( \mathit{bits} \) bits, by the test of Miller and Rabin on 40 bases
In pseudocode (section 17.3), a wrong answer with probability at most \( 4^{-40} \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{randomPrime}( \mathit{bits} ) \cr 2 & \quad \textbf{repeat}\ c \gets \text{an odd integer drawn in } [ 2^{\mathit{bits} - 1} , 2^{\mathit{bits}} )\ \textbf{until} \ \mathrm{millerRabin}( c , 40 ) \cr 3 & \quad \textbf{return}\ c \end{array}} \]
the message \( M < N \) under the key, \( M^e \bmod N \): encrypting with a public key, or decrypting and signing with a private one
In pseudocode (section 17.3):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{rsa}( M , ( N , e ) ) \cr 2 & \quad \textbf{return}\ \mathrm{powMod}( M , e , N ) \end{array}} \]
a pair (public, private) of keys, with \( N \) of about 62 bits
In pseudocode (section 17.3), the private exponent a prime \( s \) that does not divide \( \varphi = ( x - 1 ) ( y - 1 ) \), hence prime to it:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{rsaKeys}()\quad \triangleright\ ( \text{secret} , \text{public} ) \cr 2 & \quad \textbf{repeat} \cr 3 & \qquad x , y \gets \text{two primes of 31 bits};\ s \gets \text{a prime of 24 bits};\ \varphi \gets ( x - 1 ) ( y - 1 ) \cr 4 & \quad \textbf{until}\ x \ne y\ \textbf{and}\ s \nmid \varphi\quad \triangleright\ s \text{ prime: invertible modulo } \varphi \cr 5 & \quad \textbf{return}\ ( ( x y , \mathrm{modInverse}( s , \varphi ) ) , ( x y , s ) ) \end{array}} \]