@ -1,141 +1,72 @@ |
|||
use crate::Vec;
|
|||
use algebra::Field;
|
|||
use core::borrow::Borrow;
|
|||
use r1cs_core::{ConstraintSystem, SynthesisError};
|
|||
use r1cs_core::{Namespace, SynthesisError};
|
|||
|
|||
pub trait AllocGadget<V, ConstraintF: Field>
|
|||
where
|
|||
Self: Sized,
|
|||
V: ?Sized,
|
|||
{
|
|||
fn alloc_constant<T, CS: ConstraintSystem<ConstraintF>>(
|
|||
cs: CS,
|
|||
t: T,
|
|||
) -> Result<Self, SynthesisError>
|
|||
where
|
|||
T: Borrow<V>;
|
|||
|
|||
fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(cs: CS, f: F) -> Result<Self, SynthesisError>
|
|||
where
|
|||
F: FnOnce() -> Result<T, SynthesisError>,
|
|||
T: Borrow<V>;
|
|||
|
|||
fn alloc_checked<F, T, CS: ConstraintSystem<ConstraintF>>(
|
|||
cs: CS,
|
|||
f: F,
|
|||
) -> Result<Self, SynthesisError>
|
|||
where
|
|||
F: FnOnce() -> Result<T, SynthesisError>,
|
|||
T: Borrow<V>,
|
|||
{
|
|||
Self::alloc(cs, f)
|
|||
}
|
|||
|
|||
fn alloc_input<F, T, CS: ConstraintSystem<ConstraintF>>(
|
|||
cs: CS,
|
|||
f: F,
|
|||
) -> Result<Self, SynthesisError>
|
|||
where
|
|||
F: FnOnce() -> Result<T, SynthesisError>,
|
|||
T: Borrow<V>;
|
|||
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Copy, Clone)]
|
|||
pub enum AllocationMode {
|
|||
Constant = 0,
|
|||
Input = 1,
|
|||
Witness = 2,
|
|||
}
|
|||
|
|||
fn alloc_input_checked<F, T, CS: ConstraintSystem<ConstraintF>>(
|
|||
cs: CS,
|
|||
f: F,
|
|||
) -> Result<Self, SynthesisError>
|
|||
where
|
|||
F: FnOnce() -> Result<T, SynthesisError>,
|
|||
T: Borrow<V>,
|
|||
{
|
|||
Self::alloc_input(cs, f)
|
|||
impl AllocationMode {
|
|||
// Outputs the maximum according to the relation `Constant < Input < Witness`.
|
|||
pub fn max(&self, other: Self) -> Self {
|
|||
use AllocationMode::*;
|
|||
match (self, other) {
|
|||
(Constant, _) => other,
|
|||
(Input, Constant) => *self,
|
|||
(Input, _) => other,
|
|||
(Witness, _) => *self,
|
|||
}
|
|||
}
|
|||
}
|
|||
|
|||
impl<I, ConstraintF: Field, A: AllocGadget<I, ConstraintF>> AllocGadget<[I], ConstraintF>
|
|||
for Vec<A>
|
|||
pub trait AllocVar<V, F: Field>
|
|||
where
|
|||
Self: Sized,
|
|||
V: ?Sized,
|
|||
{
|
|||
#[inline]
|
|||
fn alloc_constant<T, CS: ConstraintSystem<ConstraintF>>(
|
|||
mut cs: CS,
|
|||
t: T,
|
|||
) -> Result<Self, SynthesisError>
|
|||
where
|
|||
T: Borrow<[I]>,
|
|||
{
|
|||
let mut vec = Vec::new();
|
|||
for (i, value) in t.borrow().iter().enumerate() {
|
|||
vec.push(A::alloc_constant(cs.ns(|| format!("value_{}", i)), value)?);
|
|||
}
|
|||
Ok(vec)
|
|||
}
|
|||
fn new_variable<T: Borrow<V>>(
|
|||
cs: impl Into<Namespace<F>>,
|
|||
f: impl FnOnce() -> Result<T, SynthesisError>,
|
|||
mode: AllocationMode,
|
|||
) -> Result<Self, SynthesisError>;
|
|||
|
|||
fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(
|
|||
mut cs: CS,
|
|||
f: F,
|
|||
) -> Result<Self, SynthesisError>
|
|||
where
|
|||
F: FnOnce() -> Result<T, SynthesisError>,
|
|||
T: Borrow<[I]>,
|
|||
{
|
|||
let mut vec = Vec::new();
|
|||
for (i, value) in f()?.borrow().iter().enumerate() {
|
|||
vec.push(A::alloc(&mut cs.ns(|| format!("value_{}", i)), || {
|
|||
Ok(value)
|
|||
})?);
|
|||
}
|
|||
Ok(vec)
|
|||
fn new_constant(
|
|||
cs: impl Into<Namespace<F>>,
|
|||
t: impl Borrow<V>,
|
|||
) -> Result<Self, SynthesisError> {
|
|||
Self::new_variable(cs, || Ok(t), AllocationMode::Constant)
|
|||
}
|
|||
|
|||
fn alloc_input<F, T, CS: ConstraintSystem<ConstraintF>>(
|
|||
mut cs: CS,
|
|||
f: F,
|
|||
) -> Result<Self, SynthesisError>
|
|||
where
|
|||
F: FnOnce() -> Result<T, SynthesisError>,
|
|||
T: Borrow<[I]>,
|
|||
{
|
|||
let mut vec = Vec::new();
|
|||
for (i, value) in f()?.borrow().iter().enumerate() {
|
|||
vec.push(A::alloc_input(
|
|||
&mut cs.ns(|| format!("value_{}", i)),
|
|||
|| Ok(value),
|
|||
)?);
|
|||
}
|
|||
Ok(vec)
|
|||
fn new_input<T: Borrow<V>>(
|
|||
cs: impl Into<Namespace<F>>,
|
|||
f: impl FnOnce() -> Result<T, SynthesisError>,
|
|||
) -> Result<Self, SynthesisError> {
|
|||
Self::new_variable(cs, f, AllocationMode::Input)
|
|||
}
|
|||
|
|||
fn alloc_checked<F, T, CS: ConstraintSystem<ConstraintF>>(
|
|||
mut cs: CS,
|
|||
f: F,
|
|||
) -> Result<Self, SynthesisError>
|
|||
where
|
|||
F: FnOnce() -> Result<T, SynthesisError>,
|
|||
T: Borrow<[I]>,
|
|||
{
|
|||
let mut vec = Vec::new();
|
|||
for (i, value) in f()?.borrow().iter().enumerate() {
|
|||
vec.push(A::alloc_checked(
|
|||
&mut cs.ns(|| format!("value_{}", i)),
|
|||
|| Ok(value),
|
|||
)?);
|
|||
}
|
|||
Ok(vec)
|
|||
fn new_witness<T: Borrow<V>>(
|
|||
cs: impl Into<Namespace<F>>,
|
|||
f: impl FnOnce() -> Result<T, SynthesisError>,
|
|||
) -> Result<Self, SynthesisError> {
|
|||
Self::new_variable(cs, f, AllocationMode::Witness)
|
|||
}
|
|||
}
|
|||
|
|||
fn alloc_input_checked<F, T, CS: ConstraintSystem<ConstraintF>>(
|
|||
mut cs: CS,
|
|||
f: F,
|
|||
) -> Result<Self, SynthesisError>
|
|||
where
|
|||
F: FnOnce() -> Result<T, SynthesisError>,
|
|||
T: Borrow<[I]>,
|
|||
{
|
|||
impl<I, F: Field, A: AllocVar<I, F>> AllocVar<[I], F> for Vec<A> {
|
|||
fn new_variable<T: Borrow<[I]>>(
|
|||
cs: impl Into<Namespace<F>>,
|
|||
f: impl FnOnce() -> Result<T, SynthesisError>,
|
|||
mode: AllocationMode,
|
|||
) -> Result<Self, SynthesisError> {
|
|||
let ns = cs.into();
|
|||
let cs = ns.cs();
|
|||
let mut vec = Vec::new();
|
|||
for (i, value) in f()?.borrow().iter().enumerate() {
|
|||
vec.push(A::alloc_input_checked(
|
|||
&mut cs.ns(|| format!("value_{}", i)),
|
|||
|| Ok(value),
|
|||
)?);
|
|||
for value in f()?.borrow().iter() {
|
|||
vec.push(A::new_variable(cs.clone(), || Ok(value), mode)?);
|
|||
}
|
|||
Ok(vec)
|
|||
}
|
|||
|
@ -1,142 +1,131 @@ |
|||
use crate::prelude::*;
|
|||
use crate::{prelude::*, Vec};
|
|||
use algebra::Field;
|
|||
use r1cs_core::{ConstraintSystem, SynthesisError};
|
|||
use r1cs_core::SynthesisError;
|
|||
|
|||
/// If `condition == 1`, then enforces that `self` and `other` are equal;
|
|||
/// otherwise, it doesn't enforce anything.
|
|||
pub trait ConditionalEqGadget<ConstraintF: Field>: Eq {
|
|||
fn conditional_enforce_equal<CS: ConstraintSystem<ConstraintF>>(
|
|||
&self,
|
|||
cs: CS,
|
|||
other: &Self,
|
|||
condition: &Boolean,
|
|||
) -> Result<(), SynthesisError>;
|
|||
pub trait EqGadget<F: Field> {
|
|||
/// Output a `Boolean` value representing whether `self.value() == other.value()`.
|
|||
fn is_eq(&self, other: &Self) -> Result<Boolean<F>, SynthesisError>;
|
|||
|
|||
fn cost() -> usize;
|
|||
}
|
|||
impl<T: ConditionalEqGadget<ConstraintF>, ConstraintF: Field> ConditionalEqGadget<ConstraintF>
|
|||
for [T]
|
|||
{
|
|||
fn conditional_enforce_equal<CS: ConstraintSystem<ConstraintF>>(
|
|||
/// Output a `Boolean` value representing whether `self.value() != other.value()`.
|
|||
fn is_neq(&self, other: &Self) -> Result<Boolean<F>, SynthesisError> {
|
|||
Ok(self.is_eq(other)?.not())
|
|||
}
|
|||
|
|||
/// If `should_enforce == true`, enforce that `self` and `other` are equal; else,
|
|||
/// enforce a vacuously true statement.
|
|||
fn conditional_enforce_equal(
|
|||
&self,
|
|||
mut cs: CS,
|
|||
other: &Self,
|
|||
condition: &Boolean,
|
|||
should_enforce: &Boolean<F>,
|
|||
) -> Result<(), SynthesisError> {
|
|||
for (i, (a, b)) in self.iter().zip(other.iter()).enumerate() {
|
|||
let mut cs = cs.ns(|| format!("Iteration {}", i));
|
|||
a.conditional_enforce_equal(&mut cs, b, condition)?;
|
|||
}
|
|||
Ok(())
|
|||
self.is_eq(&other)?
|
|||
.conditional_enforce_equal(&Boolean::constant(true), should_enforce)
|
|||
}
|
|||
|
|||
fn cost() -> usize {
|
|||
unimplemented!()
|
|||
/// Enforce that `self` and `other` are equal.
|
|||
fn enforce_equal(&self, other: &Self) -> Result<(), SynthesisError> {
|
|||
self.conditional_enforce_equal(other, &Boolean::constant(true))
|
|||
}
|
|||
}
|
|||
|
|||
pub trait EqGadget<ConstraintF: Field>: Eq |
|||
where
|
|||
Self: ConditionalEqGadget<ConstraintF>,
|
|||
{
|
|||
fn enforce_equal<CS: ConstraintSystem<ConstraintF>>(
|
|||
/// If `should_enforce == true`, enforce that `self` and `other` are not equal; else,
|
|||
/// enforce a vacuously true statement.
|
|||
fn conditional_enforce_not_equal(
|
|||
&self,
|
|||
cs: CS,
|
|||
other: &Self,
|
|||
should_enforce: &Boolean<F>,
|
|||
) -> Result<(), SynthesisError> {
|
|||
self.conditional_enforce_equal(cs, other, &Boolean::constant(true))
|
|||
self.is_neq(&other)?
|
|||
.conditional_enforce_equal(&Boolean::constant(true), should_enforce)
|
|||
}
|
|||
|
|||
fn cost() -> usize {
|
|||
<Self as ConditionalEqGadget<ConstraintF>>::cost()
|
|||
/// Enforce that `self` and `other` are not equal.
|
|||
fn enforce_not_equal(&self, other: &Self) -> Result<(), SynthesisError> {
|
|||
self.conditional_enforce_not_equal(other, &Boolean::constant(true))
|
|||
}
|
|||
}
|
|||
|
|||
impl<T: EqGadget<ConstraintF>, ConstraintF: Field> EqGadget<ConstraintF> for [T] {}
|
|||
impl<T: EqGadget<F> + R1CSVar<F>, F: Field> EqGadget<F> for [T] {
|
|||
fn is_eq(&self, other: &Self) -> Result<Boolean<F>, SynthesisError> {
|
|||
assert_eq!(self.len(), other.len());
|
|||
assert!(!self.is_empty());
|
|||
let mut results = Vec::with_capacity(self.len());
|
|||
for (a, b) in self.iter().zip(other) {
|
|||
results.push(a.is_eq(b)?);
|
|||
}
|
|||
Boolean::kary_and(&results)
|
|||
}
|
|||
|
|||
pub trait NEqGadget<ConstraintF: Field>: Eq {
|
|||
fn enforce_not_equal<CS: ConstraintSystem<ConstraintF>>(
|
|||
fn conditional_enforce_equal(
|
|||
&self,
|
|||
cs: CS,
|
|||
other: &Self,
|
|||
) -> Result<(), SynthesisError>;
|
|||
condition: &Boolean<F>,
|
|||
) -> Result<(), SynthesisError> {
|
|||
assert_eq!(self.len(), other.len());
|
|||
for (a, b) in self.iter().zip(other) {
|
|||
a.conditional_enforce_equal(b, condition)?;
|
|||
}
|
|||
Ok(())
|
|||
}
|
|||
|
|||
fn cost() -> usize;
|
|||
fn conditional_enforce_not_equal(
|
|||
&self,
|
|||
other: &Self,
|
|||
should_enforce: &Boolean<F>,
|
|||
) -> Result<(), SynthesisError> {
|
|||
assert_eq!(self.len(), other.len());
|
|||
let some_are_different = self.is_neq(other)?;
|
|||
if let Some(cs) = some_are_different.cs().or(should_enforce.cs()) {
|
|||
cs.enforce_constraint(
|
|||
some_are_different.lc(),
|
|||
should_enforce.lc(),
|
|||
should_enforce.lc(),
|
|||
)
|
|||
} else {
|
|||
// `some_are_different` and `should_enforce` are both constants
|
|||
assert!(some_are_different.value().unwrap());
|
|||
Ok(())
|
|||
}
|
|||
}
|
|||
}
|
|||
|
|||
pub trait OrEqualsGadget<ConstraintF: Field>
|
|||
where
|
|||
Self: Sized,
|
|||
{
|
|||
fn enforce_equal_or<CS: ConstraintSystem<ConstraintF>>(
|
|||
cs: CS,
|
|||
cond: &Boolean,
|
|||
var: &Self,
|
|||
/// If `should_enforce == true`, enforce that `self` equals
|
|||
/// (a) `first` (if `cond` is `true`)
|
|||
/// (b) `second` (if `cond` is `false`)
|
|||
fn conditional_enforce_equal_or(
|
|||
&self,
|
|||
cond: &Boolean<ConstraintF>,
|
|||
first: &Self,
|
|||
second: &Self,
|
|||
should_enforce: &Boolean<ConstraintF>,
|
|||
) -> Result<(), SynthesisError>;
|
|||
|
|||
fn cost() -> usize;
|
|||
}
|
|||
|
|||
impl<ConstraintF: Field, T: Sized + ConditionalOrEqualsGadget<ConstraintF>>
|
|||
OrEqualsGadget<ConstraintF> for T
|
|||
{
|
|||
fn enforce_equal_or<CS: ConstraintSystem<ConstraintF>>(
|
|||
cs: CS,
|
|||
cond: &Boolean,
|
|||
var: &Self,
|
|||
fn enforce_equal_or(
|
|||
&self,
|
|||
cond: &Boolean<ConstraintF>,
|
|||
first: &Self,
|
|||
second: &Self,
|
|||
) -> Result<(), SynthesisError> {
|
|||
Self::conditional_enforce_equal_or(cs, cond, var, first, second, &Boolean::Constant(true))
|
|||
}
|
|||
|
|||
fn cost() -> usize {
|
|||
<Self as ConditionalOrEqualsGadget<ConstraintF>>::cost()
|
|||
self.conditional_enforce_equal_or(cond, first, second, &Boolean::Constant(true))
|
|||
}
|
|||
}
|
|||
|
|||
pub trait ConditionalOrEqualsGadget<ConstraintF: Field>
|
|||
impl<ConstraintF, T> OrEqualsGadget<ConstraintF> for T
|
|||
where
|
|||
Self: Sized,
|
|||
ConstraintF: Field,
|
|||
T: Sized + EqGadget<ConstraintF> + CondSelectGadget<ConstraintF>,
|
|||
{
|
|||
fn conditional_enforce_equal_or<CS: ConstraintSystem<ConstraintF>>(
|
|||
cs: CS,
|
|||
cond: &Boolean,
|
|||
var: &Self,
|
|||
first: &Self,
|
|||
second: &Self,
|
|||
should_enforce: &Boolean,
|
|||
) -> Result<(), SynthesisError>;
|
|||
|
|||
fn cost() -> usize;
|
|||
}
|
|||
|
|||
impl<
|
|||
ConstraintF: Field,
|
|||
T: Sized + ConditionalEqGadget<ConstraintF> + CondSelectGadget<ConstraintF>,
|
|||
> ConditionalOrEqualsGadget<ConstraintF> for T
|
|||
{
|
|||
fn conditional_enforce_equal_or<CS: ConstraintSystem<ConstraintF>>(
|
|||
mut cs: CS,
|
|||
cond: &Boolean,
|
|||
var: &Self,
|
|||
fn conditional_enforce_equal_or(
|
|||
&self,
|
|||
cond: &Boolean<ConstraintF>,
|
|||
first: &Self,
|
|||
second: &Self,
|
|||
should_enforce: &Boolean,
|
|||
should_enforce: &Boolean<ConstraintF>,
|
|||
) -> Result<(), SynthesisError> {
|
|||
let match_opt = Self::conditionally_select(
|
|||
&mut cs.ns(|| "conditional_select_in_or"),
|
|||
cond,
|
|||
first,
|
|||
second,
|
|||
)?;
|
|||
var.conditional_enforce_equal(&mut cs.ns(|| "equals_in_or"), &match_opt, should_enforce)
|
|||
}
|
|||
|
|||
fn cost() -> usize {
|
|||
<Self as ConditionalEqGadget<ConstraintF>>::cost()
|
|||
+ <Self as CondSelectGadget<ConstraintF>>::cost()
|
|||
let match_opt = cond.select(first, second)?;
|
|||
self.conditional_enforce_equal(&match_opt, should_enforce)
|
|||
}
|
|||
}
|
@ -0,0 +1,135 @@ |
|||
// Implements AddAssign on Self by deferring to an implementation on &Self
|
|||
#[macro_export]
|
|||
macro_rules! impl_ops {
|
|||
(
|
|||
$type: ty,
|
|||
$native: ty,
|
|||
$trait: ident,
|
|||
$fn: ident,
|
|||
$assign_trait: ident,
|
|||
$assign_fn: ident,
|
|||
$impl: expr,
|
|||
$constant_impl: expr,
|
|||
$($args:tt)*
|
|||
) => {
|
|||
impl_bounded_ops!($type, $native, $trait, $fn, $assign_trait, $assign_fn, $impl, $constant_impl, ($($args)+), );
|
|||
};
|
|||
}
|
|||
|
|||
macro_rules! impl_bounded_ops {
|
|||
(
|
|||
$type: ty,
|
|||
$native: ty,
|
|||
$trait: ident,
|
|||
$fn: ident,
|
|||
$assign_trait: ident,
|
|||
$assign_fn: ident,
|
|||
$impl: expr,
|
|||
$constant_impl: expr,
|
|||
($($params:tt)+),
|
|||
$($bounds:tt)*
|
|||
) => {
|
|||
impl<'a, $($params)+> core::ops::$trait<&'a $type> for &'a $type
|
|||
where
|
|||
$($bounds)*
|
|||
{
|
|||
type Output = $type;
|
|||
|
|||
fn $fn(self, other: Self) -> Self::Output {
|
|||
$impl(self, other)
|
|||
}
|
|||
}
|
|||
|
|||
impl<'a, $($params)+> core::ops::$trait<$type> for &'a $type
|
|||
where
|
|||
$($bounds)*
|
|||
{
|
|||
type Output = $type;
|
|||
|
|||
fn $fn(self, other: $type) -> Self::Output {
|
|||
core::ops::$trait::$fn(self, &other)
|
|||
}
|
|||
}
|
|||
|
|||
impl<'a, $($params)+> core::ops::$trait<&'a $type> for $type
|
|||
where
|
|||
$($bounds)*
|
|||
{
|
|||
type Output = $type;
|
|||
|
|||
fn $fn(self, other: &'a $type) -> Self::Output {
|
|||
core::ops::$trait::$fn(&self, other)
|
|||
}
|
|||
}
|
|||
|
|||
impl<$($params)+> core::ops::$trait<$type> for $type
|
|||
where
|
|||
|
|||
$($bounds)*
|
|||
{
|
|||
type Output = $type;
|
|||
|
|||
fn $fn(self, other: $type) -> Self::Output {
|
|||
core::ops::$trait::$fn(&self, &other)
|
|||
}
|
|||
}
|
|||
|
|||
impl<$($params)+> core::ops::$assign_trait<$type> for $type
|
|||
where
|
|||
|
|||
$($bounds)*
|
|||
{
|
|||
fn $assign_fn(&mut self, other: $type) {
|
|||
let result = core::ops::$trait::$fn(&*self, &other);
|
|||
*self = result
|
|||
}
|
|||
}
|
|||
|
|||
impl<'a, $($params)+> core::ops::$assign_trait<&'a $type> for $type
|
|||
where
|
|||
|
|||
$($bounds)*
|
|||
{
|
|||
fn $assign_fn(&mut self, other: &'a $type) {
|
|||
let result = core::ops::$trait::$fn(&*self, other);
|
|||
*self = result
|
|||
}
|
|||
}
|
|||
|
|||
impl<'a, $($params)+> core::ops::$trait<$native> for &'a $type
|
|||
where
|
|||
|
|||
$($bounds)*
|
|||
{
|
|||
type Output = $type;
|
|||
|
|||
fn $fn(self, other: $native) -> Self::Output {
|
|||
$constant_impl(self, other)
|
|||
}
|
|||
}
|
|||
|
|||
impl<$($params)+> core::ops::$trait<$native> for $type
|
|||
where
|
|||
|
|||
$($bounds)*
|
|||
{
|
|||
type Output = $type;
|
|||
|
|||
fn $fn(self, other: $native) -> Self::Output {
|
|||
core::ops::$trait::$fn(&self, other)
|
|||
}
|
|||
}
|
|||
|
|||
impl<$($params)+> core::ops::$assign_trait<$native> for $type
|
|||
where
|
|||
|
|||
$($bounds)*
|
|||
{
|
|||
|
|||
fn $assign_fn(&mut self, other: $native) {
|
|||
let result = core::ops::$trait::$fn(&*self, other);
|
|||
*self = result
|
|||
}
|
|||
}
|
|||
}
|
|||
}
|