mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-11-27 02:24:15 +03:00
Merge pull request #773 from AleoHQ/refactor/gadget-sign-extend
Refactor sign extend gadget
This commit is contained in:
commit
1fbd337fec
@ -18,9 +18,9 @@
|
|||||||
|
|
||||||
use crate::{errors::ExpressionError, value::ConstrainedValue, GroupType};
|
use crate::{errors::ExpressionError, value::ConstrainedValue, GroupType};
|
||||||
use leo_asg::Span;
|
use leo_asg::Span;
|
||||||
use leo_gadgets::bits::ComparatorGadget;
|
|
||||||
|
|
||||||
use snarkvm_fields::PrimeField;
|
use snarkvm_fields::PrimeField;
|
||||||
|
use snarkvm_gadgets::utilities::bits::ComparatorGadget;
|
||||||
use snarkvm_r1cs::ConstraintSystem;
|
use snarkvm_r1cs::ConstraintSystem;
|
||||||
|
|
||||||
pub fn evaluate_ge<'a, F: PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
|
pub fn evaluate_ge<'a, F: PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
|
||||||
|
@ -18,9 +18,9 @@
|
|||||||
|
|
||||||
use crate::{errors::ExpressionError, value::ConstrainedValue, GroupType};
|
use crate::{errors::ExpressionError, value::ConstrainedValue, GroupType};
|
||||||
use leo_asg::Span;
|
use leo_asg::Span;
|
||||||
use leo_gadgets::bits::ComparatorGadget;
|
|
||||||
|
|
||||||
use snarkvm_fields::PrimeField;
|
use snarkvm_fields::PrimeField;
|
||||||
|
use snarkvm_gadgets::utilities::bits::ComparatorGadget;
|
||||||
use snarkvm_r1cs::ConstraintSystem;
|
use snarkvm_r1cs::ConstraintSystem;
|
||||||
|
|
||||||
pub fn evaluate_gt<'a, F: PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
|
pub fn evaluate_gt<'a, F: PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
|
||||||
|
@ -18,9 +18,9 @@
|
|||||||
|
|
||||||
use crate::{errors::ExpressionError, value::ConstrainedValue, GroupType};
|
use crate::{errors::ExpressionError, value::ConstrainedValue, GroupType};
|
||||||
use leo_asg::Span;
|
use leo_asg::Span;
|
||||||
use leo_gadgets::bits::ComparatorGadget;
|
|
||||||
|
|
||||||
use snarkvm_fields::PrimeField;
|
use snarkvm_fields::PrimeField;
|
||||||
|
use snarkvm_gadgets::utilities::bits::ComparatorGadget;
|
||||||
use snarkvm_r1cs::ConstraintSystem;
|
use snarkvm_r1cs::ConstraintSystem;
|
||||||
|
|
||||||
pub fn evaluate_le<'a, F: PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
|
pub fn evaluate_le<'a, F: PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
|
||||||
|
@ -18,9 +18,9 @@
|
|||||||
|
|
||||||
use crate::{errors::ExpressionError, value::ConstrainedValue, GroupType};
|
use crate::{errors::ExpressionError, value::ConstrainedValue, GroupType};
|
||||||
use leo_asg::Span;
|
use leo_asg::Span;
|
||||||
use leo_gadgets::bits::comparator::EvaluateLtGadget;
|
|
||||||
|
|
||||||
use snarkvm_fields::PrimeField;
|
use snarkvm_fields::PrimeField;
|
||||||
|
use snarkvm_gadgets::utilities::bits::EvaluateLtGadget;
|
||||||
use snarkvm_r1cs::ConstraintSystem;
|
use snarkvm_r1cs::ConstraintSystem;
|
||||||
|
|
||||||
pub fn evaluate_lt<'a, F: PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
|
pub fn evaluate_lt<'a, F: PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
|
||||||
|
@ -18,15 +18,13 @@
|
|||||||
use crate::{errors::IntegerError, IntegerTrait};
|
use crate::{errors::IntegerError, IntegerTrait};
|
||||||
use leo_asg::{ConstInt, IntegerType, Span};
|
use leo_asg::{ConstInt, IntegerType, Span};
|
||||||
use leo_ast::InputValue;
|
use leo_ast::InputValue;
|
||||||
use leo_gadgets::{
|
use leo_gadgets::signed_integer::*;
|
||||||
arithmetic::*,
|
|
||||||
bits::comparator::{ComparatorGadget, EvaluateLtGadget},
|
|
||||||
signed_integer::*,
|
|
||||||
};
|
|
||||||
|
|
||||||
use snarkvm_fields::{Field, PrimeField};
|
use snarkvm_fields::{Field, PrimeField};
|
||||||
use snarkvm_gadgets::traits::utilities::{
|
use snarkvm_gadgets::traits::utilities::{
|
||||||
alloc::AllocGadget,
|
alloc::AllocGadget,
|
||||||
|
arithmetic::{Add, Div, Mul, Neg, Pow, Sub},
|
||||||
|
bits::comparator::{ComparatorGadget, EvaluateLtGadget},
|
||||||
boolean::Boolean,
|
boolean::Boolean,
|
||||||
eq::{ConditionalEqGadget, EqGadget, EvaluateEqGadget},
|
eq::{ConditionalEqGadget, EqGadget, EvaluateEqGadget},
|
||||||
select::CondSelectGadget,
|
select::CondSelectGadget,
|
||||||
|
@ -24,3 +24,4 @@ function main() -> group {
|
|||||||
let hash_input: [bool; 256] = [true; 256];
|
let hash_input: [bool; 256] = [true; 256];
|
||||||
return pedersen.hash(hash_input)
|
return pedersen.hash(hash_input)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,9 +14,12 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use leo_gadgets::{arithmetic::*, Int128, Int16, Int32, Int64, Int8};
|
use leo_gadgets::{Int128, Int16, Int32, Int64, Int8};
|
||||||
|
|
||||||
use snarkvm_gadgets::traits::utilities::alloc::AllocGadget;
|
use snarkvm_gadgets::traits::utilities::{
|
||||||
|
alloc::AllocGadget,
|
||||||
|
arithmetic::{Add, Div, Mul, Sub},
|
||||||
|
};
|
||||||
use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem};
|
use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem};
|
||||||
|
|
||||||
use rand::{Rng, SeedableRng};
|
use rand::{Rng, SeedableRng};
|
||||||
|
@ -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 <https://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
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<F: Field, Rhs = Self>
|
|
||||||
where
|
|
||||||
Self: std::marker::Sized,
|
|
||||||
{
|
|
||||||
type ErrorType;
|
|
||||||
|
|
||||||
fn add<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Self, Self::ErrorType>;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Implement unsigned integers
|
|
||||||
macro_rules! add_uint_impl {
|
|
||||||
($($gadget: ident),*) => ($(
|
|
||||||
impl<F: PrimeField> Add<F> for $gadget {
|
|
||||||
type ErrorType = SynthesisError;
|
|
||||||
|
|
||||||
fn add<CS: ConstraintSystem<F>>(
|
|
||||||
&self,
|
|
||||||
cs: CS,
|
|
||||||
other: &Self
|
|
||||||
) -> Result<Self, Self::ErrorType> {
|
|
||||||
<$gadget as UInt>::addmany(cs, &[self.clone(), other.clone()])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)*)
|
|
||||||
}
|
|
||||||
|
|
||||||
add_uint_impl!(UInt8, UInt16, UInt32, UInt64, UInt128);
|
|
@ -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 <https://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
use snarkvm_fields::Field;
|
|
||||||
use snarkvm_r1cs::ConstraintSystem;
|
|
||||||
|
|
||||||
/// Returns division of `self` / `other` in the constraint system.
|
|
||||||
pub trait Div<F: Field, Rhs = Self>
|
|
||||||
where
|
|
||||||
Self: std::marker::Sized,
|
|
||||||
{
|
|
||||||
type ErrorType;
|
|
||||||
|
|
||||||
fn div<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Self, Self::ErrorType>;
|
|
||||||
}
|
|
@ -1,33 +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 <https://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
pub mod add;
|
|
||||||
pub use self::add::*;
|
|
||||||
|
|
||||||
pub mod div;
|
|
||||||
pub use self::div::*;
|
|
||||||
|
|
||||||
pub mod mul;
|
|
||||||
pub use self::mul::*;
|
|
||||||
|
|
||||||
pub mod neg;
|
|
||||||
pub use self::neg::*;
|
|
||||||
|
|
||||||
pub mod pow;
|
|
||||||
pub use self::pow::*;
|
|
||||||
|
|
||||||
pub mod sub;
|
|
||||||
pub use self::sub::*;
|
|
@ -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 <https://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
use snarkvm_fields::Field;
|
|
||||||
use snarkvm_r1cs::ConstraintSystem;
|
|
||||||
|
|
||||||
/// Returns multiplication of `self` * `other` in the constraint system.
|
|
||||||
pub trait Mul<F: Field, Rhs = Self>
|
|
||||||
where
|
|
||||||
Self: std::marker::Sized,
|
|
||||||
{
|
|
||||||
type ErrorType;
|
|
||||||
|
|
||||||
fn mul<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Self, Self::ErrorType>;
|
|
||||||
}
|
|
@ -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 <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)
|
|
||||||
}
|
|
||||||
}
|
|
@ -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 <https://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
use snarkvm_fields::Field;
|
|
||||||
use snarkvm_r1cs::ConstraintSystem;
|
|
||||||
|
|
||||||
/// Returns exponentiation of `self` ** `other` in the constraint system.
|
|
||||||
pub trait Pow<F: Field, Rhs = Self>
|
|
||||||
where
|
|
||||||
Self: std::marker::Sized,
|
|
||||||
{
|
|
||||||
type ErrorType;
|
|
||||||
|
|
||||||
fn pow<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Self, Self::ErrorType>;
|
|
||||||
}
|
|
@ -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 <https://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
use snarkvm_fields::Field;
|
|
||||||
use snarkvm_r1cs::ConstraintSystem;
|
|
||||||
|
|
||||||
/// Returns subtraction of `self` - `other` in the constraint system.
|
|
||||||
pub trait Sub<F: Field, Rhs = Self>
|
|
||||||
where
|
|
||||||
Self: std::marker::Sized,
|
|
||||||
{
|
|
||||||
type ErrorType;
|
|
||||||
|
|
||||||
fn sub<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Self, Self::ErrorType>;
|
|
||||||
}
|
|
@ -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 <https://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
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<F: Field> {
|
|
||||||
fn less_than<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Boolean, SynthesisError>;
|
|
||||||
}
|
|
||||||
|
|
||||||
// implementing `EvaluateLtGadget` will implement `ComparatorGadget`
|
|
||||||
pub trait ComparatorGadget<F: Field>
|
|
||||||
where
|
|
||||||
Self: EvaluateLtGadget<F>,
|
|
||||||
{
|
|
||||||
fn greater_than<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Boolean, SynthesisError> {
|
|
||||||
other.less_than(cs, self)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn less_than_or_equal<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Boolean, SynthesisError> {
|
|
||||||
let is_gt = self.greater_than(cs, other)?;
|
|
||||||
Ok(is_gt.not())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn greater_than_or_equal<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Boolean, SynthesisError> {
|
|
||||||
other.less_than_or_equal(cs, self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
macro_rules! uint_cmp_impl {
|
|
||||||
($($gadget: ident),*) => ($(
|
|
||||||
/* Bitwise less than comparison of two unsigned integers */
|
|
||||||
impl<F: PrimeField> EvaluateLtGadget<F> for $gadget {
|
|
||||||
fn less_than<CS: ConstraintSystem<F>>(&self, mut cs: CS, other: &Self) -> Result<Boolean, SynthesisError> {
|
|
||||||
|
|
||||||
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<F: PrimeField> ComparatorGadget<F> for $gadget {}
|
|
||||||
)*)
|
|
||||||
}
|
|
||||||
|
|
||||||
uint_cmp_impl!(UInt8, UInt16, UInt32, UInt64, UInt128);
|
|
@ -18,11 +18,5 @@
|
|||||||
pub mod adder;
|
pub mod adder;
|
||||||
pub use self::adder::*;
|
pub use self::adder::*;
|
||||||
|
|
||||||
pub mod comparator;
|
|
||||||
pub use self::comparator::*;
|
|
||||||
|
|
||||||
pub mod rca;
|
pub mod rca;
|
||||||
pub use self::rca::*;
|
pub use self::rca::*;
|
||||||
|
|
||||||
pub mod sign_extend;
|
|
||||||
pub use self::sign_extend::*;
|
|
||||||
|
@ -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 <https://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
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<Boolean>;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SignExtend for Boolean {
|
|
||||||
fn sign_extend(bits: &[Boolean], length: usize) -> Vec<Boolean> {
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
@ -17,8 +17,6 @@
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate thiserror;
|
extern crate thiserror;
|
||||||
|
|
||||||
pub mod arithmetic;
|
|
||||||
|
|
||||||
pub mod bits;
|
pub mod bits;
|
||||||
|
|
||||||
pub mod errors;
|
pub mod errors;
|
||||||
|
@ -14,21 +14,12 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use crate::{
|
use crate::{bits::RippleCarryAdder, errors::SignedIntegerError, Int, Int128, Int16, Int32, Int64, Int8};
|
||||||
arithmetic::Add,
|
|
||||||
bits::RippleCarryAdder,
|
|
||||||
errors::SignedIntegerError,
|
|
||||||
Int,
|
|
||||||
Int128,
|
|
||||||
Int16,
|
|
||||||
Int32,
|
|
||||||
Int64,
|
|
||||||
Int8,
|
|
||||||
};
|
|
||||||
|
|
||||||
use snarkvm_fields::{fp_parameters::FpParameters, PrimeField};
|
use snarkvm_fields::{fp_parameters::FpParameters, PrimeField};
|
||||||
use snarkvm_gadgets::traits::utilities::{
|
use snarkvm_gadgets::traits::utilities::{
|
||||||
alloc::AllocGadget,
|
alloc::AllocGadget,
|
||||||
|
arithmetic::Add,
|
||||||
boolean::{AllocatedBit, Boolean},
|
boolean::{AllocatedBit, Boolean},
|
||||||
};
|
};
|
||||||
use snarkvm_r1cs::{Assignment, ConstraintSystem, LinearCombination};
|
use snarkvm_r1cs::{Assignment, ConstraintSystem, LinearCombination};
|
||||||
|
@ -14,20 +14,12 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use crate::{
|
use crate::{errors::SignedIntegerError, Int, Int128, Int16, Int32, Int64, Int8};
|
||||||
arithmetic::{Add, Div, Neg, Sub},
|
|
||||||
bits::ComparatorGadget,
|
|
||||||
errors::SignedIntegerError,
|
|
||||||
Int,
|
|
||||||
Int128,
|
|
||||||
Int16,
|
|
||||||
Int32,
|
|
||||||
Int64,
|
|
||||||
Int8,
|
|
||||||
};
|
|
||||||
use snarkvm_fields::PrimeField;
|
use snarkvm_fields::PrimeField;
|
||||||
use snarkvm_gadgets::traits::utilities::{
|
use snarkvm_gadgets::traits::utilities::{
|
||||||
alloc::AllocGadget,
|
alloc::AllocGadget,
|
||||||
|
arithmetic::{Add, Div, Neg, Sub},
|
||||||
|
bits::ComparatorGadget,
|
||||||
boolean::{AllocatedBit, Boolean},
|
boolean::{AllocatedBit, Boolean},
|
||||||
eq::EvaluateEqGadget,
|
eq::EvaluateEqGadget,
|
||||||
select::CondSelectGadget,
|
select::CondSelectGadget,
|
||||||
|
@ -14,20 +14,12 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use crate::{
|
use crate::{bits::RippleCarryAdder, errors::SignedIntegerError, Int, Int128, Int16, Int32, Int64, Int8};
|
||||||
arithmetic::Mul,
|
|
||||||
bits::{RippleCarryAdder, SignExtend},
|
|
||||||
errors::SignedIntegerError,
|
|
||||||
Int,
|
|
||||||
Int128,
|
|
||||||
Int16,
|
|
||||||
Int32,
|
|
||||||
Int64,
|
|
||||||
Int8,
|
|
||||||
};
|
|
||||||
use snarkvm_fields::{FpParameters, PrimeField};
|
use snarkvm_fields::{FpParameters, PrimeField};
|
||||||
use snarkvm_gadgets::traits::utilities::{
|
use snarkvm_gadgets::traits::utilities::{
|
||||||
alloc::AllocGadget,
|
alloc::AllocGadget,
|
||||||
|
arithmetic::Mul,
|
||||||
|
bits::SignExtend,
|
||||||
boolean::{AllocatedBit, Boolean},
|
boolean::{AllocatedBit, Boolean},
|
||||||
select::CondSelectGadget,
|
select::CondSelectGadget,
|
||||||
};
|
};
|
||||||
|
@ -14,9 +14,10 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use crate::{arithmetic::Neg, errors::SignedIntegerError, signed_integer::*};
|
use crate::{errors::SignedIntegerError, signed_integer::*};
|
||||||
|
|
||||||
use snarkvm_fields::PrimeField;
|
use snarkvm_fields::PrimeField;
|
||||||
|
use snarkvm_gadgets::utilities::arithmetic::Neg;
|
||||||
use snarkvm_r1cs::ConstraintSystem;
|
use snarkvm_r1cs::ConstraintSystem;
|
||||||
|
|
||||||
macro_rules! neg_int_impl {
|
macro_rules! neg_int_impl {
|
||||||
|
@ -14,19 +14,15 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use crate::{
|
use crate::{errors::SignedIntegerError, Int, Int128, Int16, Int32, Int64, Int8};
|
||||||
arithmetic::{Mul, Pow},
|
|
||||||
errors::SignedIntegerError,
|
|
||||||
Int,
|
|
||||||
Int128,
|
|
||||||
Int16,
|
|
||||||
Int32,
|
|
||||||
Int64,
|
|
||||||
Int8,
|
|
||||||
};
|
|
||||||
|
|
||||||
use snarkvm_fields::PrimeField;
|
use snarkvm_fields::PrimeField;
|
||||||
use snarkvm_gadgets::traits::utilities::{alloc::AllocGadget, boolean::Boolean, select::CondSelectGadget};
|
use snarkvm_gadgets::traits::utilities::{
|
||||||
|
alloc::AllocGadget,
|
||||||
|
arithmetic::{Mul, Pow},
|
||||||
|
boolean::Boolean,
|
||||||
|
select::CondSelectGadget,
|
||||||
|
};
|
||||||
use snarkvm_r1cs::ConstraintSystem;
|
use snarkvm_r1cs::ConstraintSystem;
|
||||||
|
|
||||||
macro_rules! pow_int_impl {
|
macro_rules! pow_int_impl {
|
||||||
|
@ -14,16 +14,9 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use crate::{
|
use crate::{errors::SignedIntegerError, Int128, Int16, Int32, Int64, Int8};
|
||||||
arithmetic::{Add, Neg, Sub},
|
|
||||||
errors::SignedIntegerError,
|
|
||||||
Int128,
|
|
||||||
Int16,
|
|
||||||
Int32,
|
|
||||||
Int64,
|
|
||||||
Int8,
|
|
||||||
};
|
|
||||||
use snarkvm_fields::PrimeField;
|
use snarkvm_fields::PrimeField;
|
||||||
|
use snarkvm_gadgets::traits::utilities::arithmetic::{Add, Neg, Sub};
|
||||||
use snarkvm_r1cs::ConstraintSystem;
|
use snarkvm_r1cs::ConstraintSystem;
|
||||||
|
|
||||||
macro_rules! sub_int_impl {
|
macro_rules! sub_int_impl {
|
||||||
|
@ -14,17 +14,14 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use crate::{
|
use crate::{Int128, Int16, Int32, Int64, Int8};
|
||||||
bits::{ComparatorGadget, EvaluateLtGadget},
|
|
||||||
Int128,
|
|
||||||
Int16,
|
|
||||||
Int32,
|
|
||||||
Int64,
|
|
||||||
Int8,
|
|
||||||
};
|
|
||||||
|
|
||||||
use snarkvm_fields::PrimeField;
|
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 snarkvm_r1cs::{ConstraintSystem, SynthesisError};
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
|
@ -14,10 +14,14 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use leo_gadgets::{arithmetic::*, Int128};
|
use leo_gadgets::Int128;
|
||||||
|
|
||||||
use snarkvm_fields::{One, Zero};
|
use snarkvm_fields::{One, Zero};
|
||||||
use snarkvm_gadgets::traits::utilities::{alloc::AllocGadget, boolean::Boolean};
|
use snarkvm_gadgets::traits::utilities::{
|
||||||
|
alloc::AllocGadget,
|
||||||
|
arithmetic::{Add, Div, Mul, Sub},
|
||||||
|
boolean::Boolean,
|
||||||
|
};
|
||||||
use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem};
|
use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem};
|
||||||
|
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
@ -14,10 +14,14 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use leo_gadgets::{arithmetic::*, Int16};
|
use leo_gadgets::Int16;
|
||||||
|
|
||||||
use snarkvm_fields::{One, Zero};
|
use snarkvm_fields::{One, Zero};
|
||||||
use snarkvm_gadgets::traits::utilities::{alloc::AllocGadget, boolean::Boolean};
|
use snarkvm_gadgets::traits::utilities::{
|
||||||
|
alloc::AllocGadget,
|
||||||
|
arithmetic::{Add, Div, Mul, Pow, Sub},
|
||||||
|
boolean::Boolean,
|
||||||
|
};
|
||||||
use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem};
|
use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem};
|
||||||
|
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
@ -14,10 +14,14 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use leo_gadgets::{arithmetic::*, Int32};
|
use leo_gadgets::Int32;
|
||||||
|
|
||||||
use snarkvm_fields::{One, Zero};
|
use snarkvm_fields::{One, Zero};
|
||||||
use snarkvm_gadgets::traits::utilities::{alloc::AllocGadget, boolean::Boolean};
|
use snarkvm_gadgets::traits::utilities::{
|
||||||
|
alloc::AllocGadget,
|
||||||
|
arithmetic::{Add, Div, Mul, Pow, Sub},
|
||||||
|
boolean::Boolean,
|
||||||
|
};
|
||||||
use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem};
|
use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem};
|
||||||
|
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
@ -14,10 +14,14 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use leo_gadgets::{arithmetic::*, Int64};
|
use leo_gadgets::Int64;
|
||||||
|
|
||||||
use snarkvm_fields::{One, Zero};
|
use snarkvm_fields::{One, Zero};
|
||||||
use snarkvm_gadgets::traits::utilities::{alloc::AllocGadget, boolean::Boolean};
|
use snarkvm_gadgets::traits::utilities::{
|
||||||
|
alloc::AllocGadget,
|
||||||
|
arithmetic::{Add, Div, Mul, Pow, Sub},
|
||||||
|
boolean::Boolean,
|
||||||
|
};
|
||||||
use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem};
|
use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem};
|
||||||
|
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
@ -14,10 +14,14 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use leo_gadgets::{arithmetic::*, Int8};
|
use leo_gadgets::Int8;
|
||||||
|
|
||||||
use snarkvm_fields::{One, Zero};
|
use snarkvm_fields::{One, Zero};
|
||||||
use snarkvm_gadgets::traits::utilities::{alloc::AllocGadget, boolean::Boolean};
|
use snarkvm_gadgets::traits::utilities::{
|
||||||
|
alloc::AllocGadget,
|
||||||
|
arithmetic::{Add, Div, Mul, Pow, Sub},
|
||||||
|
boolean::Boolean,
|
||||||
|
};
|
||||||
use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem};
|
use snarkvm_r1cs::{ConstraintSystem, Fr, TestConstraintSystem};
|
||||||
|
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
Loading…
Reference in New Issue
Block a user