Skip to content

feat(ConstraintSystem): universal CS abstraction with Hom and BehavioralContract - #465

Open
quangvdao wants to merge 3 commits into
mainfrom
quang/constraint-system
Open

feat(ConstraintSystem): universal CS abstraction with Hom and BehavioralContract#465
quangvdao wants to merge 3 commits into
mainfrom
quang/constraint-system

Conversation

@quangvdao

Copy link
Copy Markdown
Collaborator

Summary

Introduce a small theory of constraint systems that unifies the indexed relations used across ArkLib (R1CS, Plonkish, lookups, memory checking, AIR, CCS, and DSL-level systems such as Clean's FormalCircuit), so protocols and compilers can target a single universal interface.

Three goals:

  1. Unify — capture the common shape (index, statement, oracle statement, witness, satisfies) so that one abstract CS value covers an entire family (e.g. R1CS of every size) rather than one value per parameter choice.
  2. ComposeConstraintSystem.Hom transports satisfiability along index/statement/witness maps, modelling reductions between constraint systems (e.g. Clean gadget → R1CS row block, Plonkish → CCS, plain R1CS → padded R1CS). Name chosen per Mathlib convention (RelHom, RingHom, LinearMap).
  3. Extend with behaviourBehavioralContract pairs a CS with user-facing Assumptions + Spec plus soundness and completeness proofs, mirroring the FormalCircuit pattern from Clean. This gives us a native ArkLib target for DSL-level systems without a language-specific Reduction bridge.

Files

New

  • ArkLib/ProofSystem/ConstraintSystem/Basic.lean

    • structure ConstraintSystem bundling Index, Stmt, OStmt, Wit, satisfies.
    • Projections relation, IsSatisfiable.
    • Smart constructors ofRelation (no oracle slot) and ofWitnessFree.
    • structure Hom with index, stmt, oStmt, wit, preserves.
    • Hom.id, Hom.comp, id_comp, comp_id, comp_assoc (all by rfl), Hom.isSatisfiable_map.
    • structure BehavioralContract with Assumptions, Spec, soundness, completeness, plus isSatisfiable_of_assumptions.
  • ArkLib/ProofSystem/ConstraintSystem/Examples.lean

    • R1CS.toConstraintSystem (R : Type*) [CommSemiring R] : ConstraintSystem.
    • Plonk.Shape + Plonk.toConstraintSystem (𝓡 : Type) [CommRing 𝓡] : ConstraintSystem.
    • Iff.rfl sanity lemmas confirming agreement with the existing concrete relations.

Modified

  • ArkLib/ProofSystem/ConstraintSystem/Plonk.leanPlonk.ConstraintSystem marked protected so bare ConstraintSystem inside namespace Plonk resolves to the universal one. Internal uses qualified to Plonk.ConstraintSystem (accepts, partition, perm, isPreparedFor, relation).
  • ArkLib.lean — umbrella imports regenerated via scripts/update-lib.sh.

Notes

  • Morphisms are one-way (completeness-preserving). Soundness-reflecting variants (embeddings, extractors, isos) are deferred.
  • OStmt is a single Type*; bundle multiple oracle slots via a dependent function type inside (e.g. MatrixIdx → Matrix _ _ R for R1CS).
  • This supersedes the Clean-specific Reduction bridge in feat(Bridge): Clean to ArkLib bridge with completeness and structural soundness #451: a Clean FormalCircuit is naturally a BehavioralContract over an appropriate constraint system, without introducing a protocol-level wrapper.

Test plan

  • ./scripts/validate.sh passes (3630 jobs, no new warnings beyond pre-existing sorries).
  • R1CS.toConstraintSystem_satisfies and Plonk.toConstraintSystem_satisfies confirm definitional agreement with existing relations.

Opened by Cursor assistant (model: Opus 4.7) on behalf of the user (Quang Dao) with approval.

Made with Cursor

…ralContract

Introduces a small theory of constraint systems that unifies the indexed relations used
across ArkLib (R1CS, Plonkish, lookups, memory checking, AIR, CCS, and DSL-level systems
such as Clean's FormalCircuit).

ArkLib/ProofSystem/ConstraintSystem/Basic.lean (new):
- `ConstraintSystem` bundling Index, Stmt, OStmt, Wit, satisfies.
- Smart constructors `ofRelation` (no oracle slot) and `ofWitnessFree`.
- `ConstraintSystem.Hom` — completeness-preserving morphism with `index`, `stmt`, `oStmt`,
  `wit`, and `preserves`. Name follows Mathlib convention (RelHom, RingHom, LinearMap).
- `Hom.id`, `Hom.comp`, and the category laws `id_comp`, `comp_id`, `comp_assoc` (all rfl).
- `Hom.isSatisfiable_map` lifting satisfiability along a morphism.
- `BehavioralContract` — per-index contract with `Assumptions`, `Spec`, soundness, and
  completeness, mirroring Clean's `FormalCircuit` pattern.

ArkLib/ProofSystem/ConstraintSystem/Examples.lean (new):
- `R1CS.toConstraintSystem` and `Plonk.toConstraintSystem` (+ `Plonk.Shape`) wiring the
  existing concrete relations into the universal abstraction, with `Iff.rfl` sanity
  lemmas.

ArkLib/ProofSystem/ConstraintSystem/Plonk.lean:
- `Plonk.ConstraintSystem` marked `protected` so bare `ConstraintSystem` inside namespace
  `Plonk` resolves to the universal one. Internal uses qualified to
  `Plonk.ConstraintSystem`.

ArkLib.lean regenerated via scripts/update-lib.sh.

Made-with: Cursor
@github-actions

github-actions Bot commented Apr 18, 2026

Copy link
Copy Markdown
Contributor

🤖 PR Summary

Universal Constraint System Theory

  • Defines ConstraintSystem, a foundational structure bundling Index, Stmt, OStmt, Wit, and a satisfaction relation.
  • Introduces ConstraintSystem.Hom to model completeness-preserving morphisms between systems, enabling satisfiability transport via isSatisfiable_map.
  • Establishes the BehavioralContract framework, pairing a ConstraintSystem with Assumptions, Spec, and proofs of soundness and completeness.

Refactoring and Integration

  • Bridges existing indexed relations to the universal interface through R1CS.toConstraintSystem and Plonk.toConstraintSystem constructors.
  • Marks Plonk.ConstraintSystem as protected in ArkLib/ProofSystem/ConstraintSystem/Plonk.lean and qualifies internal references to resolve namespace conflicts.
  • Regenerates top-level library imports in ArkLib.lean to incorporate new core modules.

Proof Status

  • CRITICAL: A sorry placeholder remains in the perm definition within ArkLib/ProofSystem/ConstraintSystem/Plonk.lean.

Statistics

Metric Count
📝 Files Changed 4
Lines Added 316
Lines Removed 6

Lean Declarations

✏️ **Added:** 15 declaration(s)
  • def relation (i : C.Index) : Set (C.Stmt i × C.OStmt i × C.Wit i) in ArkLib/ProofSystem/ConstraintSystem/Basic.lean
  • @[simp] theorem id_comp (f : Hom C D) : (id D).comp f = f in ArkLib/ProofSystem/ConstraintSystem/Basic.lean
  • def comp {C D E : ConstraintSystem.{u, v, w}} (g : Hom D E) (f : Hom C D) : Hom C E where in ArkLib/ProofSystem/ConstraintSystem/Basic.lean
  • def ofRelation in ArkLib/ProofSystem/ConstraintSystem/Basic.lean
  • theorem Hom.isSatisfiable_map {C D : ConstraintSystem.{u, v, w}} (f : Hom C D) in ArkLib/ProofSystem/ConstraintSystem/Basic.lean
  • def IsSatisfiable (i : C.Index) (s : C.Stmt i) : Prop in ArkLib/ProofSystem/ConstraintSystem/Basic.lean
  • def ofWitnessFree in ArkLib/ProofSystem/ConstraintSystem/Basic.lean
  • @[simp] theorem R1CS.toConstraintSystem_satisfies in ArkLib/ProofSystem/ConstraintSystem/Examples.lean
  • @[simp] theorem comp_id (f : Hom C D) : f.comp (id C) = f in ArkLib/ProofSystem/ConstraintSystem/Basic.lean
  • def Plonk.toConstraintSystem (𝓡 : Type) [CommRing 𝓡] : ConstraintSystem where in ArkLib/ProofSystem/ConstraintSystem/Examples.lean
  • def R1CS.toConstraintSystem (R : Type*) [CommSemiring R] : ConstraintSystem where in ArkLib/ProofSystem/ConstraintSystem/Examples.lean
  • theorem comp_assoc (h : Hom E F) (g : Hom D E) (f : Hom C D) : in ArkLib/ProofSystem/ConstraintSystem/Basic.lean
  • @[simp] theorem Plonk.toConstraintSystem_satisfies in ArkLib/ProofSystem/ConstraintSystem/Examples.lean
  • def id (C : ConstraintSystem.{u, v, w}) : Hom C C where in ArkLib/ProofSystem/ConstraintSystem/Basic.lean
  • theorem isSatisfiable_of_assumptions {s : C.Stmt i} (hs : B.Assumptions s) : in ArkLib/ProofSystem/ConstraintSystem/Basic.lean
✏️ **Affected:** 4 declaration(s) (line number changed)
  • def partition (cs : Plonk.ConstraintSystem 𝓡 numWires numGates) : in ArkLib/ProofSystem/ConstraintSystem/Plonk.lean moved from L133 to L137
  • def relation (cs : Plonk.ConstraintSystem 𝓡 numWires numGates) (ℓ : ℕ) (hℓ : ℓ ≤ numWires) : in ArkLib/ProofSystem/ConstraintSystem/Plonk.lean moved from L161 to L166
  • def perm (cs : Plonk.ConstraintSystem 𝓡 numWires numGates) : Equiv.Perm (Fin (3 * numGates)) in ArkLib/ProofSystem/ConstraintSystem/Plonk.lean moved from L145 to L149
  • protected def ConstraintSystem (𝓡 : Type) (numWires numGates : ℕ) in ArkLib/ProofSystem/ConstraintSystem/Plonk.lean moved from L111 to L115

sorry Tracking

✏️ **Affected:** 1 `sorry`(s) (line number changed)
  • def perm (cs : Plonk.ConstraintSystem 𝓡 numWires numGates) : Equiv.Perm (Fin (3 * numGates)) in ArkLib/ProofSystem/ConstraintSystem/Plonk.lean moved from L145 to L150

🎨 **Style Guide Adherence**

There are 35 style guide violations in the provided changes. They are grouped by rule below.

Functions and Terms: lowerCamelCase

  • Violation Count: 15
  • Representative Examples:
    • ArkLib/ProofSystem/ConstraintSystem/Basic.lean (Line 50, 52, 55, 59): The structure fields Index, Stmt, OStmt, and Wit are terms/projections and must use lowerCamelCase (e.g., index, stmt, oStmt, wit).
    • ArkLib/ProofSystem/ConstraintSystem/Basic.lean (Line 75): The predicate IsSatisfiable violates the lowerCamelCase rule for functions and terms (should be isSatisfiable).
    • ArkLib/ProofSystem/ConstraintSystem/Basic.lean (Line 178, 180): The structure fields Assumptions and Spec in BehavioralContract must be lowerCamelCase (e.g., assumptions, spec).
    • ArkLib/ProofSystem/ConstraintSystem/Examples.lean (Line 33-36): Structure instance labels Index, Stmt, OStmt, and Wit must use lowerCamelCase.

Functions: Prefer fun x ↦ ... over λ x, ...

  • Violation Count: 20
  • Representative Examples:
    • ArkLib/ProofSystem/ConstraintSystem/Basic.lean (Line 69): OStmt := fun _ => PUnit uses => instead of the preferred symbol.
    • ArkLib/ProofSystem/ConstraintSystem/Basic.lean (Line 138): stmt := fun i s => g.stmt (f.index i) (f.stmt i s) uses => instead of .
    • ArkLib/ProofSystem/ConstraintSystem/Examples.lean (Line 72): satisfies := fun s stmt cs wit => ... uses => instead of .
    • ArkLib/ProofSystem/ConstraintSystem/Plonk.lean (Line 166): fun (x : Fin ℓ → 𝓡) (ω : Fin (numWires - ℓ) → 𝓡) => uses => instead of .

📄 **Per-File Summaries**
  • ArkLib.lean: This update expands the ArkLib library by importing new modules for basic constraint system definitions and examples. These changes integrate fundamental components and illustrative use cases into the library's top-level structure.
  • ArkLib/ProofSystem/ConstraintSystem/Basic.lean: This file introduces a unified abstraction for constraint systems in Lean 4, defining the core ConstraintSystem structure alongside morphisms (Hom) that model completeness-preserving reductions. It also establishes a BehavioralContract framework to pair constraint systems with high-level specifications and provides foundational theorems for satisfiability without any sorry or admit placeholders.
  • ArkLib/ProofSystem/ConstraintSystem/Examples.lean: This file introduces new definitions and theorems that adapt R1CS and Plonkish relations into the universal ConstraintSystem abstraction. It provides concrete instances via R1CS.toConstraintSystem and Plonk.toConstraintSystem, accompanied by verification lemmas to ensure consistency with their original relations, and contains no sorry placeholders.
  • ArkLib/ProofSystem/ConstraintSystem/Plonk.lean: The definition of ConstraintSystem in ArkLib/ProofSystem/ConstraintSystem/Plonk.lean is now marked protected to resolve naming conflicts with the universal ConstraintSystem within the Plonk namespace. Several dependent definitions, including accepts, partition, and relation, were updated to reflect this change, while the perm definition still includes a sorry placeholder.

Last updated: 2026-04-28 16:32 UTC.

@github-actions

github-actions Bot commented Apr 18, 2026

Copy link
Copy Markdown
Contributor

Build Timing Report

  • Commit: 3eae2fc
  • Message: Merge 15e76b2 into 0a67335
  • Ref: quang/constraint-system
  • Comparison baseline: 91f92a5 from the previous successful PR update.
  • Measured on ubuntu-latest with /usr/bin/time -p.
  • Commands: clean build rm -rf .lake/build && lake build; warm rebuild lake build; validation wrapper ./scripts/validate.sh.
Measurement Baseline (s) Current (s) Delta (s) Status
Clean build 768.38 803.48 +35.10 ok
Warm rebuild 2.52 2.43 -0.09 ok
Validation wrapper 2.01 2.09 +0.08 ok

Incremental Rebuild Signal

  • Warm rebuild saved 801.05s vs clean (330.65x faster).

This compares a clean project build against an incremental rebuild in the same CI job; it is a lightweight variability signal, not a full cross-run benchmark.

Slowest Current Clean-Build Files

Showing 20 slowest current targets, with comparison against the selected baseline when available.

Current (s) Baseline (s) Delta (s) Path
359.00 335.00 +24.00 ArkLib/ProofSystem/Component/RandomQuery.lean
54.00 64.00 -10.00 ArkLib/Data/CodingTheory/JohnsonBound/Lemmas.lean
52.00 59.00 -7.00 ArkLib/Data/CodingTheory/GuruswamiSudan/Basic.lean
52.00 51.00 +1.00 ArkLib/Data/CodingTheory/ProximityGap/BCIKS20/AffineLines/BWMatrix.lean
51.00 36.00 +15.00 ArkLib/OracleReduction/Security/RoundByRound.lean
47.00 45.00 +2.00 ArkLib/OracleReduction/LiftContext/Reduction.lean
45.00 49.00 -4.00 ArkLib/Data/CodingTheory/BerlekampWelch/Condition.lean
45.00 36.00 +9.00 ArkLib/ProofSystem/Fri/Spec/SingleRound.lean
44.00 4.90 +39.10 ArkLib/Data/CodingTheory/ProximityGap/AHIV22.lean
38.00 30.00 +8.00 ArkLib/Data/CodingTheory/JohnsonBound/Basic.lean
36.00 35.00 +1.00 ArkLib/Data/CodingTheory/ReedSolomon/FftDomain.lean
32.00 32.00 +0.00 ArkLib/Data/CodingTheory/ProximityGap/DG25/MainResults.lean
30.00 32.00 -2.00 ArkLib/Data/CodingTheory/DivergenceOfSets.lean
27.00 - - ArkLib/Data/Polynomial/FoldingPolynomial.lean
25.00 20.00 +5.00 ArkLib/Data/CodingTheory/PolishchukSpielman/Existence.lean
25.00 21.00 +4.00 ArkLib/Data/Polynomial/SplitFold.lean
24.00 23.00 +1.00 ArkLib/Data/CodingTheory/ProximityGap/BCIKS20/AffineLines/GoodCoeffs.lean
24.00 23.00 +1.00 ArkLib/ProofSystem/BatchedFri/Security.lean
22.00 25.00 -3.00 ArkLib/Data/CodingTheory/PolishchukSpielman/Resultant.lean
21.00 12.00 +9.00 ArkLib/OracleReduction/Security/Implications.lean

@github-actions

Copy link
Copy Markdown
Contributor

🤖 AI Review

Overall Summary:
An error occurred while synthesizing the summary: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'You have reached your specified workspace API usage limits. You will regain access on 2026-05-01 at 00:00 UTC.'}, 'request_id': 'req_011CaAUguY9fg9QWAz2nc9gj'}


