Documentation

CslibFrontier.Crypto.Computational.RSATrapdoor

The RSA trapdoor permutation #

The RSA trapdoor permutation family over products N = p * q of distinct primes: key parameters (RSADesc and RSAPublicKey), modular exponentiation evaluation rsaEval and trapdoor inversion rsaInvert, correctness of inversion on all x < N, bijectivity on Fin N and (ZMod N)ˣ, polynomial-time computability of both evaluation and trapdoor inversion, and the computational RSA inversion game and assumption.

Main definitions #

Main results #

Implementation notes #

The evaluation and trapdoor-inversion functions rsaEval and rsaInvert are defined on arbitrary natural numbers and proved mutually inverse on all residues x < N (rsaInvert_rsaEval, rsaEval_rsaInvert), inducing bijections both on Fin N (rsaEval_bijective_fin, including nonunits) and on the multiplicative unit group (ZMod N)ˣ (rsaEval_bijective_units). The inversion experiment rsaInversionGame runs Cslib.Crypto.inversionExperiment on x drawn uniformly from Fin N (differing from Katz–Lindell Definition 9.46, which samples uniformly from (ZMod N)ˣ; for valid RSA moduli N = p * q, the nonunit fraction in Fin N is at most (p + q) / N) under an arbitrary generator family RSAGen := ℕ → PMF RSADesc (efficient key generation and bit-length bounds in n are not bundled).

An RSAInverter receives the public key and a challenge residue y : Fin N and returns a residue. RSAInverter.IsPPT asks for one uniform strict PPT machine that reads encRSAInput, the unary security parameter followed by N, e and y in binary, and writes the returned residue in binary. Since RSA evaluation is a bijection on Fin N, accepting any preimage is the same as recovering the challenge (rsaInversionGame_eq). rsaOneWay_iff_encoded shows that RSAOneWay is the assumption over word-level inverters that read the same layout on keys with 0 < N and may output any natural number: a word-level answer is reduced modulo N, and a typed inverter extends to all words (IsPPTOn.exists_extension).

References #

RSA evaluation: x ^ e % N.

RSA trapdoor inversion: y ^ d % N.

Public RSA parameters: the modulus N and the public exponent e.

  • N : ℕ

    The RSA modulus N = p * q.

  • e : ℕ

    The public exponent e.

The encoding of an RSA public key: N and e in binary, N framed (encRSAPublicKey_apply).

theorem Cslib.Crypto.encRSAPublicKey_N_budget {γ✝ : Type} {encIn✝ : γ✝ ↪ List Bool} {S✝ : γ✝ → ℕ} {g✝ : γ✝ → RSAPublicKey} :
@[reducible, inline]

The tuple of the fields of encRSAPublicKey.

theorem Cslib.Crypto.encRSAPublicKey_mk_inO {γ : Type} {encIn : γ ↪ List Bool} {x0 T0 S0 x1 T1 S1 : γ → ℕ} (h0 : Turing.MultiTapeTM.ComputableInO x0 encIn Turing.MultiTapeTM.encNat T0 S0) (h1 : Turing.MultiTapeTM.ComputableInO x1 encIn Turing.MultiTapeTM.encNat T1 S1) :
Turing.MultiTapeTM.ComputableInO (fun (x : γ) => { N := x0 x, e := x1 x }) encIn encRSAPublicKey (fun (x : γ) => T0 x + T1 x) fun (x : γ) => S0 x + S1 x
theorem Cslib.Crypto.encRSAPublicKey_mk_budget {γ : Type} {encIn : γ ↪ List Bool} {budget x0 x1 : γ → ℕ} (h0 : Turing.MultiTapeTM.ComputableInSpace x0 encIn Turing.MultiTapeTM.encNat budget) (h1 : Turing.MultiTapeTM.ComputableInSpace x1 encIn Turing.MultiTapeTM.encNat budget) :
Turing.MultiTapeTM.ComputableInSpace (fun (x : γ) => { N := x0 x, e := x1 x }) encIn encRSAPublicKey budget
theorem Cslib.Crypto.encRSAPublicKey_e_budget {γ✝ : Type} {encIn✝ : γ✝ ↪ List Bool} {S✝ : γ✝ → ℕ} {g✝ : γ✝ → RSAPublicKey} :

RSA evaluation x ↦ x ^ e % N on residues modulo N.

@[simp]
theorem Cslib.Crypto.RSAPublicKey.val_eval (pk : RSAPublicKey) (x : Fin pk.N) :
↑(pk.eval x) = rsaEval pk.N pk.e ↑x

The encoding of a residue modulo N by its value in binary.

