Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 162 additions & 3 deletions CompPoly/Fields/Mersenne31/Circle.lean
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ respects addition. Each subgroup step has the requested exact order.

`Coset.Valid` requires the step order to equal the declared size. Standard constructors
satisfy it; valid cosets enumerate distinct indices and points and have the declared
range cardinality. Circle domains still need a validity condition requiring disjoint
halves before their declared size can be identified with set cardinality.
range cardinality. A valid circle domain has disjoint halves and enumerates distinct
points. The canonical domain enumerates the entire canonical coset.
-/

public section
Expand Down Expand Up @@ -593,7 +593,7 @@ end Coset

/-- A domain indexing shape: a half coset followed by its conjugate.
The halves need not be disjoint, and either half may contain repeated points.
A follow-up must impose validity conditions before identifying `size` with cardinality. -/
`CircleDomain.Valid` gives the conditions for distinctness and cardinality. -/
structure CircleDomain where
halfCoset : Coset

Expand Down Expand Up @@ -666,6 +666,56 @@ theorem indexAt_right (D : CircleDomain) (i : Nat) :
D.indexAt (D.halfCoset.size + i) = -D.halfCoset.indexAt i := by
rw [indexAt_of_le D _ (Nat.le_add_right _ _), Nat.add_sub_cancel_left]

/-- The half coset has exact step order and shares no bounded index with its conjugate. -/
def Valid (D : CircleDomain) : Prop :=
D.halfCoset.Valid ∧ ∀ i j : Fin D.halfCoset.size,
D.halfCoset.indexAt i.val ≠ -D.halfCoset.indexAt j.val

/-- Domain validity separates exact order within each half from disjointness across halves. -/
theorem valid_iff (D : CircleDomain) : D.Valid ↔ D.halfCoset.Valid ∧
∀ i j : Fin D.halfCoset.size, D.halfCoset.indexAt i.val ≠ -D.halfCoset.indexAt j.val :=
Iff.rfl

/-- A valid domain visits each bounded index only once. -/
theorem indexAt_injective (D : CircleDomain) (hD : D.Valid) :
Function.Injective (fun i : Fin D.size => D.indexAt i.val) := by
intro i j hij
have hi := i.isLt
have hj := j.isLt
have hs := size_eq_two_mul_halfSize D
change D.indexAt i.val = D.indexAt j.val at hij
have inj := D.halfCoset.indexAt_injective hD.1
apply Fin.ext
by_cases il : i.val < D.halfCoset.size <;> by_cases jl : j.val < D.halfCoset.size
· rw [indexAt_left _ _ il, indexAt_left _ _ jl] at hij
exact congrArg (Fin.val (n := D.halfCoset.size)) (inj (a₁ := ⟨_, il⟩) (a₂ := ⟨_, jl⟩) hij)
· rw [indexAt_left _ _ il, indexAt_of_le _ _ (by omega)] at hij
exact (hD.2 ⟨_, il⟩ ⟨_, by omega⟩ hij).elim
· rw [indexAt_of_le _ _ (by omega : D.halfCoset.size ≤ i.val), indexAt_left _ _ jl] at hij
exact (hD.2 ⟨_, jl⟩ ⟨_, by omega⟩ hij.symm).elim
· rw [indexAt_of_le _ _ (by omega : D.halfCoset.size ≤ i.val),
indexAt_of_le _ _ (by omega : D.halfCoset.size ≤ j.val), neg_inj] at hij
have he := congrArg Fin.val (inj
(a₁ := ⟨i.val - D.halfCoset.size, by omega⟩)
(a₂ := ⟨j.val - D.halfCoset.size, by omega⟩) hij)
change i.val - D.halfCoset.size = j.val - D.halfCoset.size at he
omega

/-- A valid domain visits each bounded circle point only once. -/
theorem pointAt_injective (D : CircleDomain) (hD : D.Valid) :
Function.Injective (fun i : Fin D.size => D.pointAt i.val) :=
CirclePointIndex.toPoint_injective.comp (D.indexAt_injective hD)

/-- The bounded index range of a valid domain has its declared cardinality. -/
theorem card_range_indexAt (D : CircleDomain) (hD : D.Valid) :
Nat.card (Set.range (fun i : Fin D.size => D.indexAt i.val)) = D.size := by
simpa only [Nat.card_fin] using Nat.card_range_of_injective (D.indexAt_injective hD)

/-- The bounded point range of a valid domain has its declared cardinality. -/
theorem card_range_pointAt (D : CircleDomain) (hD : D.Valid) :
Nat.card (Set.range (fun i : Fin D.size => D.pointAt i.val)) = D.size := by
simpa only [Nat.card_fin] using Nat.card_range_of_injective (D.pointAt_injective hD)

end CircleDomain

/-- A canonical STWO coset `G_{2n} + <G_n>` for `1 ≤ logSize < logOrder`. -/
Expand Down Expand Up @@ -731,6 +781,115 @@ theorem circleDomain_logSize (c : CanonicCoset) : c.circleDomain.logSize = c.log
theorem circleDomain_size (c : CanonicCoset) : c.circleDomain.size = 2 ^ c.logSize := by
rw [CircleDomain.size_eq, circleDomain_logSize]

/-- Doubling the generator of the larger subgroup gives the generator one level below. -/
private theorem subgroupGen_double (n : Nat) (h : n + 1 ≤ logOrder) :
CirclePointIndex.subgroupGen n (by omega) =
2 * CirclePointIndex.subgroupGen (n + 1) h := by
simp only [CirclePointIndex.subgroupGen_eq, Nat.cast_pow, Nat.cast_ofNat]
rw [show logOrder - n = (logOrder - (n + 1)) + 1 by omega, pow_succ']

/-- The full canonical coset advances by twice its initial index. -/
private theorem step_eq (c : CanonicCoset) : c.coset.stepSize = 2 * c.coset.initialIndex :=
subgroupGen_double c.logSize c.logSize_succ_le_logOrder

/-- The half coset starts at the full coset’s initial index and takes double steps. -/
private theorem half_parameters (c : CanonicCoset) :
c.halfCoset.initialIndex = c.coset.initialIndex ∧
c.halfCoset.stepSize = 2 * c.coset.stepSize := by
obtain ⟨n, hn, hb⟩ := c
cases n with
| zero => omega
| succ n => exact ⟨rfl, subgroupGen_double n (by omega)⟩

/-- The full coset has twice as many positions as the half coset. -/
private theorem full_size (c : CanonicCoset) : c.coset.size = 2 * c.halfCoset.size := by
calc
c.coset.size = c.circleDomain.size := (circleDomain_size c).symm
_ = _ := CircleDomain.size_eq_two_mul_halfSize _

/-- Half-coset position `i` visits full-coset position `2 * i`. -/
private theorem half_indexAt (c : CanonicCoset) (i : Nat) :
c.halfCoset.indexAt i = c.coset.indexAt (2 * i) := by
simp only [Coset.indexAt_eq, (half_parameters c).1, (half_parameters c).2,
Nat.cast_mul, Nat.cast_ofNat]
ring

/-- Conjugating the half coset visits the odd full-coset positions in descending order. -/
private theorem neg_half_indexAt (c : CanonicCoset) (i : Fin c.halfCoset.size) :
-c.halfCoset.indexAt i.val = c.coset.indexAt (c.coset.size - (2 * i.val + 1)) := by
have hi := i.isLt
have hs := full_size c
have hz := addOrderOf_nsmul_eq_zero c.coset.stepSize
rw [show addOrderOf c.coset.stepSize = c.coset.size from c.coset_valid, nsmul_eq_mul,
mul_comm] at hz
rw [half_indexAt, Coset.indexAt_eq, Coset.indexAt_eq, Nat.cast_sub (by omega)]
simp only [Nat.cast_add, Nat.cast_mul, Nat.cast_ofNat, Nat.cast_one]
rw [mul_sub, hz, step_eq]
ring

/-- The canonical half coset and its conjugate occupy disjoint full-coset positions. -/
theorem circleDomain_valid (c : CanonicCoset) : c.circleDomain.Valid := by
refine ⟨c.halfCoset_valid, ?_⟩
change ∀ i j : Fin c.halfCoset.size, c.halfCoset.indexAt i.val ≠ -c.halfCoset.indexAt j.val
intro i j hij
rw [half_indexAt, neg_half_indexAt] at hij
have hs := full_size c
have hi := i.isLt
have hj := j.isLt
have he := congrArg Fin.val (c.coset.indexAt_injective c.coset_valid
(a₁ := ⟨2 * i.val, by omega⟩) (a₂ := ⟨c.coset.size - (2 * j.val + 1), by omega⟩) hij)
change 2 * i.val = c.coset.size - (2 * j.val + 1) at he
omega

/-- Traversing the canonical domain covers exactly the bounded full-coset indices. -/
theorem range_circleDomain_indexAt (c : CanonicCoset) :
Set.range (fun i : Fin c.circleDomain.size => c.circleDomain.indexAt i.val) =
Set.range (fun i : Fin c.coset.size => c.coset.indexAt i.val) := by
have hs := full_size c
have hd : c.circleDomain.size = 2 * c.halfCoset.size := CircleDomain.size_eq_two_mul_halfSize _
have left (i : Nat) (hi : i < c.halfCoset.size) :
c.circleDomain.indexAt i = c.coset.indexAt (2 * i) :=
(CircleDomain.indexAt_left _ _ hi).trans (half_indexAt c i)
have right (i : Fin c.halfCoset.size) :
c.circleDomain.indexAt (c.halfCoset.size + i.val) =
c.coset.indexAt (c.coset.size - (2 * i.val + 1)) :=
(CircleDomain.indexAt_right _ _).trans (neg_half_indexAt c i)
ext x
constructor
· rintro ⟨i, rfl⟩
have hi := i.isLt
by_cases h : i.val < c.halfCoset.size
· exact ⟨⟨_, by omega⟩, (left _ h).symm⟩
· let j : Fin c.halfCoset.size := ⟨i.val - c.halfCoset.size, by omega⟩
have he : c.halfCoset.size + j.val = i.val := by dsimp [j]; omega
exact ⟨⟨_, by have hj := j.isLt; omega⟩, (right j).symm.trans (congrArg _ he)⟩
· rintro ⟨i, rfl⟩
have hi := i.isLt
by_cases h : i.val % 2 = 0
· refine ⟨⟨i.val / 2, by omega⟩, ?_⟩
exact (left (i.val / 2) (by omega)).trans (congrArg c.coset.indexAt (by omega))
· let j : Fin c.halfCoset.size := ⟨(c.coset.size - 1 - i.val) / 2, by omega⟩
have he : c.coset.size - (2 * j.val + 1) = i.val := by dsimp [j]; omega
exact ⟨⟨_, by have hj := j.isLt; omega⟩, (right j).trans (congrArg _ he)⟩

/-- The canonical domain and its full coset enumerate the same circle points. -/
theorem range_circleDomain_pointAt (c : CanonicCoset) :
Set.range (fun i : Fin c.circleDomain.size => c.circleDomain.pointAt i.val) =
Set.range (fun i : Fin c.coset.size => c.coset.pointAt i.val) := by
simpa only [← Set.range_comp, Function.comp_def, CircleDomain.pointAt_def, Coset.pointAt_def]
using congrArg (Set.image CirclePointIndex.toPoint) c.range_circleDomain_indexAt

/-- Each point of the full canonical coset occurs at a unique bounded domain position. -/
theorem existsUnique_circleDomain_pointAt (c : CanonicCoset) (j : Fin c.coset.size) :
∃! i : Fin c.circleDomain.size, c.circleDomain.pointAt i.val = c.coset.pointAt j.val := by
have hj : c.coset.pointAt j.val ∈ Set.range (fun i : Fin c.circleDomain.size =>
c.circleDomain.pointAt i.val) := by
rw [c.range_circleDomain_pointAt]
exact ⟨j, rfl⟩
obtain ⟨i, hi⟩ := hj
exact ⟨i, hi, fun _ hk =>
c.circleDomain.pointAt_injective c.circleDomain_valid (hk.trans hi.symm)⟩

end CanonicCoset

end Circle
Expand Down
2 changes: 1 addition & 1 deletion CompPoly/Fields/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ This directory contains formally verified field infrastructure used in zero-know
| **Mersenne31.lean** | Facade for the \(2^{31} - 1\) Circle STARK field, re-exporting the canonical `ZMod` model, fast native-word implementation, and circle group/domain indexing API. |
| **Mersenne31/Basic.lean** | Canonical \(2^{31} - 1\) field model and primality proof. |
| **Mersenne31/Fast.lean** | Verified `UInt32` implementation of Mersenne31 arithmetic. |
| **Mersenne31/Circle.lean** | Additive circle group with fast scalars, proved generator order and index homomorphism, and bounded subgroup indices. Domain distinctness and cardinality remain follow-up work; see the [circle-group guide](../../docs/wiki/field-extensions.md#mersenne31-circle-group). |
| **Mersenne31/Circle.lean** | Additive circle group with fast scalars, proved generator order and index homomorphism, and bounded subgroup indices. The canonical domain enumerates every full-coset point exactly once. See the [circle-group guide](../../docs/wiki/field-extensions.md#mersenne31-circle-group). |
| **Montgomery/Basic.lean** | Radix-generic Montgomery reduction, field-agnostic number theory shared by the fast prime fields. |
| **Montgomery/Native32.lean** | Raw `UInt32`/`UInt64` Montgomery reduction over explicit word constants, including bounds and correctness. |
| **Montgomery/Native32Field.lean** | Per-field parameters, the shared `FastField` carrier, arithmetic, instances, and canonical-field bridge. |
Expand Down
14 changes: 11 additions & 3 deletions docs/wiki/field-extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,17 @@ Conjugation preserves validity. For valid cosets, `indexAt_injective` and
`card_range_pointAt` identify the bounded ranges' cardinalities with `c.size`.
Arbitrary structure values need not satisfy this condition.

`CircleDomain` still describes an indexing shape: even a valid half coset may overlap
its conjugate. Domain validity, disjointness of the halves, and domain cardinality
remain follow-up work. Coset validity alone does not establish these claims.
`CircleDomain.Valid` requires a valid half coset whose bounded indices are disjoint
from their conjugates. For a valid domain, `CircleDomain.pointAt_injective` proves
that distinct positions give distinct points, and `CircleDomain.card_range_pointAt`
gives the number of points. The same results hold for indices.

`CanonicCoset.circleDomain_valid` proves validity at every supported log size
from 1 through 30. The first half visits the even positions of the full coset in
ascending order. Its conjugate visits the odd positions in descending order.
`CanonicCoset.range_circleDomain_pointAt` identifies the resulting set of points
with the full coset's bounded point range. For an individual full-coset position,
`CanonicCoset.existsUnique_circleDomain_pointAt` gives the unique matching domain position.

## Irreducibility: Rabin, Collapsed

Expand Down
35 changes: 35 additions & 0 deletions tests/CompPolyTests/Fields/Mersenne31/Circle.lean
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,41 @@ example : (CircleDomain.new (Coset.subgroup 0 (by decide))).indexAt 0 =
Nat.sub_self, Coset.indexAt_zero, Coset.new_initialIndex, Coset.conjugate_initialIndex,
neg_zero]

example : ¬ (CircleDomain.new (Coset.subgroup 0 (by decide))).Valid := by
intro h
obtain ⟨_, disjoint⟩ := (CircleDomain.valid_iff _).mp h
have hsize : 0 < (CircleDomain.new (Coset.subgroup 0 (by decide))).halfCoset.size := by
rw [CircleDomain.new_halfCoset, Coset.size_eq]
exact pow_pos (by decide) _
apply disjoint ⟨0, hsize⟩ ⟨0, hsize⟩
simp only [CircleDomain.new_halfCoset, Coset.indexAt_zero, Coset.subgroup_eq,
Coset.new_initialIndex, neg_zero]

example (D : CircleDomain) (h : D.Valid) :
Function.Injective (fun i : Fin D.size => D.pointAt i.val) := D.pointAt_injective h

example (c : CanonicCoset) :
Set.range (fun i : Fin c.circleDomain.size => c.circleDomain.pointAt i.val) =
Set.range (fun i : Fin c.coset.size => c.coset.pointAt i.val) := c.range_circleDomain_pointAt

example (c : CanonicCoset) (j : Fin c.coset.size) :
∃! i : Fin c.circleDomain.size, c.circleDomain.pointAt i.val = c.coset.pointAt j.val :=
c.existsUnique_circleDomain_pointAt j

-- Both endpoint cardinalities follow symbolically, including the billion-point domain.
example : let c : CanonicCoset := ⟨1, by decide, by decide⟩
Nat.card (Set.range (fun i : Fin c.circleDomain.size => c.circleDomain.pointAt i.val)) =
2 := by
intro c
rw [CircleDomain.card_range_pointAt _ c.circleDomain_valid, CanonicCoset.circleDomain_size]
rfl

example : let c : CanonicCoset := ⟨30, by decide, by decide⟩
Nat.card (Set.range (fun i : Fin c.circleDomain.size => c.circleDomain.pointAt i.val)) =
2 ^ 30 := by
intro c
rw [CircleDomain.card_range_pointAt _ c.circleDomain_valid, CanonicCoset.circleDomain_size]

example : order • generator = 0 := order_nsmul_generator

example : (2 ^ 30 : Nat) • generator = Point.antipode 0 := generator_half_order
Expand Down
Loading