Errors during review:

  • Agent B failed for ArkLib.lean
  • Agent B failed for ArkLib/ProofSystem/ConstraintSystem/Basic.lean
  • Agent B failed for ArkLib/ProofSystem/ConstraintSystem/Examples.lean
  • Agent B failed for ArkLib/ProofSystem/ConstraintSystem/Plonk.lean

🔍 **Mechanical Pre-Check Results**

Escape hatches introduced in this PR (triggers hard verdict rule):

  • sorry introduced in ArkLib/ProofSystem/ConstraintSystem/Plonk.lean: sorry

Pre-existing escape hatches in touched files (context only, does not affect verdict):

  • sorry in ArkLib/ProofSystem/ConstraintSystem/Plonk.lean line 150: sorry
🔗 **Cross-File Analysis**

Cross-file analysis failed: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'You have reached your specified workspace API usage limits. You will regain access on 2026-05-01 at 00:00 UTC.'}, 'request_id': 'req_011CaAUgu6rn97a6ozDFgBVb'}

📄 **Review for `ArkLib.lean`**

An error occurred while analyzing ArkLib.lean: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'You have reached your specified workspace API usage limits. You will regain access on 2026-05-01 at 00:00 UTC.'}, 'request_id': 'req_011CaAUgsxeSiu7sPfAcS4RQ'}

