proof_of_sql/proof_primitive/dory/
dynamic_dory_commitment.rs1use 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)]
50pub struct DynamicDoryCommitment(pub(super) GT);
52
53impl Default for DynamicDoryCommitment {
55 fn default() -> Self {
56 Self(PairingOutput(One::one()))
57 }
58}
59
60impl_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::{
97 base::{
98 commitment::{ColumnCommitments, Commitment, TableCommitment},
99 database::{
100 owned_table_utility::{
101 bigint, boolean, decimal75, int, int128, owned_table, scalar, smallint,
102 timestamptz, tinyint, uint8, varbinary, varchar,
103 },
104 OwnedTable,
105 },
106 posql_time::{PoSQLTimeUnit, PoSQLTimeZone},
107 try_standard_binary_deserialization, try_standard_binary_serialization,
108 },
109 proof_primitive::dory::{test_rng, DoryScalar, ProverSetup, PublicParameters},
110 };
111 use ark_ff::UniformRand;
112 use rand::{rngs::StdRng, SeedableRng};
113
114 #[test]
115 fn we_get_different_transcript_bytes_from_different_dynamic_dory_commitments() {
116 let mut rng = StdRng::seed_from_u64(42);
117 let commitment1 = DynamicDoryCommitment(GT::rand(&mut rng));
118 let commitment2 = DynamicDoryCommitment(GT::rand(&mut rng));
119 assert_ne!(
120 commitment1.to_transcript_bytes(),
121 commitment2.to_transcript_bytes()
122 );
123 }
124
125 #[test]
128 fn commitment_serialization_does_not_change() {
129 let expected_serialization =
130 include_bytes!("./test_table_commitmet_do_not_modify.bin").to_vec();
131 let public_parameters = PublicParameters::test_rand(5, &mut test_rng());
132 let setup = ProverSetup::from(&public_parameters);
133
134 let base_table: OwnedTable<DoryScalar> = owned_table([
135 uint8("uint8_column", [1, 2, 3, 4]),
136 tinyint("tinyint_column", [1, -2, 3, 4]),
137 smallint("smallint_column", [1i16, 2, -3, 4]),
138 int("int_column", [1, 2, 3, -14]),
139 bigint("bigint_column", [1, 2, -333, 4]),
140 int128("int128_column", [1, 2, 3, i128::MIN]),
141 boolean("bool_column", [true, true, true, false]),
142 decimal75("decimal_column", 3, 1, [1, 300, -1, 2]),
143 varchar("varchar_column", ["Lorem", "ipsum", "dolor", "sit"]),
144 scalar("scalar_column", [1, 3, -1, 2]),
145 timestamptz(
146 "timestamp_column",
147 PoSQLTimeUnit::Second,
148 PoSQLTimeZone::utc(),
149 [-18, -17, 17, 18],
150 ),
151 varbinary(
152 "varbinary_column",
153 [
154 [1, 2, 3, 0].as_slice(),
155 &[4, 5, 6, 7],
156 &[4, 5, u8::MAX, 7],
157 &[4, 0, 6, 7],
158 ],
159 ),
160 ]);
161 let base_commitments =
162 ColumnCommitments::<DynamicDoryCommitment>::try_from_columns_with_offset(
163 base_table.inner_table(),
164 0,
165 &&setup,
166 )
167 .unwrap();
168 let table_commitment = TableCommitment::try_new(base_commitments, 0..4).unwrap();
169 let serialized_table_commitment =
170 try_standard_binary_serialization(table_commitment.clone()).unwrap();
171 assert_eq!(serialized_table_commitment, expected_serialization);
172 let (deserialized_table_commitment, _) = try_standard_binary_deserialization::<
173 TableCommitment<DynamicDoryCommitment>,
174 >(&serialized_table_commitment)
175 .unwrap();
176 assert_eq!(deserialized_table_commitment, table_commitment);
177 }
178}