-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
249695d
commit 61963ec
Showing
8 changed files
with
135 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use std::iter::zip; | ||
|
||
use crate::core::backend::CPUBackend; | ||
use crate::core::fields::m31::BaseField; | ||
use crate::core::fields::qm31::SecureField; | ||
use crate::core::lookups::mle::{Mle, MleOps}; | ||
|
||
impl MleOps<BaseField> for CPUBackend { | ||
fn fix_first(mle: Mle<Self, BaseField>, assignment: SecureField) -> Mle<Self, SecureField> { | ||
let midpoint = mle.len() / 2; | ||
let (lhs_evals, rhs_evals) = mle.split_at(midpoint); | ||
|
||
let res = zip(lhs_evals, rhs_evals) | ||
// Equivalent to `eq(0, assignment) * lhs_eval + eq(1, assignment) * rhs_eval`. | ||
.map(|(&lhs_eval, &rhs_eval)| assignment * (rhs_eval - lhs_eval) + lhs_eval) | ||
.collect(); | ||
|
||
Mle::new(res) | ||
} | ||
} | ||
|
||
impl MleOps<SecureField> for CPUBackend { | ||
fn fix_first(mle: Mle<Self, SecureField>, assignment: SecureField) -> Mle<Self, SecureField> { | ||
let midpoint = mle.len() / 2; | ||
let mut evals = mle.into_evals(); | ||
|
||
for i in 0..midpoint { | ||
let lhs_eval = evals[i]; | ||
let rhs_eval = evals[i + midpoint]; | ||
// Equivalent to `eq(0, assignment) * lhs_eval + eq(1, assignment) * rhs_eval`. | ||
evals[i] = lhs_eval + assignment * (rhs_eval - lhs_eval); | ||
} | ||
|
||
evals.truncate(midpoint); | ||
|
||
Mle::new(evals) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod mle; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use std::ops::Deref; | ||
|
||
use derivative::Derivative; | ||
|
||
use crate::core::backend::{Col, Column, ColumnOps}; | ||
use crate::core::fields::qm31::SecureField; | ||
use crate::core::fields::Field; | ||
|
||
/// TODO | ||
pub trait MleOps<F: Field>: ColumnOps<F> + Sized { | ||
/// Returns a transformed [`Mle`] where the first variable is fixed to `assignment`. | ||
fn fix_first(mle: Mle<Self, F>, assignment: SecureField) -> Mle<Self, SecureField> | ||
where | ||
Self: MleOps<SecureField>; | ||
} | ||
|
||
/// Multilinear Extension stored as evaluations of a multilinear polynomial over the boolean | ||
/// hypercube in bit-reversed order. | ||
#[derive(Derivative)] | ||
#[derivative(Debug(bound = ""), Clone(bound = ""))] | ||
pub struct Mle<B: ColumnOps<F>, F: Field> { | ||
evals: Col<B, F>, | ||
} | ||
|
||
impl<B: MleOps<F>, F: Field> Mle<B, F> { | ||
/// Creates a [`Mle`] from evaluations of a multilinear polynomial on the boolean hypercube. | ||
/// | ||
/// # Panics | ||
/// | ||
/// Panics if the number of evaluations is not a power of two. | ||
pub fn new(evals: Col<B, F>) -> Self { | ||
assert!(evals.len().is_power_of_two()); | ||
Self { evals } | ||
} | ||
|
||
pub fn into_evals(self) -> Col<B, F> { | ||
self.evals | ||
} | ||
|
||
/// Returns a transformed polynomial where the first variable is fixed to `assignment`. | ||
pub fn fix_first(self, assignment: SecureField) -> Mle<B, SecureField> | ||
where | ||
B: MleOps<SecureField>, | ||
{ | ||
B::fix_first(self, assignment) | ||
} | ||
|
||
/// Returns the number of variables in the polynomial. | ||
pub fn n_variables(&self) -> usize { | ||
self.evals.len().ilog2() as usize | ||
} | ||
} | ||
|
||
impl<B: ColumnOps<F>, F: Field> Deref for Mle<B, F> { | ||
type Target = Col<B, F>; | ||
|
||
fn deref(&self) -> &Col<B, F> { | ||
&self.evals | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod mle; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters