sp1_core_machine/operations/
not.rs

1use p3_air::AirBuilder;
2use p3_field::{AbstractField, Field};
3use sp1_core_executor::{events::ByteRecord, ByteOpcode};
4use sp1_derive::AlignedBorrow;
5use sp1_primitives::consts::WORD_SIZE;
6use sp1_stark::{air::SP1AirBuilder, Word};
7
8/// A set of columns needed to compute the not of a word.
9#[derive(AlignedBorrow, Default, Debug, Clone, Copy)]
10#[repr(C)]
11pub struct NotOperation<T> {
12    /// The result of `!x`.
13    pub value: Word<T>,
14}
15
16impl<F: Field> NotOperation<F> {
17    pub fn populate(&mut self, record: &mut impl ByteRecord, x: u32) -> u32 {
18        let expected = !x;
19        let x_bytes = x.to_le_bytes();
20        for i in 0..WORD_SIZE {
21            self.value[i] = F::from_canonical_u8(!x_bytes[i]);
22        }
23        record.add_u8_range_checks(&x_bytes);
24        expected
25    }
26
27    #[allow(unused_variables)]
28    pub fn eval<AB: SP1AirBuilder>(
29        builder: &mut AB,
30        a: Word<AB::Var>,
31        cols: NotOperation<AB::Var>,
32        is_real: impl Into<AB::Expr> + Copy,
33    ) {
34        for i in (0..WORD_SIZE).step_by(2) {
35            builder.send_byte_pair(
36                AB::F::from_canonical_u32(ByteOpcode::U8Range as u32),
37                AB::F::zero(),
38                AB::F::zero(),
39                a[i],
40                a[i + 1],
41                is_real,
42            );
43        }
44
45        // For any byte b, b + !b = 0xFF.
46        for i in 0..WORD_SIZE {
47            builder
48                .when(is_real)
49                .assert_eq(cols.value[i] + a[i], AB::F::from_canonical_u8(u8::MAX));
50        }
51    }
52}