arnaucube
arnaucube/ark-r1cs-stdPublic Self-hosted repository
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#![allow(clippy::op_ref)]
 12
 13#[macro_use]
 14extern crate ark_std;
 15
 16#[macro_use]
 17extern crate ark_ff;
 18
 19#[macro_use]
 20extern crate ark_relations;
 21
 22#[doc(hidden)]
 23#[macro_use]
 24extern crate derivative;
 25
 26/// Some utility macros for making downstream impls easier.
 27#[macro_use]
 28pub mod macros;
 29
 30pub(crate) use ark_std::vec::Vec;
 31
 32use ark_ff::Field;
 33
 34/// This module contains `Boolean`, an R1CS equivalent of the `bool` type.
 35pub mod boolean;
 36
 37/// Finite field arithmetic.
 38pub mod fields;
 39
 40/// Implementations of elliptic curve group arithmetic for popular curve models.
 41pub mod groups;
 42
 43/// Gadgets for computing pairings in bilinear groups.
 44pub mod pairing;
 45
 46/// Utilities for allocating new variables in a constraint system.
 47pub mod alloc;
 48
 49/// Utilities for comparing  variables.
 50pub mod cmp;
 51
 52/// Utilities for converting variables to other kinds of variables.
 53pub mod convert;
 54
 55/// Utilities for checking equality of variables.
 56pub mod eq;
 57
 58/// Definitions of polynomial variables over finite fields.
 59pub mod poly;
 60
 61/// Contains traits for conditionally selecting a variable from a
 62/// list of variables.
 63pub mod select;
 64
 65#[cfg(test)]
 66pub(crate) mod test_utils;
 67
 68/// This module contains `UInt8`, a R1CS equivalent of the `u8` type.
 69pub mod uint8;
 70/// This module contains a macro for generating `UIntN` types, which are R1CS
 71/// equivalents of `N`-bit unsigned integers.
 72#[macro_use]
 73pub mod uint;
 74
 75pub mod uint16 {
 76    pub type UInt16<F> = super::uint::UInt<16, u16, F>;
 77}
 78pub mod uint32 {
 79    pub type UInt32<F> = super::uint::UInt<32, u32, F>;
 80}
 81pub mod uint64 {
 82    pub type UInt64<F> = super::uint::UInt<64, u64, F>;
 83}
 84pub mod uint128 {
 85    pub type UInt128<F> = super::uint::UInt<128, u128, F>;
 86}
 87
 88#[allow(missing_docs)]
 89pub mod prelude {
 90    pub use crate::{
 91        alloc::*,
 92        boolean::Boolean,
 93        eq::*,
 94        fields::{FieldOpsBounds, FieldVar},
 95        groups::{CurveVar, GroupOpsBounds},
 96        pairing::PairingVar,
 97        select::*,
 98        uint128::UInt128,
 99        uint16::UInt16,
100        uint32::UInt32,
101        uint64::UInt64,
102        uint8::UInt8,
103        R1CSVar,
104    };
105}
106
107/// This trait describes some core functionality that is common to high-level
108/// variables, such as `Boolean`s, `FieldVar`s, `GroupVar`s, etc.
109pub trait R1CSVar<F: Field> {
110    /// The type of the "native" value that `Self` represents in the constraint
111    /// system.
112    type Value: core::fmt::Debug + Eq + Clone;
113
114    /// Returns the underlying `ConstraintSystemRef`.
115    ///
116    /// If `self` is a constant value, then this *must* return
117    /// `ark_relations::r1cs::ConstraintSystemRef::None`.
118    fn cs(&self) -> ark_relations::r1cs::ConstraintSystemRef<F>;
119
120    /// Returns `true` if `self` is a circuit-generation-time constant.
121    fn is_constant(&self) -> bool {
122        self.cs().is_none()
123    }
124
125    /// Returns the value that is assigned to `self` in the underlying
126    /// `ConstraintSystem`.
127    fn value(&self) -> Result<Self::Value, ark_relations::r1cs::SynthesisError>;
128}
129
130impl<F: Field, T: R1CSVar<F>> R1CSVar<F> for [T] {
131    type Value = Vec<T::Value>;
132
133    fn cs(&self) -> ark_relations::r1cs::ConstraintSystemRef<F> {
134        let mut result = ark_relations::r1cs::ConstraintSystemRef::None;
135        for var in self {
136            result = var.cs().or(result);
137        }
138        result
139    }
140
141    fn value(&self) -> Result<Self::Value, ark_relations::r1cs::SynthesisError> {
142        let mut result = Vec::new();
143        for var in self {
144            result.push(var.value()?);
145        }
146        Ok(result)
147    }
148}
149
150impl<'a, F: Field, T: 'a + R1CSVar<F>> R1CSVar<F> for &'a T {
151    type Value = T::Value;
152
153    fn cs(&self) -> ark_relations::r1cs::ConstraintSystemRef<F> {
154        (*self).cs()
155    }
156
157    fn value(&self) -> Result<Self::Value, ark_relations::r1cs::SynthesisError> {
158        (*self).value()
159    }
160}
161
162/// A utility trait to convert `Self` to `Result<T, SynthesisErrorA`.>
163pub trait Assignment<T> {
164    /// Converts `self` to `Result`.
165    fn get(self) -> Result<T, ark_relations::r1cs::SynthesisError>;
166}
167
168impl<T> Assignment<T> for Option<T> {
169    fn get(self) -> Result<T, ark_relations::r1cs::SynthesisError> {
170        self.ok_or(ark_relations::r1cs::SynthesisError::AssignmentMissing)
171    }
172}