TCSLib

133 Cryptography — RSA

133.1 Overview

This file develops a small cost-tracking monad TimeM T α, whose values carry both a return value of type \(\alpha \) and an accumulated time cost of type \(T\) (typically \(T = \mathbb {N}\) counting comparisons), together with worked examples (median of three, insertion sort) that prove functional correctness on the returned value and complexity bounds on the recorded cost. It then formalizes the RSA cryptosystem: for distinct primes \(p,q\) and exponents \(e,d\) with \(ed = 1 + k(p-1)(q-1)\), decryption inverts encryption modulo \(n = pq\).

133.2 Declarations

Definition 133.1 Pure computation in the time monad
#

Lifts a value \(a : \alpha \) into the computation \(\langle a, 0\rangle \) of type TimeM T α, i.e. the computation that returns \(a\) at zero time cost.

Definition 133.2 Sequential composition in the time monad
#

Given \(m : \texttt{TimeM T α}\) and \(f : \alpha \to \texttt{TimeM T β}\), the bind of \(m\) and \(f\) runs \(f\) on the value returned by \(m\) and returns \(\langle (f\, m.\mathrm{ret}).\mathrm{ret},\; m.\mathrm{time} + (f\, m.\mathrm{ret}).\mathrm{time}\rangle \): the two time costs are added.

Theorem 133.3 Return value of pure
#

The value returned by \(\mathrm{pure}\, a\) is \(a\).

Theorem 133.4 Return value of a bind
#

The value returned by the bind of \(m\) with \(f\) is the value returned by \(f\, m.\mathrm{ret}\).

Theorem 133.5 Return value of a map
#

For \(f : \alpha \to \beta \) and \(x : \texttt{TimeM T α}\), the value returned by the mapped computation is \(f(x.\mathrm{ret})\).

Theorem 133.6 Return value of seqRight
#

The right-sequencing of \(x\) and \(y\) returns the value returned by \(y\, ()\).

Theorem 133.7 Return value of seqLeft
#

The left-sequencing of \(x\) and \(y\) returns \(x.\mathrm{ret}\).

Theorem 133.8 Return value of seq
#

For \(f : \texttt{TimeM T (α → β)}\) and \(x : \texttt{Unit → TimeM T α}\), the application \(\mathrm{seq}\, f\, x\) returns \(f.\mathrm{ret}\bigl((x\, ()).\mathrm{ret}\bigr)\).

Theorem 133.9 Time cost of a bind
#

The time cost of the bind of \(m\) with \(f\) is \(m.\mathrm{time} + (f\, m.\mathrm{ret}).\mathrm{time}\).

Theorem 133.10 Time cost of pure
#

The time cost of \(\mathrm{pure}\, a\) is \(0\).

Theorem 133.11 Time cost of a map
#

Mapping a function over a computation does not change its time cost: it is \(x.\mathrm{time}\).

Theorem 133.12 Time cost of seqRight
#

The time cost of the right-sequencing of \(x\) and \(y\) is \(x.\mathrm{time} + (y\, ()).\mathrm{time}\).

Theorem 133.13 Time cost of seqLeft
#

The time cost of the left-sequencing of \(x\) and \(y\) is \(x.\mathrm{time} + (y\, ()).\mathrm{time}\).

Theorem 133.14 Time cost of seq
#

The time cost of \(\mathrm{seq}\, f\, x\) is \(f.\mathrm{time} + (x\, ()).\mathrm{time}\).

Definition 133.15 Tick
#

The computation \(\mathrm{tick}\, c\) returns the trivial value and records time cost \(c\); it is the basic way of charging \(c\) units of time.

Theorem 133.16 Return value of a tick
#

The value returned by \(\mathrm{tick}\, c\) is the unit value.

Theorem 133.17 Time cost of a tick
#

The time cost of \(\mathrm{tick}\, c\) is \(c\).

Definition 133.18 Maximum of two naturals, timed
#

