Skip to content
100 changes: 22 additions & 78 deletions CompPoly/Fields/Goldilocks/Fast.lean
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ kernels from `FastDefs` with the bounds proved in `FastReduction`. Every operati
identified with its counterpart in the canonical `ZMod` model, and the field instances
are transferred across `toField`.

Reduction rests on `2^64 ≡ 2^32 - 1 (mod p)`, so a 128-bit product folds back into one
word with shifts, one multiply by `2^32 - 1`, and carry corrections.
Reduction rests on `2^64 ≡ 2^32 - 1 (mod p)`, which folds a 128-bit product back into one
word with shifts, additions and carry corrections.
-/

@[expose] public section
Expand Down Expand Up @@ -122,11 +122,7 @@ def toField (x : Field) : Goldilocks.Field :=
/-- Fast modular addition in canonical form. -/
@[inline]
def add (x y : Field) : Field :=
let lo := x.val + y.val
let carry := decide (lo < x.val)
⟨reduceAddWithCarryRaw lo carry,
reduceAddWithCarryRaw_lt lo carry
(addWithCarry_bound x.val y.val x.property y.property)⟩
⟨addRaw x.val y.val, addRaw_lt x.property y.property⟩

/-- Fast modular negation in canonical form. -/
@[inline]
Expand All @@ -148,41 +144,25 @@ def mul (x y : Field) : Field :=
def square (x : Field) : Field :=
mul x x

/-- Repeated squaring: `squareN x n` computes `x^(2^n)`. -/
/-- Repeated squaring, `squareN x n = x^(2^n)`, as a tail-recursive loop. -/
@[inline]
def squareN (x : Field) : Nat → Field
| 0 => x
| n + 1 => square (squareN x n)
| n + 1 => squareN (square x) n

/-- Exponentiation over the fast representation using binary exponentiation. -/
/-- Exponentiation through the lazy ladder `powLazy`, canonicalized once. -/
@[inline]
def pow (x : Field) (n : Nat) : Field :=
@npowBinRec Field ⟨one⟩ ⟨mul⟩ n x
⟨reduceUInt64Raw (powLazy 1 x.val n), reduceUInt64Raw_lt _⟩

/-- Fermat exponent used for inversion in the Goldilocks prime field. -/
@[inline]
private def invExponent : Nat := Goldilocks.fieldSize - 2

/-- Fast modular inversion using an addition chain for `p - 2`.

For Goldilocks, `p - 2 = 0xFFFFFFFEFFFFFFFF`. The chain builds
`x^(2^31 - 1)`, derives `x^(2^32 - 2)` and `x^(2^32 - 1)`, then combines them as

`(2^32 - 2) * 2^32 + (2^32 - 1) = p - 2`.
-/
/-- Fast modular inversion through the lazy Fermat chain `invLazy`, canonicalized once. -/
@[inline]
def inv (x : Field) : Field :=
let t2 := mul (square x) x
let t4 := mul (squareN t2 2) t2
let t8 := mul (squareN t4 4) t4
let t16 := mul (squareN t8 8) t8
let t31 :=
mul (squareN t16 15)
(mul (squareN t8 7)
(mul (squareN t4 3)
(mul (square t2) x)))
let t32m2 := square t31
let t32m1 := mul t32m2 x
mul (squareN t32m2 32) t32m1
⟨reduceUInt64Raw (invLazy x.val), reduceUInt64Raw_lt _⟩

/-- Division through inversion and fast multiplication. -/
@[inline]
Expand Down Expand Up @@ -421,25 +401,9 @@ theorem toField_one : toField (1 : Field) = 1 := by
/-- Fast addition agrees with canonical-field addition. -/
@[simp]
theorem toField_add (x y : Field) : toField (x + y) = toField x + toField y := by
change
(((add x y).val.toNat : Goldilocks.Field) =
(x.val.toNat : Goldilocks.Field) + (y.val.toNat : Goldilocks.Field))
unfold add
rw [reduceAddWithCarryRaw_cast _ _
(addWithCarry_bound x.val y.val x.property y.property)]
let lo := x.val + y.val
let carry := decide (lo < x.val)
have hvalue := addWithCarry_value x.val y.val
change lo.toNat + (if carry then UInt64.size else 0) = x.val.toNat + y.val.toNat at hvalue
change
((lo.toNat : Goldilocks.Field) +
(if carry then (UInt64.size : Goldilocks.Field) else 0) =
(x.val.toNat : Goldilocks.Field) + (y.val.toNat : Goldilocks.Field))
by_cases hcarry : carry = true
· simp only [hcarry, ite_true] at hvalue ⊢
rw [← Nat.cast_add, hvalue, Nat.cast_add]
· simp only [hcarry, Bool.false_eq_true, ite_false, add_zero] at hvalue ⊢
rw [hvalue, Nat.cast_add]
change toField (add x y) = toField x + toField y
simp only [add, toField, toNat]
exact addRaw_cast x.property y.property

