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