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, AppendToTranscriptTrait, CompressedGroup, Group},
  6};
  7use core::{
  8  fmt::Debug,
  9  ops::{Add, AddAssign, Mul, MulAssign},
 10};
 11use serde::{Deserialize, Serialize};
 12
 13/// This trait defines the behavior of commitment key
 14#[allow(clippy::len_without_is_empty)]
 15pub trait CommitmentGensTrait<G: Group>:
 16  Clone + Debug + Send + Sync + Serialize + for<'de> Deserialize<'de>
 17{
 18  /// Holds the type of the commitment that can be produced
 19  type Commitment;
 20
 21  /// Holds the type of the compressed commitment
 22  type CompressedCommitment;
 23
 24  /// Samples a new commitment key of a specified size
 25  fn new(label: &'static [u8], n: usize) -> Self;
 26
 27  /// Returns the vector length that can be committed
 28  fn len(&self) -> usize;
 29
 30  /// Commits to a vector using the commitment key
 31  fn commit(&self, v: &[G::Scalar]) -> Self::Commitment;
 32}
 33
 34/// Defines basic operations on commitments
 35pub trait CommitmentOps<Rhs = Self, Output = Self>:
 36  Add<Rhs, Output = Output> + AddAssign<Rhs>
 37{
 38}
 39
 40impl<T, Rhs, Output> CommitmentOps<Rhs, Output> for T where
 41  T: Add<Rhs, Output = Output> + AddAssign<Rhs>
 42{
 43}
 44
 45/// A helper trait for references with a commitment operation
 46pub trait CommitmentOpsOwned<Rhs = Self, Output = Self>:
 47  for<'r> CommitmentOps<&'r Rhs, Output>
 48{
 49}
 50impl<T, Rhs, Output> CommitmentOpsOwned<Rhs, Output> for T where
 51  T: for<'r> CommitmentOps<&'r Rhs, Output>
 52{
 53}
 54
 55/// A helper trait for types implementing a multiplication of a commitment with a scalar
 56pub trait ScalarMul<Rhs, Output = Self>: Mul<Rhs, Output = Output> + MulAssign<Rhs> {}
 57
 58impl<T, Rhs, Output> ScalarMul<Rhs, Output> for T where T: Mul<Rhs, Output = Output> + MulAssign<Rhs>
 59{}
 60
 61/// This trait defines the behavior of the commitment
 62pub trait CommitmentTrait<G: Group>:
 63  Clone
 64  + Copy
 65  + Debug
 66  + Default
 67  + PartialEq
 68  + Eq
 69  + Send
 70  + Sync
 71  + Serialize
 72  + for<'de> Deserialize<'de>
 73  + AbsorbInROTrait<G>
 74  + AppendToTranscriptTrait<G>
 75  + CommitmentOps
 76  + CommitmentOpsOwned
 77  + ScalarMul<G::Scalar>
 78{
 79  /// Holds the type of the compressed commitment
 80  type CompressedCommitment;
 81
 82  /// Compresses self into a compressed commitment
 83  fn compress(&self) -> Self::CompressedCommitment;
 84
 85  /// Returns the coordinate representation of the commitment
 86  fn to_coordinates(&self) -> (G::Base, G::Base, bool);
 87}
 88
 89/// This trait defines the behavior of a compressed commitment
 90pub trait CompressedCommitmentTrait<C: CompressedGroup>:
 91  Clone
 92  + Debug
 93  + PartialEq
 94  + Eq
 95  + Send
 96  + Sync
 97  + Serialize
 98  + for<'de> Deserialize<'de>
 99  + AppendToTranscriptTrait<C::GroupElement>
100{
101  /// Holds the type of the commitment that can be decompressed into
102  type Commitment;
103
104  /// Decompresses self into a commitment
105  fn decompress(&self) -> Result<Self::Commitment, NovaError>;
106}
107
108/// A trait that ties different pieces of the commitment generation together
109pub trait CommitmentEngineTrait<G: Group>:
110  Clone + Send + Sync + Serialize + for<'de> Deserialize<'de>
111{
112  /// Holds the type of the commitment key
113  type CommitmentGens: CommitmentGensTrait<
114    G,
115    Commitment = Self::Commitment,
116    CompressedCommitment = Self::CompressedCommitment,
117  >;
118
119  /// Holds the type of the commitment
120  type Commitment: CommitmentTrait<G, CompressedCommitment = Self::CompressedCommitment>;
121
122  /// Holds the type of the compressed commitment
123  type CompressedCommitment: CompressedCommitmentTrait<
124    G::CompressedGroupElement,
125    Commitment = Self::Commitment,
126  >;
127
128  /// Commits to the provided vector using the provided generators
129  fn commit(gens: &Self::CommitmentGens, v: &[G::Scalar]) -> Self::Commitment;
130}