miden-processor 1.0.0-rc.1

Miden VM processor
Documentation
//! Bitwise chiplet bus test.
//!
//! Runs a program with `U32and` + `U32xor` operations and verifies that every bitwise request
//! row emits the expected `BitwiseMsg` on the chiplet-requests side AND that every bitwise
//! chiplet row emits the matching response on the chiplet-responses side.
//!
//! Column-blind by design: the subset matcher in `lookup_harness` compares `(mult, denom)`
//! pairs regardless of which aux column the framework routes them onto.

use alloc::vec::Vec;

use miden_air_definitions::vm::logup::BitwiseMsg;
use miden_core::{
    Felt,
    operations::{Operation, opcodes},
};

use super::super::{
    build_trace_from_ops,
    lookup_harness::{Expectations, InteractionLog},
};
use crate::RowIndex;

#[test]
fn bitwise_chiplet_bus_emits_per_request_row() {
    // Two distinct operand pairs for the two AND requests so per-row denominators differ:
    // a copy-paste bug attaching an expectation to the wrong row can't pass the subset check.
    let a1: u32 = 0x1111_2222;
    let b1: u32 = 0x3333_4444;
    let a2: u32 = 0x5555_6666;
    let b2: u32 = 0x7777_8888;
    let a3: u32 = 0xdead_beef;
    let b3: u32 = 0x1234_5678;

    // `Drop` between ops isn't strictly required under subset semantics (extra pushes are
    // ignored), but it keeps the stack overflow table from growing and matches sibling tests.
    let ops = vec![
        Operation::Push(Felt::from_u32(a1)),
        Operation::Push(Felt::from_u32(b1)),
        Operation::U32and,
        Operation::Drop,
        Operation::Push(Felt::from_u32(a2)),
        Operation::Push(Felt::from_u32(b2)),
        Operation::U32and,
        Operation::Drop,
        Operation::Push(Felt::from_u32(a3)),
        Operation::Push(Felt::from_u32(b3)),
        Operation::U32xor,
        Operation::Drop,
    ];
    let trace = build_trace_from_ops(ops, &[]);
    let log = InteractionLog::new(&trace);
    let main = trace.main_trace();

    let mut exp = Expectations::new(&log);

    // ---- Request side: decoder emits a `-1` push of `BitwiseMsg` at each U32AND/U32XOR row.
    //
    // Operands are hardcoded (not read back from the trace) so a bug that swaps `s0`/`s1` in
    // the request emitter would produce a message with the `a`/`b` fields flipped and fail
    // the subset check. The stack pushes `a` then `b`, so `b` ends up on top (`s0`) and `a`
    // sits at slot 1 (`s1`); `BitwiseMsg::and(s0, s1, c)` = `BitwiseMsg::and(b, a, a & b)`.
    let and_expected = [(a1, b1, a1 & b1), (a2, b2, a2 & b2)];
    let xor_expected = [(a3, b3, a3 ^ b3)];

    let mut and_rows: Vec<RowIndex> = Vec::new();
    let mut xor_rows: Vec<RowIndex> = Vec::new();
    for row in 0..main.core_height() {
        let idx = RowIndex::from(row);
        let op = main.get_op_code(idx).as_canonical_u64();
        if op == opcodes::U32AND as u64 {
            and_rows.push(idx);
        } else if op == opcodes::U32XOR as u64 {
            xor_rows.push(idx);
        }
    }
    assert_eq!(
        and_rows.len(),
        and_expected.len(),
        "request cardinality guardrail: expected {} U32AND rows, found {}",
        and_expected.len(),
        and_rows.len(),
    );
    assert_eq!(
        xor_rows.len(),
        xor_expected.len(),
        "request cardinality guardrail: expected {} U32XOR rows, found {}",
        xor_expected.len(),
        xor_rows.len(),
    );

    for (&row, &(a, b, c)) in and_rows.iter().zip(and_expected.iter()) {
        let msg = BitwiseMsg::and(Felt::from_u32(b), Felt::from_u32(a), Felt::from_u32(c));
        exp.remove(usize::from(row), &msg);
    }
    for (&row, &(a, b, c)) in xor_rows.iter().zip(xor_expected.iter()) {
        let msg = BitwiseMsg::xor(Felt::from_u32(b), Felt::from_u32(a), Felt::from_u32(c));
        exp.remove(usize::from(row), &msg);
    }

    // ---- Response side: each normal bitwise row emits one `BitwiseMsg` with multiplicity +1.
    let mut response_rows_seen = 0usize;
    for row in 0..main.chiplets_height() {
        let idx = RowIndex::from(row);
        if !main.is_bitwise_row(idx) {
            continue;
        }
        response_rows_seen += 1;

        let cols = main.chiplet_cols(idx).bitwise_cols();
        let op = cols.op_flag;
        let a = u32_from_bitwise_bytes(cols.a_bytes);
        let b = u32_from_bitwise_bytes(cols.b_bytes);
        let and = u32_from_bitwise_bytes(cols.and_bytes);
        let xor = a + b - and.double();
        let result = and + op * (xor - and);
        exp.add(row, &BitwiseMsg { op, a, b, result });
    }
    let expected_responses = and_expected.len() + xor_expected.len();
    assert_eq!(
        response_rows_seen, expected_responses,
        "response cardinality guardrail: expected {expected_responses} bitwise rows, found \
         {response_rows_seen}",
    );

    log.assert_contains(&exp);
}

fn u32_from_bitwise_bytes(bytes: [Felt; 4]) -> Felt {
    let bytes = bytes.map(|byte| {
        u8::try_from(byte.as_canonical_u64()).expect("bitwise byte witness should fit in u8")
    });
    Felt::from_u32(u32::from_le_bytes(bytes))
}

/// Regression test for split-trace projection: every chiplet classifier must return `false` past
/// the chiplets-AIR height, including the ordinary bitwise and AEAD stream classifiers.
#[test]
fn rows_past_chiplets_height_are_not_classified_as_chiplets() {
    let trace = build_trace_from_ops(vec![Operation::Noop; 128], &[]);
    let main = trace.main_trace();

    for row in main.chiplets_height()..main.num_rows() {
        let idx = RowIndex::from(row);
        for (chiplet, is_active) in [
            ("controller", main.is_controller_row(idx)),
            ("bitwise", main.is_bitwise_row(idx)),
            ("AEAD stream", main.is_aead_stream_row(idx)),
            ("memory", main.is_memory_row(idx)),
            ("ACE", main.is_ace_row(idx)),
            ("kernel ROM", main.is_kernel_row(idx)),
        ] {
            assert!(!is_active, "row {row} (past chiplets height) classified as {chiplet}");
        }
    }
}