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