Skip to main content

sp1_core_machine/operations/
add.rs

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