refactor to use snarkvm neg gadget

This commit is contained in:
collin 2021-03-11 16:12:33 -08:00
parent c02b281ed0
commit faacfbc97e
12 changed files with 67 additions and 74 deletions

View File

@ -27,7 +27,7 @@ use leo_gadgets::{
use snarkvm_fields::{Field, PrimeField}; use snarkvm_fields::{Field, PrimeField};
use snarkvm_gadgets::traits::utilities::{ use snarkvm_gadgets::traits::utilities::{
alloc::AllocGadget, alloc::AllocGadget,
arithmetic::{Add, Div, Mul}, arithmetic::{Add, Div, Mul, Neg},
boolean::Boolean, boolean::Boolean,
eq::{ConditionalEqGadget, EqGadget, EvaluateEqGadget}, eq::{ConditionalEqGadget, EqGadget, EvaluateEqGadget},
select::CondSelectGadget, select::CondSelectGadget,

View File

@ -18,7 +18,7 @@ use leo_gadgets::{arithmetic::*, Int128, Int16, Int32, Int64, Int8};
use snarkvm_gadgets::traits::utilities::{ use snarkvm_gadgets::traits::utilities::{
alloc::AllocGadget, alloc::AllocGadget,
arithmetic::{Add, Div, Mul}, arithmetic::{Add, Div, Mul, Neg},
}; };
use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem}; use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem};

View File

@ -14,8 +14,8 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>. // along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
pub mod neg; // pub mod neg;
pub use self::neg::*; // pub use self::neg::*;
pub mod pow; pub mod pow;
pub use self::pow::*; pub use self::pow::*;

View File

@ -1,52 +1,52 @@
// Copyright (C) 2019-2021 Aleo Systems Inc. // // Copyright (C) 2019-2021 Aleo Systems Inc.
// This file is part of the Leo library. // // This file is part of the Leo library.
//
// The Leo library is free software: you can redistribute it and/or modify // // The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by // // it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or // // the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. // // (at your option) any later version.
//
// The Leo library is distributed in the hope that it will be useful, // // The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of // // but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details. // // GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License // // You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>. // // along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
//
use crate::bits::RippleCarryAdder; // use crate::bits::RippleCarryAdder;
//
use snarkvm_fields::Field; // use snarkvm_fields::Field;
use snarkvm_gadgets::traits::utilities::boolean::Boolean; // use snarkvm_gadgets::traits::utilities::boolean::Boolean;
use snarkvm_r1cs::{ConstraintSystem, SynthesisError}; // use snarkvm_r1cs::{ConstraintSystem, SynthesisError};
//
use std::iter; // use std::iter;
//
/// Returns a negated representation of `self` in the constraint system. // /// Returns a negated representation of `self` in the constraint system.
pub trait Neg<F: Field> // pub trait Neg<F: Field>
where // where
Self: std::marker::Sized, // Self: std::marker::Sized,
{ // {
type ErrorType; // type ErrorType;
//
fn neg<CS: ConstraintSystem<F>>(&self, cs: CS) -> Result<Self, Self::ErrorType>; // fn neg<CS: ConstraintSystem<F>>(&self, cs: CS) -> Result<Self, Self::ErrorType>;
} // }
//
impl<F: Field> Neg<F> for Vec<Boolean> { // impl<F: Field> Neg<F> for Vec<Boolean> {
type ErrorType = SynthesisError; // type ErrorType = SynthesisError;
//
fn neg<CS: ConstraintSystem<F>>(&self, mut cs: CS) -> Result<Self, SynthesisError> { // fn neg<CS: ConstraintSystem<F>>(&self, mut cs: CS) -> Result<Self, SynthesisError> {
// flip all bits // // flip all bits
let flipped: Self = self.iter().map(|bit| bit.not()).collect(); // let flipped: Self = self.iter().map(|bit| bit.not()).collect();
//
// add one // // add one
let mut one = Vec::with_capacity(self.len()); // let mut one = Vec::with_capacity(self.len());
one.push(Boolean::constant(true)); // one.push(Boolean::constant(true));
one.extend(iter::repeat(Boolean::Constant(false)).take(self.len() - 1)); // one.extend(iter::repeat(Boolean::Constant(false)).take(self.len() - 1));
//
let mut bits = flipped.add_bits(cs.ns(|| "add one"), &one)?; // let mut bits = flipped.add_bits(cs.ns(|| "add one"), &one)?;
let _carry = bits.pop(); // we already accounted for overflow above // let _carry = bits.pop(); // we already accounted for overflow above
//
Ok(bits) // Ok(bits)
} // }
} // }

View File

@ -15,7 +15,7 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>. // along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{ use crate::{
arithmetic::{Neg, Sub}, arithmetic::Sub,
bits::ComparatorGadget, bits::ComparatorGadget,
errors::SignedIntegerError, errors::SignedIntegerError,
Int, Int,
@ -28,7 +28,7 @@ use crate::{
use snarkvm_fields::PrimeField; use snarkvm_fields::PrimeField;
use snarkvm_gadgets::traits::utilities::{ use snarkvm_gadgets::traits::utilities::{
alloc::AllocGadget, alloc::AllocGadget,
arithmetic::{Add, Div}, arithmetic::{Add, Div, Neg},
boolean::{AllocatedBit, Boolean}, boolean::{AllocatedBit, Boolean},
eq::EvaluateEqGadget, eq::EvaluateEqGadget,
select::CondSelectGadget, select::CondSelectGadget,

View File

@ -14,9 +14,10 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>. // along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{arithmetic::Neg, errors::SignedIntegerError, signed_integer::*}; use crate::{errors::SignedIntegerError, signed_integer::*};
use snarkvm_fields::PrimeField; use snarkvm_fields::PrimeField;
use snarkvm_gadgets::utilities::arithmetic::Neg;
use snarkvm_r1cs::ConstraintSystem; use snarkvm_r1cs::ConstraintSystem;
macro_rules! neg_int_impl { macro_rules! neg_int_impl {

View File

@ -14,17 +14,9 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>. // along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{ use crate::{arithmetic::Sub, errors::SignedIntegerError, Int128, Int16, Int32, Int64, Int8};
arithmetic::{Neg, Sub},
errors::SignedIntegerError,
Int128,
Int16,
Int32,
Int64,
Int8,
};
use snarkvm_fields::PrimeField; use snarkvm_fields::PrimeField;
use snarkvm_gadgets::traits::utilities::arithmetic::Add; use snarkvm_gadgets::traits::utilities::arithmetic::{Add, Neg};
use snarkvm_r1cs::ConstraintSystem; use snarkvm_r1cs::ConstraintSystem;
macro_rules! sub_int_impl { macro_rules! sub_int_impl {

View File

@ -19,7 +19,7 @@ use leo_gadgets::{arithmetic::*, Int128};
use snarkvm_fields::{One, Zero}; use snarkvm_fields::{One, Zero};
use snarkvm_gadgets::traits::utilities::{ use snarkvm_gadgets::traits::utilities::{
alloc::AllocGadget, alloc::AllocGadget,
arithmetic::{Add, Div, Mul}, arithmetic::{Add, Div, Mul, Neg},
boolean::Boolean, boolean::Boolean,
}; };
use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem}; use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem};

View File

@ -19,7 +19,7 @@ use leo_gadgets::{arithmetic::*, Int16};
use snarkvm_fields::{One, Zero}; use snarkvm_fields::{One, Zero};
use snarkvm_gadgets::traits::utilities::{ use snarkvm_gadgets::traits::utilities::{
alloc::AllocGadget, alloc::AllocGadget,
arithmetic::{Add, Div, Mul}, arithmetic::{Add, Div, Mul, Neg},
boolean::Boolean, boolean::Boolean,
}; };
use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem}; use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem};

View File

@ -19,7 +19,7 @@ use leo_gadgets::{arithmetic::*, Int32};
use snarkvm_fields::{One, Zero}; use snarkvm_fields::{One, Zero};
use snarkvm_gadgets::traits::utilities::{ use snarkvm_gadgets::traits::utilities::{
alloc::AllocGadget, alloc::AllocGadget,
arithmetic::{Add, Div, Mul}, arithmetic::{Add, Div, Mul, Neg},
boolean::Boolean, boolean::Boolean,
}; };
use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem}; use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem};

View File

@ -19,7 +19,7 @@ use leo_gadgets::{arithmetic::*, Int64};
use snarkvm_fields::{One, Zero}; use snarkvm_fields::{One, Zero};
use snarkvm_gadgets::traits::utilities::{ use snarkvm_gadgets::traits::utilities::{
alloc::AllocGadget, alloc::AllocGadget,
arithmetic::{Add, Div, Mul}, arithmetic::{Add, Div, Mul, Neg},
boolean::Boolean, boolean::Boolean,
}; };
use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem}; use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem};

View File

@ -19,7 +19,7 @@ use leo_gadgets::{arithmetic::*, Int8};
use snarkvm_fields::{One, Zero}; use snarkvm_fields::{One, Zero};
use snarkvm_gadgets::traits::utilities::{ use snarkvm_gadgets::traits::utilities::{
alloc::AllocGadget, alloc::AllocGadget,
arithmetic::{Add, Div, Mul}, arithmetic::{Add, Div, Mul, Neg},
boolean::Boolean, boolean::Boolean,
}; };
use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem}; use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem};