fullcodec_plonk/proof_system/
verifier.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4//
5// Copyright (c) DUSK NETWORK. All rights reserved.
6
7use crate::commitment_scheme::{CommitKey, OpeningKey};
8use crate::constraint_system::TurboComposer;
9use crate::error::Error;
10use crate::proof_system::widget::VerifierKey;
11use crate::proof_system::Proof;
12use dusk_bls12_381::BlsScalar;
13use merlin::Transcript;
14
15/// Abstraction structure designed verify [`Proof`]s.
16#[allow(missing_debug_implementations)]
17pub struct Verifier {
18    /// VerificationKey which is used to verify a specific PLONK circuit
19    pub verifier_key: Option<VerifierKey>,
20
21    pub(crate) cs: TurboComposer,
22    /// Store the messages exchanged during the preprocessing stage
23    /// This is copied each time, we make a proof, so that we can use the same
24    /// verifier to Verify multiple proofs from the same circuit. If this
25    /// is not copied, then the verification procedure will modify
26    /// the transcript, making it unusable for future proofs.
27    pub preprocessed_transcript: Transcript,
28}
29
30impl Default for Verifier {
31    fn default() -> Verifier {
32        Verifier::new(b"plonk")
33    }
34}
35
36impl Verifier {
37    /// Creates a new `Verifier` instance.
38    pub fn new(label: &'static [u8]) -> Verifier {
39        Verifier {
40            verifier_key: None,
41            cs: TurboComposer::new(),
42            preprocessed_transcript: Transcript::new(label),
43        }
44    }
45
46    /// Creates a new `Verifier` instance with some expected size.
47    pub fn with_size(label: &'static [u8], size: usize) -> Verifier {
48        Verifier {
49            verifier_key: None,
50            cs: TurboComposer::with_size(size),
51            preprocessed_transcript: Transcript::new(label),
52        }
53    }
54
55    /// Returns the number of gates in the circuit.
56    pub const fn gates(&self) -> u32 {
57        self.cs.gates()
58    }
59
60    /// Mutable borrow of the [`TurboComposer`].
61    pub fn composer_mut(&mut self) -> &mut TurboComposer {
62        &mut self.cs
63    }
64
65    /// Preprocess a circuit to obtain a [`VerifierKey`] and a circuit
66    /// descriptor so that the `Verifier` instance can verify [`Proof`]s
67    /// for this circuit descriptor instance.
68    pub fn preprocess(&mut self, commit_key: &CommitKey) -> Result<(), Error> {
69        let vk = self.cs.preprocess_verifier(
70            commit_key,
71            &mut self.preprocessed_transcript,
72        )?;
73
74        self.verifier_key = Some(vk);
75        Ok(())
76    }
77
78    /// Keys the [`Transcript`] with additional seed information
79    /// Wrapper around [`Transcript::append_message`].
80    pub fn key_transcript(&mut self, label: &'static [u8], message: &[u8]) {
81        self.preprocessed_transcript.append_message(label, message);
82    }
83
84    /// Verifies a [`Proof`].
85    pub fn verify(
86        &self,
87        proof: &Proof,
88        opening_key: &OpeningKey,
89        public_inputs: &[BlsScalar],
90    ) -> Result<(), Error> {
91        let mut cloned_transcript = self.preprocessed_transcript.clone();
92        let verifier_key = self.verifier_key.as_ref().unwrap();
93
94        proof.verify(
95            verifier_key,
96            &mut cloned_transcript,
97            opening_key,
98            public_inputs,
99        )
100    }
101}