use alloc::vec::Vec;
use core::borrow::BorrowMut;
use miden_air_definitions::vm::{
BitwiseOpCols,
trace::chiplets::bitwise::{BITWISE_AND, BITWISE_XOR, OP_CYCLE_LEN, TRACE_WIDTH},
};
use super::{assert_u32, count_and8};
use crate::{Felt, ZERO, operation::OperationError, trace::ChipletTraceFragment};
#[cfg(test)]
mod tests;
const INIT_OPS_CAPACITY: usize = 128;
#[derive(Debug, Clone, Copy)]
enum Op {
And,
Xor,
}
impl Op {
fn selector(self) -> Felt {
match self {
Self::And => BITWISE_AND,
Self::Xor => BITWISE_XOR,
}
}
fn apply(self, a: u32, b: u32) -> u32 {
match self {
Self::And => a & b,
Self::Xor => a ^ b,
}
}
}
#[derive(Debug, Clone, Copy)]
struct BitwiseOp {
op: Op,
a: u32,
b: u32,
}
#[derive(Debug)]
pub(crate) struct Bitwise {
entries: Vec<BitwiseOp>,
}
impl Bitwise {
pub fn new() -> Self {
Self {
entries: Vec::with_capacity(INIT_OPS_CAPACITY),
}
}
pub fn trace_len(&self) -> usize {
self.entries.len() * OP_CYCLE_LEN
}
pub fn u32and(&mut self, a: Felt, b: Felt) -> Result<Felt, OperationError> {
self.record(Op::And, a, b)
}
pub fn u32xor(&mut self, a: Felt, b: Felt) -> Result<Felt, OperationError> {
self.record(Op::Xor, a, b)
}
fn record(&mut self, op: Op, a: Felt, b: Felt) -> Result<Felt, OperationError> {
let a = assert_u32(a)?;
let b = assert_u32(b)?;
self.entries.push(BitwiseOp { op, a, b });
Ok(Felt::from_u32(op.apply(a, b)))
}
pub fn fill_trace(self, trace: &mut ChipletTraceFragment, and8_counts: &mut [u64]) {
debug_assert_eq!(self.trace_len(), trace.len(), "inconsistent trace lengths");
debug_assert_eq!(trace.width(), TRACE_WIDTH, "inconsistent trace widths");
for (row, BitwiseOp { op, a, b }) in self.entries.into_iter().enumerate() {
let a_bytes = a.to_le_bytes();
let b_bytes = b.to_le_bytes();
let mut payload = [ZERO; TRACE_WIDTH];
let cols: &mut BitwiseOpCols<Felt> = payload.as_mut_slice().borrow_mut();
cols.op_flag = op.selector();
cols.a_bytes = a_bytes.map(Felt::from_u8);
cols.b_bytes = b_bytes.map(Felt::from_u8);
cols.and_bytes = core::array::from_fn(|idx| {
let and = a_bytes[idx] & b_bytes[idx];
count_and8(and8_counts, a_bytes[idx], b_bytes[idx]);
Felt::from_u8(and)
});
trace.copy_rows_into(row, &payload);
}
}
}
impl Default for Bitwise {
fn default() -> Self {
Self::new()
}
}