vyre-conform 0.1.0

Conformance suite for vyre backends — proves byte-identical output to CPU reference
Documentation
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
//! Pipeline implementation for the contribution feedback loop.

use std::path::Path;
use std::time::Instant;

use crate::enforce::admission::check_admission;
use crate::enforce::category::{
    check_category_a_zero_overhead, check_category_b_tripwire, check_category_c_intrinsic,
};
use crate::enforce::enforcers::signature_match::enforce_signature;
use crate::meta::contribute::{
    ContributeFilter, ContributeReport, ContributeResult, Finding, StageStatus,
};
use crate::op_registry;
use crate::proof::algebra::checker::verify_laws;
use crate::proof::algebra::inference::{find_missing_laws, infer_binary_laws, infer_unary_laws};
use crate::spec::registry::verify::verify_specs;
use crate::spec::types::{ConstructionTime, DataType, OpSpec, ProofToken};

/// Run the full contribution pipeline against the requested operation(s).
#[inline]
pub fn run_contribute(filter: ContributeFilter, vyre_src_root: &Path) -> Vec<ContributeReport> {
    let start = Instant::now();
    let all_specs = op_registry::all_specs();

    let specs: Vec<OpSpec> = match filter {
        ContributeFilter::All => all_specs,
        ContributeFilter::Op(id) => all_specs.into_iter().filter(|s| s.id == id).collect(),
    };

    if specs.is_empty() {
        let target = match filter {
            ContributeFilter::Op(id) => id,
            ContributeFilter::All => "all",
        };
        return vec![ContributeReport {
            op_id: target.to_string(),
            total_duration_ms: start.elapsed().as_millis() as u64,
            stages: vec![ContributeResult {
                stage_name: "registry".to_string(),
                duration_ms: start.elapsed().as_millis() as u64,
                status: StageStatus::Fail,
                findings: vec![Finding {
                    message: format!("Fix: operation '{target}' not found in registry."),
                }],
            }],
            overall_status: StageStatus::Fail,
        }];
    }

    specs
        .into_iter()
        .map(|spec| run_pipeline_for_op(&spec, vyre_src_root))
        .collect()
}

fn run_pipeline_for_op(op: &OpSpec, vyre_src_root: &Path) -> ContributeReport {
    let total_start = Instant::now();
    let mut stages = Vec::with_capacity(8);
    let mut overall = StageStatus::Pass;

    // Stage 1: registry
    let (status, findings, dur) = timed(|| match verify_specs(std::slice::from_ref(op)) {
        Ok(()) => (StageStatus::Pass, vec![]),
        Err(errors) => {
            let findings: Vec<Finding> = errors
                .into_iter()
                .map(|e| Finding {
                    message: e.to_string(),
                })
                .collect();
            (StageStatus::Fail, findings)
        }
    });
    overall = max_status(overall, status);
    stages.push(result("registry", dur, status, findings));
    if status == StageStatus::Fail {
        return fail_fast_report(op, total_start, stages, "registry");
    }

    // Stage 2: signature enforcer
    let (status, findings, dur) = timed(|| match enforce_signature(op) {
        Ok(()) => (StageStatus::Pass, vec![]),
        Err(msg) => (StageStatus::Fail, vec![Finding { message: msg }]),
    });
    overall = max_status(overall, status);
    stages.push(result("signature", dur, status, findings));

    if status == StageStatus::Fail {
        return fail_fast_report(op, total_start, stages, "signature");
    }

    // Stage 3: category gate (A + B + C overhead)
    let (status, findings, dur) = timed(|| {
        let mut cat_findings = check_category_a_zero_overhead(std::slice::from_ref(op));
        cat_findings.extend(check_category_c_intrinsic(std::slice::from_ref(op)));
        cat_findings.extend(check_category_b_tripwire(vyre_src_root));

        let mapped: Vec<Finding> = cat_findings
            .into_iter()
            .filter(|f| f.op_id() == op.id || f.op_id() == "CategoryB")
            .map(|f| Finding {
                message: f.to_string(),
            })
            .collect();

        if mapped.is_empty() {
            (StageStatus::Pass, vec![])
        } else {
            (StageStatus::Fail, mapped)
        }
    });
    overall = max_status(overall, status);
    stages.push(result("category", dur, status, findings));

    if status == StageStatus::Fail {
        return fail_fast_report(op, total_start, stages, "category");
    }

    // Stage 4: admission gates 4-8 (category overhead removed; lives in stage 3)
    let (status, findings, dur) = timed(|| {
        let admissions = check_admission(std::slice::from_ref(op), vyre_src_root);
        let mapped: Vec<Finding> = admissions
            .into_iter()
            .filter(|f| f.op_id() == op.id || f.op_id() == "CategoryB")
            .map(|f| Finding {
                message: f.to_string(),
            })
            .collect();
        if mapped.is_empty() {
            (StageStatus::Pass, vec![])
        } else {
            (StageStatus::Fail, mapped)
        }
    });
    overall = max_status(overall, status);
    stages.push(result("admission", dur, status, findings));
    if status == StageStatus::Fail {
        return fail_fast_report(op, total_start, stages, "admission");
    }

    // Stage 5: mandatory inference
    let (status, findings, dur) = timed(|| run_inference(op));
    overall = max_status(overall, status);
    stages.push(result("inference", dur, status, findings));

    // Stage 6: exhaustive u8 law check
    let (status, findings, dur) = timed(|| run_exhaustive_laws(op));
    overall = max_status(overall, status);
    stages.push(result("laws", dur, status, findings));

    // Stage 7: composition proof check
    let (status, findings, dur) = timed(|| run_composition_check(op));
    overall = max_status(overall, status);
    stages.push(result("composition", dur, status, findings));

    // Stage 8: summary
    let pass_count = stages
        .iter()
        .filter(|s| s.status == StageStatus::Pass)
        .count();
    let fail_count = stages
        .iter()
        .filter(|s| s.status == StageStatus::Fail)
        .count();
    let skip_count = stages
        .iter()
        .filter(|s| s.status == StageStatus::Skip)
        .count();
    stages.push(result(
        "summary",
        0,
        overall,
        vec![Finding {
            message: format!(
                "Pipeline complete for {}: {pass_count} passed, {fail_count} failed, {skip_count} skipped. Overall: {overall:?}.",
                op.id
            ),
        }],
    ));

    ContributeReport {
        op_id: op.id.to_string(),
        total_duration_ms: total_start.elapsed().as_millis() as u64,
        stages,
        overall_status: overall,
    }
}

