Skip to main content

sp1_core_machine/operations/
addrs_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};
5use struct_reflection::{StructReflection, StructReflectionHelper};
6
7use slop_air::AirBuilder;
8use slop_algebra::{AbstractField, Field};
9use sp1_derive::{AlignedBorrow, InputExpr, InputParams, IntoShape, SP1OperationBuilder};
10
11use crate::air::{SP1Operation, WordAirBuilder};
12
13/// A set of columns needed to compute the addition of two Words as an u48 address.
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 AddrAddOperation<T> {
28    /// The result of `a + b` in u48 (three u16 limbs).
29    pub value: [T; 3],
30}
31
32impl<F: Field> AddrAddOperation<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        assert!(expected >> 48 == 0);
36        self.value = [
37            F::from_canonical_u16((expected & 0xFFFF) as u16),
38            F::from_canonical_u16((expected >> 16) as u16),
39            F::from_canonical_u16((expected >> 32) as u16),
40        ];
41        // Range check
42        record.add_u16_range_checks(&u64_to_u16_limbs(expected)[..3]);
43        expected
44    }
45
46    /// Evaluate the add operation.
47    /// Assumes that `a`, `b` are valid `Word`s.
48    /// Constrains that `is_real` is boolean.
49    /// If `is_real` is true, `value` is constrained to a valid u48 address `a + b`.
50    pub fn eval<AB: SP1AirBuilder>(
51        builder: &mut AB,
52        a: Word<AB::Expr>,
53        b: Word<AB::Expr>,
54        cols: AddrAddOperation<AB::Var>,
55        is_real: AB::Expr,
56    ) {
57        // Constrain that `is_real` is boolean.
58        builder.assert_bool(is_real.clone());
59
60        let base = AB::F::from_canonical_u32(1 << 16);
61        let mut builder_is_real = builder.when(is_real.clone());
62        let mut carry = AB::Expr::zero();
63
64        // The set of constraints are
65        //  - carry is initialized to zero
66        //  - 2^16 * carry_next + value[i] = a[i] + b[i] + carry
67        //  - carry is boolean
68        //  - 0 <= value[i] < 2^16
69        for i in 0..WORD_SIZE {
70            let value = if i < WORD_SIZE - 1 { cols.value[i].into() } else { AB::Expr::zero() };
71            carry = (a[i].clone() + b[i].clone() - value + carry) * base.inverse();
72            builder_is_real.assert_bool(carry.clone());
73        }
74
75        // Range check each limb.
76        builder.slice_range_check_u16(&cols.value, is_real);
77    }
78}
79
80#[derive(Debug, Clone, InputParams, InputExpr)]
81pub struct AddrAddOperationInput<AB: SP1AirBuilder> {
82    pub a: Word<AB::Expr>,
83    pub b: Word<AB::Expr>,
84    pub cols: AddrAddOperation<AB::Var>,
85    pub is_real: AB::Expr,
86}
87
88impl<AB: SP1AirBuilder> SP1Operation<AB> for AddrAddOperation<AB::F> {
89    type Input = AddrAddOperationInput<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}