rsnark_provers_core/
backend.rs

1use rsnark_core::types::{CircuitDefinition, PublicWitness, Witness};
2
3use crate::Proof;
4
5/// Core trait defining the interface for zero-knowledge proof backends.
6///
7/// This trait abstracts over different ZK-SNARK implementations (such as Groth16, PLONK, Marlin, etc.)
8/// and provides a unified interface for circuit compilation, trusted setup, proof generation, and verification.
9///
10/// ## Type Parameters
11///
12/// The trait defines several associated types that must be implemented by concrete backends:
13/// - `CircuitConstraint`: The internal representation of compiled circuit constraints
14/// - `ProvingKey`: The proving key generated during the trusted setup phase
15/// - `VerifyingKey`: The verifying key generated during the trusted setup phase  
16/// - `Error`: The error type for backend-specific operations
17///
18/// ## Workflow
19///
20/// The typical workflow when using a backend implementation:
21/// 1. Create a new backend instance with [`Backend::new()`]
22/// 2. Compile the circuit definition with [`Backend::compile()`]
23/// 3. Perform trusted setup with [`Backend::setup()`] to generate keys
24/// 4. Generate proofs with [`Backend::prove()`]
25/// 5. Verify proofs with [`Backend::verify()`]
26///
27pub trait Backend: Clone {
28    type CircuitConstraint;
29    type ProvingKey;
30    type VerifyingKey;
31
32    /// Error type for backend operations that must implement standard error traits.
33    type Error: std::error::Error + Send + Sync + 'static;
34
35    /// Creates a new instance of the backend.
36    ///
37    /// This is typically used to initialize any backend-specific state or configuration.
38    fn new() -> Self;
39
40    /// Compiles a circuit definition into the backend's internal constraint representation.
41    ///
42    /// This method takes a high-level circuit definition and converts it into the specific
43    /// constraint system format required by the backend implementation.
44    ///
45    /// # Arguments
46    ///
47    /// * `circuit` - The circuit definition to compile
48    ///
49    /// # Returns
50    ///
51    /// Returns the compiled circuit constraints on success, or an error if compilation fails.
52    ///
53    /// # Errors
54    ///
55    /// This function may return an error if:
56    /// - The circuit definition is malformed or invalid
57    /// - The backend cannot represent the given circuit constraints
58    /// - Internal compilation errors occur
59    fn compile(&self, circuit: &CircuitDefinition) -> Result<Self::CircuitConstraint, Self::Error>;
60
61    /// Performs the trusted setup phase to generate proving and verifying keys.
62    ///
63    /// This is a critical phase in ZK-SNARK systems that generates the cryptographic keys
64    /// required for proof generation and verification. The setup is specific to the compiled
65    /// circuit constraints.
66    ///
67    /// # Arguments
68    ///
69    /// * `cs` - The compiled circuit constraints from [`Backend::compile()`]
70    ///
71    /// # Returns
72    ///
73    /// Returns a tuple containing the proving key and verifying key on success.
74    ///
75    /// # Security Note
76    ///
77    /// The security of the entire proving system depends on this setup phase being performed
78    /// correctly and the setup randomness being properly discarded ("toxic waste").
79    fn setup(
80        &self,
81        cs: &Self::CircuitConstraint,
82    ) -> Result<(Self::ProvingKey, Self::VerifyingKey), Self::Error>;
83
84    /// Generates a zero-knowledge proof for the given witness.
85    ///
86    /// Creates a cryptographic proof that demonstrates knowledge of a valid witness
87    /// satisfying the circuit constraints, without revealing the witness itself.
88    ///
89    /// # Arguments
90    ///
91    /// * `cs` - The compiled circuit constraints
92    /// * `pk` - The proving key from the trusted setup
93    /// * `witness` - The complete witness (both public and private inputs)
94    ///
95    /// # Returns
96    ///
97    /// Returns the generated proof on success.
98    ///
99    /// # Errors
100    ///
101    /// This function may return an error if:
102    /// - The witness does not satisfy the circuit constraints
103    /// - The proving key is incompatible with the circuit constraints
104    /// - Cryptographic operations fail during proof generation
105    fn prove(
106        &self,
107        cs: &Self::CircuitConstraint,
108        pk: &Self::ProvingKey,
109        witness: &Witness,
110    ) -> Result<Proof, Self::Error>;
111
112    /// Verifies a zero-knowledge proof against public inputs.
113    ///
114    /// Checks whether a given proof is valid for the specified public witness,
115    /// without requiring knowledge of the private witness components.
116    ///
117    /// # Arguments
118    ///
119    /// * `vk` - The verifying key from the trusted setup
120    /// * `proof` - The proof to verify
121    /// * `public_witness` - The public inputs and outputs
122    ///
123    /// # Returns
124    ///
125    /// Returns `true` if the proof is valid, `false` otherwise.
126    ///
127    /// # Errors
128    ///
129    /// This function may return an error if:
130    /// - The verifying key is malformed or incompatible
131    /// * The proof format is invalid
132    /// - Cryptographic verification operations fail
133    fn verify(
134        &self,
135        vk: &Self::VerifyingKey,
136        proof: &Proof,
137        public_witness: &PublicWitness,
138    ) -> Result<bool, Self::Error>;
139}