arnaucube
arnaucube/NovaPublic Self-hosted repository
Nova/src/traits.rs
src/traits.rsRaw
  1//! This module defines various traits required by the users of the library to implement.
  2use bellperson::{gadgets::num::AllocatedNum, ConstraintSystem, SynthesisError};
  3use core::{
  4  fmt::Debug,
  5  ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign},
  6};
  7use ff::{PrimeField, PrimeFieldBits};
  8use merlin::Transcript;
  9use rug::Integer;
 10
 11/// Represents an element of a group
 12pub trait Group:
 13  Clone
 14  + Copy
 15  + Debug
 16  + Eq
 17  + Sized
 18  + GroupOps
 19  + GroupOpsOwned
 20  + ScalarMul<<Self as Group>::Scalar>
 21  + ScalarMulOwned<<Self as Group>::Scalar>
 22{
 23  /// A type representing an element of the base field of the group
 24  type Base: PrimeField + PrimeFieldBits;
 25
 26  /// A type representing an element of the scalar field of the group
 27  type Scalar: PrimeField + PrimeFieldBits + ChallengeTrait;
 28
 29  /// A type representing the compressed version of the group element
 30  type CompressedGroupElement: CompressedGroup<GroupElement = Self>;
 31
 32  /// A type representing preprocessed group element
 33  type PreprocessedGroupElement;
 34
 35  /// A method to compute a multiexponentation
 36  fn vartime_multiscalar_mul(
 37    scalars: &[Self::Scalar],
 38    bases: &[Self::PreprocessedGroupElement],
 39  ) -> Self;
 40
 41  /// Compresses the group element
 42  fn compress(&self) -> Self::CompressedGroupElement;
 43
 44  /// Attempts to create a group element from a sequence of bytes,
 45  /// failing with a `None` if the supplied bytes do not encode the group element
 46  fn from_uniform_bytes(bytes: &[u8]) -> Option<Self::PreprocessedGroupElement>;
 47
 48  /// Returns the affine coordinates (x, y, infinty) for the point
 49  fn to_coordinates(&self) -> (Self::Base, Self::Base, bool);
 50
 51  /// Returns the order of the group as a big integer
 52  fn get_order() -> Integer;
 53}
 54
 55/// Represents a compressed version of a group element
 56pub trait CompressedGroup: Clone + Copy + Debug + Eq + Sized + Send + Sync + 'static {
 57  /// A type that holds the decompressed version of the compressed group element
 58  type GroupElement: Group;
 59
 60  /// Decompresses the compressed group element
 61  fn decompress(&self) -> Option<Self::GroupElement>;
 62
 63  /// Returns a byte array representing the compressed group element
 64  fn as_bytes(&self) -> &[u8];
 65}
 66
 67/// A helper trait to generate challenges using a transcript object
 68pub trait ChallengeTrait {
 69  /// Returns a Scalar representing the challenge using the transcript
 70  fn challenge(label: &'static [u8], transcript: &mut Transcript) -> Self;
 71}
 72
 73/// A helper trait for types with a group operation.
 74pub trait GroupOps<Rhs = Self, Output = Self>:
 75  Add<Rhs, Output = Output> + Sub<Rhs, Output = Output> + AddAssign<Rhs> + SubAssign<Rhs>
 76{
 77}
 78
 79impl<T, Rhs, Output> GroupOps<Rhs, Output> for T where
 80  T: Add<Rhs, Output = Output> + Sub<Rhs, Output = Output> + AddAssign<Rhs> + SubAssign<Rhs>
 81{
 82}
 83
 84/// A helper trait for references with a group operation.
 85pub trait GroupOpsOwned<Rhs = Self, Output = Self>: for<'r> GroupOps<&'r Rhs, Output> {}
 86impl<T, Rhs, Output> GroupOpsOwned<Rhs, Output> for T where T: for<'r> GroupOps<&'r Rhs, Output> {}
 87
 88/// A helper trait for types implementing group scalar multiplication.
 89pub trait ScalarMul<Rhs, Output = Self>: Mul<Rhs, Output = Output> + MulAssign<Rhs> {}
 90
 91impl<T, Rhs, Output> ScalarMul<Rhs, Output> for T where T: Mul<Rhs, Output = Output> + MulAssign<Rhs>
 92{}
 93
 94/// A helper trait for references implementing group scalar multiplication.
 95pub trait ScalarMulOwned<Rhs, Output = Self>: for<'r> ScalarMul<&'r Rhs, Output> {}
 96impl<T, Rhs, Output> ScalarMulOwned<Rhs, Output> for T where T: for<'r> ScalarMul<&'r Rhs, Output> {}
 97
 98/// A helper trait for a step of the incremental computation (i.e., circuit for F)
 99pub trait StepCircuit<F: PrimeField> {
100  /// Sythesize the circuit for a computation step and return variable
101  /// that corresponds to the output of the step z_{i+1}
102  fn synthesize<CS: ConstraintSystem<F>>(
103    &self,
104    cs: &mut CS,
105    z: AllocatedNum<F>,
106  ) -> Result<AllocatedNum<F>, SynthesisError>;
107}