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
447
448
449
450
451
452
453
454
455
456
457
458
459
//! Dispatch for binary algebra law checkers.
//!
//! Investigation report for the April 2026 dead-code finding:
//! - `check_exhaustive_u8` and `check_witnessed_u32` were crate-visible but
//!   had no callers under `conform/src`.
//! - Every standalone checker below was only reachable through those orphaned
//!   helpers, so rustc correctly warned that the module was bypassed.
//! - The active dispatch had been duplicated inline in `algebra/checker.rs`.
//!   That path did execute the same exhaustive checks and a stronger witnessed
//!   path with boundary-grid sweeps.
//! - The fix wires `algebra/checker.rs` back to this module and keeps the
//!   stronger witnessed boundary coverage here, making this file the single
//!   binary-law dispatch engine again.

use crate::proof::algebra::checker::support::{
    boundary_grid_u32, call_binary, engine_failure_violation, simple_rng, violation,
};
use crate::spec::law::{AlgebraicLaw, LawViolation};

/// Verify one binary law over exhaustive u8 inputs.
#[inline]
pub(crate) fn check_exhaustive_u8(
    op_id: &str,
    f: fn(&[u8]) -> Vec<u8>,
    law: &AlgebraicLaw,
) -> Result<(u64, Option<LawViolation>), LawViolation> {
    match law {
        AlgebraicLaw::Commutative => check_commutative_exhaustive(op_id, f),
        AlgebraicLaw::Associative => check_associative_exhaustive(op_id, f),
        AlgebraicLaw::Identity { element } => check_identity_exhaustive(op_id, f, *element),
        AlgebraicLaw::LeftIdentity { element } => {
            crate::proof::algebra::laws::one_sided::check_identity_exhaustive_u8(
                op_id,
                f,
                crate::proof::algebra::laws::one_sided::Side::Left,
                *element,
            )
        }
        AlgebraicLaw::RightIdentity { element } => {
            crate::proof::algebra::laws::one_sided::check_identity_exhaustive_u8(
                op_id,
                f,
                crate::proof::algebra::laws::one_sided::Side::Right,
                *element,
            )
        }
        AlgebraicLaw::SelfInverse { result } => check_self_inverse_exhaustive(op_id, f, *result),
        AlgebraicLaw::Idempotent => check_idempotent_exhaustive(op_id, f),
        AlgebraicLaw::Absorbing { element } => check_absorbing_exhaustive(op_id, f, *element),
        AlgebraicLaw::LeftAbsorbing { element } => {
            crate::proof::algebra::laws::one_sided::check_absorbing_exhaustive_u8(
                op_id,
                f,
                crate::proof::algebra::laws::one_sided::Side::Left,
                *element,
            )
        }
        AlgebraicLaw::RightAbsorbing { element } => {
            crate::proof::algebra::laws::one_sided::check_absorbing_exhaustive_u8(
                op_id,
                f,
                crate::proof::algebra::laws::one_sided::Side::Right,
                *element,
            )
        }
        AlgebraicLaw::Bounded { lo, hi } => check_bounded_exhaustive(op_id, f, *lo, *hi),
        AlgebraicLaw::ZeroProduct { holds } => check_zero_product_exhaustive(op_id, f, *holds),
        AlgebraicLaw::DistributiveOver { over_op } => {
            crate::proof::algebra::laws::distributive::check_exhaustive_u8(op_id, f, over_op, 0)
        }
        AlgebraicLaw::Complement {
            complement_op,
            universe,
        } => crate::proof::algebra::laws::complement::check_binary_exhaustive_u8(
            op_id,
            f,
            complement_op,
            *universe,
            0,
        ),
        AlgebraicLaw::LatticeAbsorption { dual_op } => {
            Ok(crate::proof::algebra::laws::lattice::check_exhaustive_u8(op_id, f, dual_op, 0))
        }
        AlgebraicLaw::InverseOf { op } => {
            crate::proof::algebra::laws::inverse::check_exhaustive_u8(op_id, f, op, 0)
        }
        AlgebraicLaw::Trichotomy {
            less_op,
            equal_op,
            greater_op,
        } => crate::proof::algebra::laws::trichotomy::check_exhaustive_u8(
            op_id, f, less_op, equal_op, greater_op, 0,
        ),
        AlgebraicLaw::Custom {
            name, arity, check, ..
        } => Ok(crate::proof::algebra::laws::custom::check_exhaustive_u8(
            op_id, f, name, *arity, *check,
        )),
        other => Ok((0, Some(LawViolation {
            law: format!("{:?}", other),
            op_id: op_id.to_string(),
            message: format!("unimplemented binary law checker for {:?}. Fix: add a real checker or remove the declared law.", other),
            a: 0, b: 0, c: 0, lhs: 0, rhs: 0,
        }))),
    }
}

