ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches

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

Detailed description

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.

Function documentation

◆ randomPrime()

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

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}} \]

Precondition
\( 2 \le \mathit{bits} \le 63 \)
Postcondition
an odd number of exactly \( \mathit{bits} \) bits

◆ rsa()

U64 ADS::rsa ( U64 M,
const RsaKey & k )
inline

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}} \]

Precondition
\( M < N \)

◆ rsaKeys()

template<class R>
std::pair< RsaKey, RsaKey > ADS::rsaKeys ( R & rng)

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}} \]

Postcondition
each key undoes the other, checked on the message 2