From d89e1c1e5952ff7c843247101fa95cf2ae7f37d9 Mon Sep 17 00:00:00 2001 From: collin Date: Thu, 16 Jul 2020 20:12:29 -0700 Subject: [PATCH] rename twos comp to negate --- gadgets/src/binary/rca.rs | 12 ------------ gadgets/src/signed_integer/arithmetic/div.rs | 6 +++--- gadgets/src/signed_integer/arithmetic/mod.rs | 4 ++-- .../arithmetic/{twos_complement.rs => neg.rs} | 16 ++++++++-------- gadgets/src/signed_integer/arithmetic/sub.rs | 10 +++++----- 5 files changed, 18 insertions(+), 30 deletions(-) rename gadgets/src/signed_integer/arithmetic/{twos_complement.rs => neg.rs} (74%) diff --git a/gadgets/src/binary/rca.rs b/gadgets/src/binary/rca.rs index 12add7678a..6c96466734 100644 --- a/gadgets/src/binary/rca.rs +++ b/gadgets/src/binary/rca.rs @@ -46,18 +46,6 @@ macro_rules! rpc_impl { ($($gadget: ident)*) => ($( impl RippleCarryAdder for $gadget { fn add_bits>(&self, cs: CS, other: &Self) -> Result, SynthesisError> { - // let mut result = vec![]; - // let mut carry = Boolean::constant(false); - // for (i, (a, b)) in self.bits.iter().zip(other.bits.iter()).enumerate() { - // let (sum, next) = Boolean::add(cs.ns(|| format!("rpc {}", i)), a, b, &carry)?; - // - // carry = next; - // result.push(sum); - // } - // - // // append the carry bit to the end - // result.push(carry); - self.bits.add_bits(cs, &other.bits) } } diff --git a/gadgets/src/signed_integer/arithmetic/div.rs b/gadgets/src/signed_integer/arithmetic/div.rs index 7edf316fe3..ef87257cc2 100644 --- a/gadgets/src/signed_integer/arithmetic/div.rs +++ b/gadgets/src/signed_integer/arithmetic/div.rs @@ -101,7 +101,7 @@ macro_rules! div_int_impl { let positive = a_msb.evaluate_equal(cs.ns(|| "compare_msb"), &b_msb)?; // Get the absolute value of each number - let a_comp = self.twos_comp(&mut cs.ns(|| "a_twos_comp"))?; + let a_comp = self.neg(&mut cs.ns(|| "a_neg"))?; let a = Self::conditionally_select( &mut cs.ns(|| "a_abs"), &a_msb, @@ -109,7 +109,7 @@ macro_rules! div_int_impl { &self )?; - let b_comp = other.twos_comp(&mut cs.ns(|| "b_twos_comp"))?; + let b_comp = other.neg(&mut cs.ns(|| "b_neg"))?; let b = Self::conditionally_select( &mut cs.ns(|| "b_abs"), &b_msb, @@ -179,7 +179,7 @@ macro_rules! div_int_impl { } - let q_neg = q.twos_comp(&mut cs.ns(|| "twos comp"))?; + let q_neg = q.neg(&mut cs.ns(|| "negate"))?; q = Self::conditionally_select( &mut cs.ns(|| "positive or negative"), diff --git a/gadgets/src/signed_integer/arithmetic/mod.rs b/gadgets/src/signed_integer/arithmetic/mod.rs index ae4970b62e..8ddd2e086e 100644 --- a/gadgets/src/signed_integer/arithmetic/mod.rs +++ b/gadgets/src/signed_integer/arithmetic/mod.rs @@ -8,8 +8,8 @@ pub use self::div::*; pub mod mul; pub use self::mul::*; -pub mod twos_complement; -pub use self::twos_complement::*; +pub mod neg; +pub use self::neg::*; pub mod pow; pub use self::pow::*; diff --git a/gadgets/src/signed_integer/arithmetic/twos_complement.rs b/gadgets/src/signed_integer/arithmetic/neg.rs similarity index 74% rename from gadgets/src/signed_integer/arithmetic/twos_complement.rs rename to gadgets/src/signed_integer/arithmetic/neg.rs index ba388fb051..b82ea18876 100644 --- a/gadgets/src/signed_integer/arithmetic/twos_complement.rs +++ b/gadgets/src/signed_integer/arithmetic/neg.rs @@ -5,17 +5,17 @@ use snarkos_models::{ gadgets::{r1cs::ConstraintSystem, utilities::boolean::Boolean}, }; -/// Inverts the given number and adds 1 to the lsb of the result -pub trait TwosComplement +/// Returns a negated representation of the given signed integer. +pub trait Negate where Self: std::marker::Sized, { #[must_use] - fn twos_comp>(&self, cs: CS) -> Result; + fn neg>(&self, cs: CS) -> Result; } -impl TwosComplement for Vec { - fn twos_comp>(&self, mut cs: CS) -> Result { +impl Negate for Vec { + fn neg>(&self, mut cs: CS) -> Result { // flip all bits let flipped: Self = self.iter().map(|bit| bit.not()).collect(); @@ -32,8 +32,8 @@ impl TwosComplement for Vec { macro_rules! twos_comp_int_impl { ($($gadget: ident)*) => ($( - impl TwosComplement for $gadget { - fn twos_comp>( + impl Negate for $gadget { + fn neg>( &self, cs: CS ) -> Result { @@ -48,7 +48,7 @@ macro_rules! twos_comp_int_impl { }; // calculate two's complement - let bits = self.bits.twos_comp(cs)?; + let bits = self.bits.neg(cs)?; Ok(Self { bits, diff --git a/gadgets/src/signed_integer/arithmetic/sub.rs b/gadgets/src/signed_integer/arithmetic/sub.rs index 8a766f4866..777906265b 100644 --- a/gadgets/src/signed_integer/arithmetic/sub.rs +++ b/gadgets/src/signed_integer/arithmetic/sub.rs @@ -1,4 +1,4 @@ -use crate::{errors::SignedIntegerError, Add, Int128, Int16, Int32, Int64, Int8, TwosComplement}; +use crate::{errors::SignedIntegerError, Add, Int128, Int16, Int32, Int64, Int8, Negate}; use snarkos_models::{curves::PrimeField, gadgets::r1cs::ConstraintSystem}; /// Subtraction for a signed integer gadget @@ -14,11 +14,11 @@ macro_rules! sub_int_impl { ($($gadget: ident)*) => ($( impl Sub for $gadget { fn sub>(&self, mut cs: CS, other: &Self) -> Result { - // Evaluate the two's complement of the subtrahend - let s = other.twos_comp(cs.ns(|| format!("complement")))?; + // Negate other + let other_neg = other.neg(cs.ns(|| format!("negate")))?; - // Add minuend + subtrahend - self.add(cs.ns(|| format!("add_complement")), &s) + // self + negated other + self.add(cs.ns(|| format!("add_complement")), &other_neg) } } )*)