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