From dd945ef5b73bac72929677f598ac98cfc806f166 Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Wed, 10 Nov 2021 11:19:06 -0800 Subject: [PATCH] addressed comments --- src/gadgets/biguint.rs | 13 ++++++------- src/gadgets/nonnative.rs | 14 ++++---------- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/src/gadgets/biguint.rs b/src/gadgets/biguint.rs index f9878c50..fff97a6e 100644 --- a/src/gadgets/biguint.rs +++ b/src/gadgets/biguint.rs @@ -61,7 +61,6 @@ impl, const D: usize> CircuitBuilder { (a.clone(), padded_b) } else { let mut padded_a = a.clone(); - let to_extend = b.num_limbs() - a.num_limbs(); for _ in a.num_limbs()..b.num_limbs() { padded_a.limbs.push(self.zero_u32()); } @@ -266,10 +265,10 @@ mod tests { fn test_biguint_sub() -> Result<()> { let mut rng = rand::thread_rng(); - let x_value = BigUint::from_u128(rng.gen()).unwrap(); + let mut x_value = BigUint::from_u128(rng.gen()).unwrap(); let mut y_value = BigUint::from_u128(rng.gen()).unwrap(); - while y_value > x_value { - y_value = BigUint::from_u128(rng.gen()).unwrap(); + if y_value > x_value { + (x_value, y_value) = (y_value, x_value); } let expected_z_value = &x_value - &y_value; @@ -343,10 +342,10 @@ mod tests { fn test_biguint_div_rem() -> Result<()> { let mut rng = rand::thread_rng(); - let x_value = BigUint::from_u128(rng.gen()).unwrap(); + let mut x_value = BigUint::from_u128(rng.gen()).unwrap(); let mut y_value = BigUint::from_u128(rng.gen()).unwrap(); - while y_value > x_value { - y_value = BigUint::from_u128(rng.gen()).unwrap(); + if y_value > x_value { + (x_value, y_value) = (y_value, x_value); } let (expected_div_value, expected_rem_value) = x_value.div_rem(&y_value); diff --git a/src/gadgets/nonnative.rs b/src/gadgets/nonnative.rs index fbe46fe2..fd883e5d 100644 --- a/src/gadgets/nonnative.rs +++ b/src/gadgets/nonnative.rs @@ -44,9 +44,7 @@ impl, const D: usize> CircuitBuilder { a: &ForeignFieldTarget, b: &ForeignFieldTarget, ) -> ForeignFieldTarget { - let a_biguint = self.nonnative_to_biguint(a); - let b_biguint = self.nonnative_to_biguint(b); - let result = self.add_biguint(&a_biguint, &b_biguint); + let result = self.add_biguint(&a.value, &b.value); self.reduce(&result) } @@ -58,10 +56,8 @@ impl, const D: usize> CircuitBuilder { b: &ForeignFieldTarget, ) -> ForeignFieldTarget { let order = self.constant_biguint(&FF::order()); - let a_biguint = self.nonnative_to_biguint(a); - let a_plus_order = self.add_biguint(&order, &a_biguint); - let b_biguint = self.nonnative_to_biguint(b); - let result = self.sub_biguint(&a_plus_order, &b_biguint); + let a_plus_order = self.add_biguint(&order, &a.value); + let result = self.sub_biguint(&a_plus_order, &b.value); // TODO: reduce sub result with only one conditional addition? self.reduce(&result) @@ -72,9 +68,7 @@ impl, const D: usize> CircuitBuilder { a: &ForeignFieldTarget, b: &ForeignFieldTarget, ) -> ForeignFieldTarget { - let a_biguint = self.nonnative_to_biguint(a); - let b_biguint = self.nonnative_to_biguint(b); - let result = self.mul_biguint(&a_biguint, &b_biguint); + let result = self.mul_biguint(&a.value, &b.value); self.reduce(&result) }