Documentation

CslibFrontier.Crypto.Computational.QuadraticResiduosity

Quadratic residuosity, Jacobi symbols, and Blum integers #

Quadratic residues in (ZMod (p * q))ˣ (building on Mathlib's IsSquare and Subgroup.square), the composite Jacobi symbol J_N(x) = (x / p)(x / q) for distinct odd primes p ≠ q, the sign sets J_N^{+1}, J_N^{-1}, and QNR_N^{+1}, Blum integers (p ≡ q ≡ 3 (mod 4)), and the computational quadratic residuosity assumption.

Main definitions #

Main results #

Implementation notes #

Quadratic residues and their subgroup in a commutative group use Mathlib's IsSquare and Subgroup.square (Subgroup.mem_square). The computational games qrRealGame and qrIdealGame and assumption QuadraticResiduosityHard are parameterized by an arbitrary generator family setup : ℕ → PMF ModulusDesc over pairs of distinct odd primes (p, q) (efficient sampling of (p, q) and bit-length bounds in n are not bundled in ModulusDesc), and quantify over uniform native strict-PPT distinguishers (IsPPTOn) taking (1^n, (N, y)) with binary encNat encodings (encQRInput).

References #

Composite Jacobi symbol and sign sets #

def Cslib.Crypto.jacobiSymbol (p q : ℕ) [Fact (Nat.Prime p)] [Fact (Nat.Prime q)] (x : (ZMod (p * q))ˣ) :

The composite Jacobi symbol J_{p*q}(x) = (x / p) * (x / q) for units modulo p * q.

theorem Cslib.Crypto.jacobiSymbol_eq_jacobiSym (p q : ℕ) [Fact (Nat.Prime p)] [Fact (Nat.Prime q)] (x : (ZMod (p * q))ˣ) :
jacobiSymbol p q x = jacobiSym (↑(↑x).val) (p * q)

jacobiSymbol p q x agrees with Mathlib's Jacobi symbol jacobiSym (x.val.val : ℤ) (p * q).

theorem Cslib.Crypto.jacobiSymbol_cases (p q : ℕ) [Fact (Nat.Prime p)] [Fact (Nat.Prime q)] (x : (ZMod (p * q))ˣ) :
jacobiSymbol p q x = 1 ∨ jacobiSymbol p q x = -1

For primes p, q, jacobiSymbol p q x takes values in {1, -1}.

theorem Cslib.Crypto.jacobiSymbol_eq_one_of_isSquare (p q : ℕ) [Fact (Nat.Prime p)] [Fact (Nat.Prime q)] {x : (ZMod (p * q))ˣ} (hx : IsSquare x) :
jacobiSymbol p q x = 1

Every quadratic residue modulo p * q has Jacobi symbol 1.

Units modulo p * q with Jacobi symbol +1.

Units modulo p * q with Jacobi symbol -1.

def Cslib.Crypto.quadResidueSet (p q : ℕ) [NeZero (p * q)] :
Finset (ZMod (p * q))ˣ

Quadratic residues in (ZMod (p * q))ˣ.

theorem Cslib.Crypto.mem_quadResidueSet_iff (p q : ℕ) [NeZero (p * q)] (x : (ZMod (p * q))ˣ) :

Membership in quadResidueSet p q is equivalent to membership in Subgroup.square.

Quadratic nonresidues in (ZMod (p * q))ˣ with Jacobi symbol +1 (QNR_N^{+1}).

Every quadratic residue has Jacobi symbol +1.

J_N^{+1} partitions into QR_N and QNR_N^{+1}.

For coprime odd primes p and q, a unit modulo p * q lies in QNR_N^{+1} iff neither of its Chinese remainder components is a square.

For distinct odd primes p ≠ q, |QR_N| = |QNR_N^{+1}|.

theorem Cslib.Crypto.isSquare_mul_of_mem_positiveNonresidues (p q : ℕ) [Fact (Nat.Prime p)] [Fact (Nat.Prime q)] (hpq : p ≠ q) (hp : 2 < p) (hq : 2 < q) {x y : (ZMod (p * q))ˣ} (hx : x ∈ positiveNonresidues p q) (hy : y ∈ positiveNonresidues p q) :
IsSquare (x * y)

For distinct odd primes p ≠ q, the product of two elements of QNR_N^{+1} is a quadratic residue: QNR_N^{+1} is a single coset of QR_N.

Blum integers and principal square roots #

A prime p is a Blum prime if p ≡ 3 (mod 4).

A Blum modulus (p, q) consists of two distinct Blum primes.

For any Blum modulus, -1 is a quadratic nonresidue with Jacobi symbol +1.

def Cslib.Crypto.blumSqrt (p q : ℕ) (y : (ZMod (p * q))ˣ) :
(ZMod (p * q))ˣ

Principal square root in (ZMod (p * q))ˣ for a Blum modulus: y ^ (((p - 1)(q - 1) + 4) / 8).

theorem Cslib.Crypto.blum_sq_pow_eq_one (p q : ℕ) (h : IsBlumModulus p q) (u : (ZMod (p * q))ˣ) :
(u ^ 2) ^ ((p - 1) * (q - 1) / 4) = 1

For a Blum modulus p * q, every square in (ZMod (p * q))ˣ has order dividing (p - 1) * (q - 1) / 4.

theorem Cslib.Crypto.blumSqrt_sq (p q : ℕ) [NeZero (p * q)] (h : IsBlumModulus p q) {y : (ZMod (p * q))ˣ} (hy : y ∈ quadResidueSet p q) :
blumSqrt p q y ^ 2 = y ∧ blumSqrt p q y ∈ quadResidueSet p q

For a Blum modulus, blumSqrt p q y is a square root of y whenever y ∈ QR_N, and lies in QR_N.

theorem Cslib.Crypto.blumSqrt_sq_self (p q : ℕ) [NeZero (p * q)] (h : IsBlumModulus p q) {x : (ZMod (p * q))ˣ} (hx : x ∈ quadResidueSet p q) :
blumSqrt p q (x ^ 2) = x

For a Blum modulus, blumSqrt p q (x ^ 2) = x whenever x ∈ QR_N.

theorem Cslib.Crypto.sq_bijOn_quadResidueSet (p q : ℕ) [NeZero (p * q)] (h : IsBlumModulus p q) :
Set.BijOn (fun (x : (ZMod (p * q))ˣ) => x ^ 2) ↑(quadResidueSet p q) ↑(quadResidueSet p q)

For a Blum modulus, squaring x ↦ x ^ 2 is a bijection from QR_N onto QR_N.

Computational quadratic residuosity assumption #

Valid RSA/QR modulus parameters (p, q) with distinct odd primes.

  • p : ℕ

    First odd prime factor.

  • q : ℕ

    Second odd prime factor.

  • prime_p : Nat.Prime self.p

    p is prime.

  • prime_q : Nat.Prime self.q

    q is prime.

  • ne_pq : self.p ≠ self.q

    p and q are distinct.

  • two_lt_p : 2 < self.p

    p is odd.

  • two_lt_q : 2 < self.q

    q is odd.

@[reducible, inline]

The composite modulus N = p * q of a ModulusDesc.

Every valid modulus N = p * q exceeds 1.

theorem Cslib.Crypto.ModulusDesc.val_neg_one (d : ModulusDesc) :
(↑(-1)).val = d.p * d.q - 1

The unit -1 modulo N = p * q has canonical residue N - 1.

The prime factors (p, q) of a modulus description; the proof fields are determined by them.

Encoding of a ModulusDesc as the binary pair (p, q); p is framed.

@[reducible, inline]

Input (1ⁿ, (N, y)) of a quadratic residuosity distinguisher: the security parameter n in unary, then the modulus N and the challenge residue y in binary. The parameter and N are framed.

noncomputable def Cslib.Crypto.qrRealGame (setup : ℕ → PMF ModulusDesc) (adversary : ℕ → ℕ × ℕ → ProbComp Bool) (n : ℕ) :

Real quadratic residuosity experiment: sample N = p * q and a uniform quadratic residue y ∈ QR_N, and give (N, y) to the distinguisher.

noncomputable def Cslib.Crypto.qrIdealGame (setup : ℕ → PMF ModulusDesc) (adversary : ℕ → ℕ × ℕ → ProbComp Bool) (n : ℕ) :

Ideal quadratic residuosity experiment: sample N = p * q and a uniform quadratic nonresidue y ∈ QNR_N^{+1} with Jacobi symbol +1, and give (N, y) to the distinguisher.

The computational quadratic residuosity assumption ([KL20], Section 15.4.3; locator not independently inspected in the 3rd-edition PDF): no uniform strict PPT distinguisher can distinguish a random quadratic residue in QR_N from a random quadratic nonresidue in QNR_N^{+1} given N = p * q, relative to the arbitrary modulus law setup.

Quadratic residuosity hardness implies integer factoring hardness #

Euler-criterion test for quadratic residuosity modulo the factor a of N = a * b: returns true when 1 < a, 1 < b, a * b = N, and y ^ (a / 2) % a = 1.

def Cslib.Crypto.factorToQR (F : ℕ → ℕ → ProbComp (ℕ × ℕ)) (n : ℕ) (Ny : ℕ × ℕ) :

Explicit PPT reduction from quadratic residuosity distinguishing to integer factoring ([KL20], Section 15.4): given (N, y), run the factoring algorithm F n N to obtain (a, b) and test Euler's criterion modulo a.

theorem Cslib.Crypto.qrRealGame_factorToQR (setup : ℕ → PMF ModulusDesc) (F : ℕ → ℕ → ProbComp (ℕ × ℕ)) (n : ℕ) :
qrRealGame setup (factorToQR F) n = modulusFactoringGame (fun (k : ℕ) => PMF.map ModulusDesc.modulus (setup k)) F n

In qrRealGame, factorToQR F succeeds with the exact distribution of modulusFactoringGame on the pushed-forward modulus distribution.

In qrIdealGame, factorToQR F always outputs false.

The QR distinguishing advantage of factorToQR F equals the factoring success probability of F.

Quadratic residuosity hardness implies integer factoring hardness: if setup satisfies QuadraticResiduosityHard, then factoring N = d.p * d.q is hard for (setup n).map ModulusDesc.modulus.