Skip to content
Merged
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
24 changes: 6 additions & 18 deletions CompPoly/Univariate/Bytes.lean
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,6 @@ variable {R : Type*} [Zero R] [BEq R] [LawfulBEq R]
theorem ofArray_val (p : CPolynomial R) : ofArray p.val = p :=
CPolynomial.ext (Raw.Trim.trim_eq_of_isCanonical p.property)

/-- `ofFn` on `n` coefficients has degree below `n`. -/
theorem ofFn_mem_degreeLT {n : ℕ} (f : Fin n → R) : ofFn f ∈ degreeLT (R := R) n := by
rw [mem_degreeLT_iff_size_le]
calc (ofFn f).val.size ≤ (Array.ofFn f).size := Raw.Trim.size_le_size _
_ = n := Array.size_ofFn

/-- A polynomial of degree below `n` is `ofFn` of its first `n` coefficients. -/
theorem ofFn_coeff_of_mem_degreeLT {n : ℕ} {p : CPolynomial R} (hp : p ∈ degreeLT (R := R) n) :
ofFn (fun i : Fin n => coeff p i) = p := by
rw [eq_iff_coeff]
intro i
rw [coeff_ofFn]
split
· rfl
· rename_i h
exact (coeff_eq_zero_of_size_le p
(le_trans (mem_degreeLT_iff_size_le.mp hp) (Nat.le_of_not_lt h))).symm

/-! ## Fixed width: `degreeLT n` -/

/-- The coefficient vector of a polynomial of degree below `n`, zero-padded. -/
Expand All @@ -67,6 +49,12 @@ def coeffVector (n : ℕ) (p : ↥(degreeLT (R := R) n)) : Vector R n :=
def ofCoeffVector (n : ℕ) (v : Vector R n) : ↥(degreeLT (R := R) n) :=
⟨ofFn fun i => v[i], ofFn_mem_degreeLT _⟩

/-- Rebuilding from a vector agrees with applying `ofFnEquiv` to its entries. -/
theorem ofCoeffVector_eq_ofFnEquiv (n : ℕ) (v : Vector R n) :
ofCoeffVector n v = ofFnEquiv n (fun i => v[i]) :=
Subtype.ext (ofFnEquiv_apply_val n _).symm

/-- Reading the coefficients into a vector and rebuilding the polynomial recovers `p`. -/
@[simp]
theorem ofCoeffVector_coeffVector (n : ℕ) (p : ↥(degreeLT (R := R) n)) :
ofCoeffVector n (coeffVector n p) = p := by
Expand Down
81 changes: 79 additions & 2 deletions CompPoly/Univariate/Linear.lean
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/-
Copyright (c) 2025 CompPoly. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Desmond Coles, Derek Sorensen
Authors: Desmond Coles, Derek Sorensen, Alexander Hicks
-/
module

Expand All @@ -11,7 +11,11 @@ public import CompPoly.Univariate.Basic
/-!
# Linear Algebra API for Computable Univariate Polynomials

This file contains linear maps and instance-stable bounded-degree predicates for `CPolynomial`.
This file defines coefficient linear maps and bounded-degree subtypes for `CPolynomial`.
The subtypes depend only on `Zero R`.

`ofFnEquiv n` identifies the coefficient vectors `Fin n → R` with the polynomials of degree below
`n`. Over a finite coefficient type, it gives the executable enumeration `instFintypeDegreeLT`.
-/

public section
Expand Down Expand Up @@ -77,6 +81,72 @@ theorem zero_mem_degreeLT (n : ℕ) : (0 : CPolynomial R) ∈ degreeLT (R := R)

end DegreeBounds

section OfFn

variable [Zero R] [BEq R] [LawfulBEq R]

/-- `ofFn` on `n` coefficients has degree below `n`. -/
theorem ofFn_mem_degreeLT {n : ℕ} (f : Fin n → R) : ofFn f ∈ degreeLT (R := R) n := by
rw [mem_degreeLT_iff_size_le]
calc (ofFn f).val.size ≤ (Array.ofFn f).size := Raw.Trim.size_le_size _
_ = n := Array.size_ofFn

/-- A polynomial of degree below `n` is `ofFn` of its first `n` coefficients. -/
theorem ofFn_coeff_of_mem_degreeLT {n : ℕ} {p : CPolynomial R} (hp : p ∈ degreeLT (R := R) n) :
ofFn (fun i : Fin n => coeff p i) = p := by
rw [eq_iff_coeff]
intro i
rw [coeff_ofFn]
split
· rfl
· rename_i h
exact (coeff_eq_zero_of_size_le p
(le_trans (mem_degreeLT_iff_size_le.mp hp) (Nat.le_of_not_lt h))).symm