Full RSA parameters: distinct primes p and q, a public exponent e coprime to (p - 1) * (q - 1), and a private exponent d inverting e modulo (p - 1) * (q - 1).

  • p : ℕ

    First prime factor.

  • q : ℕ

    Second prime factor.

  • e : ℕ

    Public exponent.

  • d : ℕ

    Private trapdoor exponent.

  • hp : Nat.Prime self.p

    Primality of p.

  • hq : Nat.Prime self.q

    Primality of q.

  • hpq : self.p ≠ self.q

    Distinctness of p and q.

  • he : self.e.Coprime ((self.p - 1) * (self.q - 1))

    e is coprime to φ(N) = (p - 1) * (q - 1).

  • hed : self.e * self.d ≡ 1 [MOD (self.p - 1) * (self.q - 1)]

    e * d ≡ 1 [MOD φ(N)].

@[reducible, inline]

The RSA modulus N = p * q.

Instances For
    @[reducible, inline]

    Euler's totient φ(N) = (p - 1) * (q - 1).

    The RSA modulus is positive.

    The RSA modulus is strictly greater than 1.

    The totient (p - 1) * (q - 1) is strictly greater than 1.

    theorem Cslib.Crypto.RSADesc.e_pos (desc : RSADesc) :
    0 < desc.e

    The public exponent is positive, since 0 is not coprime to φ(N) > 1.

    @[reducible, inline]

    The public key (N, e).

    theorem Cslib.Crypto.rsa_pow_mul_modEq (desc : RSADesc) (x : ℕ) :
    x ^ (desc.e * desc.d) ≡ x [MOD desc.modulus]

    Core modular identity: x ^ (e * d) ≡ x [MOD p * q] for all x : ℕ.

    theorem Cslib.Crypto.rsaInvert_rsaEval (desc : RSADesc) {x : ℕ} (hx : x < desc.modulus) :
    rsaInvert desc.modulus desc.d (rsaEval desc.modulus desc.e x) = x

    Trapdoor inversion recovers any x < N from rsaEval N e x.

    theorem Cslib.Crypto.rsaEval_rsaInvert (desc : RSADesc) {y : ℕ} (hy : y < desc.modulus) :
    rsaEval desc.modulus desc.e (rsaInvert desc.modulus desc.d y) = y

    RSA evaluation recovers any y < N from rsaInvert N d y.

    theorem Cslib.Crypto.rsaEval_rsaInvert_mod (desc : RSADesc) (y : ℕ) :
    rsaEval desc.modulus desc.e (rsaInvert desc.modulus desc.d y) = y % desc.modulus

    RSA evaluation undoes trapdoor inversion modulo N, for every natural number y.

    RSA evaluation is a bijection on Fin N.

    theorem Cslib.Crypto.rsaEval_bijective_units (desc : RSADesc) :
    Function.Bijective fun (x : (ZMod desc.modulus)ˣ) => x ^ desc.e

    RSA evaluation x ↦ x ^ e is a bijection on (ZMod N)ˣ.

    RSA evaluation x ↦ x ^ e % N is computable in polynomial time whenever N, e, and x are computable in polynomial time and 0 < N a.

    RSA trapdoor inversion y ↦ y ^ d % N is computable in polynomial time whenever N, d, and y are computable in polynomial time and 0 < N a.

    The RSA assumption #

    @[reducible, inline]

    An RSA parameter generator indexed by the security parameter n.

    The input of an RSA inverter: the security parameter, a public key (N, e) and a challenge residue modulo N, which is x ^ e % N in the experiment.

    • secParam : ℕ

      The security parameter, given to machines in unary.

    • publicKey : RSAPublicKey

      The public key (N, e).

    • challenge : Fin self.publicKey.N

      The challenge residue modulo N.

    @[reducible, inline]

    The tuple of the fields of encRSAInput.

    theorem Cslib.Crypto.encRSAInput_secParam_budget {γ✝ : Type} {encIn✝ : γ✝ ↪ List Bool} {S✝ : γ✝ → ℕ} {g✝ : γ✝ → RSAInput} :
    theorem Cslib.Crypto.encRSAInput_mk_budget {γ : Type} {encIn : γ ↪ List Bool} {budget x0 : γ → ℕ} {x1 : γ → RSAPublicKey} {x2 : (c : γ) → Fin (x1 c).N} (h0 : Turing.MultiTapeTM.ComputableInSpace x0 encIn Turing.MultiTapeTM.encUnary budget) (h1 : Turing.MultiTapeTM.ComputableInSpace x1 encIn encRSAPublicKey budget) (h2 : Turing.MultiTapeTM.ComputableInSpace (fun (c : γ) => (x1 c).encResidue (x2 c)) encIn Turing.MultiTapeTM.encBits budget) :
    Turing.MultiTapeTM.ComputableInSpace (fun (x : γ) => { secParam := x0 x, publicKey := x1 x, challenge := x2 x }) encIn encRSAInput budget
    theorem Cslib.Crypto.encRSAInput_challenge_budget {γ✝ : Type} {encIn✝ : γ✝ ↪ List Bool} {S✝ : γ✝ → ℕ} {g✝ : γ✝ → RSAInput} :
    theorem Cslib.Crypto.encRSAInput_mk_inO {γ : Type} {encIn : γ ↪ List Bool} {x0 T0 S0 : γ → ℕ} {x1 : γ → RSAPublicKey} {T1 S1 : γ → ℕ} {x2 : (c : γ) → Fin (x1 c).N} {T2 S2 : γ → ℕ} (h0 : Turing.MultiTapeTM.ComputableInO x0 encIn Turing.MultiTapeTM.encUnary T0 S0) (h1 : Turing.MultiTapeTM.ComputableInO x1 encIn encRSAPublicKey T1 S1) (h2 : Turing.MultiTapeTM.ComputableInO (fun (c : γ) => (x1 c).encResidue (x2 c)) encIn Turing.MultiTapeTM.encBits T2 S2) :
    Turing.MultiTapeTM.ComputableInO (fun (x : γ) => { secParam := x0 x, publicKey := x1 x, challenge := x2 x }) encIn encRSAInput (fun (x : γ) => T0 x + (T1 x + T2 x)) fun (x : γ) => S0 x + (S1 x + S2 x)
    theorem Cslib.Crypto.encRSAInput_publicKey_budget {γ✝ : Type} {encIn✝ : γ✝ ↪ List Bool} {S✝ : γ✝ → ℕ} {g✝ : γ✝ → RSAInput} :
    theorem Cslib.Crypto.encRSAInput_publicKey_polyTime {γ✝ : Type} {encIn✝ : γ✝ ↪ List Bool} {g✝ : γ✝ → RSAInput} :

    The encoding of an RSA inverter's input: the unary security parameter, the public key and the challenge in binary, each framed except the last (encRSAInput_apply).

    theorem Cslib.Crypto.encRSAInput_mk_polyTime {γ : Type} {encIn : γ ↪ List Bool} {x0 : γ → ℕ} {x1 : γ → RSAPublicKey} {x2 : (c : γ) → Fin (x1 c).N} (h0 : Turing.MultiTapeTM.ComputableInPolyTime x0 encIn Turing.MultiTapeTM.encUnary) (h1 : Turing.MultiTapeTM.ComputableInPolyTime x1 encIn encRSAPublicKey) (h2 : Turing.MultiTapeTM.ComputableInPolyTime (fun (c : γ) => (x1 c).encResidue (x2 c)) encIn Turing.MultiTapeTM.encBits) :
    Turing.MultiTapeTM.ComputableInPolyTime (fun (x : γ) => { secParam := x0 x, publicKey := x1 x, challenge := x2 x }) encIn encRSAInput

    The challenge of a computed RSA input, as a binary natural number.

    @[reducible, inline]

    An RSA inverter: given the security parameter, a public key (N, e) and a residue modulo N, it outputs a residue modulo N. RSAInverter.IsPPT states its efficiency on encodings.

    An RSA inverter is PPT when one uniform strict PPT machine reads encRSAInput and writes the returned residue in binary.

    noncomputable def Cslib.Crypto.rsaInversionGame (gen : RSAGen) (inverter : RSAInverter) (n : ℕ) :

    The RSA inversion experiment: sample desc ← gen n and run Cslib.Crypto.inversionExperiment for RSA evaluation on a uniform residue x modulo N. The inverter receives (pk, x ^ e % N) and wins when it returns a preimage.

    theorem Cslib.Crypto.rsaInversionGame_eq (gen : RSAGen) (inverter : RSAInverter) (n : ℕ) :
    rsaInversionGame gen inverter n = do let desc ← gen n let x ← Probability.PMF.uniformOfFintype (Fin desc.modulus) let x' ← (inverter n desc.publicKey (desc.publicKey.eval x)).eval pure (decide (x' = x))

    RSA evaluation is a bijection on Fin N, so an inverter wins rsaInversionGame exactly when it recovers the challenge.

    The RSA assumption for gen ([BS23], Attack Game 10.3 and Definition 10.5, pp. 404–405): every uniform strict PPT inverter recovers a uniform residue x ∈ Fin N from its RSA image x ^ e mod N with negligible probability. Keys are drawn from an arbitrary gen : RSAGen family rather than RSAGen(ℓ, e).

    RSAOneWay is the word-level RSA assumption. Quantifying over inverters that read (1ⁿ, (N, e), y) with binary numbers on keys with 0 < N, each with one uniform strict PPT machine, and may answer with any natural number gives the same assumption as quantifying over typed inverters. A word-level answer is reduced modulo N, which can only help; conversely, a typed inverter extends to all words by rerunning its machine and decoding its output (IsPPTOn.exists_extension).