add skeleton macros for arithmetic

This commit is contained in:
collin 2020-07-10 00:33:54 -07:00
parent 192e6ba2dd
commit d4c8c4252d
20 changed files with 166 additions and 69 deletions

View File

@ -0,0 +1,23 @@
use crate::{errors::IntegerError, Int128, Int16, Int32, Int64, Int8};
use snarkos_models::{curves::PrimeField, gadgets::r1cs::ConstraintSystem};
/// Implements modular addition for a signed integer gadget
pub trait Add<Rhs = Self>
where
Self: std::marker::Sized,
{
#[must_use]
fn add<F: PrimeField, CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<(), IntegerError>;
}
macro_rules! add_int_impl {
($($t:ty)*) => ($(
impl Add for $t {
fn add<F: PrimeField, CS: ConstraintSystem<F>>(&self, _cs: CS, _other: &Self) -> Result<(), IntegerError> {
Ok(())
}
}
)*)
}
add_int_impl!(Int8 Int16 Int32 Int64 Int128);

View File

@ -0,0 +1,23 @@
use crate::{errors::IntegerError, Int128, Int16, Int32, Int64, Int8};
use snarkos_models::{curves::PrimeField, gadgets::r1cs::ConstraintSystem};
/// Implements modular division for a signed integer gadget
pub trait Div<Rhs = Self>
where
Self: std::marker::Sized,
{
#[must_use]
fn div<F: PrimeField, CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<(), IntegerError>;
}
macro_rules! div_int_impl {
($($t:ty)*) => ($(
impl Div for $t {
fn div<F: PrimeField, CS: ConstraintSystem<F>>(&self, _cs: CS, _other: &Self) -> Result<(), IntegerError> {
Ok(())
}
}
)*)
}
div_int_impl!(Int8 Int16 Int32 Int64 Int128);

View File

@ -0,0 +1,15 @@
#[macro_use]
pub mod add;
pub use self::add::*;
pub mod div;
pub use self::div::*;
pub mod mul;
pub use self::mul::*;
pub mod pow;
pub use self::pow::*;
pub mod sub;
pub use self::sub::*;

View File

@ -0,0 +1,23 @@
use crate::{errors::IntegerError, Int128, Int16, Int32, Int64, Int8};
use snarkos_models::{curves::PrimeField, gadgets::r1cs::ConstraintSystem};
/// Implements modular multiplication for a signed integer gadget
pub trait Mul<Rhs = Self>
where
Self: std::marker::Sized,
{
#[must_use]
fn mul<F: PrimeField, CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<(), IntegerError>;
}
macro_rules! mul_int_impl {
($($t:ty)*) => ($(
impl Mul for $t {
fn mul<F: PrimeField, CS: ConstraintSystem<F>>(&self, _cs: CS, _other: &Self) -> Result<(), IntegerError> {
Ok(())
}
}
)*)
}
mul_int_impl!(Int8 Int16 Int32 Int64 Int128);

View File

@ -0,0 +1,23 @@
use crate::{errors::IntegerError, Int128, Int16, Int32, Int64, Int8};
use snarkos_models::{curves::PrimeField, gadgets::r1cs::ConstraintSystem};
/// Implements modular exponentiation for a signed integer gadget
pub trait Pow<Rhs = Self>
where
Self: std::marker::Sized,
{
#[must_use]
fn pow<F: PrimeField, CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<(), IntegerError>;
}
macro_rules! pow_int_impl {
($($t:ty)*) => ($(
impl Pow for $t {
fn pow<F: PrimeField, CS: ConstraintSystem<F>>(&self, _cs: CS, _other: &Self) -> Result<(), IntegerError> {
Ok(())
}
}
)*)
}
pow_int_impl!(Int8 Int16 Int32 Int64 Int128);

View File

