Skip to content

Commit

Permalink
Merge pull request #7 from Brechtpd/mpt-refactor-layout
Browse files Browse the repository at this point in the history
MPT layout refactor
  • Loading branch information
Brechtpd authored Mar 1, 2023
2 parents b13b1bd + fb94d83 commit 7c89e6f
Show file tree
Hide file tree
Showing 35 changed files with 5,647 additions and 8,239 deletions.
64 changes: 53 additions & 11 deletions gadgets/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,34 @@ pub mod select {
}
}

/// Trait that implements functionality to get a scalar from
/// commonly used types.
pub trait Scalar<F: FieldExt> {
/// Returns a scalar for the type.
fn scalar(&self) -> F;
}

/// Implementation trait `Scalar` for type able to be casted to u64
#[macro_export]
macro_rules! impl_scalar {
($type:ty) => {
impl<F: halo2_proofs::arithmetic::FieldExt> $crate::util::Scalar<F> for $type {
#[inline]
fn scalar(&self) -> F {
F::from(*self as u64)
}
}
};
($type:ty, $method:path) => {
impl<F: halo2_proofs::arithmetic::FieldExt> $crate::util::Scalar<F> for $type {
#[inline]
fn scalar(&self) -> F {
F::from($method(self) as u64)
}
}
};
}

/// Trait that implements functionality to get a constant expression from
/// commonly used types.
pub trait Expr<F: FieldExt> {
Expand All @@ -143,6 +171,7 @@ pub trait Expr<F: FieldExt> {
#[macro_export]
macro_rules! impl_expr {
($type:ty) => {
$crate::impl_scalar!($type);
impl<F: halo2_proofs::arithmetic::FieldExt> $crate::util::Expr<F> for $type {
#[inline]
fn expr(&self) -> Expression<F> {
Expand All @@ -151,6 +180,7 @@ macro_rules! impl_expr {
}
};
($type:ty, $method:path) => {
$crate::impl_scalar!($type, $method);
impl<F: halo2_proofs::arithmetic::FieldExt> $crate::util::Expr<F> for $type {
#[inline]
fn expr(&self) -> Expression<F> {
Expand All @@ -168,6 +198,25 @@ impl_expr!(isize);
impl_expr!(OpcodeId, OpcodeId::as_u8);
impl_expr!(GasCost, GasCost::as_u64);

impl<F: FieldExt> Scalar<F> for i32 {
#[inline]
fn scalar(&self) -> F {
F::from(self.unsigned_abs() as u64)
* if self.is_negative() {
-F::one()
} else {
F::one()
}
}
}

impl<F: FieldExt> Expr<F> for i32 {
#[inline]
fn expr(&self) -> Expression<F> {
Expression::Constant(self.scalar())
}
}

impl<F: FieldExt> Expr<F> for Expression<F> {
#[inline]
fn expr(&self) -> Expression<F> {
Expand All @@ -182,19 +231,12 @@ impl<F: FieldExt> Expr<F> for &Expression<F> {
}
}

impl<F: FieldExt> Expr<F> for i32 {
/*impl Scalar<dyn FieldExt> for FieldExt {
#[inline]
fn expr(&self) -> Expression<F> {
Expression::Constant(
F::from(self.unsigned_abs() as u64)
* if self.is_negative() {
-F::one()
} else {
F::one()
},
)
fn scalar(&self) -> FieldExt {
self.clone()
}
}
}*/

/// Given a bytes-representation of an expression, it computes and returns the
/// single expression.
Expand Down
Loading

0 comments on commit 7c89e6f

Please sign in to comment.