mirror of
https://github.com/AleoHQ/leo.git
synced 2024-12-19 07:32:26 +03:00
refactor to use snarkvm neg gadget
This commit is contained in:
parent
c02b281ed0
commit
faacfbc97e
@ -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,
|
||||
|
@ -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};
|
||||
|
||||
|
@ -14,8 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
pub mod neg;
|
||||
pub use self::neg::*;
|
||||
// pub mod neg;
|
||||
// pub use self::neg::*;
|
||||
|
||||
pub mod pow;
|
||||
pub use self::pow::*;
|
||||
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
|
||||
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<F: Field>
|
||||
where
|
||||
Self: std::marker::Sized,
|
||||
{
|
||||
type ErrorType;
|
||||
|
||||
fn neg<CS: ConstraintSystem<F>>(&self, cs: CS) -> Result<Self, Self::ErrorType>;
|
||||
}
|
||||
|
||||
impl<F: Field> Neg<F> for Vec<Boolean> {
|
||||
type ErrorType = SynthesisError;
|
||||
|
||||
fn neg<CS: ConstraintSystem<F>>(&self, mut cs: CS) -> Result<Self, SynthesisError> {
|
||||
// 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 <https://www.gnu.org/licenses/>.
|
||||
//
|
||||
// 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<F: Field>
|
||||
// where
|
||||
// Self: std::marker::Sized,
|
||||
// {
|
||||
// type ErrorType;
|
||||
//
|
||||
// fn neg<CS: ConstraintSystem<F>>(&self, cs: CS) -> Result<Self, Self::ErrorType>;
|
||||
// }
|
||||
//
|
||||
// impl<F: Field> Neg<F> for Vec<Boolean> {
|
||||
// type ErrorType = SynthesisError;
|
||||
//
|
||||
// fn neg<CS: ConstraintSystem<F>>(&self, mut cs: CS) -> Result<Self, SynthesisError> {
|
||||
// // 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)
|
||||
// }
|
||||
// }
|
||||
|
@ -15,7 +15,7 @@
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
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,
|
||||
|
@ -14,9 +14,10 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
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 {
|
||||
|
@ -14,17 +14,9 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
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 {
|
||||
|
@ -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};
|
||||
|
@ -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};
|
||||
|
@ -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};
|
||||
|
@ -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};
|
||||
|
@ -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};
|
||||
|
Loading…
Reference in New Issue
Block a user