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 + GroupOps
52 + GroupOpsOwned
53 + ScalarMul<<Self as Group>::Scalar>
54 + ScalarMulOwned<<Self as Group>::Scalar>
55{
56 type Scalar: PrimeField + ChallengeTrait;
57 type CompressedGroupElement: CompressedGroup<GroupElement = Self>;
58
59 fn vartime_multiscalar_mul<I, J>(scalars: I, points: J) -> Self
60 where
61 I: IntoIterator,
62 I::Item: Borrow<Self::Scalar>,
63 J: IntoIterator,
64 J::Item: Borrow<Self>,
65 Self: Clone;
66
67 fn compress(&self) -> Self::CompressedGroupElement;
68
69 fn from_uniform_bytes(bytes: &[u8]) -> Option<Self>;
70}
71
72/// Represents a compressed version of a group element
73pub trait CompressedGroup: Clone + Copy + Debug + Eq + Sized + Send + Sync + 'static {
74 type GroupElement: Group;
75
76 fn decompress(&self) -> Option<Self::GroupElement>;
77
78 fn as_bytes(&self) -> &[u8];
79}
80
81/// A helper trait to generate challenges using a transcript object
82pub trait ChallengeTrait {
83 fn challenge(label: &'static [u8], transcript: &mut Transcript) -> Self;
84}
85
86/// A helper trait for types with a group operation.
87pub trait GroupOps<Rhs = Self, Output = Self>:
88 Add<Rhs, Output = Output> + Sub<Rhs, Output = Output> + AddAssign<Rhs> + SubAssign<Rhs>
89{
90}
91
92impl<T, Rhs, Output> GroupOps<Rhs, Output> for T where
93 T: Add<Rhs, Output = Output> + Sub<Rhs, Output = Output> + AddAssign<Rhs> + SubAssign<Rhs>
94{
95}
96
97/// A helper trait for references with a group operation.
98pub trait GroupOpsOwned<Rhs = Self, Output = Self>: for<'r> GroupOps<&'r Rhs, Output> {}
99impl<T, Rhs, Output> GroupOpsOwned<Rhs, Output> for T where T: for<'r> GroupOps<&'r Rhs, Output> {}
100
101/// A helper trait for types implementing group scalar multiplication.
102pub trait ScalarMul<Rhs, Output = Self>: Mul<Rhs, Output = Output> + MulAssign<Rhs> {}
103
104impl<T, Rhs, Output> ScalarMul<Rhs, Output> for T where T: Mul<Rhs, Output = Output> + MulAssign<Rhs>
105{}
106
107/// A helper trait for references implementing group scalar multiplication.
108pub trait ScalarMulOwned<Rhs, Output = Self>: for<'r> ScalarMul<&'r Rhs, Output> {}
109impl<T, Rhs, Output> ScalarMulOwned<Rhs, Output> for T where T: for<'r> ScalarMul<&'r Rhs, Output> {}