Documentation

CslibFrontier.Foundations.Data.ZMod.ModulusSwitch

Nearest-integer modulus switching #

Scale the canonical centered integer representative by the rational ratio of target to source modulus, round to the nearest integer, and reduce modulo the target. Half-integer ties round toward positive infinity, as in Mathlib's round.

Main definitions #

Main results #

This is a numerical conversion, not a ring homomorphism. The intended source and target moduli are positive; the definition is total, and representative independence is stated for nonzero source modulus. No statement about plaintext preservation, hardness, or computational complexity is made.

def ZMod.modulusSwitch (p : ℕ) {q : ℕ} (x : ZMod q) :

Nearest-integer scaling from modulus q to modulus p, using the canonical centered lift. The rational division is total; representative independence is stated for nonzero source modulus.

theorem ZMod.modulusSwitch_intCast {q p : ℕ} (hq : q ≠ 0) (x : ℤ) :
modulusSwitch p ↑x = ↑(round (↑p / ↑q * ↑x))

Rounding after scaling gives the same residue from every integer representative.

Integer-only implementation of scaled nearest-integer rounding followed by reduction. Both moduli and the signed representative are runtime arithmetic inputs. At zero source or target modulus the formula is still total, using Lean's division and remainder conventions.

theorem ZMod.round_scale_eq_div {q : ℕ} (hq : 0 < q) (p : ℕ) (x : ℤ) :
round (↑p / ↑q * ↑x) = (2 * ↑p * x + ↑q) / (2 * ↑q)

For positive source modulus, Euclidean integer division implements nearest rounding, including negative half-integer ties towards positive infinity.

theorem ZMod.modulusSwitchResidue_eq_val {q p : ℕ} (hq : 0 < q) (hp : 0 < p) (x : ℤ) :

The integer-only implementation returns exactly the existing switch's canonical residue.