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        convert::{ToBitsGadget, ToBytesGadget},
 94        eq::*,
 95        fields::{FieldOpsBounds, FieldVar},
 96        groups::{CurveVar, GroupOpsBounds},
 97        pairing::PairingVar,
 98        select::*,
 99        uint128::UInt128,
100        uint16::UInt16,
101        uint32::UInt32,
102        uint64::UInt64,
103        uint8::UInt8,
104        R1CSVar,
105    };
106}
107
108/// This trait describes some core functionality that is common to high-level
109/// variables, such as `Boolean`s, `FieldVar`s, `GroupVar`s, etc.
110pub trait R1CSVar<F: Field> {
111    /// The type of the "native" value that `Self` represents in the constraint
112    /// system.
113    type Value: core::fmt::Debug + Eq + Clone;
114
115    /// Returns the underlying `ConstraintSystemRef`.
116    ///
117    /// If `self` is a constant value, then this *must* return
118    /// `ark_relations::r1cs::ConstraintSystemRef::None`.
119    fn cs(&self) -> ark_relations::r1cs::ConstraintSystemRef<F>;
120
121    /// Returns `true` if `self` is a circuit-generation-time constant.
122    fn is_constant(&self) -> bool {
123        self.cs().is_none()
124    }
125
126    /// Returns the value that is assigned to `self` in the underlying
127    /// `ConstraintSystem`.
128    fn value(&self) -> Result<Self::Value, ark_relations::r1cs::SynthesisError>;
129}
130
131impl<F: Field, T: R1CSVar<F>> R1CSVar<F> for [T] {
132    type Value = Vec<T::Value>;
133
134    fn cs(&self) -> ark_relations::r1cs::ConstraintSystemRef<F> {
135        let mut result = ark_relations::r1cs::ConstraintSystemRef::None;
136        for var in self {
137            result = var.cs().or(result);
138        }
139        result
140    }
141
142    fn value(&self) -> Result<Self::Value, ark_relations::r1cs::SynthesisError> {
143        let mut result = Vec::new();
144        for var in self {
145            result.push(var.value()?);
146        }
147        Ok(result)
148    }
149}
150
151impl<'a, F: Field, T: 'a + R1CSVar<F>> R1CSVar<F> for &'a T {
152    type Value = T::Value;
153
154    fn cs(&self) -> ark_relations::r1cs::ConstraintSystemRef<F> {
155        (*self).cs()
156    }
157
158    fn value(&self) -> Result<Self::Value, ark_relations::r1cs::SynthesisError> {
159        (*self).value()
160    }
161}
162
163/// A utility trait to convert `Self` to `Result<T, SynthesisErrorA`.>
164pub trait Assignment<T> {
165    /// Converts `self` to `Result`.
166    fn get(self) -> Result<T, ark_relations::r1cs::SynthesisError>;
167}
168
169impl<T> Assignment<T> for Option<T> {
170    fn get(self) -> Result<T, ark_relations::r1cs::SynthesisError> {
171        self.ok_or(ark_relations::r1cs::SynthesisError::AssignmentMissing)
172    }
173}