openvm_circuit_primitives/xor/
bus.rs

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
use openvm_stark_backend::{
    interaction::{InteractionBuilder, InteractionType},
    p3_field::AbstractField,
};

/// Represents a bus for `(x, y, x ^ y)` identified by a unique bus index (`usize`).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct XorBus(pub usize);

impl XorBus {
    pub fn send<T>(
        &self,
        x: impl Into<T>,
        y: impl Into<T>,
        x_xor_y: impl Into<T>,
    ) -> XorBusInteraction<T> {
        self.push(x, y, x_xor_y, InteractionType::Send)
    }

    pub fn receive<T>(
        &self,
        x: impl Into<T>,
        y: impl Into<T>,
        x_xor_y: impl Into<T>,
    ) -> XorBusInteraction<T> {
        self.push(x, y, x_xor_y, InteractionType::Receive)
    }

    pub fn push<T>(
        &self,
        x: impl Into<T>,
        y: impl Into<T>,
        x_xor_y: impl Into<T>,
        interaction_type: InteractionType,
    ) -> XorBusInteraction<T> {
        XorBusInteraction {
            x: x.into(),
            y: y.into(),
            x_xor_y: x_xor_y.into(),
            bus_index: self.0,
            interaction_type,
        }
    }
}

#[derive(Clone, Copy, Debug)]
pub struct XorBusInteraction<T> {
    pub x: T,
    pub y: T,
    pub x_xor_y: T,

    pub bus_index: usize,
    pub interaction_type: InteractionType,
}

impl<T: AbstractField> XorBusInteraction<T> {
    /// Finalizes and sends/receives over the Xor bus.
    pub fn eval<AB>(self, builder: &mut AB, count: impl Into<AB::Expr>)
    where
        AB: InteractionBuilder<Expr = T>,
    {
        builder.push_interaction(
            self.bus_index,
            [self.x, self.y, self.x_xor_y],
            count,
            self.interaction_type,
        );
    }
}