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
/// Policy presets carried by the Rust manifest schema and independently
/// re-derived by `aver cert verify` from the classified artifact bytes.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CertificationPolicy {
SimulatesModel,
SimulatesModelTotally,
}
impl CertificationPolicy {
pub fn manifest_name(self) -> &'static str {
match self {
Self::SimulatesModel => "simulatesModel",
Self::SimulatesModelTotally => "simulatesModelTotally",
}
}
pub fn lean_value(self) -> &'static str {
match self {
Self::SimulatesModel => ".simulatesModel",
Self::SimulatesModelTotally => ".simulatesModelTotally",
}
}
pub fn level(self) -> &'static str {
match self {
Self::SimulatesModel => "L1",
Self::SimulatesModelTotally => "L3",
}
}
}
/// Closed v48 measure vocabulary. New variants require a schema bump and a
/// corresponding in-kernel `checkTerm` branch; unknown JSON measures never
/// fall back to this one.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TerminationMeasure {
IntNatAbs { param_idx: u32 },
}
/// Claim-axis termination data. It is deliberately absent from recursion
/// plans and their hashes: the verifier re-derives this value from the admitted
/// recursion family and pins it separately on the obligation.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct TerminationWitness {
pub measure: TerminationMeasure,
pub descent: i64,
}
impl TerminationWitness {
pub fn lean_value(self) -> String {
match self.measure {
TerminationMeasure::IntNatAbs { param_idx } => format!(
"({{ measure := .intNatAbs {param_idx}, descent := ({}) }} : AverCert.Schema.TerminationWitness)",
self.descent
),
}
}
}
/// One field of a stage-1 flat scalar record, as a `TypeDecl` scalar leaf. The
/// producer derives the ordered leaf list by decoding the module's struct type
/// section at the projected struct index (`record_leaves_from_bytes`); the wall
/// re-pins the whole declaration by equality against those same bytes, so this
/// is byte-derived data, not a trusted layout.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RecordLeaf {
IntCarrier,
BoolScalar,
FloatScalar,
}
impl RecordLeaf {
/// The Lean `TypeDecl` scalar-leaf constructor this field lowers to.
#[cfg(feature = "engine")]
fn lean_ctor(self) -> &'static str {
match self {
RecordLeaf::IntCarrier => ".intCarrier",
RecordLeaf::BoolScalar => ".boolScalar",
RecordLeaf::FloatScalar => ".floatScalar",
}
}
}
/// The Lean `List TypeDecl` literal of a record's ordered scalar-leaf fields.
#[cfg(feature = "engine")]
fn record_leaves_lean_value(leaves: &[RecordLeaf]) -> String {
format!(
"[{}]",
leaves
.iter()
.map(|leaf| leaf.lean_ctor())
.collect::<Vec<_>>()
.join(", ")
)
}
/// A certified function and the template holes extracted from its body.
enum Cert {
/// Generic non-recursive certificate. The inner shape still carries the
/// byte-derived face and proof parameters; the outer class records that the
/// non-recursive walker admitted it.
NonRecursive { inner: Box<Cert> },
/// Single-argument fuel self-recursion `f n = if n≤0 then BASE else <combine>`;
/// box/add/sub host helpers. All of the shape below is DATA recovered from the
/// bytes; only the descent (`n-1`) and the host `add` combinator are pinned:
/// - `base_k`: the literal returned in the base arm (sumTo's `0`, but any).
/// - `other` + `rec_first`: the `add` combines the self-call result `f(n-1)`
/// with `other` (the input `n`, or a boxed constant); `rec_first` records
/// which side the recursive result sits on — `f(n-1) + n` vs `n + f(n-1)`.
Recursive {
name: String,
self_idx: u32,
/// Declared type index of the exported function, carried for the
/// byte-first `recursion-plan-v1` artifact claim's function binding.
/// It does not enter the fuel-induction obligation.
type_idx: u32,
nlocals: usize,
carrier: u32,
box_idx: u32,
/// The combinator helper index (whichever arithmetic contract it obeys).
add_idx: u32,
sub_idx: u32,
base_k: i64,
rec_first: bool,
other: BodyOperand,
/// `+` or `*`, read from the model operator (see [`Combinator`]).
combinator: Combinator,
/// Raw code-entry bytes of the export. The recognizer normalizes
/// local-alias hops before classification, so a certified body may be
/// byte-noisier than the canonical template; the `recursion-plan-v1`
/// claim is emitted ONLY when the canonical plan lowering reproduces
/// exactly these bytes (otherwise the export stays on the legacy
/// witness route — never an unverifiable claim).
code_entry_bytes: Vec<u8>,
},
/// countDown-shape two-argument accumulator recursion; box/add/sub host helpers.
AccumulatorRecursive {
name: String,
self_idx: u32,
type_idx: u32,
nlocals: usize,
carrier: u32,
box_idx: u32,
add_idx: u32,
sub_idx: u32,
/// See `Recursive::code_entry_bytes`.
code_entry_bytes: Vec<u8>,
},
/// Non-recursive constructor: local arguments wrapped by `struct.new`.
AdtConstructor {
name: String,
self_idx: u32,
type_idx: u32,
nlocals: usize,
carrier: u32,
struct_idx: u32,
field_count: u32,
elem_ty: TyKind,
arity: usize,
fields: Vec<ConstructorField>,
ops: Vec<Op>,
},
/// Non-recursive record/variant field projection.
FieldProjection {
name: String,
self_idx: u32,
type_idx: u32,
nlocals: usize,
carrier: u32,
struct_idx: u32,
field_count: u32,
field_idx: u32,
result_ty: TyKind,
code_entry_bytes: Vec<u8>,
ops: Vec<Op>,
},
/// Non-recursive two-branch "widened" match projecting one integer-payload
/// variant of a user inductive, with a boxed-`0` default for every other
/// variant: `match j { JsonInt(n) -> n; _ -> 0 }`. Generalises the fixed
/// three-variant match to any inductive with a single projected Int variant.
WidenedIntMatch {
name: String,
self_idx: u32,
nlocals: usize,
carrier: u32,
/// Struct type index of the projected (integer-payload) variant.
hit_variant_idx: u32,
box_idx: u32,
/// Raw code-entry bytes of the export. The `int-dispatch-v1` claim is
/// emitted ONLY when the canonical plan lowering reproduces exactly
/// these bytes (otherwise the export stays on the legacy witness
/// route), exactly like `Cert::VerbatimWidenedMatch::code_entry_bytes`.
/// The claim binds the export by name (anonymous `FuncBinding`
/// witness), so no code/type index is carried.
code_entry_bytes: Vec<u8>,
ops: Vec<Op>,
},
/// Non-recursive two-branch match projecting one variant's first field
/// VERBATIM (as a raw `WVal`), defaulting to the null reference for every
/// other variant: `match j { JsonList(items) -> items; _ -> [] }` where the
/// empty list lowers to `ref.null`. No claim about the projected value's
/// meaning — `Cod := WVal`, `verbatimRepr` — so it needs no carrier/string
/// representation.
VerbatimWidenedMatch {
name: String,
self_idx: u32,
nlocals: usize,
carrier: u32,
hit_variant_idx: u32,
default: VerbatimDefault,
/// Raw code-entry bytes of the export. The `verbatim-plan-v1` claim is
/// emitted ONLY when the canonical plan lowering reproduces exactly
/// these bytes (otherwise analysis declines the export before rendering),
/// exactly like `Cert::Recursive::code_entry_bytes`. The claim binds the
/// export by name (anonymous `FuncBinding` witness), so no code/type
/// index is carried.
code_entry_bytes: Vec<u8>,
ops: Vec<Op>,
},
/// Non-recursive `ref.test` dispatch over a user enum where EVERY arm returns
/// a distinct VERBATIM constant (a String literal `array.new_data`, a null, or
/// an f64), e.g. `match code { Quote -> "\""; Backslash -> "\\"; ... }`
/// (`unescapedChar`). No host, no arithmetic, no field projection — each tag
/// maps to a byte-derived constant `WVal`. Certified `Cod := WVal`,
/// `verbatimRepr`, model `{name}Model : WVal → WVal`; reuses the verbatim
/// widened face. The generalisation of the verbatim widened match from "one
/// projected hit + a default" to "k constant arms".
VerbatimVariantDispatch {
name: String,
self_idx: u32,
nlocals: usize,
carrier: u32,
/// `(variant tag, its verbatim constant)`, in dispatch order.
arms: Vec<(u32, VerbatimDefault)>,
/// The terminal else constant.
default: VerbatimDefault,
/// Raw code-entry bytes of the export; see
/// `VerbatimWidenedMatch::code_entry_bytes`.
code_entry_bytes: Vec<u8>,
ops: Vec<Op>,
},
/// Non-recursive String-literal dispatch whose condition is delegated to a
/// contracted `String.eq` helper. The certified user body stays loop-free:
/// it constructs byte-derived string constants with `array.new_data`, calls
/// the exact String.eq host slot, then returns a byte-derived constant or
/// the original input verbatim.
StringEqVerbatimMatch {
name: String,
self_idx: u32,
type_idx: u32,
nlocals: usize,
carrier: u32,
string_eq_idx: u32,
arms: Vec<(VerbatimDefault, VerbatimDefault)>,
default: StringEqDefault,
ops: Vec<Op>,
},
/// Non-recursive String transformation whose body builds a small container
/// of string arrays (one or more literals from `array.new_data` plus the
/// function argument), then invokes the contracted `String.concat` host
/// helper once and returns the byte-concatenated result. The certified body
/// is loop-free; only the helper is abstract. The simplest beachhead shape
/// is `fn(s) = prefix ++ s` (or `s ++ suffix`) — a single literal concatenated
/// with the input on either side.
StringConcatVerbatimMatch {
name: String,
self_idx: u32,
type_idx: u32,
nlocals: usize,
/// The module's Int-carrier struct type index, or `None` when the module
/// emits no Int carrier at all. Concatenation never touches the carrier,
/// so unlike every other family this one certifies in both states; the
/// index only picks the locals prelude the emitter reserved, and the
/// wall re-derives that choice from the type section rather than taking
/// it from here (`CertDecode.carrierState`).
carrier: Option<u32>,
/// The wasm func index of the String.concat host helper.
string_concat_idx: u32,
/// The container type index (the array-of-string-arrays built by
/// `array.new_fixed`).
container_ty: u32,
/// The string byte-array result type returned by the concat helper.
result_ty: u32,
/// The literal prefixes (applied before the input). May be empty.
prefixes: Vec<VerbatimDefault>,
/// The literal suffixes (applied after the input). May be empty.
suffixes: Vec<VerbatimDefault>,
ops: Vec<Op>,
},
/// Non-recursive typed expression fragment. The byte-bound body lowers from
/// a small ordered ANF representation plan (`ExprFragmentPlan`). When the
/// producer/checker also has a source-level view, `source_plan` preserves
/// that `SymPlan` instead of making renderers recover it from the
/// representation shape.
ExprFragment {
name: String,
self_idx: u32,
type_idx: u32,
nlocals: usize,
carrier: u32,
source_plan: Option<SymPlan>,
/// When the plan is a stage-1 record scalar field read, the projected
/// wasm struct index and the record's ordered scalar-leaf field list,
/// byte-derived from the module type section. `None` for every
/// non-record fragment. Renderers pin the whole declaration by equality
/// against the bytes and discharge through the wall's record face.
record_decl: Option<(u32, Vec<RecordLeaf>)>,
plan: ExprFragmentPlan,
ops: Vec<Op>,
},
/// General non-recursive variant dispatch over one user inductive: a chain
/// of `ref.test` branches (each else-arm continuing the chain) whose hit
/// arms each reduce to one recognised leaf — payload projection, or a
/// contracted host add/sub combining the payload with an integer constant —
/// and whose terminal else is a boxed integer constant. Recognised from the
/// parsed instruction tree, so arm count, arm order, per-arm semantics and
/// the default value are free; no full opcode sequence is pinned.
VariantDispatch {
name: String,
self_idx: u32,
nlocals: usize,
carrier: u32,
box_idx: u32,
add_idx: Option<u32>,
sub_idx: Option<u32>,
/// `(variant struct tag, leaf)` in dispatch order.
arms: Vec<(u32, ArmLeaf)>,
/// The terminal else: a boxed integer constant.
default_k: i64,
/// Raw code-entry bytes of the export; see
/// `WidenedIntMatch::code_entry_bytes`.
code_entry_bytes: Vec<u8>,
ops: Vec<Op>,
},
/// Cross-function composition: a non-recursive `Int -> Int` caller whose body
/// is a unary chain of calls to other user functions, each of which is itself
/// a straight-line integer shape (self-sum or a nested chain). The obligation
/// carries the caller's ENTIRE call closure in one `CodeTbl`, and the caller's
/// simulation lemma cites each callee's simulation lemma at its call site.
Composition {
name: String,
self_idx: u32,
carrier: u32,
/// The whole closure (caller + all transitively-reached callees, incl.
/// the caller's own chain entry), sorted by `self_idx`. Every entry's
/// body goes into the shared `CodeTbl`.
closure: Vec<ClosureEntry>,
/// Runtime contracts consumed anywhere in the closure.
has_add: bool,
has_sub: bool,
has_box: bool,
},
/// One member of a mutually-recursive SCC `f n = if n≤0 then base else g(n-1)`
/// (each member tail-calls the next in the cycle). Every member of the SCC is
/// a certified export sharing ONE proof: a conjunction fuel-induction over the
/// whole SCC where each cross-call is discharged by the matching conjunct of
/// the induction hypothesis (the `mutual_sim` shape). The lowest-`self_idx`
/// member is the "primary" that emits the shared code table + the conjunction
/// bridge + `mutual_sim`; every member emits its own obligation citing the
/// matching conjunct. Carries the whole (sorted) SCC so any member's cert can
/// render the shared block.
MutualRecursion {
name: String,
self_idx: u32,
carrier: u32,
box_idx: u32,
sub_idx: u32,
/// This member's index in `scc` (which conjunct of `mutual_sim`). The
/// member's own `nlocals`/`base_k` live in `scc[position]`.
position: usize,
/// The whole SCC, sorted by `self_idx`.
scc: Vec<MutualMember>,
},
}
/// One function of a mutually-recursive SCC. The recogniser verified this
/// member's body against the fixed shape `f n = if n≤0 then base else g(n-1)`,
/// so the shared code table is reconstructed losslessly from these byte-derived
/// fields (base literal, cross-call target) the way `Cert::Recursive` is — no
/// need to carry the raw ops.
#[derive(Clone)]
struct MutualMember {
name: String,
self_idx: u32,
/// Declared type index of this member's exported function, carried for the
/// byte-first `mutual-plan-v1` artifact claim's function binding. It does
/// not enter the fuel-induction obligation.
type_idx: u32,
nlocals: usize,
/// Literal returned in the base arm (`n ≤ 0`).
base_k: i64,
/// The SCC member this one tail-calls in its step arm.
cross_idx: u32,
/// Raw code-entry bytes of this member's export. The `mutual-plan-v1` claim
/// is emitted ONLY when the canonical plan lowering reproduces exactly these
/// bytes (otherwise analysis declines the member before rendering — never
/// an unverifiable claim), exactly like `Cert::Recursive::code_entry_bytes`.
code_entry_bytes: Vec<u8>,
}