From faacfbc97ed64ffc284f7a9efba85b9c6d77d2f9 Mon Sep 17 00:00:00 2001 From: collin Date: Thu, 11 Mar 2021 16:12:33 -0800 Subject: [PATCH] refactor to use snarkvm neg gadget --- compiler/src/value/integer/integer.rs | 2 +- gadgets/benches/integer_arithmetic.rs | 2 +- gadgets/src/arithmetic/mod.rs | 4 +- gadgets/src/arithmetic/neg.rs | 104 +++++++++---------- gadgets/src/signed_integer/arithmetic/div.rs | 4 +- gadgets/src/signed_integer/arithmetic/neg.rs | 3 +- gadgets/src/signed_integer/arithmetic/sub.rs | 12 +-- gadgets/tests/signed_integer/i128.rs | 2 +- gadgets/tests/signed_integer/i16.rs | 2 +- gadgets/tests/signed_integer/i32.rs | 2 +- gadgets/tests/signed_integer/i64.rs | 2 +- gadgets/tests/signed_integer/i8.rs | 2 +- 12 files changed, 67 insertions(+), 74 deletions(-) diff --git a/compiler/src/value/integer/integer.rs b/compiler/src/value/integer/integer.rs index 5df86d8f21..c7bdce46ab 100644 --- a/compiler/src/value/integer/integer.rs +++ b/compiler/src/value/integer/integer.rs @@ -27,7 +27,7 @@ use leo_gadgets::{ use snarkvm_fields::{Field, PrimeField}; use snarkvm_gadgets::traits::utilities::{ alloc::AllocGadget, - arithmetic::{Add, Div, Mul}, + arithmetic::{Add, Div, Mul, Neg}, boolean::Boolean, eq::{ConditionalEqGadget, EqGadget, EvaluateEqGadget}, select::CondSelectGadget, diff --git a/gadgets/benches/integer_arithmetic.rs b/gadgets/benches/integer_arithmetic.rs index cac9654ba5..c31358142e 100644 --- a/gadgets/benches/integer_arithmetic.rs +++ b/gadgets/benches/integer_arithmetic.rs @@ -18,7 +18,7 @@ use leo_gadgets::{arithmetic::*, Int128, Int16, Int32, Int64, Int8}; use snarkvm_gadgets::traits::utilities::{ alloc::AllocGadget, - arithmetic::{Add, Div, Mul}, + arithmetic::{Add, Div, Mul, Neg}, }; use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem}; diff --git a/gadgets/src/arithmetic/mod.rs b/gadgets/src/arithmetic/mod.rs index 5758f728c6..145bda7c01 100644 --- a/gadgets/src/arithmetic/mod.rs +++ b/gadgets/src/arithmetic/mod.rs @@ -14,8 +14,8 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -pub mod neg; -pub use self::neg::*; +// pub mod neg; +// pub use self::neg::*; pub mod pow; pub use self::pow::*; diff --git a/gadgets/src/arithmetic/neg.rs b/gadgets/src/arithmetic/neg.rs index 170014950a..48b09c5381 100644 --- a/gadgets/src/arithmetic/neg.rs +++ b/gadgets/src/arithmetic/neg.rs @@ -1,52 +1,52 @@ -// Copyright (C) 2019-2021 Aleo Systems Inc. -// This file is part of the Leo library. - -// 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 -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// The Leo library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with the Leo library. If not, see . - -use crate::bits::RippleCarryAdder; - -use snarkvm_fields::Field; -use snarkvm_gadgets::traits::utilities::boolean::Boolean; -use snarkvm_r1cs::{ConstraintSystem, SynthesisError}; - -use std::iter; - -/// Returns a negated representation of `self` in the constraint system. -pub trait Neg -where - Self: std::marker::Sized, -{ - type ErrorType; - - fn neg>(&self, cs: CS) -> Result; -} - -impl Neg for Vec { - type ErrorType = SynthesisError; - - fn neg>(&self, mut cs: CS) -> Result { - // flip all bits - let flipped: Self = self.iter().map(|bit| bit.not()).collect(); - - // add one - let mut one = Vec::with_capacity(self.len()); - one.push(Boolean::constant(true)); - one.extend(iter::repeat(Boolean::Constant(false)).take(self.len() - 1)); - - let mut bits = flipped.add_bits(cs.ns(|| "add one"), &one)?; - let _carry = bits.pop(); // we already accounted for overflow above - - Ok(bits) - } -} +// // Copyright (C) 2019-2021 Aleo Systems Inc. +// // This file is part of the Leo library. +// +// // 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 +// // the Free Software Foundation, either version 3 of the License, or +// // (at your option) any later version. +// +// // The Leo library is distributed in the hope that it will be useful, +// // but WITHOUT ANY WARRANTY; without even the implied warranty of +// // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// // GNU General Public License for more details. +// +// // You should have received a copy of the GNU General Public License +// // along with the Leo library. If not, see . +// +// use crate::bits::RippleCarryAdder; +// +// use snarkvm_fields::Field; +// use snarkvm_gadgets::traits::utilities::boolean::Boolean; +// use snarkvm_r1cs::{ConstraintSystem, SynthesisError}; +// +// use std::iter; +// +// /// Returns a negated representation of `self` in the constraint system. +// pub trait Neg +// where +// Self: std::marker::Sized, +// { +// type ErrorType; +// +// fn neg>(&self, cs: CS) -> Result; +// } +// +// impl Neg for Vec { +// type ErrorType = SynthesisError; +// +// fn neg>(&self, mut cs: CS) -> Result { +// // flip all bits +// let flipped: Self = self.iter().map(|bit| bit.not()).collect(); +// +// // add one +// let mut one = Vec::with_capacity(self.len()); +// one.push(Boolean::constant(true)); +// one.extend(iter::repeat(Boolean::Constant(false)).take(self.len() - 1)); +// +// let mut bits = flipped.add_bits(cs.ns(|| "add one"), &one)?; +// let _carry = bits.pop(); // we already accounted for overflow above +// +// Ok(bits) +// } +// } diff --git a/gadgets/src/signed_integer/arithmetic/div.rs b/gadgets/src/signed_integer/arithmetic/div.rs index 4cca0c3efa..a4a47ac9fb 100644 --- a/gadgets/src/signed_integer/arithmetic/div.rs +++ b/gadgets/src/signed_integer/arithmetic/div.rs @@ -15,7 +15,7 @@ // along with the Leo library. If not, see . use crate::{ - arithmetic::{Neg, Sub}, + arithmetic::Sub, bits::ComparatorGadget, errors::SignedIntegerError, Int, @@ -28,7 +28,7 @@ use crate::{ use snarkvm_fields::PrimeField; use snarkvm_gadgets::traits::utilities::{ alloc::AllocGadget, - arithmetic::{Add, Div}, + arithmetic::{Add, Div, Neg}, boolean::{AllocatedBit, Boolean}, eq::EvaluateEqGadget, select::CondSelectGadget, diff --git a/gadgets/src/signed_integer/arithmetic/neg.rs b/gadgets/src/signed_integer/arithmetic/neg.rs index 3f802878bf..a030f42a50 100644 --- a/gadgets/src/signed_integer/arithmetic/neg.rs +++ b/gadgets/src/signed_integer/arithmetic/neg.rs @@ -14,9 +14,10 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{arithmetic::Neg, errors::SignedIntegerError, signed_integer::*}; +use crate::{errors::SignedIntegerError, signed_integer::*}; use snarkvm_fields::PrimeField; +use snarkvm_gadgets::utilities::arithmetic::Neg; use snarkvm_r1cs::ConstraintSystem; macro_rules! neg_int_impl { diff --git a/gadgets/src/signed_integer/arithmetic/sub.rs b/gadgets/src/signed_integer/arithmetic/sub.rs index 7ab5b1debe..aaf51a59ac 100644 --- a/gadgets/src/signed_integer/arithmetic/sub.rs +++ b/gadgets/src/signed_integer/arithmetic/sub.rs @@ -14,17 +14,9 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{ - arithmetic::{Neg, Sub}, - errors::SignedIntegerError, - Int128, - Int16, - Int32, - Int64, - Int8, -}; +use crate::{arithmetic::Sub, errors::SignedIntegerError, Int128, Int16, Int32, Int64, Int8}; use snarkvm_fields::PrimeField; -use snarkvm_gadgets::traits::utilities::arithmetic::Add; +use snarkvm_gadgets::traits::utilities::arithmetic::{Add, Neg}; use snarkvm_r1cs::ConstraintSystem; macro_rules! sub_int_impl { diff --git a/gadgets/tests/signed_integer/i128.rs b/gadgets/tests/signed_integer/i128.rs index 5dc1c237e1..3bc93788d1 100644 --- a/gadgets/tests/signed_integer/i128.rs +++ b/gadgets/tests/signed_integer/i128.rs @@ -19,7 +19,7 @@ use leo_gadgets::{arithmetic::*, Int128}; use snarkvm_fields::{One, Zero}; use snarkvm_gadgets::traits::utilities::{ alloc::AllocGadget, - arithmetic::{Add, Div, Mul}, + arithmetic::{Add, Div, Mul, Neg}, boolean::Boolean, }; use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem}; diff --git a/gadgets/tests/signed_integer/i16.rs b/gadgets/tests/signed_integer/i16.rs index 30d4f3d9a2..4f56547b85 100644 --- a/gadgets/tests/signed_integer/i16.rs +++ b/gadgets/tests/signed_integer/i16.rs @@ -19,7 +19,7 @@ use leo_gadgets::{arithmetic::*, Int16}; use snarkvm_fields::{One, Zero}; use snarkvm_gadgets::traits::utilities::{ alloc::AllocGadget, - arithmetic::{Add, Div, Mul}, + arithmetic::{Add, Div, Mul, Neg}, boolean::Boolean, }; use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem}; diff --git a/gadgets/tests/signed_integer/i32.rs b/gadgets/tests/signed_integer/i32.rs index 8c10418e29..440cfbd631 100644 --- a/gadgets/tests/signed_integer/i32.rs +++ b/gadgets/tests/signed_integer/i32.rs @@ -19,7 +19,7 @@ use leo_gadgets::{arithmetic::*, Int32}; use snarkvm_fields::{One, Zero}; use snarkvm_gadgets::traits::utilities::{ alloc::AllocGadget, - arithmetic::{Add, Div, Mul}, + arithmetic::{Add, Div, Mul, Neg}, boolean::Boolean, }; use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem}; diff --git a/gadgets/tests/signed_integer/i64.rs b/gadgets/tests/signed_integer/i64.rs index 22e982a421..a46ed15cbc 100644 --- a/gadgets/tests/signed_integer/i64.rs +++ b/gadgets/tests/signed_integer/i64.rs @@ -19,7 +19,7 @@ use leo_gadgets::{arithmetic::*, Int64}; use snarkvm_fields::{One, Zero}; use snarkvm_gadgets::traits::utilities::{ alloc::AllocGadget, - arithmetic::{Add, Div, Mul}, + arithmetic::{Add, Div, Mul, Neg}, boolean::Boolean, }; use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem}; diff --git a/gadgets/tests/signed_integer/i8.rs b/gadgets/tests/signed_integer/i8.rs index f8a2343711..a9cd91b092 100644 --- a/gadgets/tests/signed_integer/i8.rs +++ b/gadgets/tests/signed_integer/i8.rs @@ -19,7 +19,7 @@ use leo_gadgets::{arithmetic::*, Int8}; use snarkvm_fields::{One, Zero}; use snarkvm_gadgets::traits::utilities::{ alloc::AllocGadget, - arithmetic::{Add, Div, Mul}, + arithmetic::{Add, Div, Mul, Neg}, boolean::Boolean, }; use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem};