proof_of_sql/sql/proof/
proof_plan.rs

1use super::{verification_builder::VerificationBuilder, FinalRoundBuilder, FirstRoundBuilder};
2use crate::base::{
3    database::{ColumnField, ColumnRef, LiteralValue, Table, TableEvaluation, TableRef},
4    map::{IndexMap, IndexSet},
5    proof::{PlaceholderResult, ProofError},
6    scalar::Scalar,
7};
8use alloc::vec::Vec;
9use bumpalo::Bump;
10use core::fmt::Debug;
11use sqlparser::ast::Ident;
12
13/// Provable nodes in the provable AST.
14#[enum_dispatch::enum_dispatch(DynProofPlan)]
15pub trait ProofPlan: Debug + Send + Sync + ProverEvaluate {
16    /// Form components needed to verify and proof store into `VerificationBuilder`
17    fn verifier_evaluate<S: Scalar>(
18        &self,
19        builder: &mut impl VerificationBuilder<S>,
20        accessor: &IndexMap<TableRef, IndexMap<Ident, S>>,
21        chi_eval_map: &IndexMap<TableRef, (S, usize)>,
22        params: &[LiteralValue],
23    ) -> Result<TableEvaluation<S>, ProofError>;
24
25    /// Return all the result column fields
26    fn get_column_result_fields(&self) -> Vec<ColumnField>;
27
28    /// Return all the columns referenced in the Query
29    fn get_column_references(&self) -> IndexSet<ColumnRef>;
30
31    /// Return all the tables referenced in the Query
32    fn get_table_references(&self) -> IndexSet<TableRef>;
33}
34
35#[enum_dispatch::enum_dispatch(DynProofPlan)]
36pub trait ProverEvaluate {
37    /// Evaluate the query, modify `FirstRoundBuilder` and return the result.
38    fn first_round_evaluate<'a, S: Scalar>(
39        &self,
40        builder: &mut FirstRoundBuilder<'a, S>,
41        alloc: &'a Bump,
42        table_map: &IndexMap<TableRef, Table<'a, S>>,
43        params: &[LiteralValue],
44    ) -> PlaceholderResult<Table<'a, S>>;
45
46    /// Evaluate the query and modify `FinalRoundBuilder` to store an intermediate representation
47    /// of the query result and track all the components needed to form the query's proof.
48    ///
49    /// Intermediate values that are needed to form the proof are allocated into the arena
50    /// allocator alloc. These intermediate values will persist through proof creation and
51    /// will be bulk deallocated once the proof is formed.
52    fn final_round_evaluate<'a, S: Scalar>(
53        &self,
54        builder: &mut FinalRoundBuilder<'a, S>,
55        alloc: &'a Bump,
56        table_map: &IndexMap<TableRef, Table<'a, S>>,
57        params: &[LiteralValue],
58    ) -> PlaceholderResult<Table<'a, S>>;
59}
60
61/// Marker used as a trait bound for generic [`ProofPlan`] types to indicate the honesty of their implementation.
62///
63/// This allows us to define alternative prover implementations that misbehave, and test that the verifier rejects their results.
64pub trait ProverHonestyMarker: Debug + Send + Sync + PartialEq + 'static {}
65
66/// [`ProverHonestyMarker`] for generic [`ProofPlan`] types whose implementation is canonical/honest.
67#[derive(Debug, PartialEq, Clone)]
68pub struct HonestProver;
69impl ProverHonestyMarker for HonestProver {}