/// Verify one binary law over deterministic u32 witnesses.
#[inline]
pub(crate) fn check_witnessed_u32(
    op_id: &str,
    f: fn(&[u8]) -> Vec<u8>,
    law: &AlgebraicLaw,
    count: u64,
) -> Result<(u64, Option<LawViolation>), LawViolation> {
    let mut rng = simple_rng(op_id, law.name());
    let grid = boundary_grid_u32();
    match law {
        AlgebraicLaw::Commutative => {
            for &a in &grid {
                for &b in &grid {
                    let ab = call_binary(f, a, b).map_err(|e| engine_failure_violation(op_id, e))?;
                    let ba = call_binary(f, b, a).map_err(|e| engine_failure_violation(op_id, e))?;
                    if ab != ba {
                        return Ok((1, Some(violation(op_id, "commutative", a, b, 0, ab, ba))));
                    }
                }
            }
            for i in 0..count {
                let a = rng.next_u32();
                let b = rng.next_u32();
                let ab = call_binary(f, a, b).map_err(|e| engine_failure_violation(op_id, e))?;
                let ba = call_binary(f, b, a).map_err(|e| engine_failure_violation(op_id, e))?;
                if ab != ba {
                    return Ok((i + 1, Some(violation(op_id, "commutative", a, b, 0, ab, ba))));
                }
            }
            Ok((count, None))
        }
        AlgebraicLaw::Associative => {
            for &a in &grid {
                for &b in &grid {
                    for &c in &grid {
                        let ab = call_binary(f, a, b).map_err(|e| engine_failure_violation(op_id, e))?;
                        let ab_c = call_binary(f, ab, c).map_err(|e| engine_failure_violation(op_id, e))?;
                        let bc = call_binary(f, b, c).map_err(|e| engine_failure_violation(op_id, e))?;
                        let a_bc = call_binary(f, a, bc).map_err(|e| engine_failure_violation(op_id, e))?;
                        if ab_c != a_bc {
                            return Ok((
                                1,
                                Some(violation(op_id, "associative", a, b, c, ab_c, a_bc)),
                            ));
                        }
                    }
                }
            }
            for i in 0..count {
                let a = rng.next_u32();
                let b = rng.next_u32();
                let c = rng.next_u32();
                let ab = call_binary(f, a, b).map_err(|e| engine_failure_violation(op_id, e))?;
                let ab_c = call_binary(f, ab, c).map_err(|e| engine_failure_violation(op_id, e))?;
                let bc = call_binary(f, b, c).map_err(|e| engine_failure_violation(op_id, e))?;
                let a_bc = call_binary(f, a, bc).map_err(|e| engine_failure_violation(op_id, e))?;
                if ab_c != a_bc {
                    return Ok((
                        i + 1,
                        Some(violation(op_id, "associative", a, b, c, ab_c, a_bc)),
                    ));
                }
            }
            Ok((count, None))
        }
        AlgebraicLaw::Identity { element } => {
            check_identity_witnessed(op_id, f, *element, count, &grid)
        }
        AlgebraicLaw::LeftIdentity { element } => {
            crate::proof::algebra::laws::one_sided::check_identity_witnessed_u32(
                op_id,
                f,
                crate::proof::algebra::laws::one_sided::Side::Left,
                *element,
                count,
            )
        }
        AlgebraicLaw::RightIdentity { element } => {
            crate::proof::algebra::laws::one_sided::check_identity_witnessed_u32(
                op_id,
                f,
                crate::proof::algebra::laws::one_sided::Side::Right,
                *element,
                count,
            )
        }
        AlgebraicLaw::SelfInverse { result } => {
            check_self_inverse_witnessed(op_id, f, *result, count)
        }
        AlgebraicLaw::Idempotent => check_idempotent_witnessed(op_id, f, count),
        AlgebraicLaw::Absorbing { element } => check_absorbing_witnessed(op_id, f, *element, count),
        AlgebraicLaw::LeftAbsorbing { element } => {
            crate::proof::algebra::laws::one_sided::check_absorbing_witnessed_u32(
                op_id,
                f,
                crate::proof::algebra::laws::one_sided::Side::Left,
                *element,
                count,
            )
        }
        AlgebraicLaw::RightAbsorbing { element } => {
            crate::proof::algebra::laws::one_sided::check_absorbing_witnessed_u32(
                op_id,
                f,
                crate::proof::algebra::laws::one_sided::Side::Right,
                *element,
                count,
            )
        }
        AlgebraicLaw::Bounded { lo, hi } => check_bounded_witnessed(op_id, f, *lo, *hi, count),
        AlgebraicLaw::ZeroProduct { holds } => check_zero_product_witnessed(op_id, f, *holds, count),
        AlgebraicLaw::DistributiveOver { over_op } => {
            crate::proof::algebra::laws::distributive::check_witnessed_u32(op_id, f, over_op, count)
        }
        AlgebraicLaw::Complement {
            complement_op,
            universe,
        } => crate::proof::algebra::laws::complement::check_binary_witnessed_u32(
            op_id,
            f,
            complement_op,
            *universe,
            count,
        ),
        AlgebraicLaw::LatticeAbsorption { dual_op } => {
            Ok(crate::proof::algebra::laws::lattice::check_witnessed_u32(op_id, f, dual_op, count))
        }
        AlgebraicLaw::InverseOf { op } => {
            crate::proof::algebra::laws::inverse::check_witnessed_u32(op_id, f, op, count)
        }
        AlgebraicLaw::Trichotomy {
            less_op,
            equal_op,
            greater_op,
        } => crate::proof::algebra::laws::trichotomy::check_witnessed_u32(
            op_id, f, less_op, equal_op, greater_op, count,
        ),
        AlgebraicLaw::Custom {
            name, arity, check, ..
        } => Ok(crate::proof::algebra::laws::custom::check_witnessed_u32(
            op_id, f, name, *arity, *check, count,
        )),
        other => Ok((0, Some(LawViolation {
            law: format!("{:?}", other),
            op_id: op_id.to_string(),
            message: format!("unimplemented binary witnessed law checker for {:?}. Fix: add a real checker or remove the declared law.", other),
            a: 0, b: 0, c: 0, lhs: 0, rhs: 0,
        }))),
    }
}

