arnaucube
arnaucube/NovaPublic Self-hosted repository
Nova/src/traits/mod.rs
src/traits/mod.rsRaw
  1//! This module defines various traits required by the users of the library to implement.
  2use bellperson::{
  3  gadgets::{boolean::AllocatedBit, num::AllocatedNum},
  4  ConstraintSystem, SynthesisError,
  5};
  6use core::{
  7  fmt::Debug,
  8  ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign},
  9};
 10use ff::{PrimeField, PrimeFieldBits};
 11use merlin::Transcript;
 12use num_bigint::BigInt;
 13
 14/// Represents an element of a group
 15pub trait Group:
 16  Clone
 17  + Copy
 18  + Debug
 19  + Eq
 20  + Sized
 21  + GroupOps
 22  + GroupOpsOwned
 23  + ScalarMul<<Self as Group>::Scalar>
 24  + ScalarMulOwned<<Self as Group>::Scalar>
 25  + Send
 26  + Sync
 27{
 28  /// A type representing an element of the base field of the group
 29  type Base: PrimeField + PrimeFieldBits;
 30
 31  /// A type representing an element of the scalar field of the group
 32  type Scalar: PrimeField + PrimeFieldBits + ChallengeTrait + Send + Sync;
 33
 34  /// A type representing the compressed version of the group element
 35  type CompressedGroupElement: CompressedGroup<GroupElement = Self>;
 36
 37  /// A type representing preprocessed group element
 38  type PreprocessedGroupElement: Clone + Send + Sync;
 39
 40  /// A type that represents a hash function that consumes elements
 41  /// from the base field and squeezes out elements of the scalar field
 42  type RO: ROTrait<Self::Base, Self::Scalar>;
 43
 44  /// An alternate implementation of Self::RO in the circuit model
 45  type ROCircuit: ROCircuitTrait<Self::Base>;
 46
 47  /// A method to compute a multiexponentation
 48  fn vartime_multiscalar_mul(
 49    scalars: &[Self::Scalar],
 50    bases: &[Self::PreprocessedGroupElement],
 51  ) -> Self;
 52
 53  /// Compresses the group element
 54  fn compress(&self) -> Self::CompressedGroupElement;
 55
 56  /// Produces a preprocessed element
 57  fn preprocessed(&self) -> Self::PreprocessedGroupElement;
 58
 59  /// Produce a vector of group elements using a static label
 60  fn from_label(label: &'static [u8], n: usize) -> Vec<Self::PreprocessedGroupElement>;
 61
 62  /// Returns the affine coordinates (x, y, infinty) for the point
 63  fn to_coordinates(&self) -> (Self::Base, Self::Base, bool);
 64
 65  /// Returns the order of the group as a big integer
 66  fn get_order() -> BigInt;
 67
 68  /// Returns an element that is the additive identity of the group
 69  fn zero() -> Self;
 70
 71  /// Returns the generator of the group
 72  fn get_generator() -> Self;
 73}
 74
 75/// Represents a compressed version of a group element
 76pub trait CompressedGroup: Clone + Copy + Debug + Eq + Sized + Send + Sync + 'static {
 77  /// A type that holds the decompressed version of the compressed group element
 78  type GroupElement: Group;
 79
 80  /// Decompresses the compressed group element
 81  fn decompress(&self) -> Option<Self::GroupElement>;
 82
 83  /// Returns a byte array representing the compressed group element
 84  fn as_bytes(&self) -> &[u8];
 85}
 86
 87/// A helper trait to append different types to the transcript
 88pub trait AppendToTranscriptTrait {
 89  /// appends the value to the transcript under the provided label
 90  fn append_to_transcript(&self, label: &'static [u8], transcript: &mut Transcript);
 91}
 92
 93/// A helper trait to absorb different objects in RO
 94pub trait AbsorbInROTrait<G: Group> {
 95  /// Absorbs the value in the provided RO
 96  fn absorb_in_ro(&self, ro: &mut G::RO);
 97}
 98
 99/// A helper trait to generate challenges using a transcript object
100pub trait ChallengeTrait {
101  /// Returns a Scalar representing the challenge using the transcript
102  fn challenge(label: &'static [u8], transcript: &mut Transcript) -> Self;
103}
104
105/// A helper trait that defines the behavior of a hash function that we use as an RO
106pub trait ROTrait<Base, Scalar> {
107  /// A type representing constants/parameters associated with the hash function
108  type Constants: ROConstantsTrait<Base> + Clone + Send + Sync;
109
110  /// Initializes the hash function
111  fn new(constants: Self::Constants, num_absorbs: usize) -> Self;
112
113  /// Adds a scalar to the internal state
114  fn absorb(&mut self, e: Base);
115
116  /// Returns a challenge of `num_bits` by hashing the internal state
117  fn squeeze(&mut self, num_bits: usize) -> Scalar;
118}
119
120/// A helper trait that defines the behavior of a hash function that we use as an RO in the circuit model
121pub trait ROCircuitTrait<Base: PrimeField> {
122  /// A type representing constants/parameters associated with the hash function
123  type Constants: ROConstantsTrait<Base> + Clone + Send + Sync;
124
125  /// Initializes the hash function
126  fn new(constants: Self::Constants, num_absorbs: usize) -> Self;
127
128  /// Adds a scalar to the internal state
129  fn absorb(&mut self, e: AllocatedNum<Base>);
130
131  /// Returns a challenge of `num_bits` by hashing the internal state
132  fn squeeze<CS>(&mut self, cs: CS, num_bits: usize) -> Result<Vec<AllocatedBit>, SynthesisError>
133  where
134    CS: ConstraintSystem<Base>;
135}
136
137/// A helper trait that defines the constants associated with a hash function
138pub trait ROConstantsTrait<Base> {
139  /// produces constants/parameters associated with the hash function
140  fn new() -> Self;
141}
142
143/// An alias for constants associated with G::RO
144pub type ROConstants<G> =
145  <<G as Group>::RO as ROTrait<<G as Group>::Base, <G as Group>::Scalar>>::Constants;
146
147/// An alias for constants associated with G::ROCircuit
148pub type ROConstantsCircuit<G> =
149  <<G as Group>::ROCircuit as ROCircuitTrait<<G as Group>::Base>>::Constants;
150
151/// A helper trait for types with a group operation.
152pub trait GroupOps<Rhs = Self, Output = Self>:
153  Add<Rhs, Output = Output> + Sub<Rhs, Output = Output> + AddAssign<Rhs> + SubAssign<Rhs>
154{
155}
156
157impl<T, Rhs, Output> GroupOps<Rhs, Output> for T where
158  T: Add<Rhs, Output = Output> + Sub<Rhs, Output = Output> + AddAssign<Rhs> + SubAssign<Rhs>
159{
160}
161
162/// A helper trait for references with a group operation.
163pub trait GroupOpsOwned<Rhs = Self, Output = Self>: for<'r> GroupOps<&'r Rhs, Output> {}
164impl<T, Rhs, Output> GroupOpsOwned<Rhs, Output> for T where T: for<'r> GroupOps<&'r Rhs, Output> {}
165
166/// A helper trait for types implementing group scalar multiplication.
167pub trait ScalarMul<Rhs, Output = Self>: Mul<Rhs, Output = Output> + MulAssign<Rhs> {}
168
169impl<T, Rhs, Output> ScalarMul<Rhs, Output> for T where T: Mul<Rhs, Output = Output> + MulAssign<Rhs>
170{}
171
172/// A helper trait for references implementing group scalar multiplication.
173pub trait ScalarMulOwned<Rhs, Output = Self>: for<'r> ScalarMul<&'r Rhs, Output> {}
174impl<T, Rhs, Output> ScalarMulOwned<Rhs, Output> for T where T: for<'r> ScalarMul<&'r Rhs, Output> {}
175
176impl<F: PrimeField> AppendToTranscriptTrait for F {
177  fn append_to_transcript(&self, label: &'static [u8], transcript: &mut Transcript) {
178    transcript.append_message(label, self.to_repr().as_ref());
179  }
180}
181
182impl<F: PrimeField> AppendToTranscriptTrait for [F] {
183  fn append_to_transcript(&self, label: &'static [u8], transcript: &mut Transcript) {
184    for s in self {
185      s.append_to_transcript(label, transcript);
186    }
187  }
188}
189
190pub mod circuit;
191pub mod snark;