snarkvm_algorithms/snark/varuna/data_structures/
circuit_proving_key.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::{
17    polycommit::sonic_pc,
18    snark::varuna::{CircuitVerifyingKey, SNARKMode, ahp::indexer::*},
19};
20use snarkvm_curves::PairingEngine;
21use snarkvm_utilities::{FromBytes, ToBytes, serialize::*};
22use std::io::{self, Read, Write};
23
24use std::{cmp::Ordering, sync::Arc};
25
26/// Proving key for a specific circuit (i.e., R1CS matrices).
27#[derive(Clone, Debug)]
28pub struct CircuitProvingKey<E: PairingEngine, SM: SNARKMode> {
29    /// The circuit verifying key.
30    pub circuit_verifying_key: CircuitVerifyingKey<E>,
31    // NOTE: The circuit verifying key's circuit_info and circuit id are also stored in Circuit for convenience.
32    /// The circuit itself.
33    pub circuit: Arc<Circuit<E::Fr, SM>>,
34    /// The committer key for this index, trimmed from the universal SRS.
35    pub committer_key: Arc<sonic_pc::CommitterKey<E>>,
36}
37
38impl<E: PairingEngine, SM: SNARKMode> ToBytes for CircuitProvingKey<E, SM> {
39    fn write_le<W: Write>(&self, mut writer: W) -> io::Result<()> {
40        CanonicalSerialize::serialize_compressed(&self.circuit_verifying_key, &mut writer)?;
41        CanonicalSerialize::serialize_compressed(&self.circuit, &mut writer)?;
42
43        self.committer_key.write_le(&mut writer)
44    }
45}
46
47impl<E: PairingEngine, SM: SNARKMode> FromBytes for CircuitProvingKey<E, SM> {
48    #[inline]
49    fn read_le<R: Read>(mut reader: R) -> io::Result<Self> {
50        let circuit_verifying_key = CanonicalDeserialize::deserialize_compressed(&mut reader)?;
51        let circuit = CanonicalDeserialize::deserialize_compressed(&mut reader)?;
52        let committer_key = Arc::new(FromBytes::read_le(&mut reader)?);
53
54        Ok(Self { circuit_verifying_key, circuit, committer_key })
55    }
56}
57
58impl<E: PairingEngine, SM: SNARKMode> PartialEq for CircuitProvingKey<E, SM> {
59    fn eq(&self, other: &Self) -> bool {
60        self.circuit.id == other.circuit.id
61    }
62}
63
64impl<E: PairingEngine, SM: SNARKMode> Eq for CircuitProvingKey<E, SM> {}
65
66impl<E: PairingEngine, SM: SNARKMode> Ord for CircuitProvingKey<E, SM> {
67    fn cmp(&self, other: &Self) -> Ordering {
68        self.circuit.id.cmp(&other.circuit.id)
69    }
70}
71
72impl<E: PairingEngine, SM: SNARKMode> PartialOrd for CircuitProvingKey<E, SM> {
73    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
74        Some(self.cmp(other))
75    }
76}