proof_of_sql/sql/proof/
proof_plan.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use super::{CountBuilder, FinalRoundBuilder, FirstRoundBuilder, VerificationBuilder};
use crate::base::{
    commitment::Commitment,
    database::{
        Column, ColumnField, ColumnRef, CommitmentAccessor, DataAccessor, MetadataAccessor,
        OwnedTable, TableRef,
    },
    map::IndexSet,
    proof::ProofError,
    scalar::Scalar,
};
use alloc::vec::Vec;
use bumpalo::Bump;
use core::fmt::Debug;

/// Provable nodes in the provable AST.
#[enum_dispatch::enum_dispatch(DynProofPlan)]
pub trait ProofPlan: Debug + Send + Sync + ProverEvaluate {
    /// Count terms used within the Query's proof
    fn count(&self, builder: &mut CountBuilder) -> Result<(), ProofError>;

    /// Check if the input table is empty
    fn is_empty(&self, accessor: &dyn MetadataAccessor) -> bool {
        let table_refs = self.get_table_references();
        table_refs
            .iter()
            .all(|table_ref| accessor.get_length(*table_ref) == 0)
    }

    /// Form components needed to verify and proof store into `VerificationBuilder`
    fn verifier_evaluate<C: Commitment>(
        &self,
        builder: &mut VerificationBuilder<C>,
        accessor: &dyn CommitmentAccessor<C>,
        result: Option<&OwnedTable<C::Scalar>>,
    ) -> Result<Vec<C::Scalar>, ProofError>;

    /// Return all the result column fields
    fn get_column_result_fields(&self) -> Vec<ColumnField>;

    /// Return all the columns referenced in the Query
    fn get_column_references(&self) -> IndexSet<ColumnRef>;

    /// Return all the tables referenced in the Query
    fn get_table_references(&self) -> IndexSet<TableRef>;
}

#[enum_dispatch::enum_dispatch(DynProofPlan)]
pub trait ProverEvaluate {
    /// Evaluate the query and modify `FirstRoundBuilder` to track the result of the query.
    fn result_evaluate<'a, S: Scalar>(
        &self,
        input_length: usize,
        alloc: &'a Bump,
        accessor: &'a dyn DataAccessor<S>,
    ) -> Vec<Column<'a, S>>;

    /// Evaluate the query and modify `FirstRoundBuilder` to form the query's proof.
    fn first_round_evaluate(&self, builder: &mut FirstRoundBuilder);

    /// Evaluate the query and modify `FinalRoundBuilder` to store an intermediate representation
    /// of the query result and track all the components needed to form the query's proof.
    ///
    /// Intermediate values that are needed to form the proof are allocated into the arena
    /// allocator alloc. These intermediate values will persist through proof creation and
    /// will be bulk deallocated once the proof is formed.
    fn final_round_evaluate<'a, S: Scalar>(
        &self,
        builder: &mut FinalRoundBuilder<'a, S>,
        alloc: &'a Bump,
        accessor: &'a dyn DataAccessor<S>,
    ) -> Vec<Column<'a, S>>;
}

/// Marker used as a trait bound for generic [`ProofPlan`] types to indicate the honesty of their implementation.
///
/// This allows us to define alternative prover implementations that misbehave, and test that the verifier rejects their results.
pub trait ProverHonestyMarker: Debug + Send + Sync + PartialEq + 'static {}

/// [`ProverHonestyMarker`] for generic [`ProofPlan`] types whose implementation is canonical/honest.
#[derive(Debug, PartialEq)]
pub struct HonestProver;
impl ProverHonestyMarker for HonestProver {}