Documentation

CslibFrontier.Crypto.Computational.RSACommonRoot

Shamir's trick: a common root from coprime exponents #

Theorem 10.6 of [BS23] (p. 416), "Shamir's trick": given a modulus n, relatively prime exponents e and f, and units w, y modulo n with w ^ e = y ^ f, one can compute an e-th root of y modulo n without knowing the order of (ℤ/nℤ)ˣ. The book's algorithm computes s, t with e * s + f * t = gcd(e, f) = 1 by the extended Euclidean algorithm and outputs x = y ^ s * w ^ t.

Cslib.Crypto.commonRoot n e f w y is that algorithm on natural numbers. It takes s = modInv e f, the inverse of e modulo f computed by BinaryGcd.modInv, and t = (e * s - 1) / f, so that e * s - f * t = 1, and it outputs y ^ s * (w⁻¹) ^ t mod n with w⁻¹ = modInv w n. The cases f = 0 (which forces e = 1; the output is y mod n) and f = 1 (the output is w mod n) are separate branches: at f = 1, modInv e 1 = 0, so the general branch would output 1. Correctness follows from the group identities Commute.zpow_mul_zpow_zpow_eq_right and Commute.zpow_mul_zpow_zpow_eq_left of CslibFrontier/Foundations/Algebra/Group/ZPowCoprime.lean, applied in (ZMod n)ˣ.

Main definitions #

Main results #

Implementation notes #

The statement differs from the book's in three ways.

The certificate covers the computation on encoded numbers. Nothing here concerns an adversary or a security game. Theorem 10.5 is in RSAUnitInversion.lean and Theorem 10.7 in RSAHash.lean; the accumulator of §10.9 is in GUOAccumulator.lean, and its security under strong RSA in CslibFrontier/Crypto/Computational/GUOAccumulator/Security.lean.

The common-modulus attack on plain RSA is this program on two ciphertexts: Cslib.Crypto.commonRoot_rsaEval in RSAAttacks.lean proves that for coprime e₁, e₂ and m coprime to N, commonRoot N e₂ e₁ (rsaEval N e₁ m) (rsaEval N e₂ m) = m % N, and computableInPolyTime_commonRoot is the attack's polynomial-time certificate.

References #

def Cslib.Crypto.commonRoot (n e f w y : ℕ) :

The algorithm of [BS23], Theorem 10.6 (Shamir's trick), on natural numbers. For a modulus n, exponents e, f and residues w, y, it returns y mod n when f = 0, w mod n when f = 1, and otherwise y ^ s * (w⁻¹) ^ t mod n, where s = modInv e f, t = (e * s - 1) / f and w⁻¹ = modInv w n. When 0 < n, e and f are coprime, w is coprime to n and w ^ e ≡ y ^ f [MOD n], the output is an e-th root of y and an f-th root of w modulo n (commonRoot_pow_modEq_right, commonRoot_pow_modEq_left), and a unit modulo n if y is also coprime to n (commonRoot_coprime).

theorem Cslib.Crypto.commonRoot_lt {n : ℕ} (hn : 0 < n) (e f w y : ℕ) :
commonRoot n e f w y < n

The output of commonRoot is below a positive modulus.

theorem Cslib.Crypto.commonRoot_pow_modEq_right {n e f w y : ℕ} (hn : 0 < n) (hef : e.Coprime f) (hw : w.Coprime n) (h : w ^ e ≡ y ^ f [MOD n]) :
commonRoot n e f w y ^ e ≡ y [MOD n]

Shamir's trick ([BS23], Theorem 10.6, p. 416). For 0 < n, coprime e and f, w coprime to n and w ^ e ≡ y ^ f [MOD n], the output x = commonRoot n e f w y satisfies x ^ e ≡ y [MOD n]. The book's exponents are integers; here they are natural numbers.

theorem Cslib.Crypto.commonRoot_pow_modEq_left {n e f w y : ℕ} (hn : 0 < n) (hef : e.Coprime f) (hw : w.Coprime n) (h : w ^ e ≡ y ^ f [MOD n]) :
commonRoot n e f w y ^ f ≡ w [MOD n]

The output of Shamir's trick is also an f-th root of w: for 0 < n, coprime e and f, w coprime to n and w ^ e ≡ y ^ f [MOD n], commonRoot n e f w y ^ f ≡ w [MOD n]. The book's Theorem 10.6 does not state this; §10.9 (p. 422) uses it to split an aggregated membership proof.

theorem Cslib.Crypto.commonRoot_coprime {n e f w y : ℕ} (hn : 0 < n) (hef : e.Coprime f) (hw : w.Coprime n) (hy : y.Coprime n) (h : w ^ e ≡ y ^ f [MOD n]) :
(commonRoot n e f w y).Coprime n

The output of Shamir's trick is a unit modulo n, as in [BS23], Theorem 10.6 (x ∈ ℤₙ*): for 0 < n, coprime e and f, w and y coprime to n and w ^ e ≡ y ^ f [MOD n], commonRoot n e f w y is coprime to n. The hypothesis on y is used only when f = 0, where the output is y mod n.

commonRoot is computable in polynomial time whenever n, e, f, w and y are computable in polynomial time as natural numbers on encNat. There is no condition on the inputs: the modular exponentiations use powMod, which returns 1 at the modulus 0.