ark-r1cs-std/src/lib.rs
src/lib.rsRaw
1#![cfg_attr(not(feature = "std"), no_std)]
2//! This crate implements common "gadgets" that make
3//! programming rank-1 constraint systems easier.
4#![deny(
5 warnings,
6 unused,
7 future_incompatible,
8 nonstandard_style,
9 rust_2018_idioms
10)]
11
12#[macro_use]
13extern crate ark_std;
14
15#[macro_use]
16extern crate ark_relations;
17
18#[doc(hidden)]
19#[macro_use]
20extern crate derivative;
21
22/// Some utility macros for making downstream impls easier.
23#[macro_use]
24pub mod macros;
25
26pub(crate) use ark_std::vec::Vec;
27
28use ark_ff::Field;
29
30/// This module implements gadgets related to bit manipulation, such as
31/// `Boolean` and `UInt`s.
32pub mod bits;
33pub use self::bits::*;
34
35/// This module implements gadgets related to field arithmetic.
36pub mod fields;
37
38/// This module implements gadgets related to group arithmetic, and specifically
39/// elliptic curve arithmetic.
40pub mod groups;
41
42/// This module implements gadgets related to computing pairings in bilinear
43/// groups.
44pub mod pairing;
45
46/// This module describes a trait for allocating new variables in a constraint
47/// system.
48pub mod alloc;
49/// This module describes a trait for checking equality of variables.
50pub mod eq;
51/// This module describes traits for conditionally selecting a variable from a
52/// list of variables.
53pub mod select;
54
55#[allow(missing_docs)]
56pub mod prelude {
57 pub use crate::{
58 alloc::*,
59 bits::{boolean::Boolean, uint32::UInt32, uint8::UInt8, ToBitsGadget, ToBytesGadget},
60 eq::*,
61 fields::{FieldOpsBounds, FieldVar},
62 groups::{CurveVar, GroupOpsBounds},
63 pairing::PairingVar,
64 select::*,
65 R1CSVar,
66 };
67}
68
69/// This trait describes some core functionality that is common to high-level
70/// variables, such as `Boolean`s, `FieldVar`s, `GroupVar`s, etc.
71pub trait R1CSVar<F: Field> {
72 /// The type of the "native" value that `Self` represents in the constraint
73 /// system.
74 type Value: core::fmt::Debug + Eq + Clone;
75
76 /// Returns the underlying `ConstraintSystemRef`.
77 ///
78 /// If `self` is a constant value, then this *must* return
79 /// `ark_relations::r1cs::ConstraintSystemRef::None`.
80 fn cs(&self) -> ark_relations::r1cs::ConstraintSystemRef<F>;
81
82 /// Returns `true` if `self` is a circuit-generation-time constant.
83 fn is_constant(&self) -> bool {
84 self.cs().is_none()
85 }
86
87 /// Returns the value that is assigned to `self` in the underlying
88 /// `ConstraintSystem`.
89 fn value(&self) -> Result<Self::Value, ark_relations::r1cs::SynthesisError>;
90}
91
92impl<F: Field, T: R1CSVar<F>> R1CSVar<F> for [T] {
93 type Value = Vec<T::Value>;
94
95 fn cs(&self) -> ark_relations::r1cs::ConstraintSystemRef<F> {
96 let mut result = ark_relations::r1cs::ConstraintSystemRef::None;
97 for var in self {
98 result = var.cs().or(result);
99 }
100 result
101 }
102
103 fn value(&self) -> Result<Self::Value, ark_relations::r1cs::SynthesisError> {
104 let mut result = Vec::new();
105 for var in self {
106 result.push(var.value()?);
107 }
108 Ok(result)
109 }
110}
111
112impl<'a, F: Field, T: 'a + R1CSVar<F>> R1CSVar<F> for &'a T {
113 type Value = T::Value;
114
115 fn cs(&self) -> ark_relations::r1cs::ConstraintSystemRef<F> {
116 (*self).cs()
117 }
118
119 fn value(&self) -> Result<Self::Value, ark_relations::r1cs::SynthesisError> {
120 (*self).value()
121 }
122}
123
124/// A utility trait to convert `Self` to `Result<T, SynthesisErrorA`.>
125pub trait Assignment<T> {
126 /// Converts `self` to `Result`.
127 fn get(self) -> Result<T, ark_relations::r1cs::SynthesisError>;
128}
129
130impl<T> Assignment<T> for Option<T> {
131 fn get(self) -> Result<T, ark_relations::r1cs::SynthesisError> {
132 self.ok_or(ark_relations::r1cs::SynthesisError::AssignmentMissing)
133 }
134}
135
136/// Specifies how to convert a variable of type `Self` to variables of
137/// type `FpVar<ConstraintF>`
138pub trait ToConstraintFieldGadget<ConstraintF: ark_ff::PrimeField> {
139 /// Converts `self` to `FpVar<ConstraintF>` variables.
140 fn to_constraint_field(
141 &self,
142 ) -> Result<Vec<crate::fields::fp::FpVar<ConstraintF>>, ark_relations::r1cs::SynthesisError>;
143}