storage_proofs_core/
proof.rs

1use std::time::Instant;
2
3use log::info;
4use serde::{de::DeserializeOwned, Serialize};
5
6use crate::error::Result;
7
8/// The ProofScheme trait provides the methods that any proof scheme needs to implement.
9pub trait ProofScheme<'a> {
10    type PublicParams: Clone;
11    type SetupParams: Clone;
12    type PublicInputs: Clone + Serialize + DeserializeOwned;
13    type PrivateInputs;
14    type Proof: Clone + Serialize + DeserializeOwned;
15    type Requirements: Default;
16
17    /// setup is used to generate public parameters from setup parameters in order to specialize
18    /// a ProofScheme to the specific parameters required by a consumer.
19    fn setup(_: &Self::SetupParams) -> Result<Self::PublicParams>;
20
21    /// prove generates and returns a proof from public parameters, public inputs, and private inputs.
22    fn prove(
23        _: &Self::PublicParams,
24        _: &Self::PublicInputs,
25        _: &Self::PrivateInputs,
26    ) -> Result<Self::Proof>;
27
28    fn prove_all_partitions(
29        pub_params: &Self::PublicParams,
30        pub_in: &Self::PublicInputs,
31        priv_in: &Self::PrivateInputs,
32        partition_count: usize,
33    ) -> Result<Vec<Self::Proof>> {
34        info!("groth_proof_count: {}", partition_count);
35        info!("generating {} groth proofs.", partition_count);
36
37        let start = Instant::now();
38
39        let result = (0..partition_count)
40            .map(|k| {
41                info!("generating groth proof {}.", k);
42                let start = Instant::now();
43
44                let partition_pub_in = Self::with_partition((*pub_in).clone(), Some(k));
45                let proof = Self::prove(pub_params, &partition_pub_in, priv_in);
46
47                let proof_time = start.elapsed();
48                info!("groth_proof_time: {:?}", proof_time);
49
50                proof
51            })
52            .collect::<Result<Vec<Self::Proof>>>();
53
54        let total_proof_time = start.elapsed();
55        info!("total_groth_proof_time: {:?}", total_proof_time);
56
57        result
58    }
59
60    /// verify returns true if the supplied proof is valid for the given public parameter and public inputs.
61    /// Note that verify does not have access to private inputs.
62    /// Remember that proof is untrusted, and any data it provides MUST be validated as corresponding
63    /// to the supplied public parameters and inputs.
64    fn verify(
65        _pub_params: &Self::PublicParams,
66        _pub_inputs: &Self::PublicInputs,
67        _proof: &Self::Proof,
68    ) -> Result<bool> {
69        unimplemented!();
70    }
71
72    fn verify_all_partitions(
73        pub_params: &Self::PublicParams,
74        pub_in: &Self::PublicInputs,
75        proofs: &[Self::Proof],
76    ) -> Result<bool> {
77        for (k, proof) in proofs.iter().enumerate() {
78            let partition_pub_in = Self::with_partition((*pub_in).clone(), Some(k)); //
79
80            if !Self::verify(pub_params, &partition_pub_in, proof)? {
81                return Ok(false);
82            }
83        }
84
85        Ok(true)
86    }
87
88    // This method must be specialized by concrete ProofScheme implementations which use partitions.
89    fn with_partition(pub_in: Self::PublicInputs, _k: Option<usize>) -> Self::PublicInputs {
90        pub_in
91    }
92
93    fn satisfies_requirements(
94        _pub_params: &Self::PublicParams,
95        _requirements: &Self::Requirements,
96        _partitions: usize,
97    ) -> bool {
98        true
99    }
100}
101
102#[derive(Default)]
103pub struct NoRequirements;