Nova/src/traits/commitment.rs
src/traits/commitment.rsRaw
1//! This module defines a collection of traits that define the behavior of a commitment engine
2//! We require the commitment engine to provide a commitment to vectors with a single group element
3use crate::{
4 errors::NovaError,
5 traits::{AbsorbInROTrait, Group, TranscriptReprTrait},
6};
7use core::{
8 fmt::Debug,
9 ops::{Add, AddAssign},
10};
11use serde::{Deserialize, Serialize};
12
13use super::ScalarMul;
14
15/// Defines basic operations on commitments
16pub trait CommitmentOps<Rhs = Self, Output = Self>:
17 Add<Rhs, Output = Output> + AddAssign<Rhs>
18{
19}
20
21impl<T, Rhs, Output> CommitmentOps<Rhs, Output> for T where
22 T: Add<Rhs, Output = Output> + AddAssign<Rhs>
23{
24}
25
26/// A helper trait for references with a commitment operation
27pub trait CommitmentOpsOwned<Rhs = Self, Output = Self>:
28 for<'r> CommitmentOps<&'r Rhs, Output>
29{
30}
31impl<T, Rhs, Output> CommitmentOpsOwned<Rhs, Output> for T where
32 T: for<'r> CommitmentOps<&'r Rhs, Output>
33{
34}
35
36/// This trait defines the behavior of the commitment
37pub trait CommitmentTrait<G: Group>:
38 Clone
39 + Copy
40 + Debug
41 + Default
42 + PartialEq
43 + Eq
44 + Send
45 + Sync
46 + TranscriptReprTrait<G>
47 + Serialize
48 + for<'de> Deserialize<'de>
49 + AbsorbInROTrait<G>
50 + CommitmentOps
51 + CommitmentOpsOwned
52 + ScalarMul<G::Scalar>
53{
54 /// Holds the type of the compressed commitment
55 type CompressedCommitment: Clone
56 + Debug
57 + PartialEq
58 + Eq
59 + Send
60 + Sync
61 + TranscriptReprTrait<G>
62 + Serialize
63 + for<'de> Deserialize<'de>;
64
65 /// Compresses self into a compressed commitment
66 fn compress(&self) -> Self::CompressedCommitment;
67
68 /// Returns the coordinate representation of the commitment
69 fn to_coordinates(&self) -> (G::Base, G::Base, bool);
70
71 /// Decompresses a compressed commitment into a commitment
72 fn decompress(c: &Self::CompressedCommitment) -> Result<Self, NovaError>;
73}
74
75/// A trait that ties different pieces of the commitment generation together
76pub trait CommitmentEngineTrait<G: Group>:
77 Clone + Send + Sync + Serialize + for<'de> Deserialize<'de>
78{
79 /// Holds the type of the commitment key
80 type CommitmentKey: Clone + Debug + Send + Sync + Serialize + for<'de> Deserialize<'de>;
81
82 /// Holds the type of the commitment
83 type Commitment: CommitmentTrait<G>;
84
85 /// Samples a new commitment key of a specified size
86 fn setup(label: &'static [u8], n: usize) -> Self::CommitmentKey;
87
88 /// Commits to the provided vector using the provided generators
89 fn commit(ck: &Self::CommitmentKey, v: &[G::Scalar]) -> Self::Commitment;
90}