You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
4.2 KiB

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