rename twos comp to negate

This commit is contained in:
collin 2020-07-16 20:12:29 -07:00
parent 8f397a3382
commit d89e1c1e59
5 changed files with 18 additions and 30 deletions

View File

@ -46,18 +46,6 @@ macro_rules! rpc_impl {
($($gadget: ident)*) => ($(
impl RippleCarryAdder for $gadget {
fn add_bits<F: PrimeField, CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Vec<Boolean>, 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)
}
}

View File

@ -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"),

View File

@ -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::*;

View File

@ -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<F: PrimeField, CS: ConstraintSystem<F>>(&self, cs: CS) -> Result<Self, SignedIntegerError>;
fn neg<F: PrimeField, CS: ConstraintSystem<F>>(&self, cs: CS) -> Result<Self, SignedIntegerError>;
}
impl TwosComplement for Vec<Boolean> {
fn twos_comp<F: PrimeField, CS: ConstraintSystem<F>>(&self, mut cs: CS) -> Result<Self, SignedIntegerError> {
impl Negate for Vec<Boolean> {
fn neg<F: PrimeField, CS: ConstraintSystem<F>>(&self, mut cs: CS) -> Result<Self, SignedIntegerError> {
// flip all bits
let flipped: Self = self.iter().map(|bit| bit.not()).collect();
@ -32,8 +32,8 @@ impl TwosComplement for Vec<Boolean> {
macro_rules! twos_comp_int_impl {
($($gadget: ident)*) => ($(
impl TwosComplement for $gadget {
fn twos_comp<F: PrimeField, CS: ConstraintSystem<F>>(
impl Negate for $gadget {
fn neg<F: PrimeField, CS: ConstraintSystem<F>>(
&self,
cs: CS
) -> Result<Self, SignedIntegerError> {
@ -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,

View File

@ -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<F: PrimeField, CS: ConstraintSystem<F>>(&self, mut cs: CS, other: &Self) -> Result<Self, SignedIntegerError> {
// 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)
}
}
)*)