vyre-conform 0.1.0

Conformance suite for vyre backends — proves byte-identical output to CPU reference
Documentation
//! Checker for `DistributiveOver`.

use crate::proof::algebra::checker::support::{
    call_binary, engine_failure_violation, missing_companion_violation, simple_rng, violation,
};
use crate::spec::law::LawViolation;
use crate::{spec::op_registry, DataType};

use super::BOUNDARY_VALUES;

/// Verify `f(a, g(b,c)) = g(f(a,b), f(a,c))` over u8 plus u32 witnesses.
#[inline]
pub(crate) fn check_exhaustive_u8(
    op_id: &str,
    f: fn(&[u8]) -> Vec<u8>,
    over_op: &str,
    witness_count: u64,
) -> Result<(u64, Option<LawViolation>), LawViolation> {
    let Some(g) = lookup_binary(over_op) else {
        return Ok((
            0,
            Some(missing_companion_violation(
                op_id,
                "DistributiveOver",
                over_op,
                "binary",
                "u32,u32 -> u32",
            )),
        ));
    };
    let mut cases = 0u64;
    for a in 0u32..256 {
        for b in 0u32..256 {
            for c in 0u32..256 {
                cases += 1;
                if let Some(v) = check_case(op_id, f, g, a, b, c)? {
                    return Ok((cases, Some(v)));
                }
            }
        }
    }
    check_witnesses(op_id, f, g, cases, witness_count)
}

/// Verify `f(a, g(b,c)) = g(f(a,b), f(a,c))` over deterministic u32 witnesses.
#[inline]
pub(crate) fn check_witnessed_u32(
    op_id: &str,
    f: fn(&[u8]) -> Vec<u8>,
    over_op: &str,
    count: u64,
) -> Result<(u64, Option<LawViolation>), LawViolation> {
    let Some(g) = lookup_binary(over_op) else {
        return Ok((
            0,
            Some(missing_companion_violation(
                op_id,
                "DistributiveOver",
                over_op,
                "binary",
                "u32,u32 -> u32",
            )),
        ));
    };
    check_witnesses(op_id, f, g, 0, count)
}

fn check_witnesses(
    op_id: &str,
    f: fn(&[u8]) -> Vec<u8>,
    g: fn(&[u8]) -> Vec<u8>,
    mut cases: u64,
    count: u64,
) -> Result<(u64, Option<LawViolation>), LawViolation> {
    for &a in BOUNDARY_VALUES {
        for &b in BOUNDARY_VALUES {
            for &c in BOUNDARY_VALUES {
                cases += 1;
                if let Some(v) = check_case(op_id, f, g, a, b, c)? {
                    return Ok((cases, Some(v)));
                }
            }
        }
    }

    let mut rng = simple_rng(op_id, "distributive");
    for _ in 0..count {
        let a = rng.next_u32();
        let b = rng.next_u32();
        let c = rng.next_u32();
        cases += 1;
        if let Some(v) = check_case(op_id, f, g, a, b, c)? {
            return Ok((cases, Some(v)));
        }
    }
    Ok((cases, None))
}

fn check_case(
    op_id: &str,
    f: fn(&[u8]) -> Vec<u8>,
    g: fn(&[u8]) -> Vec<u8>,
    a: u32,
    b: u32,
    c: u32,
) -> Result<Option<LawViolation>, LawViolation> {
    // Left distributive: f(a, g(b,c)) = g(f(a,b), f(a,c))
    let g_bc = call_binary(g, b, c).map_err(|e| engine_failure_violation(op_id, e))?;
    let lhs = call_binary(f, a, g_bc).map_err(|e| engine_failure_violation(op_id, e))?;
    let f_ab = call_binary(f, a, b).map_err(|e| engine_failure_violation(op_id, e))?;
    let f_ac = call_binary(f, a, c).map_err(|e| engine_failure_violation(op_id, e))?;
    let rhs = call_binary(g, f_ab, f_ac).map_err(|e| engine_failure_violation(op_id, e))?;
    if lhs != rhs {
        return Ok(Some(violation(op_id, "distributive", a, b, c, lhs, rhs)));
    }

    // Right distributive: f(g(b,c), a) = g(f(b,a), f(c,a))
    let g_bc_right = call_binary(g, b, c).map_err(|e| engine_failure_violation(op_id, e))?;
    let lhs2 = call_binary(f, g_bc_right, a).map_err(|e| engine_failure_violation(op_id, e))?;
    let f_ba = call_binary(f, b, a).map_err(|e| engine_failure_violation(op_id, e))?;
    let f_ca = call_binary(f, c, a).map_err(|e| engine_failure_violation(op_id, e))?;
    let rhs2 = call_binary(g, f_ba, f_ca).map_err(|e| engine_failure_violation(op_id, e))?;
    if lhs2 != rhs2 {
        return Ok(Some(violation(op_id, "distributive", a, b, c, lhs2, rhs2)));
    }

    Ok(None)
}

fn lookup_binary(op_id: &str) -> Option<fn(&[u8]) -> Vec<u8>> {
    op_registry::all_specs()
        .into_iter()
        .find(|spec| {
            spec.id == op_id
                && spec.signature.inputs == [DataType::U32, DataType::U32]
                && spec.signature.output == DataType::U32
        })
        .map(|spec| spec.cpu_fn)
}