Skip to content

Commit

Permalink
Expose verifier key's digest with a trait (#243)
Browse files Browse the repository at this point in the history
* Expose verifier key's digest with a trait

* run cargo fmt and clippy
  • Loading branch information
TheLitFire authored Oct 30, 2023
1 parent abd43bd commit 02009c2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
13 changes: 12 additions & 1 deletion src/spartan/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ use crate::{
},
errors::NovaError,
r1cs::{R1CSShape, RelaxedR1CSInstance, RelaxedR1CSWitness},
traits::{circuit::StepCircuit, snark::RelaxedR1CSSNARKTrait, Group},
traits::{
circuit::StepCircuit,
snark::{DigestHelperTrait, RelaxedR1CSSNARKTrait},
Group,
},
Commitment, CommitmentKey,
};
use bellpepper_core::{num::AllocatedNum, Circuit, ConstraintSystem, SynthesisError};
Expand Down Expand Up @@ -75,6 +79,13 @@ where
vk: S::VerifierKey,
}

impl<G: Group, S: RelaxedR1CSSNARKTrait<G>> VerifierKey<G, S> {
/// Returns the digest of the verifier's key
pub fn digest(&self) -> G::Scalar {
self.vk.digest()
}
}

/// A direct SNARK proving a step circuit
#[derive(Clone, Serialize, Deserialize)]
#[serde(bound = "")]
Expand Down
7 changes: 4 additions & 3 deletions src/spartan/ppsnark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
traits::{
commitment::{CommitmentEngineTrait, CommitmentTrait},
evaluation::EvaluationEngineTrait,
snark::RelaxedR1CSSNARKTrait,
snark::{DigestHelperTrait, RelaxedR1CSSNARKTrait},
Group, TranscriptEngineTrait, TranscriptReprTrait,
},
Commitment, CommitmentKey, CompressedCommitment,
Expand Down Expand Up @@ -856,9 +856,10 @@ impl<G: Group, EE: EvaluationEngineTrait<G>> VerifierKey<G, EE> {
digest: Default::default(),
}
}

}
impl<G: Group, EE: EvaluationEngineTrait<G>> DigestHelperTrait<G> for VerifierKey<G, EE> {
/// Returns the digest of the verifier's key
pub fn digest(&self) -> G::Scalar {
fn digest(&self) -> G::Scalar {
self
.digest
.get_or_try_init(|| {
Expand Down
8 changes: 6 additions & 2 deletions src/spartan/snark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ use crate::{
PolyEvalInstance, PolyEvalWitness,
},
traits::{
evaluation::EvaluationEngineTrait, snark::RelaxedR1CSSNARKTrait, Group, TranscriptEngineTrait,
evaluation::EvaluationEngineTrait,
snark::{DigestHelperTrait, RelaxedR1CSSNARKTrait},
Group, TranscriptEngineTrait,
},
Commitment, CommitmentKey,
};
Expand Down Expand Up @@ -53,9 +55,11 @@ impl<G: Group, EE: EvaluationEngineTrait<G>> VerifierKey<G, EE> {
digest: OnceCell::new(),
}
}
}

impl<G: Group, EE: EvaluationEngineTrait<G>> DigestHelperTrait<G> for VerifierKey<G, EE> {
/// Returns the digest of the verifier's key.
pub fn digest(&self) -> G::Scalar {
fn digest(&self) -> G::Scalar {
self
.digest
.get_or_try_init(|| {
Expand Down
8 changes: 7 additions & 1 deletion src/traits/snark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub trait RelaxedR1CSSNARKTrait<G: Group>:
type ProverKey: Send + Sync + Serialize + for<'de> Deserialize<'de>;

/// A type that represents the verifier's key
type VerifierKey: Send + Sync + Serialize + for<'de> Deserialize<'de>;
type VerifierKey: Send + Sync + Serialize + for<'de> Deserialize<'de> + DigestHelperTrait<G>;

/// Produces the keys for the prover and the verifier
fn setup(
Expand All @@ -36,3 +36,9 @@ pub trait RelaxedR1CSSNARKTrait<G: Group>:
/// Verifies a SNARK for a relaxed R1CS
fn verify(&self, vk: &Self::VerifierKey, U: &RelaxedR1CSInstance<G>) -> Result<(), NovaError>;
}

/// A helper trait that defines the behavior of a verifier key of `zkSNARK`
pub trait DigestHelperTrait<G: Group> {
/// Returns the digest of the verifier's key
fn digest(&self) -> G::Scalar;
}

0 comments on commit 02009c2

Please sign in to comment.