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