Carry-safe addition and subtraction of reduced residues #
Widen both operands before adding, then reduce and truncate. The extra bit prevents wraparound before modular reduction; comparisons and selections are charged. For modular subtraction of two already reduced residues, no widening or general division is needed: three subtractions, one comparison, and one word selection suffice.
Main definitions #
addResidue a b r: modulusa, initial residueb, and incrementr; returnbunchanged whenb ≥ a.subtractResidues a b d: modular difference(a - b) mod dof reduced residuesa, b < d.
Main results #
toNat_addResidue: exact modular addition of two reduced residues.synthesis_addResidue: at most100 * (k + 1)gates.toNat_subtractResidues: exact modular subtraction of two reduced residues.synthesis_subtractResidues: at most140 * (k + 1)gates.
References #
- Adapted from lean/Proofs/{CounterRemainders,ReducedResidue}.lean at revision
c4cfa198of [S. Schlesinger, Square-root circuit bounds for iterated functions] [Sch26f].
Add reduced residues b and r modulo a (requiring b < a and r < a for
modular-addition correctness), retaining b when b ≥ a.
Retaining the carry bit is essential: two valid residues can overflow k bits.
Instances For
Linear arithmetic and two selections, with a shared zero for widening.
Subtract reduced residues a and b modulo d without general division when a < d and
b < d.
Instances For
Three shared-carry subtractions, a comparison, and one word selection.