/ algorithms / src / snark / varuna / data_structures / circuit_proving_key.rs
circuit_proving_key.rs
 1  // Copyright (c) 2025-2026 ACDC Network
 2  // This file is part of the alphavm library.
 3  //
 4  // Alpha Chain | Delta Chain Protocol
 5  // International Monetary Graphite.
 6  //
 7  // Derived from Aleo (https://aleo.org) and ProvableHQ (https://provable.com).
 8  // They built world-class ZK infrastructure. We installed the EASY button.
 9  // Their cryptography: elegant. Our modifications: bureaucracy-compatible.
10  // Original brilliance: theirs. Robert's Rules: ours. Bugs: definitely ours.
11  //
12  // Original Aleo/ProvableHQ code subject to Apache 2.0 https://www.apache.org/licenses/LICENSE-2.0
13  // All modifications and new work: CC0 1.0 Universal Public Domain Dedication.
14  // No rights reserved. No permission required. No warranty. No refunds.
15  //
16  // https://creativecommons.org/publicdomain/zero/1.0/
17  // SPDX-License-Identifier: CC0-1.0
18  
19  use crate::{
20      polycommit::sonic_pc,
21      snark::varuna::{ahp::indexer::*, CircuitVerifyingKey, SNARKMode},
22  };
23  use alphavm_curves::PairingEngine;
24  use alphavm_utilities::{serialize::*, FromBytes, ToBytes};
25  use std::io::{self, Read, Write};
26  
27  use std::{cmp::Ordering, sync::Arc};
28  
29  /// Proving key for a specific circuit (i.e., R1CS matrices).
30  #[derive(Clone, Debug)]
31  pub struct CircuitProvingKey<E: PairingEngine, SM: SNARKMode> {
32      /// The circuit verifying key.
33      pub circuit_verifying_key: CircuitVerifyingKey<E>,
34      // NOTE: The circuit verifying key's circuit_info and circuit id are also stored in Circuit for convenience.
35      /// The circuit itself.
36      pub circuit: Arc<Circuit<E::Fr, SM>>,
37      /// The committer key for this index, trimmed from the universal SRS.
38      pub committer_key: Arc<sonic_pc::CommitterKey<E>>,
39  }
40  
41  impl<E: PairingEngine, SM: SNARKMode> ToBytes for CircuitProvingKey<E, SM> {
42      fn write_le<W: Write>(&self, mut writer: W) -> io::Result<()> {
43          CanonicalSerialize::serialize_compressed(&self.circuit_verifying_key, &mut writer)?;
44          CanonicalSerialize::serialize_compressed(&self.circuit, &mut writer)?;
45  
46          self.committer_key.write_le(&mut writer)
47      }
48  }
49  
50  impl<E: PairingEngine, SM: SNARKMode> FromBytes for CircuitProvingKey<E, SM> {
51      #[inline]
52      fn read_le<R: Read>(mut reader: R) -> io::Result<Self> {
53          let circuit_verifying_key = CanonicalDeserialize::deserialize_compressed(&mut reader)?;
54          let circuit = CanonicalDeserialize::deserialize_compressed(&mut reader)?;
55          let committer_key = Arc::new(FromBytes::read_le(&mut reader)?);
56  
57          Ok(Self { circuit_verifying_key, circuit, committer_key })
58      }
59  }
60  
61  impl<E: PairingEngine, SM: SNARKMode> PartialEq for CircuitProvingKey<E, SM> {
62      fn eq(&self, other: &Self) -> bool {
63          self.circuit.id == other.circuit.id
64      }
65  }
66  
67  impl<E: PairingEngine, SM: SNARKMode> Eq for CircuitProvingKey<E, SM> {}
68  
69  impl<E: PairingEngine, SM: SNARKMode> Ord for CircuitProvingKey<E, SM> {
70      fn cmp(&self, other: &Self) -> Ordering {
71          self.circuit.id.cmp(&other.circuit.id)
72      }
73  }
74  
75  impl<E: PairingEngine, SM: SNARKMode> PartialOrd for CircuitProvingKey<E, SM> {
76      fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
77          Some(self.cmp(other))
78      }
79  }