Skip to main content

sp1_core_machine/operations/
addw.rs

1use sp1_core_executor::events::ByteRecord;
2use sp1_hypercube::{air::SP1AirBuilder, Word};
3use sp1_primitives::consts::{u32_to_u16_limbs, WORD_SIZE};
4use struct_reflection::{StructReflection, StructReflectionHelper};
5
6use slop_air::AirBuilder;
7use slop_algebra::{AbstractField, Field};
8use sp1_derive::{AlignedBorrow, InputExpr, InputParams, IntoShape, SP1OperationBuilder};
9
10use crate::{
11    air::{SP1Operation, SP1OperationBuilder, WordAirBuilder},
12    operations::{U16MSBOperation, U16MSBOperationInput},
13};
14
15/// A set of columns needed to compute the ADDW of two words.
16#[derive(
17    AlignedBorrow, StructReflection, Default, Debug, Clone, Copy, IntoShape, SP1OperationBuilder,
18)]
19#[repr(C)]
20pub struct AddwOperation<T> {
21    /// The result of the ADDW operation.
22    pub value: [T; WORD_SIZE / 2],
23    /// The msb of the result.
24    pub msb: U16MSBOperation<T>,
25}
26
27impl<F: Field> AddwOperation<F> {
28    pub fn populate(&mut self, record: &mut impl ByteRecord, a_u64: u64, b_u64: u64) {
29        let value = (a_u64 as u32).wrapping_add(b_u64 as u32);
30        let limbs = u32_to_u16_limbs(value);
31        self.value = [F::from_canonical_u16(limbs[0]), F::from_canonical_u16(limbs[1])];
32        // Range check
33        record.add_u16_range_checks(&limbs);
34        self.msb.populate_msb(record, limbs[1]);
35    }
36
37    /// Evaluate the addw operation.
38    /// Assumes that `a`, `b` are valid `Word`s of u16 limbs.
39    /// Constrains that `is_real` is boolean.
40    /// If `is_real` is true, the `value` is constrained to a the lower u32 of the ADDW result.
41    /// Also, the `msb` will be constrained to equal the most significant bit of the `value`.
42    pub fn eval<AB>(
43        builder: &mut AB,
44        a: Word<AB::Expr>,
45        b: Word<AB::Expr>,
46        cols: AddwOperation<AB::Var>,
47        is_real: AB::Expr,
48    ) where
49        AB: SP1AirBuilder + SP1OperationBuilder<U16MSBOperation<<AB as AirBuilder>::F>>,
50    {
51        builder.assert_bool(is_real.clone());
52
53        let base = AB::F::from_canonical_u32(1 << 16);
54        let mut builder_is_real = builder.when(is_real.clone());
55        let mut carry = AB::Expr::zero();
56
57        // The set of constraints are
58        //  - carry is initialized to zero
59        //  - 2^16 * carry_next + value[i] = a[i] + b[i] + carry
60        //  - carry is boolean
61        //  - 0 <= value[i] < 2^16
62        for i in 0..WORD_SIZE / 2 {
63            carry = (a[i].clone() + b[i].clone() - cols.value[i] + carry) * base.inverse();
64            builder_is_real.assert_bool(carry.clone());
65        }
66
67        // Range check each limb.
68        builder.slice_range_check_u16(&cols.value, is_real.clone());
69
70        <U16MSBOperation<AB::F> as SP1Operation<AB>>::eval(
71            builder,
72            U16MSBOperationInput::new(cols.value[1].into(), cols.msb, is_real.clone()),
73        );
74    }
75}
76
77#[derive(Debug, Clone, InputParams, InputExpr)]
78pub struct AddwOperationInput<AB: SP1AirBuilder> {
79    pub a: Word<AB::Expr>,
80    pub b: Word<AB::Expr>,
81    pub cols: AddwOperation<AB::Var>,
82    pub is_real: AB::Expr,
83}
84
85impl<AB> SP1Operation<AB> for AddwOperation<AB::F>
86where
87    AB: SP1AirBuilder + SP1OperationBuilder<U16MSBOperation<<AB as AirBuilder>::F>>,
88{
89    type Input = AddwOperationInput<AB>;
90    type Output = ();
91
92    fn lower(builder: &mut AB, input: Self::Input) -> Self::Output {
93        Self::eval(builder, input.a, input.b, input.cols, input.is_real);
94    }
95}