rsnark_provers_core/prover.rs
1use std::marker::PhantomData;
2
3use anyhow::Result;
4use rsnark_core::{Circuit, CircuitBuilder, CircuitElement, CircuitWitness};
5
6use crate::{Backend, CircuitProver};
7
8/// High-level prover that orchestrates the zero-knowledge proof generation process.
9///
10/// The `Prover` serves as the main entry point for creating zero-knowledge proofs using
11/// a specific backend implementation. It handles the circuit compilation process and
12/// creates circuit-specific provers for actual proof generation.
13///
14/// # Type Parameters
15///
16/// * `B` - The backend implementation that defines the underlying cryptographic operations
17///
18pub struct Prover<B> {
19 pub(crate) backend: B,
20}
21
22impl<B: Backend> Default for Prover<B> {
23 fn default() -> Self {
24 Self::new()
25 }
26}
27
28impl<B> Prover<B>
29where
30 B: Backend,
31{
32 /// Creates a new prover instance with the specified backend.
33 ///
34 /// This initializes the backend and prepares the prover for circuit compilation.
35 /// The backend is created using its [`Backend::new()`] method.
36 ///
37 /// # Returns
38 ///
39 /// A new `Prover` instance ready for circuit compilation.
40 ///
41 pub fn new() -> Self {
42 let backend = B::new();
43
44 Self { backend }
45 }
46
47 /// Compiles a circuit and creates a circuit-specific prover.
48 ///
49 /// This method takes a circuit witness type and compiles it into a form suitable
50 /// for the backend. It creates the circuit definition by instantiating the circuit
51 /// with fresh variables and then compiling it using the backend.
52 ///
53 /// # Type Parameters
54 ///
55 /// * `C` - The circuit witness type that implements [`CircuitWitness`]
56 ///
57 /// # Returns
58 ///
59 /// Returns a [`CircuitProver`] instance that can be used for setup, proving, and verification
60 /// operations on the compiled circuit.
61 ///
62 /// # Errors
63 ///
64 /// This function may return an error if:
65 /// - Circuit compilation fails due to invalid constraints
66 /// - The backend cannot represent the circuit
67 /// - Internal compilation errors occur
68 ///
69 pub fn compile_circuit<C>(self) -> Result<CircuitProver<B, C::CircuitWitness>>
70 where
71 C: CircuitElement,
72 <C::CircuitWitness as CircuitWitness>::CircuitElement: Circuit,
73 {
74 let metadata = self.backend.metadata();
75 let mut builder = CircuitBuilder::new(metadata);
76 let circuit = C::CircuitWitness::create_public(builder.variable_initer_mut(), false);
77 circuit.define(&mut builder);
78
79 let define = builder.build();
80
81 let cs = self.backend.compile(&define)?;
82
83 Ok(CircuitProver {
84 backend: self.backend,
85 constraint: cs,
86 marker: PhantomData,
87 })
88 }
89
90 // pub fn compile_constraints(&self, constraints: &[u8]) -> B::CircuitConstraint {
91}