proof_of_sql/proof_primitive/dory/
dynamic_dory_commitment.rs

1//! Module containing the `DoryCommitment` type and its implementation.
2//!
3//! While this can be used as a black box, it can be helpful to understand the underlying structure of the commitment.
4//! Ultimately, the commitment is a commitment to a Matrix. This matrix is filled out from a column in the following fashion.
5//!
6//! We let `sigma` be a parameter that specifies the number of non-zero columns in the matrix.
7//! More specifically, the number of non-zero columns is `2^sigma`.
8//!
9//! For an example, we will set `sigma=2` and thus, the number of columns is 4.
10//! The column `[100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115]` with offset 9 is converted to the following matrix:
11//! ```ignore
12//!  0   0   0   0
13//!  0   0   0   0
14//!  0  100 101 102
15//! 103 104 105 106
16//! 107 108 109 110
17//! 111 112 113 114
18//! 115  0   0   0
19//! ```
20//! This matrix is then committed to using a matrix commitment.
21//!
22//! Note: the `VecCommitmentExt` trait requires using this offset when computing commitments.
23//! This is to allow for updateability of the commitments as well as to allow for smart indexing/partitioning.
24
25use super::{DoryScalar, ProverSetup, GT};
26use crate::base::{
27    commitment::{Commitment, CommittableColumn},
28    impl_serde_for_ark_serde_checked,
29};
30use alloc::vec::Vec;
31use ark_ec::pairing::PairingOutput;
32use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
33use core::ops::Mul;
34use derive_more::{AddAssign, Neg, Sub, SubAssign};
35use num_traits::One;
36
37#[derive(
38    Debug,
39    Sub,
40    Eq,
41    PartialEq,
42    Neg,
43    Copy,
44    Clone,
45    AddAssign,
46    SubAssign,
47    CanonicalSerialize,
48    CanonicalDeserialize,
49)]
50/// The Dory commitment type.
51pub struct DynamicDoryCommitment(pub(super) GT);
52
53/// The default for GT is the the additive identity, but should be the multiplicative identity.
54impl Default for DynamicDoryCommitment {
55    fn default() -> Self {
56        Self(PairingOutput(One::one()))
57    }
58}
59
60// Traits required for `DoryCommitment` to impl `Commitment`.
61impl_serde_for_ark_serde_checked!(DynamicDoryCommitment);
62impl Mul<DynamicDoryCommitment> for DoryScalar {
63    type Output = DynamicDoryCommitment;
64    fn mul(self, rhs: DynamicDoryCommitment) -> Self::Output {
65        DynamicDoryCommitment(rhs.0 * self.0)
66    }
67}
68impl<'a> Mul<&'a DynamicDoryCommitment> for DoryScalar {
69    type Output = DynamicDoryCommitment;
70    fn mul(self, rhs: &'a DynamicDoryCommitment) -> Self::Output {
71        DynamicDoryCommitment(rhs.0 * self.0)
72    }
73}
74impl Commitment for DynamicDoryCommitment {
75    type Scalar = DoryScalar;
76    type PublicSetup<'a> = &'a ProverSetup<'a>;
77
78    fn compute_commitments(
79        committable_columns: &[CommittableColumn],
80        offset: usize,
81        setup: &Self::PublicSetup<'_>,
82    ) -> Vec<Self> {
83        super::compute_dynamic_dory_commitments(committable_columns, offset, setup)
84    }
85
86    fn to_transcript_bytes(&self) -> Vec<u8> {
87        let mut buf = Vec::with_capacity(self.0.compressed_size());
88        self.0.serialize_compressed(&mut buf).unwrap();
89        buf
90    }
91}
92
93#[cfg(test)]
94mod tests {
95    use super::{DynamicDoryCommitment, GT};
96    use crate::base::commitment::Commitment;
97    use ark_ff::UniformRand;
98    use rand::{rngs::StdRng, SeedableRng};
99
100    #[test]
101    fn we_get_different_transcript_bytes_from_different_dynamic_dory_commitments() {
102        let mut rng = StdRng::seed_from_u64(42);
103        let commitment1 = DynamicDoryCommitment(GT::rand(&mut rng));
104        let commitment2 = DynamicDoryCommitment(GT::rand(&mut rng));
105        assert_ne!(
106            commitment1.to_transcript_bytes(),
107            commitment2.to_transcript_bytes()
108        );
109    }
110}