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