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
mod dense;
mod sparse;
pub use dense::DenseMultilinearExtension;
pub use sparse::SparseMultilinearExtension;
use ark_std::fmt::Debug;
use ark_std::hash::Hash;
use ark_std::ops::{Add, AddAssign, Index, Neg, SubAssign};
use ark_std::vec::Vec;
use ark_ff::{Field, Zero};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_std::rand::Rng;
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>
{
fn num_vars(&self) -> usize;
fn evaluate(&self, point: &[F]) -> Option<F>;
fn rand<R: Rng>(num_vars: usize, rng: &mut R) -> Self;
fn relabel(&self, a: usize, b: usize, k: usize) -> Self;
fn fix_variables(&self, partial_point: &[F]) -> Self;
fn to_evaluations(&self) -> Vec<F>;
}
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
}