ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
RSA.h File Reference

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. More...

#include "ADS/Contract.h"
#include "ADS/numeric/NumberTheory.h"
#include <cstdint>
#include <random>
#include <utility>
#include "ADS/numeric/impl/RSA.tpp"

Go to the source code of this file.

Classes

struct  ADS::RsaKey
 a key \( ( N , e ) \) More...

Namespaces

namespace  ADS

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 keys need some thousand bits.

Three random primes \( s \), \( x \) and \( y \), found by the test of Miller and Rabin, give \( N = x y \) and the public exponent \( p \) with \( p s \bmod ( x - 1 ) ( y - 1 ) = 1 \); the public key is \( ( N , p ) \), the private one \( ( N , s ) \), \( C = M^p \bmod N \) encrypts and \( M = C^s \bmod N \) decrypts, since \( M^{p s} \bmod N = M \). Signing is decrypting with the private key, checking is encrypting with the public one.

The contracts [see Contract.h]: randomPrime() needs \( 2 \le \mathit{bits} \le 63 \) and gives an odd number of exactly that many bits; the keys of rsaKeys() undo each other, checked on the message 2; rsa() needs a message \( M < N \). The functions are defined in RSA.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, section 17.3, https://algoritmicamente.it/materiali/libro/capitoli/cap17.pdf
R. L. Rivest, A. Shamir, L. Adleman, A method for obtaining digital signatures and public-key cryptosystems, Communications of the ACM 21(2), 120-126, 1978, doi:10.1145/359340.359342
G. L. Miller, Riemann's hypothesis and tests for primality, Journal of Computer and System Sciences 13(3), 300-317, 1976, doi:10.1016/S0022-0000(76)80043-8
M. O. Rabin, Probabilistic algorithm for testing primality, Journal of Number Theory 12(1), 128-138, 1980, doi:10.1016/0022-314X(80)90084-0
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa