Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
157 commits
Select commit Hold shift + click to select a range
a2b1ed4
feat(fields): secp256k1 base & scalar field in Montgomery
graikos Sep 29, 2026
20d201e
minor changes
graikos Sep 29, 2026
f5a4876
perf(fields): fused squaring and lazy exponent chains for fast Goldil…
graikos Sep 29, 2026
02ae337
perf(fields): inline conditionalSubtract in 32-bit Montgomery addition
graikos Sep 29, 2026
9695395
bench(fields): fast Goldilocks rows in univariate-dense, guards for u…
graikos Sep 29, 2026
7247bac
perf(fields): borrow-selected canonical addition for fast Goldilocks
graikos Sep 29, 2026
00317c1
Add matched small-field Lean and Plonky3 benchmarks
mitschabaude-bot Sep 29, 2026
e743eb3
Inline native 32-bit conditional modular subtraction
mitschabaude-bot Sep 29, 2026
44e8fa4
Merge remote-tracking branch 'origin/main' into rust-field-benchmarks
mitschabaude-bot Sep 29, 2026
d039d57
Fix benchmark CI cancellation and fork comment permissions
mitschabaude-bot Sep 29, 2026
4458711
Add BN254 scalar benchmarks and shared field suites
mitschabaude-bot Sep 29, 2026
0d2e67c
Remove trivial benchmark driver and encoding tests
mitschabaude-bot Sep 29, 2026
1535907
Compare all selected BN254 scalar operations with arkworks
mitschabaude-bot Sep 29, 2026
76f4a91
Use matched two-chain BN254 throughput workloads
mitschabaude-bot Sep 29, 2026
e4c30ae
feat(fields): Montgomery speedup by switching to 4x64-bit limbs
graikos Sep 29, 2026
95753cd
refactor(fields): minor changes
graikos Sep 29, 2026
7d02245
Use native signed-word coefficients in checked Montgomery inversion
mitschabaude-bot Sep 29, 2026
7c0bcd7
Compare Montgomery limbs from the most significant end
mitschabaude-bot Sep 29, 2026
d7449d3
Expose inline squaring in Montgomery exponentiation
mitschabaude-bot Sep 29, 2026
856f88e
Merge remote-tracking branch 'origin/pr-391' into rust-field-benchmarks
mitschabaude-bot Sep 29, 2026
bc2466a
perf(fields): use native divsteps in four-limb inversion
mitschabaude-bot Sep 29, 2026
4502b00
docs(bench): record four-limb optimization measurements
mitschabaude-bot Sep 29, 2026
937b435
perf(fields): branch on the rare borrow and shift-form middle term in…
graikos Sep 30, 2026
17a1eb9
perf(fields): reuse the accumulator in four-limb addition
mitschabaude-bot Sep 30, 2026
ba8b8b8
style(fields): apply fast-field conventions to the Goldilocks speedup
graikos Sep 30, 2026
b69a497
perf(fields): expose one-bit carry and borrow to code generation
mitschabaude-bot Sep 30, 2026
7912071
perf(fields): omit impossible addition carry for spare-bit moduli
mitschabaude-bot Sep 30, 2026
a4dd24a
feat(fields): expose scalar limb arithmetic for hot loops
mitschabaude-bot Sep 30, 2026
79a3ea0
perf(fields): optimize four-limb squaring and exponentiation
mitschabaude-bot Sep 30, 2026
770f992
merge: incorporate Goldilocks optimizations from PR #392
mitschabaude-bot Sep 30, 2026
546982d
docs(bench): record Goldilocks merge measurements
mitschabaude-bot Sep 30, 2026
1aa011e
feat(bench): compare binary tower fields against Binius
mitschabaude-bot Sep 30, 2026
c297b33
docs(bench): record binary tower inline trials
mitschabaude-bot Sep 30, 2026
1e1518a
feat(bench): add binary array addition throughput
mitschabaude-bot Sep 30, 2026
777bb74
refactor(bench): remove binary array addition workloads
mitschabaude-bot Sep 30, 2026
4075fb8
fix(bench): verify provenance before reusing field binaries
mitschabaude-bot Sep 30, 2026
c0c37ae
fix(bench): restore original author attribution
mitschabaude-bot Sep 30, 2026
8b98fdf
docs(bench): credit Gregor alongside original author
mitschabaude-bot Sep 30, 2026
e0a69b9
docs: credit significant field and benchmark contributions
mitschabaude-bot Sep 30, 2026
d172ddf
perf(univariate): add verified parallel evaluation and Rust benchmarks
mitschabaude-bot Oct 1, 2026
7ed54fb
docs(bench): show parallel Lean to Rust ratios
mitschabaude-bot Oct 1, 2026
aa0b316
perf(poly): use matched lazy arithmetic in parallel evaluation
mitschabaude-bot Oct 1, 2026
45d6384
perf(poly): scalarize BN254 lazy evaluation and preserve widening pro…
mitschabaude-bot Oct 1, 2026
deccc8f
perf(fields): preserve widening products in canonical Montgomery kernels
mitschabaude-bot Oct 1, 2026
fac96a9
bench(ntt): add matched KoalaBear forward and inverse transforms
mitschabaude-bot Oct 1, 2026
8a34661
bench(ntt): compare against parallel SIMD Plonky3 transforms
mitschabaude-bot Oct 1, 2026
51bb43a
perf(ntt): cache ordering and prove machine-index butterfly loops
mitschabaude-bot Oct 1, 2026
2651370
docs(ntt): record storage and compiler performance experiments
mitschabaude-bot Oct 2, 2026
58d39a7
perf(ntt): prove and reuse in-place bit reversal
mitschabaude-bot Oct 2, 2026
5575d85
docs(bench): record fused NTT and physical-core comparisons
mitschabaude-bot Oct 2, 2026
72c470a
docs(bench): minimize NTT storage externs and compare verified Lean
mitschabaude-bot Oct 2, 2026
b41a49b
docs(bench): record fused NTT input and parallel normalization
mitschabaude-bot Oct 2, 2026
449499e
fix(ntt): select optimized loops explicitly
mitschabaude-bot Oct 2, 2026
79d6708
perf(ntt): optimize verified externless stage loops
mitschabaude-bot Oct 2, 2026
8ad5024
feat(ntt): add proved parallel array transforms
mitschabaude-bot Oct 2, 2026
145fc66
docs(ntt): record proved parallel benchmark results
mitschabaude-bot Oct 2, 2026
5c70fef
docs(ntt): record selected native buffer collection
mitschabaude-bot Oct 2, 2026
dbc2481
feat(ntt): prove the packed parallel FFT pipeline
mitschabaude-bot Oct 2, 2026
d017a2b
docs(bench): replace local NTT experiment source references
mitschabaude-bot Oct 3, 2026
d7de410
perf(fields): preserve widening products in canonical Montgomery kernels
mitschabaude-bot Oct 1, 2026
7e4459e
refactor(fields): share the unnormalized Montgomery product and proof
mitschabaude-bot Oct 6, 2026
e73b7ec
merge: inherit canonical Montgomery improvements from PR #390
mitschabaude-bot Oct 6, 2026
0b1d9f8
refactor(poly): reuse the shared Montgomery product and proof
mitschabaude-bot Oct 6, 2026
fe37ea8
perf(ntt): parallel natural-order output and packed I/O for the packe…
mitschabaude-bot Oct 6, 2026
46e67fa
perf(ntt): choose the packed task depth from the transform size
mitschabaude-bot Oct 6, 2026
c40bfb5
perf(ntt): sliced split tree with one-pass paired kernels
mitschabaude-bot Oct 6, 2026
48580e6
chore: remove a local scratch executable from the lakefile
mitschabaude-bot Oct 6, 2026
840e70c
chore: restore the lakefile's final newline
mitschabaude-bot Oct 6, 2026
695f468
perf(ntt): reverse each leaf inside its leaf task
mitschabaude-bot Oct 6, 2026
1f8cd2d
perf(ntt): store kernel batches at machine indices
mitschabaude-bot Oct 6, 2026
0975ff6
docs(bench): record the rejected early output allocation
mitschabaude-bot Oct 6, 2026
e04766f
perf(ntt): column-parallel externless natural-order transform
mitschabaude-bot Oct 6, 2026
9267c15
perf(ntt): hand externless column and leaf results over as raw words
mitschabaude-bot Oct 6, 2026
7b41f20
chore: add Data.Bytes.Words to CompPoly.lean
mitschabaude-bot Oct 6, 2026
a88fabb
docs(bench): describe the column-parallel externless NTT in the report
mitschabaude-bot Oct 6, 2026
533a4ee
perf(ntt): column path for even externless transforms from 2^16
mitschabaude-bot Oct 6, 2026
f846670
perf(ntt): reverse externless leaves straight into their byte arrays
mitschabaude-bot Oct 6, 2026
26fe36f
bench(ntt): default to physical cores and report 1/4/8-worker scaling
mitschabaude-bot Oct 6, 2026
a29fa7e
bench(poly-eval): default to physical cores and report 4/8-worker res…
mitschabaude-bot Oct 7, 2026
442c7d8
perf(ntt): fuse the packed leaves' final layers with their reversal
mitschabaude-bot Oct 7, 2026
96c901a
perf(ntt): check split-loop ranges once and step machine indices
mitschabaude-bot Oct 7, 2026
8c99e06
perf(ntt): check leaf-pass batch ranges once and step machine indices
mitschabaude-bot Oct 7, 2026
6fa44c2
chore: move parallel polynomial evaluation out of the NTT PR
mitschabaude-bot Oct 7, 2026
f4bb734
bench(ntt): warm parallel transforms for 200 ms on both sides
mitschabaude-bot Oct 7, 2026
eefd2cd
docs(bench): record NTT results with the longer warmup
mitschabaude-bot Oct 7, 2026
3f60843
feat(fields): fast KoalaBear quartic extension
mitschabaude-bot Oct 7, 2026
6e65b0c
feat(poly): coset barycentric evaluation theorem and batch inversion
mitschabaude-bot Oct 7, 2026
03c5357
feat(fields): proved reference coset interpolation for KoalaBear
mitschabaude-bot Oct 7, 2026
dc948a8
bench(interpolate): compare coset interpolation against Plonky3
mitschabaude-bot Oct 7, 2026
12e6255
perf(fields): coset interpolation over packed words, proved equal to …
mitschabaude-bot Oct 7, 2026
4263995
perf(fields): 512-row interpolation blocks
mitschabaude-bot Oct 7, 2026
ee58d46
bench(interpolate): time the packed path on Montgomery words
mitschabaude-bot Oct 7, 2026
24ef151
docs(bench): record packed coset interpolation against Plonky3
mitschabaude-bot Oct 7, 2026
aad5149
refactor(ntt): replace the storage externs with core's proposed ByteA…
mitschabaude-bot Oct 8, 2026
12a5003
perf(ntt): write kernel outputs in place into zero-filled buffers
mitschabaude-bot Oct 8, 2026
828e1f8
perf(ntt): fill transpose zeros per tile and encode by overwriting
mitschabaude-bot Oct 8, 2026
6d4d831
docs(bench): record the NTT on core's proposed word accessors with in…
mitschabaude-bot Oct 8, 2026
130c36a
Merge branch 'grant-closeout-followup' into koalabear-fast-ext4
mitschabaude-bot Oct 9, 2026
52591ef
refactor(ntt): remove storage leftovers from the append-based outputs
mitschabaude-bot Oct 9, 2026
d590b47
Merge branch 'grant-closeout-followup' into koalabear-fast-ext4
mitschabaude-bot Oct 9, 2026
f3ea253
test(interpolate): drop the compiled packed-interpolation smoke checks
mitschabaude-bot Oct 9, 2026
bd6d098
refactor(storage): one packed-array representation for fast field arrays
mitschabaude-bot Oct 11, 2026
cf727c3
Merge branch 'grant-closeout-followup' into koalabear-fast-ext4
mitschabaude-bot Oct 11, 2026
5acf713
Merge branch 'grant-closeout-followup' into koalabear-fast-ext4
mitschabaude-bot Oct 11, 2026
286b10f
fix(storage): keep PackedRepr word instances on core byte operations
mitschabaude-bot Oct 12, 2026
9bb2ce4
refactor(ntt): packed plan takes and returns packed words only
mitschabaude-bot Oct 12, 2026
8f89634
feat(ntt): packed API for the extern-free natural-order plan
mitschabaude-bot Oct 12, 2026
600173d
Merge branch 'grant-closeout-followup' into koalabear-fast-ext4
mitschabaude-bot Oct 12, 2026
c4ba02f
refactor(ntt): one column path for every extern-free size from 2^15
mitschabaude-bot Oct 13, 2026
94f4739
refactor(ntt): one column pipeline for the packed plan
mitschabaude-bot Oct 13, 2026
621b821
Merge branch 'grant-closeout-followup' into koalabear-fast-ext4
mitschabaude-bot Oct 13, 2026
3443f6b
perf(ntt): odd leaves through the fused final layers
mitschabaude-bot Oct 13, 2026
1164a1e
refactor(ntt): shared task combinators; realign the kernels' stack
mitschabaude-bot Oct 13, 2026
a7b594f
Merge branch 'grant-closeout-followup' into koalabear-fast-ext4
mitschabaude-bot Oct 13, 2026
675aead
build: interpolation kernels in their own native library
mitschabaude-bot Oct 13, 2026
3675abd
build: correct the interpolation library comment
mitschabaude-bot Oct 13, 2026
dc2fc3a
refactor(ntt): generate the unrolled kernels
mitschabaude-bot Oct 13, 2026
66e2d5f
Merge branch 'grant-closeout-followup' into koalabear-fast-ext4
mitschabaude-bot Oct 13, 2026
0717283
refactor(ntt): a parallel flag instead of a worker count
mitschabaude-bot Oct 13, 2026
9ebe417
Merge branch 'grant-closeout-followup' into koalabear-fast-ext4
mitschabaude-bot Oct 13, 2026
70c3c5b
ci: rebuild host-tuned native objects after restoring the build cache
mitschabaude-bot Oct 13, 2026
4d13d90
Merge branch 'grant-closeout-followup' into koalabear-fast-ext4
mitschabaude-bot Oct 13, 2026
a865f65
Merge remote-tracking branch 'origin/main' into rust-field-benchmarks
mitschabaude-bot Oct 15, 2026
c485639
Merge branch 'rust-field-benchmarks' into grant-closeout-followup
mitschabaude-bot Oct 15, 2026
4569dc0
chore: build on Lean v4.35
mitschabaude-bot Oct 15, 2026
2dd4a7e
docs: the release archive carries no host-tuned objects
mitschabaude-bot Oct 15, 2026
b1b9cbd
Merge branch 'grant-closeout-followup' into koalabear-fast-ext4
mitschabaude-bot Oct 15, 2026
3187e65
chore: build on Lean v4.35
mitschabaude-bot Oct 15, 2026
373950f
feat(ntt): batched coset LDE over packed row-major KoalaBear matrices
mitschabaude-bot Oct 15, 2026
3e09608
feat(ntt): prove the batched coset LDE correct
mitschabaude-bot Oct 16, 2026
c4e48e7
feat(fields): generic fast quartic binomial extension over Mont32Field
mitschabaude-bot Oct 21, 2026
2b8a6f8
Merge branch 'koalabear-fast-ext4' into coset-lde
mitschabaude-bot Oct 21, 2026
5222691
feat(ntt): packed NTT generic over Mont32Field
mitschabaude-bot Oct 21, 2026
4631bb8
Merge branch 'grant-closeout-followup' into koalabear-fast-ext4
mitschabaude-bot Oct 21, 2026
0ea118e
feat(fields): coset interpolation generic over Mont32Field
mitschabaude-bot Oct 21, 2026
c220831
Merge branch 'koalabear-fast-ext4' into coset-lde
mitschabaude-bot Oct 21, 2026
6bd782e
feat(ntt): batched coset LDE generic over Mont32Field
mitschabaude-bot Oct 21, 2026
ac6a414
feat(ntt): coset LDE input in chunks of rows
mitschabaude-bot Oct 22, 2026
6af5846
Merge branch 'grant-closeout-followup' into koalabear-fast-ext4
mitschabaude-bot Oct 23, 2026
f2ad5b2
perf(tasks): join tasks without copying their results
mitschabaude-bot Oct 23, 2026
db200ad
Merge branch 'koalabear-fast-ext4' into coset-lde
mitschabaude-bot Oct 23, 2026
5441dc2
perf(fields): inline the fast quartic extensions' Binomial4 instances
mitschabaude-bot Oct 23, 2026
607b1b9
Merge branch 'koalabear-fast-ext4' into coset-lde
mitschabaude-bot Oct 23, 2026
b642583
perf(ntt): proved stores in the fused last layers, one range check pe…
mitschabaude-bot Oct 23, 2026
1667831
Merge branch 'grant-closeout-followup' into koalabear-fast-ext4
mitschabaude-bot Oct 23, 2026
b694374
Merge branch 'koalabear-fast-ext4' into coset-lde
mitschabaude-bot Oct 23, 2026
97e8273
perf(lde): vectorized in-place last layers, checked-once output tiles
mitschabaude-bot Oct 23, 2026
92c5d21
perf(lde): range checks of the in-place last layers and coset product…
mitschabaude-bot Oct 23, 2026
02fb3e1
docs(wiki): proved stores and checks once per loop in the packed kern…
mitschabaude-bot Oct 23, 2026
5a71723
perf(ntt): Montgomery products as the difference of the high words
mitschabaude-bot Oct 23, 2026
00092f3
perf(lde): the final forward layers as four in-place passes over grou…
mitschabaude-bot Oct 23, 2026
5f8387a
perf(lde): input transpose down blocks of 1024 rows before across
mitschabaude-bot Oct 23, 2026
c469bb4
perf(lde): coset products in an odd leaf's radix-two layer
mitschabaude-bot Oct 23, 2026
8b8662e
docs(wiki): Montgomery products by high words, leaf stages, blocked i…
mitschabaude-bot Oct 23, 2026
4c36c8f
perf(lde): input transposes by column range
mitschabaude-bot Oct 23, 2026
3e68e0b
perf(storage): word-sized loads and stores in the 32-bit packed repre…
mitschabaude-bot Oct 28, 2026
fd569e6
Merge remote-tracking branch 'origin/grant-closeout-followup' into co…
mitschabaude-bot Oct 28, 2026
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
22 changes: 14 additions & 8 deletions .github/workflows/benchmarks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,6 @@ on:
paths:
- 'bench/**'

# Keyed on the PR or ref rather than shared with CI, so asking for benchmarks
# never cancels a correctness run. A separate workflow file already gets a
# distinct `github.workflow`, so this only needs to disambiguate within itself.
concurrency:
group: ${{ github.workflow }}-${{ github.event.issue.number || github.event.pull_request.number || github.ref }}
cancel-in-progress: true

jobs:
benchmark:
# The COMMENT author, not the PR author, is the trust boundary on the
Expand All @@ -60,6 +53,12 @@ jobs:
github.event.comment.author_association == 'COLLABORATOR'
)
)
# Apply concurrency after the job's comment filter. Workflow-level concurrency
# would let an ordinary PR comment cancel a running benchmark even when its
# own benchmark job is skipped.
concurrency:
group: ${{ github.workflow }}-job-${{ github.event.issue.number || github.event.pull_request.number || github.ref }}
cancel-in-progress: true
runs-on: ubuntu-latest
timeout-minutes: 90
permissions:
Expand Down Expand Up @@ -135,6 +134,8 @@ jobs:
key: lake-build-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('lean-toolchain') }}-${{ hashFiles('lake-manifest.json') }}-${{ github.sha }}
restore-keys: |
lake-build-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('lean-toolchain') }}-${{ hashFiles('lake-manifest.json') }}
- name: Drop host-tuned native objects
run: ./scripts/drop-native-objects.sh
- name: Set up Lean environment
uses: leanprover/lean-action@v1
with:
Expand Down Expand Up @@ -226,7 +227,12 @@ jobs:
# Same marker-upsert shape as the build-timing comment in
# `lean_action_ci.yml`, so repeated runs replace rather than pile up.
- name: Upsert benchmark PR comment
if: always() && steps.target.outputs.pr != ''
# Fork pull_request tokens are read-only; keep their report in the
# Actions summary and artifact instead of failing on a comment write.
if: >-
always() && steps.target.outputs.pr != '' &&
(github.event_name != 'pull_request' ||
github.event.pull_request.head.repo.full_name == github.repository)
uses: actions/github-script@v7
with:
script: |
Expand Down
16 changes: 15 additions & 1 deletion .github/workflows/lean_action_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ jobs:
key: lake-build-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('lean-toolchain') }}-${{ hashFiles('lake-manifest.json') }}-${{ github.sha }}
restore-keys: |
lake-build-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('lean-toolchain') }}-${{ hashFiles('lake-manifest.json') }}
- name: Drop host-tuned native objects
run: ./scripts/drop-native-objects.sh
- name: Set up Lean environment
uses: leanprover/lean-action@v1
with:
Expand Down Expand Up @@ -185,7 +187,19 @@ jobs:
continue-on-error: true
run: lake build --wfail
- name: Build evaluation benchmark executable
run: lake build CompPolyBench
run: lake build CompPolyBench CompPolyFieldFixtures
- name: Install field benchmark Rust toolchain
working-directory: bench/rust
run: rustup show active-toolchain
- name: Test and build field Rust benchmarks
working-directory: bench/rust
env:
RUSTFLAGS: "-C target-cpu=native"
run: |
cargo test --release --locked -j 1
cargo build --release --locked -j 1
- name: Check Lean and Rust workload agreement
run: python3 scripts/bench-fields.py --suite all --validate-only
- name: Validate benchmark implementations
# Correctness only: every implementation in a group runs over the same
# inputs and must agree on a digest, so a wrong-but-fast implementation
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/.lake
.DS_Store
__pycache__/
73 changes: 73 additions & 0 deletions CompPoly.lean
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,16 @@ public import CompPoly.Bivariate.GuruswamiSudan.Root.ShiftedSubstitution.Lemmas
public import CompPoly.Bivariate.GuruswamiSudan.Util
public import CompPoly.Bivariate.Kronecker
public import CompPoly.Bivariate.ToPoly
public import CompPoly.Data.Array.Involution
public import CompPoly.Data.Array.Lemmas
public import CompPoly.Data.ByteArray.Pack
public import CompPoly.Data.ByteArray.Packed
public import CompPoly.Data.Bytes.Bias
public import CompPoly.Data.Bytes.CanonicalNat
public import CompPoly.Data.Bytes.Codec
public import CompPoly.Data.Bytes.Delimited
public import CompPoly.Data.Bytes.LittleEndian
public import CompPoly.Data.Bytes.UInt32
public import CompPoly.Data.Bytes.Vector
public import CompPoly.Data.Classes.CanonicalNat
public import CompPoly.Data.Classes.DCast
Expand All @@ -78,6 +82,7 @@ public import CompPoly.Data.ExtTreeMap.DTreeMap
public import CompPoly.Data.ExtTreeMap.ExtDTreeMap
public import CompPoly.Data.ExtTreeMap.ExtTreeMap
public import CompPoly.Data.Fin.BigOperators
public import CompPoly.Data.List.BatchInv
public import CompPoly.Data.List.Lemmas
public import CompPoly.Data.MvPolynomial.Notation
public import CompPoly.Data.Nat.Bitwise
Expand All @@ -90,6 +95,7 @@ public import CompPoly.Data.RingTheory.AlgebraTower
public import CompPoly.Data.RingTheory.AlgebraTower.Basis
public import CompPoly.Data.RingTheory.AlgebraTower.Coordinates
public import CompPoly.Data.RingTheory.CanonicalEuclideanDomain
public import CompPoly.Data.Tasks
public import CompPoly.Data.Vector.Basic
public import CompPoly.Fields.BLS12_377
public import CompPoly.Fields.BLS12_377.Basic
Expand All @@ -104,6 +110,8 @@ public import CompPoly.Fields.BabyBear
public import CompPoly.Fields.BabyBear.Basic
public import CompPoly.Fields.BabyBear.Ext4
public import CompPoly.Fields.BabyBear.Fast
public import CompPoly.Fields.BabyBear.FastExt4
public import CompPoly.Fields.BabyBear.InterpolateCoset
public import CompPoly.Fields.Basic
public import CompPoly.Fields.Binary.AdditiveNTT.AdditiveNTT
public import CompPoly.Fields.Binary.AdditiveNTT.Algorithm
Expand Down Expand Up @@ -169,6 +177,7 @@ public import CompPoly.Fields.Extension.Bytes
public import CompPoly.Fields.Extension.Cardinality
public import CompPoly.Fields.Extension.Defs
public import CompPoly.Fields.Extension.Field
public import CompPoly.Fields.Extension.Map
public import CompPoly.Fields.Goldilocks
public import CompPoly.Fields.Goldilocks.Basic
public import CompPoly.Fields.Goldilocks.Bytes
Expand All @@ -188,15 +197,28 @@ public import CompPoly.Fields.KoalaBear.Ext6.GaloisField
public import CompPoly.Fields.KoalaBear.Ext6.SexticCertData
public import CompPoly.Fields.KoalaBear.Ext6.SexticIrreducible
public import CompPoly.Fields.KoalaBear.Fast
public import CompPoly.Fields.KoalaBear.FastExt4
public import CompPoly.Fields.KoalaBear.InterpolateCoset
public import CompPoly.Fields.Mersenne31
public import CompPoly.Fields.Mersenne31.Basic
public import CompPoly.Fields.Mersenne31.Bytes
public import CompPoly.Fields.Mersenne31.Circle
public import CompPoly.Fields.Mersenne31.Fast
public import CompPoly.Fields.Montgomery.Basic
public import CompPoly.Fields.Montgomery.InterpolateCoset
public import CompPoly.Fields.Montgomery.InterpolateCosetPacked
public import CompPoly.Fields.Montgomery.InterpolateCosetPackedCorrectness
public import CompPoly.Fields.Montgomery.Native32
public import CompPoly.Fields.Montgomery.Native32Bytes
public import CompPoly.Fields.Montgomery.Native32Ext4
public import CompPoly.Fields.Montgomery.Native32Field
public import CompPoly.Fields.Montgomery.Native64x4
public import CompPoly.Fields.Montgomery.Native64x4Bytes
public import CompPoly.Fields.Montgomery.Native64x4Defs
public import CompPoly.Fields.Montgomery.Native64x4Field
public import CompPoly.Fields.Montgomery.Native64x4Inv
public import CompPoly.Fields.Montgomery.Native64x4InvDefs
public import CompPoly.Fields.Montgomery.Native64x4Mul
public import CompPoly.Fields.Montgomery.Native64x8
public import CompPoly.Fields.Montgomery.Native64x8Bytes
public import CompPoly.Fields.Montgomery.Native64x8Defs
Expand All @@ -210,6 +232,8 @@ public import CompPoly.Fields.Pasta.Basic
public import CompPoly.Fields.Pasta.Fast
public import CompPoly.Fields.PrattCertificate
public import CompPoly.Fields.Secp256k1
public import CompPoly.Fields.Secp256k1.Basic
public import CompPoly.Fields.Secp256k1.Fast
public import CompPoly.LinearAlgebra.Dense
public import CompPoly.LinearAlgebra.Dense.Basic
public import CompPoly.LinearAlgebra.Dense.Kernel
Expand Down Expand Up @@ -317,6 +341,7 @@ public import CompPoly.Univariate.Modular
public import CompPoly.Univariate.NTT.BabyBear
public import CompPoly.Univariate.NTT.Barycentric
public import CompPoly.Univariate.NTT.Coset
public import CompPoly.Univariate.NTT.CosetBarycentric
public import CompPoly.Univariate.NTT.Domain
public import CompPoly.Univariate.NTT.Evaluation
public import CompPoly.Univariate.NTT.FastMul
Expand All @@ -327,6 +352,10 @@ public import CompPoly.Univariate.NTT.Inverse
public import CompPoly.Univariate.NTT.Kernel
public import CompPoly.Univariate.NTT.KoalaBear
public import CompPoly.Univariate.NTT.Transform
public import CompPoly.Univariate.NTTFast.Butterfly
public import CompPoly.Univariate.NTTFast.ButterflyDIT
public import CompPoly.Univariate.NTTFast.Columns
public import CompPoly.Univariate.NTTFast.ColumnsCorrectness
public import CompPoly.Univariate.NTTFast.Correctness
public import CompPoly.Univariate.NTTFast.Correctness.Basic
public import CompPoly.Univariate.NTTFast.Correctness.DIF
Expand All @@ -339,7 +368,51 @@ public import CompPoly.Univariate.NTTFast.Evaluation
public import CompPoly.Univariate.NTTFast.FastMul
public import CompPoly.Univariate.NTTFast.FastMulLow
public import CompPoly.Univariate.NTTFast.Interpolation
public import CompPoly.Univariate.NTTFast.Natural
public import CompPoly.Univariate.NTTFast.NaturalParallel
public import CompPoly.Univariate.NTTFast.Packed.Arithmetic
public import CompPoly.Univariate.NTTFast.Packed.ArrayLemmas
public import CompPoly.Univariate.NTTFast.Packed.Arrays
public import CompPoly.Univariate.NTTFast.Packed.BatchCorrectness
public import CompPoly.Univariate.NTTFast.Packed.BitReversal
public import CompPoly.Univariate.NTTFast.Packed.ButterflyLemmas
public import CompPoly.Univariate.NTTFast.Packed.Columns
public import CompPoly.Univariate.NTTFast.Packed.ColumnsCorrectness
public import CompPoly.Univariate.NTTFast.Packed.CosetLde
public import CompPoly.Univariate.NTTFast.Packed.CosetLdeCorrectness
public import CompPoly.Univariate.NTTFast.Packed.CosetLdeTransposes
public import CompPoly.Univariate.NTTFast.Packed.Decoder
public import CompPoly.Univariate.NTTFast.Packed.DifSpec
public import CompPoly.Univariate.NTTFast.Packed.Expressions
public import CompPoly.Univariate.NTTFast.Packed.FieldStorage
public import CompPoly.Univariate.NTTFast.Packed.Folds
public import CompPoly.Univariate.NTTFast.Packed.Instances
public import CompPoly.Univariate.NTTFast.Packed.KernelCorrectness
public import CompPoly.Univariate.NTTFast.Packed.KernelModels
public import CompPoly.Univariate.NTTFast.Packed.KernelRefinement
public import CompPoly.Univariate.NTTFast.Packed.KernelSpecialization
public import CompPoly.Univariate.NTTFast.Packed.LeafLayers
public import CompPoly.Univariate.NTTFast.Packed.LeafStages
public import CompPoly.Univariate.NTTFast.Packed.LocalLayers
public import CompPoly.Univariate.NTTFast.Packed.LoopRefinement
public import CompPoly.Univariate.NTTFast.Packed.Native
public import CompPoly.Univariate.NTTFast.Packed.NativeLast
public import CompPoly.Univariate.NTTFast.Packed.NativeLastCorrectness
public import CompPoly.Univariate.NTTFast.Packed.NativeOrder
public import CompPoly.Univariate.NTTFast.Packed.NativeOrderCorrectness
public import CompPoly.Univariate.NTTFast.Packed.PassRefinement
public import CompPoly.Univariate.NTTFast.Packed.Plan
public import CompPoly.Univariate.NTTFast.Packed.QuadCells
public import CompPoly.Univariate.NTTFast.Packed.Radix2Refinement
public import CompPoly.Univariate.NTTFast.Packed.Radix4Lemmas
public import CompPoly.Univariate.NTTFast.Packed.Rows
public import CompPoly.Univariate.NTTFast.Packed.StorageLemmas
public import CompPoly.Univariate.NTTFast.Packed.Twiddles
public import CompPoly.Univariate.NTTFast.Packed.Unrolled
public import CompPoly.Univariate.NTTFast.Parallel
public import CompPoly.Univariate.NTTFast.Permutation
public import CompPoly.Univariate.NTTFast.Plan
public import CompPoly.Univariate.NTTFast.Reverse32
public import CompPoly.Univariate.Quotient.Core
public import CompPoly.Univariate.Quotient.Equiv
public import CompPoly.Univariate.Raw
Expand Down
140 changes: 140 additions & 0 deletions CompPoly/Data/Array/Involution.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/-
Copyright (c) 2026 CompPoly Contributors. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Gregor Mitscha-Baude
-/
module

public import Mathlib.Init
public import Init.Data.Range.Lemmas

/-! # Array permutations from index involutions

Swap each nontrivial pair once instead of allocating a second array for a gather.
-/

@[expose] public section
namespace Array
variable {α : Type*}

@[inline] private def involutionStep (f : Nat → Nat) (a : Array α) (i : Nat) : Array α :=
if i < f i then a.swapIfInBounds i (f i) else a

@[simp] private theorem size_involutionStep (f : Nat → Nat) (a : Array α) (i : Nat) :
(involutionStep f a i).size = a.size := by
simp only [involutionStep]; split <;> simp only [Array.size_swapIfInBounds]

private theorem size_fold (f : Nat → Nat) (is : List Nat) (a : Array α) :
(is.foldl (involutionStep f) a).size = a.size := by
induction is generalizing a with
| nil => rfl
| cons i is ih =>
simpa only [List.foldl_cons, size_involutionStep] using ih (involutionStep f a i)

private theorem getD_swap (a : Array α) (i j k : Nat) (d : α)
(hi : i < a.size) (hj : j < a.size) (hk : k < a.size) :
(a.swapIfInBounds i j).getD k d =
if k = i then a.getD j d else if k = j then a.getD i d else a.getD k d := by
simp only [Array.swapIfInBounds, hi, hj, ↓reduceDIte, Array.getD_eq_getD_getElem?,
Array.getElem?_eq_getElem (show k < (a.swap i j hi hj).size by simpa using hk),
Array.getElem?_eq_getElem hi, Array.getElem?_eq_getElem hj,
Array.getElem?_eq_getElem hk, Option.getD_some, Array.getElem_swap]

private theorem prefix_invariant (f : Nat → Nat) (a : Array α) (d : α)
(hb : ∀ i < a.size, f i < a.size)
(hf : ∀ i < a.size, f (f i) = i) (k : Nat) (hk : k ≤ a.size) :
∀ j < a.size, ((List.range k).foldl (involutionStep f) a).getD j d =
if min j (f j) < k then a.getD (f j) d else a.getD j d := by
induction k with
| zero => intro j hj; simp only [List.range_zero, List.foldl_nil, Nat.not_lt_zero, ↓reduceIte]
| succ k ih =>
have hk' : k < a.size := by omega
have ih := ih (by omega)
intro j hj
simp only [List.range_succ, List.foldl_append, List.foldl_cons, List.foldl_nil]
by_cases hswap : k < f k
· simp only [involutionStep, hswap, ↓reduceIte]
rw [getD_swap _ _ _ _ _ (by simpa only [size_fold] using hk')
(by simpa only [size_fold] using hb k hk') (by simpa only [size_fold] using hj)]
by_cases hji : j = k
· subst j
rw [ite_eq_left rfl, ih (f k) (hb k hk'), hf k hk']
have hmin1 : ¬ min (f k) k < k := by omega
have hmin2 : min k (f k) < k + 1 := by omega
simp only [hmin1, hmin2, ↓reduceIte]
· rw [ite_eq_right hji]
by_cases hjf : j = f k
· subst j
rw [ite_eq_left rfl, ih k hk', hf k hk']
have hmin1 : ¬ min k (f k) < k := by omega
have hmin2 : min (f k) k < k + 1 := by omega
simp only [hmin1, hmin2, ↓reduceIte]
· rw [ite_eq_right hjf, ih j hj]
have hfj : f j ≠ k := by
intro he
have hh := hf j hj
simp only [he] at hh
exact hjf hh.symm
have he : (min j (f j) < k) = (min j (f j) < k + 1) := by
apply propext; omega
simp only [he]
· simp only [involutionStep, hswap, ↓reduceIte]
rw [ih j hj]
by_cases hfix : f j = j
· simp only [hfix, ite_self]
· have hmin : min j (f j) ≠ k := by
by_cases hji : j = k
· subst j; omega
· by_cases hfj : f j = k
· have hh := hf j hj
rw [hfj] at hh
omega
· omega
have he : (min j (f j) < k) = (min j (f j) < k + 1) := by
apply propext; omega
simp only [he]

/-- Apply an index involution by swapping each pair once, reusing a uniquely owned array. -/
@[inline] def permuteInvolution (order : Array Nat) (a : Array α) : Array α := Id.run do
let mut out := a
for i in [:order.size] do
let j := order[i]!
if i < j then out := out.swapIfInBounds i j
return out

private theorem permuteInvolution_eq_fold (order : Array Nat) (a : Array α) :
permuteInvolution order a =
(List.range order.size).foldl (involutionStep (fun i ↦ order[i]!)) a := by
simp [permuteInvolution, ← apply_ite, Std.Legacy.Range.forIn_eq_forIn_range',
List.forIn_pure_yield_eq_foldl,
Std.Legacy.Range.size, List.range_eq_range']
rfl

/-- Swapping entries preserves the array length, including for invalid index tables. -/
@[simp] theorem size_permuteInvolution (order : Array Nat) (a : Array α) :
(permuteInvolution order a).size = a.size := by
rw [permuteInvolution_eq_fold, size_fold]

/-- A bounded involutive index table gives the same result as gathering a fresh array. -/
theorem permuteInvolution_eq_map (order : Array Nat) (a : Array α) (d : α)
(hs : order.size = a.size)
(hb : ∀ i < order.size, order[i]! < order.size)
(hf : ∀ i < order.size, order[order[i]!]! = i) :
permuteInvolution order a = order.map (fun i ↦ a.getD i d) := by
apply Array.ext
· rw [size_permuteInvolution, Array.size_map, hs]
· intro j hj hk
have hja : j < a.size := by simpa only [size_permuteInvolution] using hj
have hjo : j < order.size := by omega
have hfb : ∀ i < a.size, order[i]! < a.size := by simpa only [hs] using hb
have hff : ∀ i < a.size, order[order[i]!]! = i := by simpa only [hs] using hf
have hm := prefix_invariant (fun i ↦ order[i]!) a d hfb hff a.size (Nat.le_refl _) j hja
have hmin : min j order[j]! < a.size := Nat.lt_of_le_of_lt (Nat.min_le_left _ _) hja
simp only [hmin, ↓reduceIte] at hm
have he : (permuteInvolution order a).getD j d = a.getD order[j]! d := by
rw [permuteInvolution_eq_fold, hs]
exact hm
simpa only [Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem hj,
Option.getD_some, getElem!_pos order j hjo, Array.getElem_map] using he

end Array
Loading
Loading