The timed computation returning the larger of two natural numbers \(x,y\), charging one unit of time for the single comparison \(x \le y\).

Definition 133.19 Median of three, timed
#

For \(a,b,c\) in a linear order, the timed computation returning a median of \(a,b,c\) by a nested sequence of comparisons, charging one unit of time per comparison performed.

Theorem 133.20 Correctness of the timed median
#

The value returned by \(\mathrm{median3}\, a\, b\, c\) is a median of \(a\), \(b\), \(c\), in the sense that it lies between at least one of the pairs drawn from \(\{ a,b,c\} \).

Theorem 133.21 Comparison count of the timed median
#

The computation \(\mathrm{median3}\, a\, b\, c\) records a time cost of at most \(3\), i.e. it performs at most three comparisons.

Definition 133.22 Timed ordered insertion
#

Inserts \(x\) into a list, walking the list until the first element \(y\) with \(x \le y\) and charging one unit of time per comparison. On a sorted input it produces the sorted list containing \(x\).

Definition 133.23 Timed insertion sort
#

Insertion sort in the time monad: recursively sort the tail and then insert the head using the timed insertion, accumulating the comparisons performed by every insertion step.

Definition 133.24 Sortedness by adjacent pairs
#

The predicate on lists defined recursively: the empty list and singletons are sorted, and \(x :: y :: \mathit{rest}\) is sorted when \(x \le y\) and \(y :: \mathit{rest}\) is sorted.

Theorem 133.25 Adjacent sortedness is a chain
#

For any list \(\mathit{xs}\), the predicate IsSorted holds of \(\mathit{xs}\) if and only if \(\mathit{xs}\) is a chain for \(\le \).

Theorem 133.26 Adjacent sortedness equals pairwise sortedness
#

For any list \(\mathit{xs}\), the predicate IsSorted holds of \(\mathit{xs}\) if and only if \(\mathit{xs}\) is sorted in the usual pairwise sense with respect to \(\le \).

Theorem 133.27 Value returned by the timed insertion
#

The value returned by the timed insertion of \(x\) into \(\mathit{xs}\) is the ordinary ordered insertion of \(x\) into \(\mathit{xs}\) for the relation \(\le \).

Theorem 133.28 Value returned by the timed insertion sort
#

The value returned by the timed insertion sort of \(\mathit{xs}\) is the ordinary insertion sort of \(\mathit{xs}\) for the relation \(\le \).

Theorem 133.29 Length after a timed insertion
#

The list returned by inserting \(x\) into \(\mathit{xs}\) has length \(\left\lvert \mathit{xs}\right\rvert + 1\).

Theorem 133.30 Length after a timed insertion sort

The list returned by the timed insertion sort of \(\mathit{xs}\) has the same length as \(\mathit{xs}\).

Theorem 133.31 Timed insertion is a permutation
#

The list returned by inserting \(x\) into \(\mathit{xs}\) is a permutation of \(x :: \mathit{xs}\).

Theorem 133.32 Timed insertion preserves sortedness

If \(\mathit{xs}\) is sorted, then the list returned by inserting \(x\) into \(\mathit{xs}\) is sorted.

Theorem 133.33 Timed insertion sort is a permutation
#

The list returned by the timed insertion sort of \(\mathit{xs}\) is a permutation of \(\mathit{xs}\).

Theorem 133.34 Timed insertion sort returns a sorted list

For every list \(\mathit{xs}\), the list returned by the timed insertion sort is sorted.

Theorem 133.35 Functional correctness of insertion sort

For every list \(\mathit{xs}\), the output of the timed insertion sort is both sorted and a permutation of \(\mathit{xs}\).

Definition 133.36 Insertion sort recurrence
#

The worst-case comparison recurrence \(T(0) = 0\) and \(T(n+1) = T(n) + n\).

Theorem 133.37 Cost of one insertion
#

Inserting \(x\) into a list \(\mathit{xs}\) costs at most \(\left\lvert \mathit{xs}\right\rvert \) comparisons.

