From 5dd4ed3e1c5c63137a7994942c09cc31132d310f Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Mon, 1 Nov 2021 11:12:21 -0700 Subject: [PATCH] addressed comments --- src/gadgets/arithmetic_u32.rs | 15 +-------------- src/gadgets/biguint.rs | 4 ++-- src/gates/subtraction_u32.rs | 2 +- 3 files changed, 4 insertions(+), 17 deletions(-) diff --git a/src/gadgets/arithmetic_u32.rs b/src/gadgets/arithmetic_u32.rs index db6d3669..ba076a8f 100644 --- a/src/gadgets/arithmetic_u32.rs +++ b/src/gadgets/arithmetic_u32.rs @@ -81,24 +81,11 @@ impl, const D: usize> CircuitBuilder { self.mul_add_u32(a, one, b) } - pub fn add_three_u32( - &mut self, - a: U32Target, - b: U32Target, - c: U32Target, - ) -> (U32Target, U32Target) { - let (init_low, carry1) = self.add_u32(a, b); - let (final_low, carry2) = self.add_u32(c, init_low); - let (combined_carry, _zero) = self.add_u32(carry1, carry2); - (final_low, combined_carry) - } - - pub fn add_many_u32(&mut self, to_add: Vec) -> (U32Target, U32Target) { + pub fn add_many_u32(&mut self, to_add: &[U32Target]) -> (U32Target, U32Target) { match to_add.len() { 0 => (self.zero_u32(), self.zero_u32()), 1 => (to_add[0], self.zero_u32()), 2 => self.add_u32(to_add[0], to_add[1]), - 3 => self.add_three_u32(to_add[0], to_add[1], to_add[2]), _ => { let (mut low, mut carry) = self.add_u32(to_add[0], to_add[1]); for i in 2..to_add.len() { diff --git a/src/gadgets/biguint.rs b/src/gadgets/biguint.rs index 81880eef..a524a79b 100644 --- a/src/gadgets/biguint.rs +++ b/src/gadgets/biguint.rs @@ -117,7 +117,7 @@ impl, const D: usize> CircuitBuilder { self.zero_u32() }; - let (new_limb, new_carry) = self.add_three_u32(carry.clone(), a_limb, b_limb); + let (new_limb, new_carry) = self.add_many_u32(&[carry.clone(), a_limb, b_limb]); carry = new_carry; combined_limbs.push(new_limb); } @@ -164,7 +164,7 @@ impl, const D: usize> CircuitBuilder { let mut carry = self.zero_u32(); for i in 0..total_limbs { to_add[i].push(carry); - let (new_result, new_carry) = self.add_many_u32(to_add[i].clone()); + let (new_result, new_carry) = self.add_many_u32(&to_add[i].clone()); combined_limbs.push(new_result); carry = new_carry; } diff --git a/src/gates/subtraction_u32.rs b/src/gates/subtraction_u32.rs index c11c6f0f..afac85be 100644 --- a/src/gates/subtraction_u32.rs +++ b/src/gates/subtraction_u32.rs @@ -15,7 +15,7 @@ use crate::plonk::vars::{EvaluationTargets, EvaluationVars, EvaluationVarsBase}; pub const NUM_U32_SUBTRACTION_OPS: usize = 3; /// A gate to perform a subtraction on 32-bit limbs: given `x`, `y`, and `borrow`, it returns -/// the result `x - y - borrow` and, if this underflows, a new `borrow`. +/// the result `x - y - borrow` and, if this underflows, a new `borrow`. Inputs are not range-checked. #[derive(Clone, Debug)] pub struct U32SubtractionGate, const D: usize> { _phantom: PhantomData,