//! This module defines a collection of traits that define the behavior of a commitment engine //! We require the commitment engine to provide a commitment to vectors with a single group element use crate::{ errors::NovaError, traits::{AbsorbInROTrait, AppendToTranscriptTrait, CompressedGroup, Group}, }; use core::{ fmt::Debug, ops::{Add, AddAssign, Mul, MulAssign}, }; use serde::{Deserialize, Serialize}; /// This trait defines the behavior of commitment key #[allow(clippy::len_without_is_empty)] pub trait CommitmentGensTrait: Clone + Debug + Send + Sync + Serialize + for<'de> Deserialize<'de> { /// Holds the type of the commitment that can be produced type Commitment; /// Holds the type of the compressed commitment type CompressedCommitment; /// Samples a new commitment key of a specified size fn new(label: &'static [u8], n: usize) -> Self; /// Returns the vector length that can be committed fn len(&self) -> usize; /// Commits to a vector using the commitment key fn commit(&self, v: &[G::Scalar]) -> Self::Commitment; /// Splits the commitment key into two pieces at a specified point fn split_at(&self, n: usize) -> (Self, Self) where Self: Sized; /// Combines two commitment keys into one fn combine(&self, other: &Self) -> Self; /// Folds the two commitment keys into one using the provided weights fn fold(&self, w1: &G::Scalar, w2: &G::Scalar) -> Self; /// Scales the commitment key using the provided scalar fn scale(&self, r: &G::Scalar) -> Self; /// Reinterprets commitments as commitment keys fn reinterpret_commitments_as_gens(c: &[Self::CompressedCommitment]) -> Result where Self: Sized; } /// Defines basic operations on commitments pub trait CommitmentOps: Add + AddAssign { } impl CommitmentOps for T where T: Add + AddAssign { } /// A helper trait for references with a commitment operation pub trait CommitmentOpsOwned: for<'r> CommitmentOps<&'r Rhs, Output> { } impl CommitmentOpsOwned for T where T: for<'r> CommitmentOps<&'r Rhs, Output> { } /// A helper trait for types implementing a multiplication of a commitment with a scalar pub trait ScalarMul: Mul + MulAssign {} impl ScalarMul for T where T: Mul + MulAssign {} /// This trait defines the behavior of the commitment pub trait CommitmentTrait: Clone + Copy + Debug + Default + PartialEq + Eq + Send + Sync + Serialize + for<'de> Deserialize<'de> + AbsorbInROTrait + AppendToTranscriptTrait + CommitmentOps + CommitmentOpsOwned + ScalarMul { /// Holds the type of the compressed commitment type CompressedCommitment; /// Compresses self into a compressed commitment fn compress(&self) -> Self::CompressedCommitment; /// Returns the coordinate representation of the commitment fn to_coordinates(&self) -> (G::Base, G::Base, bool); } /// This trait defines the behavior of a compressed commitment pub trait CompressedCommitmentTrait: Clone + Debug + PartialEq + Eq + Send + Sync + Serialize + for<'de> Deserialize<'de> + AppendToTranscriptTrait { /// Holds the type of the commitment that can be decompressed into type Commitment; /// Decompresses self into a commitment fn decompress(&self) -> Result; } /// A trait that ties different pieces of the commitment generation together pub trait CommitmentEngineTrait: Clone + Send + Sync + Serialize + for<'de> Deserialize<'de> { /// Holds the type of the commitment key type CommitmentGens: CommitmentGensTrait< G, Commitment = Self::Commitment, CompressedCommitment = Self::CompressedCommitment, >; /// Holds the type of the commitment type Commitment: CommitmentTrait; /// Holds the type of the compressed commitment type CompressedCommitment: CompressedCommitmentTrait< G::CompressedGroupElement, Commitment = Self::Commitment, >; /// Commits to the provided vector using the provided generators fn commit(gens: &Self::CommitmentGens, v: &[G::Scalar]) -> Self::Commitment; }