mirror of
https://github.com/arnaucube/ark-r1cs-std.git
synced 2026-01-11 08:21:30 +01:00
Refactor variable traits in r1cs-std.
This commit is contained in:
@@ -1,141 +1,72 @@
|
|||||||
use crate::Vec;
|
use crate::Vec;
|
||||||
use algebra::Field;
|
use algebra::Field;
|
||||||
use core::borrow::Borrow;
|
use core::borrow::Borrow;
|
||||||
use r1cs_core::{ConstraintSystem, SynthesisError};
|
use r1cs_core::{Namespace, SynthesisError};
|
||||||
|
|
||||||
pub trait AllocGadget<V, ConstraintF: Field>
|
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Copy, Clone)]
|
||||||
|
pub enum AllocationMode {
|
||||||
|
Constant = 0,
|
||||||
|
Input = 1,
|
||||||
|
Witness = 2,
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait AllocVar<V, F: Field>
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
V: ?Sized,
|
V: ?Sized,
|
||||||
{
|
{
|
||||||
fn alloc_constant<T, CS: ConstraintSystem<ConstraintF>>(
|
fn new_variable<T: Borrow<V>>(
|
||||||
cs: CS,
|
cs: impl Into<Namespace<F>>,
|
||||||
t: T,
|
f: impl FnOnce() -> Result<T, SynthesisError>,
|
||||||
) -> Result<Self, SynthesisError>
|
mode: AllocationMode,
|
||||||
where
|
) -> Result<Self, SynthesisError>;
|
||||||
T: Borrow<V>;
|
|
||||||
|
|
||||||
fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(cs: CS, f: F) -> Result<Self, SynthesisError>
|
fn new_constant(
|
||||||
where
|
cs: impl Into<Namespace<F>>,
|
||||||
F: FnOnce() -> Result<T, SynthesisError>,
|
t: impl Borrow<V>,
|
||||||
T: Borrow<V>;
|
) -> Result<Self, SynthesisError> {
|
||||||
|
Self::new_variable(cs, || Ok(t), AllocationMode::Constant)
|
||||||
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>>(
|
fn new_input<T: Borrow<V>>(
|
||||||
cs: CS,
|
cs: impl Into<Namespace<F>>,
|
||||||
f: F,
|
f: impl FnOnce() -> Result<T, SynthesisError>,
|
||||||
) -> Result<Self, SynthesisError>
|
) -> Result<Self, SynthesisError> {
|
||||||
where
|
Self::new_variable(cs, f, AllocationMode::Input)
|
||||||
F: FnOnce() -> Result<T, SynthesisError>,
|
}
|
||||||
T: Borrow<V>;
|
|
||||||
|
|
||||||
fn alloc_input_checked<F, T, CS: ConstraintSystem<ConstraintF>>(
|
fn new_witness<T: Borrow<V>>(
|
||||||
cs: CS,
|
cs: impl Into<Namespace<F>>,
|
||||||
f: F,
|
f: impl FnOnce() -> Result<T, SynthesisError>,
|
||||||
) -> Result<Self, SynthesisError>
|
) -> Result<Self, SynthesisError> {
|
||||||
where
|
Self::new_variable(cs, f, AllocationMode::Witness)
|
||||||
F: FnOnce() -> Result<T, SynthesisError>,
|
|
||||||
T: Borrow<V>,
|
|
||||||
{
|
|
||||||
Self::alloc_input(cs, f)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<I, ConstraintF: Field, A: AllocGadget<I, ConstraintF>> AllocGadget<[I], ConstraintF>
|
impl<I, F: Field, A: AllocVar<I, F>> AllocVar<[I], F> for Vec<A> {
|
||||||
for Vec<A>
|
fn new_variable<T: Borrow<[I]>>(
|
||||||
{
|
cs: impl Into<Namespace<F>>,
|
||||||
#[inline]
|
f: impl FnOnce() -> Result<T, SynthesisError>,
|
||||||
fn alloc_constant<T, CS: ConstraintSystem<ConstraintF>>(
|
mode: AllocationMode,
|
||||||
mut cs: CS,
|
) -> Result<Self, SynthesisError> {
|
||||||
t: T,
|
let ns = cs.into();
|
||||||
) -> Result<Self, SynthesisError>
|
let cs = ns.cs();
|
||||||
where
|
|
||||||
T: Borrow<[I]>,
|
|
||||||
{
|
|
||||||
let mut vec = Vec::new();
|
let mut vec = Vec::new();
|
||||||
for (i, value) in t.borrow().iter().enumerate() {
|
for value in f()?.borrow().iter() {
|
||||||
vec.push(A::alloc_constant(cs.ns(|| format!("value_{}", i)), value)?);
|
vec.push(A::new_variable(cs.clone(), || Ok(value), mode)?);
|
||||||
}
|
|
||||||
Ok(vec)
|
|
||||||
}
|
|
||||||
|
|
||||||
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 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 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 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]>,
|
|
||||||
{
|
|
||||||
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),
|
|
||||||
)?);
|
|
||||||
}
|
}
|
||||||
Ok(vec)
|
Ok(vec)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,142 +1,131 @@
|
|||||||
use crate::prelude::*;
|
use crate::{prelude::*, Vec};
|
||||||
use algebra::Field;
|
use algebra::Field;
|
||||||
use r1cs_core::{ConstraintSystem, SynthesisError};
|
use r1cs_core::SynthesisError;
|
||||||
|
|
||||||
/// If `condition == 1`, then enforces that `self` and `other` are equal;
|
pub trait EqGadget<F: Field> {
|
||||||
/// otherwise, it doesn't enforce anything.
|
/// Output a `Boolean` value representing whether `self.value() == other.value()`.
|
||||||
pub trait ConditionalEqGadget<ConstraintF: Field>: Eq {
|
fn is_eq(&self, other: &Self) -> Result<Boolean<F>, SynthesisError>;
|
||||||
fn conditional_enforce_equal<CS: ConstraintSystem<ConstraintF>>(
|
|
||||||
&self,
|
|
||||||
cs: CS,
|
|
||||||
other: &Self,
|
|
||||||
condition: &Boolean,
|
|
||||||
) -> Result<(), SynthesisError>;
|
|
||||||
|
|
||||||
fn cost() -> usize;
|
/// 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())
|
||||||
}
|
}
|
||||||
impl<T: ConditionalEqGadget<ConstraintF>, ConstraintF: Field> ConditionalEqGadget<ConstraintF>
|
|
||||||
for [T]
|
/// If `should_enforce == true`, enforce that `self` and `other` are equal; else,
|
||||||
{
|
/// enforce a vacuously true statement.
|
||||||
fn conditional_enforce_equal<CS: ConstraintSystem<ConstraintF>>(
|
fn conditional_enforce_equal(
|
||||||
&self,
|
&self,
|
||||||
mut cs: CS,
|
|
||||||
other: &Self,
|
other: &Self,
|
||||||
condition: &Boolean,
|
should_enforce: &Boolean<F>,
|
||||||
) -> Result<(), SynthesisError> {
|
) -> Result<(), SynthesisError> {
|
||||||
for (i, (a, b)) in self.iter().zip(other.iter()).enumerate() {
|
self.is_eq(&other)?
|
||||||
let mut cs = cs.ns(|| format!("Iteration {}", i));
|
.conditional_enforce_equal(&Boolean::constant(true), should_enforce)
|
||||||
a.conditional_enforce_equal(&mut cs, b, condition)?;
|
}
|
||||||
|
|
||||||
|
/// Enforce that `self` and `other` are equal.
|
||||||
|
fn enforce_equal(&self, other: &Self) -> Result<(), SynthesisError> {
|
||||||
|
self.conditional_enforce_equal(other, &Boolean::constant(true))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// If `should_enforce == true`, enforce that `self` and `other` are not equal; else,
|
||||||
|
/// enforce a vacuously true statement.
|
||||||
|
fn conditional_enforce_not_equal(
|
||||||
|
&self,
|
||||||
|
other: &Self,
|
||||||
|
should_enforce: &Boolean<F>,
|
||||||
|
) -> Result<(), SynthesisError> {
|
||||||
|
self.is_neq(&other)?
|
||||||
|
.conditional_enforce_equal(&Boolean::constant(true), should_enforce)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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<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)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn conditional_enforce_equal(
|
||||||
|
&self,
|
||||||
|
other: &Self,
|
||||||
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cost() -> usize {
|
fn conditional_enforce_not_equal(
|
||||||
unimplemented!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait EqGadget<ConstraintF: Field>: Eq
|
|
||||||
where
|
|
||||||
Self: ConditionalEqGadget<ConstraintF>,
|
|
||||||
{
|
|
||||||
fn enforce_equal<CS: ConstraintSystem<ConstraintF>>(
|
|
||||||
&self,
|
&self,
|
||||||
cs: CS,
|
|
||||||
other: &Self,
|
other: &Self,
|
||||||
|
should_enforce: &Boolean<F>,
|
||||||
) -> Result<(), SynthesisError> {
|
) -> Result<(), SynthesisError> {
|
||||||
self.conditional_enforce_equal(cs, other, &Boolean::constant(true))
|
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()) {
|
||||||
fn cost() -> usize {
|
cs.enforce_constraint(
|
||||||
<Self as ConditionalEqGadget<ConstraintF>>::cost()
|
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(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: EqGadget<ConstraintF>, ConstraintF: Field> EqGadget<ConstraintF> for [T] {}
|
|
||||||
|
|
||||||
pub trait NEqGadget<ConstraintF: Field>: Eq {
|
|
||||||
fn enforce_not_equal<CS: ConstraintSystem<ConstraintF>>(
|
|
||||||
&self,
|
|
||||||
cs: CS,
|
|
||||||
other: &Self,
|
|
||||||
) -> Result<(), SynthesisError>;
|
|
||||||
|
|
||||||
fn cost() -> usize;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait OrEqualsGadget<ConstraintF: Field>
|
pub trait OrEqualsGadget<ConstraintF: Field>
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
{
|
{
|
||||||
fn enforce_equal_or<CS: ConstraintSystem<ConstraintF>>(
|
/// If `should_enforce == true`, enforce that `self` equals
|
||||||
cs: CS,
|
/// (a) `first` (if `cond` is `true`)
|
||||||
cond: &Boolean,
|
/// (b) `second` (if `cond` is `false`)
|
||||||
var: &Self,
|
fn conditional_enforce_equal_or(
|
||||||
|
&self,
|
||||||
|
cond: &Boolean<ConstraintF>,
|
||||||
first: &Self,
|
first: &Self,
|
||||||
second: &Self,
|
second: &Self,
|
||||||
|
should_enforce: &Boolean<ConstraintF>,
|
||||||
) -> Result<(), SynthesisError>;
|
) -> Result<(), SynthesisError>;
|
||||||
|
|
||||||
fn cost() -> usize;
|
fn enforce_equal_or(
|
||||||
}
|
&self,
|
||||||
|
cond: &Boolean<ConstraintF>,
|
||||||
impl<ConstraintF: Field, T: Sized + ConditionalOrEqualsGadget<ConstraintF>>
|
|
||||||
OrEqualsGadget<ConstraintF> for T
|
|
||||||
{
|
|
||||||
fn enforce_equal_or<CS: ConstraintSystem<ConstraintF>>(
|
|
||||||
cs: CS,
|
|
||||||
cond: &Boolean,
|
|
||||||
var: &Self,
|
|
||||||
first: &Self,
|
first: &Self,
|
||||||
second: &Self,
|
second: &Self,
|
||||||
) -> Result<(), SynthesisError> {
|
) -> Result<(), SynthesisError> {
|
||||||
Self::conditional_enforce_equal_or(cs, cond, var, first, second, &Boolean::Constant(true))
|
self.conditional_enforce_equal_or(cond, first, second, &Boolean::Constant(true))
|
||||||
}
|
|
||||||
|
|
||||||
fn cost() -> usize {
|
|
||||||
<Self as ConditionalOrEqualsGadget<ConstraintF>>::cost()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait ConditionalOrEqualsGadget<ConstraintF: Field>
|
impl<ConstraintF, T> OrEqualsGadget<ConstraintF> for T
|
||||||
where
|
where
|
||||||
Self: Sized,
|
|
||||||
{
|
|
||||||
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,
|
ConstraintF: Field,
|
||||||
T: Sized + ConditionalEqGadget<ConstraintF> + CondSelectGadget<ConstraintF>,
|
T: Sized + EqGadget<ConstraintF> + CondSelectGadget<ConstraintF>,
|
||||||
> ConditionalOrEqualsGadget<ConstraintF> for T
|
|
||||||
{
|
{
|
||||||
fn conditional_enforce_equal_or<CS: ConstraintSystem<ConstraintF>>(
|
fn conditional_enforce_equal_or(
|
||||||
mut cs: CS,
|
&self,
|
||||||
cond: &Boolean,
|
cond: &Boolean<ConstraintF>,
|
||||||
var: &Self,
|
|
||||||
first: &Self,
|
first: &Self,
|
||||||
second: &Self,
|
second: &Self,
|
||||||
should_enforce: &Boolean,
|
should_enforce: &Boolean<ConstraintF>,
|
||||||
) -> Result<(), SynthesisError> {
|
) -> Result<(), SynthesisError> {
|
||||||
let match_opt = Self::conditionally_select(
|
let match_opt = cond.select(first, second)?;
|
||||||
&mut cs.ns(|| "conditional_select_in_or"),
|
self.conditional_enforce_equal(&match_opt, should_enforce)
|
||||||
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()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,21 +20,16 @@ extern crate algebra;
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate derivative;
|
extern crate derivative;
|
||||||
|
|
||||||
/// used by test_constraint_system
|
#[macro_use]
|
||||||
#[cfg(not(feature = "std"))]
|
pub mod macros;
|
||||||
macro_rules! println {
|
|
||||||
() => {};
|
|
||||||
($($arg: tt)*) => {};
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(feature = "std"))]
|
#[cfg(not(feature = "std"))]
|
||||||
use ralloc::{collections::BTreeMap, string::String, vec::Vec};
|
use ralloc::vec::Vec;
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
use std::{collections::BTreeMap, string::String, vec::Vec};
|
use std::vec::Vec;
|
||||||
|
|
||||||
pub mod test_constraint_counter;
|
use algebra::prelude::Field;
|
||||||
pub mod test_constraint_system;
|
|
||||||
|
|
||||||
pub mod bits;
|
pub mod bits;
|
||||||
pub use self::bits::*;
|
pub use self::bits::*;
|
||||||
@@ -48,6 +43,9 @@ mod instantiated;
|
|||||||
#[cfg(feature = "bls12_377")]
|
#[cfg(feature = "bls12_377")]
|
||||||
pub use instantiated::bls12_377;
|
pub use instantiated::bls12_377;
|
||||||
|
|
||||||
|
#[cfg(feature = "ed_on_bn254")]
|
||||||
|
pub use instantiated::ed_on_bn254;
|
||||||
|
|
||||||
#[cfg(feature = "ed_on_bls12_377")]
|
#[cfg(feature = "ed_on_bls12_377")]
|
||||||
pub use instantiated::ed_on_bls12_377;
|
pub use instantiated::ed_on_bls12_377;
|
||||||
|
|
||||||
@@ -60,8 +58,8 @@ pub use instantiated::ed_on_mnt4_753;
|
|||||||
#[cfg(feature = "ed_on_cp6_782")]
|
#[cfg(feature = "ed_on_cp6_782")]
|
||||||
pub use instantiated::ed_on_cp6_782;
|
pub use instantiated::ed_on_cp6_782;
|
||||||
|
|
||||||
#[cfg(feature = "ed_on_bn254")]
|
#[cfg(feature = "ed_on_bw6_761")]
|
||||||
pub use instantiated::ed_on_bn254;
|
pub use instantiated::ed_on_bw6_761;
|
||||||
|
|
||||||
#[cfg(feature = "ed_on_bls12_381")]
|
#[cfg(feature = "ed_on_bls12_381")]
|
||||||
pub use instantiated::ed_on_bls12_381;
|
pub use instantiated::ed_on_bls12_381;
|
||||||
@@ -89,20 +87,70 @@ pub mod prelude {
|
|||||||
alloc::*,
|
alloc::*,
|
||||||
bits::{boolean::Boolean, uint32::UInt32, uint8::UInt8, ToBitsGadget, ToBytesGadget},
|
bits::{boolean::Boolean, uint32::UInt32, uint8::UInt8, ToBitsGadget, ToBytesGadget},
|
||||||
eq::*,
|
eq::*,
|
||||||
fields::{fp::FpGadget, FieldGadget, ToConstraintFieldGadget},
|
fields::{FieldOpsBounds, FieldVar},
|
||||||
groups::GroupGadget,
|
groups::{CurveVar, GroupOpsBounds},
|
||||||
instantiated::*,
|
instantiated::*,
|
||||||
pairing::PairingGadget,
|
pairing::PairingVar,
|
||||||
select::*,
|
select::*,
|
||||||
|
R1CSVar,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait R1CSVar<F: Field> {
|
||||||
|
type Value: core::fmt::Debug + Eq + Clone;
|
||||||
|
|
||||||
|
/// Returns the underlying `ConstraintSystemRef`.
|
||||||
|
fn cs(&self) -> Option<r1cs_core::ConstraintSystemRef<F>>;
|
||||||
|
|
||||||
|
/// Returns `true` if `self` is a circuit-generation-time constant.
|
||||||
|
fn is_constant(&self) -> bool {
|
||||||
|
self.cs()
|
||||||
|
.map_or(true, |cs| cs == r1cs_core::ConstraintSystemRef::None)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the value that is assigned to `self` in the underlying
|
||||||
|
/// `ConstraintSystem`.
|
||||||
|
fn value(&self) -> Result<Self::Value, r1cs_core::SynthesisError>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<F: Field, T: R1CSVar<F>> R1CSVar<F> for [T] {
|
||||||
|
type Value = Vec<T::Value>;
|
||||||
|
|
||||||
|
fn cs(&self) -> Option<r1cs_core::ConstraintSystemRef<F>> {
|
||||||
|
let mut result = None;
|
||||||
|
for var in self {
|
||||||
|
result = var.cs().or(result);
|
||||||
|
}
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
fn value(&self) -> Result<Self::Value, r1cs_core::SynthesisError> {
|
||||||
|
let mut result = Vec::new();
|
||||||
|
for var in self {
|
||||||
|
result.push(var.value()?);
|
||||||
|
}
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, F: Field, T: 'a + R1CSVar<F>> R1CSVar<F> for &'a T {
|
||||||
|
type Value = T::Value;
|
||||||
|
|
||||||
|
fn cs(&self) -> Option<r1cs_core::ConstraintSystemRef<F>> {
|
||||||
|
(*self).cs()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn value(&self) -> Result<Self::Value, r1cs_core::SynthesisError> {
|
||||||
|
(*self).value()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub trait Assignment<T> {
|
pub trait Assignment<T> {
|
||||||
fn get(self) -> Result<T, r1cs_core::SynthesisError>;
|
fn get(self) -> Result<T, r1cs_core::SynthesisError>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Assignment<T> for Option<T> {
|
impl<T> Assignment<T> for Option<T> {
|
||||||
fn get(self) -> Result<T, r1cs_core::SynthesisError> {
|
fn get(self) -> Result<T, r1cs_core::SynthesisError> {
|
||||||
self.ok_or_else(|| r1cs_core::SynthesisError::AssignmentMissing)
|
self.ok_or(r1cs_core::SynthesisError::AssignmentMissing)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
135
r1cs-std/src/macros.rs
Normal file
135
r1cs-std/src/macros.rs
Normal file
@@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,20 +1,17 @@
|
|||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use algebra::Field;
|
use algebra::Field;
|
||||||
use r1cs_core::{ConstraintSystem, SynthesisError};
|
use r1cs_core::SynthesisError;
|
||||||
|
|
||||||
/// If condition is `true`, return `true_value`; else, select `false_value`.
|
/// If condition is `true`, return `true_value`; else, select `false_value`.
|
||||||
pub trait CondSelectGadget<ConstraintF: Field>
|
pub trait CondSelectGadget<ConstraintF: Field>
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
{
|
{
|
||||||
fn conditionally_select<CS: ConstraintSystem<ConstraintF>>(
|
fn conditionally_select(
|
||||||
cs: CS,
|
cond: &Boolean<ConstraintF>,
|
||||||
cond: &Boolean,
|
|
||||||
true_value: &Self,
|
true_value: &Self,
|
||||||
false_value: &Self,
|
false_value: &Self,
|
||||||
) -> Result<Self, SynthesisError>;
|
) -> Result<Self, SynthesisError>;
|
||||||
|
|
||||||
fn cost() -> usize;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Uses two bits to perform a lookup into a table
|
/// Uses two bits to perform a lookup into a table
|
||||||
@@ -23,13 +20,10 @@ where
|
|||||||
Self: Sized,
|
Self: Sized,
|
||||||
{
|
{
|
||||||
type TableConstant;
|
type TableConstant;
|
||||||
fn two_bit_lookup<CS: ConstraintSystem<ConstraintF>>(
|
fn two_bit_lookup(
|
||||||
cs: CS,
|
bits: &[Boolean<ConstraintF>],
|
||||||
bits: &[Boolean],
|
|
||||||
constants: &[Self::TableConstant],
|
constants: &[Self::TableConstant],
|
||||||
) -> Result<Self, SynthesisError>;
|
) -> Result<Self, SynthesisError>;
|
||||||
|
|
||||||
fn cost() -> usize;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Uses three bits to perform a lookup into a table, where the last bit
|
/// Uses three bits to perform a lookup into a table, where the last bit
|
||||||
@@ -39,12 +33,9 @@ where
|
|||||||
Self: Sized,
|
Self: Sized,
|
||||||
{
|
{
|
||||||
type TableConstant;
|
type TableConstant;
|
||||||
fn three_bit_cond_neg_lookup<CS: ConstraintSystem<ConstraintF>>(
|
fn three_bit_cond_neg_lookup(
|
||||||
cs: CS,
|
bits: &[Boolean<ConstraintF>],
|
||||||
bits: &[Boolean],
|
b0b1: &Boolean<ConstraintF>,
|
||||||
b0b1: &Boolean,
|
|
||||||
constants: &[Self::TableConstant],
|
constants: &[Self::TableConstant],
|
||||||
) -> Result<Self, SynthesisError>;
|
) -> Result<Self, SynthesisError>;
|
||||||
|
|
||||||
fn cost() -> usize;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user