vyre-conform 0.1.0

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

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

use super::BOUNDARY_VALUES;

fn resolve_pair(
    op_id: &str,
    inner_op: &str,
    dual_op: &str,
) -> Result<(fn(&[u8]) -> Vec<u8>, fn(&[u8]) -> Vec<u8>), LawViolation> {
    let inner = lookup_binary(inner_op).ok_or_else(|| {
        missing_companion_violation(op_id, "DeMorgan", inner_op, "inner", "u32,u32 -> u32")
    })?;
    let dual = lookup_binary(dual_op).ok_or_else(|| {
        missing_companion_violation(op_id, "DeMorgan", dual_op, "dual", "u32,u32 -> u32")
    })?;
    Ok((inner, dual))
}

/// Verify `f(inner(a,b)) = dual(f(a), f(b))` over u8 plus u32 witnesses.
#[inline]
pub(crate) fn check_exhaustive_u8(
    op_id: &str,
    f: fn(&[u8]) -> Vec<u8>,
    inner_op: &str,
    dual_op: &str,
    witness_count: u64,
) -> Result<(u64, Option<LawViolation>), LawViolation> {
    let (inner, dual) = match resolve_pair(op_id, inner_op, dual_op) {
        Ok(pair) => pair,
        Err(v) => return Ok((0, Some(v))),
    };
    let mut cases = 0u64;
    for a in 0u32..256 {
        for b in 0u32..256 {
            cases += 1;
            if let Some(v) = check_case(op_id, f, inner, dual, a, b)? {
                return Ok((cases, Some(v)));
            }
        }
    }
    check_witnesses(op_id, f, inner, dual, cases, witness_count)
}

/// Verify `f(inner(a,b)) = dual(f(a), f(b))` over deterministic u32 witnesses.
#[inline]
pub(crate) fn check_witnessed_u32(
    op_id: &str,
    f: fn(&[u8]) -> Vec<u8>,
    inner_op: &str,
    dual_op: &str,
    count: u64,
) -> Result<(u64, Option<LawViolation>), LawViolation> {
    let (inner, dual) = match resolve_pair(op_id, inner_op, dual_op) {
        Ok(pair) => pair,
        Err(v) => return Ok((0, Some(v))),
    };
    check_witnesses(op_id, f, inner, dual, 0, count)
}

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

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

fn check_case(
    op_id: &str,
    f: fn(&[u8]) -> Vec<u8>,
    inner: fn(&[u8]) -> Vec<u8>,
    dual: fn(&[u8]) -> Vec<u8>,
    a: u32,
    b: u32,
) -> Result<Option<LawViolation>, LawViolation> {
    let inner_ab = call_binary(inner, a, b).map_err(|e| engine_failure_violation(op_id, e))?;
    let lhs = call_unary(f, inner_ab).map_err(|e| engine_failure_violation(op_id, e))?;
    let fa = call_unary(f, a).map_err(|e| engine_failure_violation(op_id, e))?;
    let fb = call_unary(f, b).map_err(|e| engine_failure_violation(op_id, e))?;
    let rhs = call_binary(dual, fa, fb).map_err(|e| engine_failure_violation(op_id, e))?;
    if lhs == rhs {
        Ok(None)
    } else {
        Ok(Some(violation(op_id, "de-morgan", a, b, 0, lhs, rhs)))
    }
}

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)
}