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