openvm_circuit_primitives/bitwise_op_lookup/
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use openvm_stark_backend::{
    interaction::{InteractionBuilder, InteractionType},
    p3_field::AbstractField,
};

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct BitwiseOperationLookupBus {
    pub index: usize,
}

impl BitwiseOperationLookupBus {
    pub const fn new(index: usize) -> Self {
        Self { index }
    }

    #[must_use]
    pub fn send_range<T>(
        &self,
        x: impl Into<T>,
        y: impl Into<T>,
    ) -> BitwiseOperationLookupBusInteraction<T>
    where
        T: AbstractField,
    {
        self.push(x, y, T::ZERO, T::ZERO, InteractionType::Send)
    }

    #[must_use]
    pub fn send_xor<T>(
        &self,
        x: impl Into<T>,
        y: impl Into<T>,
        z: impl Into<T>,
    ) -> BitwiseOperationLookupBusInteraction<T>
    where
        T: AbstractField,
    {
        self.push(x, y, z, T::ONE, InteractionType::Send)
    }

    #[must_use]
    pub fn receive<T>(
        &self,
        x: impl Into<T>,
        y: impl Into<T>,
        z: impl Into<T>,
        op: impl Into<T>,
    ) -> BitwiseOperationLookupBusInteraction<T> {
        self.push(x, y, z, op, InteractionType::Receive)
    }

    pub fn push<T>(
        &self,
        x: impl Into<T>,
        y: impl Into<T>,
        z: impl Into<T>,
        op: impl Into<T>,
        interaction_type: InteractionType,
    ) -> BitwiseOperationLookupBusInteraction<T> {
        BitwiseOperationLookupBusInteraction {
            x: x.into(),
            y: y.into(),
            z: z.into(),
            op: op.into(),
            bus_index: self.index,
            interaction_type,
        }
    }
}

#[derive(Clone, Copy, Debug)]
pub struct BitwiseOperationLookupBusInteraction<T> {
    pub x: T,
    pub y: T,
    pub z: T,
    pub op: T,
    pub bus_index: usize,
    pub interaction_type: InteractionType,
}

impl<T: AbstractField> BitwiseOperationLookupBusInteraction<T> {
    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.z, self.op],
            count,
            self.interaction_type,
        );
    }
}