arnaucube
arnaucube/NovaPublic Self-hosted repository
Nova/src/traits.rs
src/traits.rsRaw
 1use core::borrow::Borrow;
 2use core::fmt::Debug;
 3use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
 4use merlin::Transcript;
 5use rand::{CryptoRng, RngCore};
 6
 7/// Represents an element of a prime field
 8pub trait PrimeField:
 9  Sized
10  + Eq
11  + Copy
12  + Clone
13  + Default
14  + Send
15  + Sync
16  + Debug
17  + Add<Output = Self>
18  + Sub<Output = Self>
19  + Mul<Output = Self>
20  + Neg<Output = Self>
21  + for<'a> Add<&'a Self, Output = Self>
22  + for<'a> Mul<&'a Self, Output = Self>
23  + for<'a> Sub<&'a Self, Output = Self>
24  + AddAssign
25  + MulAssign
26  + SubAssign
27  + for<'a> AddAssign<&'a Self>
28  + for<'a> MulAssign<&'a Self>
29  + for<'a> SubAssign<&'a Self>
30{
31  /// returns the additive identity of the field
32  fn zero() -> Self;
33
34  /// returns the multiplicative identity of the field
35  fn one() -> Self;
36
37  /// converts the supplied bytes into an element of the field
38  fn from_bytes_mod_order_wide(bytes: &[u8]) -> Option<Self>;
39
40  /// returns an uniformly random element from the finite field
41  fn random(rng: &mut (impl RngCore + CryptoRng)) -> Self;
42}
43
44/// Represents an element of a group
45pub trait Group:
46  Clone
47  + Copy
48  + Debug
49  + Eq
50  + Sized
51  + Mul<<Self as Group>::Scalar, Output = Self>
52  + MulAssign<<Self as Group>::Scalar>
53  + for<'a> MulAssign<&'a <Self as Group>::Scalar>
54  + for<'a> Mul<&'a <Self as Group>::Scalar, Output = Self>
55  + Add<Self, Output = Self>
56  + AddAssign<Self>
57  + for<'a> AddAssign<&'a Self>
58  + for<'a> Add<&'a Self, Output = Self>
59{
60  type Scalar: PrimeField + ChallengeTrait;
61  type CompressedGroupElement: CompressedGroup;
62
63  fn vartime_multiscalar_mul<I, J>(scalars: I, points: J) -> Self
64  where
65    I: IntoIterator,
66    I::Item: Borrow<Self::Scalar>,
67    J: IntoIterator,
68    J::Item: Borrow<Self>,
69    Self: Clone;
70
71  fn compress(&self) -> Self::CompressedGroupElement;
72
73  fn from_uniform_bytes(bytes: &[u8]) -> Option<Self>;
74}
75
76/// Represents a compressed version of a group element
77pub trait CompressedGroup: Clone + Copy + Debug + Eq + Sized + Send + Sync + 'static {
78  type GroupElement: Group;
79
80  fn decompress(&self) -> Option<Self::GroupElement>;
81
82  fn as_bytes(&self) -> &[u8];
83}
84
85/// A helper trait to generate challenges using a transcript object
86pub trait ChallengeTrait {
87  fn challenge(label: &'static [u8], transcript: &mut Transcript) -> Self;
88}