@ -0,0 +1,23 @@
use crate::{errors::IntegerError, Int128, Int16, Int32, Int64, Int8};
use snarkos_models::{curves::PrimeField, gadgets::r1cs::ConstraintSystem};
/// Implements modular subtraction for a signed integer gadget
pub trait Sub<Rhs = Self>
where
Self: std::marker::Sized,
{
#[must_use]
fn sub<F: PrimeField, CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<(), IntegerError>;
}
macro_rules! sub_int_impl {
($($t:ty)*) => ($(
impl Sub for $t {
fn sub<F: PrimeField, CS: ConstraintSystem<F>>(&self, _cs: CS, _other: &Self) -> Result<(), IntegerError> {
Ok(())
}
}
)*)
}
sub_int_impl!(Int8 Int16 Int32 Int64 Int128);

View File

@ -1,3 +1,8 @@
use snarkos_models::gadgets::utilities::boolean::Boolean;
use std::fmt::Debug;
/// Implements the base struct for a signed integer gadget
macro_rules! int_impl {
($name: ident, $_type: ty, $size: expr) => {
#[derive(Clone, Debug)]
@ -31,3 +36,9 @@ macro_rules! int_impl {
}
};
}
int_impl!(Int8, i8, 8);
int_impl!(Int16, i16, 16);
int_impl!(Int32, i32, 32);
int_impl!(Int64, i64, 64);
int_impl!(Int128, i128, 128);

View File

@ -1,5 +1,7 @@
#[macro_use]
mod macros;
pub mod signed_integer;
pub use self::signed_integer::*;
pub mod arithmetic;
pub use self::arithmetic::*;
pub mod int_impl;
pub use self::int_impl::*;

View File

@ -1,42 +0,0 @@
use crate::errors::IntegerError;
use snarkos_models::{
curves::PrimeField,
gadgets::{
r1cs::ConstraintSystem,
utilities::boolean::{AllocatedBit, Boolean},
},
};
use std::{cmp::Ordering, fmt::Debug};
int_impl!(Int8, i8, 8);
int_impl!(Int16, i16, 16);
int_impl!(Int32, i32, 32);
int_impl!(Int64, i64, 64);
int_impl!(Int128, i128, 128);
/// A signed two's complement integer object
pub trait Int: Debug + Clone + PartialOrd + Eq + PartialEq {
/// Returns true if all bits in this `Int` are constant
fn is_constant(&self) -> bool;
/// Returns true if both `Int` objects have constant bits
fn result_is_constant(first: &Self, second: &Self) -> bool {
first.is_constant() && second.is_constant()
}
/// Add two `Int` objects
fn add<F: PrimeField, CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Self, IntegerError>;
/// Subtract two `Int` objects
fn sub<F: PrimeField, CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Self, IntegerError>;
/// Multiply two `Int` objects
fn mul<F: PrimeField, CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Self, IntegerError>;
/// Divide two `Int` objects
fn div<F: PrimeField, CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Self, IntegerError>;
/// Exponentiation between two `Int` objects
fn pow<F: PrimeField, CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Self, IntegerError>;
}

View File

@ -0,0 +1,15 @@
#[macro_use]
pub mod test_add;
pub use self::test_add::*;
pub mod test_div;
pub use self::test_div::*;
pub mod test_mul;
pub use self::test_mul::*;
pub mod test_pow;
pub use self::test_pow::*;
pub mod test_sub;
pub use self::test_sub::*;

View File

@ -0,0 +1 @@

View File

@ -1,14 +0,0 @@
use leo_gadgets::signed_integer::Int8;
#[test]
fn test_i8() {
let i8 = Int8::constant(-1i8);
println!("{:?}", i8.value);
println!("{:?}", i8.bits);
}
#[test]
fn test_constant() {
test_constant!(i8, Int8);
}

View File

@ -1,5 +0,0 @@
pub mod i128;
pub mod i16;
pub mod i32;
pub mod i64;
pub mod i8;

View File

@ -1,2 +0,0 @@
#[macro_use]
pub mod test_constant;

View File

@ -1,5 +1,6 @@
#[macro_use]
pub mod macros;
pub mod arithmetic;
pub use self::arithmetic::*;
pub mod integers;
pub use self::integers::*;
pub mod test_constant;
pub use self::test_constant::*;