sp1_recursion_executor/
block.rs1use serde::{Deserialize, Serialize};
2use slop_algebra::{AbstractField, ExtensionField, Field};
3use sp1_derive::AlignedBorrow;
4use sp1_hypercube::air::{BinomialExtension, ExtensionAirBuilder, SP1AirBuilder};
5
6use std::ops::{Index, IndexMut};
7
8use crate::D;
9
10#[derive(
12 AlignedBorrow, Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize,
13)]
14#[repr(C)]
15pub struct Block<T>(pub [T; D]);
16
17impl<T> Block<T> {
18 pub fn map<F, U>(self, f: F) -> Block<U>
19 where
20 F: FnMut(T) -> U,
21 {
22 Block(self.0.map(f))
23 }
24
25 pub fn ext<E>(&self) -> E
26 where
27 T: Field,
28 E: ExtensionField<T>,
29 {
30 E::from_base_fn(|i| self.0[i])
31 }
32}
33
34impl<T: Clone> Block<T> {
35 pub fn as_extension<AB: ExtensionAirBuilder<Var = T>>(&self) -> BinomialExtension<AB::Expr> {
36 let arr: [AB::Expr; 4] = self.0.clone().map(|x| AB::Expr::zero() + x);
37 BinomialExtension(arr)
38 }
39
40 pub fn as_extension_from_base<AB: SP1AirBuilder<Var = T>>(
41 &self,
42 base: AB::Expr,
43 ) -> BinomialExtension<AB::Expr> {
44 let mut arr: [AB::Expr; 4] = self.0.clone().map(|_| AB::Expr::zero());
45 arr[0] = base;
46
47 BinomialExtension(arr)
48 }
49}
50
51impl<T> From<[T; D]> for Block<T> {
52 fn from(arr: [T; D]) -> Self {
53 Self(arr)
54 }
55}
56
57impl<T: AbstractField> From<T> for Block<T> {
58 fn from(value: T) -> Self {
59 Self([value, T::zero(), T::zero(), T::zero()])
60 }
61}
62
63impl<T: Copy> From<&[T]> for Block<T> {
64 fn from(slice: &[T]) -> Self {
65 let arr: [T; D] = slice.try_into().unwrap();
66 Self(arr)
67 }
68}
69
70impl<T, I> Index<I> for Block<T>
71where
72 [T]: Index<I>,
73{
74 type Output = <[T] as Index<I>>::Output;
75
76 #[inline]
77 fn index(&self, index: I) -> &Self::Output {
78 Index::index(&self.0, index)
79 }
80}
81
82impl<T, I> IndexMut<I> for Block<T>
83where
84 [T]: IndexMut<I>,
85{
86 #[inline]
87 fn index_mut(&mut self, index: I) -> &mut Self::Output {
88 IndexMut::index_mut(&mut self.0, index)
89 }
90}
91
92impl<T> IntoIterator for Block<T> {
93 type Item = T;
94 type IntoIter = std::array::IntoIter<T, D>;
95
96 fn into_iter(self) -> Self::IntoIter {
97 self.0.into_iter()
98 }
99}