arnaucube
arnaucube/ark-r1cs-stdPublic Self-hosted repository
ark-r1cs-std/r1cs-std/src/lib.rs
r1cs-std/src/lib.rsRaw
  1#![deny(
  2    unused_import_braces,
  3    unused_qualifications,
  4    trivial_casts,
  5    trivial_numeric_casts
  6)]
  7#![deny(
  8    unused_qualifications,
  9    variant_size_differences,
 10    stable_features,
 11    unreachable_pub
 12)]
 13#![deny(
 14    non_shorthand_field_patterns,
 15    unused_attributes,
 16    unused_imports,
 17    unused_extern_crates
 18)]
 19#![deny(
 20    renamed_and_removed_lints,
 21    stable_features,
 22    unused_allocation,
 23    unused_comparisons,
 24    bare_trait_objects
 25)]
 26#![deny(
 27    const_err,
 28    unused_must_use,
 29    unused_mut,
 30    unused_unsafe,
 31    private_in_public,
 32    unsafe_code
 33)]
 34#![forbid(unsafe_code)]
 35
 36#[macro_use]
 37extern crate algebra;
 38#[macro_use]
 39extern crate derivative;
 40
 41use crate::ConstraintVar::*;
 42use algebra::Field;
 43use r1cs_core::{LinearCombination, SynthesisError, Variable};
 44use std::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub};
 45
 46pub mod test_constraint_system;
 47
 48pub mod bits;
 49pub use self::bits::*;
 50
 51pub mod fields;
 52
 53pub mod groups;
 54
 55pub mod pairing;
 56
 57pub mod eq;
 58pub mod select;
 59pub mod alloc;
 60
 61pub mod prelude {
 62    pub use crate::eq::*;
 63    pub use crate::select::*;
 64    pub use crate::alloc::*;
 65    pub use crate::fields::FieldGadget;
 66    pub use crate::groups::GroupGadget;
 67    pub use crate::pairing::PairingGadget;
 68    pub use crate::bits::{ToBitsGadget, ToBytesGadget, boolean::Boolean, uint8::UInt8, uint32::UInt32};
 69}
 70
 71pub trait Assignment<T> {
 72    fn get(self) -> Result<T, SynthesisError>;
 73}
 74
 75impl<T> Assignment<T> for Option<T> {
 76    fn get(self) -> Result<T, SynthesisError> {
 77        match self {
 78            Some(v) => Ok(v),
 79            None => Err(SynthesisError::AssignmentMissing),
 80        }
 81    }
 82}
 83
 84#[derive(Clone, Debug)]
 85pub enum ConstraintVar<F: Field> {
 86    LC(LinearCombination<F>),
 87    Var(Variable),
 88}
 89
 90impl<F: Field> From<Variable> for ConstraintVar<F> {
 91    #[inline]
 92    fn from(var: Variable) -> Self {
 93        Var(var)
 94    }
 95}
 96
 97impl<F: Field> From<(F, Variable)> for ConstraintVar<F> {
 98    #[inline]
 99    fn from(coeff_var: (F, Variable)) -> Self {
100        LC(coeff_var.into())
101    }
102}
103
104impl<F: Field> From<(F, LinearCombination<F>)> for ConstraintVar<F> {
105    #[inline]
106    fn from((coeff, mut lc): (F, LinearCombination<F>)) -> Self {
107        lc *= coeff;
108        LC(lc)
109    }
110}
111
112impl<F: Field> From<(F, ConstraintVar<F>)> for ConstraintVar<F> {
113    #[inline]
114    fn from((coeff, var): (F, ConstraintVar<F>)) -> Self {
115        match var {
116            LC(lc) => (coeff, lc).into(),
117            Var(var) => (coeff, var).into(),
118        }
119    }
120}
121
122impl<F: Field> ConstraintVar<F> {
123    #[inline]
124    pub fn zero() -> Self {
125        LC(LinearCombination::zero())
126    }
127
128    pub fn negate_in_place(&mut self) {
129        match self {
130            LC(ref mut lc) => lc.negate_in_place(),
131            Var(var) => *self = (-F::one(), *var).into(),
132        }
133    }
134
135    pub fn double_in_place(&mut self) {
136        match self {
137            LC(lc) => lc.double_in_place(),
138            Var(var) => *self = (F::one().double(), *var).into(),
139        }
140    }
141}
142
143impl<F: Field> Add<LinearCombination<F>> for ConstraintVar<F> {
144    type Output = LinearCombination<F>;
145
146    #[inline]
147    fn add(self, other_lc: LinearCombination<F>) -> LinearCombination<F> {
148        match self {
149            LC(lc) => other_lc + lc,
150            Var(var) => other_lc + var,
151        }
152    }
153}
154
155impl<F: Field> Sub<LinearCombination<F>> for ConstraintVar<F> {
156    type Output = LinearCombination<F>;
157
158    #[inline]
159    fn sub(self, other_lc: LinearCombination<F>) -> LinearCombination<F> {
160        let result = match self {
161            LC(lc) => other_lc - lc,
162            Var(var) => other_lc - var,
163        };
164        -result
165    }
166}
167
168impl<F: Field> Add<LinearCombination<F>> for &ConstraintVar<F> {
169    type Output = LinearCombination<F>;
170
171    #[inline]
172    fn add(self, other_lc: LinearCombination<F>) -> LinearCombination<F> {
173        match self {
174            LC(lc) => other_lc + lc,
175            Var(var) => other_lc + *var,
176        }
177    }
178}
179
180impl<F: Field> Sub<LinearCombination<F>> for &ConstraintVar<F> {
181    type Output = LinearCombination<F>;
182
183    #[inline]
184    fn sub(self, other_lc: LinearCombination<F>) -> LinearCombination<F> {
185        let result = match self {
186            LC(lc) => other_lc - lc,
187            Var(var) => other_lc - *var,
188        };
189        -result
190    }
191}
192
193impl<F: Field> Add<(F, Variable)> for ConstraintVar<F> {
194    type Output = Self;
195
196    #[inline]
197    fn add(self, var: (F, Variable)) -> Self {
198        let lc = match self {
199            LC(lc) => lc + var,
200            Var(var2) => LinearCombination::from(var2) + var,
201        };
202        LC(lc)
203    }
204}
205
206impl<F: Field> AddAssign<(F, Variable)> for ConstraintVar<F> {
207    #[inline]
208    fn add_assign(&mut self, var: (F, Variable)) {
209        match self {
210            LC(ref mut lc) => *lc += var,
211            Var(var2) => *self = LC(LinearCombination::from(*var2) + var),
212        };
213    }
214}
215
216impl<F: Field> Neg for ConstraintVar<F> {
217    type Output = Self;
218
219    #[inline]
220    fn neg(mut self) -> Self {
221        self.negate_in_place();
222        self
223    }
224}
225
226impl<F: Field> Mul<F> for ConstraintVar<F> {
227    type Output = Self;
228
229    #[inline]
230    fn mul(self, scalar: F) -> Self {
231        match self {
232            LC(lc) => LC(lc * scalar),
233            Var(var) => (scalar, var).into(),
234        }
235    }
236}
237
238impl<F: Field> MulAssign<F> for ConstraintVar<F> {
239    #[inline]
240    fn mul_assign(&mut self, scalar: F) {
241        match self {
242            LC(lc) => *lc *= scalar,
243            Var(var) => *self = (scalar, *var).into(),
244        }
245    }
246}
247
248impl<F: Field> Sub<(F, Variable)> for ConstraintVar<F> {
249    type Output = Self;
250
251    #[inline]
252    fn sub(self, (coeff, var): (F, Variable)) -> Self {
253        self + (-coeff, var)
254    }
255}
256
257impl<F: Field> Add<Variable> for ConstraintVar<F> {
258    type Output = Self;
259
260    fn add(self, other: Variable) -> Self {
261        self + (F::one(), other)
262    }
263}
264
265impl<F: Field> Sub<Variable> for ConstraintVar<F> {
266    type Output = Self;
267
268    #[inline]
269    fn sub(self, other: Variable) -> Self {
270        self - (F::one(), other)
271    }
272}
273
274impl<'a, F: Field> Add<&'a Self> for ConstraintVar<F> {
275    type Output = Self;
276
277    #[inline]
278    fn add(self, other: &'a Self) -> Self {
279        let lc = match self {
280            LC(lc2) => lc2,
281            Var(var) => var.into(),
282        };
283        let lc2 = match other {
284            LC(lc2) => lc + lc2,
285            Var(var) => lc + *var,
286        };
287        LC(lc2)
288    }
289}
290
291impl<'a, F: Field> Sub<&'a Self> for ConstraintVar<F> {
292    type Output = Self;
293
294    #[inline]
295    fn sub(self, other: &'a Self) -> Self {
296        let lc = match self {
297            LC(lc2) => lc2,
298            Var(var) => var.into(),
299        };
300        let lc2 = match other {
301            LC(lc2) => lc - lc2,
302            Var(var) => lc - *var,
303        };
304        LC(lc2)
305    }
306}
307
308impl<F: Field> Add<&ConstraintVar<F>> for &ConstraintVar<F> {
309    type Output = ConstraintVar<F>;
310
311    #[inline]
312    fn add(self, other: &ConstraintVar<F>) -> Self::Output {
313        (ConstraintVar::zero() + self) + other
314    }
315}
316
317impl<F: Field> Sub<&ConstraintVar<F>> for &ConstraintVar<F> {
318    type Output = ConstraintVar<F>;
319
320    #[inline]
321    fn sub(self, other: &ConstraintVar<F>) -> Self::Output {
322        (ConstraintVar::zero() + self) - other
323    }
324}
325
326impl<'a, F: Field> Add<(F, &'a Self)> for ConstraintVar<F> {
327    type Output = Self;
328
329    #[inline]
330    fn add(self, (coeff, other): (F, &'a Self)) -> Self {
331        let mut lc = match self {
332            LC(lc2) => lc2,
333            Var(var) => LinearCombination::zero() + var,
334        };
335
336        lc = match other {
337            LC(lc2) => lc + (coeff, lc2),
338            Var(var) => lc + (coeff, *var),
339        };
340        LC(lc)
341    }
342}
343
344impl<'a, F: Field> Sub<(F, &'a Self)> for ConstraintVar<F> {
345    type Output = Self;
346
347    #[inline]
348    fn sub(self, (coeff, other): (F, &'a Self)) -> Self {
349        let mut lc = match self {
350            LC(lc2) => lc2,
351            Var(var) => LinearCombination::zero() + var,
352        };
353        lc = match other {
354            LC(lc2) => lc - (coeff, lc2),
355            Var(var) => lc - (coeff, *var),
356        };
357        LC(lc)
358    }
359}