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 core::borrow::Borrow;
  3use core::fmt::Debug;
  4use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
  5use merlin::Transcript;
  6use rand::{CryptoRng, RngCore};
  7
  8/// Represents an element of a prime field
  9pub trait PrimeField:
 10  Sized
 11  + Eq
 12  + Copy
 13  + Clone
 14  + Default
 15  + Send
 16  + Sync
 17  + Debug
 18  + Add<Output = Self>
 19  + Sub<Output = Self>
 20  + Mul<Output = Self>
 21  + Neg<Output = Self>
 22  + for<'a> Add<&'a Self, Output = Self>
 23  + for<'a> Mul<&'a Self, Output = Self>
 24  + for<'a> Sub<&'a Self, Output = Self>
 25  + AddAssign
 26  + MulAssign
 27  + SubAssign
 28  + for<'a> AddAssign<&'a Self>
 29  + for<'a> MulAssign<&'a Self>
 30  + for<'a> SubAssign<&'a Self>
 31{
 32  /// returns the additive identity of the field
 33  fn zero() -> Self;
 34
 35  /// returns the multiplicative identity of the field
 36  fn one() -> Self;
 37
 38  /// converts the supplied bytes into an element of the field
 39  fn from_bytes_mod_order_wide(bytes: &[u8]) -> Option<Self>;
 40
 41  /// returns an uniformly random element from the finite field
 42  fn random(rng: &mut (impl RngCore + CryptoRng)) -> Self;
 43}
 44
 45/// Represents an element of a group
 46pub trait Group:
 47  Clone
 48  + Copy
 49  + Debug
 50  + Eq
 51  + Sized
 52  + GroupOps
 53  + GroupOpsOwned
 54  + ScalarMul<<Self as Group>::Scalar>
 55  + ScalarMulOwned<<Self as Group>::Scalar>
 56{
 57  /// A type representing an element of the scalar field of the group
 58  type Scalar: PrimeField + ChallengeTrait;
 59
 60  /// A type representing the compressed version of the group element
 61  type CompressedGroupElement: CompressedGroup<GroupElement = Self>;
 62
 63  /// A method to compute a multiexponentation
 64  fn vartime_multiscalar_mul<I, J>(scalars: I, points: J) -> Self
 65  where
 66    I: IntoIterator,
 67    I::Item: Borrow<Self::Scalar>,
 68    J: IntoIterator,
 69    J::Item: Borrow<Self>,
 70    Self: Clone;
 71
 72  /// Compresses the group element
 73  fn compress(&self) -> Self::CompressedGroupElement;
 74
 75  /// Attempts to create a group element from a sequence of bytes, 
 76  /// failing with a `None` if the supplied bytes do not encode the group element
 77  fn from_uniform_bytes(bytes: &[u8]) -> Option<Self>;
 78}
 79
 80/// Represents a compressed version of a group element
 81pub trait CompressedGroup: Clone + Copy + Debug + Eq + Sized + Send + Sync + 'static {
 82  /// A type that holds the decompressed version of the compressed group element
 83  type GroupElement: Group;
 84
 85  /// Decompresses the compressed group element
 86  fn decompress(&self) -> Option<Self::GroupElement>;
 87
 88  /// Returns a byte array representing the compressed group element
 89  fn as_bytes(&self) -> &[u8];
 90}
 91
 92/// A helper trait to generate challenges using a transcript object
 93pub trait ChallengeTrait {
 94  /// Returns a Scalar representing the challenge using the transcript
 95  fn challenge(label: &'static [u8], transcript: &mut Transcript) -> Self;
 96}
 97
 98/// A helper trait for types with a group operation.
 99pub trait GroupOps<Rhs = Self, Output = Self>:
100  Add<Rhs, Output = Output> + Sub<Rhs, Output = Output> + AddAssign<Rhs> + SubAssign<Rhs>
101{
102}
103
104impl<T, Rhs, Output> GroupOps<Rhs, Output> for T where
105  T: Add<Rhs, Output = Output> + Sub<Rhs, Output = Output> + AddAssign<Rhs> + SubAssign<Rhs>
106{
107}
108
109/// A helper trait for references with a group operation.
110pub trait GroupOpsOwned<Rhs = Self, Output = Self>: for<'r> GroupOps<&'r Rhs, Output> {}
111impl<T, Rhs, Output> GroupOpsOwned<Rhs, Output> for T where T: for<'r> GroupOps<&'r Rhs, Output> {}
112
113/// A helper trait for types implementing group scalar multiplication.
114pub trait ScalarMul<Rhs, Output = Self>: Mul<Rhs, Output = Output> + MulAssign<Rhs> {}
115
116impl<T, Rhs, Output> ScalarMul<Rhs, Output> for T where T: Mul<Rhs, Output = Output> + MulAssign<Rhs>
117{}
118
119/// A helper trait for references implementing group scalar multiplication.
120pub trait ScalarMulOwned<Rhs, Output = Self>: for<'r> ScalarMul<&'r Rhs, Output> {}
121impl<T, Rhs, Output> ScalarMulOwned<Rhs, Output> for T where T: for<'r> ScalarMul<&'r Rhs, Output> {}