fn check_commutative_exhaustive(
    op_id: &str,
    f: fn(&[u8]) -> Vec<u8>,
) -> Result<(u64, Option<LawViolation>), LawViolation> {
    let mut cases = 0u64;
    for a in 0u32..256 {
        for b in 0u32..256 {
            let ab = call_binary(f, a, b).map_err(|e| engine_failure_violation(op_id, e))?;
            let ba = call_binary(f, b, a).map_err(|e| engine_failure_violation(op_id, e))?;
            cases += 1;
            if ab != ba {
                return Ok((
                    cases,
                    Some(violation(op_id, "commutative", a, b, 0, ab, ba)),
                ));
            }
        }
    }
    Ok((cases, None))
}

fn check_associative_exhaustive(
    op_id: &str,
    f: fn(&[u8]) -> Vec<u8>,
) -> Result<(u64, Option<LawViolation>), LawViolation> {
    let mut cases = 0u64;
    for a in 0u32..256 {
        for b in 0u32..256 {
            for c in 0u32..256 {
                let ab = call_binary(f, a, b).map_err(|e| engine_failure_violation(op_id, e))?;
                let ab_c = call_binary(f, ab, c).map_err(|e| engine_failure_violation(op_id, e))?;
                let bc = call_binary(f, b, c).map_err(|e| engine_failure_violation(op_id, e))?;
                let a_bc = call_binary(f, a, bc).map_err(|e| engine_failure_violation(op_id, e))?;
                cases += 1;
                if ab_c != a_bc {
                    return Ok((
                        cases,
                        Some(violation(op_id, "associative", a, b, c, ab_c, a_bc)),
                    ));
                }
            }
        }
    }
    Ok((cases, None))
}

fn check_identity_exhaustive(
    op_id: &str,
    f: fn(&[u8]) -> Vec<u8>,
    e: u32,
) -> Result<(u64, Option<LawViolation>), LawViolation> {
    let mut cases = 0u64;
    for a in 0u32..256 {
        cases += 2;
        let ae = call_binary(f, a, e).map_err(|e| engine_failure_violation(op_id, e))?;
        if ae != a {
            return Ok((
                cases,
                Some(violation(op_id, "identity(right)", a, e, 0, ae, a)),
            ));
        }
        let ea = call_binary(f, e, a).map_err(|e| engine_failure_violation(op_id, e))?;
        if ea != a {
            return Ok((
                cases,
                Some(violation(op_id, "identity(left)", e, a, 0, ea, a)),
            ));
        }
    }
    Ok((cases, None))
}

