snarkvm_algorithms/traits/
snark.rs

1// Copyright (c) 2019-2025 Provable Inc.
2// This file is part of the snarkVM library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use crate::{AlgebraicSponge, r1cs::ConstraintSynthesizer, snark::varuna::VarunaVersion};
17use snarkvm_fields::PrimeField;
18use snarkvm_utilities::{CanonicalDeserialize, CanonicalSerialize, FromBytes, ToBytes};
19
20use anyhow::Result;
21use rand::{CryptoRng, Rng};
22use std::{borrow::Borrow, collections::BTreeMap, fmt::Debug};
23
24/// Defines trait that describes preparing from an unprepared version to a
25/// prepare version.
26pub trait Prepare {
27    type Prepared;
28    fn prepare(&self) -> Self::Prepared;
29}
30
31pub trait SNARK {
32    type ScalarField: Clone + PrimeField;
33    type BaseField: Clone + PrimeField;
34
35    /// A certificate that the indexing was performed correctly.
36    type Certificate: CanonicalSerialize
37        + CanonicalDeserialize
38        + Clone
39        + Debug
40        + ToBytes
41        + FromBytes
42        + PartialEq
43        + Eq
44        + Send
45        + Sync;
46    type Proof: Clone + Debug + ToBytes + FromBytes + PartialEq + Eq + Send + Sync;
47    type ProvingKey: Clone + ToBytes + FromBytes + Send + Sync + Ord;
48
49    type UniversalSRS: Clone;
50    type UniversalProver;
51    type UniversalVerifier;
52
53    type VerifierInput: ?Sized;
54    type VerifyingKey: Clone + Send + Sync + ToBytes + FromBytes + Ord;
55
56    type FiatShamirRng: AlgebraicSponge<Self::BaseField, 2, Parameters = Self::FSParameters>;
57    type FSParameters;
58
59    fn universal_setup(config: usize) -> Result<Self::UniversalSRS>;
60
61    fn circuit_setup<C: ConstraintSynthesizer<Self::ScalarField>>(
62        srs: &Self::UniversalSRS,
63        circuit: &C,
64    ) -> Result<(Self::ProvingKey, Self::VerifyingKey)>;
65
66    fn prove_vk(
67        universal_prover: &Self::UniversalProver,
68        fs_parameters: &Self::FSParameters,
69        verifying_key: &Self::VerifyingKey,
70        proving_key: &Self::ProvingKey,
71    ) -> Result<Self::Certificate>;
72
73    fn prove<C: ConstraintSynthesizer<Self::ScalarField>, R: Rng + CryptoRng>(
74        universal_prover: &Self::UniversalProver,
75        fs_parameters: &Self::FSParameters,
76        proving_key: &Self::ProvingKey,
77        varuna_version: VarunaVersion,
78        constraints: &C,
79        rng: &mut R,
80    ) -> Result<Self::Proof> {
81        let mut keys_to_constraints = BTreeMap::new();
82        keys_to_constraints.insert(proving_key, std::slice::from_ref(constraints));
83        Self::prove_batch(universal_prover, fs_parameters, varuna_version, &keys_to_constraints, rng)
84    }
85
86    fn prove_batch<C: ConstraintSynthesizer<Self::ScalarField>, R: Rng + CryptoRng>(
87        universal_prover: &Self::UniversalProver,
88        fs_parameters: &Self::FSParameters,
89        varuna_version: VarunaVersion,
90        keys_to_constraints: &BTreeMap<&Self::ProvingKey, &[C]>,
91        rng: &mut R,
92    ) -> Result<Self::Proof>;
93
94    fn verify_vk<C: ConstraintSynthesizer<Self::ScalarField>>(
95        universal_verifier: &Self::UniversalVerifier,
96        fs_parameters: &Self::FSParameters,
97        circuit: &C,
98        verifying_key: &Self::VerifyingKey,
99        certificate: &Self::Certificate,
100    ) -> Result<bool>;
101
102    fn verify<B: Borrow<Self::VerifierInput>>(
103        universal_verifier: &Self::UniversalVerifier,
104        fs_parameters: &Self::FSParameters,
105        verifying_key: &Self::VerifyingKey,
106        varuna_version: VarunaVersion,
107        input: B,
108        proof: &Self::Proof,
109    ) -> Result<bool> {
110        let mut keys_to_inputs = BTreeMap::new();
111        let inputs = [input];
112        keys_to_inputs.insert(verifying_key, &inputs[..]);
113        Self::verify_batch(universal_verifier, fs_parameters, varuna_version, &keys_to_inputs, proof)
114    }
115
116    fn verify_batch<B: Borrow<Self::VerifierInput>>(
117        universal_verifier: &Self::UniversalVerifier,
118        fs_parameters: &Self::FSParameters,
119        varuna_version: VarunaVersion,
120        keys_to_inputs: &BTreeMap<&Self::VerifyingKey, &[B]>,
121        proof: &Self::Proof,
122    ) -> Result<bool>;
123}