Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expr formatting. #888

Open
wants to merge 1 commit into
base: alont/formal-expr-logup
Choose a base branch
from
Open
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
53 changes: 53 additions & 0 deletions crates/prover/src/constraint_framework/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,35 @@ enum Expr {
Inv(Box<Expr>),
}

impl Expr {
#[allow(dead_code)]
pub fn format_expr(&self) -> String {
match self {
Expr::Col(ColumnExpr {
interaction,
idx,
offset,
}) => {
format!("col_{interaction}_{idx}[{offset}]")
}
Expr::SecureCol([a, b, c, d]) => format!(
"SecureCol({}, {}, {}, {})",
a.format_expr(),
b.format_expr(),
c.format_expr(),
d.format_expr()
),
Expr::Const(c) => c.0.to_string(),
Expr::Param(v) => v.to_string(),
Expr::Add(a, b) => format!("{} + {}", a.format_expr(), b.format_expr()),
Expr::Sub(a, b) => format!("{} - ({})", a.format_expr(), b.format_expr()),
Expr::Mul(a, b) => format!("({}) * ({})", a.format_expr(), b.format_expr()),
Expr::Neg(a) => format!("-({})", a.format_expr()),
Expr::Inv(a) => format!("1/({})", a.format_expr()),
}
}
}

impl From<BaseField> for Expr {
fn from(val: BaseField) -> Self {
Expr::Const(val)
Expand Down Expand Up @@ -256,6 +285,7 @@ mod tests {
use crate::core::fields::m31::M31;
use crate::core::fields::qm31::SecureField;
use crate::core::fields::FieldExpOps;

#[test]
fn test_expr_eval() {
let test_struct = TestStruct {};
Expand Down Expand Up @@ -414,6 +444,29 @@ mod tests {
);
}

#[test]
fn test_format_expr() {
let test_struct = TestStruct {};
let eval = test_struct.evaluate(ExprEvaluator::new(16, (SecureField::zero(), None)));
let constraint0_str = "(1) * ((((col_1_0[0]) * (col_1_1[0])) * (col_1_2[0])) * (1/(col_1_0[0] + col_1_1[0])))";
assert_eq!(eval.constraints[0].format_expr(), constraint0_str);
let constraint1_str = "(1) \
* ((SecureCol(col_2_4[0], col_2_6[0], col_2_8[0], col_2_10[0]) \
- (SecureCol(\
col_2_5[-1], \
col_2_7[-1], \
col_2_9[-1], \
col_2_11[-1]\
) - ((col_0_3[0]) * (SecureCol(0, 0, 0, 0)))) \
- (0)) \
* (0 + (TestRelation_alpha0) * (col_1_0[0]) \
+ (TestRelation_alpha1) * (col_1_1[0]) \
+ (TestRelation_alpha2) * (col_1_2[0]) \
- (TestRelation_z)) \
- (1))";
assert_eq!(eval.constraints[1].format_expr(), constraint1_str);
}

relation!(TestRelation, 3);

struct TestStruct {}
Expand Down
Loading