mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-12-25 19:22:01 +03:00
add skeleton macros for arithmetic
This commit is contained in:
parent
192e6ba2dd
commit
d4c8c4252d
23
gadgets/src/signed_integer/arithmetic/add.rs
Normal file
23
gadgets/src/signed_integer/arithmetic/add.rs
Normal 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);
|
23
gadgets/src/signed_integer/arithmetic/div.rs
Normal file
23
gadgets/src/signed_integer/arithmetic/div.rs
Normal 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);
|
15
gadgets/src/signed_integer/arithmetic/mod.rs
Normal file
15
gadgets/src/signed_integer/arithmetic/mod.rs
Normal 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::*;
|
23
gadgets/src/signed_integer/arithmetic/mul.rs
Normal file
23
gadgets/src/signed_integer/arithmetic/mul.rs
Normal 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);
|
23
gadgets/src/signed_integer/arithmetic/pow.rs
Normal file
23
gadgets/src/signed_integer/arithmetic/pow.rs
Normal 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);
|
23
gadgets/src/signed_integer/arithmetic/sub.rs
Normal file
23
gadgets/src/signed_integer/arithmetic/sub.rs
Normal 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);
|
@ -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);
|
@ -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::*;
|
||||
|
@ -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>;
|
||||
}
|
15
gadgets/tests/signed_integer/arithmetic/mod.rs
Normal file
15
gadgets/tests/signed_integer/arithmetic/mod.rs
Normal 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::*;
|
1
gadgets/tests/signed_integer/arithmetic/test_sub.rs
Normal file
1
gadgets/tests/signed_integer/arithmetic/test_sub.rs
Normal file
@ -0,0 +1 @@
|
||||
|
@ -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);
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
pub mod i128;
|
||||
pub mod i16;
|
||||
pub mod i32;
|
||||
pub mod i64;
|
||||
pub mod i8;
|
@ -1,2 +0,0 @@
|
||||
#[macro_use]
|
||||
pub mod test_constant;
|
@ -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::*;
|
||||
|
Loading…
Reference in New Issue
Block a user