forked from zcash/halo2
-
Notifications
You must be signed in to change notification settings - Fork 129
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
adria0
committed
Jun 17, 2024
1 parent
f3843ee
commit 09aa0b0
Showing
28 changed files
with
315 additions
and
250 deletions.
There are no files selected for viewing
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
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
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
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
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,74 @@ | ||
use super::ExprRef; | ||
use super::Expression; | ||
|
||
use halo2_middleware::ff::Field; | ||
|
||
/// A field that allows to store `Expression`s in an arena. | ||
pub trait FieldFront: Field { | ||
// Since base trait is not referenciable, we need a way to access it. | ||
// This is necessary to provide a way to transform fields from/to backend. | ||
type Field: Field; | ||
|
||
/// Transform FieldFront to Field | ||
fn into_field(self) -> Self::Field; | ||
|
||
/// Transform Field to FieldFront | ||
fn into_fieldfront(f: Self::Field) -> Self; | ||
|
||
/// Allocate a new expression to the arena. | ||
fn alloc(expr: Expression<Self>) -> ExprRef<Self>; | ||
|
||
/// Get an expression from the arena, panics if the reference is invalid. | ||
fn get(ref_: &ExprRef<Self>) -> Expression<Self>; | ||
} | ||
|
||
#[derive(Default)] | ||
struct ExprArena<F: FieldFront>(Vec<Expression<F>>); | ||
|
||
impl<F: FieldFront> ExprArena<F> { | ||
fn push(&mut self, expr: Expression<F>) -> crate::plonk::ExprRef<F> { | ||
let index = self.0.len(); | ||
self.0.push(expr); | ||
ExprRef(index, std::marker::PhantomData) | ||
} | ||
fn get(&self, ref_: crate::plonk::ExprRef<F>) -> &Expression<F> { | ||
&self.0[ref_.0] | ||
} | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! expression_arena { | ||
($arena:ident, $field:ty) => { | ||
fn $arena() -> &'static std::sync::RwLock<ExprArena<$field>> { | ||
static LINES: std::sync::OnceLock<std::sync::RwLock<ExprArena<$field>>> = | ||
std::sync::OnceLock::new(); | ||
LINES.get_or_init(|| std::sync::RwLock::new(ExprArena::default())) | ||
} | ||
|
||
impl $crate::plonk::FieldFront for $field { | ||
type Field = $field; | ||
fn into_field(self) -> Self::Field { | ||
self | ||
} | ||
fn into_fieldfront(f: Self::Field) -> Self { | ||
f | ||
} | ||
fn alloc(expr: $crate::plonk::Expression<Self>) -> $crate::plonk::ExprRef<Self> { | ||
$arena().write().unwrap().push(expr) | ||
} | ||
fn get(ref_: &$crate::plonk::ExprRef<Self>) -> $crate::plonk::Expression<Self> { | ||
*$arena().read().unwrap().get(*ref_) | ||
} | ||
} | ||
impl From<$field> for $crate::plonk::ExprRef<$field> { | ||
fn from(f: $field) -> Self { | ||
$crate::plonk::FieldFront::alloc($crate::plonk::Expression::Constant(f)) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
expression_arena!(arena_bn256_fr, halo2curves::bn256::Fr); | ||
expression_arena!(arena_bn256_fq, halo2curves::bn256::Fq); | ||
expression_arena!(arena_pasta_fp, halo2curves::pasta::Fp); | ||
expression_arena!(arena_pasta_fq, halo2curves::pasta::Fq); |
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
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
Oops, something went wrong.