proof_of_sql/sql/proof/
proof_plan.rs

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