/-- Fast negation agrees with canonical-field negation. -/
@[simp]
Expand Down Expand Up @@ -483,46 +447,26 @@ theorem toField_squareN (x : Field) (n : Nat) :
simp
| succ n ih =>
unfold squareN
rw [toField_square, ih]
rw [← pow_add]
rw [ih, toField_square, ← pow_two, ← pow_mul]
congr 1
rw [Nat.pow_succ]
omega

/-- Fast multiplication is associative, proved by transporting to the canonical field. -/
private theorem mul_assoc_field (x y z : Field) : (x * y) * z = x * (y * z) := by
apply toField_injective
rw [toField_mul, toField_mul, toField_mul, toField_mul]
ring

/-- Binary exponentiation satisfies the expected successor equation. -/
private theorem pow_succ (x : Field) (n : Nat) : pow x (n + 1) = pow x n * x := by
unfold pow
let _ : Semigroup Field := {
mul := (· * ·)
mul_assoc := mul_assoc_field
}
exact npowBinRec_succ n x

/-- Fast natural-power computation agrees with powers in the canonical field. -/
@[simp]
theorem toField_pow (x : Field) (n : Nat) : toField (pow x n) = toField x ^ n := by
induction n with
| zero =>
unfold pow
rw [npowBinRec_zero]
rw [toField_one]
simp
| succ n ih =>
rw [pow_succ, toField_mul, ih, _root_.pow_succ]
change ((reduceUInt64Raw (powLazy 1 x.val n)).toNat : Goldilocks.Field) =
(x.val.toNat : Goldilocks.Field) ^ n
rw [reduceUInt64Raw_cast, powLazy_cast]
simp

/-- The optimized inversion chain computes the Fermat inverse exponent. -/
private theorem toField_inv_chain (x : Field) :
toField (inv x) = toField x ^ invExponent := by
unfold inv
simp only [toField_mul_def, toField_square, toField_squareN]
ring_nf
simp [invExponent, Goldilocks.fieldSize]
change ((reduceUInt64Raw (invLazy x.val)).toNat : Goldilocks.Field) =
(x.val.toNat : Goldilocks.Field) ^ invExponent
rw [reduceUInt64Raw_cast, invLazy_cast]
rfl

/-- Fast inversion agrees with canonical inversion before notation is unfolded. -/
private theorem toField_inv_raw (x : Field) : toField (inv x) = (toField x)⁻¹ := by
Expand Down
115 changes: 77 additions & 38 deletions CompPoly/Fields/Goldilocks/FastDefs.lean
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@ module
/-!
# Fast Goldilocks: runtime definitions (zero-import)

The runtime definitions of the native-word Goldilocks arithmetic, split out of
`CompPoly.Fields.Goldilocks.Fast` verbatim. All correctness statements about them
live in that sibling module, which imports this one.

This module deliberately has **zero imports**: downstream consumers put it into
`precompileModules` native-compilation lanes, and `precompileModules` compiles the
entire import closure, so the runtime definitions must not pull in mathlib.
Runtime word kernels of the native Goldilocks field, proved correct in
`CompPoly.Fields.Goldilocks.FastReduction`. This module has zero imports so that
`precompileModules` lanes, which compile the whole import closure, can take it without Mathlib.
-/

@[expose] public section
Expand All @@ -33,11 +29,12 @@ def negModulus : UInt64 := 0xffffffff

/-! ## Raw word kernels

Every kernel takes canonical `UInt64` inputs and returns a canonical representative
below the modulus. Correctness lives in `CompPoly.Fields.Goldilocks.Fast`. -/
Kernels take and return canonical words below the modulus; the `Lazy` ones take and
return any congruent word, so a chain canonicalizes once at the end. -/


/-- Full 64-by-64 product as `(lo, hi)` words, computed from 32-bit limbs. -/
/-- Full 64-by-64 product as `(lo, hi)` words from 32-bit limbs, in the shape clang fuses
into one widening multiply. -/
@[inline]
def wideMul (x y : UInt64) : UInt64 × UInt64 :=
let xLo := x &&& negModulus
Expand All @@ -48,50 +45,59 @@ def wideMul (x y : UInt64) : UInt64 × UInt64 :=
let p01 := xLo * yHi
let p10 := xHi * yLo
let p11 := xHi * yHi
let carry := (p00 >>> 32) + (p01 &&& negModulus) + (p10 &&& negModulus)
let hi := p11 + (p01 >>> 32) + (p10 >>> 32) + (carry >>> 32)
let t := (p00 >>> 32) + p01
let u := (t &&& negModulus) + p10
let hi := p11 + (t >>> 32) + (u >>> 32)
(x * y, hi)

/-- Raw one-word reduction for a `UInt64` value.

