From d53b3e0fc12cf1581287e9cdf5547e832735980d Mon Sep 17 00:00:00 2001 From: Srinath Setty Date: Tue, 28 Feb 2023 18:21:25 -0800 Subject: [PATCH] simplify trait requirements (#143) * simplify trait requirements * update version --- Cargo.toml | 2 +- src/lib.rs | 8 +++--- src/nifs.rs | 9 +++---- src/provider/pasta.rs | 4 +-- src/provider/pedersen.rs | 55 ++++++++++++++-------------------------- src/traits/commitment.rs | 49 ++++++++++------------------------- src/traits/mod.rs | 2 +- 7 files changed, 44 insertions(+), 85 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9639929..d64d0ae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "nova-snark" -version = "0.14.0" +version = "0.15.0" authors = ["Srinath Setty "] edition = "2021" description = "Recursive zkSNARKs without trusted setup" diff --git a/src/lib.rs b/src/lib.rs index d0d9697..3dd5e0e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -44,7 +44,7 @@ use r1cs::{ 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 @@ where 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::::decompress(&nifs_secondary.comm_T)?), ); let circuit_primary: NovaAugmentedCircuit = NovaAugmentedCircuit::new( @@ -351,7 +351,7 @@ where 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::::decompress(&nifs_primary.comm_T)?), ); let circuit_secondary: NovaAugmentedCircuit = NovaAugmentedCircuit::new( @@ -727,7 +727,7 @@ where type CommitmentGens = <::CE as CommitmentEngineTrait>::CommitmentGens; type Commitment = <::CE as CommitmentEngineTrait>::Commitment; -type CompressedCommitment = <::CE as CommitmentEngineTrait>::CompressedCommitment; +type CompressedCommitment = <<::CE as CommitmentEngineTrait>::Commitment as CommitmentTrait>::CompressedCommitment; type CE = ::CE; #[cfg(test)] diff --git a/src/nifs.rs b/src/nifs.rs index aead810..632acb9 100644 --- a/src/nifs.rs +++ b/src/nifs.rs @@ -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 @@ impl NIFS { 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::::decompress(&self.comm_T)?; comm_T.absorb_in_ro(&mut ro); // compute a challenge from the RO diff --git a/src/provider/pasta.rs b/src/provider/pasta.rs index 1197154..5b32ea8 100644 --- a/src/provider/pasta.rs +++ b/src/provider/pasta.rs @@ -190,8 +190,8 @@ macro_rules! impl_traits { Some($name_curve::from_bytes(&self.repr).unwrap()) } - fn as_bytes(&self) -> &[u8] { - &self.repr + fn as_bytes(&self) -> Vec { + self.repr.to_vec() } } }; diff --git a/src/provider/pedersen.rs b/src/provider/pedersen.rs index 2d0c5c8..7a32b3e 100644 --- a/src/provider/pedersen.rs +++ b/src/provider/pedersen.rs @@ -2,9 +2,7 @@ use crate::{ errors::NovaError, traits::{ - commitment::{ - CommitmentEngineTrait, CommitmentGensTrait, CommitmentTrait, CompressedCommitmentTrait, - }, + commitment::{CommitmentEngineTrait, CommitmentGensTrait, CommitmentTrait}, AbsorbInROTrait, AppendToTranscriptTrait, CompressedGroup, Group, ROTrait, TranscriptEngineTrait, }, @@ -35,13 +33,12 @@ pub struct Commitment { /// A type that holds a compressed commitment #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(bound = "")] -pub struct CompressedCommitment { - comm: C, +pub struct CompressedCommitment { + comm: G::CompressedGroupElement, } impl CommitmentGensTrait for CommitmentGens { type Commitment = Commitment; - type CompressedCommitment = CompressedCommitment; fn new(label: &'static [u8], n: usize) -> Self { CommitmentGens { @@ -63,9 +60,9 @@ impl CommitmentGensTrait for CommitmentGens { } impl CommitmentTrait for Commitment { - type CompressedCommitment = CompressedCommitment; + type CompressedCommitment = CompressedCommitment; - fn compress(&self) -> CompressedCommitment { + fn compress(&self) -> Self::CompressedCommitment { CompressedCommitment { comm: self.comm.compress(), } @@ -74,19 +71,9 @@ impl CommitmentTrait for Commitment { fn to_coordinates(&self) -> (G::Base, G::Base, bool) { self.comm.to_coordinates() } -} - -impl Default for Commitment { - fn default() -> Self { - Commitment { comm: G::zero() } - } -} - -impl CompressedCommitmentTrait for CompressedCommitment { - type Commitment = Commitment; - fn decompress(&self) -> Result { - let comm = self.comm.decompress(); + fn decompress(c: &Self::CompressedCommitment) -> Result { + let comm = c.comm.decompress(); if comm.is_none() { return Err(NovaError::DecompressionError); } @@ -96,6 +83,12 @@ impl CompressedCommitmentTrait for CompressedCommitment Default for Commitment { + fn default() -> Self { + Commitment { comm: G::zero() } + } +} + impl AppendToTranscriptTrait for Commitment { 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 AbsorbInROTrait for Commitment { } } -impl AppendToTranscriptTrait for CompressedCommitment { - fn append_to_transcript( - &self, - label: &'static [u8], - transcript: &mut ::TE, - ) { - let comm = self.decompress().unwrap(); - as AppendToTranscriptTrait>::append_to_transcript( - &comm, label, transcript, - ); +impl AppendToTranscriptTrait for CompressedCommitment { + fn append_to_transcript(&self, label: &'static [u8], transcript: &mut G::TE) { + transcript.absorb_bytes(label, &self.comm.as_bytes()); } } @@ -225,7 +211,6 @@ pub struct CommitmentEngine { impl CommitmentEngineTrait for CommitmentEngine { type CommitmentGens = CommitmentGens; type Commitment = Commitment; - type CompressedCommitment = CompressedCommitment; fn commit(gens: &Self::CommitmentGens, v: &[G::Scalar]) -> Self::Commitment { gens.commit(v) @@ -251,7 +236,7 @@ pub(crate) trait CommitmentGensExtTrait: CommitmentGensTrait { /// Reinterprets commitments as commitment keys fn reinterpret_commitments_as_gens( - c: &[<>::CE as CommitmentEngineTrait>::CompressedCommitment], + c: &[<<>::CE as CommitmentEngineTrait>::Commitment as CommitmentTrait>::CompressedCommitment], ) -> Result where Self: Sized; @@ -320,12 +305,10 @@ impl CommitmentGensExtTrait for CommitmentGens { } /// reinterprets a vector of commitments as a set of generators - fn reinterpret_commitments_as_gens( - c: &[CompressedCommitment], - ) -> Result { + fn reinterpret_commitments_as_gens(c: &[CompressedCommitment]) -> Result { let d = (0..c.len()) .into_par_iter() - .map(|i| c[i].decompress()) + .map(|i| Commitment::::decompress(&c[i])) .collect::>, NovaError>>()?; let gens = (0..d.len()) .into_par_iter() diff --git a/src/traits/commitment.rs b/src/traits/commitment.rs index e4c4dc9..94b6378 100644 --- a/src/traits/commitment.rs +++ b/src/traits/commitment.rs @@ -2,7 +2,7 @@ //! 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, @@ -18,9 +18,6 @@ pub trait CommitmentGensTrait: /// 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 @@ pub trait CommitmentTrait: + ScalarMul { /// Holds the type of the compressed commitment - type CompressedCommitment; + type CompressedCommitment: Clone + + Debug + + PartialEq + + Eq + + Send + + Sync + + Serialize + + for<'de> Deserialize<'de> + + AppendToTranscriptTrait; /// 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; + /// Decompresses a compressed commitment into a commitment + fn decompress(c: &Self::CompressedCommitment) -> Result; } /// A trait that ties different pieces of the commitment generation together @@ -110,20 +99,10 @@ 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, - >; + type CommitmentGens: CommitmentGensTrait; /// Holds the type of the commitment - type Commitment: CommitmentTrait; - - /// Holds the type of the compressed commitment - type CompressedCommitment: CompressedCommitmentTrait< - G::CompressedGroupElement, - Commitment = Self::Commitment, - >; + type Commitment: CommitmentTrait; /// Commits to the provided vector using the provided generators fn commit(gens: &Self::CommitmentGens, v: &[G::Scalar]) -> Self::Commitment; diff --git a/src/traits/mod.rs b/src/traits/mod.rs index 6e7a166..78f6f60 100644 --- a/src/traits/mod.rs +++ b/src/traits/mod.rs @@ -106,7 +106,7 @@ pub trait CompressedGroup: fn decompress(&self) -> Option; /// Returns a byte array representing the compressed group element - fn as_bytes(&self) -> &[u8]; + fn as_bytes(&self) -> Vec; } /// A helper trait to absorb different objects in RO