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