Since every `UInt64` is below `2^64 = p + 2^32 - 1`, one subtraction by `p`
is enough to canonicalize a native word.
-/
/-- Canonicalize a word by one conditional subtraction, since every `UInt64` is below `2 * p`. -/
@[inline]
def reduceUInt64Raw (x : UInt64) : UInt64 :=
if x < modulus then x else x - modulus

/-- Borrow arm of the 128-bit fold, out of line so the common path takes a predicted branch. -/
@[noinline]
def foldUInt128Borrow (lo hiHi hiLo : UInt64) : UInt64 :=
let t0 := lo - hiHi - negModulus
let t1 := hiLo * negModulus
let t2 := t0 + t1
if t2 < t0 then t2 + negModulus else t2

/-- Fold `lo + hi * 2^64` into one congruent, not necessarily canonical, word using
`2^64 ≡ 2^32 - 1`; the middle term is `(hi <<< 32) - hiLo` rather than a multiply. -/
@[inline]
def foldUInt128Lazy (lo hi : UInt64) : UInt64 :=
let hiHi := hi >>> 32
let hiLo := hi &&& negModulus
if lo < hiHi then foldUInt128Borrow lo hiHi hiLo
else
let t0 := lo - hiHi
let t1 := (hi <<< 32) - hiLo
let t2 := t0 + t1
if t2 < t0 then t2 + negModulus else t2

/-- Raw reduction of a 128-bit integer represented by low and high words modulo Goldilocks. -/
@[inline]
def reduceUInt128Raw (lo hi : UInt64) : UInt64 :=
let hi_hi := hi >>> 32
let hi_lo := hi &&& negModulus

let borrow := lo < hi_hi
let t0 := lo - hi_hi
let t0 := if borrow then t0 - negModulus else t0

let t1 := hi_lo * negModulus
reduceUInt64Raw (foldUInt128Lazy lo hi)

let t2 := t0 + t1
let overflow := t2 < t0
let t2 := if overflow then t2 + negModulus else t2

reduceUInt64Raw t2
/-- Product of two arbitrary words as one congruent, not necessarily canonical, word. -/
@[inline]
def mulLazy (x y : UInt64) : UInt64 :=
let product := wideMul x y
foldUInt128Lazy product.1 product.2

/-- Raw reduction of a 64-by-64 product modulo Goldilocks. -/
@[inline]
def reduceMulRaw (x y : UInt64) : UInt64 :=
let product := wideMul x y
reduceUInt128Raw product.1 product.2
reduceUInt64Raw (mulLazy x y)

/-- Raw one-step reduction for a 65-bit addition represented by low word and carry. -/
/-- Canonical sum as `x - (p - y)`, which borrows exactly when `x + y < p`. -/
@[inline]
def reduceAddWithCarryRaw (lo : UInt64) (carry : Bool) : UInt64 :=
if carry then
lo + negModulus
else
reduceUInt64Raw lo
def addRaw (x y : UInt64) : UInt64 :=
let u := modulus - y
let s := x - u
if x < u then s - negModulus else s

/-- Raw modular negation in canonical form. -/
@[inline]
Expand All @@ -103,4 +109,37 @@ def negRaw (x : UInt64) : UInt64 :=
def subRaw (x y : UInt64) : UInt64 :=
if y ≤ x then x - y else x - y - negModulus

/-! ## Lazy exponentiation -/


/-- Square-and-multiply on unreduced words: `powLazy acc x n` computes `acc * x^n`. -/
def powLazy (acc x : UInt64) (n : Nat) : UInt64 :=
if h : n = 0 then acc
else
let acc := if n % 2 = 1 then mulLazy acc x else acc
powLazy acc (mulLazy x x) (n / 2)
termination_by n
decreasing_by omega

/-- `x^(2^n)` on unreduced words. -/
def squareNLazy (x : UInt64) : Nat → UInt64
| 0 => x
| n + 1 => squareNLazy (mulLazy x x) n

/-- Fermat chain for `x^(p - 2)` on unreduced words, with
`p - 2 = (2^32 - 2) * 2^32 + (2^32 - 1)`. -/
def invLazy (x : UInt64) : UInt64 :=
let t2 := mulLazy (mulLazy x x) x
let t4 := mulLazy (squareNLazy t2 2) t2
let t8 := mulLazy (squareNLazy t4 4) t4
let t16 := mulLazy (squareNLazy t8 8) t8
let t31 :=
mulLazy (squareNLazy t16 15)
(mulLazy (squareNLazy t8 7)
(mulLazy (squareNLazy t4 3)
(mulLazy (mulLazy t2 t2) x)))
let t32m2 := mulLazy t31 t31
let t32m1 := mulLazy t32m2 x
mulLazy (squareNLazy t32m2 32) t32m1

end Goldilocks.Fast
Loading
Loading