monero_bulletproofs/original/
mod.rs1#![allow(clippy::many_single_char_names)]
2
3use std_shims::{sync::LazyLock, vec::Vec};
4
5use rand_core::{RngCore, CryptoRng};
6
7use zeroize::Zeroize;
8
9use curve25519_dalek::{constants::ED25519_BASEPOINT_POINT, Scalar, EdwardsPoint};
10
11use monero_ed25519::{CompressedPoint, Commitment};
12use monero_bulletproofs_generators::{Generators, COMMITMENT_BITS};
13
14use crate::{
15 core::{MAX_COMMITMENTS, multiexp},
16 scalar_vector::ScalarVector,
17 MONERO_H, BulletproofsBatchVerifier,
18};
19
20pub(crate) mod inner_product;
21use inner_product::*;
22pub(crate) use inner_product::IpProof;
23
24include!(concat!(env!("OUT_DIR"), "/generators.rs"));
25
26const INV_EIGHT: monero_ed25519::Scalar = monero_ed25519::Scalar::INV_EIGHT;
27
28#[derive(Clone, Debug)]
29pub(crate) struct AggregateRangeStatement<'a> {
30 commitments: &'a [EdwardsPoint],
31}
32
33#[derive(Clone)]
34pub(crate) struct AggregateRangeWitness {
35 commitments: Vec<Commitment>,
36}
37
38#[doc(hidden)]
40#[derive(Clone, PartialEq, Eq, Debug, Zeroize)]
41pub struct AggregateRangeProof {
42 pub(crate) A: CompressedPoint,
43 pub(crate) S: CompressedPoint,
44 pub(crate) T1: CompressedPoint,
45 pub(crate) T2: CompressedPoint,
46 pub(crate) tau_x: Scalar,
47 pub(crate) mu: Scalar,
48 pub(crate) t_hat: Scalar,
49 pub(crate) ip: IpProof,
50}
51
52impl<'a> AggregateRangeStatement<'a> {
53 pub(crate) fn new(commitments: &'a [EdwardsPoint]) -> Option<Self> {
54 if commitments.is_empty() || (commitments.len() > MAX_COMMITMENTS) {
55 None?;
56 }
57 Some(Self { commitments })
58 }
59}
60
61impl AggregateRangeWitness {
62 pub(crate) fn new(commitments: Vec<Commitment>) -> Option<Self> {
63 if commitments.is_empty() || (commitments.len() > MAX_COMMITMENTS) {
64 None?;
65 }
66 Some(Self { commitments })
67 }
68}
69
70impl AggregateRangeStatement<'_> {
71 fn initial_transcript(&self) -> (Scalar, Vec<EdwardsPoint>) {
72 let V = self.commitments.iter().map(|c| c * INV_EIGHT.into()).collect::<Vec<_>>();
73 (
74 monero_ed25519::Scalar::hash(
75 V.iter().flat_map(|V| V.compress().to_bytes()).collect::<Vec<_>>(),
76 )
77 .into(),
78 V,
79 )
80 }
81
82 fn transcript_A_S(
83 transcript: Scalar,
84 A: CompressedPoint,
85 S: CompressedPoint,
86 ) -> (Scalar, Scalar) {
87 let mut buf = Vec::with_capacity(96);
88 buf.extend(transcript.to_bytes());
89 buf.extend(A.to_bytes());
90 buf.extend(S.to_bytes());
91 let y = monero_ed25519::Scalar::hash(buf).into();
92 let z = monero_ed25519::Scalar::hash(y.to_bytes()).into();
93 (y, z)
94 }
95
96 fn transcript_T12(transcript: Scalar, T1: CompressedPoint, T2: CompressedPoint) -> Scalar {
97 let mut buf = Vec::with_capacity(128);
98 buf.extend(transcript.to_bytes());
99 buf.extend(transcript.to_bytes());
100 buf.extend(T1.to_bytes());
101 buf.extend(T2.to_bytes());
102 monero_ed25519::Scalar::hash(buf).into()
103 }
104
105 fn transcript_tau_x_mu_t_hat(
106 transcript: Scalar,
107 tau_x: Scalar,
108 mu: Scalar,
109 t_hat: Scalar,
110 ) -> Scalar {
111 let mut buf = Vec::with_capacity(128);
112 buf.extend(transcript.to_bytes());
113 buf.extend(transcript.to_bytes());
114 buf.extend(tau_x.to_bytes());
115 buf.extend(mu.to_bytes());
116 buf.extend(t_hat.to_bytes());
117 monero_ed25519::Scalar::hash(buf).into()
118 }
119
120 #[allow(clippy::needless_pass_by_value)]
121 pub(crate) fn prove(
122 self,
123 rng: &mut (impl RngCore + CryptoRng),
124 witness: AggregateRangeWitness,
125 ) -> Option<AggregateRangeProof> {
126 if self.commitments !=
127 witness.commitments.iter().map(|commitment| commitment.commit().into()).collect::<Vec<_>>()
128 {
129 None?;
130 }
131
132 let generators = &GENERATORS;
133
134 let (mut transcript, _) = self.initial_transcript();
135
136 let mut padded_pow_of_2 = 1;
138 while padded_pow_of_2 < witness.commitments.len() {
139 padded_pow_of_2 <<= 1;
140 }
141
142 let mut aL = ScalarVector::new(padded_pow_of_2 * COMMITMENT_BITS);
143 for (i, commitment) in witness.commitments.iter().enumerate() {
144 let mut amount = commitment.amount;
145 for j in 0 .. COMMITMENT_BITS {
146 aL[(i * COMMITMENT_BITS) + j] = Scalar::from(amount & 1);
147 amount >>= 1;
148 }
149 }
150 let aR = aL.clone() - Scalar::ONE;
151
152 let alpha = monero_ed25519::Scalar::random(&mut *rng).into();
153
154 let A = CompressedPoint::from(
155 {
156 let mut terms = Vec::with_capacity(1 + (2 * aL.len()));
157 terms.push((alpha, ED25519_BASEPOINT_POINT));
158 for (aL, G) in aL.0.iter().zip(&generators.G) {
159 terms.push((*aL, *G));
160 }
161 for (aR, H) in aR.0.iter().zip(&generators.H) {
162 terms.push((*aR, *H));
163 }
164 let res = multiexp(&terms) * INV_EIGHT.into();
165 terms.zeroize();
166 res
167 }
168 .compress()
169 .to_bytes(),
170 );
171
172 let mut sL = ScalarVector::new(padded_pow_of_2 * COMMITMENT_BITS);
173 let mut sR = ScalarVector::new(padded_pow_of_2 * COMMITMENT_BITS);
174 for i in 0 .. (padded_pow_of_2 * COMMITMENT_BITS) {
175 sL[i] = monero_ed25519::Scalar::random(&mut *rng).into();
176 sR[i] = monero_ed25519::Scalar::random(&mut *rng).into();
177 }
178 let rho = monero_ed25519::Scalar::random(&mut *rng).into();
179
180 let S = CompressedPoint::from(
181 {
182 let mut terms = Vec::with_capacity(1 + (2 * sL.len()));
183 terms.push((rho, ED25519_BASEPOINT_POINT));
184 for (sL, G) in sL.0.iter().zip(&generators.G) {
185 terms.push((*sL, *G));
186 }
187 for (sR, H) in sR.0.iter().zip(&generators.H) {
188 terms.push((*sR, *H));
189 }
190 let res = multiexp(&terms) * INV_EIGHT.into();
191 terms.zeroize();
192 res
193 }
194 .compress()
195 .to_bytes(),
196 );
197
198 let (y, z) = Self::transcript_A_S(transcript, A, S);
199 transcript = z;
200 let z = ScalarVector::powers(z, 3 + padded_pow_of_2);
201
202 let twos = ScalarVector::powers(Scalar::from(2u8), COMMITMENT_BITS);
203
204 let l = [aL - z[1], sL];
205 let y_pow_n = ScalarVector::powers(y, aR.len());
206 let mut r = [((aR + z[1]) * &y_pow_n), sR * &y_pow_n];
207 {
208 for j in 0 .. padded_pow_of_2 {
209 for i in 0 .. COMMITMENT_BITS {
210 r[0].0[(j * COMMITMENT_BITS) + i] += z[2 + j] * twos[i];
211 }
212 }
213 }
214 let t1 = (l[0].clone().inner_product(&r[1])) + (r[0].clone().inner_product(&l[1]));
215 let t2 = l[1].clone().inner_product(&r[1]);
216
217 let tau_1 = monero_ed25519::Scalar::random(&mut *rng).into();
218 let T1 = CompressedPoint::from(
219 {
220 let mut T1_terms = [(t1, *MONERO_H), (tau_1, ED25519_BASEPOINT_POINT)];
221 for term in &mut T1_terms {
222 term.0 *= INV_EIGHT.into();
223 }
224 let T1 = multiexp(&T1_terms);
225 T1_terms.zeroize();
226 T1
227 }
228 .compress()
229 .to_bytes(),
230 );
231 let tau_2 = monero_ed25519::Scalar::random(&mut *rng).into();
232 let T2 = CompressedPoint::from(
233 {
234 let mut T2_terms = [(t2, *MONERO_H), (tau_2, ED25519_BASEPOINT_POINT)];
235 for term in &mut T2_terms {
236 term.0 *= INV_EIGHT.into();
237 }
238 let T2 = multiexp(&T2_terms);
239 T2_terms.zeroize();
240 T2
241 }
242 .compress()
243 .to_bytes(),
244 );
245
246 transcript = Self::transcript_T12(transcript, T1, T2);
247 let x = transcript;
248
249 let [l0, l1] = l;
250 let l = l0 + &(l1 * x);
251 let [r0, r1] = r;
252 let r = r0 + &(r1 * x);
253 let t_hat = l.clone().inner_product(&r);
254 let mut tau_x = ((tau_2 * x) + tau_1) * x;
255 {
256 for (i, commitment) in witness.commitments.iter().enumerate() {
257 tau_x += z[2 + i] * commitment.mask.into();
258 }
259 }
260 let mu = alpha + (rho * x);
261
262 let y_inv_pow_n = ScalarVector::powers(y.invert(), l.len());
263
264 transcript = Self::transcript_tau_x_mu_t_hat(transcript, tau_x, mu, t_hat);
265 let x_ip = transcript;
266
267 let ip = IpStatement::new_without_P_transcript(y_inv_pow_n, x_ip)
268 .prove(
269 transcript,
270 IpWitness::new(l, r).expect("Bulletproofs::Original created an invalid IpWitness"),
271 )
272 .expect("Bulletproofs::Original failed to prove the inner-product");
273
274 let res = AggregateRangeProof { A, S, T1, T2, tau_x, mu, t_hat, ip };
275 #[cfg(debug_assertions)]
276 {
277 let mut verifier = BulletproofsBatchVerifier::default();
278 debug_assert!(self.verify(rng, &mut verifier, res.clone()));
279 debug_assert!(verifier.verify());
280 }
281 Some(res)
282 }
283
284 #[must_use]
285 pub(crate) fn verify(
286 self,
287 rng: &mut (impl RngCore + CryptoRng),
288 verifier: &mut BulletproofsBatchVerifier,
289 AggregateRangeProof { A, S, T1, T2, tau_x, mu, t_hat, ip }: AggregateRangeProof,
290 ) -> bool {
291 let mut padded_pow_of_2 = 1;
292 while padded_pow_of_2 < self.commitments.len() {
293 padded_pow_of_2 <<= 1;
294 }
295 let ip_rows = padded_pow_of_2 * COMMITMENT_BITS;
296
297 while verifier.0.g_bold.len() < ip_rows {
298 verifier.0.g_bold.push(Scalar::ZERO);
299 verifier.0.h_bold.push(Scalar::ZERO);
300 }
301
302 let (mut transcript, mut commitments) = self.initial_transcript();
303 for commitment in &mut commitments {
304 *commitment = commitment.mul_by_cofactor();
305 }
306
307 let (y, z) = Self::transcript_A_S(transcript, A, S);
308 transcript = z;
309 let z = ScalarVector::powers(z, 3 + padded_pow_of_2);
310 transcript = Self::transcript_T12(transcript, T1, T2);
311 let x = transcript;
312 transcript = Self::transcript_tau_x_mu_t_hat(transcript, tau_x, mu, t_hat);
313 let x_ip = transcript;
314
315 let decomp_mul_cofactor =
316 |p| CompressedPoint::decompress(&p).map(|p| EdwardsPoint::mul_by_cofactor(&p.into()));
317
318 let (Some(A), Some(S), Some(T1), Some(T2)) = (
319 decomp_mul_cofactor(A),
320 decomp_mul_cofactor(S),
321 decomp_mul_cofactor(T1),
322 decomp_mul_cofactor(T2),
323 ) else {
324 return false;
325 };
326
327 let y_pow_n = ScalarVector::powers(y, ip_rows);
328 let y_inv_pow_n = ScalarVector::powers(y.invert(), ip_rows);
329
330 let twos = ScalarVector::powers(Scalar::from(2u8), COMMITMENT_BITS);
331
332 {
334 let weight = monero_ed25519::Scalar::random(&mut *rng).into();
335 verifier.0.h += weight * t_hat;
336 verifier.0.g += weight * tau_x;
337
338 let weight = -weight;
341
342 verifier.0.h += weight * (z[1] - (z[2])) * y_pow_n.sum();
343
344 for (i, commitment) in commitments.iter().enumerate() {
345 verifier.0.other.push((weight * z[2 + i], *commitment));
346 }
347
348 for i in 0 .. padded_pow_of_2 {
349 verifier.0.h -= weight * z[3 + i] * twos.clone().sum();
350 }
351 verifier.0.other.push((weight * x, T1));
352 verifier.0.other.push((weight * (x * x), T2));
353 }
354
355 let ip_weight = monero_ed25519::Scalar::random(&mut *rng).into();
356
357 verifier.0.other.push((ip_weight, A));
359 verifier.0.other.push((ip_weight * x, S));
360 let ip_z = ip_weight * z[1];
364 for i in 0 .. ip_rows {
365 verifier.0.h_bold[i] += ip_z;
366 }
367 let neg_ip_z = -ip_z;
368 for i in 0 .. ip_rows {
369 verifier.0.g_bold[i] += neg_ip_z;
370 }
371 {
372 for j in 0 .. padded_pow_of_2 {
373 for i in 0 .. COMMITMENT_BITS {
374 let full_i = (j * COMMITMENT_BITS) + i;
375 verifier.0.h_bold[full_i] += ip_weight * y_inv_pow_n[full_i] * z[2 + j] * twos[i];
376 }
377 }
378 }
379 verifier.0.h += ip_weight * x_ip * t_hat;
380
381 verifier.0.g += ip_weight * -mu;
383 let res = IpStatement::new_without_P_transcript(y_inv_pow_n, x_ip)
384 .verify(verifier, ip_rows, transcript, ip_weight, ip);
385 res.is_ok()
386 }
387}