fn fail_fast_report(
    op: &OpSpec,
    total_start: Instant,
    mut stages: Vec<ContributeResult>,
    failed_stage: &str,
) -> ContributeReport {
    stages.push(result(
        "summary",
        0,
        StageStatus::Fail,
        vec![Finding {
            message: format!(
                "Fix: {failed_stage} stage failed for {}. Skipping remaining stages.",
                op.id
            ),
        }],
    ));
    ContributeReport {
        op_id: op.id.to_string(),
        total_duration_ms: total_start.elapsed().as_millis() as u64,
        stages,
        overall_status: StageStatus::Fail,
    }
}

fn timed<F>(f: F) -> (StageStatus, Vec<Finding>, u64)
where
    F: FnOnce() -> (StageStatus, Vec<Finding>),
{
    let start = Instant::now();
    let (status, findings) = f();
    let dur = start.elapsed().as_millis() as u64;
    (status, findings, dur)
}

fn result(name: &str, dur: u64, status: StageStatus, findings: Vec<Finding>) -> ContributeResult {
    ContributeResult {
        stage_name: name.to_string(),
        duration_ms: dur,
        status,
        findings,
    }
}

fn max_status(a: StageStatus, b: StageStatus) -> StageStatus {
    match (a, b) {
        (StageStatus::Fail, _) | (_, StageStatus::Fail) => StageStatus::Fail,
        (StageStatus::Skip, _) | (_, StageStatus::Skip) => StageStatus::Skip,
        _ => StageStatus::Pass,
    }
}

fn run_inference(op: &OpSpec) -> (StageStatus, Vec<Finding>) {
    let is_binary = op.signature.inputs.len() == 2
        && op.signature.inputs.iter().all(|t| *t == DataType::U32)
        && op.signature.output == DataType::U32;

    let is_unary = op.signature.inputs.len() == 1
        && op.signature.inputs[0] == DataType::U32
        && op.signature.output == DataType::U32;

    if !is_binary && !is_unary {
        return (
            StageStatus::Skip,
            vec![Finding {
                message: format!(
                    "Inference only supports unary/binary u32 ops. {} has signature {:?} -> {:?}.",
                    op.id, op.signature.inputs, op.signature.output
                ),
            }],
        );
    }

    let report = if is_binary {
        infer_binary_laws(op.id, op.cpu_fn)
    } else {
        infer_unary_laws(op.id, op.cpu_fn)
    };

    let missing = find_missing_laws(&op.laws, &report);
    if missing.is_empty() {
        (
            StageStatus::Pass,
            vec![Finding {
                message: format!("{}: all inferred laws are already declared.", op.id),
            }],
        )
    } else {
        let names: Vec<String> = missing.iter().map(|m| m.name.to_string()).collect();
        let recs: Vec<String> = missing
            .iter()
            .map(|m| format!("  {}", m.recommendation))
            .collect();
        (
            StageStatus::Fail,
            vec![Finding {
                message: format!(
                    "{}: missing inferred laws [{}]. Recommended declarations:\n{}",
                    op.id,
                    names.join(", "),
                    recs.join("\n")
                ),
            }],
        )
    }
}