Theorem 133.38 Insertion sort meets its recurrence

The time cost recorded by the timed insertion sort of \(\mathit{xs}\) is at most \(T(\left\lvert \mathit{xs}\right\rvert )\), where \(T\) is the insertion sort recurrence.

Theorem 133.39 Quadratic bound for the recurrence
#

For every \(n\), the recurrence satisfies \(T(n) \le n^2\).

Theorem 133.40 Insertion sort performs at most \(n^2\) comparisons

The time cost recorded by the timed insertion sort of a list \(\mathit{xs}\) of length \(n\) is at most \(n^2\).

Lemma 133.41 Core RSA exponent identity
#

Let \(p\) be prime. For every \(c \in \mathbb {N}\) and every \(x \in \mathbb {Z}/p\mathbb {Z}\),

\[ x^{\, 1 + c(p-1)} = x . \]

This is the form of Fermat’s little theorem needed for RSA, valid also at \(x = 0\).

Lemma 133.42 RSA identity from an exponent factorization
#

Let \(p\) be prime and \(m, ed, c \in \mathbb {N}\) with \(ed = 1 + c(p-1)\). Then \(m^{ed} \equiv m\) in \(\mathbb {Z}/p\mathbb {Z}\), where both sides are the images of natural numbers.

Lemma 133.43 RSA identity modulo \(p\)
#

If \(p\) is prime and \(ed = 1 + k(p-1)(q-1)\), then \(m^{ed} \equiv m\) in \(\mathbb {Z}/p\mathbb {Z}\) for every \(m \in \mathbb {N}\).

Lemma 133.44 RSA identity modulo \(q\)
#

If \(q\) is prime and \(ed = 1 + k(p-1)(q-1)\), then \(m^{ed} \equiv m\) in \(\mathbb {Z}/q\mathbb {Z}\) for every \(m \in \mathbb {N}\).

Lemma 133.45 Combining the two factors by CRT
#

Let \(p\) and \(q\) be coprime and suppose \(m^{ed} \equiv m\) both in \(\mathbb {Z}/p\mathbb {Z}\) and in \(\mathbb {Z}/q\mathbb {Z}\). Then \(m^{ed} \equiv m\) in \(\mathbb {Z}/pq\mathbb {Z}\).

Lemma 133.46 CRT combination in power form
#

The same statement written with the power taken inside the quotient ring: if \(p\) and \(q\) are coprime and \(\bar m^{\, ed} = \bar m\) in \(\mathbb {Z}/p\mathbb {Z}\) and in \(\mathbb {Z}/q\mathbb {Z}\), then \(\bar m^{\, ed} = \bar m\) in \(\mathbb {Z}/pq\mathbb {Z}\).

Definition 133.47 RSA public key
#

A public key consists of a modulus \(n \in \mathbb {N}\) and a public exponent \(e \in \mathbb {N}\).

Definition 133.48 RSA secret key
#

A secret key consists of a public key together with naturals \(p, q, d, k\) and proofs that \(p\) and \(q\) are prime, that \(p \ne q\), that the public modulus satisfies \(n = pq\), and that the exponents satisfy \(e d = 1 + k(p-1)(q-1)\).

Definition 133.49 RSA encryption
#

Encryption of a message \(m \in \mathbb {N}\) under a public key is \(\bar m^{\, e} \in \mathbb {Z}/n\mathbb {Z}\), using only the public data.

Definition 133.50 RSA decryption
#

Decryption of a ciphertext \(c \in \mathbb {Z}/n\mathbb {Z}\) under a secret key is \(c^{\, d}\), where \(d\) is the private exponent.

Theorem 133.51 Correctness of RSA

For any secret key and any message \(m \in \mathbb {N}\), decrypting the encryption of \(m\) recovers \(m\):

\[ \bigl(\bar m^{\, e}\bigr)^{d} = \bar m \quad \text{in } \mathbb {Z}/n\mathbb {Z}. \]