fn check_self_inverse_exhaustive(
    op_id: &str,
    f: fn(&[u8]) -> Vec<u8>,
    r: u32,
) -> Result<(u64, Option<LawViolation>), LawViolation> {
    let mut cases = 0u64;
    for a in 0u32..256 {
        let aa = call_binary(f, a, a).map_err(|e| engine_failure_violation(op_id, e))?;
        cases += 1;
        if aa != r {
            return Ok((
                cases,
                Some(violation(op_id, "self-inverse", a, a, 0, aa, r)),
            ));
        }
    }
    Ok((cases, None))
}

fn check_idempotent_exhaustive(
    op_id: &str,
    f: fn(&[u8]) -> Vec<u8>,
) -> Result<(u64, Option<LawViolation>), LawViolation> {
    let mut cases = 0u64;
    for a in 0u32..256 {
        let aa = call_binary(f, a, a).map_err(|e| engine_failure_violation(op_id, e))?;
        cases += 1;
        if aa != a {
            return Ok((cases, Some(violation(op_id, "idempotent", a, a, 0, aa, a))));
        }
    }
    Ok((cases, None))
}

fn check_absorbing_exhaustive(
    op_id: &str,
    f: fn(&[u8]) -> Vec<u8>,
    z: u32,
) -> Result<(u64, Option<LawViolation>), LawViolation> {
    let mut cases = 0u64;
    for a in 0u32..256 {
        cases += 2;
        let az = call_binary(f, a, z).map_err(|e| engine_failure_violation(op_id, e))?;
        if az != z {
            return Ok((
                cases,
                Some(violation(op_id, "absorbing(right)", a, z, 0, az, z)),
            ));
        }
        let za = call_binary(f, z, a).map_err(|e| engine_failure_violation(op_id, e))?;
        if za != z {
            return Ok((
                cases,
                Some(violation(op_id, "absorbing(left)", z, a, 0, za, z)),
            ));
        }
    }
    Ok((cases, None))
}

fn check_bounded_exhaustive(
    op_id: &str,
    f: fn(&[u8]) -> Vec<u8>,
    lo: u32,
    hi: u32,
) -> Result<(u64, Option<LawViolation>), LawViolation> {
    let mut cases = 0u64;
    for a in 0u32..256 {
        for b in 0u32..256 {
            let ab = call_binary(f, a, b).map_err(|e| engine_failure_violation(op_id, e))?;
            cases += 1;
            if ab < lo || ab > hi {
                return Ok((cases, Some(violation(op_id, "bounded", a, b, 0, ab, lo))));
            }
        }
    }
    Ok((cases, None))
}

fn check_zero_product_exhaustive(
    op_id: &str,
    f: fn(&[u8]) -> Vec<u8>,
    holds: bool,
) -> Result<(u64, Option<LawViolation>), LawViolation> {
    if !holds {
        let mut cases = 0u64;
        for a in 0u32..256 {
            for b in 0u32..256 {
                let ab = call_binary(f, a, b).map_err(|e| engine_failure_violation(op_id, e))?;
                cases += 1;
                if ab == 0 && a != 0 && b != 0 {
                    return Ok((cases, None));
                }
            }
        }
        let zp = call_binary(f, 2, 0x8000_0000).map_err(|e| engine_failure_violation(op_id, e))?;
        return Ok((
            cases,
            Some(violation(
                op_id,
                "zero-product(false)",
                2,
                0x8000_0000,
                0,
                zp,
                0,
            )),
        ));
    }

    let mut cases = 0u64;
    for a in 0u32..256 {
        for b in 0u32..256 {
            let ab = call_binary(f, a, b).map_err(|e| engine_failure_violation(op_id, e))?;
            cases += 1;
            if ab == 0 && a != 0 && b != 0 {
                return Ok((
                    cases,
                    Some(violation(op_id, "zero-product(true)", a, b, 0, ab, 1)),
                ));
            }
        }
    }
    Ok((cases, None))
}

mod witnessed;
use witnessed::*;