fn run_exhaustive_laws(op: &OpSpec) -> (StageStatus, Vec<Finding>) {
    if op.laws.is_empty() {
        return (
            StageStatus::Skip,
            vec![Finding {
                message: format!("{}: no laws declared to verify.", op.id),
            }],
        );
    }

    let is_binary = op.signature.inputs.len() >= 2;
    let law_results = verify_laws(op.id, op.cpu_fn, &op.laws, is_binary);
    let failures: Vec<_> = law_results.into_iter().filter(|r| !r.passed()).collect();

    if failures.is_empty() {
        (
            StageStatus::Pass,
            vec![Finding {
                message: format!(
                    "{}: all {} declared laws hold under exhaustive u8 verification.",
                    op.id,
                    op.laws.len()
                ),
            }],
        )
    } else {
        let findings: Vec<Finding> = failures
            .into_iter()
            .map(|f| {
                let msg = if let Some(v) = f.violation {
                    format!("Law '{}' violated: {}", v.law, v.message)
                } else {
                    format!("Law '{}' failed with unknown violation.", f.law_name)
                };
                Finding { message: msg }
            })
            .collect();
        (StageStatus::Fail, findings)
    }
}

fn run_composition_check(op: &OpSpec) -> (StageStatus, Vec<Finding>) {
    let can_self_compose = op
        .signature
        .inputs
        .first()
        .is_some_and(|input| *input == op.signature.output);

    if !can_self_compose {
        return (
            StageStatus::Skip,
            vec![Finding {
                message: format!(
                    "{}: output type does not match input type — self-composition not applicable.",
                    op.id
                ),
            }],
        );
    }

    let token = match ProofToken::from_specs(&[op.clone(), op.clone()], ConstructionTime::Manual) {
        Ok(t) => t,
        Err(err) => {
            return (
                StageStatus::Fail,
                vec![Finding {
                    message: format!("{}: proof-token verification failed: {}", op.id, err),
                }],
            );
        }
    };

    if token.theorem_chain.is_empty() {
        return (
            StageStatus::Fail,
            vec![Finding {
                message: format!(
                    "{}: self-composition produces no applicable theorems but the op is type-compatible with self-composition. Fix: declare commutative/associative/identity laws or explain why self-composition is not structurally valid.",
                    op.id
                ),
            }],
        );
    }

    if let Err(msg) = self_composition_witness_diff(op) {
        return (StageStatus::Fail, vec![Finding { message: msg }]);
    }

    (
        StageStatus::Pass,
        vec![Finding {
            message: format!(
                "{}: applicable composition theorems: [{}]",
                op.id,
                token.theorem_chain.join(", ")
            ),
        }],
    )
}

fn self_composition_witness_diff(op: &OpSpec) -> Result<(), String> {
    let inner_input_len = op.signature.min_input_bytes();
    let extra_input_len = op
        .signature
        .inputs
        .iter()
        .skip(1)
        .map(|t| t.min_bytes())
        .sum::<usize>();
    let total_input_len = inner_input_len + extra_input_len;

    if total_input_len == 0 {
        return Err(format!(
            "{}: cannot self-compose variable-length op without expected bytes.",
            op.id
        ));
    }

    let expected_output_len = if op.signature.output.min_bytes() > 0 {
        op.signature.output.min_bytes()
    } else {
        op.expected_output_bytes.unwrap_or(0)
    };

    let test_inputs: Vec<Vec<u8>> = vec![
        vec![0u8; total_input_len],
        (0..total_input_len).map(|i| i as u8).collect(),
        (0..total_input_len)
            .map(|i| (i as u8).wrapping_mul(17).wrapping_add(42))
            .collect(),
    ];

    for input in &test_inputs {
        let inner_out = (op.cpu_fn)(&input[..inner_input_len.min(input.len())]);
        let mut outer_in = inner_out;
        outer_in.extend_from_slice(&input[inner_input_len..]);
        let composed_out = (op.cpu_fn)(&outer_in);

        if expected_output_len > 0 && composed_out.len() != expected_output_len {
            return Err(format!(
                "{}: self-composition witness diff failed: expected output length {} but got {}.",
                op.id,
                expected_output_len,
                composed_out.len()
            ));
        }
    }

    Ok(())
}