fix: align with snarkVM's Integer changes

Signed-off-by: ljedrz <ljedrz@gmail.com>
This commit is contained in:
ljedrz 2021-03-22 14:25:50 +01:00
parent e31bace4cb
commit 74f02411e8
5 changed files with 27 additions and 51 deletions

View File

@ -16,7 +16,7 @@
use leo_ast::{FormattedError, LeoError, Span};
use snarkvm_gadgets::errors::SignedIntegerError;
use snarkvm_gadgets::errors::{SignedIntegerError, UnsignedIntegerError};
use snarkvm_r1cs::SynthesisError;
#[derive(Debug, Error)]
@ -38,6 +38,15 @@ impl IntegerError {
Self::new_from_span(message, span)
}
pub fn unsigned(error: UnsignedIntegerError, span: &Span) -> Self {
let message = format!(
"integer operation failed due to the unsigned integer error `{:?}`",
error
);
Self::new_from_span(message, span)
}
pub fn synthesis(error: SynthesisError, span: &Span) -> Self {
let message = format!("integer operation failed due to the synthesis error `{}`", error);

View File

@ -23,6 +23,7 @@ use crate::{
GroupType,
IndicatorAndConstrainedValue,
Integer,
IntegerTrait,
StatementResult,
};
use leo_asg::IterationStatement;

View File

@ -14,7 +14,7 @@
// 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::{errors::AddressError, ConstrainedValue, GroupType};
use crate::{errors::AddressError, ConstrainedValue, GroupType, IntegerTrait};
use leo_ast::{InputValue, Span};
use snarkvm_dpc::{account::AccountAddress, base_dpc::instantiated::Components};
@ -24,7 +24,7 @@ use snarkvm_gadgets::traits::utilities::{
boolean::Boolean,
eq::{ConditionalEqGadget, EqGadget, EvaluateEqGadget},
select::CondSelectGadget,
uint::{UInt, UInt8},
uint::UInt8,
};
use snarkvm_r1cs::{Assignment, ConstraintSystem, SynthesisError};
use snarkvm_utilities::ToBytes;

View File

@ -28,7 +28,7 @@ use snarkvm_gadgets::traits::utilities::{
eq::{ConditionalEqGadget, EqGadget, EvaluateEqGadget},
int::{Int128, Int16, Int32, Int64, Int8},
select::CondSelectGadget,
uint::*,
uint::{Sub as UIntSub, *},
};
use snarkvm_r1cs::{ConstraintSystem, SynthesisError};
use std::fmt;
@ -83,7 +83,7 @@ impl Integer {
pub fn get_bits(&self) -> Vec<Boolean> {
let integer = self;
match_integer!(integer => integer.get_bits())
match_integer!(integer => integer.to_bits_le())
}
// pub fn get_bits_typed(&self) -> (Vec<Boolean>, IntegerType) {

View File

@ -14,41 +14,7 @@
// 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,
int::{Int128, Int16, Int32, Int64, Int8},
uint::{UInt128, UInt16, UInt32, UInt64, UInt8},
};
use std::{convert::TryInto, fmt::Debug};
pub trait IntegerTrait: Sized + Clone + Debug {
fn get_value(&self) -> Option<String>;
fn get_index(&self) -> Option<usize>;
fn get_bits(&self) -> Vec<Boolean>;
}
macro_rules! integer_trait_impl {
($($gadget: ident)*) => ($(
impl IntegerTrait for $gadget {
fn get_value(&self) -> Option<String> {
self.value.map(|num| num.to_string())
}
fn get_index(&self) -> Option<usize> {
self.value.map(|num| num.try_into().ok()).flatten()
}
fn get_bits(&self) -> Vec<Boolean> {
self.bits.clone()
}
}
)*)
}
integer_trait_impl!(UInt8 UInt16 UInt32 UInt64 UInt128 Int8 Int16 Int32 Int64 Int128);
pub use snarkvm_gadgets::traits::utilities::integer::Integer as IntegerTrait;
/// Useful macros to avoid duplicating `match` constructions.
#[macro_export]
@ -125,19 +91,19 @@ macro_rules! match_integers_span {
(($a: ident, $b: ident), $span: ident => $expression:expr) => {
match ($a, $b) {
(Integer::U8($a), Integer::U8($b)) => {
Some(Integer::U8($expression.map_err(|e| IntegerError::synthesis(e, $span))?))
Some(Integer::U8($expression.map_err(|e| IntegerError::unsigned(e, $span))?))
}
(Integer::U16($a), Integer::U16($b)) => {
Some(Integer::U16($expression.map_err(|e| IntegerError::unsigned(e, $span))?))
}
(Integer::U32($a), Integer::U32($b)) => {
Some(Integer::U32($expression.map_err(|e| IntegerError::unsigned(e, $span))?))
}
(Integer::U64($a), Integer::U64($b)) => {
Some(Integer::U64($expression.map_err(|e| IntegerError::unsigned(e, $span))?))
}
(Integer::U16($a), Integer::U16($b)) => Some(Integer::U16(
$expression.map_err(|e| IntegerError::synthesis(e, $span))?,
)),
(Integer::U32($a), Integer::U32($b)) => Some(Integer::U32(
$expression.map_err(|e| IntegerError::synthesis(e, $span))?,
)),
(Integer::U64($a), Integer::U64($b)) => Some(Integer::U64(
$expression.map_err(|e| IntegerError::synthesis(e, $span))?,
)),
(Integer::U128($a), Integer::U128($b)) => Some(Integer::U128(
$expression.map_err(|e| IntegerError::synthesis(e, $span))?,
$expression.map_err(|e| IntegerError::unsigned(e, $span))?,
)),
(Integer::I8($a), Integer::I8($b)) => {