/-- `ofFnEquiv n` is the equivalence, through `ofFn`, from the coefficient vectors `Fin n → R` to
the polynomials of degree below `n`. Its inverse reads the first `n` coefficients. Both directions
are executable. -/
def ofFnEquiv (n : ℕ) : (Fin n → R) ≃ ↥(degreeLT (R := R) n) where
toFun f := ⟨ofFn f, ofFn_mem_degreeLT f⟩
invFun p i := p.val.coeff i
left_inv f := funext fun i ↦ (coeff_ofFn f i).trans (dite_eq_left i.isLt)
right_inv p := Subtype.ext (ofFn_coeff_of_mem_degreeLT p.property)

/-- The polynomial that `ofFnEquiv n` assigns to `f` is `ofFn f`. -/
@[simp]
theorem ofFnEquiv_apply_val (n : ℕ) (f : Fin n → R) : (ofFnEquiv n f).val = ofFn f := by
rfl

/-- The inverse of `ofFnEquiv n` reads the first `n` coefficients. -/
@[simp]
theorem ofFnEquiv_symm_apply (n : ℕ) (p : ↥(degreeLT (R := R) n)) (i : Fin n) :
(ofFnEquiv n).symm p i = p.val.coeff i := by
rfl

/-- Over a finite coefficient type, the polynomials of degree below `n` form a finite type.

The enumeration converts each coefficient function `Fin n → R` to a polynomial with `ofFn`.
At `n = 0` it lists only the zero polynomial.

Downstream `by decide` can count small concrete enumerations. Use compiled evaluation such as
`#guard` to test predicates that inspect the polynomials. For proofs about such searches, use the
quantifier lemmas in `CompPoly.Univariate.ToPoly.Degree`.

The enumeration uses `BEq R` to trim trailing zeros. When the `BEq R` instance changes,
the subtype stays definitionally equal, but its enumerations may require a proof of equality.
`card_degreeLT` applies to any enumeration. -/
instance instFintypeDegreeLT [Fintype R] (n : ℕ) : Fintype ↥(degreeLT (R := R) n) :=
Fintype.ofEquiv (Fin n → R) (ofFnEquiv n)

end OfFn

/-- Over a finite coefficient type, there are `card R ^ n` polynomials of degree below `n`.
The statement holds for every `Fintype` instance on `↥(degreeLT n)`. -/
theorem card_degreeLT [Zero R] [Fintype R] (n : ℕ) [Fintype ↥(degreeLT (R := R) n)] :
Fintype.card ↥(degreeLT (R := R) n) = Fintype.card R ^ n := by
classical
rw [← Fintype.card_congr (ofFnEquiv (R := R) n), Fintype.card_fun, Fintype.card_fin]

section DegreeLTSubtype

variable [Semiring R] [BEq R] [LawfulBEq R]
Expand Down Expand Up @@ -203,6 +273,13 @@ theorem degreeLTCoeffs_apply (n : ℕ) (p : ↥(degreeLT (R := R) n)) (i : Fin n
degreeLTCoeffs n p i = coeff p.1 i := by
rfl

/-- The inverse of `ofFnEquiv` is the coefficient linear map. -/
theorem ofFnEquiv_symm_eq_degreeLTCoeffs (n : ℕ) :
((ofFnEquiv (R := R) n).symm : ↥(degreeLT (R := R) n) → (Fin n → R)) =
degreeLTCoeffs n := by
funext p i
simp only [ofFnEquiv_symm_apply, degreeLTCoeffs_apply]

end DegreeLTSubtype

end CPolynomial
Expand Down
13 changes: 11 additions & 2 deletions CompPoly/Univariate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,22 @@ on top (root finding and Reed-Solomon coding).
- **ToPoly.lean** — Umbrella import for conversion/equivalence with Mathlib's `Polynomial R`.
- **ToPoly/Core.lean** — Core conversion maps and API lemmas.
- **ToPoly/Equiv.lean** — Round-trip theorems and ring equivalence with Mathlib polynomials.
- **ToPoly/Degree.lean** — Degree/support transport lemmas for conversions, plus `degreeLT` / `degreeLTEquiv`.
- **ToPoly/Degree.lean** — Degree and support lemmas for `toPoly`, plus the linear equivalence
`degreeLTEquiv`. The quantifier lemmas connect searches over bounded-degree computable
polynomials to statements about Mathlib polynomials.
- `exists_degreeLT_toPoly_iff` transports one existential quantifier.
- `forall_degreeLT_toPoly_iff` transports one universal quantifier.
- `forall₂_degreeLT_toPoly_iff` transports two universal quantifiers with separate degree bounds.
- `exists_degreeLT_toPoly_iff_of_degree_lt` lets the predicate supply the degree bound.
- **ToPoly/Impl.lean** — Implementation-facing transport lemmas.
- **CMvEquiv.lean** — Ring equivalence `CPolynomial R ≃+* CMvPolynomial 1 R`, via `CPolynomial R ≃ R[X] ≃ (CMvPolynomial 0 R)[X] ≃ CMvPolynomial 1 R`.

## Arithmetic and structure

- **Linear.lean** — Linear-factor and affine helper lemmas for univariate workflows.
- **Linear.lean** — Coefficient linear maps and degree bounds for computable polynomials.
`ofFnEquiv` identifies `degreeLT n` with coefficient functions on `Fin n`.
Over a finite coefficient type, `instFintypeDegreeLT` uses this equivalence to enumerate the
subtype, and `card_degreeLT` gives its cardinality.
- **Deriv.lean** — Formal derivative `CPolynomial.derivative` and the Taylor shift `CPolynomial.taylor`.
- **Modular.lean** — Executable modular arithmetic over the `MulContext` / `ModContext` backends.
- **DivisionCorrectness.lean** — Correctness theorems for the division-style algorithms.
Expand Down
60 changes: 58 additions & 2 deletions CompPoly/Univariate/ToPoly/Degree.lean
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
/-
Copyright (c) 2025 CompPoly. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Quang Dao, Gregor Mitscha-Baude, Derek Sorensen, Julian Sutherland
Authors: Quang Dao, Gregor Mitscha-Baude, Derek Sorensen, Julian Sutherland, Alexander Hicks
-/
module

import all CompPoly.Univariate.ToPoly.Impl
public import CompPoly.Univariate.ToPoly.Impl

/-!
# `toPoly` Degree Lemmas
# Degrees and degree bounds under `toPoly`

Degree lemmas for the computable-univariate to `Polynomial` conversion.

`degreeLTEquiv` identifies computable polynomials of degree below `n` with their coefficient
functions `Fin n → R` by a linear equivalence.

The quantifier lemmas transport statements over `↥(degreeLT n)` to Mathlib polynomials.

* `exists_degreeLT_toPoly_iff` transports an existential quantifier with a degree bound.
* `forall_degreeLT_toPoly_iff` transports a universal quantifier with a degree bound.
* `forall₂_degreeLT_toPoly_iff` transports two universal quantifiers with separate bounds.
* `exists_degreeLT_toPoly_iff_of_degree_lt` drops the explicit bound when the predicate implies it.
-/

public section
Expand Down Expand Up @@ -188,6 +198,52 @@ theorem eval_eq_sum_degreeLTEquiv [DecidableEq R] {n : ℕ} {p : CPolynomial R}

end degreeLTEquiv

/-! ### Quantifiers over bounded-degree polynomials

`toPoly` maps `degreeLT n` onto the Mathlib polynomials of degree below `n`. The lemmas in this
section use this fact to restate a quantifier over `↥(degreeLT n)` as a quantifier over Mathlib
polynomials. They serve only in proofs, because `toPoly` is noncomputable. A typical use decides
an executable predicate `P` by a search over the enumeration `instFintypeDegreeLT`. Suppose that
`P p` holds exactly when `Q p.toPoly` holds. The lemmas then relate the search to the corresponding
quantifier over Mathlib polynomials of degree below `n`. -/

/-- Some polynomial of degree below `n` satisfies `Q` after `toPoly` exactly when some Mathlib
polynomial of degree below `n` satisfies `Q`. -/
theorem exists_degreeLT_toPoly_iff (n : ℕ) (Q : R[X] → Prop) :
(∃ p : ↥(degreeLT (R := R) n), Q p.val.toPoly) ↔ ∃ p : R[X], p.degree < n ∧ Q p := by
constructor
· rintro ⟨p, hQ⟩
exact ⟨p.val.toPoly, Polynomial.mem_degreeLT.mp (degreeLT_toPoly.mp p.property), hQ⟩
· rintro ⟨p, hp, hQ⟩
refine ⟨⟨⟨p.toImpl, Raw.isCanonical_toImpl p⟩, ?_⟩, by rwa [toPoly_mk_toImpl]⟩
rw [degreeLT_toPoly, toPoly_mk_toImpl]
exact Polynomial.mem_degreeLT.mpr hp

/-- Every polynomial of degree below `n` satisfies `Q` after `toPoly` exactly when every Mathlib
polynomial of degree below `n` satisfies `Q`. -/
theorem forall_degreeLT_toPoly_iff (n : ℕ) (Q : R[X] → Prop) :
(∀ p : ↥(degreeLT (R := R) n), Q p.val.toPoly) ↔ ∀ p : R[X], p.degree < n → Q p := by
simpa only [not_exists, not_and, not_not] using (exists_degreeLT_toPoly_iff n (¬ Q ·)).not

/-- Transport two universal quantifiers through `toPoly`. The first polynomial has degree below
`m`, and the second has degree below `n`. -/
theorem forall₂_degreeLT_toPoly_iff (m n : ℕ) (Q : R[X] → R[X] → Prop) :
(∀ f : ↥(degreeLT (R := R) m), ∀ g : ↥(degreeLT (R := R) n),
Q f.val.toPoly g.val.toPoly) ↔
∀ f g : R[X], f.degree < m → g.degree < n → Q f g := by
simp only [forall_degreeLT_toPoly_iff n (Q _),
forall_degreeLT_toPoly_iff m fun f ↦ ∀ g : R[X], g.degree < n → Q f g]
exact forall_congr' fun _ ↦ forall_comm

/-- Suppose that every Mathlib polynomial that satisfies `Q` has degree below `n`. Then some
polynomial of degree below `n` satisfies `Q` after `toPoly` exactly when some Mathlib polynomial
satisfies `Q`. -/
theorem exists_degreeLT_toPoly_iff_of_degree_lt (n : ℕ) (Q : R[X] → Prop)
(hQ : ∀ p, Q p → p.degree < n) :
(∃ p : ↥(degreeLT (R := R) n), Q p.val.toPoly) ↔ ∃ p : R[X], Q p :=
(exists_degreeLT_toPoly_iff n Q).trans
⟨fun ⟨p, _, hp⟩ ↦ ⟨p, hp⟩, fun ⟨p, hp⟩ ↦ ⟨p, hQ p hp, hp⟩⟩

end LinearEquiv

section OfFinCoeff
Expand Down
14 changes: 6 additions & 8 deletions docs/wiki/field-extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -405,14 +405,12 @@ invariant to maintain — the bound is *structural*, a consequence of the length
carried alongside the data. As a result this subtree is **independent of the `CPolynomial`
stack**: nothing under `CompPoly/Fields/Extension/` imports `CompPoly/Univariate/`.

The one place a degree bound is needed on the *polynomial* side — showing that the representative
`Ext.toQuot` picks is the canonical degree-`< d` one — is proved directly in
[`Extension/Bridge.lean`](../../CompPoly/Fields/Extension/Bridge.lean) as `degree_repr_lt`, from
`Polynomial.degree_sum_le` and `Polynomial.degree_C_mul_X_pow_le`. If you want the
`CPolynomial`-side theory instead (`degreeLT`, `degreeLTEquiv` in
[`Univariate/Linear.lean`](../../CompPoly/Univariate/Linear.lean) and
[`Univariate/ToPoly/Degree.lean`](../../CompPoly/Univariate/ToPoly/Degree.lean)), it exists but is
**not** wired to this framework; connecting them would be new work.
`Ext.toQuot` picks a canonical representative of degree below `d`. The theorem `degree_repr_lt`
in [`Extension/Bridge.lean`](../../CompPoly/Fields/Extension/Bridge.lean) proves the degree bound
from `Polynomial.degree_sum_le` and `Polynomial.degree_C_mul_X_pow_le`.
The separate `CPolynomial` API defines `degreeLT` in
[`Univariate/Linear.lean`](../../CompPoly/Univariate/Linear.lean) and `degreeLTEquiv` in
[`Univariate/ToPoly/Degree.lean`](../../CompPoly/Univariate/ToPoly/Degree.lean).

`ExtensionParams` carries `d`, the modulus's lower coefficients, and a proposed base cardinality
`q` as a *type index*. With fixed operations on the coefficient type, different parameter
Expand Down
1 change: 1 addition & 0 deletions tests/CompPolyTests.lean
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,4 @@ public import CompPolyTests.Univariate.Roots.FiniteField
public import CompPolyTests.Univariate.Roots.LasVegas
public import CompPolyTests.Univariate.Roots.Shoup
public import CompPolyTests.Univariate.ToPoly
public import CompPolyTests.Univariate.ToPoly.Degree
Loading
Loading