simplify trait requirements (#143) * simplify trait requirements * update version
@@ -1,6 +1,6 @@
[package] name = "nova-snark" -version = "0.14.0" +version = "0.15.0" authors = ["Srinath Setty <srinath@microsoft.com>"] edition = "2021" description = "Recursive zkSNARKs without trusted setup"
@@ -44,7 +44,7 @@ };
use serde::{Deserialize, Serialize}; use traits::{ circuit::StepCircuit, - commitment::{CommitmentEngineTrait, CompressedCommitmentTrait}, + commitment::{CommitmentEngineTrait, CommitmentTrait}, snark::RelaxedR1CSSNARKTrait, AbsorbInROTrait, Group, ROConstants, ROConstantsCircuit, ROConstantsTrait, ROTrait, };@@ -317,7 +317,7 @@ z0_primary,
Some(r_snark.zi_primary.clone()), Some(r_snark.r_U_secondary.clone()), Some(r_snark.l_u_secondary.clone()), - Some(nifs_secondary.comm_T.decompress()?), + Some(Commitment::<G2>::decompress(&nifs_secondary.comm_T)?), ); let circuit_primary: NovaAugmentedCircuit<G2, C1> = NovaAugmentedCircuit::new(@@ -351,7 +351,7 @@ z0_secondary,
Some(r_snark.zi_secondary.clone()), Some(r_snark.r_U_primary.clone()), Some(l_u_primary.clone()), - Some(nifs_primary.comm_T.decompress()?), + Some(Commitment::<G1>::decompress(&nifs_primary.comm_T)?), ); let circuit_secondary: NovaAugmentedCircuit<G1, C2> = NovaAugmentedCircuit::new(@@ -727,7 +727,7 @@ }
type CommitmentGens<G> = <<G as traits::Group>::CE as CommitmentEngineTrait<G>>::CommitmentGens; type Commitment<G> = <<G as Group>::CE as CommitmentEngineTrait<G>>::Commitment; -type CompressedCommitment<G> = <<G as Group>::CE as CommitmentEngineTrait<G>>::CompressedCommitment; +type CompressedCommitment<G> = <<<G as Group>::CE as CommitmentEngineTrait<G>>::Commitment as CommitmentTrait<G>>::CompressedCommitment; type CE<G> = <G as Group>::CE; #[cfg(test)]
@@ -6,11 +6,8 @@ use crate::{
constants::{NUM_CHALLENGE_BITS, NUM_FE_FOR_RO}, errors::NovaError, r1cs::{R1CSGens, R1CSInstance, R1CSShape, R1CSWitness, RelaxedR1CSInstance, RelaxedR1CSWitness}, - traits::{ - commitment::{CommitmentTrait, CompressedCommitmentTrait}, - AbsorbInROTrait, Group, ROTrait, - }, - CompressedCommitment, + traits::{commitment::CommitmentTrait, AbsorbInROTrait, Group, ROTrait}, + Commitment, CompressedCommitment, }; use core::marker::PhantomData; use serde::{Deserialize, Serialize};@@ -101,7 +98,7 @@ U1.absorb_in_ro(&mut ro);
U2.absorb_in_ro(&mut ro); // append `comm_T` to the transcript and obtain a challenge - let comm_T = self.comm_T.decompress()?; + let comm_T = Commitment::<G>::decompress(&self.comm_T)?; comm_T.absorb_in_ro(&mut ro); // compute a challenge from the RO
@@ -190,8 +190,8 @@ fn decompress(&self) -> Option<$name::Point> {
Some($name_curve::from_bytes(&self.repr).unwrap()) } - fn as_bytes(&self) -> &[u8] { - &self.repr + fn as_bytes(&self) -> Vec<u8> { + self.repr.to_vec() } } };
@@ -2,9 +2,7 @@ //! This module provides an implementation of a commitment engine
use crate::{ errors::NovaError, traits::{ - commitment::{ - CommitmentEngineTrait, CommitmentGensTrait, CommitmentTrait, CompressedCommitmentTrait, - }, + commitment::{CommitmentEngineTrait, CommitmentGensTrait, CommitmentTrait}, AbsorbInROTrait, AppendToTranscriptTrait, CompressedGroup, Group, ROTrait, TranscriptEngineTrait, },@@ -35,13 +33,12 @@
/// A type that holds a compressed commitment #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(bound = "")] -pub struct CompressedCommitment<C: CompressedGroup> { - comm: C, +pub struct CompressedCommitment<G: Group> { + comm: G::CompressedGroupElement, } impl<G: Group> CommitmentGensTrait<G> for CommitmentGens<G> { type Commitment = Commitment<G>; - type CompressedCommitment = CompressedCommitment<G::CompressedGroupElement>; fn new(label: &'static [u8], n: usize) -> Self { CommitmentGens {@@ -63,9 +60,9 @@ }
} impl<G: Group> CommitmentTrait<G> for Commitment<G> { - type CompressedCommitment = CompressedCommitment<G::CompressedGroupElement>; + type CompressedCommitment = CompressedCommitment<G>; - fn compress(&self) -> CompressedCommitment<G::CompressedGroupElement> { + fn compress(&self) -> Self::CompressedCommitment { CompressedCommitment { comm: self.comm.compress(), }@@ -74,19 +71,9 @@
fn to_coordinates(&self) -> (G::Base, G::Base, bool) { self.comm.to_coordinates() } -} -impl<G: Group> Default for Commitment<G> { - fn default() -> Self { - Commitment { comm: G::zero() } - } -} - -impl<C: CompressedGroup> CompressedCommitmentTrait<C> for CompressedCommitment<C> { - type Commitment = Commitment<C::GroupElement>; - - fn decompress(&self) -> Result<Self::Commitment, NovaError> { - let comm = self.comm.decompress(); + fn decompress(c: &Self::CompressedCommitment) -> Result<Self, NovaError> { + let comm = c.comm.decompress(); if comm.is_none() { return Err(NovaError::DecompressionError); }@@ -96,6 +83,12 @@ })
} } +impl<G: Group> Default for Commitment<G> { + fn default() -> Self { + Commitment { comm: G::zero() } + } +} + impl<G: Group> AppendToTranscriptTrait<G> for Commitment<G> { fn append_to_transcript(&self, label: &'static [u8], transcript: &mut G::TE) { let (x, y, is_infinity) = self.comm.to_coordinates();@@ -123,16 +116,9 @@ });
} } -impl<C: CompressedGroup> AppendToTranscriptTrait<C::GroupElement> for CompressedCommitment<C> { - fn append_to_transcript( - &self, - label: &'static [u8], - transcript: &mut <C::GroupElement as Group>::TE, - ) { - let comm = self.decompress().unwrap(); - <Commitment<C::GroupElement> as AppendToTranscriptTrait<C::GroupElement>>::append_to_transcript( - &comm, label, transcript, - ); +impl<G: Group> AppendToTranscriptTrait<G> for CompressedCommitment<G> { + fn append_to_transcript(&self, label: &'static [u8], transcript: &mut G::TE) { + transcript.absorb_bytes(label, &self.comm.as_bytes()); } }@@ -225,7 +211,6 @@
impl<G: Group> CommitmentEngineTrait<G> for CommitmentEngine<G> { type CommitmentGens = CommitmentGens<G>; type Commitment = Commitment<G>; - type CompressedCommitment = CompressedCommitment<G::CompressedGroupElement>; fn commit(gens: &Self::CommitmentGens, v: &[G::Scalar]) -> Self::Commitment { gens.commit(v)@@ -251,7 +236,7 @@ fn scale(&self, r: &G::Scalar) -> Self;
/// Reinterprets commitments as commitment keys fn reinterpret_commitments_as_gens( - c: &[<<Self as CommitmentGensExtTrait<G>>::CE as CommitmentEngineTrait<G>>::CompressedCommitment], + c: &[<<<Self as CommitmentGensExtTrait<G>>::CE as CommitmentEngineTrait<G>>::Commitment as CommitmentTrait<G>>::CompressedCommitment], ) -> Result<Self, NovaError> where Self: Sized;@@ -320,12 +305,10 @@ }
} /// reinterprets a vector of commitments as a set of generators - fn reinterpret_commitments_as_gens( - c: &[CompressedCommitment<G::CompressedGroupElement>], - ) -> Result<Self, NovaError> { + fn reinterpret_commitments_as_gens(c: &[CompressedCommitment<G>]) -> Result<Self, NovaError> { let d = (0..c.len()) .into_par_iter() - .map(|i| c[i].decompress()) + .map(|i| Commitment::<G>::decompress(&c[i])) .collect::<Result<Vec<Commitment<G>>, NovaError>>()?; let gens = (0..d.len()) .into_par_iter()
@@ -2,7 +2,7 @@ //! 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}, + traits::{AbsorbInROTrait, AppendToTranscriptTrait, Group}, }; use core::{ fmt::Debug,@@ -17,9 +17,6 @@ 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;@@ -77,32 +74,24 @@ + CommitmentOpsOwned
+ ScalarMul<G::Scalar> { /// Holds the type of the compressed commitment - type CompressedCommitment; + type CompressedCommitment: Clone + + Debug + + PartialEq + + Eq + + Send + + Sync + + Serialize + + for<'de> Deserialize<'de> + + AppendToTranscriptTrait<G>; /// 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<C: CompressedGroup>: - Clone - + Debug - + PartialEq - + Eq - + Send - + Sync - + Serialize - + for<'de> Deserialize<'de> - + AppendToTranscriptTrait<C::GroupElement> -{ - /// Holds the type of the commitment that can be decompressed into - type Commitment; - - /// Decompresses self into a commitment - fn decompress(&self) -> Result<Self::Commitment, NovaError>; + /// Decompresses a compressed commitment into a commitment + fn decompress(c: &Self::CompressedCommitment) -> Result<Self, NovaError>; } /// A trait that ties different pieces of the commitment generation together@@ -110,20 +99,10 @@ pub trait CommitmentEngineTrait<G: Group>:
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, - >; + type CommitmentGens: CommitmentGensTrait<G, Commitment = Self::Commitment>; /// Holds the type of the commitment - type Commitment: CommitmentTrait<G, CompressedCommitment = Self::CompressedCommitment>; - - /// Holds the type of the compressed commitment - type CompressedCommitment: CompressedCommitmentTrait< - G::CompressedGroupElement, - Commitment = Self::Commitment, - >; + type Commitment: CommitmentTrait<G>; /// Commits to the provided vector using the provided generators fn commit(gens: &Self::CommitmentGens, v: &[G::Scalar]) -> Self::Commitment;
@@ -106,7 +106,7 @@ /// Decompresses the compressed group element
fn decompress(&self) -> Option<Self::GroupElement>; /// Returns a byte array representing the compressed group element - fn as_bytes(&self) -> &[u8]; + fn as_bytes(&self) -> Vec<u8>; } /// A helper trait to absorb different objects in RO