ark_poly/evaluations/multivariate/multilinear/mod.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
mod dense;
mod sparse;
pub use dense::DenseMultilinearExtension;
pub use sparse::SparseMultilinearExtension;
use ark_std::{
fmt::Debug,
hash::Hash,
ops::{Add, AddAssign, Index, Neg, SubAssign},
vec::*,
};
use ark_ff::{Field, Zero};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_std::rand::Rng;
use crate::Polynomial;
#[cfg(all(
target_has_atomic = "8",
target_has_atomic = "16",
target_has_atomic = "32",
target_has_atomic = "64",
target_has_atomic = "ptr"
))]
type DefaultHasher = ahash::AHasher;
#[cfg(not(all(
target_has_atomic = "8",
target_has_atomic = "16",
target_has_atomic = "32",
target_has_atomic = "64",
target_has_atomic = "ptr"
)))]
type DefaultHasher = fnv::FnvHasher;
/// This trait describes an interface for the multilinear extension
/// of an array.
/// The latter is a multilinear polynomial represented in terms of its
/// evaluations over the domain {0,1}^`num_vars` (i.e. the Boolean hypercube).
///
/// Index represents a point, which is a vector in {0,1}^`num_vars` in little
/// endian form. For example, `0b1011` represents `P(1,1,0,1)`
pub trait MultilinearExtension<F: Field>:
Sized
+ Clone
+ Debug
+ Hash
+ PartialEq
+ Eq
+ Add
+ Neg
+ Zero
+ CanonicalSerialize
+ CanonicalDeserialize
+ for<'a> AddAssign<&'a Self>
+ for<'a> AddAssign<(F, &'a Self)>
+ for<'a> SubAssign<&'a Self>
+ Index<usize>
+ Polynomial<F, Point = Vec<F>>
{
/// Returns the number of variables in `self`
fn num_vars(&self) -> usize;
/// Outputs an `l`-variate multilinear extension where value of evaluations
/// are sampled uniformly at random.
fn rand<R: Rng>(num_vars: usize, rng: &mut R) -> Self;
/// Relabel the point by swapping `k` scalars from positions `a..a+k` to
/// positions `b..b+k`, and from position `b..b+k` to position `a..a+k`
/// in vector.
///
/// This function turns `P(x_1,...,x_a,...,x_{a+k - 1},...,x_b,...,x_{b+k - 1},...,x_n)`
/// to `P(x_1,...,x_b,...,x_{b+k - 1},...,x_a,...,x_{a+k - 1},...,x_n)`
fn relabel(&self, a: usize, b: usize, k: usize) -> Self;
/// Reduce the number of variables of `self` by fixing the
/// `partial_point.len()` variables at `partial_point`.
fn fix_variables(&self, partial_point: &[F]) -> Self;
/// Returns a list of evaluations over the domain, which is the boolean
/// hypercube. The evaluations are in little-endian order.
fn to_evaluations(&self) -> Vec<F>;
}
/// swap the bits of `x` from position `a..a+n` to `b..b+n` and from `b..b+n` to `a..a+n` in little endian order
pub(crate) fn swap_bits(x: usize, a: usize, b: usize, n: usize) -> usize {
let a_bits = (x >> a) & ((1usize << n) - 1);
let b_bits = (x >> b) & ((1usize << n) - 1);
let local_xor_mask = a_bits ^ b_bits;
let global_xor_mask = (local_xor_mask << a) | (local_xor_mask << b);
x ^ global_xor_mask
}