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  /// Splits the commitment key into two pieces at a specified point
 34  fn split_at(&self, n: usize) -> (Self, Self)
 35  where
 36    Self: Sized;
 37
 38  /// Combines two commitment keys into one
 39  fn combine(&self, other: &Self) -> Self;
 40
 41  /// Folds the two commitment keys into one using the provided weights
 42  fn fold(&self, w1: &G::Scalar, w2: &G::Scalar) -> Self;
 43
 44  /// Scales the commitment key using the provided scalar
 45  fn scale(&self, r: &G::Scalar) -> Self;
 46
 47  /// Reinterprets commitments as commitment keys
 48  fn reinterpret_commitments_as_gens(c: &[Self::CompressedCommitment]) -> Result<Self, NovaError>
 49  where
 50    Self: Sized;
 51}
 52
 53/// Defines basic operations on commitments
 54pub trait CommitmentOps<Rhs = Self, Output = Self>:
 55  Add<Rhs, Output = Output> + AddAssign<Rhs>
 56{
 57}
 58
 59impl<T, Rhs, Output> CommitmentOps<Rhs, Output> for T where
 60  T: Add<Rhs, Output = Output> + AddAssign<Rhs>
 61{
 62}
 63
 64/// A helper trait for references with a commitment operation
 65pub trait CommitmentOpsOwned<Rhs = Self, Output = Self>:
 66  for<'r> CommitmentOps<&'r Rhs, Output>
 67{
 68}
 69impl<T, Rhs, Output> CommitmentOpsOwned<Rhs, Output> for T where
 70  T: for<'r> CommitmentOps<&'r Rhs, Output>
 71{
 72}
 73
 74/// A helper trait for types implementing a multiplication of a commitment with a scalar
 75pub trait ScalarMul<Rhs, Output = Self>: Mul<Rhs, Output = Output> + MulAssign<Rhs> {}
 76
 77impl<T, Rhs, Output> ScalarMul<Rhs, Output> for T where T: Mul<Rhs, Output = Output> + MulAssign<Rhs>
 78{}
 79
 80/// This trait defines the behavior of the commitment
 81pub trait CommitmentTrait<G: Group>:
 82  Clone
 83  + Copy
 84  + Debug
 85  + Default
 86  + PartialEq
 87  + Eq
 88  + Send
 89  + Sync
 90  + Serialize
 91  + for<'de> Deserialize<'de>
 92  + AbsorbInROTrait<G>
 93  + AppendToTranscriptTrait
 94  + CommitmentOps
 95  + CommitmentOpsOwned
 96  + ScalarMul<G::Scalar>
 97{
 98  /// Holds the type of the compressed commitment
 99  type CompressedCommitment;
100
101  /// Compresses self into a compressed commitment
102  fn compress(&self) -> Self::CompressedCommitment;
103
104  /// Returns the coordinate representation of the commitment
105  fn to_coordinates(&self) -> (G::Base, G::Base, bool);
106}
107
108/// This trait defines the behavior of a compressed commitment
109pub trait CompressedCommitmentTrait<C: CompressedGroup>:
110  Clone
111  + Debug
112  + PartialEq
113  + Eq
114  + Send
115  + Sync
116  + Serialize
117  + for<'de> Deserialize<'de>
118  + AppendToTranscriptTrait
119{
120  /// Holds the type of the commitment that can be decompressed into
121  type Commitment;
122
123  /// Decompresses self into a commitment
124  fn decompress(&self) -> Result<Self::Commitment, NovaError>;
125}
126
127/// A trait that ties different pieces of the commitment generation together
128pub trait CommitmentEngineTrait<G: Group>:
129  Clone + Send + Sync + Serialize + for<'de> Deserialize<'de>
130{
131  /// Holds the type of the commitment key
132  type CommitmentGens: CommitmentGensTrait<
133    G,
134    Commitment = Self::Commitment,
135    CompressedCommitment = Self::CompressedCommitment,
136  >;
137
138  /// Holds the type of the commitment
139  type Commitment: CommitmentTrait<G, CompressedCommitment = Self::CompressedCommitment>;
140
141  /// Holds the type of the compressed commitment
142  type CompressedCommitment: CompressedCommitmentTrait<
143    G::CompressedGroupElement,
144    Commitment = Self::Commitment,
145  >;
146
147  /// Commits to the provided vector using the provided generators
148  fn commit(gens: &Self::CommitmentGens, v: &[G::Scalar]) -> Self::Commitment;
149}