Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
rutefig committed Jul 9, 2024
1 parent 671cd7a commit 3f35446
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 25 deletions.
5 changes: 4 additions & 1 deletion src/compiler/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ use crate::{
};

use super::{
cse::cse, semantic::{SymTable, SymbolCategory}, setup_inter::{interpret, Setup}, Config, Message, Messages
cse::cse,
semantic::{SymTable, SymbolCategory},
setup_inter::{interpret, Setup},
Config, Message, Messages,
};

/// Contains the result of a compilation.
Expand Down
8 changes: 4 additions & 4 deletions src/compiler/cse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@ impl<F: Field + Hash> CSE<F> {
self.identify_common_subexpressions();

let mut signal_factory = SignalFactory::default();
hashed_step.decomp_constraints(|expr| {
replace_expr(expr, &self.common_exprs, &mut signal_factory)
});
hashed_step
.decomp_constraints(|expr| replace_expr(expr, &self.common_exprs, &mut signal_factory));

// Convert back to StepType<F, ()> and update the original step
*step = hashed_step.without_meta();
Expand Down Expand Up @@ -143,7 +142,8 @@ impl<F: Field + Hash> CSE<F> {
if self.count_map.get(&hash).copied().unwrap_or(0)
>= self.config.min_occurrences.unwrap_or(2) as u32
&& expr.degree() >= self.config.min_degree.unwrap_or(2)
&& !matches!(expr, Expr::MI(_, _)) // Exclude MI expressions
&& !matches!(expr, Expr::MI(_, _))
// Exclude MI expressions
{
Some(expr.clone())
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ use crate::{
pub mod abepi;
#[allow(clippy::module_inception)]
pub mod compiler;
mod cse;
pub mod semantic;
mod setup_inter;
mod cse;

#[derive(Default)]
pub struct Config {
Expand Down
6 changes: 5 additions & 1 deletion src/frontend/dsl/cb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,11 @@ pub fn next_step_must_not_be<F: From<u64>, ST: Into<StepTypeHandler>>(

/// Takes a string annotation and an expression, and returns a new constraint with the given
/// annotation and expression.
pub fn annotate<F, E: Into<PIR<F, ()>>>(annotation: String, expr: E, typing: Typing) -> Constraint<F> {
pub fn annotate<F, E: Into<PIR<F, ()>>>(
annotation: String,
expr: E,
typing: Typing,
) -> Constraint<F> {
Constraint {
annotation,
expr: expr.into(),
Expand Down
3 changes: 2 additions & 1 deletion src/frontend/python/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1763,7 +1763,8 @@ mod tests {
}"#;
let constraint: Constraint<Fr, ()> = serde_json::from_str(json).unwrap();
println!("{:?}", constraint);
let transition_constraint: TransitionConstraint<Fr, ()> = serde_json::from_str(json).unwrap();
let transition_constraint: TransitionConstraint<Fr, ()> =
serde_json::from_str(json).unwrap();
println!("{:?}", transition_constraint);
}

Expand Down
37 changes: 26 additions & 11 deletions src/poly/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub trait ToField<F> {
pub trait Meta: Clone + Default + Debug + Hash {
fn from_expr<F: Field + Hash, V: Debug + Clone + Eq + Hash>(
expr: &Expr<F, V, ()>,
assignments: &VarAssignments<F, V>
assignments: &VarAssignments<F, V>,
) -> Self;
}

Expand All @@ -44,7 +44,7 @@ pub enum Expr<F, V, M: Meta> {
impl Meta for () {
fn from_expr<F: Field + Hash, V: Debug + Clone + Eq + Hash>(
_expr: &Expr<F, V, ()>,
_assignments: &VarAssignments<F, V>
_assignments: &VarAssignments<F, V>,
) -> Self {
()
}
Expand All @@ -53,7 +53,7 @@ impl Meta for () {
impl Meta for HashResult {
fn from_expr<F: Field + Hash, V: Debug + Clone + Eq + Hash>(
expr: &Expr<F, V, ()>,
assignments: &VarAssignments<F, V>
assignments: &VarAssignments<F, V>,
) -> Self {
let hashed_expr = expr.hash(assignments);
hashed_expr.meta().clone()
Expand All @@ -76,7 +76,8 @@ impl<F: Clone, V: Clone, M: Meta> Expr<F, V, M> {
Expr::Pow(se, exp, _) => se.degree() * (*exp as usize),
Expr::Query(_, _) => 1,
Expr::Halo2Expr(_, _) => panic!("not implemented"),
Expr::MI(se, _) => se.degree(), // Treat MI as not changing the degree of the inner expression
Expr::MI(se, _) => se.degree(), /* Treat MI as not changing the degree of the inner
* expression */
}
}

Expand All @@ -93,8 +94,6 @@ impl<F: Clone, V: Clone, M: Meta> Expr<F, V, M> {
}
}



pub fn map_meta<N: Meta, Func: Fn(&M) -> N>(&self, f: Func) -> Expr<F, V, N> {
match self {
Expr::Const(v, m) => Expr::Const(v.clone(), f(m)),
Expand All @@ -115,8 +114,14 @@ impl<F: Field + Hash, V: Debug + Clone + Eq + Hash, M: Meta> Expr<F, V, M> {
let new_meta = N::from_expr(&expr_without_meta, assignments);
match self {
Expr::Const(v, _) => Expr::Const(v.clone(), new_meta),
Expr::Sum(ses, _) => Expr::Sum(ses.iter().map(|e| e.with_meta(assignments)).collect(), new_meta),
Expr::Mul(ses, _) => Expr::Mul(ses.iter().map(|e| e.with_meta(assignments)).collect(), new_meta),
Expr::Sum(ses, _) => Expr::Sum(
ses.iter().map(|e| e.with_meta(assignments)).collect(),
new_meta,
),
Expr::Mul(ses, _) => Expr::Mul(
ses.iter().map(|e| e.with_meta(assignments)).collect(),
new_meta,
),
Expr::Neg(se, _) => Expr::Neg(Box::new(se.with_meta(assignments)), new_meta),
Expr::Pow(se, exp, _) => Expr::Pow(Box::new(se.with_meta(assignments)), *exp, new_meta),
Expr::Query(v, _) => Expr::Query(v.clone(), new_meta),
Expand Down Expand Up @@ -413,7 +418,10 @@ pub struct ConstrDecomp<F, V, M: Meta> {
}

impl<F, V: Debug, M: Meta> ConstrDecomp<F, V, M> {
pub fn get_auto_signal<S: Into<String> + Copy>(&self, annotation: S) -> Option<(&V, &Expr<F, V, M>)> {
pub fn get_auto_signal<S: Into<String> + Copy>(
&self,
annotation: S,
) -> Option<(&V, &Expr<F, V, M>)> {
self.auto_signals.iter().find_map(|(s, e)| {
if format!("{:#?}", s) == annotation.into() {
Some((s, e))
Expand All @@ -438,7 +446,10 @@ impl<F: Clone, V: Clone + Eq + PartialEq + Hash, M: Meta> ConstrDecomp<F, V, M>
self.constrs.push(Expr::Sum(
vec![
expr.clone(),
Expr::Neg(Box::new(Expr::Query(signal.clone(), M::default())), M::default()),
Expr::Neg(
Box::new(Expr::Query(signal.clone(), M::default())),
M::default(),
),
],
M::default(),
));
Expand All @@ -451,7 +462,11 @@ impl<F: Field, V: Clone + Hash + Eq> ConstrDecomp<F, V, HashResult> {
pub fn without_meta(&self) -> ConstrDecomp<F, V, ()> {
ConstrDecomp {
constrs: self.constrs.iter().map(|c| c.without_meta()).collect(),
auto_signals: self.auto_signals.iter().map(|(k, v)| (k.clone(), v.without_meta())).collect(),
auto_signals: self
.auto_signals
.iter()
.map(|(k, v)| (k.clone(), v.without_meta()))
.collect(),
}
}
}
Expand Down
33 changes: 27 additions & 6 deletions src/sbpir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,11 @@ impl<F: Field + Hash, TG: TraceGenerator<F> + Clone, M: Meta> SBPIR<F, TG, M> {
impl<F: Field + Hash, TG: TraceGenerator<F> + Clone, M: Meta> SBPIR<F, TG, M> {
pub fn without_meta(&self) -> SBPIR<F, TG, ()> {
SBPIR {
step_types: self.step_types.iter().map(|(k, v)| (*k, v.without_meta())).collect(),
step_types: self
.step_types
.iter()
.map(|(k, v)| (*k, v.without_meta()))
.collect(),
forward_signals: self.forward_signals.clone(),
shared_signals: self.shared_signals.clone(),
fixed_signals: self.fixed_signals.clone(),
Expand Down Expand Up @@ -442,9 +446,17 @@ impl<F: Field + Hash, M: Meta> StepType<F, M> {
name: self.name.clone(),
signals: self.signals.clone(),
constraints: self.constraints.iter().map(|c| c.without_meta()).collect(),
transition_constraints: self.transition_constraints.iter().map(|c| c.without_meta()).collect(),
transition_constraints: self
.transition_constraints
.iter()
.map(|c| c.without_meta())
.collect(),
lookups: self.lookups.iter().map(|l| l.without_meta()).collect(),
auto_signals: self.auto_signals.iter().map(|(k, v)| (*k, v.without_meta())).collect(),
auto_signals: self
.auto_signals
.iter()
.map(|(k, v)| (*k, v.without_meta()))
.collect(),
annotations: self.annotations.clone(),
}
}
Expand Down Expand Up @@ -534,7 +546,10 @@ pub struct Constraint<F, M: Meta> {
}

impl<F: Field + Hash, M: Meta> Constraint<F, M> {
pub fn with_meta<N: Meta>(&self, assignments: &VarAssignments<F, Queriable<F>>) -> Constraint<F, N> {
pub fn with_meta<N: Meta>(
&self,
assignments: &VarAssignments<F, Queriable<F>>,
) -> Constraint<F, N> {
Constraint {
annotation: self.annotation.clone(),
expr: self.expr.with_meta(assignments),
Expand All @@ -559,7 +574,10 @@ pub struct TransitionConstraint<F, M: Meta> {
}

impl<F: Field + Hash, M: Meta> TransitionConstraint<F, M> {
pub fn with_meta<N: Meta>(&self, assignments: &VarAssignments<F, Queriable<F>>) -> TransitionConstraint<F, N> {
pub fn with_meta<N: Meta>(
&self,
assignments: &VarAssignments<F, Queriable<F>>,
) -> TransitionConstraint<F, N> {
TransitionConstraint {
annotation: self.annotation.clone(),
expr: self.expr.with_meta(assignments),
Expand Down Expand Up @@ -659,7 +677,10 @@ impl<F: Debug + Clone, M: Meta> Lookup<F, M> {
}

impl<F: Field + Hash, M: Meta> Lookup<F, M> {
pub fn with_meta<N: Meta>(&self, assignments: &VarAssignments<F, Queriable<F>>) -> Lookup<F, N> {
pub fn with_meta<N: Meta>(
&self,
assignments: &VarAssignments<F, Queriable<F>>,
) -> Lookup<F, N> {
Lookup {
annotation: self.annotation.clone(),
exprs: self
Expand Down

0 comments on commit 3f35446

Please sign in to comment.