From f32d3c0e02e7cf7cc8470f9a8464a6e38ebfac10 Mon Sep 17 00:00:00 2001 From: Johann Date: Wed, 1 Nov 2023 14:53:07 -0700 Subject: [PATCH] use add method for `AllocateNum` --- src/circuit/circuit_frame.rs | 21 +++++++-------------- src/circuit/gadgets/constraints.rs | 13 ++----------- src/coprocessor/mod.rs | 4 ++-- src/lem/circuit.rs | 10 +++++----- 4 files changed, 16 insertions(+), 32 deletions(-) diff --git a/src/circuit/circuit_frame.rs b/src/circuit/circuit_frame.rs index d647f77a0d..b908660bbd 100644 --- a/src/circuit/circuit_frame.rs +++ b/src/circuit/circuit_frame.rs @@ -32,7 +32,7 @@ use super::gadgets::constraints::{ pick, pick_const, sub, }; use crate::circuit::circuit_frame::constraints::{ - add, allocate_is_negative, boolean_to_num, enforce_pack, enforce_product_and_sum, mul, + allocate_is_negative, boolean_to_num, enforce_pack, enforce_product_and_sum, mul, }; use crate::circuit::gadgets::hashes::{AllocatedConsWitness, AllocatedContWitness}; use crate::circuit::ToInputs; @@ -4304,7 +4304,7 @@ fn apply_continuation>( ContTag::Binop2.to_field(), )?; - let sum = add(&mut cs.namespace(|| "sum"), a, b)?; + let sum = a.add(&mut cs.namespace(|| "sum"), b)?; let diff = sub(&mut cs.namespace(|| "difference"), a, b)?; let product = mul(&mut cs.namespace(|| "product"), a, b)?; @@ -4486,9 +4486,8 @@ fn apply_continuation>( // Subtraction in U64 is almost the same as subtraction in the field. // If the difference is negative, we need to add 2^64 to get back to U64 domain. - let field_arithmetic_result_plus_2p64 = add( + let field_arithmetic_result_plus_2p64 = field_arithmetic_result.hash().add( &mut cs.namespace(|| "field arithmetic result plus 2^64"), - field_arithmetic_result.hash(), &g.power2_64_num, )?; @@ -5383,11 +5382,8 @@ fn enforce_u64_div_mod>( &alloc_q_num, &alloc_arg2_num, )?; - let sum_u64mod = add( - &mut cs.namespace(|| "sum remainder mod u64"), - &product_u64mod, - &alloc_r_num, - )?; + let sum_u64mod = + product_u64mod.add(&mut cs.namespace(|| "sum remainder mod u64"), &alloc_r_num)?; let u64mod_decomp = alloc_equal( &mut cs.namespace(|| "check u64 mod decomposition"), &sum_u64mod, @@ -5657,11 +5653,8 @@ fn destructure_list_aux>( let is_cons = alloc_equal(&mut cs.namespace(|| "is_cons"), list.tag(), &g.cons_tag)?; let increment = boolean_to_num(&mut cs.namespace(|| "increment"), &is_cons)?; - let new_length_so_far = add( - &mut cs.namespace(|| "new_length_so_far"), - &increment, - length_so_far, - )?; + let new_length_so_far = + increment.add(&mut cs.namespace(|| "new_length_so_far"), length_so_far)?; if n == 0 { return Ok(new_length_so_far.clone()); diff --git a/src/circuit/gadgets/constraints.rs b/src/circuit/gadgets/constraints.rs index 765e3c0f96..d9081443ab 100644 --- a/src/circuit/gadgets/constraints.rs +++ b/src/circuit/gadgets/constraints.rs @@ -74,15 +74,6 @@ pub(crate) fn enforce_equal_const> ); } -/// Compute sum and enforce it. -pub(crate) fn add>( - cs: CS, - a: &AllocatedNum, - b: &AllocatedNum, -) -> Result, SynthesisError> { - a.add(cs, b) -} - /// Creates a linear combination representing the popcount (sum of one bits) of `v`. pub(crate) fn popcount_lc>( v: &[Boolean], @@ -900,7 +891,7 @@ pub(crate) fn allocate_is_negative>( mut cs: CS, num: &AllocatedNum, ) -> Result { - let double_num = add(&mut cs.namespace(|| "double num"), num, num)?; + let double_num = num.add(&mut cs.namespace(|| "double num"), num)?; let double_num_bits = double_num .to_bits_le_strict(&mut cs.namespace(|| "double num bits")) .unwrap(); @@ -1145,7 +1136,7 @@ mod tests { let a = AllocatedNum::alloc_infallible(cs.namespace(|| "a"), || x.0); let b = AllocatedNum::alloc_infallible(cs.namespace(|| "b"), || y.0); - let res = add(cs.namespace(|| "a+b"), &a, &b).expect("add failed"); + let res = a.add(cs.namespace(|| "a+b"), &b).expect("add failed"); let mut tmp = a.get_value().expect("get_value failed"); tmp.add_assign(&b.get_value().expect("get_value failed")); diff --git a/src/coprocessor/mod.rs b/src/coprocessor/mod.rs index 9513c460dd..ba704c1dd7 100644 --- a/src/coprocessor/mod.rs +++ b/src/coprocessor/mod.rs @@ -244,7 +244,7 @@ pub(crate) mod test { use serde::{Deserialize, Serialize}; use super::*; - use crate::circuit::gadgets::constraints::{add, alloc_equal, and, mul}; + use crate::circuit::gadgets::constraints::{alloc_equal, and, mul}; use crate::lem::Tag as LEMTag; use crate::tag::{ExprTag, Tag}; use std::marker::PhantomData; @@ -278,7 +278,7 @@ pub(crate) mod test { // a^2 + b = c let a2 = mul(&mut cs.namespace(|| "square"), a.hash(), a.hash())?; - let c = add(&mut cs.namespace(|| "add"), &a2, b.hash())?; + let c = a2.add(&mut cs.namespace(|| "add"), b.hash())?; let c_ptr = AllocatedPtr::alloc_tag(cs, ExprTag::Num.to_field(), c)?; let result_expr0 = diff --git a/src/lem/circuit.rs b/src/lem/circuit.rs index 32821cf53f..0c41d5c96e 100644 --- a/src/lem/circuit.rs +++ b/src/lem/circuit.rs @@ -38,7 +38,7 @@ use std::{ use crate::{ circuit::gadgets::{ constraints::{ - add, alloc_equal, alloc_is_zero, and, div, enforce_product_and_sum, + alloc_equal, alloc_is_zero, and, div, enforce_product_and_sum, enforce_selector_with_premise, implies_equal, implies_equal_const, implies_pack, implies_u64, implies_unequal_const, mul, or, pick, sub, }, @@ -789,7 +789,7 @@ fn synthesize_block, C: Coprocessor>( let b = bound_allocations.get_ptr(b)?; let a_num = a.hash(); let b_num = b.hash(); - let c_num = add(cs.namespace(|| "add"), a_num, b_num)?; + let c_num = a_num.add(cs.namespace(|| "add"), b_num)?; let tag = ctx.global_allocator.get_tag_cloned(&Num)?; let c = AllocatedPtr::from_parts(tag, c_num); bound_allocations.insert_ptr(tgt.clone(), c); @@ -848,9 +848,9 @@ fn synthesize_block, C: Coprocessor>( let b_num = bound_allocations.get_ptr(b)?.hash(); let diff = sub(cs.namespace(|| "diff"), a_num, b_num)?; // Double a, b, a-b - let double_a = add(cs.namespace(|| "double_a"), a_num, a_num)?; - let double_b = add(cs.namespace(|| "double_b"), b_num, b_num)?; - let double_diff = add(cs.namespace(|| "double_diff"), &diff, &diff)?; + let double_a = a_num.add(cs.namespace(|| "double_a"), a_num)?; + let double_b = b_num.add(cs.namespace(|| "double_b"), b_num)?; + let double_diff = diff.add(cs.namespace(|| "double_diff"), &diff)?; // Get slot allocated preimages/bits for the double of a, b, a-b let (double_a_preimg, double_a_bits) = &ctx.bit_decomp_slots[next_slot.consume_bit_decomp()];