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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//! Types for creation and utilization of cryptographic commitments to proof-of-sql data.
use crate::base::scalar::Scalar;
#[cfg(feature = "blitzar")]
pub use blitzar::{
    compute::{init_backend, init_backend_with_config, BackendConfig},
    proof::InnerProductProof,
};
use core::ops::{AddAssign, SubAssign};
use curve25519_dalek::ristretto::RistrettoPoint;

mod committable_column;
pub(crate) use committable_column::CommittableColumn;

mod vec_commitment_ext;
pub use vec_commitment_ext::{NumColumnsMismatch, VecCommitmentExt};

mod column_bounds;
use super::scalar::Curve25519Scalar;
pub use column_bounds::{Bounds, ColumnBounds, NegativeBounds};

mod column_commitment_metadata;
pub use column_commitment_metadata::ColumnCommitmentMetadata;

mod column_commitment_metadata_map;
pub use column_commitment_metadata_map::{
    ColumnCommitmentMetadataMap, ColumnCommitmentMetadataMapExt, ColumnCommitmentsMismatch,
};

mod column_commitments;
pub use column_commitments::{
    AppendColumnCommitmentsError, ColumnCommitments, DuplicateIdentifiers,
};

mod table_commitment;
pub use table_commitment::{
    AppendTableCommitmentError, MixedLengthColumns, NegativeRange, TableCommitment,
    TableCommitmentArithmeticError, TableCommitmentFromColumnsError,
};

mod query_commitments;
pub use query_commitments::{QueryCommitments, QueryCommitmentsExt};

/// A trait for using commitment schemes generically.
pub trait Commitment:
    AddAssign
    + SubAssign
    + Sized
    + Default
    + Copy
    + core::ops::Neg<Output = Self>
    + Eq
    + core::ops::Sub<Output = Self>
    + core::fmt::Debug
    + std::marker::Sync
    + std::marker::Send
{
    /// The associated scalar that the commitment is for.
    /// There are multiple possible commitment schemes for a scalar, but only one scalar for any commitment.
    type Scalar: Scalar
        + for<'a> core::ops::Mul<&'a Self, Output = Self>
        + core::ops::Mul<Self, Output = Self>
        + serde::Serialize
        + for<'a> serde::Deserialize<'a>;

    /// The public setup for the commitment scheme.
    type PublicSetup;

    /// Compute the commitments for the given columns.
    fn compute_commitments(
        commitments: &mut [Self],
        committable_columns: &[CommittableColumn],
        offset: usize,
        setup: &Self::PublicSetup,
    );

    /// Compute a linear combination of the given commitments: `sum commitment[i] * multiplier[i]`.
    fn fold_commitments(commitments: &[Self], multipliers: &[Self::Scalar]) -> Self;
}

impl Commitment for RistrettoPoint {
    type Scalar = Curve25519Scalar;
    type PublicSetup = ();
    #[cfg(feature = "blitzar")]
    fn compute_commitments(
        commitments: &mut [Self],
        committable_columns: &[CommittableColumn],
        offset: usize,
        _setup: &Self::PublicSetup,
    ) {
        let sequences = Vec::from_iter(committable_columns.iter().map(Into::into));
        let mut compressed_commitments = vec![Default::default(); committable_columns.len()];
        blitzar::compute::compute_curve25519_commitments(
            &mut compressed_commitments,
            &sequences,
            offset as u64,
        );
        commitments
            .iter_mut()
            .zip(compressed_commitments.iter())
            .for_each(|(c, cc)| {
                *c = cc.decompress().expect(
                    "invalid ristretto point decompression in Commitment::compute_commitments",
                );
            });
    }
    #[cfg(not(feature = "blitzar"))]
    fn compute_commitments(
        _commitments: &mut [Self],
        _committable_columns: &[CommittableColumn],
        _offset: usize,
        _setup: &Self::PublicSetup,
    ) {
        unimplemented!()
    }

    fn fold_commitments(commitments: &[Self], multipliers: &[Self::Scalar]) -> Self {
        commitments
            .iter()
            .zip(multipliers.iter())
            .map(|(c, m)| *m * c)
            .fold(Default::default(), |mut a, c| {
                a += c;
                a
            })
    }
}

mod commitment_evaluation_proof;
pub use commitment_evaluation_proof::CommitmentEvaluationProof;
#[cfg(test)]
pub(crate) mod commitment_evaluation_proof_test;