Skip to main content

sp1_recursion_machine/chips/poseidon2_helper/
linear.rs

1use core::borrow::Borrow;
2use slop_air::{Air, BaseAir, PairBuilder};
3use slop_algebra::{extension::BinomiallyExtendable, AbstractField, Field, PrimeField32};
4use slop_matrix::Matrix;
5use slop_maybe_rayon::prelude::{IndexedParallelIterator, ParallelIterator, ParallelSliceMut};
6use sp1_derive::AlignedBorrow;
7use sp1_hypercube::{
8    air::MachineAir,
9    next_multiple_of_32,
10    operations::poseidon2::air::{external_linear_layer_mut, internal_linear_layer_mut},
11};
12use sp1_primitives::SP1Field;
13use sp1_recursion_executor::{
14    Address, Block, ExecutionRecord, Instruction, Poseidon2LinearLayerInstr,
15    Poseidon2LinearLayerIo, RecursionProgram, D, PERMUTATION_WIDTH,
16};
17use std::{borrow::BorrowMut, iter::zip, mem::MaybeUninit};
18
19use crate::builder::SP1RecursionAirBuilder;
20
21pub const NUM_LINEAR_ENTRIES_PER_ROW: usize = 1;
22
23#[derive(Default, Clone)]
24pub struct Poseidon2LinearLayerChip;
25
26pub const NUM_LINEAR_COLS: usize = core::mem::size_of::<Poseidon2LinearLayerCols<u8>>();
27
28#[derive(AlignedBorrow, Debug, Clone, Copy)]
29#[repr(C)]
30pub struct Poseidon2LinearLayerCols<F: Copy> {
31    pub values: [Poseidon2LinearLayerValueCols<F>; NUM_LINEAR_ENTRIES_PER_ROW],
32}
33const NUM_LINEAR_VALUE_COLS: usize = core::mem::size_of::<Poseidon2LinearLayerValueCols<u8>>();
34
35#[derive(AlignedBorrow, Debug, Clone, Copy)]
36#[repr(C)]
37pub struct Poseidon2LinearLayerValueCols<F: Copy> {
38    pub input: [Block<F>; 4],
39}
40
41pub const NUM_LINEAR_PREPROCESSED_COLS: usize =
42    core::mem::size_of::<Poseidon2LinearLayerPreprocessedCols<u8>>();
43
44#[derive(AlignedBorrow, Debug, Clone, Copy)]
45#[repr(C)]
46pub struct Poseidon2LinearLayerPreprocessedCols<F: Copy> {
47    pub accesses: [Poseidon2LinearLayerAccessCols<F>; NUM_LINEAR_ENTRIES_PER_ROW],
48}
49
50pub const NUM_LINEAR_ACCESS_COLS: usize =
51    core::mem::size_of::<Poseidon2LinearLayerAccessCols<u8>>();
52
53#[derive(AlignedBorrow, Debug, Clone, Copy)]
54#[repr(C)]
55pub struct Poseidon2LinearLayerAccessCols<F: Copy> {
56    pub addrs: Poseidon2LinearLayerIo<Address<F>>,
57    pub external: F,
58    pub internal: F,
59}
60
61impl<F: Field> BaseAir<F> for Poseidon2LinearLayerChip {
62    fn width(&self) -> usize {
63        NUM_LINEAR_COLS
64    }
65}
66
67impl<F: PrimeField32 + BinomiallyExtendable<D>> MachineAir<F> for Poseidon2LinearLayerChip {
68    type Record = ExecutionRecord<F>;
69
70    type Program = RecursionProgram<F>;
71
72    fn name(&self) -> &'static str {
73        "Poseidon2LinearLayer"
74    }
75
76    fn preprocessed_width(&self) -> usize {
77        NUM_LINEAR_PREPROCESSED_COLS
78    }
79
80    fn preprocessed_num_rows(&self, program: &Self::Program) -> Option<usize> {
81        let instrs_len = program
82            .inner
83            .iter()
84            .filter_map(|instruction| match instruction.inner() {
85                Instruction::Poseidon2LinearLayer(x) => Some(x.as_ref()),
86                _ => None,
87            })
88            .count();
89        self.preprocessed_num_rows_with_instrs_len(program, instrs_len)
90    }
91
92    fn preprocessed_num_rows_with_instrs_len(
93        &self,
94        program: &Self::Program,
95        instrs_len: usize,
96    ) -> Option<usize> {
97        let height = program.shape.as_ref().and_then(|shape| shape.height(self));
98        let nb_rows = instrs_len.div_ceil(NUM_LINEAR_ENTRIES_PER_ROW);
99        Some(next_multiple_of_32(nb_rows, height))
100    }
101
102    fn generate_preprocessed_trace_into(
103        &self,
104        program: &Self::Program,
105        buffer: &mut [MaybeUninit<F>],
106    ) {
107        assert_eq!(
108            std::any::TypeId::of::<F>(),
109            std::any::TypeId::of::<SP1Field>(),
110            "generate_preprocessed_trace only supports SP1Field field"
111        );
112
113        let instrs = program
114            .inner
115            .iter()
116            .filter_map(|instruction| match instruction.inner() {
117                Instruction::Poseidon2LinearLayer(x) => Some(x.as_ref()),
118                _ => None,
119            })
120            .collect::<Vec<_>>();
121
122        let padded_nb_rows =
123            self.preprocessed_num_rows_with_instrs_len(program, instrs.len()).unwrap();
124
125        let buffer_ptr = buffer.as_mut_ptr() as *mut F;
126        let values = unsafe {
127            core::slice::from_raw_parts_mut(
128                buffer_ptr,
129                padded_nb_rows * NUM_LINEAR_PREPROCESSED_COLS,
130            )
131        };
132
133        unsafe {
134            let padding_start = instrs.len() * NUM_LINEAR_ACCESS_COLS;
135            let padding_size = padded_nb_rows * NUM_LINEAR_PREPROCESSED_COLS - padding_start;
136            if padding_size > 0 {
137                core::ptr::write_bytes(buffer[padding_start..].as_mut_ptr(), 0, padding_size);
138            }
139        }
140
141        // Generate the trace rows & corresponding records for each chunk of events in parallel.
142        let populate_len = instrs.len() * NUM_LINEAR_ACCESS_COLS;
143        values[..populate_len].par_chunks_mut(NUM_LINEAR_ACCESS_COLS).zip_eq(instrs).for_each(
144            |(row, instr)| {
145                let Poseidon2LinearLayerInstr { addrs, mults, external } = instr;
146                let access: &mut Poseidon2LinearLayerAccessCols<_> = row.borrow_mut();
147                access.addrs = addrs.to_owned();
148                #[allow(clippy::needless_range_loop)]
149                for i in 0..PERMUTATION_WIDTH / D {
150                    assert!(mults[i] == F::one());
151                }
152                if *external {
153                    access.external = F::one();
154                    access.internal = F::zero();
155                } else {
156                    access.external = F::zero();
157                    access.internal = F::one();
158                }
159            },
160        );
161    }
162
163    fn generate_dependencies(&self, _: &Self::Record, _: &mut Self::Record) {
164        // This is a no-op.
165    }
166
167    fn num_rows(&self, input: &Self::Record) -> Option<usize> {
168        let height = input.program.shape.as_ref().and_then(|shape| shape.height(self));
169        let events = &input.poseidon2_linear_layer_events;
170        let nb_rows = events.len().div_ceil(NUM_LINEAR_ENTRIES_PER_ROW);
171        Some(next_multiple_of_32(nb_rows, height))
172    }
173
174    fn generate_trace_into(
175        &self,
176        input: &ExecutionRecord<F>,
177        _: &mut ExecutionRecord<F>,
178        buffer: &mut [MaybeUninit<F>],
179    ) {
180        assert_eq!(
181            std::any::TypeId::of::<F>(),
182            std::any::TypeId::of::<SP1Field>(),
183            "generate_trace_into only supports SP1Field field"
184        );
185
186        let padded_nb_rows = self.num_rows(input).unwrap();
187        let events = &input.poseidon2_linear_layer_events;
188        let num_event_rows = events.len();
189
190        unsafe {
191            let padding_start = num_event_rows * NUM_LINEAR_COLS;
192            let padding_size = (padded_nb_rows - num_event_rows) * NUM_LINEAR_COLS;
193            if padding_size > 0 {
194                core::ptr::write_bytes(buffer[padding_start..].as_mut_ptr(), 0, padding_size);
195            }
196        }
197
198        let buffer_ptr = buffer.as_mut_ptr() as *mut F;
199        let values = unsafe {
200            core::slice::from_raw_parts_mut(buffer_ptr, num_event_rows * NUM_LINEAR_COLS)
201        };
202
203        // Generate the trace rows & corresponding records for each chunk of events in parallel.
204        let populate_len = events.len() * NUM_LINEAR_VALUE_COLS;
205        values[..populate_len].par_chunks_mut(NUM_LINEAR_VALUE_COLS).zip_eq(events).for_each(
206            |(row, &vals)| {
207                let cols: &mut Poseidon2LinearLayerValueCols<_> = row.borrow_mut();
208                cols.input = vals.input.to_owned();
209            },
210        );
211    }
212
213    fn included(&self, _record: &Self::Record) -> bool {
214        true
215    }
216}
217
218impl<AB> Air<AB> for Poseidon2LinearLayerChip
219where
220    AB: SP1RecursionAirBuilder + PairBuilder,
221{
222    fn eval(&self, builder: &mut AB) {
223        let main = builder.main();
224        let local = main.row_slice(0);
225        let local: &Poseidon2LinearLayerCols<AB::Var> = (*local).borrow();
226        let prep = builder.preprocessed();
227        let prep_local = prep.row_slice(0);
228        let prep_local: &Poseidon2LinearLayerPreprocessedCols<AB::Var> = (*prep_local).borrow();
229
230        for (
231            Poseidon2LinearLayerValueCols { input },
232            Poseidon2LinearLayerAccessCols { addrs, external, internal },
233        ) in zip(local.values, prep_local.accesses)
234        {
235            // Check that the `external`, `internal` flags are boolean, and at most one is on.
236            let is_real = external + internal;
237            builder.assert_bool(external);
238            builder.assert_bool(internal);
239            builder.assert_bool(is_real.clone());
240
241            // Read the inputs from memory. The inputs are packed in extension elements.
242            #[allow(clippy::needless_range_loop)]
243            for i in 0..PERMUTATION_WIDTH / D {
244                builder.receive_block(addrs.input[i], input[i], is_real.clone());
245            }
246
247            let mut state_external: [_; PERMUTATION_WIDTH] =
248                core::array::from_fn(|_| AB::Expr::zero());
249            let mut state_internal: [_; PERMUTATION_WIDTH] =
250                core::array::from_fn(|_| AB::Expr::zero());
251
252            // Unpack the extension elements into field elements.
253            for i in 0..PERMUTATION_WIDTH / D {
254                for j in 0..D {
255                    state_external[i * D + j] = input[i].0[j].into();
256                    state_internal[i * D + j] = input[i].0[j].into();
257                }
258            }
259
260            // Apply the external/internal linear layer.
261            external_linear_layer_mut(&mut state_external);
262            internal_linear_layer_mut(&mut state_internal);
263
264            // Write the output to memory for each case.
265            for i in 0..PERMUTATION_WIDTH / D {
266                builder.send_block(
267                    Address(addrs.output[i].0.into()),
268                    Block([
269                        state_external[i * D].clone(),
270                        state_external[i * D + 1].clone(),
271                        state_external[i * D + 2].clone(),
272                        state_external[i * D + 3].clone(),
273                    ]),
274                    external,
275                );
276                builder.send_block(
277                    Address(addrs.output[i].0.into()),
278                    Block([
279                        state_internal[i * D].clone(),
280                        state_internal[i * D + 1].clone(),
281                        state_internal[i * D + 2].clone(),
282                        state_internal[i * D + 3].clone(),
283                    ]),
284                    internal,
285                );
286            }
287        }
288    }
289}
290
291#[cfg(test)]
292mod tests {
293    use slop_matrix::Matrix;
294    use sp1_hypercube::air::MachineAir;
295    use sp1_recursion_executor::ExecutionRecord;
296
297    use super::Poseidon2LinearLayerChip;
298
299    use crate::chips::test_fixtures;
300
301    #[tokio::test]
302    async fn generate_trace() {
303        let shard = test_fixtures::shard().await;
304        let trace = Poseidon2LinearLayerChip.generate_trace(shard, &mut ExecutionRecord::default());
305        assert!(trace.height() > test_fixtures::MIN_ROWS);
306    }
307
308    #[tokio::test]
309    async fn generate_preprocessed_trace() {
310        let program = &test_fixtures::program_with_input().await.0;
311        let trace = Poseidon2LinearLayerChip.generate_preprocessed_trace(program).unwrap();
312        assert!(trace.height() > test_fixtures::MIN_ROWS);
313    }
314}