Skip to content

Commit

Permalink
[fix] div autosave bug (#863)
Browse files Browse the repository at this point in the history
* fix div autosave

* put it back
  • Loading branch information
luffykai authored Nov 26, 2024
1 parent 31ebdd7 commit b684b43
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
7 changes: 7 additions & 0 deletions circuits/ecc/src/field_expression/field_variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,13 @@ impl FieldVariable {

let mut builder = self.builder.borrow_mut();
let (new_var_idx, new_var) = builder.new_var();
let new_constraint = SymbolicExpr::Sub(
Box::new(SymbolicExpr::Mul(
Box::new(other.expr.clone()),
Box::new(new_var.clone()),
)),
Box::new(self.expr.clone()),
);
builder.set_constraint(new_var_idx, new_constraint);
// Only compute can have division.
let compute = SymbolicExpr::Div(Box::new(self.expr.clone()), Box::new(other.expr.clone()));
Expand Down
41 changes: 41 additions & 0 deletions circuits/ecc/src/field_expression/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,47 @@ fn test_auto_carry_add() {
.expect("Verification failed");
}

#[test]
fn test_auto_carry_div() {
let prime = secp256k1_coord_prime();
let (range_checker, builder) = setup(&prime);

let mut x1 = ExprBuilder::new_input(builder.clone());
let x2 = ExprBuilder::new_input(builder.clone());
// The choice of scalar (7) needs to be such that
// 1. the denominator 7x^2 doesn't trigger autosave, >=8 doesn't work.
// 2. But doing a division on it triggers autosave, because of division constraint, <= 6 doesn't work.
let mut x3 = x1.square().int_mul(7) / x2;
x3.save();

let builder = builder.borrow().clone();
assert_eq!(builder.num_variables, 2); // numerator autosaved, and the final division

let expr = FieldExpr::new(builder, range_checker.bus());
let width = BaseAir::<BabyBear>::width(&expr);

let x = generate_random_biguint(&prime);
let y = generate_random_biguint(&prime);
// let expected = (&x * &x * BigUint::from(10u32)) % prime;
let inputs = vec![x, y];

let mut row = BabyBear::zero_vec(width);
expr.generate_subrow((&range_checker, inputs, vec![]), &mut row);
let FieldExprCols { vars, .. } = expr.load_vars(&row);
assert_eq!(vars.len(), 2);
// let generated = evaluate_biguint(&vars[x5_id], LIMB_BITS);
// assert_eq!(generated, expected);

let trace = RowMajorMatrix::new(row, width);
let range_trace = range_checker.generate_trace();

BabyBearBlake3Engine::run_simple_test_no_pis_fast(
any_rap_arc_vec![expr, range_checker.air],
vec![trace, range_trace],
)
.expect("Verification failed");
}

fn make_addsub_chip(builder: Rc<RefCell<ExprBuilder>>) -> ExprBuilder {
let x1 = ExprBuilder::new_input(builder.clone());
let x2 = ExprBuilder::new_input(builder.clone());
Expand Down

0 comments on commit b684b43

Please sign in to comment.