A short monograph (in Italian, typeset with LaTeX in the Tufte book style) that walks through the RSA cryptosystem, its most common elementary attacks, and the continued-fraction attack on small private exponents published by Michael J. Wiener in 1990. It ends with a compact implementation of the classic and the extended attack in Wolfram Mathematica — Read the paper.
Is mathematics "useful", directly useful, as other sciences such as chemistry and physiology are? This is not an altogether easy or uncontroversial question, and I shall ultimately say No… — G.H. Hardy, A Mathematician's Apology, 1940
The paper opens with Hardy's famous claim that number theory is "useless", and then shows how that same theory became the foundation of computational number theory and asymmetric cryptography.
.
├── README.md
├── LICENSE # GPL-3.0
├── assets/
│ ├── snoopy.gif
│ └── code/
│ ├── classic.wl # classic Wiener attack (wolframscript)
│ └── extended.wl # Verheul–van Tilborg extension (wolframscript)
└── src/
├── wiener_attack.pdf # the compiled paper <- start here
├── main.tex # LaTeX source of the paper
├── bibliography.bib # Tufte template leftover (not used by the paper)
├── tufte-book.cls # document class
├── tufte-common.def
├── *.jpg / *.png / *.pdf # figures (Escher, Tenniel's Alice, Peanuts, COPACOBANA, …)
├── graphics/ … # Tufte template graphics
├── book_1_template.* # Tufte book template
└── template/ … # original Tufte-book template
The repository is mostly the paper itself: the code lives in its Appendix, and is also provided as the standalone scripts in assets/code/.
flowchart LR
A[Symmetric vs<br/>asymmetric crypto] --> B[RSA<br/>cryptosystem]
B --> C[Elementary attacks]
C --> C1["Computing φ(N)"]
C --> C2[e-th root]
C --> C3[Common modulus]
C --> C4[Fixed point]
B --> D[Wiener attack]
D --> D1[Continued fractions<br/>& convergents]
D1 --> D2[Legendre's theorem]
D2 --> D3[Classic attack]
D3 --> D4[Efficiency &<br/>countermeasures]
D3 --> D5[Extended attack]
D3 --> E[Mathematica<br/>implementation]
D5 --> E
| Chapter | Section | Topics |
|---|---|---|
| Towards RSA | Introduction | Symmetric ciphers and their limits (key distribution, |
| The RSA cryptosystem | Euler's |
|
| Elementary attacks | Computing |
|
| Wiener's attack | The role of convergents | Regular continued fractions, recursive computation of convergents, monotonicity lemmas, best rational approximations, Legendre's theorem |
| Classic attack | Wiener's theorem and proof, search algorithm, |
|
| Efficiency & countermeasures | Balanced primes, distribution of |
|
| Extended attack | Verheul–van Tilborg brute-force extension, Dujella's improvement | |
| Appendix | Mathematica implementation of both attacks |
Throughout the paper the usual cast is used: Alice (sender), Bob (receiver) and Oscar (the cryptanalyst).
Bob picks two large primes
Correctness follows from the Euler–Fermat theorem (
While inverting
---
config:
sequence:
actorMargin: 120
diagramMarginX: 80
---
sequenceDiagram
participant B as Bob
participant A as Alice
participant O as Oscar
B->>B: choose p, q · N = pq<br/>choose e · d = e⁻¹ mod λ(N)
B->>A: publish (e, N)
O-->>O: also sees (e, N)
A->>B: c = mᵉ mod N
O-->>O: intercepts c
B->>B: m = cᵈ mod N
The paper focuses on indirect algorithmic attacks, which exploit mathematical weaknesses or misuse of the system rather than attacking factorization head-on.
| Attack | Idea | Result |
|---|---|---|
| Computing |
Since |
|
| Knowing |
||
| Common modulus | Same |
|
| Fixed point |
|
if |
The first row is the key fact reused by Wiener's attack
Two open problems are stated along the way: whether computing
Small private exponents are tempting because they make decryption fast (e.g. on smart cards). Wiener showed that if
A regular continued fraction is written
The paper proves that:
- every convergent is already in lowest terms, since
$a_{i-1}b_i - a_ib_{i-1} = (-1)^i$ ; - even convergents increase strictly, odd ones decrease strictly, and
$\gamma$ lies between them; - each convergent
$a_n/b_n$ approximates$\gamma$ better than any other fraction whose denominator is at most$b_n$ ; -
Legendre's theorem: if a fraction is "close enough" to
$\gamma$ , it must be one of its convergents:
The expansion of a rational
Theorem (Wiener). Let
then
Proof sketch. From
which is exactly the hypothesis of Legendre's theorem. Therefore
flowchart TD
S(["Public key (e, N)"]) --> CF["Expand e/N as a continued fraction<br/>and list its convergents c₁ … cₙ"]
CF --> M["m <- 0"]
M --> I["take the next<br/>convergent cᵢ"]
I --> PHI["candidate<br/>φ̃ = ⌊e / cᵢ⌋ − m"]
PHI --> Q["solve<br/>x² − (N − φ̃ + 1)x + N = 0"]
Q --> T{"integer roots p, q<br/>with pq = N?"}
T -- yes --> OK(["N factored -> d = e⁻¹ mod φ(N)"])
T -- no --> MORE{"more convergents?"}
MORE -- yes --> I
MORE -- no --> INC["m <- m + 1,<br/>restart from c₁<br/>(m never exceeds ⌊g/k⌋)"]
INC --> I
There are
- The right convergent has denominator
$dg_0$ , so any convergent$c_i = a_i/b_i$ with$\lvert e/N - c_i\rvert \ge 1/(2b_i^2)$ can be discarded a priori. - With balanced primes (
$p < q < 2p$ ) we have$\lvert N - \phi(N)\rvert = \lvert p+q-1\rvert < 3\sqrt N$ , so$N$ and$\phi(N)$ share about half of their most significant bits. - For random balanced primes,
$g = \gcd(p-1,q-1)$ is usually tiny. Experiments on 128–1024-bit primes in the paper give$\Pr[g \le 6] \approx 0.77$ and$\Pr[g \le 20] \approx 0.91$ . In practice$\lfloor g/k \rfloor = 0$ , and a single pass ($m=0$ ) over the convergents is enough.
Worked example (from the paper). With
The convergent
Under the usual assumptions (
So with a 2048-bit modulus,
- use unbalanced primes to make
$N - \phi(N)$ larger; - pick
$p, q$ with a large$g$ (as in Common Prime RSA); - use a large public exponent
$e > N$ , obtained by adding a multiple of$\lambda(N)$ to$e$ ; for$e > N^{3/2}$ the attack is no longer guaranteed to succeed; - decrypt with the CRT, using small
$d_p \equiv d \pmod{p-1}$ and$d_q \equiv d \pmod{q-1}$ while$d$ itself stays of order$\phi(N)$ .
Setting
where
If
where
With a 64-bit brute-force budget,
The Appendix of the paper implements both attacks in a handful of lines of Wolfram Language. The same code is in assets/code/.
Classic attack: classic.wl
e = 7502876735617; n = 28562942440499; (* example of an attackable public key *)
fc = ContinuedFraction[e/n];
cList = Convergents[fc];
phiList = Floor[e/Rest[cList]]; (* candidate φ(N) for every convergent *)
(* a candidate φ is right iff both roots of x² − (n − φ + 1)x + n divide n *)
check[phi_] := (x/.p) /; (Mod[n,x] /. (p=Solve[x^2-(n-phi+1)x+n==0,x])) == {0, 0};
checkL[phi_List] := Flatten[Cases[check /@ phi,_List]];
primes = Flatten[checkL /@ (phiList-m /. {m->#}& /@ Range[0,9])]; (* try m = 0 … 9 *)| Step | Wolfram code | Section |
|---|---|---|
| expand |
ContinuedFraction, Convergents
|
§3.1 |
Floor[e/Rest[cList]], phiList - m
|
§3.2 | |
| factor via |
Solve[x^2-(n-phi+1)x+n==0, x] |
§2, |
For the sample key above the expansion is
d = 769 p = 5685857 q = 5023507
Extended attack: extended.wl
r = 4; Dl = 2; (* tune to the case at hand *)
CClist = Partition[Rest[cList],2,1]; (* pairs of consecutive convergents *)
UVlist = Tuples[Range[2^r],2]; (* all (U, V) to try *)
T = Tuples[{CClist,UVlist}];
phiList = Floor[
e(T[[2,1]]Denominator[T[[1,2]]]+(T[[2,1]]Dl+T[[2,2]])Denominator[T[[1,1]]])/
(T[[2,1]] Numerator[T[[1,2]]]+(T[[2,1]]Dl+T[[2,2]])Numerator[T[[1,1]]])];
primes = Flatten[checkL /@ (phiList-m /. {m->#}& /@ Range[0,9])];extended.wl reuses e, cList and checkL from the classic script, so it has to run in the same session after it. r (search size) and Dl (
With wolframscript installed, from the repository root:
# classic attack
wolframscript -code 'Get["assets/code/classic.wl"]; primes'
# extended attack (needs the definitions from classic.wl)
wolframscript -code 'Get["assets/code/classic.wl"]; Get["assets/code/extended.wl"]; primes'Both scripts end with a ;, which suppresses output, so primes is evaluated explicitly at the end. You can also paste the cells into a Mathematica notebook and evaluate primes.
The paper is written in Italian with the tufte-book class (included in src/). It uses wide margins for definitions, lemmas, side notes and historical asides. With a full TeX distribution (e.g. TeX Live):
cd src
latexmk -pdf main.texThe precompiled output is src/wiener_attack.pdf.
- W. Diffie, M. Hellman — New Directions in Cryptography, IEEE Trans. Inf. Theory, 1976.
- R.L. Rivest, A. Shamir, L. Adleman — A Method for Obtaining Digital Signatures and Public-Key Cryptosystems, Commun. ACM, 1978.
- G.J. Simmons, M.J. Norris — Preliminary comments on the MIT public-key cryptosystem, Cryptologia, 1977.
- M.J. Wiener — Cryptanalysis of Short RSA Secret Exponents, IEEE Trans. Inf. Theory, 1990.
- D. Boneh — Twenty years of attacks on the RSA cryptosystem, Notices of the AMS, 1999.
- E.R. Verheul, H.C.A. van Tilborg — Cryptanalysis of 'less short' RSA secret exponents, AAECC, 1997.
- A. Dujella — Continued fractions and RSA with small secret exponent, Tatra Mt. Math. Publ., 2004.
- D. Boneh, G. Durfee — Cryptanalysis of RSA with Private Key d Less Than N^0.292, 1998.
- PKCS #1: RSA Cryptography Specifications Version 2.2, IETF, 2012.
The full bibliography is at the end of the paper, written directly in src/main.tex (thebibliography environment).
