Skip to content

Commit

Permalink
refactor!: Shorten enum names
Browse files Browse the repository at this point in the history
changelog: ignore
  • Loading branch information
jan-ferdinand committed Sep 12, 2024
1 parent b803e13 commit f9139c4
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 63 deletions.
20 changes: 10 additions & 10 deletions triton-constraint-builder/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ impl RustBackend {
}

// constants can be declared trivially
let CircuitExpression::BinaryOperation(_, lhs, rhs) = &circuit.expression else {
let CircuitExpression::BinOp(_, lhs, rhs) = &circuit.expression else {
return None;
};

Expand Down Expand Up @@ -339,7 +339,7 @@ impl RustBackend {
return Self::binding_name(circuit);
}

let CircuitExpression::BinaryOperation(binop, lhs, rhs) = &circuit.expression else {
let CircuitExpression::BinOp(binop, lhs, rhs) = &circuit.expression else {
return Self::binding_name(circuit);
};

Expand All @@ -350,11 +350,11 @@ impl RustBackend {

fn binding_name<II: InputIndicator>(circuit: &ConstraintCircuit<II>) -> TokenStream {
match &circuit.expression {
CircuitExpression::BConstant(bfe) => Self::tokenize_bfe(*bfe),
CircuitExpression::XConstant(xfe) => Self::tokenize_xfe(*xfe),
CircuitExpression::BConst(bfe) => Self::tokenize_bfe(*bfe),
CircuitExpression::XConst(xfe) => Self::tokenize_xfe(*xfe),
CircuitExpression::Input(idx) => quote!(#idx),
CircuitExpression::Challenge(challenge) => quote!(challenges[#challenge]),
CircuitExpression::BinaryOperation(_, _, _) => {
CircuitExpression::BinOp(_, _, _) => {
let node_ident = format_ident!("node_{}", circuit.id);
quote!(#node_ident)
}
Expand Down Expand Up @@ -712,7 +712,7 @@ impl TasmBackend {
return vec![];
}

let CircuitExpression::BinaryOperation(_, lhs, rhs) = &constraint.expression else {
let CircuitExpression::BinOp(_, lhs, rhs) = &constraint.expression else {
return vec![];
};

Expand All @@ -739,7 +739,7 @@ impl TasmBackend {
return self.load_node(constraint);
}

let CircuitExpression::BinaryOperation(binop, lhs, rhs) = &constraint.expression else {
let CircuitExpression::BinOp(binop, lhs, rhs) = &constraint.expression else {
return self.load_node(constraint);
};

Expand All @@ -765,11 +765,11 @@ impl TasmBackend {

fn load_node<II: InputIndicator>(&self, circuit: &ConstraintCircuit<II>) -> Vec<TokenStream> {
match circuit.expression {
CircuitExpression::BConstant(bfe) => Self::load_ext_field_constant(bfe.into()),
CircuitExpression::XConstant(xfe) => Self::load_ext_field_constant(xfe),
CircuitExpression::BConst(bfe) => Self::load_ext_field_constant(bfe.into()),
CircuitExpression::XConst(xfe) => Self::load_ext_field_constant(xfe),
CircuitExpression::Input(input) => self.load_input(input),
CircuitExpression::Challenge(challenge_idx) => Self::load_challenge(challenge_idx),
CircuitExpression::BinaryOperation(_, _, _) => Self::load_evaluated_bin_op(circuit.id),
CircuitExpression::BinOp(_, _, _) => Self::load_evaluated_bin_op(circuit.id),
}
}

Expand Down
5 changes: 2 additions & 3 deletions triton-constraint-builder/src/substitutions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,15 +218,14 @@ impl Substitutions {
fn substitution_rule_to_code<II: InputIndicator>(
circuit: ConstraintCircuit<II>,
) -> TokenStream {
let CircuitExpression::BinaryOperation(BinOp::Add, new_var, expr) = circuit.expression
else {
let CircuitExpression::BinOp(BinOp::Add, new_var, expr) = circuit.expression else {
panic!("Substitution rule must be a subtraction, i.e., addition of `x` and `-expr`.");
};
let CircuitExpression::Input(_) = new_var.borrow().expression else {
panic!("Substitution rule must be a simple substitution.");
};
let expr = expr.borrow();
let CircuitExpression::BinaryOperation(BinOp::Mul, neg_one, expr) = &expr.expression else {
let CircuitExpression::BinOp(BinOp::Mul, neg_one, expr) = &expr.expression else {
panic!("Substitution rule must be a subtraction.");
};
assert!(neg_one.borrow().is_neg_one());
Expand Down
92 changes: 45 additions & 47 deletions triton-constraint-circuit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,11 @@ impl InputIndicator for DualRowIndicator {
/// bookkeeping information.
#[derive(Debug, Clone)]
pub enum CircuitExpression<II: InputIndicator> {
BConstant(BFieldElement),
XConstant(XFieldElement),
BConst(BFieldElement),
XConst(XFieldElement),
Input(II),
Challenge(usize),
BinaryOperation(
BinOp(
BinOp,
Rc<RefCell<ConstraintCircuit<II>>>,
Rc<RefCell<ConstraintCircuit<II>>>,
Expand All @@ -295,11 +295,11 @@ pub enum CircuitExpression<II: InputIndicator> {
impl<II: InputIndicator> Hash for CircuitExpression<II> {
fn hash<H: Hasher>(&self, state: &mut H) {
match self {
Self::BConstant(bfe) => {
Self::BConst(bfe) => {
"bfe".hash(state);
bfe.hash(state);
}
Self::XConstant(xfe) => {
Self::XConst(xfe) => {
"xfe".hash(state);
xfe.hash(state);
}
Expand All @@ -311,7 +311,7 @@ impl<II: InputIndicator> Hash for CircuitExpression<II> {
"challenge".hash(state);
table_challenge_id.hash(state);
}
Self::BinaryOperation(binop, lhs, rhs) => {
Self::BinOp(binop, lhs, rhs) => {
"binop".hash(state);
binop.hash(state);
lhs.borrow().hash(state);
Expand All @@ -324,11 +324,11 @@ impl<II: InputIndicator> Hash for CircuitExpression<II> {
impl<II: InputIndicator> PartialEq for CircuitExpression<II> {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::BConstant(b), Self::BConstant(b_o)) => b == b_o,
(Self::XConstant(x), Self::XConstant(x_o)) => x == x_o,
(Self::BConst(b), Self::BConst(b_o)) => b == b_o,
(Self::XConst(x), Self::XConst(x_o)) => x == x_o,
(Self::Input(i), Self::Input(i_o)) => i == i_o,
(Self::Challenge(c), Self::Challenge(c_o)) => c == c_o,
(Self::BinaryOperation(op, l, r), Self::BinaryOperation(op_o, l_o, r_o)) => {
(Self::BinOp(op, l, r), Self::BinOp(op_o, l_o, r_o)) => {
op == op_o && l == l_o && r == r_o
}
_ => false,
Expand Down Expand Up @@ -370,11 +370,11 @@ impl<II: InputIndicator> PartialEq for ConstraintCircuit<II> {
impl<II: InputIndicator> Display for ConstraintCircuit<II> {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
match &self.expression {
CircuitExpression::XConstant(xfe) => write!(f, "{xfe}"),
CircuitExpression::BConstant(bfe) => write!(f, "{bfe}"),
CircuitExpression::XConst(xfe) => write!(f, "{xfe}"),
CircuitExpression::BConst(bfe) => write!(f, "{bfe}"),
CircuitExpression::Input(input) => write!(f, "{input} "),
CircuitExpression::Challenge(self_challenge_idx) => write!(f, "{self_challenge_idx}"),
CircuitExpression::BinaryOperation(operation, lhs, rhs) => {
CircuitExpression::BinOp(operation, lhs, rhs) => {
write!(f, "({}) {operation} ({})", lhs.borrow(), rhs.borrow())
}
}
Expand All @@ -394,7 +394,7 @@ impl<II: InputIndicator> ConstraintCircuit<II> {
fn reset_ref_count_for_tree(&mut self) {
self.ref_count = 0;

if let CircuitExpression::BinaryOperation(_, lhs, rhs) = &self.expression {
if let CircuitExpression::BinOp(_, lhs, rhs) = &self.expression {
lhs.borrow_mut().reset_ref_count_for_tree();
rhs.borrow_mut().reset_ref_count_for_tree();
}
Expand All @@ -418,7 +418,7 @@ impl<II: InputIndicator> ConstraintCircuit<II> {
panic!("Repeated ID: {self_id}\nSelf:\n{self}\n{self:?}\nOther:\n{other}\n{other:?}");
}

if let CircuitExpression::BinaryOperation(_, lhs, rhs) = &self.expression {
if let CircuitExpression::BinOp(_, lhs, rhs) = &self.expression {
lhs.borrow_mut().assert_unique_ids_inner(ids);
rhs.borrow_mut().assert_unique_ids_inner(ids);
}
Expand Down Expand Up @@ -448,7 +448,7 @@ impl<II: InputIndicator> ConstraintCircuit<II> {
}

match &self.expression {
CircuitExpression::BinaryOperation(binop, lhs, rhs) => {
CircuitExpression::BinOp(binop, lhs, rhs) => {
let degree_lhs = lhs.borrow().degree();
let degree_rhs = rhs.borrow().degree();
let degree_additive = cmp::max(degree_lhs, degree_rhs);
Expand All @@ -463,16 +463,16 @@ impl<II: InputIndicator> ConstraintCircuit<II> {
}
}
CircuitExpression::Input(_) => 1,
CircuitExpression::BConstant(_)
| CircuitExpression::XConstant(_)
CircuitExpression::BConst(_)
| CircuitExpression::XConst(_)
| CircuitExpression::Challenge(_) => 0,
}
}

/// All unique reference counters in the subtree, sorted.
pub fn all_ref_counters(&self) -> Vec<usize> {
let mut ref_counters = vec![self.ref_count];
if let CircuitExpression::BinaryOperation(_, lhs, rhs) = &self.expression {
if let CircuitExpression::BinOp(_, lhs, rhs) = &self.expression {
ref_counters.extend(lhs.borrow().all_ref_counters());
ref_counters.extend(rhs.borrow().all_ref_counters());
};
Expand All @@ -485,8 +485,8 @@ impl<II: InputIndicator> ConstraintCircuit<II> {
/// Does not catch composite expressions that will always evaluate to zero, like `0·a`.
pub fn is_zero(&self) -> bool {
match self.expression {
CircuitExpression::BConstant(bfe) => bfe.is_zero(),
CircuitExpression::XConstant(xfe) => xfe.is_zero(),
CircuitExpression::BConst(bfe) => bfe.is_zero(),
CircuitExpression::XConst(xfe) => xfe.is_zero(),
_ => false,
}
}
Expand All @@ -495,16 +495,16 @@ impl<II: InputIndicator> ConstraintCircuit<II> {
/// Does not catch composite expressions that will always evaluate to one, like `1·1`.
pub fn is_one(&self) -> bool {
match self.expression {
CircuitExpression::BConstant(bfe) => bfe.is_one(),
CircuitExpression::XConstant(xfe) => xfe.is_one(),
CircuitExpression::BConst(bfe) => bfe.is_one(),
CircuitExpression::XConst(xfe) => xfe.is_one(),
_ => false,
}
}

pub fn is_neg_one(&self) -> bool {
match self.expression {
CircuitExpression::BConstant(bfe) => (-bfe).is_one(),
CircuitExpression::XConstant(xfe) => (-xfe).is_one(),
CircuitExpression::BConst(bfe) => (-bfe).is_one(),
CircuitExpression::XConst(xfe) => (-xfe).is_one(),
_ => false,
}
}
Expand All @@ -515,11 +515,11 @@ impl<II: InputIndicator> ConstraintCircuit<II> {
/// 3. binary operations on BFieldElements.
pub fn evaluates_to_base_element(&self) -> bool {
match &self.expression {
CircuitExpression::BConstant(_) => true,
CircuitExpression::XConstant(_) => false,
CircuitExpression::BConst(_) => true,
CircuitExpression::XConst(_) => false,
CircuitExpression::Input(indicator) => indicator.is_main_table_column(),
CircuitExpression::Challenge(_) => false,
CircuitExpression::BinaryOperation(_, lhs, rhs) => {
CircuitExpression::BinOp(_, lhs, rhs) => {
lhs.borrow().evaluates_to_base_element() && rhs.borrow().evaluates_to_base_element()
}
}
Expand All @@ -532,11 +532,11 @@ impl<II: InputIndicator> ConstraintCircuit<II> {
challenges: &[XFieldElement],
) -> XFieldElement {
match &self.expression {
CircuitExpression::BConstant(bfe) => bfe.lift(),
CircuitExpression::XConstant(xfe) => *xfe,
CircuitExpression::BConst(bfe) => bfe.lift(),
CircuitExpression::XConst(xfe) => *xfe,
CircuitExpression::Input(input) => input.evaluate(base_table, ext_table),
CircuitExpression::Challenge(challenge_id) => challenges[*challenge_id],
CircuitExpression::BinaryOperation(binop, lhs, rhs) => {
CircuitExpression::BinOp(binop, lhs, rhs) => {
let lhs_value = lhs.borrow().evaluate(base_table, ext_table, challenges);
let rhs_value = rhs.borrow().evaluate(base_table, ext_table, challenges);
binop.operation(lhs_value, rhs_value)
Expand Down Expand Up @@ -607,16 +607,16 @@ fn binop<II: InputIndicator>(
&lhs.circuit.borrow().expression,
&rhs.circuit.borrow().expression,
) {
(&CircuitExpression::BConstant(l), &CircuitExpression::BConstant(r)) => {
(&CircuitExpression::BConst(l), &CircuitExpression::BConst(r)) => {
return lhs.builder.b_constant(binop.operation(l, r))
}
(&CircuitExpression::BConstant(l), &CircuitExpression::XConstant(r)) => {
(&CircuitExpression::BConst(l), &CircuitExpression::XConst(r)) => {
return lhs.builder.x_constant(binop.operation(l, r))
}
(&CircuitExpression::XConstant(l), &CircuitExpression::BConstant(r)) => {
(&CircuitExpression::XConst(l), &CircuitExpression::BConst(r)) => {
return lhs.builder.x_constant(binop.operation(l, r))
}
(&CircuitExpression::XConstant(l), &CircuitExpression::XConstant(r)) => {
(&CircuitExpression::XConst(l), &CircuitExpression::XConst(r)) => {
return lhs.builder.x_constant(binop.operation(l, r))
}
_ => (),
Expand Down Expand Up @@ -648,8 +648,7 @@ fn binop_new_node<II: InputIndicator>(
rhs: &ConstraintCircuitMonad<II>,
) -> ConstraintCircuitMonad<II> {
let id = lhs.builder.id_counter.borrow().to_owned();
let expression =
CircuitExpression::BinaryOperation(binop, lhs.circuit.clone(), rhs.circuit.clone());
let expression = CircuitExpression::BinOp(binop, lhs.circuit.clone(), rhs.circuit.clone());
let circuit = ConstraintCircuit::new(id, expression);
lhs.builder.new_monad(circuit)
}
Expand Down Expand Up @@ -853,7 +852,7 @@ impl<II: InputIndicator> ConstraintCircuitMonad<II> {
/// Internal helper function to recursively find all nodes in a circuit.
fn all_nodes_in_circuit(circuit: &ConstraintCircuit<II>) -> Vec<ConstraintCircuit<II>> {
let mut all_nodes = vec![];
if let CircuitExpression::BinaryOperation(_, lhs, rhs) = circuit.expression.clone() {
if let CircuitExpression::BinOp(_, lhs, rhs) = circuit.expression.clone() {
let lhs_nodes = Self::all_nodes_in_circuit(&lhs.borrow());
let rhs_nodes = Self::all_nodes_in_circuit(&rhs.borrow());
all_nodes.extend(lhs_nodes);
Expand Down Expand Up @@ -942,15 +941,15 @@ impl<II: InputIndicator> ConstraintCircuitBuilder<II> {
where
B: Into<BFieldElement>,
{
self.make_leaf(CircuitExpression::BConstant(bfe.into()))
self.make_leaf(CircuitExpression::BConst(bfe.into()))
}

/// Leaf node with constant over the [extension field][XFieldElement].
pub fn x_constant<X>(&self, xfe: X) -> ConstraintCircuitMonad<II>
where
X: Into<XFieldElement>,
{
self.make_leaf(CircuitExpression::XConstant(xfe.into()))
self.make_leaf(CircuitExpression::XConst(xfe.into()))
}

/// Create deterministic input leaf node.
Expand All @@ -968,9 +967,9 @@ impl<II: InputIndicator> ConstraintCircuitBuilder<II> {

fn make_leaf(&self, mut expression: CircuitExpression<II>) -> ConstraintCircuitMonad<II> {
// Don't generate an X field leaf if it can be expressed as a B field leaf
if let CircuitExpression::XConstant(xfe) = expression {
if let CircuitExpression::XConst(xfe) = expression {
if let Some(bfe) = xfe.unlift() {
expression = CircuitExpression::BConstant(bfe);
expression = CircuitExpression::BConst(bfe);
}
}

Expand Down Expand Up @@ -998,8 +997,7 @@ impl<II: InputIndicator> ConstraintCircuitBuilder<II> {
self.all_nodes.borrow_mut().remove(&old_id);
for node in self.all_nodes.borrow_mut().values_mut() {
let node_expression = &mut node.circuit.borrow_mut().expression;
let CircuitExpression::BinaryOperation(_, ref mut node_lhs, ref mut node_rhs) =
node_expression
let CircuitExpression::BinOp(_, ref mut node_lhs, ref mut node_rhs) = node_expression
else {
continue;
};
Expand Down Expand Up @@ -1122,8 +1120,8 @@ mod tests {
}

let evaluation = match &circuit.borrow().expression {
CircuitExpression::BConstant(bfe) => bfe.lift(),
CircuitExpression::XConstant(xfe) => *xfe,
CircuitExpression::BConst(bfe) => bfe.lift(),
CircuitExpression::XConst(xfe) => *xfe,
CircuitExpression::Input(input) => {
let [s0, s1, s2] = master_seed.coefficients;
let dom_sep = if input.is_current_row() {
Expand All @@ -1142,7 +1140,7 @@ mod tests {
let Digest([d0, d1, d2, _, _]) = Tip5::hash_varlen(&[s0, s1, s2, dom_sep, ch]);
xfe!([d0, d1, d2])
}
CircuitExpression::BinaryOperation(bin_op, lhs, rhs) => {
CircuitExpression::BinOp(bin_op, lhs, rhs) => {
let l =
Self::probe_random(lhs, id_to_eval, eval_to_ids, id_to_node, master_seed);
let r =
Expand Down Expand Up @@ -1172,7 +1170,7 @@ mod tests {
if self == other {
return true;
}
let CircuitExpression::BinaryOperation(_, lhs, rhs) = self else {
let CircuitExpression::BinOp(_, lhs, rhs) = self else {
return false;
};

Expand Down
6 changes: 3 additions & 3 deletions triton-vm/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ mod tests {
values: &mut HashMap<XFieldElement, (usize, ConstraintCircuit<II>)>,
) -> XFieldElement {
let value = match &constraint.expression {
CircuitExpression::BinaryOperation(binop, lhs, rhs) => {
CircuitExpression::BinOp(binop, lhs, rhs) => {
let lhs = lhs.borrow();
let rhs = rhs.borrow();
let lhs = evaluate_assert_unique(&lhs, challenges, main_rows, aux_rows, values);
Expand Down Expand Up @@ -287,7 +287,7 @@ mod tests {
] {
for (i, constraint) in constraints.iter().enumerate() {
let expression = constraint.circuit.borrow().expression.clone();
let CircuitExpression::BinaryOperation(BinOp::Add, lhs, rhs) = expression else {
let CircuitExpression::BinOp(BinOp::Add, lhs, rhs) = expression else {
panic!("New {constraint_type} constraint {i} must be a subtraction.");
};
let CircuitExpression::Input(input_indicator) = lhs.borrow().expression.clone()
Expand Down Expand Up @@ -361,7 +361,7 @@ mod tests {
substitution_rule: &ConstraintCircuit<II>,
) {
match substitution_rule.expression.clone() {
CircuitExpression::BinaryOperation(_, lhs, rhs) => {
CircuitExpression::BinOp(_, lhs, rhs) => {
let lhs = lhs.borrow();
let rhs = rhs.borrow();
assert_substitution_rule_uses_legal_variables(new_var, &lhs);
Expand Down

0 comments on commit f9139c4

Please sign in to comment.