From 74f02411e843ff70ea293dc7b953823a43187581 Mon Sep 17 00:00:00 2001 From: ljedrz Date: Mon, 22 Mar 2021 14:25:50 +0100 Subject: [PATCH] fix: align with snarkVM's Integer changes Signed-off-by: ljedrz --- compiler/src/errors/value/integer.rs | 11 +++- compiler/src/statement/iteration/iteration.rs | 1 + compiler/src/value/address/address.rs | 4 +- compiler/src/value/integer/integer.rs | 4 +- compiler/src/value/integer/macros.rs | 58 ++++--------------- 5 files changed, 27 insertions(+), 51 deletions(-) diff --git a/compiler/src/errors/value/integer.rs b/compiler/src/errors/value/integer.rs index 457575b1dc..10a9f80bef 100644 --- a/compiler/src/errors/value/integer.rs +++ b/compiler/src/errors/value/integer.rs @@ -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); diff --git a/compiler/src/statement/iteration/iteration.rs b/compiler/src/statement/iteration/iteration.rs index fc01e33d36..684f8f7dcb 100644 --- a/compiler/src/statement/iteration/iteration.rs +++ b/compiler/src/statement/iteration/iteration.rs @@ -23,6 +23,7 @@ use crate::{ GroupType, IndicatorAndConstrainedValue, Integer, + IntegerTrait, StatementResult, }; use leo_asg::IterationStatement; diff --git a/compiler/src/value/address/address.rs b/compiler/src/value/address/address.rs index fcd606f81b..0c9393ff68 100644 --- a/compiler/src/value/address/address.rs +++ b/compiler/src/value/address/address.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 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; diff --git a/compiler/src/value/integer/integer.rs b/compiler/src/value/integer/integer.rs index 06a2001abd..808c167d53 100644 --- a/compiler/src/value/integer/integer.rs +++ b/compiler/src/value/integer/integer.rs @@ -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 { let integer = self; - match_integer!(integer => integer.get_bits()) + match_integer!(integer => integer.to_bits_le()) } // pub fn get_bits_typed(&self) -> (Vec, IntegerType) { diff --git a/compiler/src/value/integer/macros.rs b/compiler/src/value/integer/macros.rs index 1ed608b8d5..1d5d7f8f9b 100644 --- a/compiler/src/value/integer/macros.rs +++ b/compiler/src/value/integer/macros.rs @@ -14,41 +14,7 @@ // 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, - 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; - - fn get_index(&self) -> Option; - - fn get_bits(&self) -> Vec; -} - -macro_rules! integer_trait_impl { - ($($gadget: ident)*) => ($( - impl IntegerTrait for $gadget { - fn get_value(&self) -> Option { - self.value.map(|num| num.to_string()) - } - - fn get_index(&self) -> Option { - self.value.map(|num| num.try_into().ok()).flatten() - } - - fn get_bits(&self) -> Vec { - 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)) => {