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

Blockhash: some refactoring #117

Merged
merged 1 commit into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions zkevm-circuits/src/evm_circuit/util/constraint_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,15 @@ impl<F: Field> ReversionInfo<F> {
pub struct BaseConstraintBuilder<F> {
pub constraints: Vec<(&'static str, Expression<F>)>,
pub max_degree: usize,
pub condition: Option<Expression<F>>,
pub conditions: Vec<Expression<F>>,
}

impl<F: Field> BaseConstraintBuilder<F> {
pub(crate) fn new(max_degree: usize) -> Self {
BaseConstraintBuilder {
constraints: Vec::new(),
max_degree,
condition: None,
conditions: Vec::new(),
}
}

Expand Down Expand Up @@ -189,27 +189,32 @@ impl<F: Field> BaseConstraintBuilder<F> {
condition: Expression<F>,
constraint: impl FnOnce(&mut Self) -> R,
) -> R {
debug_assert!(
self.condition.is_none(),
"Nested condition is not supported"
);
self.condition = Some(condition);
self.conditions.push(condition);
let ret = constraint(self);
self.condition = None;
self.conditions.pop();
ret
}

pub(crate) fn get_condition(&self) -> Option<Expression<F>> {
if self.conditions.is_empty() {
None
} else {
Some(and::expr(self.conditions.iter()))
}
}

pub(crate) fn get_condition_expr(&self) -> Expression<F> {
self.get_condition().unwrap_or_else(|| 1.expr())
}

pub(crate) fn add_constraints(&mut self, constraints: Vec<(&'static str, Expression<F>)>) {
for (name, constraint) in constraints {
self.add_constraint(name, constraint);
}
}

pub(crate) fn add_constraint(&mut self, name: &'static str, constraint: Expression<F>) {
let constraint = match &self.condition {
Some(condition) => condition.clone() * constraint,
None => constraint,
};
let constraint = self.get_condition_expr() * constraint;
self.validate_degree(constraint.degree(), name);
self.constraints.push((name, constraint));
}
Expand Down
Loading