ark-r1cs-std/crypto-primitives/src/nizk/constraints.rs
crypto-primitives/src/nizk/constraints.rsRaw
1use algebra_core::Field;
2use core::borrow::Borrow;
3use r1cs_core::{Namespace, SynthesisError};
4use r1cs_std::prelude::*;
5
6use crate::nizk::NIZK;
7
8pub trait NIZKVerifierGadget<N: NIZK, ConstraintF: Field> {
9 type PreparedVerificationKeyVar;
10 type VerificationKeyVar: AllocVar<N::VerificationParameters, ConstraintF>
11 + ToBytesGadget<ConstraintF>;
12 type ProofVar: AllocVar<N::Proof, ConstraintF>;
13
14 /// Optionally allocates `N::Proof` in `cs` without performing
15 /// subgroup checks.
16 ///
17 /// The default implementation doesn't omit these checks.
18 #[tracing::instrument(target = "r1cs", skip(cs, f))]
19 fn new_proof_unchecked<T: Borrow<N::Proof>>(
20 cs: impl Into<Namespace<ConstraintF>>,
21 f: impl FnOnce() -> Result<T, SynthesisError>,
22 mode: AllocationMode,
23 ) -> Result<Self::ProofVar, SynthesisError> {
24 Self::ProofVar::new_variable(cs, f, mode)
25 }
26
27 /// Optionally allocates `N::VerificationParameters` in `cs`
28 /// without performing subgroup checks.
29 ///
30 /// The default implementation doesn't omit these checks.
31 #[tracing::instrument(target = "r1cs", skip(cs, f))]
32 fn new_verification_key_unchecked<T: Borrow<N::VerificationParameters>>(
33 cs: impl Into<Namespace<ConstraintF>>,
34 f: impl FnOnce() -> Result<T, SynthesisError>,
35 mode: AllocationMode,
36 ) -> Result<Self::VerificationKeyVar, SynthesisError> {
37 Self::VerificationKeyVar::new_variable(cs, f, mode)
38 }
39
40 fn verify<'a, T: 'a + ToBitsGadget<ConstraintF> + ?Sized>(
41 verification_key: &Self::VerificationKeyVar,
42 input: impl IntoIterator<Item = &'a T>,
43 proof: &Self::ProofVar,
44 ) -> Result<Boolean<ConstraintF>, SynthesisError>;
45
46 fn verify_prepared<'a, T: 'a + ToBitsGadget<ConstraintF> + ?Sized>(
47 prepared_verification_key: &Self::PreparedVerificationKeyVar,
48 input: impl IntoIterator<Item = &'a T>,
49 proof: &Self::ProofVar,
50 ) -> Result<Boolean<ConstraintF>, SynthesisError>;
51}