Skip to content

Commit

Permalink
Using if_else.
Browse files Browse the repository at this point in the history
  • Loading branch information
lvella committed Oct 21, 2024
1 parent 44912f0 commit deb2d67
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
8 changes: 8 additions & 0 deletions std/constraints.asm
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,11 @@ let make_conditional: Constr, expr -> Constr = |constraint, condition| match con
Constr::Permutation((Option::Some(sel_l), sel_r), exprs) => Constr::Permutation((Option::Some(sel_l * condition), sel_r), exprs),
Constr::Connection(_) => std::check::panic("Connection constraints cannot be conditional"),
};

/// Either one constraint or the other, depending on a boolen condition.
let if_else: expr, Constr, Constr -> Constr = |condition, if_true, if_false| match (if_true, if_false) {
(Constr::Identity(l_t, r_t), Constr::Identity(l_f, r_f)) =>
condition * (l_t - r_t) +
(1 - condition) * (l_f - r_f) = 0,
_ => std::check::panic("if_else can only be used with two identity constraints"),
};
7 changes: 3 additions & 4 deletions std/machines/small_field/pointer_arith.asm
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ let word_increment_ptr: expr, expr, expr, expr -> Constr[] = constr |pre_high, p

// Increment polynomials, to be used by the caller in constraints:
[
// If low limb is about to overflow, next value must be 0:
low_overflow * post_low +
// Otherwise, next value is current plus 4:
(1 - low_overflow) * (pre_low + 4 - post_low) = 0,
// If low limb is about to overflow, next value must be 0,
// otherwise, next value is current plus 4.
std::constraints::if_else(low_overflow, post_low = 0, post_low = pre_low + 4),

// Set high limb, incremented if low overflowed:
post_high - pre_high - low_overflow = 0
Expand Down

0 comments on commit deb2d67

Please sign in to comment.