arnaucube
arnaucube/NovaPublic Self-hosted repository
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, Mul, MulAssign},
10};
11use serde::{Deserialize, Serialize};
12
13/// Defines basic operations on commitments
14pub trait CommitmentOps<Rhs = Self, Output = Self>:
15  Add<Rhs, Output = Output> + AddAssign<Rhs>
16{
17}
18
19impl<T, Rhs, Output> CommitmentOps<Rhs, Output> for T where
20  T: Add<Rhs, Output = Output> + AddAssign<Rhs>
21{
22}
23
24/// A helper trait for references with a commitment operation
25pub trait CommitmentOpsOwned<Rhs = Self, Output = Self>:
26  for<'r> CommitmentOps<&'r Rhs, Output>
27{
28}
29impl<T, Rhs, Output> CommitmentOpsOwned<Rhs, Output> for T where
30  T: for<'r> CommitmentOps<&'r Rhs, Output>
31{
32}
33
34/// A helper trait for types implementing a multiplication of a commitment with a scalar
35pub trait ScalarMul<Rhs, Output = Self>: Mul<Rhs, Output = Output> + MulAssign<Rhs> {}
36
37impl<T, Rhs, Output> ScalarMul<Rhs, Output> for T where T: Mul<Rhs, Output = Output> + MulAssign<Rhs>
38{}
39
40/// This trait defines the behavior of the commitment
41pub trait CommitmentTrait<G: Group>:
42  Clone
43  + Copy
44  + Debug
45  + Default
46  + PartialEq
47  + Eq
48  + Send
49  + Sync
50  + TranscriptReprTrait<G>
51  + Serialize
52  + for<'de> Deserialize<'de>
53  + AbsorbInROTrait<G>
54  + CommitmentOps
55  + CommitmentOpsOwned
56  + ScalarMul<G::Scalar>
57{
58  /// Holds the type of the compressed commitment
59  type CompressedCommitment: Clone
60    + Debug
61    + PartialEq
62    + Eq
63    + Send
64    + Sync
65    + TranscriptReprTrait<G>
66    + Serialize
67    + for<'de> Deserialize<'de>;
68
69  /// Compresses self into a compressed commitment
70  fn compress(&self) -> Self::CompressedCommitment;
71
72  /// Returns the coordinate representation of the commitment
73  fn to_coordinates(&self) -> (G::Base, G::Base, bool);
74
75  /// Decompresses a compressed commitment into a commitment
76  fn decompress(c: &Self::CompressedCommitment) -> Result<Self, NovaError>;
77}
78
79/// A trait that ties different pieces of the commitment generation together
80pub trait CommitmentEngineTrait<G: Group>:
81  Clone + Send + Sync + Serialize + for<'de> Deserialize<'de>
82{
83  /// Holds the type of the commitment key
84  type CommitmentKey: Clone + Debug + Send + Sync + Serialize + for<'de> Deserialize<'de>;
85
86  /// Holds the type of the commitment
87  type Commitment: CommitmentTrait<G>;
88
89  /// Samples a new commitment key of a specified size
90  fn setup(label: &'static [u8], n: usize) -> Self::CommitmentKey;
91
92  /// Commits to the provided vector using the provided generators
93  fn commit(ck: &Self::CommitmentKey, v: &[G::Scalar]) -> Self::Commitment;
94}