zinc_poly/mle.rs
1pub mod dense;
2
3use crypto_primitives::PrimeField;
4pub use dense::DenseMultilinearExtension;
5
6use rand::prelude::*;
7use std::{
8 fmt::Debug,
9 ops::{Add, AddAssign, SubAssign},
10};
11use zinc_utils::mul_by_scalar::MulByScalar;
12
13use crate::EvaluationError;
14
15/// This trait describes an interface for the multilinear extension
16/// of an array.
17/// The latter is a multilinear polynomial represented in terms of its
18/// evaluations over the domain {0,1}^`num_vars` (i.e. the Boolean hypercube).
19///
20/// Index represents a point, which is a vector in {0,1}^`num_vars` in little
21/// endian form. For example, `0b1011` represents `P(1,1,0,1)`
22pub trait MultilinearExtension<T>:
23 Sized
24 + Clone
25 + Debug
26 + PartialEq
27 + Eq
28 + Add
29 + for<'a> AddAssign<&'a Self>
30 + for<'a> AddAssign<(T, &'a Self)>
31 + for<'a> SubAssign<&'a Self>
32{
33 /// Reduce the number of variables of `self` by fixing the
34 /// `partial_point.len()` variables at `partial_point`.
35 fn fix_variables<S>(&mut self, partial_point: &[S], zero: T)
36 where
37 T: for<'a> MulByScalar<&'a S>;
38
39 /// Creates a new object with the number of variables of `self` reduced by
40 /// fixing the `partial_point.len()` variables at `partial_point`.
41 fn fixed_variables<S>(&self, partial_point: &[S], zero: T) -> Self
42 where
43 T: for<'a> MulByScalar<&'a S>;
44}
45
46/// This trait allows to evaluate
47/// multilinear extension types that store
48/// `F::Inner` Montgomery representations
49/// instead of field elements `F` which are typically
50/// an `F::Inner` and the field config.
51pub trait MultilinearExtensionWithConfig<F: PrimeField> {
52 /// Reduce the number of variables of `self` by fixing the
53 /// `partial_point.len()` variables at `partial_point`.
54 fn fix_variables_with_config(&mut self, partial_point: &[F], config: &F::Config);
55
56 /// Creates a new object with the number of variables of `self` reduced by
57 /// fixing the `partial_point.len()` variables at `partial_point`.
58 fn fixed_variables_with_config(&self, partial_point: &[F], config: &F::Config) -> Self;
59
60 /// Evaluate the MLE in full. Asserts that the `point.len()`
61 /// is equal the `self.num_vars`.
62 fn evaluate_with_config(self, point: &[F], config: &F::Config) -> Result<F, EvaluationError>;
63}
64
65pub trait MultilinearExtensionRand<T> {
66 /// Outputs an `l`-variate multilinear extension where value of evaluations
67 /// are sampled uniformly at random.
68 fn rand<R: RngCore + ?Sized>(num_vars: usize, rng: &mut R) -> Self;
69}