kimchi_msm/
expr.rs

1// @volhovm Not sure a whole file is necessary for just one type
2// alias, but maybe more code will come.
3// Consider moving to lib.rs
4
5use ark_ff::Field;
6use kimchi::circuits::{
7    berkeley_columns::BerkeleyChallengeTerm,
8    expr::{ConstantExpr, Expr, ExprInner, Variable},
9    gate::CurrOrNext,
10};
11
12use crate::columns::Column;
13
14/// An expression over /generic/ (not circuit-specific) columns
15/// defined in the msm project. To represent constraints as multi
16/// variate polynomials. The variables are over the columns.
17///
18/// For instance, if there are 3 columns X1, X2, X3, then to constraint X3 to be
19/// equals to sum of the X1 and X2 on a row, we would use the multivariate
20/// polynomial `X3 - X1 - X2 = 0`.
21/// Using the expression framework, this constraint would be
22/// ```
23/// use kimchi::circuits::expr::{ConstantExprInner, ExprInner, Operations, Variable};
24/// use kimchi::circuits::gate::CurrOrNext;
25/// use kimchi::circuits::berkeley_columns::BerkeleyChallengeTerm;
26/// use kimchi_msm::columns::{Column as GenericColumn};
27/// use kimchi_msm::expr::E;
28/// pub type Fp = ark_bn254::Fr;
29/// pub type Column = GenericColumn<usize>;
30/// let x1 = E::<Fp>::Atom(
31///     ExprInner::<Operations<ConstantExprInner<Fp, BerkeleyChallengeTerm>>, Column>::Cell(Variable {
32///         col: Column::Relation(1),
33///         row: CurrOrNext::Curr,
34///     }),
35/// );
36/// let x2 = E::<Fp>::Atom(
37///     ExprInner::<Operations<ConstantExprInner<Fp, BerkeleyChallengeTerm>>, Column>::Cell(Variable {
38///         col: Column::Relation(1),
39///         row: CurrOrNext::Curr,
40///     }),
41/// );
42/// let x3 = E::<Fp>::Atom(
43///     ExprInner::<Operations<ConstantExprInner<Fp, BerkeleyChallengeTerm>>, Column>::Cell(Variable {
44///         col: Column::Relation(1),
45///         row: CurrOrNext::Curr,
46///     }),
47/// );
48/// let constraint = x3 - x1 - x2;
49/// ```
50/// A list of such constraints is used to represent the entire circuit and will
51/// be used to build the quotient polynomial.
52pub type E<F> = Expr<ConstantExpr<F, BerkeleyChallengeTerm>, Column<usize>>;
53
54pub fn curr_cell<F: Field>(col: Column<usize>) -> E<F> {
55    E::Atom(ExprInner::Cell(Variable {
56        col,
57        row: CurrOrNext::Curr,
58    }))
59}
60
61pub fn next_cell<F: Field>(col: Column<usize>) -> E<F> {
62    E::Atom(ExprInner::Cell(Variable {
63        col,
64        row: CurrOrNext::Next,
65    }))
66}
67
68#[test]
69fn test_debug_can_be_called_on_expr() {
70    use crate::{columns::Column::*, Fp};
71    println!("{:}", curr_cell::<Fp>(Relation(0)) + curr_cell(Relation(1)))
72}