snarkvm_algorithms/traits/
snark.rs

1// Copyright 2024 Aleo Network Foundation
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};
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 prepare version.
25pub trait Prepare {
26    type Prepared;
27    fn prepare(&self) -> Self::Prepared;
28}
29
30pub trait SNARK {
31    type ScalarField: Clone + PrimeField;
32    type BaseField: Clone + PrimeField;
33
34    /// A certificate that the indexing was performed correctly.
35    type Certificate: CanonicalSerialize
36        + CanonicalDeserialize
37        + Clone
38        + Debug
39        + ToBytes
40        + FromBytes
41        + PartialEq
42        + Eq
43        + Send
44        + Sync;
45    type Proof: Clone + Debug + ToBytes + FromBytes + PartialEq + Eq + Send + Sync;
46    type ProvingKey: Clone + ToBytes + FromBytes + Send + Sync + Ord;
47
48    type UniversalSRS: Clone;
49    type UniversalProver;
50    type UniversalVerifier;
51
52    type VerifierInput: ?Sized;
53    type VerifyingKey: Clone + Send + Sync + ToBytes + FromBytes + Ord;
54
55    type FiatShamirRng: AlgebraicSponge<Self::BaseField, 2, Parameters = Self::FSParameters>;
56    type FSParameters;
57
58    fn universal_setup(config: usize) -> Result<Self::UniversalSRS>;
59
60    fn circuit_setup<C: ConstraintSynthesizer<Self::ScalarField>>(
61        srs: &Self::UniversalSRS,
62        circuit: &C,
63    ) -> Result<(Self::ProvingKey, Self::VerifyingKey)>;
64
65    fn prove_vk(
66        universal_prover: &Self::UniversalProver,
67        fs_parameters: &Self::FSParameters,
68        verifying_key: &Self::VerifyingKey,
69        proving_key: &Self::ProvingKey,
70    ) -> Result<Self::Certificate>;
71
72    fn prove<C: ConstraintSynthesizer<Self::ScalarField>, R: Rng + CryptoRng>(
73        universal_prover: &Self::UniversalProver,
74        fs_parameters: &Self::FSParameters,
75        proving_key: &Self::ProvingKey,
76        constraints: &C,
77        rng: &mut R,
78    ) -> Result<Self::Proof> {
79        let mut keys_to_constraints = BTreeMap::new();
80        keys_to_constraints.insert(proving_key, std::slice::from_ref(constraints));
81        Self::prove_batch(universal_prover, fs_parameters, &keys_to_constraints, rng)
82    }
83
84    fn prove_batch<C: ConstraintSynthesizer<Self::ScalarField>, R: Rng + CryptoRng>(
85        universal_prover: &Self::UniversalProver,
86        fs_parameters: &Self::FSParameters,
87        keys_to_constraints: &BTreeMap<&Self::ProvingKey, &[C]>,
88        rng: &mut R,
89    ) -> Result<Self::Proof>;
90
91    fn verify_vk<C: ConstraintSynthesizer<Self::ScalarField>>(
92        universal_verifier: &Self::UniversalVerifier,
93        fs_parameters: &Self::FSParameters,
94        circuit: &C,
95        verifying_key: &Self::VerifyingKey,
96        certificate: &Self::Certificate,
97    ) -> Result<bool>;
98
99    fn verify<B: Borrow<Self::VerifierInput>>(
100        universal_verifier: &Self::UniversalVerifier,
101        fs_parameters: &Self::FSParameters,
102        verifying_key: &Self::VerifyingKey,
103        input: B,
104        proof: &Self::Proof,
105    ) -> Result<bool> {
106        let mut keys_to_inputs = BTreeMap::new();
107        let inputs = [input];
108        keys_to_inputs.insert(verifying_key, &inputs[..]);
109        Self::verify_batch(universal_verifier, fs_parameters, &keys_to_inputs, proof)
110    }
111
112    fn verify_batch<B: Borrow<Self::VerifierInput>>(
113        universal_verifier: &Self::UniversalVerifier,
114        fs_parameters: &Self::FSParameters,
115        keys_to_inputs: &BTreeMap<&Self::VerifyingKey, &[B]>,
116        proof: &Self::Proof,
117    ) -> Result<bool>;
118}