📄 **Review for `ArkLib/ProofSystem/ConstraintSystem/Basic.lean`**

An error occurred while analyzing ArkLib/ProofSystem/ConstraintSystem/Basic.lean: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'You have reached your specified workspace API usage limits. You will regain access on 2026-05-01 at 00:00 UTC.'}, 'request_id': 'req_011CaAUgsqhxWwgT9HWjmnei'}

📄 **Review for `ArkLib/ProofSystem/ConstraintSystem/Examples.lean`**

An error occurred while analyzing ArkLib/ProofSystem/ConstraintSystem/Examples.lean: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'You have reached your specified workspace API usage limits. You will regain access on 2026-05-01 at 00:00 UTC.'}, 'request_id': 'req_011CaAUgtPwW1Q9ALgxW9K5q'}

📄 **Review for `ArkLib/ProofSystem/ConstraintSystem/Plonk.lean`**

An error occurred while analyzing ArkLib/ProofSystem/ConstraintSystem/Plonk.lean: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'You have reached your specified workspace API usage limits. You will regain access on 2026-05-01 at 00:00 UTC.'}, 'request_id': 'req_011CaAUgt3rj4SSgtEozPjjm'}

@XC0R

XC0R commented Apr 18, 2026

Copy link
Copy Markdown
Contributor

Closing #451 in favor of #466 and the sound ConstraintSystem abstraction from #465.

@quangvdao

Copy link
Copy Markdown
Collaborator Author

@mitschabaude what do you think about this PR? Just a generic layer for embedding constraint systems into ArkLib protocols

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants