Repository navigation
Expand file tree
/
Copy pathGate.lean
More file actions
674 lines (613 loc) · 38.1 KB
/
Copy pathGate.lean
File metadata and controls
674 lines (613 loc) · 38.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
/-
# Environment-level hygiene and computability gate (TRUSTED harness tool)
lake exe gate MODULE THEOREM --manifest FILE [--computable N1,N2,...] [--allowed-imports FILE]
[--badge-root CONST]
lake exe gate --closure ROOT1,ROOT2,... OUT (writes the import closure of the roots: the
allowed-imports list, harness/org-setup.sh)
lake exe gate --wiring MODULE --manifest FILE (rule 9, after the badge build, on the
badge's per-entry module; rules 3 and 5 too)
Loads the **elaborated environment** of `MODULE` (its `.olean` and imports; no source text is
read, so comment/string tricks are irrelevant; no `[init]` code is run, since this process never
enables initializer execution) and walks the transitive dependency closure of the certificate
`THEOREM` (types and values; inductives with their constructors and recursors).
**Trust is by content hash**, never by name, location or import graph: a module is
*trusted* (Lean core, Mathlib and its dependencies, the contract) or
*pinned* (CompPoly, ArkLib, VCVio, PolyFun) iff every one of its `.olean` files
(`.olean`, `.olean.server`, `.olean.private`) hashes (sha256) to the organiser's manifest
`FILE` (`scripts/olean-manifest.py gen` on a clean organiser build, before any solution code ran).
A module whose path is in the manifest but whose files differ is a **tampered organiser module**
and is rejected outright. Every other module (the solution, the organiser's untrusted Library,
anything dropped into a package directory) is untrusted.
It rejects (exit code 1) if:
1. any constant in the closure is `sorryAx`, `Lean.ofReduceBool`, `Lean.ofReduceNat` or
`Lean.trustCompiler`, or is an axiom other than `propext`, `Classical.choice`, `Quot.sound`
(this is `#print axioms THEOREM`, computed on the certificate itself);
2. a constant of the closure declared in an untrusted or pinned module is `unsafe`, `partial` or
`opaque` (an `opaque`/`partial` body is invisible to the kernel);
3. **any** untrusted module registers an entry of an attribute that substitutes compiled code,
runs code at load time or changes the compiler, for any target. Lean 4.34's attributes with an
effect on compiled code are: `implemented_by`, `extern`, `export` (symbol interposition in a
linked badge executable), `init`/`builtin_init` (code run when a compiled executable starts:
`@[init f] def x := 0` is an ordinary `def` whose compiled value is `f`'s result; also
every `initialize`, `register_simp_attr`, `register_option`), `csimp` (replacement lemmas),
`cpass` (a compiler-pass installer, run by every importer's compiler),
`computed_field` (compiled field implementations); all rejected (`tagged_return` qualifies
only `extern` declarations, already rejected, and is private to the compiler). `inline`, `noinline`, `macro_inline`,
`specialize`, `never_extract` and `unbox` steer the compiler without changing what a total
function computes, and are allowed. Pinned modules may register `csimp` lemmas (kernel-proved
equalities), `init` entries (their `initialize` declarations: trace classes, simp sets) and the
others; these are reported;
4. with `--computable N`, a **badge-path field** of `N` has no compiled code. Of the submission
the badge executes the compiled `acc` (the field `V`, run on the signed trace), plus the
initialisation of every module it links (rule 3 keeps `[init]` code out of the entry's). The
badge's own glue is compiled without the entry's modules in scope (`RecursionFast/Badge.lean`),
so the entry's other declarations (instances, notations) cannot change it, and the one module
elaborated with them in scope is checked by rule 9. `P` and `ext` are still required to be
compiled, a rule whose relaxation to `V` alone is an open question.
`badgeFields`: `Submission`: `V`, `P`, `ext`. If `N` itself has compiled code, every field has.
Otherwise `N` is unfolded (`whnf`, all transparency) to its constructor, and each badge-path
field term is **compiled in this process** as an auxiliary definition; it passes iff the
compiler emits code for it. Every other field (the extractor's specification `E`, `ε`, `δ`,
`qV`, `fuel`) is specification data: it is never executed and may be `noncomputable` (e.g.
`ε := errorCap`). "Computable" means *executable* (Lean has compiled code), not *feasible*: a
brute-force verifier is computable. Feasibility is enforced by the badge path's time and memory
budget and, for the extractor, by the certificate's proved step bound. No imported
extension code runs: environment extensions keep their initial state except the pure-data ones
the compiler needs (`pureExts`), and initializer execution is never enabled;
5. an organiser module of the environment does not match the manifest (above). A manifest that
lists one module path under two tags (e.g. `core/` and a pinned `packages/<pkg>/`) is refused
before anything is loaded: the gate finds a module's files by path, so their class would depend
on the line order;
6. (with `--allowed-imports FILE`) an untrusted module **imports** (directly) a module that
is neither untrusted itself nor in FILE. FILE is the import closure of the organiser's roots
(`Mathlib`, `RecursionFast.Contract`, `RecursionFast.Library`, `RecursionFast.Controls`; written
by `gate --closure` in `harness/org-setup.sh`).
This keeps the compiler's internals out of a submission's reach: a known forgery overwrites a
definition's emitted code from an elaboration-time `run_meta`, which leaves no declaration, but
needs `Lean.Compiler.LCNF.PhaseExt` (`impureExt`, `saveImpureDeclCore`), which no module of the
closure imports (nor `Lean` itself). A submission may import any module of Mathlib, the
contract, the organiser library and controls, and its own modules; nothing else (`Lean`,
`Lean.Compiler.*` beyond Mathlib's, `Lake`, …). And transitively (`rule6t`): every
module a submission-owned module reaches is its own or in FILE, so an untrusted organiser module
outside the list (the harness driver imports `Lean`) cannot carry the compiler in;
7. a **definition** declared in an untrusted module refers, in its type or value, to a
non-theorem constant of the compiler or the environment: under `Lean.Elab`, `Lean.Compiler`,
`Lean.IR`, `Lean.Meta`, `Lean.Environment`, `Lean.Kernel`, `Lean.Core`, the environment
extension types, or one of `Lean.modifyEnv`, `Lean.setEnv`, `Lean.persistentEnvExtensionsRef`,
`Lean.envExtensionsRef`, `Lean.addDecl`, `Lean.compileDecl(s)`, `unsafeCast`. A submission
defines no elaborator, tactic, compiler pass or environment manipulation (macros, notation and
`syntax` are fine). Theorems are exempt (their terms are never executed);
8. (with `--badge-root CONST`) the module that declares `CONST` (the constant the badge
executables are built from: the rendered challenge's `submission`) imports, transitively, a
module that is neither untrusted nor in the `--allowed-imports` closure, i.e. it reaches the
compiler. The badge executables import only that module (the gate prints
`gate: badge-root-module M`), so no module that can reach the compiler's state is linked into
them, whatever the submission's proofs import;
9. (`--wiring MODULE`, a separate run after the badge build) the badge's per-entry module `MODULE`
(`harness/badge/Verify.lean.in`, the only module elaborated with both the entry and the glue in
scope) declares anything but `main`, or `main`'s value is not exactly the constant application
`RFBadge.main RecursionFast.Challenge.submission`, or it has no compiled code, or the glue's
module is not trusted by hash (its `.olean`; the compiled objects the badge links are checked
by the certificate job against `org-setup.sh`'s step 4b, `olean-manifest.py objects-check`);
rule 3 runs on every non-trusted module of that environment.
Checking the elaborated value, not the source text, matters: an entry's `notation
"RFBadge.main" => fakeMain` makes the text `RFBadge.main` mean `fakeMain` in every importer
(`tests/gate/GateWiringHijack.lean`).
Each rule is a function below: `classify` (5), `rules12` over `closure` (1, 2), `rule3`, `rule6`
and `rule6t`, `rule8`, `rule7`, and `rule4` (`checkExec`). `main` runs them in that order, which is
the order of the `REJECT` lines; `wiringMain` runs 5, 3 and `rule9`.
**What the gate cannot see.** The executed code is the
code Lean's compiler emitted while elaborating the submission's badge-root module and its imports
(and the organiser's glue, compiled before).
A metaprogram run at elaboration time (`run_meta`, `#eval`, …) leaves no declaration. Rules 6 and
8 keep `Lean.Compiler.LCNF.PhaseExt` out of reach, but not the raw route: `persistentEnvExtensionsRef`
(in `Lean.Environment`, which every Mathlib module imports) and `unsafeCast` let a `run_meta` in a
Mathlib-only module rewrite the compiler data its importers inline
(`tests/source-rule/forge/ForgeRaw.lean`: the gate PASSES it, its importer's code runs `acceptAll`).
The harness's source rule (`scripts/source-rule.py`, run before any build) rejects the commands such
code needs; that raises the bar. The sound fix would be badge executables regenerated from kernel
terms, without linking the submission's objects.
-/
import Lean
open Lean
/-- Manifest packages of the **pinned** class; every other manifest entry is trusted. -/
def pinnedPackages : List String := ["CompPoly", "Arklib", "VCVio", "PolyFun"]
/-- Rule 1: constants the certificate's closure must not use. -/
def bannedConsts : List Name := [``sorryAx, ``Lean.ofReduceBool, ``Lean.ofReduceNat, ``Lean.trustCompiler]
/-- Rule 1: the only axioms allowed. -/
def allowedAxioms : List Name := [``propext, ``Classical.choice, ``Quot.sound]
/-- The module that declares `n` (`_current` for a declaration of this process). -/
def moduleOf (env : Environment) (n : Name) : Name :=
match env.getModuleIdxFor? n with
| some i => env.header.moduleNames[i.toNat]!
| none => `_current
/-- `s` is a sha256 digest as `sha256sum` prints it: 64 lowercase hexadecimal digits. -/
def isSha256 (s : String) : Bool :=
s.length == 64 && s.all fun c => c.isDigit || ('a' ≤ c && c ≤ 'f')
/-- A `sha256sum` record `<sha256> <rest>` as `(sha256, rest)`: split after the digest, so `rest`
(a path) may hold any character, two spaces included. -/
def splitHashRecord (r : String) : Option (String × String) :=
let cs := r.toList
let h := String.ofList (cs.take 64)
if isSha256 h && (cs.drop 64).take 2 == [' ', ' '] && cs.length > 66 then
some (h, String.ofList (cs.drop 66))
else none
/-- The manifest: module-relative `.olean` path (e.g. `Mathlib/Order/Basic.olean.server`) ↦
(sha256, pinned?). Lines are `<sha256> <tag>/<path>`, tag `core`, `project` or `packages/<pkg>`.
The tag decides the class (pinned iff `packages/<pkg>` with `pkg` in `pinnedPackages`) but the
gate looks modules up by path alone (`findOLean` does not say which tag a file is under), so two
keys with one path (e.g. `core/X.olean` and `packages/CompPoly/X.olean`) are an error: the class
would depend on the line order. -/
def readManifest (file : System.FilePath) : IO (Std.HashMap String (String × Bool)) := do
let mut m : Std.HashMap String (String × Bool) := {}
let mut keyOf : Std.HashMap String String := {} -- path ↦ the tagged key it came from
for line in ← IO.FS.lines file do
if line.isEmpty || line.startsWith "#" then continue
let some (h, key) := splitHashRecord line
| throw <| IO.userError s!"gate: malformed manifest line {line}"
let (rel, pinned) ← match key.splitOn "/" with
| "core" :: rel | "project" :: rel => pure ("/".intercalate rel, false)
| "packages" :: pkg :: rel => pure ("/".intercalate rel, pinnedPackages.contains pkg)
| _ => throw <| IO.userError s!"gate: malformed manifest key {key}"
if let some k0 := keyOf.get? rel then
throw <| IO.userError s!"gate: manifest keys {k0} and {key} have one module path {rel}"
keyOf := keyOf.insert rel key
m := m.insert rel (h, pinned)
return m
/-- sha256 of files, by `sha256sum` in parallel batches. `-z`: one NUL-terminated record per
file, names unescaped (a name with a backslash or a newline is not rewritten). -/
def sha256Files (files : Array String) : IO (Std.HashMap String String) := do
let mut tasks := #[]
for i in [:(files.size + 255) / 256] do
let batch := files.extract (256 * i) (256 * i + 256)
tasks := tasks.push (← IO.asTask (IO.Process.output { cmd := "sha256sum", args := #["-z", "--"] ++ batch }))
let mut out : Std.HashMap String String := {}
for t in tasks do
let r : IO.Process.Output ← IO.ofExcept t.get
unless r.exitCode == 0 do throw <| IO.userError "gate: sha256sum failed"
for rec in r.stdout.splitOn "\x00" do
if let some (h, f) := splitHashRecord rec then out := out.insert f h
return out
/-- Trusted and pinned modules of `env` by content hash; errors for tampered organiser modules. -/
def classify (env : Environment) (manifest : Std.HashMap String (String × Bool)) :
IO (NameSet × NameSet × Array String) := do
let exts := ["", ".server", ".private"]
let mut known : Array (Name × List (String × String)) := #[] -- module, (manifest key, file)
let mut files : Array String := #[]
let mut errs : Array String := #[]
for m in env.header.moduleNames do
let rel := "/".intercalate (m.components.map (·.toString (escape := false))) ++ ".olean"
unless manifest.contains rel do continue
let path := (← findOLean m).toString
let mut parts := []
for e in exts do
let onDisk ← System.FilePath.pathExists (path ++ e)
if onDisk != manifest.contains (rel ++ e) then
errs := errs.push s!"organiser module {m}: file {path ++ e} {if onDisk then "is not" else "is missing"} in the manifest"
if onDisk then
parts := (rel ++ e, path ++ e) :: parts
files := files.push (path ++ e)
known := known.push (m, parts)
let hashes ← sha256Files files
let mut trusted : NameSet := {}
let mut pinned : NameSet := {}
for (m, parts) in known do
let ok := parts.all fun (k, f) => (manifest.get? k).map (·.1) == hashes.get? f && hashes.contains f
if !ok then
errs := errs.push s!"organiser module {m}: .olean does not match the manifest (tampered or stale build)"
else if parts.any fun (k, _) => (manifest.get? k).any (·.2) then pinned := pinned.insert m
else trusted := trusted.insert m
return (trusted, pinned, errs)
/-! ### Walks shared by the rules -/
/-- The nodes reachable from `roots` along `succ`, each once, in depth-first visiting order: the
one worklist behind the certificate's closure (rules 1, 2), the badge root's import closure
(rule 8). -/
def reach (roots : Array Name) (succ : Name → Array Name) : Array Name := Id.run do
let mut seen : NameSet := {}
let mut order := #[]
let mut todo := roots
while h : todo.size > 0 do
let n := todo.back
todo := todo.pop
if seen.contains n then continue
seen := seen.insert n
order := order.push n
for m in succ n do
unless seen.contains m do todo := todo.push m
return order
/-- The constants the type and the value of `ci` refer to. -/
def usedConsts (ci : ConstantInfo) : Array Name :=
ci.type.getUsedConstants ++ ((ci.value? (allowOpaque := true)).map (·.getUsedConstants)).getD #[]
/-- `c` is a theorem: its term is never executed (rule 7 exempts it). -/
def isTheorem (env : Environment) (c : Name) : Bool :=
match env.find? c with | some (.thmInfo _) => true | _ => false
/-- The modules `m` imports directly (none if `m` is not a module of `env`). -/
def moduleImports (env : Environment) (m : Name) : Array Name :=
match env.getModuleIdx? m with
| some i => env.header.moduleData[i.toNat]!.imports.map (·.module)
| none => #[]
/-- Rules 1, 2: the transitive closure of the constants used by `root` (types and values;
inductives with their constructors and recursors). -/
def closure (env : Environment) (root : Name) : NameSet :=
.ofArray <| reach #[root] fun n => match env.find? n with
| none => #[]
| some ci => usedConsts ci ++ match ci with
| .inductInfo v => v.ctors.toArray.push (mkRecName n)
| .ctorInfo v => #[v.induct]
| .recInfo v => v.all.toArray
| _ => #[]
/-! ### Rule 4: badge-path computability -/
open Meta
/-- The computational fields **rule 4 requires compiled**, per contract structure: the verifier `V`
(the badge `verify` is the compiled `Submission.acc`, which runs `V` on the signed trace), the
prover `P`, and the extractor program `ext` (the last two run by no badge). Everything else (`E`,
`ε`, `δ`, `qV`, `fuel`) is specification data. -/
def badgeFields : List (Name × List Name) :=
[(`RecursionFast.Submission, [`V, `P, `ext])]
/-- Persistent extensions whose imported state `whnf` and the compiler need here (matcher info,
so that `match` in a field term compiles). Their `addImportedFn` is core code over plain data.
All other extensions keep their initial state: no imported macro, parser, attribute, compiler
pass or `[init]` code is evaluated by the gate (it never enables initializer execution). -/
def pureExts : List Name := [`Lean.Meta.Match.Extension.extension]
/-- `env` with the imported state of the `pureExts` loaded. -/
def loadPureExts (env : Environment) : IO Environment := do
let mut env := env
for d in ← persistentEnvExtensionsRef.get do
if pureExts.contains d.name then
let s := d.toEnvExtension.getState (asyncMode := .sync) env
let st ← d.addImportedFn s.importedEntries { env, opts := {} }
env := d.toEnvExtension.setState (asyncMode := .sync) env { s with state := st }
return env
/-- Heartbeat budget (in thousands) for unfolding and compiling the badge-path fields. -/
def gateHeartbeats : Nat := 2000000
/-- `n` has compiled code (an IR declaration). -/
def hasCode (env : Environment) (n : Name) : Bool := (IR.findEnvDecl env n).isSome
/-- Compile the closed term `v` as a fresh auxiliary definition `n` in this process (kernel-checked
first); `none` iff the compiler emitted code for it, otherwise the compiler's error. -/
def compileAux (n : Name) (v : Expr) : MetaM (Option String) := do
let v ← instantiateMVars v
tryCatchRuntimeEx (do
let type ← inferType v
let lps := (collectLevelParams (collectLevelParams {} type) v).params.toList
addDecl (.defnDecl { name := n, levelParams := lps, type, value := v, hints := .opaque,
safety := .safe })
compileDecls #[n]
return if hasCode (← getEnv) n then none else some "the compiler emitted no code")
fun ex => return some (← ex.toMessageData.toString)
/-- The literal `(⟨i, _⟩ : Fin n)`, proof by `decide`. -/
def finLit (n i : Nat) : Expr :=
let lt := mkApp4 (mkConst ``LT.lt [0]) (mkConst ``Nat) (mkConst ``instLTNat) (mkRawNatLit i) (mkRawNatLit n)
mkApp3 (mkConst ``Fin.mk) (mkRawNatLit n) (mkRawNatLit i) <|
mkApp3 (mkConst ``of_decide_eq_true) lt (mkApp2 (mkConst ``Nat.decLt) (mkRawNatLit i) (mkRawNatLit n))
(mkApp2 (mkConst ``Eq.refl [1]) (mkConst ``Bool) (mkConst ``Bool.true))
/-- Field names of a structure constructor (binder names after the parameters). -/
def ctorFields (ty : Expr) (numParams : Nat) : List Name := Id.run do
let mut out := #[]
let mut t := ty
let mut i := 0
while true do
match t with
| .forallE n _ b _ => if i ≥ numParams then out := out.push n
i := i + 1; t := b
| _ => break
return out.toList
/-- The field argument of `x` if `x` unfolds (all transparency) to a constructor application of
structure `S`, field index `i`. -/
def ctorField? (S : Name) (i : Nat) (x : Expr) : MetaM (Option Expr) := do
let some (.inductInfo iv) := (← getEnv).find? S | return none
let [ctor] := iv.ctors | return none
let x ← withTransparency .all <| whnf x
unless x.isAppOf ctor do return none
return x.getAppArgs[iv.numParams + i]?
/-- Replace each projection `s.f` whose structure value `s` unfolds to a constructor by that
field's argument (`{ s with IDX := … }` elaborates to `⟨s.VK, …⟩`, and `s` may itself lack
compiled code, e.g. because of its `ε`). Semantics-preserving (definitional unfolding). -/
def reduceProjs (e : Expr) : MetaM Expr :=
Meta.transform e (post := fun e => do
match e with
| .proj S i x => return match ← ctorField? S i x with | some a => .visit a | none => .done e
| _ =>
let .const f _ := e.getAppFn | return .done e
let some info ← getProjectionFnInfo? f | return .done e
let args := e.getAppArgs
unless !info.fromClass && args.size > info.numParams do return .done e
match ← ctorField? info.ctorName.getPrefix info.i args[info.numParams]! with
| some a => return .visit (mkAppN a args[info.numParams + 1:]).headBeta
| none => return .done e)
/-- Rule 4 on the term `e` (displayed as `path`): returns the rejections. -/
partial def checkExec (path : String) (e : Expr) : MetaM (Array String) := do
-- (a) the whole term has compiled code: then so has every field
let mut err? ← match e with
| .const n _ => pure (if hasCode (← getEnv) n then none else some s!"{n} has no compiled code")
| _ => compileAux (Name.mkSimple s!"_gate_badge {path}") e
if err?.isSome && !e.isConst then
let e2 ← reduceProjs e
if e2 != e then err? ← compileAux (Name.mkSimple s!"_gate_badge {path} (projections reduced)") e2
let some err := err? | do
IO.println s!"gate: rule 4: {path}: compiled code"
return #[]
let ty ← whnf (← inferType e)
-- (b) a family over `Fin k` (v0.3's `Submission*.protocol`; kept for families): member by member
if let .forallE _ d _ _ := ty then
let d ← whnf d
if d.isAppOfArity ``Fin 1 then
if let some k ← evalNat d.appArg! then
if k ≤ 64 then
let mut errs := #[]
for i in [:k] do errs := errs ++ (← checkExec s!"{path}[{i}]" (mkApp e (finLit k i)))
return errs
-- (c) a contract structure: unfold to its constructor, check the badge-path fields only
let some S := ty.getAppFn.constName? | return #[s!"{path}: {err}"]
let some fields := badgeFields.lookup S | return #[s!"{path}: {err}"]
let some (.inductInfo iv) := (← getEnv).find? S | return #[s!"{path}: {err}"]
let [ctor] := iv.ctors | return #[s!"{path}: {err}"]
let some cinfo := (← getEnv).find? ctor | return #[s!"{path}: {err}"]
let names := ctorFields cinfo.type iv.numParams
let e' ← withTransparency .all <| whnf e
unless e'.isAppOfArity ctor (iv.numParams + names.length) do
return #[s!"{path}: {err}; it does not unfold to a `{ctor}` application, so its badge-path fields cannot be checked separately"]
IO.println s!"gate: rule 4: {path}: no compiled code as a whole ({err}); checking its badge-path fields {fields}, the rest is specification data"
let mut errs := #[]
for f in fields do
match names.idxOf? f with
| some j => errs := errs ++ (← checkExec s!"{path}.{f}" e'.getAppArgs[iv.numParams + j]!)
| none => errs := errs.push s!"{path}: structure {S} has no field {f}"
return errs
/-! ### Rule 7: the compiler's and the environment's API -/
/-- Rule 7: name prefixes of the compiler's and the environment's API. -/
def metaPrefixes : List Name :=
[`Lean.Elab, `Lean.Compiler, `Lean.IR, `Lean.Meta, `Lean.Environment, `Lean.Kernel, `Lean.Core,
`Lean.EnvExtension, `Lean.EnvExtensionInterfaceUnsafe, `Lean.PersistentEnvExtension,
`Lean.SimplePersistentEnvExtension, `Lean.ScopedEnvExtension, `Lean.MonadEnv]
/-- Rule 7: single constants of the environment's API. -/
def metaConsts : List Name :=
[`Lean.modifyEnv, `Lean.setEnv, `Lean.persistentEnvExtensionsRef, `Lean.envExtensionsRef,
`Lean.addDecl, `Lean.compileDecl, `Lean.compileDecls, `Lean.addAndCompile, `unsafeCast]
/-- Rule 7: `c` is a non-theorem constant of the compiler's or the environment's API. -/
def isMetaConst (env : Environment) (c : Name) : Bool :=
(metaConsts.contains c || metaPrefixes.any (·.isPrefixOf c)) && !isTheorem env c
/-! ### The rules -/
/-- What the rules read: the environment, its modules classified by content hash (rule 5), and the
module lists of the command line. -/
structure GateCtx where
env : Environment
trusted : NameSet
pinned : NameSet
/-- `--allowed-imports`: the import closure of the organiser's roots (rules 6, 8). -/
allowed : NameSet
/-- `m` is neither trusted nor pinned by content hash. -/
def GateCtx.untrusted (g : GateCtx) (m : Name) : Bool := !g.trusted.contains m && !g.pinned.contains m
/-- The modules of the environment with their indices, in the header's order. -/
def GateCtx.modules (g : GateCtx) : Array (Name × ModuleIdx) :=
g.env.header.moduleNames.mapIdx fun i m => (m, i)
/-- Rule 1 on the constant `n` of the certificate's closure: banned, or a non-standard axiom. -/
def rule1 (n : Name) (ci? : Option ConstantInfo) : Array String :=
(if bannedConsts.contains n then #[s!"closure uses {n}"] else #[]) ++
match ci? with
| some (.axiomInfo _) => if allowedAxioms.contains n then #[] else #[s!"non-standard axiom {n}"]
| _ => #[]
/-- Rule 2 on the constant `n` of the certificate's closure, declared in the non-trusted module
`m`: `unsafe`, `opaque` or `partial`. -/
def rule2 (n m : Name) (ci : ConstantInfo) : Array String :=
(if ci.isUnsafe then #[s!"unsafe constant {n} (module {m})"] else #[]) ++
match ci with
| .opaqueInfo _ => #[s!"opaque (or partial) constant {n} (module {m})"]
| .defnInfo v => if v.safety == .partial then #[s!"partial definition {n} (module {m})"] else #[]
| _ => #[]
/-- Rules 1 and 2 on the closure `cl`, constant by constant; and the closure's axioms. -/
def rules12 (g : GateCtx) (cl : NameSet) : Array String × Array Name := Id.run do
let mut errs := #[]
let mut axioms := #[]
for n in cl.toList do
let ci? := g.env.find? n
errs := errs ++ rule1 n ci?
let some ci := ci? | continue
if ci matches .axiomInfo _ then axioms := axioms.push n
let m := moduleOf g.env n
unless g.trusted.contains m do errs := errs ++ rule2 n m ci
return (errs, axioms)
/-- Rule 3 on the non-trusted module `m` (index `i`): its entries of the attributes that substitute
compiled code, run code at load time or change the compiler. `implemented_by`, `extern` and
`export` are rejected outright; `[init]`, `@[csimp]`, `@[cpass]` and `@[computed_field]` entries
are reported for a pinned module (`gate: note:`) and rejected for an untrusted one. -/
def rule3 (g : GateCtx) (m : Name) (i : ModuleIdx) : IO (Array String) := do
let mut errs := #[]
for (d, impl) in Compiler.implementedByAttr.ext.getModuleEntries g.env i (level := .private) do
errs := errs.push s!"module {m}: @[implemented_by {impl}] on {d}"
for (d, _) in externAttr.ext.getModuleEntries g.env i (level := .private) do
errs := errs.push s!"module {m}: @[extern] on {d}"
for (d, sym) in exportAttr.ext.getModuleEntries g.env i (level := .private) do
errs := errs.push s!"module {m}: @[export {sym}] on {d}"
-- `count` entries described as `what`: a note for a pinned module, the rejections `rej` otherwise
let pinnedNote (count : Nat) (what : String) (rej : Array String) : IO (Array String) := do
if count == 0 then return #[]
unless g.pinned.contains m do return rej
IO.println s!"gate: note: pinned dependency {m} registers {count} {what}"
return #[]
let inits := (regularInitAttr.ext.getModuleEntries g.env i (level := .private)).map (·.1) ++
(builtinInitAttr.ext.getModuleEntries g.env i (level := .private)).map (·.1)
errs := errs ++ (← pinnedNote inits.size "[init] declaration(s)"
(inits.map fun d => s!"module {m}: [init]/[builtin_init] on {d}"))
errs := errs ++ (← pinnedNote (Compiler.CSimp.ext.ext.getModuleEntries g.env i (level := .private)).size
"@[csimp] lemma(s) (proved equalities)" #[s!"module {m}: registers @[csimp] lemmas"])
-- compiler-pass installers, computed fields
for (a, ds) in [("cpass", Compiler.LCNF.passManagerExt.getModuleEntries g.env i (level := .private)),
("computed_field", Elab.ComputedFields.computedFieldAttr.ext.getModuleEntries g.env i (level := .private))] do
errs := errs ++ (← pinnedNote ds.size s!"@[{a}] entr(y/ies)" (ds.map fun d => s!"module {m}: @[{a}] on {d}"))
return errs
/-- Rule 6 on the untrusted module `m` (index `i`): each direct import that is neither untrusted
nor in the allowed closure. -/
def rule6 (g : GateCtx) (m : Name) (i : ModuleIdx) : Array String :=
g.env.header.moduleData[i.toNat]!.imports.filterMap fun imp =>
if g.untrusted imp.module || g.allowed.contains imp.module then none
else some s!"module {m} imports {imp.module}, which is outside the allowed imports"
/-- A submission-owned module: untrusted, and not one of the organiser's modules named by the allowed
closure (a submission's module roots are fresh, `harness/evaluate.py`). -/
def GateCtx.submissionOwned (g : GateCtx) (m : Name) : Bool :=
g.untrusted m && !g.allowed.contains m
/-- Rule 6, transitive, on the submission-owned module `m`: every module it
reaches is submission-owned or in the allowed closure; so an untrusted organiser module outside the
list (e.g. the harness driver, which imports `Lean`) cannot carry the compiler into a submission. -/
def rule6t (g : GateCtx) (m : Name) : Array String :=
let bad := (reach #[m] (moduleImports g.env)).filter fun n =>
!(g.submissionOwned n || g.allowed.contains n)
if bad.isEmpty then #[] else
#[s!"module {m} reaches (transitively) {bad.size} module(s) outside the allowed imports, " ++
s!"e.g. {bad.toList.take 3} (rule 6)"]
/-- Rule 7 on the untrusted module `m` (index `i`): each definition (not a theorem) whose type or
value refers to the compiler's or the environment's API. -/
def rule7 (g : GateCtx) (m : Name) (i : ModuleIdx) : Array String :=
g.env.header.moduleData[i.toNat]!.constNames.filterMap fun n =>
match g.env.find? n with
| none | some (.thmInfo _) => none
| some ci =>
let bad := (usedConsts ci).filter (isMetaConst g.env)
if bad.isEmpty then none
else some s!"module {m}: {n} refers to the compiler/environment API ({bad.toList.take 3})"
/-- Rule 8 on the badge root `r`: its module and that module's transitive imports are untrusted
or in the allowed (compiler-free) closure. On success prints `gate: badge-root-module M`. -/
def rule8 (g : GateCtx) (r : Name) : IO (Array String) := do
unless g.env.contains r do return #[s!"--badge-root {r}: not found"]
let m := moduleOf g.env r
let bad := (reach #[m] (moduleImports g.env)).filter fun n => !(g.untrusted n || g.allowed.contains n)
if bad.isEmpty then
IO.println s!"gate: badge-root-module {m}"
return #[]
return #[s!"badge root {r} is declared in module {m}, which imports (transitively) " ++
s!"{bad.size} module(s) outside the compiler-free closure, e.g. {bad.toList.take 3} (rule 8)"]
/-- Rule 4 on the constants `comps`, in this process, over the environment with the `pureExts`
loaded. -/
def rule4 (g : GateCtx) (comps : List String) : IO (Array String) := do
if comps.isEmpty then return #[]
let opts : Options := ({} : Options) |>.set `Elab.async false |>.set `maxHeartbeats gateHeartbeats
let ctx : Core.Context := { fileName := "<gate>", fileMap := default, options := opts,
maxHeartbeats := gateHeartbeats * 1000 }
let act : MetaM (Array String) := comps.foldlM (init := #[]) fun acc c => do
unless (← getEnv).contains c.toName do return acc.push s!"--computable {c}: not found"
return acc ++ (← checkExec c (mkConst c.toName))
return (← (act.run' {} {}).toIO ctx { env := ← loadPureExts g.env }).1
/-! ### Rule 9: the badge's per-entry module (`--wiring`) -/
/-- The badge glue's entry point (`RecursionFast/Badge.lean`) and the constant the per-entry
module passes it. -/
def wiringCallee : Name := `RFBadge.main
/-- See `wiringCallee`. -/
def wiringArg : Name := `RecursionFast.Challenge.submission
/-- Rule 9 on the badge's per-entry module `m` (`harness/badge/Verify.lean.in`), elaborated with the
entry's instances and notations in scope: it declares `main` alone, a definition whose value is
exactly `RFBadge.main RecursionFast.Challenge.submission` (constants, not names: a notation that
turns either name into another term changes the value) and whose type is `RFBadge.main`'s result
type, with compiled code; the glue's module is trusted by hash. Rule 3 runs on `m` beside it. -/
def rule9 (g : GateCtx) (m : Name) : Array String := Id.run do
let some i := g.env.getModuleIdx? m | return #[s!"--wiring {m}: not a module of the environment"]
let mut errs := #[]
match g.env.find? wiringCallee with
| none => return #[s!"--wiring {m}: {wiringCallee} not found"]
| some ci =>
let gm := moduleOf g.env wiringCallee
unless g.trusted.contains gm do
errs := errs.push s!"the badge glue {wiringCallee} is declared in {gm}, which is not trusted by hash"
let extra := g.env.header.moduleData[i.toNat]!.constNames.filter (· != `main)
unless extra.isEmpty do
errs := errs.push s!"module {m} declares {extra.size} constant(s) besides main, e.g. {extra.toList.take 3}"
match g.env.find? `main with
| some (.defnInfo v) =>
if moduleOf g.env `main != m then
errs := errs.push s!"main is declared in {moduleOf g.env `main}, not in {m}"
let want := mkApp (mkConst wiringCallee) (mkConst wiringArg)
unless v.value == want && v.levelParams.isEmpty do
errs := errs.push s!"main is not {want}: its value is {v.value}"
let wantTy := match ci.type with | .forallE _ _ b _ => some b | _ => none
unless wantTy == some v.type && !v.type.hasLooseBVars do
errs := errs.push s!"main's type {v.type} is not the result type of {wiringCallee}"
unless hasCode g.env `main do errs := errs.push "main has no compiled code"
| _ => errs := errs.push s!"module {m}: main is not a definition"
return errs
/-- `gate --wiring MODULE --manifest FILE`: rule 5 (hashes) on the environment of the badge's
per-entry module, rule 3 on its every non-trusted module, and rule 9. -/
def wiringMain (modName manifestFile : String) : IO UInt32 := do
initSearchPath (← findSysroot)
let manifest ← readManifest manifestFile
let env ← importModules #[{ module := modName.toName }] {} (trustLevel := 0)
let (trusted, pinned, hashErrs) ← classify env manifest
let g : GateCtx := { env, trusted, pinned, allowed := {} }
let mut errs := hashErrs
for (m, i) in g.modules do
unless trusted.contains m do errs := errs ++ (← rule3 g m i)
errs := errs ++ rule9 g modName.toName
IO.println s!"gate: wiring {modName}: {env.header.moduleNames.size} modules, {trusted.size} trusted by hash"
if errs.isEmpty then
IO.println "gate: PASS"; return 0
for e in errs do IO.println s!"gate: REJECT {e}"
return 1
/-! ### Command line -/
/-- The command line, after `MODULE THEOREM`: the options by name. -/
structure GateArgs where
manifest : String := ""
computable : String := ""
allowed : String := ""
badgeRoot : String := ""
/-- `MODULE THEOREM` and the options; `none` on a malformed command line, without `--manifest`, or
with `--badge-root` but no `--allowed-imports`. -/
def parseArgs : List String → Option (String × String × GateArgs)
| m :: t :: rest =>
let rec go (a : GateArgs) : List String → Option GateArgs
| [] => some a
| "--manifest" :: f :: r => go { a with manifest := f } r
| "--computable" :: c :: r => go { a with computable := c } r
| "--allowed-imports" :: f :: r => go { a with allowed := f } r
| "--badge-root" :: c :: r => go { a with badgeRoot := c } r
| _ => none
(go {} rest).bind fun a =>
if a.manifest.isEmpty || (!a.badgeRoot.isEmpty && a.allowed.isEmpty) then none else some (m, t, a)
| _ => none
/-- Module names, one per line. -/
def readNames (file : String) : IO NameSet := do
if file.isEmpty then return {}
return (← IO.FS.lines file).foldl (init := {}) fun s l => if l.isEmpty then s else s.insert l.toName
/-- The import closure of `roots`, one module per line (`gate --closure`). -/
def writeClosure (roots : String) (out : String) : IO UInt32 := do
initSearchPath (← findSysroot)
let env ← importModules ((roots.splitOn ",").toArray.map fun r => { module := r.toName }) {}
(trustLevel := 0)
let ms := env.header.moduleNames.map (·.toString) |>.qsort (· < ·)
IO.FS.writeFile out (String.join (ms.toList.map (· ++ "\n")))
IO.println s!"gate: wrote the import closure of {roots} ({ms.size} modules) to {out}"
return 0
/-- The rules in the order of their `REJECT` lines: 5, 1–2, 3, 6, 8, 7, 4. -/
def main (args : List String) : IO UInt32 := do
if let ["--closure", roots, out] := args then return ← writeClosure roots out
if let ["--wiring", m, "--manifest", f] := args then return ← wiringMain m f
let some (modName, thm, ga) := parseArgs args
| throw <| IO.userError ("usage: gate MODULE THEOREM --manifest FILE [--computable N1,N2,...] " ++
"[--allowed-imports FILE] [--badge-root CONST (needs --allowed-imports)]" ++
" | gate --closure ROOT1,ROOT2,... OUT | gate --wiring MODULE --manifest FILE")
let allowed ← readNames ga.allowed
initSearchPath (← findSysroot)
let manifest ← readManifest ga.manifest
let env ← importModules #[{ module := modName.toName }] {} (trustLevel := 0)
let thmName := thm.toName
unless env.contains thmName do
IO.eprintln s!"gate: {thm} not found in {modName}"; return 1
let (trusted, pinned, hashErrs) ← classify env manifest
let g : GateCtx := { env, trusted, pinned, allowed }
let cl := closure env thmName
let (errs12, axioms) := rules12 g cl
let mut errs := hashErrs ++ errs12
let mods := g.modules
for (m, i) in mods do
unless trusted.contains m do errs := errs ++ (← rule3 g m i)
if !ga.allowed.isEmpty then
for (m, i) in mods do
if g.untrusted m then errs := errs ++ rule6 g m i
for (m, _) in mods do
if g.submissionOwned m then errs := errs ++ rule6t g m
if !ga.badgeRoot.isEmpty then errs := errs ++ (← rule8 g ga.badgeRoot.toName)
for (m, i) in mods do
if g.untrusted m then errs := errs ++ rule7 g m i
errs := errs ++ (← rule4 g ((ga.computable.splitOn ",").filter (· ≠ "")))
IO.println s!"gate: {env.header.moduleNames.size} modules, {trusted.size} trusted and {pinned.size} pinned by hash"
IO.println s!"gate: {thm}: closure of {cl.size} constants, axioms {axioms.qsort (·.lt ·) |>.toList}"
if errs.isEmpty then
IO.println "gate: PASS"; return 0
for e in errs do IO.println s!"gate: REJECT {e}"
return 1