From deb3847b8ea5845efb7955fe3fdfe538724eec64 Mon Sep 17 00:00:00 2001 From: collin Date: Thu, 11 Mar 2021 14:47:34 -0800 Subject: [PATCH 01/21] remove add trait and uint impls --- compiler/src/value/integer/integer.rs | 1 + .../pedersen-hash/inputs/pedersen-hash.in | 8 +++ examples/pedersen-hash/src/main.leo | 9 ++- gadgets/src/arithmetic/add.rs | 62 +++++++++---------- gadgets/src/arithmetic/mod.rs | 4 +- gadgets/src/signed_integer/arithmetic/add.rs | 13 +--- gadgets/src/signed_integer/arithmetic/div.rs | 3 +- gadgets/src/signed_integer/arithmetic/sub.rs | 3 +- 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 +- 13 files changed, 59 insertions(+), 54 deletions(-) diff --git a/compiler/src/value/integer/integer.rs b/compiler/src/value/integer/integer.rs index 49b157573f..092927681b 100644 --- a/compiler/src/value/integer/integer.rs +++ b/compiler/src/value/integer/integer.rs @@ -27,6 +27,7 @@ use leo_gadgets::{ use snarkvm_fields::{Field, PrimeField}; use snarkvm_gadgets::traits::utilities::{ alloc::AllocGadget, + arithmetic::Add, boolean::Boolean, eq::{ConditionalEqGadget, EqGadget, EvaluateEqGadget}, select::CondSelectGadget, diff --git a/examples/pedersen-hash/inputs/pedersen-hash.in b/examples/pedersen-hash/inputs/pedersen-hash.in index 4434b65eb8..598948b21b 100644 --- a/examples/pedersen-hash/inputs/pedersen-hash.in +++ b/examples/pedersen-hash/inputs/pedersen-hash.in @@ -1,4 +1,12 @@ +// syntax 1 [main] +const parameters: [group; 256] = [1; 256]; + + +// syntax 2 +[const] +parameters: [group; 256] = [1; 256]; + [registers] r0: group = (1, 0)group; \ No newline at end of file diff --git a/examples/pedersen-hash/src/main.leo b/examples/pedersen-hash/src/main.leo index 25050216da..ee04517341 100644 --- a/examples/pedersen-hash/src/main.leo +++ b/examples/pedersen-hash/src/main.leo @@ -17,10 +17,13 @@ circuit PedersenHash { } } -// The 'pedersen-hash' main function. -function main() -> group { - let parameters = [1group; 256]; +// syntax 1: const main function input +function main(const parameters: [group; 256]) -> group { let pedersen = PedersenHash::new(parameters); let hash_input: [bool; 256] = [true; 256]; return pedersen.hash(hash_input) } + +// syntax 2: main file global constant +@parameter +const parameters: [group; 256]; diff --git a/gadgets/src/arithmetic/add.rs b/gadgets/src/arithmetic/add.rs index eada6b2c3b..f83624c0e3 100644 --- a/gadgets/src/arithmetic/add.rs +++ b/gadgets/src/arithmetic/add.rs @@ -14,35 +14,35 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use snarkvm_fields::{Field, PrimeField}; -use snarkvm_gadgets::traits::utilities::uint::{UInt, UInt128, UInt16, UInt32, UInt64, UInt8}; -use snarkvm_r1cs::{ConstraintSystem, SynthesisError}; +// use snarkvm_fields::{Field, PrimeField}; +// use snarkvm_gadgets::traits::utilities::uint::{UInt, UInt128, UInt16, UInt32, UInt64, UInt8}; +// use snarkvm_r1cs::{ConstraintSystem, SynthesisError}; +// +// /// Returns addition of `self` + `other` in the constraint system. +// pub trait Add +// where +// Self: std::marker::Sized, +// { +// type ErrorType; +// +// fn add>(&self, cs: CS, other: &Self) -> Result; +// } -/// Returns addition of `self` + `other` in the constraint system. -pub trait Add -where - Self: std::marker::Sized, -{ - type ErrorType; - - fn add>(&self, cs: CS, other: &Self) -> Result; -} - -// Implement unsigned integers -macro_rules! add_uint_impl { - ($($gadget: ident),*) => ($( - impl Add for $gadget { - type ErrorType = SynthesisError; - - fn add>( - &self, - cs: CS, - other: &Self - ) -> Result { - <$gadget as UInt>::addmany(cs, &[self.clone(), other.clone()]) - } - } - )*) -} - -add_uint_impl!(UInt8, UInt16, UInt32, UInt64, UInt128); +// // Implement unsigned integers +// macro_rules! add_uint_impl { +// ($($gadget: ident),*) => ($( +// impl Add for $gadget { +// type ErrorType = SynthesisError; +// +// fn add>( +// &self, +// cs: CS, +// other: &Self +// ) -> Result { +// <$gadget as UInt>::addmany(cs, &[self.clone(), other.clone()]) +// } +// } +// )*) +// } +// +// add_uint_impl!(UInt8, UInt16, UInt32, UInt64, UInt128); diff --git a/gadgets/src/arithmetic/mod.rs b/gadgets/src/arithmetic/mod.rs index 469b8cf078..35b566b87c 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 add; -pub use self::add::*; +// pub mod add; +// pub use self::add::*; pub mod div; pub use self::div::*; diff --git a/gadgets/src/signed_integer/arithmetic/add.rs b/gadgets/src/signed_integer/arithmetic/add.rs index be7ae00cea..57327d434e 100644 --- a/gadgets/src/signed_integer/arithmetic/add.rs +++ b/gadgets/src/signed_integer/arithmetic/add.rs @@ -14,21 +14,12 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{ - arithmetic::Add, - bits::RippleCarryAdder, - errors::SignedIntegerError, - Int, - Int128, - Int16, - Int32, - Int64, - Int8, -}; +use crate::{bits::RippleCarryAdder, errors::SignedIntegerError, Int, Int128, Int16, Int32, Int64, Int8}; use snarkvm_fields::{fp_parameters::FpParameters, PrimeField}; use snarkvm_gadgets::traits::utilities::{ alloc::AllocGadget, + arithmetic::Add, boolean::{AllocatedBit, Boolean}, }; use snarkvm_r1cs::{Assignment, ConstraintSystem, LinearCombination}; diff --git a/gadgets/src/signed_integer/arithmetic/div.rs b/gadgets/src/signed_integer/arithmetic/div.rs index 6f93029abc..0b1683f8b1 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::{Add, Div, Neg, Sub}, + arithmetic::{Div, Neg, Sub}, bits::ComparatorGadget, errors::SignedIntegerError, Int, @@ -28,6 +28,7 @@ use crate::{ use snarkvm_fields::PrimeField; use snarkvm_gadgets::traits::utilities::{ alloc::AllocGadget, + arithmetic::Add, boolean::{AllocatedBit, Boolean}, eq::EvaluateEqGadget, select::CondSelectGadget, diff --git a/gadgets/src/signed_integer/arithmetic/sub.rs b/gadgets/src/signed_integer/arithmetic/sub.rs index fc3ae9a539..7ab5b1debe 100644 --- a/gadgets/src/signed_integer/arithmetic/sub.rs +++ b/gadgets/src/signed_integer/arithmetic/sub.rs @@ -15,7 +15,7 @@ // along with the Leo library. If not, see . use crate::{ - arithmetic::{Add, Neg, Sub}, + arithmetic::{Neg, Sub}, errors::SignedIntegerError, Int128, Int16, @@ -24,6 +24,7 @@ use crate::{ Int8, }; use snarkvm_fields::PrimeField; +use snarkvm_gadgets::traits::utilities::arithmetic::Add; 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 9efa1f4c7a..acf18acd25 100644 --- a/gadgets/tests/signed_integer/i128.rs +++ b/gadgets/tests/signed_integer/i128.rs @@ -17,7 +17,7 @@ use leo_gadgets::{arithmetic::*, Int128}; use snarkvm_fields::{One, Zero}; -use snarkvm_gadgets::traits::utilities::{alloc::AllocGadget, boolean::Boolean}; +use snarkvm_gadgets::traits::utilities::{alloc::AllocGadget, arithmetic::Add, boolean::Boolean}; use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem}; use rand::Rng; diff --git a/gadgets/tests/signed_integer/i16.rs b/gadgets/tests/signed_integer/i16.rs index c6e5af960b..7770af853a 100644 --- a/gadgets/tests/signed_integer/i16.rs +++ b/gadgets/tests/signed_integer/i16.rs @@ -17,7 +17,7 @@ use leo_gadgets::{arithmetic::*, Int16}; use snarkvm_fields::{One, Zero}; -use snarkvm_gadgets::traits::utilities::{alloc::AllocGadget, boolean::Boolean}; +use snarkvm_gadgets::traits::utilities::{alloc::AllocGadget, arithmetic::Add, boolean::Boolean}; use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem}; use rand::Rng; diff --git a/gadgets/tests/signed_integer/i32.rs b/gadgets/tests/signed_integer/i32.rs index fb9fee1c94..82c4e00051 100644 --- a/gadgets/tests/signed_integer/i32.rs +++ b/gadgets/tests/signed_integer/i32.rs @@ -17,7 +17,7 @@ use leo_gadgets::{arithmetic::*, Int32}; use snarkvm_fields::{One, Zero}; -use snarkvm_gadgets::traits::utilities::{alloc::AllocGadget, boolean::Boolean}; +use snarkvm_gadgets::traits::utilities::{alloc::AllocGadget, arithmetic::Add, boolean::Boolean}; use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem}; use rand::Rng; diff --git a/gadgets/tests/signed_integer/i64.rs b/gadgets/tests/signed_integer/i64.rs index 2aee9111c5..50dbf38abc 100644 --- a/gadgets/tests/signed_integer/i64.rs +++ b/gadgets/tests/signed_integer/i64.rs @@ -17,7 +17,7 @@ use leo_gadgets::{arithmetic::*, Int64}; use snarkvm_fields::{One, Zero}; -use snarkvm_gadgets::traits::utilities::{alloc::AllocGadget, boolean::Boolean}; +use snarkvm_gadgets::traits::utilities::{alloc::AllocGadget, arithmetic::Add, boolean::Boolean}; use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem}; use rand::Rng; diff --git a/gadgets/tests/signed_integer/i8.rs b/gadgets/tests/signed_integer/i8.rs index b8eaef23ed..2b35aaae8a 100644 --- a/gadgets/tests/signed_integer/i8.rs +++ b/gadgets/tests/signed_integer/i8.rs @@ -17,7 +17,7 @@ use leo_gadgets::{arithmetic::*, Int8}; use snarkvm_fields::{One, Zero}; -use snarkvm_gadgets::traits::utilities::{alloc::AllocGadget, boolean::Boolean}; +use snarkvm_gadgets::traits::utilities::{alloc::AllocGadget, arithmetic::Add, boolean::Boolean}; use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem}; use rand::Rng; From 8cbce25b5fd8ac652fbd3791c9323fcde980f484 Mon Sep 17 00:00:00 2001 From: collin Date: Thu, 11 Mar 2021 14:51:54 -0800 Subject: [PATCH 02/21] fix pedersen hash example --- examples/pedersen-hash/inputs/pedersen-hash.in | 8 -------- examples/pedersen-hash/src/main.leo | 8 +++----- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/examples/pedersen-hash/inputs/pedersen-hash.in b/examples/pedersen-hash/inputs/pedersen-hash.in index 598948b21b..4434b65eb8 100644 --- a/examples/pedersen-hash/inputs/pedersen-hash.in +++ b/examples/pedersen-hash/inputs/pedersen-hash.in @@ -1,12 +1,4 @@ -// syntax 1 [main] -const parameters: [group; 256] = [1; 256]; - - -// syntax 2 -[const] -parameters: [group; 256] = [1; 256]; - [registers] r0: group = (1, 0)group; \ No newline at end of file diff --git a/examples/pedersen-hash/src/main.leo b/examples/pedersen-hash/src/main.leo index ee04517341..e0adfecaec 100644 --- a/examples/pedersen-hash/src/main.leo +++ b/examples/pedersen-hash/src/main.leo @@ -17,13 +17,11 @@ circuit PedersenHash { } } -// syntax 1: const main function input -function main(const parameters: [group; 256]) -> group { +// The 'pedersen-hash' main function. +function main() -> group { + let parameters = [1group; 256]; let pedersen = PedersenHash::new(parameters); let hash_input: [bool; 256] = [true; 256]; return pedersen.hash(hash_input) } -// syntax 2: main file global constant -@parameter -const parameters: [group; 256]; From 55965e48bbacf087a3b1973011061fd7d88f71a3 Mon Sep 17 00:00:00 2001 From: collin Date: Thu, 11 Mar 2021 14:52:30 -0800 Subject: [PATCH 03/21] remove add trait file --- gadgets/src/arithmetic/add.rs | 48 ----------------------------------- gadgets/src/arithmetic/mod.rs | 3 --- 2 files changed, 51 deletions(-) delete mode 100644 gadgets/src/arithmetic/add.rs diff --git a/gadgets/src/arithmetic/add.rs b/gadgets/src/arithmetic/add.rs deleted file mode 100644 index f83624c0e3..0000000000 --- a/gadgets/src/arithmetic/add.rs +++ /dev/null @@ -1,48 +0,0 @@ -// 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 snarkvm_fields::{Field, PrimeField}; -// use snarkvm_gadgets::traits::utilities::uint::{UInt, UInt128, UInt16, UInt32, UInt64, UInt8}; -// use snarkvm_r1cs::{ConstraintSystem, SynthesisError}; -// -// /// Returns addition of `self` + `other` in the constraint system. -// pub trait Add -// where -// Self: std::marker::Sized, -// { -// type ErrorType; -// -// fn add>(&self, cs: CS, other: &Self) -> Result; -// } - -// // Implement unsigned integers -// macro_rules! add_uint_impl { -// ($($gadget: ident),*) => ($( -// impl Add for $gadget { -// type ErrorType = SynthesisError; -// -// fn add>( -// &self, -// cs: CS, -// other: &Self -// ) -> Result { -// <$gadget as UInt>::addmany(cs, &[self.clone(), other.clone()]) -// } -// } -// )*) -// } -// -// add_uint_impl!(UInt8, UInt16, UInt32, UInt64, UInt128); diff --git a/gadgets/src/arithmetic/mod.rs b/gadgets/src/arithmetic/mod.rs index 35b566b87c..c36ba0c526 100644 --- a/gadgets/src/arithmetic/mod.rs +++ b/gadgets/src/arithmetic/mod.rs @@ -14,9 +14,6 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -// pub mod add; -// pub use self::add::*; - pub mod div; pub use self::div::*; From 4e26ca5a40b8f9ec9aeb62699ad336e1ee810531 Mon Sep 17 00:00:00 2001 From: collin Date: Thu, 11 Mar 2021 15:40:03 -0800 Subject: [PATCH 04/21] fix integer_arithmetic benchmark --- gadgets/benches/integer_arithmetic.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gadgets/benches/integer_arithmetic.rs b/gadgets/benches/integer_arithmetic.rs index a75546d7e4..f2e81b8d1c 100644 --- a/gadgets/benches/integer_arithmetic.rs +++ b/gadgets/benches/integer_arithmetic.rs @@ -16,7 +16,7 @@ use leo_gadgets::{arithmetic::*, Int128, Int16, Int32, Int64, Int8}; -use snarkvm_gadgets::traits::utilities::alloc::AllocGadget; +use snarkvm_gadgets::traits::utilities::{alloc::AllocGadget, arithmetic::Add}; use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem}; use rand::{Rng, SeedableRng}; From 11ddb1f761c330c91d85de0f9633fd6e1e506583 Mon Sep 17 00:00:00 2001 From: collin Date: Thu, 11 Mar 2021 15:54:55 -0800 Subject: [PATCH 05/21] refactor to use snarkvm div gadget --- compiler/src/value/integer/integer.rs | 2 +- gadgets/benches/integer_arithmetic.rs | 5 +- gadgets/src/arithmetic/div.rs | 56 ++++++++++---------- gadgets/src/arithmetic/mod.rs | 4 +- gadgets/src/signed_integer/arithmetic/div.rs | 4 +- gadgets/tests/signed_integer/i128.rs | 6 ++- gadgets/tests/signed_integer/i16.rs | 6 ++- gadgets/tests/signed_integer/i32.rs | 6 ++- gadgets/tests/signed_integer/i64.rs | 6 ++- gadgets/tests/signed_integer/i8.rs | 6 ++- 10 files changed, 62 insertions(+), 39 deletions(-) diff --git a/compiler/src/value/integer/integer.rs b/compiler/src/value/integer/integer.rs index 092927681b..bb7bba90a7 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, + arithmetic::{Add, Div}, boolean::Boolean, eq::{ConditionalEqGadget, EqGadget, EvaluateEqGadget}, select::CondSelectGadget, diff --git a/gadgets/benches/integer_arithmetic.rs b/gadgets/benches/integer_arithmetic.rs index f2e81b8d1c..619aea1fb3 100644 --- a/gadgets/benches/integer_arithmetic.rs +++ b/gadgets/benches/integer_arithmetic.rs @@ -16,7 +16,10 @@ use leo_gadgets::{arithmetic::*, Int128, Int16, Int32, Int64, Int8}; -use snarkvm_gadgets::traits::utilities::{alloc::AllocGadget, arithmetic::Add}; +use snarkvm_gadgets::traits::utilities::{ + alloc::AllocGadget, + arithmetic::{Add, Div}, +}; use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem}; use rand::{Rng, SeedableRng}; diff --git a/gadgets/src/arithmetic/div.rs b/gadgets/src/arithmetic/div.rs index ca1a93c899..e8dbcbc860 100644 --- a/gadgets/src/arithmetic/div.rs +++ b/gadgets/src/arithmetic/div.rs @@ -1,28 +1,28 @@ -// 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 snarkvm_fields::Field; -use snarkvm_r1cs::ConstraintSystem; - -/// Returns division of `self` / `other` in the constraint system. -pub trait Div -where - Self: std::marker::Sized, -{ - type ErrorType; - - fn div>(&self, cs: CS, other: &Self) -> Result; -} +// // 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 snarkvm_fields::Field; +// use snarkvm_r1cs::ConstraintSystem; +// +// /// Returns division of `self` / `other` in the constraint system. +// pub trait Div +// where +// Self: std::marker::Sized, +// { +// type ErrorType; +// +// fn div>(&self, cs: CS, other: &Self) -> Result; +// } diff --git a/gadgets/src/arithmetic/mod.rs b/gadgets/src/arithmetic/mod.rs index c36ba0c526..30a1662136 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 div; -pub use self::div::*; +// pub mod div; +// pub use self::div::*; pub mod mul; pub use self::mul::*; diff --git a/gadgets/src/signed_integer/arithmetic/div.rs b/gadgets/src/signed_integer/arithmetic/div.rs index 0b1683f8b1..4cca0c3efa 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::{Div, Neg, Sub}, + arithmetic::{Neg, 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, + arithmetic::{Add, Div}, boolean::{AllocatedBit, Boolean}, eq::EvaluateEqGadget, select::CondSelectGadget, diff --git a/gadgets/tests/signed_integer/i128.rs b/gadgets/tests/signed_integer/i128.rs index acf18acd25..d144309795 100644 --- a/gadgets/tests/signed_integer/i128.rs +++ b/gadgets/tests/signed_integer/i128.rs @@ -17,7 +17,11 @@ use leo_gadgets::{arithmetic::*, Int128}; use snarkvm_fields::{One, Zero}; -use snarkvm_gadgets::traits::utilities::{alloc::AllocGadget, arithmetic::Add, boolean::Boolean}; +use snarkvm_gadgets::traits::utilities::{ + alloc::AllocGadget, + arithmetic::{Add, Div}, + boolean::Boolean, +}; use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem}; use rand::Rng; diff --git a/gadgets/tests/signed_integer/i16.rs b/gadgets/tests/signed_integer/i16.rs index 7770af853a..d37ab50016 100644 --- a/gadgets/tests/signed_integer/i16.rs +++ b/gadgets/tests/signed_integer/i16.rs @@ -17,7 +17,11 @@ use leo_gadgets::{arithmetic::*, Int16}; use snarkvm_fields::{One, Zero}; -use snarkvm_gadgets::traits::utilities::{alloc::AllocGadget, arithmetic::Add, boolean::Boolean}; +use snarkvm_gadgets::traits::utilities::{ + alloc::AllocGadget, + arithmetic::{Add, Div}, + boolean::Boolean, +}; use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem}; use rand::Rng; diff --git a/gadgets/tests/signed_integer/i32.rs b/gadgets/tests/signed_integer/i32.rs index 82c4e00051..0ce5d8829b 100644 --- a/gadgets/tests/signed_integer/i32.rs +++ b/gadgets/tests/signed_integer/i32.rs @@ -17,7 +17,11 @@ use leo_gadgets::{arithmetic::*, Int32}; use snarkvm_fields::{One, Zero}; -use snarkvm_gadgets::traits::utilities::{alloc::AllocGadget, arithmetic::Add, boolean::Boolean}; +use snarkvm_gadgets::traits::utilities::{ + alloc::AllocGadget, + arithmetic::{Add, Div}, + boolean::Boolean, +}; use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem}; use rand::Rng; diff --git a/gadgets/tests/signed_integer/i64.rs b/gadgets/tests/signed_integer/i64.rs index 50dbf38abc..c4b9906ff0 100644 --- a/gadgets/tests/signed_integer/i64.rs +++ b/gadgets/tests/signed_integer/i64.rs @@ -17,7 +17,11 @@ use leo_gadgets::{arithmetic::*, Int64}; use snarkvm_fields::{One, Zero}; -use snarkvm_gadgets::traits::utilities::{alloc::AllocGadget, arithmetic::Add, boolean::Boolean}; +use snarkvm_gadgets::traits::utilities::{ + alloc::AllocGadget, + arithmetic::{Add, Div}, + boolean::Boolean, +}; use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem}; use rand::Rng; diff --git a/gadgets/tests/signed_integer/i8.rs b/gadgets/tests/signed_integer/i8.rs index 2b35aaae8a..4da21ab2b8 100644 --- a/gadgets/tests/signed_integer/i8.rs +++ b/gadgets/tests/signed_integer/i8.rs @@ -17,7 +17,11 @@ use leo_gadgets::{arithmetic::*, Int8}; use snarkvm_fields::{One, Zero}; -use snarkvm_gadgets::traits::utilities::{alloc::AllocGadget, arithmetic::Add, boolean::Boolean}; +use snarkvm_gadgets::traits::utilities::{ + alloc::AllocGadget, + arithmetic::{Add, Div}, + boolean::Boolean, +}; use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem}; use rand::Rng; From e9e8fae2795578ba909f75ca399488401117bef4 Mon Sep 17 00:00:00 2001 From: collin Date: Thu, 11 Mar 2021 15:55:23 -0800 Subject: [PATCH 06/21] remove old div gadget files --- gadgets/src/arithmetic/div.rs | 28 ---------------------------- gadgets/src/arithmetic/mod.rs | 3 --- 2 files changed, 31 deletions(-) delete mode 100644 gadgets/src/arithmetic/div.rs diff --git a/gadgets/src/arithmetic/div.rs b/gadgets/src/arithmetic/div.rs deleted file mode 100644 index e8dbcbc860..0000000000 --- a/gadgets/src/arithmetic/div.rs +++ /dev/null @@ -1,28 +0,0 @@ -// // 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 snarkvm_fields::Field; -// use snarkvm_r1cs::ConstraintSystem; -// -// /// Returns division of `self` / `other` in the constraint system. -// pub trait Div -// where -// Self: std::marker::Sized, -// { -// type ErrorType; -// -// fn div>(&self, cs: CS, other: &Self) -> Result; -// } diff --git a/gadgets/src/arithmetic/mod.rs b/gadgets/src/arithmetic/mod.rs index 30a1662136..2a86235dd5 100644 --- a/gadgets/src/arithmetic/mod.rs +++ b/gadgets/src/arithmetic/mod.rs @@ -14,9 +14,6 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -// pub mod div; -// pub use self::div::*; - pub mod mul; pub use self::mul::*; From d4b8bbbe5089d54b9f6c2a26241f85564ce31d49 Mon Sep 17 00:00:00 2001 From: collin Date: Thu, 11 Mar 2021 16:02:28 -0800 Subject: [PATCH 07/21] refactor to use snarkvm mul gadget --- compiler/src/value/integer/integer.rs | 2 +- gadgets/benches/integer_arithmetic.rs | 2 +- gadgets/src/arithmetic/mod.rs | 4 +- gadgets/src/arithmetic/mul.rs | 56 ++++++++++---------- gadgets/src/signed_integer/arithmetic/mul.rs | 2 +- gadgets/src/signed_integer/arithmetic/pow.rs | 18 +++---- 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 +- 11 files changed, 45 insertions(+), 49 deletions(-) diff --git a/compiler/src/value/integer/integer.rs b/compiler/src/value/integer/integer.rs index bb7bba90a7..5df86d8f21 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}, + arithmetic::{Add, Div, Mul}, boolean::Boolean, eq::{ConditionalEqGadget, EqGadget, EvaluateEqGadget}, select::CondSelectGadget, diff --git a/gadgets/benches/integer_arithmetic.rs b/gadgets/benches/integer_arithmetic.rs index 619aea1fb3..cac9654ba5 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}, + arithmetic::{Add, Div, Mul}, }; use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem}; diff --git a/gadgets/src/arithmetic/mod.rs b/gadgets/src/arithmetic/mod.rs index 2a86235dd5..59629c1fa7 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 mul; -pub use self::mul::*; +// pub mod mul; +// pub use self::mul::*; pub mod neg; pub use self::neg::*; diff --git a/gadgets/src/arithmetic/mul.rs b/gadgets/src/arithmetic/mul.rs index e99b339d4a..f7d4a765d4 100644 --- a/gadgets/src/arithmetic/mul.rs +++ b/gadgets/src/arithmetic/mul.rs @@ -1,28 +1,28 @@ -// 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 snarkvm_fields::Field; -use snarkvm_r1cs::ConstraintSystem; - -/// Returns multiplication of `self` * `other` in the constraint system. -pub trait Mul -where - Self: std::marker::Sized, -{ - type ErrorType; - - fn mul>(&self, cs: CS, other: &Self) -> Result; -} +// // 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 snarkvm_fields::Field; +// use snarkvm_r1cs::ConstraintSystem; +// +// /// Returns multiplication of `self` * `other` in the constraint system. +// pub trait Mul +// where +// Self: std::marker::Sized, +// { +// type ErrorType; +// +// fn mul>(&self, cs: CS, other: &Self) -> Result; +// } diff --git a/gadgets/src/signed_integer/arithmetic/mul.rs b/gadgets/src/signed_integer/arithmetic/mul.rs index fb6b792715..3b6fe8f8d4 100644 --- a/gadgets/src/signed_integer/arithmetic/mul.rs +++ b/gadgets/src/signed_integer/arithmetic/mul.rs @@ -15,7 +15,6 @@ // along with the Leo library. If not, see . use crate::{ - arithmetic::Mul, bits::{RippleCarryAdder, SignExtend}, errors::SignedIntegerError, Int, @@ -28,6 +27,7 @@ use crate::{ use snarkvm_fields::{FpParameters, PrimeField}; use snarkvm_gadgets::traits::utilities::{ alloc::AllocGadget, + arithmetic::Mul, boolean::{AllocatedBit, Boolean}, select::CondSelectGadget, }; diff --git a/gadgets/src/signed_integer/arithmetic/pow.rs b/gadgets/src/signed_integer/arithmetic/pow.rs index 3f6467978f..c9f6e70260 100644 --- a/gadgets/src/signed_integer/arithmetic/pow.rs +++ b/gadgets/src/signed_integer/arithmetic/pow.rs @@ -14,19 +14,15 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{ - arithmetic::{Mul, Pow}, - errors::SignedIntegerError, - Int, - Int128, - Int16, - Int32, - Int64, - Int8, -}; +use crate::{arithmetic::Pow, errors::SignedIntegerError, Int, Int128, Int16, Int32, Int64, Int8}; use snarkvm_fields::PrimeField; -use snarkvm_gadgets::traits::utilities::{alloc::AllocGadget, boolean::Boolean, select::CondSelectGadget}; +use snarkvm_gadgets::traits::utilities::{ + alloc::AllocGadget, + arithmetic::Mul, + boolean::Boolean, + select::CondSelectGadget, +}; use snarkvm_r1cs::ConstraintSystem; macro_rules! pow_int_impl { diff --git a/gadgets/tests/signed_integer/i128.rs b/gadgets/tests/signed_integer/i128.rs index d144309795..5dc1c237e1 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}, + arithmetic::{Add, Div, Mul}, 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 d37ab50016..30d4f3d9a2 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}, + arithmetic::{Add, Div, Mul}, 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 0ce5d8829b..8c10418e29 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}, + arithmetic::{Add, Div, Mul}, 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 c4b9906ff0..22e982a421 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}, + arithmetic::{Add, Div, Mul}, 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 4da21ab2b8..f8a2343711 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}, + arithmetic::{Add, Div, Mul}, boolean::Boolean, }; use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem}; From c02b281ed0f835b3b21fb1ce44400b31cc8381bb Mon Sep 17 00:00:00 2001 From: collin Date: Thu, 11 Mar 2021 16:03:20 -0800 Subject: [PATCH 08/21] remove old mul gadget files --- gadgets/src/arithmetic/mod.rs | 3 --- gadgets/src/arithmetic/mul.rs | 28 ---------------------------- 2 files changed, 31 deletions(-) delete mode 100644 gadgets/src/arithmetic/mul.rs diff --git a/gadgets/src/arithmetic/mod.rs b/gadgets/src/arithmetic/mod.rs index 59629c1fa7..5758f728c6 100644 --- a/gadgets/src/arithmetic/mod.rs +++ b/gadgets/src/arithmetic/mod.rs @@ -14,9 +14,6 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -// pub mod mul; -// pub use self::mul::*; - pub mod neg; pub use self::neg::*; diff --git a/gadgets/src/arithmetic/mul.rs b/gadgets/src/arithmetic/mul.rs deleted file mode 100644 index f7d4a765d4..0000000000 --- a/gadgets/src/arithmetic/mul.rs +++ /dev/null @@ -1,28 +0,0 @@ -// // 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 snarkvm_fields::Field; -// use snarkvm_r1cs::ConstraintSystem; -// -// /// Returns multiplication of `self` * `other` in the constraint system. -// pub trait Mul -// where -// Self: std::marker::Sized, -// { -// type ErrorType; -// -// fn mul>(&self, cs: CS, other: &Self) -> Result; -// } From faacfbc97ed64ffc284f7a9efba85b9c6d77d2f9 Mon Sep 17 00:00:00 2001 From: collin Date: Thu, 11 Mar 2021 16:12:33 -0800 Subject: [PATCH 09/21] 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}; From fd41c70250c52400f056883f38f938d924da4b38 Mon Sep 17 00:00:00 2001 From: collin Date: Thu, 11 Mar 2021 16:13:21 -0800 Subject: [PATCH 10/21] remove old neg gadget files --- gadgets/src/arithmetic/mod.rs | 3 -- gadgets/src/arithmetic/neg.rs | 52 ----------------------------------- 2 files changed, 55 deletions(-) delete mode 100644 gadgets/src/arithmetic/neg.rs diff --git a/gadgets/src/arithmetic/mod.rs b/gadgets/src/arithmetic/mod.rs index 145bda7c01..986f5e44f1 100644 --- a/gadgets/src/arithmetic/mod.rs +++ b/gadgets/src/arithmetic/mod.rs @@ -14,9 +14,6 @@ // 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 pow; pub use self::pow::*; diff --git a/gadgets/src/arithmetic/neg.rs b/gadgets/src/arithmetic/neg.rs deleted file mode 100644 index 48b09c5381..0000000000 --- a/gadgets/src/arithmetic/neg.rs +++ /dev/null @@ -1,52 +0,0 @@ -// // 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) -// } -// } From adce3766759aedd5986cce92f139f9b89731435a Mon Sep 17 00:00:00 2001 From: collin Date: Thu, 11 Mar 2021 16:18:00 -0800 Subject: [PATCH 11/21] refactor to use snarkvm pow gadget --- gadgets/benches/integer_arithmetic.rs | 2 +- gadgets/src/arithmetic/mod.rs | 4 +- gadgets/src/arithmetic/pow.rs | 56 ++++++++++---------- gadgets/src/signed_integer/arithmetic/pow.rs | 4 +- 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 +- 9 files changed, 38 insertions(+), 38 deletions(-) diff --git a/gadgets/benches/integer_arithmetic.rs b/gadgets/benches/integer_arithmetic.rs index c31358142e..606ab14191 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, Neg}, + arithmetic::{Add, Div, Mul, Neg, Pow}, }; use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem}; diff --git a/gadgets/src/arithmetic/mod.rs b/gadgets/src/arithmetic/mod.rs index 986f5e44f1..9c05b77811 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 pow; -pub use self::pow::*; +// pub mod pow; +// pub use self::pow::*; pub mod sub; pub use self::sub::*; diff --git a/gadgets/src/arithmetic/pow.rs b/gadgets/src/arithmetic/pow.rs index bb1e8427c9..65951bd425 100644 --- a/gadgets/src/arithmetic/pow.rs +++ b/gadgets/src/arithmetic/pow.rs @@ -1,28 +1,28 @@ -// 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 snarkvm_fields::Field; -use snarkvm_r1cs::ConstraintSystem; - -/// Returns exponentiation of `self` ** `other` in the constraint system. -pub trait Pow -where - Self: std::marker::Sized, -{ - type ErrorType; - - fn pow>(&self, cs: CS, other: &Self) -> Result; -} +// // 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 snarkvm_fields::Field; +// use snarkvm_r1cs::ConstraintSystem; +// +// /// Returns exponentiation of `self` ** `other` in the constraint system. +// pub trait Pow +// where +// Self: std::marker::Sized, +// { +// type ErrorType; +// +// fn pow>(&self, cs: CS, other: &Self) -> Result; +// } diff --git a/gadgets/src/signed_integer/arithmetic/pow.rs b/gadgets/src/signed_integer/arithmetic/pow.rs index c9f6e70260..faef2159ff 100644 --- a/gadgets/src/signed_integer/arithmetic/pow.rs +++ b/gadgets/src/signed_integer/arithmetic/pow.rs @@ -14,12 +14,12 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{arithmetic::Pow, errors::SignedIntegerError, Int, Int128, Int16, Int32, Int64, Int8}; +use crate::{errors::SignedIntegerError, Int, Int128, Int16, Int32, Int64, Int8}; use snarkvm_fields::PrimeField; use snarkvm_gadgets::traits::utilities::{ alloc::AllocGadget, - arithmetic::Mul, + arithmetic::{Mul, Pow}, boolean::Boolean, select::CondSelectGadget, }; diff --git a/gadgets/tests/signed_integer/i128.rs b/gadgets/tests/signed_integer/i128.rs index 3bc93788d1..b2b05ee437 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, Neg}, + arithmetic::{Add, Div, Mul, Neg, Pow}, 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 4f56547b85..29c0c02aa6 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, Neg}, + arithmetic::{Add, Div, Mul, Neg, Pow}, 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 440cfbd631..e828c31f45 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, Neg}, + arithmetic::{Add, Div, Mul, Neg, Pow}, 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 a46ed15cbc..30a11e5e6a 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, Neg}, + arithmetic::{Add, Div, Mul, Neg, Pow}, 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 a9cd91b092..dfe6753225 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, Neg}, + arithmetic::{Add, Div, Mul, Neg, Pow}, boolean::Boolean, }; use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem}; From b5ae6dee64335a0a480453d3414853ceaf434d27 Mon Sep 17 00:00:00 2001 From: collin Date: Thu, 11 Mar 2021 16:19:20 -0800 Subject: [PATCH 12/21] remove old pow gadget files --- compiler/src/value/integer/integer.rs | 2 +- gadgets/src/arithmetic/mod.rs | 3 --- gadgets/src/arithmetic/pow.rs | 28 --------------------------- 3 files changed, 1 insertion(+), 32 deletions(-) delete mode 100644 gadgets/src/arithmetic/pow.rs diff --git a/compiler/src/value/integer/integer.rs b/compiler/src/value/integer/integer.rs index c7bdce46ab..92e3e365bc 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, Neg}, + arithmetic::{Add, Div, Mul, Neg, Pow}, boolean::Boolean, eq::{ConditionalEqGadget, EqGadget, EvaluateEqGadget}, select::CondSelectGadget, diff --git a/gadgets/src/arithmetic/mod.rs b/gadgets/src/arithmetic/mod.rs index 9c05b77811..a086490a6b 100644 --- a/gadgets/src/arithmetic/mod.rs +++ b/gadgets/src/arithmetic/mod.rs @@ -14,8 +14,5 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -// pub mod pow; -// pub use self::pow::*; - pub mod sub; pub use self::sub::*; diff --git a/gadgets/src/arithmetic/pow.rs b/gadgets/src/arithmetic/pow.rs deleted file mode 100644 index 65951bd425..0000000000 --- a/gadgets/src/arithmetic/pow.rs +++ /dev/null @@ -1,28 +0,0 @@ -// // 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 snarkvm_fields::Field; -// use snarkvm_r1cs::ConstraintSystem; -// -// /// Returns exponentiation of `self` ** `other` in the constraint system. -// pub trait Pow -// where -// Self: std::marker::Sized, -// { -// type ErrorType; -// -// fn pow>(&self, cs: CS, other: &Self) -> Result; -// } From 284ef310de5b6bf508b9e25b90ed0695db7a77b7 Mon Sep 17 00:00:00 2001 From: collin Date: Thu, 11 Mar 2021 16:21:27 -0800 Subject: [PATCH 13/21] remove unused neg dependency --- gadgets/benches/integer_arithmetic.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gadgets/benches/integer_arithmetic.rs b/gadgets/benches/integer_arithmetic.rs index c31358142e..cac9654ba5 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, Neg}, + arithmetic::{Add, Div, Mul}, }; use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem}; From 522e3b6ea68cbd6c0eda8e4f433946c5b8939bbe Mon Sep 17 00:00:00 2001 From: collin Date: Thu, 11 Mar 2021 16:27:06 -0800 Subject: [PATCH 14/21] refactor to use snarkvm sub gadget --- compiler/src/value/integer/integer.rs | 3 +- gadgets/benches/integer_arithmetic.rs | 2 +- gadgets/src/arithmetic/mod.rs | 6 +-- gadgets/src/arithmetic/sub.rs | 56 ++++++++++---------- gadgets/src/lib.rs | 2 +- gadgets/src/signed_integer/arithmetic/div.rs | 14 +---- gadgets/src/signed_integer/arithmetic/sub.rs | 4 +- 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, 43 insertions(+), 54 deletions(-) diff --git a/compiler/src/value/integer/integer.rs b/compiler/src/value/integer/integer.rs index 92e3e365bc..b4c8b2b7ce 100644 --- a/compiler/src/value/integer/integer.rs +++ b/compiler/src/value/integer/integer.rs @@ -19,7 +19,6 @@ use crate::{errors::IntegerError, IntegerTrait}; use leo_asg::{ConstInt, IntegerType, Span}; use leo_ast::InputValue; use leo_gadgets::{ - arithmetic::*, bits::comparator::{ComparatorGadget, EvaluateLtGadget}, signed_integer::*, }; @@ -27,7 +26,7 @@ use leo_gadgets::{ use snarkvm_fields::{Field, PrimeField}; use snarkvm_gadgets::traits::utilities::{ alloc::AllocGadget, - arithmetic::{Add, Div, Mul, Neg, Pow}, + arithmetic::{Add, Div, Mul, Neg, Pow, Sub}, boolean::Boolean, eq::{ConditionalEqGadget, EqGadget, EvaluateEqGadget}, select::CondSelectGadget, diff --git a/gadgets/benches/integer_arithmetic.rs b/gadgets/benches/integer_arithmetic.rs index cac9654ba5..cb7d38167c 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, Sub}, }; use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem}; diff --git a/gadgets/src/arithmetic/mod.rs b/gadgets/src/arithmetic/mod.rs index a086490a6b..968073465f 100644 --- a/gadgets/src/arithmetic/mod.rs +++ b/gadgets/src/arithmetic/mod.rs @@ -13,6 +13,6 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . - -pub mod sub; -pub use self::sub::*; +// +// pub mod sub; +// pub use self::sub::*; diff --git a/gadgets/src/arithmetic/sub.rs b/gadgets/src/arithmetic/sub.rs index ee66603526..9431ce92f1 100644 --- a/gadgets/src/arithmetic/sub.rs +++ b/gadgets/src/arithmetic/sub.rs @@ -1,28 +1,28 @@ -// 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 snarkvm_fields::Field; -use snarkvm_r1cs::ConstraintSystem; - -/// Returns subtraction of `self` - `other` in the constraint system. -pub trait Sub -where - Self: std::marker::Sized, -{ - type ErrorType; - - fn sub>(&self, cs: CS, other: &Self) -> Result; -} +// // 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 snarkvm_fields::Field; +// use snarkvm_r1cs::ConstraintSystem; +// +// /// Returns subtraction of `self` - `other` in the constraint system. +// pub trait Sub +// where +// Self: std::marker::Sized, +// { +// type ErrorType; +// +// fn sub>(&self, cs: CS, other: &Self) -> Result; +// } diff --git a/gadgets/src/lib.rs b/gadgets/src/lib.rs index d5ae0dea06..5c976aad8e 100644 --- a/gadgets/src/lib.rs +++ b/gadgets/src/lib.rs @@ -17,7 +17,7 @@ #[macro_use] extern crate thiserror; -pub mod arithmetic; +// pub mod arithmetic; pub mod bits; diff --git a/gadgets/src/signed_integer/arithmetic/div.rs b/gadgets/src/signed_integer/arithmetic/div.rs index a4a47ac9fb..7048ff1df1 100644 --- a/gadgets/src/signed_integer/arithmetic/div.rs +++ b/gadgets/src/signed_integer/arithmetic/div.rs @@ -14,21 +14,11 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{ - arithmetic::Sub, - bits::ComparatorGadget, - errors::SignedIntegerError, - Int, - Int128, - Int16, - Int32, - Int64, - Int8, -}; +use crate::{bits::ComparatorGadget, errors::SignedIntegerError, Int, Int128, Int16, Int32, Int64, Int8}; use snarkvm_fields::PrimeField; use snarkvm_gadgets::traits::utilities::{ alloc::AllocGadget, - arithmetic::{Add, Div, Neg}, + arithmetic::{Add, Div, Neg, Sub}, boolean::{AllocatedBit, Boolean}, eq::EvaluateEqGadget, select::CondSelectGadget, diff --git a/gadgets/src/signed_integer/arithmetic/sub.rs b/gadgets/src/signed_integer/arithmetic/sub.rs index aaf51a59ac..df49d4a606 100644 --- a/gadgets/src/signed_integer/arithmetic/sub.rs +++ b/gadgets/src/signed_integer/arithmetic/sub.rs @@ -14,9 +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::Sub, errors::SignedIntegerError, Int128, Int16, Int32, Int64, Int8}; +use crate::{errors::SignedIntegerError, Int128, Int16, Int32, Int64, Int8}; use snarkvm_fields::PrimeField; -use snarkvm_gadgets::traits::utilities::arithmetic::{Add, Neg}; +use snarkvm_gadgets::traits::utilities::arithmetic::{Add, Neg, Sub}; 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 b2b05ee437..9105fcec43 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, Neg, Pow}, + arithmetic::{Add, Div, Mul, Neg, Pow, Sub}, 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 29c0c02aa6..75cac1faf0 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, Neg, Pow}, + arithmetic::{Add, Div, Mul, Neg, Pow, Sub}, 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 e828c31f45..7683c54a01 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, Neg, Pow}, + arithmetic::{Add, Div, Mul, Neg, Pow, Sub}, 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 30a11e5e6a..35391a9aa5 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, Neg, Pow}, + arithmetic::{Add, Div, Mul, Neg, Pow, Sub}, 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 dfe6753225..a9f41f5cff 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, Neg, Pow}, + arithmetic::{Add, Div, Mul, Neg, Pow, Sub}, boolean::Boolean, }; use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem}; From e7745bad80875d00df7a784e43193e00ec54470f Mon Sep 17 00:00:00 2001 From: collin Date: Thu, 11 Mar 2021 16:28:22 -0800 Subject: [PATCH 15/21] remove old sub and arithmetic gadget files --- gadgets/src/arithmetic/mod.rs | 18 ------------------ gadgets/src/arithmetic/sub.rs | 28 ---------------------------- gadgets/src/lib.rs | 2 -- 3 files changed, 48 deletions(-) delete mode 100644 gadgets/src/arithmetic/mod.rs delete mode 100644 gadgets/src/arithmetic/sub.rs diff --git a/gadgets/src/arithmetic/mod.rs b/gadgets/src/arithmetic/mod.rs deleted file mode 100644 index 968073465f..0000000000 --- a/gadgets/src/arithmetic/mod.rs +++ /dev/null @@ -1,18 +0,0 @@ -// 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 . -// -// pub mod sub; -// pub use self::sub::*; diff --git a/gadgets/src/arithmetic/sub.rs b/gadgets/src/arithmetic/sub.rs deleted file mode 100644 index 9431ce92f1..0000000000 --- a/gadgets/src/arithmetic/sub.rs +++ /dev/null @@ -1,28 +0,0 @@ -// // 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 snarkvm_fields::Field; -// use snarkvm_r1cs::ConstraintSystem; -// -// /// Returns subtraction of `self` - `other` in the constraint system. -// pub trait Sub -// where -// Self: std::marker::Sized, -// { -// type ErrorType; -// -// fn sub>(&self, cs: CS, other: &Self) -> Result; -// } diff --git a/gadgets/src/lib.rs b/gadgets/src/lib.rs index 5c976aad8e..38eed81ac1 100644 --- a/gadgets/src/lib.rs +++ b/gadgets/src/lib.rs @@ -17,8 +17,6 @@ #[macro_use] extern crate thiserror; -// pub mod arithmetic; - pub mod bits; pub mod errors; From 14063f723b6e9a7666a67a9d921b86d1ec12a513 Mon Sep 17 00:00:00 2001 From: collin Date: Thu, 11 Mar 2021 16:44:46 -0800 Subject: [PATCH 16/21] refactor to use snarkvm evaluatelt and comparator gadgets --- compiler/src/expression/relational/ge.rs | 2 +- compiler/src/expression/relational/gt.rs | 2 +- compiler/src/expression/relational/le.rs | 2 +- compiler/src/expression/relational/lt.rs | 2 +- compiler/src/value/integer/integer.rs | 6 +- gadgets/src/bits/comparator.rs | 186 +++++++++---------- gadgets/src/bits/mod.rs | 4 +- gadgets/src/signed_integer/arithmetic/div.rs | 3 +- gadgets/src/signed_integer/relational/cmp.rs | 15 +- 9 files changed, 109 insertions(+), 113 deletions(-) diff --git a/compiler/src/expression/relational/ge.rs b/compiler/src/expression/relational/ge.rs index a7913f11c7..0f72215bda 100644 --- a/compiler/src/expression/relational/ge.rs +++ b/compiler/src/expression/relational/ge.rs @@ -18,9 +18,9 @@ use crate::{errors::ExpressionError, value::ConstrainedValue, GroupType}; use leo_asg::Span; -use leo_gadgets::bits::ComparatorGadget; use snarkvm_fields::PrimeField; +use snarkvm_gadgets::utilities::bits::ComparatorGadget; use snarkvm_r1cs::ConstraintSystem; pub fn evaluate_ge<'a, F: PrimeField, G: GroupType, CS: ConstraintSystem>( diff --git a/compiler/src/expression/relational/gt.rs b/compiler/src/expression/relational/gt.rs index bc1deccf95..eda1055be7 100644 --- a/compiler/src/expression/relational/gt.rs +++ b/compiler/src/expression/relational/gt.rs @@ -18,9 +18,9 @@ use crate::{errors::ExpressionError, value::ConstrainedValue, GroupType}; use leo_asg::Span; -use leo_gadgets::bits::ComparatorGadget; use snarkvm_fields::PrimeField; +use snarkvm_gadgets::utilities::bits::ComparatorGadget; use snarkvm_r1cs::ConstraintSystem; pub fn evaluate_gt<'a, F: PrimeField, G: GroupType, CS: ConstraintSystem>( diff --git a/compiler/src/expression/relational/le.rs b/compiler/src/expression/relational/le.rs index 3dd23855c5..36e63d41df 100644 --- a/compiler/src/expression/relational/le.rs +++ b/compiler/src/expression/relational/le.rs @@ -18,9 +18,9 @@ use crate::{errors::ExpressionError, value::ConstrainedValue, GroupType}; use leo_asg::Span; -use leo_gadgets::bits::ComparatorGadget; use snarkvm_fields::PrimeField; +use snarkvm_gadgets::utilities::bits::ComparatorGadget; use snarkvm_r1cs::ConstraintSystem; pub fn evaluate_le<'a, F: PrimeField, G: GroupType, CS: ConstraintSystem>( diff --git a/compiler/src/expression/relational/lt.rs b/compiler/src/expression/relational/lt.rs index 0bbd4a4219..72fd5c5fdc 100644 --- a/compiler/src/expression/relational/lt.rs +++ b/compiler/src/expression/relational/lt.rs @@ -18,9 +18,9 @@ use crate::{errors::ExpressionError, value::ConstrainedValue, GroupType}; use leo_asg::Span; -use leo_gadgets::bits::comparator::EvaluateLtGadget; use snarkvm_fields::PrimeField; +use snarkvm_gadgets::utilities::bits::EvaluateLtGadget; use snarkvm_r1cs::ConstraintSystem; pub fn evaluate_lt<'a, F: PrimeField, G: GroupType, CS: ConstraintSystem>( diff --git a/compiler/src/value/integer/integer.rs b/compiler/src/value/integer/integer.rs index b4c8b2b7ce..a2affe09b6 100644 --- a/compiler/src/value/integer/integer.rs +++ b/compiler/src/value/integer/integer.rs @@ -18,15 +18,13 @@ use crate::{errors::IntegerError, IntegerTrait}; use leo_asg::{ConstInt, IntegerType, Span}; use leo_ast::InputValue; -use leo_gadgets::{ - bits::comparator::{ComparatorGadget, EvaluateLtGadget}, - signed_integer::*, -}; +use leo_gadgets::signed_integer::*; use snarkvm_fields::{Field, PrimeField}; use snarkvm_gadgets::traits::utilities::{ alloc::AllocGadget, arithmetic::{Add, Div, Mul, Neg, Pow, Sub}, + bits::comparator::{ComparatorGadget, EvaluateLtGadget}, boolean::Boolean, eq::{ConditionalEqGadget, EqGadget, EvaluateEqGadget}, select::CondSelectGadget, diff --git a/gadgets/src/bits/comparator.rs b/gadgets/src/bits/comparator.rs index ef0a6fc5ad..40cbec43b0 100644 --- a/gadgets/src/bits/comparator.rs +++ b/gadgets/src/bits/comparator.rs @@ -1,93 +1,93 @@ -// 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 snarkvm_fields::{Field, PrimeField}; -use snarkvm_gadgets::traits::utilities::{ - boolean::Boolean, - select::CondSelectGadget, - uint::{UInt128, UInt16, UInt32, UInt64, UInt8}, -}; -use snarkvm_r1cs::{ConstraintSystem, SynthesisError}; - -pub trait EvaluateLtGadget { - fn less_than>(&self, cs: CS, other: &Self) -> Result; -} - -// implementing `EvaluateLtGadget` will implement `ComparatorGadget` -pub trait ComparatorGadget -where - Self: EvaluateLtGadget, -{ - fn greater_than>(&self, cs: CS, other: &Self) -> Result { - other.less_than(cs, self) - } - - fn less_than_or_equal>(&self, cs: CS, other: &Self) -> Result { - let is_gt = self.greater_than(cs, other)?; - Ok(is_gt.not()) - } - - fn greater_than_or_equal>(&self, cs: CS, other: &Self) -> Result { - other.less_than_or_equal(cs, self) - } -} - -macro_rules! uint_cmp_impl { - ($($gadget: ident),*) => ($( - /* Bitwise less than comparison of two unsigned integers */ - impl EvaluateLtGadget for $gadget { - fn less_than>(&self, mut cs: CS, other: &Self) -> Result { - - let mut result = Boolean::constant(true); - let mut all_equal = Boolean::constant(true); - - // msb -> lsb - for (i, (a, b)) in self - .bits - .iter() - .rev() - .zip(other.bits.iter().rev()) - .enumerate() - { - // a == 0 & b == 1 - let less = Boolean::and(cs.ns(|| format!("not a and b [{}]", i)), &a.not(), b)?; - - // a == b = !(a ^ b) - let not_equal = Boolean::xor(cs.ns(|| format!("a XOR b [{}]", i)), a, b)?; - let equal = not_equal.not(); - - // evaluate a <= b - let less_or_equal = Boolean::or(cs.ns(|| format!("less or equal [{}]", i)), &less, &equal)?; - - // select the current result if it is the first bit difference - result = Boolean::conditionally_select(cs.ns(|| format!("select bit [{}]", i)), &all_equal, &less_or_equal, &result)?; - - // keep track of equal bits - all_equal = Boolean::and(cs.ns(|| format!("accumulate equal [{}]", i)), &all_equal, &equal)?; - } - - result = Boolean::and(cs.ns(|| format!("false if all equal")), &result, &all_equal.not())?; - - Ok(result) - } - } - - /* Bitwise comparison of two unsigned integers */ - impl ComparatorGadget for $gadget {} - )*) -} - -uint_cmp_impl!(UInt8, UInt16, UInt32, UInt64, UInt128); +// // 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 snarkvm_fields::{Field, PrimeField}; +// use snarkvm_gadgets::traits::utilities::{ +// boolean::Boolean, +// select::CondSelectGadget, +// uint::{UInt128, UInt16, UInt32, UInt64, UInt8}, +// }; +// use snarkvm_r1cs::{ConstraintSystem, SynthesisError}; +// +// pub trait EvaluateLtGadget { +// fn less_than>(&self, cs: CS, other: &Self) -> Result; +// } +// +// // implementing `EvaluateLtGadget` will implement `ComparatorGadget` +// pub trait ComparatorGadget +// where +// Self: EvaluateLtGadget, +// { +// fn greater_than>(&self, cs: CS, other: &Self) -> Result { +// other.less_than(cs, self) +// } +// +// fn less_than_or_equal>(&self, cs: CS, other: &Self) -> Result { +// let is_gt = self.greater_than(cs, other)?; +// Ok(is_gt.not()) +// } +// +// fn greater_than_or_equal>(&self, cs: CS, other: &Self) -> Result { +// other.less_than_or_equal(cs, self) +// } +// } +// +// macro_rules! uint_cmp_impl { +// ($($gadget: ident),*) => ($( +// /* Bitwise less than comparison of two unsigned integers */ +// impl EvaluateLtGadget for $gadget { +// fn less_than>(&self, mut cs: CS, other: &Self) -> Result { +// +// let mut result = Boolean::constant(true); +// let mut all_equal = Boolean::constant(true); +// +// // msb -> lsb +// for (i, (a, b)) in self +// .bits +// .iter() +// .rev() +// .zip(other.bits.iter().rev()) +// .enumerate() +// { +// // a == 0 & b == 1 +// let less = Boolean::and(cs.ns(|| format!("not a and b [{}]", i)), &a.not(), b)?; +// +// // a == b = !(a ^ b) +// let not_equal = Boolean::xor(cs.ns(|| format!("a XOR b [{}]", i)), a, b)?; +// let equal = not_equal.not(); +// +// // evaluate a <= b +// let less_or_equal = Boolean::or(cs.ns(|| format!("less or equal [{}]", i)), &less, &equal)?; +// +// // select the current result if it is the first bit difference +// result = Boolean::conditionally_select(cs.ns(|| format!("select bit [{}]", i)), &all_equal, &less_or_equal, &result)?; +// +// // keep track of equal bits +// all_equal = Boolean::and(cs.ns(|| format!("accumulate equal [{}]", i)), &all_equal, &equal)?; +// } +// +// result = Boolean::and(cs.ns(|| format!("false if all equal")), &result, &all_equal.not())?; +// +// Ok(result) +// } +// } +// +// /* Bitwise comparison of two unsigned integers */ +// impl ComparatorGadget for $gadget {} +// )*) +// } +// +// uint_cmp_impl!(UInt8, UInt16, UInt32, UInt64, UInt128); diff --git a/gadgets/src/bits/mod.rs b/gadgets/src/bits/mod.rs index e4842090db..19d137f833 100644 --- a/gadgets/src/bits/mod.rs +++ b/gadgets/src/bits/mod.rs @@ -18,8 +18,8 @@ pub mod adder; pub use self::adder::*; -pub mod comparator; -pub use self::comparator::*; +// pub mod comparator; +// pub use self::comparator::*; pub mod rca; pub use self::rca::*; diff --git a/gadgets/src/signed_integer/arithmetic/div.rs b/gadgets/src/signed_integer/arithmetic/div.rs index 7048ff1df1..656583a643 100644 --- a/gadgets/src/signed_integer/arithmetic/div.rs +++ b/gadgets/src/signed_integer/arithmetic/div.rs @@ -14,11 +14,12 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{bits::ComparatorGadget, errors::SignedIntegerError, Int, Int128, Int16, Int32, Int64, Int8}; +use crate::{errors::SignedIntegerError, Int, Int128, Int16, Int32, Int64, Int8}; use snarkvm_fields::PrimeField; use snarkvm_gadgets::traits::utilities::{ alloc::AllocGadget, arithmetic::{Add, Div, Neg, Sub}, + bits::ComparatorGadget, boolean::{AllocatedBit, Boolean}, eq::EvaluateEqGadget, select::CondSelectGadget, diff --git a/gadgets/src/signed_integer/relational/cmp.rs b/gadgets/src/signed_integer/relational/cmp.rs index 07b2537748..26cfcaea7c 100644 --- a/gadgets/src/signed_integer/relational/cmp.rs +++ b/gadgets/src/signed_integer/relational/cmp.rs @@ -14,17 +14,14 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{ - bits::{ComparatorGadget, EvaluateLtGadget}, - Int128, - Int16, - Int32, - Int64, - Int8, -}; +use crate::{Int128, Int16, Int32, Int64, Int8}; use snarkvm_fields::PrimeField; -use snarkvm_gadgets::traits::utilities::{boolean::Boolean, select::CondSelectGadget}; +use snarkvm_gadgets::traits::utilities::{ + bits::comparator::{ComparatorGadget, EvaluateLtGadget}, + boolean::Boolean, + select::CondSelectGadget, +}; use snarkvm_r1cs::{ConstraintSystem, SynthesisError}; use std::cmp::Ordering; From 34c2d24bf14be0da1db8088f65c44ac9228b796e Mon Sep 17 00:00:00 2001 From: collin Date: Thu, 11 Mar 2021 16:46:29 -0800 Subject: [PATCH 17/21] remove old comparator files --- gadgets/src/bits/comparator.rs | 93 ---------------------------------- gadgets/src/bits/mod.rs | 3 -- 2 files changed, 96 deletions(-) delete mode 100644 gadgets/src/bits/comparator.rs diff --git a/gadgets/src/bits/comparator.rs b/gadgets/src/bits/comparator.rs deleted file mode 100644 index 40cbec43b0..0000000000 --- a/gadgets/src/bits/comparator.rs +++ /dev/null @@ -1,93 +0,0 @@ -// // 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 snarkvm_fields::{Field, PrimeField}; -// use snarkvm_gadgets::traits::utilities::{ -// boolean::Boolean, -// select::CondSelectGadget, -// uint::{UInt128, UInt16, UInt32, UInt64, UInt8}, -// }; -// use snarkvm_r1cs::{ConstraintSystem, SynthesisError}; -// -// pub trait EvaluateLtGadget { -// fn less_than>(&self, cs: CS, other: &Self) -> Result; -// } -// -// // implementing `EvaluateLtGadget` will implement `ComparatorGadget` -// pub trait ComparatorGadget -// where -// Self: EvaluateLtGadget, -// { -// fn greater_than>(&self, cs: CS, other: &Self) -> Result { -// other.less_than(cs, self) -// } -// -// fn less_than_or_equal>(&self, cs: CS, other: &Self) -> Result { -// let is_gt = self.greater_than(cs, other)?; -// Ok(is_gt.not()) -// } -// -// fn greater_than_or_equal>(&self, cs: CS, other: &Self) -> Result { -// other.less_than_or_equal(cs, self) -// } -// } -// -// macro_rules! uint_cmp_impl { -// ($($gadget: ident),*) => ($( -// /* Bitwise less than comparison of two unsigned integers */ -// impl EvaluateLtGadget for $gadget { -// fn less_than>(&self, mut cs: CS, other: &Self) -> Result { -// -// let mut result = Boolean::constant(true); -// let mut all_equal = Boolean::constant(true); -// -// // msb -> lsb -// for (i, (a, b)) in self -// .bits -// .iter() -// .rev() -// .zip(other.bits.iter().rev()) -// .enumerate() -// { -// // a == 0 & b == 1 -// let less = Boolean::and(cs.ns(|| format!("not a and b [{}]", i)), &a.not(), b)?; -// -// // a == b = !(a ^ b) -// let not_equal = Boolean::xor(cs.ns(|| format!("a XOR b [{}]", i)), a, b)?; -// let equal = not_equal.not(); -// -// // evaluate a <= b -// let less_or_equal = Boolean::or(cs.ns(|| format!("less or equal [{}]", i)), &less, &equal)?; -// -// // select the current result if it is the first bit difference -// result = Boolean::conditionally_select(cs.ns(|| format!("select bit [{}]", i)), &all_equal, &less_or_equal, &result)?; -// -// // keep track of equal bits -// all_equal = Boolean::and(cs.ns(|| format!("accumulate equal [{}]", i)), &all_equal, &equal)?; -// } -// -// result = Boolean::and(cs.ns(|| format!("false if all equal")), &result, &all_equal.not())?; -// -// Ok(result) -// } -// } -// -// /* Bitwise comparison of two unsigned integers */ -// impl ComparatorGadget for $gadget {} -// )*) -// } -// -// uint_cmp_impl!(UInt8, UInt16, UInt32, UInt64, UInt128); diff --git a/gadgets/src/bits/mod.rs b/gadgets/src/bits/mod.rs index 19d137f833..664a5d31b9 100644 --- a/gadgets/src/bits/mod.rs +++ b/gadgets/src/bits/mod.rs @@ -18,9 +18,6 @@ pub mod adder; pub use self::adder::*; -// pub mod comparator; -// pub use self::comparator::*; - pub mod rca; pub use self::rca::*; From 7f3916e679f2a80fdd85b0f272a4845ed85b0b9f Mon Sep 17 00:00:00 2001 From: collin Date: Thu, 11 Mar 2021 16:50:56 -0800 Subject: [PATCH 18/21] remove unused dependencies from tests --- gadgets/tests/signed_integer/i128.rs | 4 ++-- gadgets/tests/signed_integer/i16.rs | 4 ++-- gadgets/tests/signed_integer/i32.rs | 4 ++-- gadgets/tests/signed_integer/i64.rs | 4 ++-- gadgets/tests/signed_integer/i8.rs | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/gadgets/tests/signed_integer/i128.rs b/gadgets/tests/signed_integer/i128.rs index 9105fcec43..96c78d4a30 100644 --- a/gadgets/tests/signed_integer/i128.rs +++ b/gadgets/tests/signed_integer/i128.rs @@ -14,12 +14,12 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use leo_gadgets::{arithmetic::*, Int128}; +use leo_gadgets::Int128; use snarkvm_fields::{One, Zero}; use snarkvm_gadgets::traits::utilities::{ alloc::AllocGadget, - arithmetic::{Add, Div, Mul, Neg, Pow, Sub}, + arithmetic::{Add, Div, Mul, Sub}, 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 75cac1faf0..b50943ce4e 100644 --- a/gadgets/tests/signed_integer/i16.rs +++ b/gadgets/tests/signed_integer/i16.rs @@ -14,12 +14,12 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use leo_gadgets::{arithmetic::*, Int16}; +use leo_gadgets::Int16; use snarkvm_fields::{One, Zero}; use snarkvm_gadgets::traits::utilities::{ alloc::AllocGadget, - arithmetic::{Add, Div, Mul, Neg, Pow, Sub}, + arithmetic::{Add, Div, Mul, Pow, Sub}, 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 7683c54a01..e6821b9f24 100644 --- a/gadgets/tests/signed_integer/i32.rs +++ b/gadgets/tests/signed_integer/i32.rs @@ -14,12 +14,12 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use leo_gadgets::{arithmetic::*, Int32}; +use leo_gadgets::Int32; use snarkvm_fields::{One, Zero}; use snarkvm_gadgets::traits::utilities::{ alloc::AllocGadget, - arithmetic::{Add, Div, Mul, Neg, Pow, Sub}, + arithmetic::{Add, Div, Mul, Pow, Sub}, 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 35391a9aa5..5488b0b92c 100644 --- a/gadgets/tests/signed_integer/i64.rs +++ b/gadgets/tests/signed_integer/i64.rs @@ -14,12 +14,12 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use leo_gadgets::{arithmetic::*, Int64}; +use leo_gadgets::Int64; use snarkvm_fields::{One, Zero}; use snarkvm_gadgets::traits::utilities::{ alloc::AllocGadget, - arithmetic::{Add, Div, Mul, Neg, Pow, Sub}, + arithmetic::{Add, Div, Mul, Pow, Sub}, 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 a9f41f5cff..a2480e7e31 100644 --- a/gadgets/tests/signed_integer/i8.rs +++ b/gadgets/tests/signed_integer/i8.rs @@ -14,12 +14,12 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use leo_gadgets::{arithmetic::*, Int8}; +use leo_gadgets::Int8; use snarkvm_fields::{One, Zero}; use snarkvm_gadgets::traits::utilities::{ alloc::AllocGadget, - arithmetic::{Add, Div, Mul, Neg, Pow, Sub}, + arithmetic::{Add, Div, Mul, Pow, Sub}, boolean::Boolean, }; use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem}; From a255cbd01865b810cb2ee8d322d8e35187e469af Mon Sep 17 00:00:00 2001 From: collin Date: Thu, 11 Mar 2021 17:00:15 -0800 Subject: [PATCH 19/21] remove unused dependency from benchmark --- gadgets/benches/integer_arithmetic.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gadgets/benches/integer_arithmetic.rs b/gadgets/benches/integer_arithmetic.rs index cb7d38167c..eada31b5cb 100644 --- a/gadgets/benches/integer_arithmetic.rs +++ b/gadgets/benches/integer_arithmetic.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use leo_gadgets::{arithmetic::*, Int128, Int16, Int32, Int64, Int8}; +use leo_gadgets::{Int128, Int16, Int32, Int64, Int8}; use snarkvm_gadgets::traits::utilities::{ alloc::AllocGadget, From c541183f425902efa40c2fdadfd6ae1ce8fb6940 Mon Sep 17 00:00:00 2001 From: collin Date: Thu, 11 Mar 2021 17:11:19 -0800 Subject: [PATCH 20/21] refactor to use snarkvm sign extend gadget --- gadgets/src/bits/mod.rs | 4 ++-- gadgets/src/signed_integer/arithmetic/mul.rs | 12 ++---------- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/gadgets/src/bits/mod.rs b/gadgets/src/bits/mod.rs index 664a5d31b9..ca1af5cd76 100644 --- a/gadgets/src/bits/mod.rs +++ b/gadgets/src/bits/mod.rs @@ -21,5 +21,5 @@ pub use self::adder::*; pub mod rca; pub use self::rca::*; -pub mod sign_extend; -pub use self::sign_extend::*; +// pub mod sign_extend; +// pub use self::sign_extend::*; diff --git a/gadgets/src/signed_integer/arithmetic/mul.rs b/gadgets/src/signed_integer/arithmetic/mul.rs index 3b6fe8f8d4..7717287343 100644 --- a/gadgets/src/signed_integer/arithmetic/mul.rs +++ b/gadgets/src/signed_integer/arithmetic/mul.rs @@ -14,20 +14,12 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{ - bits::{RippleCarryAdder, SignExtend}, - errors::SignedIntegerError, - Int, - Int128, - Int16, - Int32, - Int64, - Int8, -}; +use crate::{bits::RippleCarryAdder, errors::SignedIntegerError, Int, Int128, Int16, Int32, Int64, Int8}; use snarkvm_fields::{FpParameters, PrimeField}; use snarkvm_gadgets::traits::utilities::{ alloc::AllocGadget, arithmetic::Mul, + bits::SignExtend, boolean::{AllocatedBit, Boolean}, select::CondSelectGadget, }; From 8e6b9f0fdd5971284dc6ce791965bba2c4c945fa Mon Sep 17 00:00:00 2001 From: collin Date: Thu, 11 Mar 2021 17:11:59 -0800 Subject: [PATCH 21/21] remove old sign extend files --- gadgets/src/bits/mod.rs | 3 --- gadgets/src/bits/sign_extend.rs | 42 --------------------------------- 2 files changed, 45 deletions(-) delete mode 100644 gadgets/src/bits/sign_extend.rs diff --git a/gadgets/src/bits/mod.rs b/gadgets/src/bits/mod.rs index ca1af5cd76..d1898884dc 100644 --- a/gadgets/src/bits/mod.rs +++ b/gadgets/src/bits/mod.rs @@ -20,6 +20,3 @@ pub use self::adder::*; pub mod rca; pub use self::rca::*; - -// pub mod sign_extend; -// pub use self::sign_extend::*; diff --git a/gadgets/src/bits/sign_extend.rs b/gadgets/src/bits/sign_extend.rs deleted file mode 100644 index 5fd6569334..0000000000 --- a/gadgets/src/bits/sign_extend.rs +++ /dev/null @@ -1,42 +0,0 @@ -// 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 snarkvm_gadgets::traits::utilities::boolean::Boolean; - -use std::iter; - -/// Sign extends an array of bits to the desired length. -/// Expects least significant bit first -pub trait SignExtend -where - Self: std::marker::Sized, -{ - #[must_use] - fn sign_extend(bits: &[Boolean], length: usize) -> Vec; -} - -impl SignExtend for Boolean { - fn sign_extend(bits: &[Boolean], length: usize) -> Vec { - let msb = bits.last().expect("empty bit list"); - let bits_needed = length - bits.len(); - - let mut result = Vec::with_capacity(length); - result.extend_from_slice(bits); - result.extend(iter::repeat(*msb).take(bits_needed)); - - result - } -}