rsnark_provers_core/
prover.rs

1use std::marker::PhantomData;
2
3use anyhow::Result;
4use rsnark_core::{Circuit, CircuitBuilder, 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>>
70    where
71        C: CircuitWitness,
72        C::PublicElement: Circuit,
73    {
74        let mut builder = CircuitBuilder::default();
75        let circuit = C::create_public(builder.variable_initer_mut(), false);
76        circuit.define(&mut builder);
77
78        let define = builder.build();
79
80        let cs = self.backend.compile(&define)?;
81
82        Ok(CircuitProver {
83            backend: self.backend,
84            constraint: cs,
85            marker: PhantomData,
86        })
87    }
88
89    // pub fn compile_constraints(&self, constraints: &[u8]) -> B::CircuitConstraint {
90}