Skip to main content

slop_sumcheck/
backend.rs

1use slop_algebra::{Field, UnivariatePolynomial};
2use slop_alloc::{Backend, HasBackend};
3
4use crate::{ComponentPoly, SumcheckPoly, SumcheckPolyBase, SumcheckPolyFirstRound};
5
6/// A trait to enable backend implementations of component polynomials.
7///
8/// An implementation of this trait for a type will imply a [crate::ComponentPoly] implementation
9pub trait ComponentPolyEvalBackend<P, K>: Backend
10where
11    P: SumcheckPolyBase + HasBackend<Backend = Self>,
12{
13    fn get_component_poly_evals(poly: &P) -> Vec<K>;
14}
15
16impl<K, P> ComponentPoly<K> for P
17where
18    K: Field,
19    P: SumcheckPolyBase + HasBackend + Sync,
20    P::Backend: ComponentPolyEvalBackend<P, K>,
21{
22    #[inline]
23    fn get_component_poly_evals(&self) -> Vec<K> {
24        P::Backend::get_component_poly_evals(self)
25    }
26}
27
28/// A trait to enable backend implementations of sumcheck polynomials for the first round.
29///
30/// An implementation of this trait for a type will imply a [crate::SumcheckPolyFirstRound]
31/// implementation for that type.
32pub trait SumCheckPolyFirstRoundBackend<P, K>: Backend
33where
34    K: Field,
35    P: SumcheckPolyBase + HasBackend<Backend = Self>,
36{
37    type NextRoundPoly: SumcheckPoly<K>;
38    fn fix_t_variables(poly: P, alpha: K, t: usize) -> Self::NextRoundPoly;
39
40    fn sum_as_poly_in_last_t_variables(
41        poly: &P,
42        claim: Option<K>,
43        t: usize,
44    ) -> UnivariatePolynomial<K>;
45}
46
47impl<K, P> SumcheckPolyFirstRound<K> for P
48where
49    K: Field,
50    P: SumcheckPolyBase + ComponentPoly<K> + HasBackend + Send + Sync,
51    P::Backend: SumCheckPolyFirstRoundBackend<P, K>,
52{
53    type NextRoundPoly = <P::Backend as SumCheckPolyFirstRoundBackend<P, K>>::NextRoundPoly;
54    #[inline]
55    fn fix_t_variables(self, alpha: K, t: usize) -> Self::NextRoundPoly {
56        P::Backend::fix_t_variables(self, alpha, t)
57    }
58
59    #[inline]
60    fn sum_as_poly_in_last_t_variables(
61        &self,
62        claim: Option<K>,
63        t: usize,
64    ) -> UnivariatePolynomial<K> {
65        P::Backend::sum_as_poly_in_last_t_variables(self, claim, t)
66    }
67}
68
69/// A trait to enable backend implementations of sumcheck polynomials.
70///
71/// An implementation of this trait for a type will imply a [crate::SumcheckPoly] implementation
72pub trait SumcheckPolyBackend<P, K>: Backend
73where
74    K: Field,
75    P: SumcheckPolyBase + ComponentPoly<K> + HasBackend<Backend = Self>,
76{
77    fn fix_last_variable(poly: P, alpha: K) -> P;
78
79    fn sum_as_poly_in_last_variable(poly: &P, claim: Option<K>) -> UnivariatePolynomial<K>;
80}
81
82impl<K, P> SumcheckPoly<K> for P
83where
84    K: Field,
85    P: SumcheckPolyBase + ComponentPoly<K> + HasBackend + Send + Sync,
86    P::Backend: SumcheckPolyBackend<P, K>,
87{
88    #[inline]
89    fn fix_last_variable(self, alpha: K) -> Self {
90        P::Backend::fix_last_variable(self, alpha)
91    }
92
93    #[inline]
94    fn sum_as_poly_in_last_variable(&self, claim: Option<K>) -> UnivariatePolynomial<K> {
95        P::Backend::sum_as_poly_in_last_variable(self, claim)
96    }
97}