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 describes traits for conditionally selecting a variable from a
 53/// list of variables.
 54pub mod select;
 55
 56#[allow(missing_docs)]
 57pub mod prelude {
 58    pub use crate::{
 59        alloc::*,
 60        bits::{boolean::Boolean, uint32::UInt32, uint8::UInt8, ToBitsGadget, ToBytesGadget},
 61        eq::*,
 62        fields::{FieldOpsBounds, FieldVar},
 63        groups::{CurveVar, GroupOpsBounds},
 64        pairing::PairingVar,
 65        select::*,
 66        R1CSVar,
 67    };
 68}
 69
 70/// This trait describes some core functionality that is common to high-level
 71/// variables, such as `Boolean`s, `FieldVar`s, `GroupVar`s, etc.
 72pub trait R1CSVar<F: Field> {
 73    /// The type of the "native" value that `Self` represents in the constraint
 74    /// system.
 75    type Value: core::fmt::Debug + Eq + Clone;
 76
 77    /// Returns the underlying `ConstraintSystemRef`.
 78    ///
 79    /// If `self` is a constant value, then this *must* return
 80    /// `ark_relations::r1cs::ConstraintSystemRef::None`.
 81    fn cs(&self) -> ark_relations::r1cs::ConstraintSystemRef<F>;
 82
 83    /// Returns `true` if `self` is a circuit-generation-time constant.
 84    fn is_constant(&self) -> bool {
 85        self.cs().is_none()
 86    }
 87
 88    /// Returns the value that is assigned to `self` in the underlying
 89    /// `ConstraintSystem`.
 90    fn value(&self) -> Result<Self::Value, ark_relations::r1cs::SynthesisError>;
 91}
 92
 93impl<F: Field, T: R1CSVar<F>> R1CSVar<F> for [T] {
 94    type Value = Vec<T::Value>;
 95
 96    fn cs(&self) -> ark_relations::r1cs::ConstraintSystemRef<F> {
 97        let mut result = ark_relations::r1cs::ConstraintSystemRef::None;
 98        for var in self {
 99            result = var.cs().or(result);
100        }
101        result
102    }
103
104    fn value(&self) -> Result<Self::Value, ark_relations::r1cs::SynthesisError> {
105        let mut result = Vec::new();
106        for var in self {
107            result.push(var.value()?);
108        }
109        Ok(result)
110    }
111}
112
113impl<'a, F: Field, T: 'a + R1CSVar<F>> R1CSVar<F> for &'a T {
114    type Value = T::Value;
115
116    fn cs(&self) -> ark_relations::r1cs::ConstraintSystemRef<F> {
117        (*self).cs()
118    }
119
120    fn value(&self) -> Result<Self::Value, ark_relations::r1cs::SynthesisError> {
121        (*self).value()
122    }
123}
124
125/// A utility trait to convert `Self` to `Result<T, SynthesisErrorA`.>
126pub trait Assignment<T> {
127    /// Converts `self` to `Result`.
128    fn get(self) -> Result<T, ark_relations::r1cs::SynthesisError>;
129}
130
131impl<T> Assignment<T> for Option<T> {
132    fn get(self) -> Result<T, ark_relations::r1cs::SynthesisError> {
133        self.ok_or(ark_relations::r1cs::SynthesisError::AssignmentMissing)
134    }
135}
136
137/// Specifies how to convert a variable of type `Self` to variables of
138/// type `FpVar<ConstraintF>`
139pub trait ToConstraintFieldGadget<ConstraintF: ark_ff::PrimeField> {
140    /// Converts `self` to `FpVar<ConstraintF>` variables.
141    fn to_constraint_field(
142        &self,
143    ) -> Result<Vec<crate::fields::fp::FpVar<ConstraintF>>, ark_relations::r1cs::SynthesisError>;
144}