arnaucube
arnaucube/testudoPublic Self-hosted repository
testudo/src/nizk/bullet.rs
src/nizk/bullet.rsRaw
  1//! This module is an adaptation of code from the bulletproofs crate.
  2//! See NOTICE.md for more details
  3#![allow(non_snake_case)]
  4#![allow(clippy::type_complexity)]
  5#![allow(clippy::too_many_arguments)]
  6use super::super::errors::ProofVerifyError;
  7use super::super::group::{CompressedGroup, GroupElement, VartimeMultiscalarMul};
  8use super::super::math::Math;
  9use super::super::scalar::Scalar;
 10use super::super::transcript::ProofTranscript;
 11use core::iter;
 12use merlin::Transcript;
 13use serde::{Deserialize, Serialize};
 14
 15#[derive(Debug, Serialize, Deserialize)]
 16pub struct BulletReductionProof {
 17  L_vec: Vec<CompressedGroup>,
 18  R_vec: Vec<CompressedGroup>,
 19}
 20
 21impl BulletReductionProof {
 22  /// Create an inner-product proof.
 23  ///
 24  /// The proof is created with respect to the bases \\(G\\).
 25  ///
 26  /// The `transcript` is passed in as a parameter so that the
 27  /// challenges depend on the *entire* transcript (including parent
 28  /// protocols).
 29  ///
 30  /// The lengths of the vectors must all be the same, and must all be
 31  /// either 0 or a power of 2.
 32  pub fn prove(
 33    transcript: &mut Transcript,
 34    Q: &GroupElement,
 35    G_vec: &[GroupElement],
 36    H: &GroupElement,
 37    a_vec: &[Scalar],
 38    b_vec: &[Scalar],
 39    blind: &Scalar,
 40    blinds_vec: &[(Scalar, Scalar)],
 41  ) -> (
 42    BulletReductionProof,
 43    GroupElement,
 44    Scalar,
 45    Scalar,
 46    GroupElement,
 47    Scalar,
 48  ) {
 49    // Create slices G, H, a, b backed by their respective
 50    // vectors.  This lets us reslice as we compress the lengths
 51    // of the vectors in the main loop below.
 52    let mut G = &mut G_vec.to_owned()[..];
 53    let mut a = &mut a_vec.to_owned()[..];
 54    let mut b = &mut b_vec.to_owned()[..];
 55
 56    // All of the input vectors must have a length that is a power of two.
 57    let mut n = G.len();
 58    assert!(n.is_power_of_two());
 59    let lg_n = n.log2();
 60
 61    let G_factors: Vec<Scalar> = iter::repeat(Scalar::one()).take(n).collect();
 62
 63    // All of the input vectors must have the same length.
 64    assert_eq!(G.len(), n);
 65    assert_eq!(a.len(), n);
 66    assert_eq!(b.len(), n);
 67    assert_eq!(G_factors.len(), n);
 68    assert_eq!(blinds_vec.len(), 2 * lg_n);
 69
 70    //transcript.innerproduct_domain_sep(n as u64);
 71
 72    let mut L_vec = Vec::with_capacity(lg_n);
 73    let mut R_vec = Vec::with_capacity(lg_n);
 74    let mut blinds_iter = blinds_vec.iter();
 75    let mut blind_fin = *blind;
 76
 77    while n != 1 {
 78      n /= 2;
 79      let (a_L, a_R) = a.split_at_mut(n);
 80      let (b_L, b_R) = b.split_at_mut(n);
 81      let (G_L, G_R) = G.split_at_mut(n);
 82
 83      let c_L = inner_product(&a_L, &b_R);
 84      let c_R = inner_product(&a_R, &b_L);
 85
 86      let (blind_L, blind_R) = blinds_iter.next().unwrap();
 87
 88      let L = GroupElement::vartime_multiscalar_mul(
 89        a_L
 90          .iter()
 91          .chain(iter::once(&c_L))
 92          .chain(iter::once(blind_L)),
 93        G_R.iter().chain(iter::once(Q)).chain(iter::once(H)),
 94      );
 95
 96      let R = GroupElement::vartime_multiscalar_mul(
 97        a_R
 98          .iter()
 99          .chain(iter::once(&c_R))
100          .chain(iter::once(blind_R)),
101        G_L.iter().chain(iter::once(Q)).chain(iter::once(H)),
102      );
103
104      transcript.append_point(b"L", &L.compress());
105      transcript.append_point(b"R", &R.compress());
106
107      let u = transcript.challenge_scalar(b"u");
108      let u_inv = u.invert().unwrap();
109
110      for i in 0..n {
111        a_L[i] = a_L[i] * u + u_inv * a_R[i];
112        b_L[i] = b_L[i] * u_inv + u * b_R[i];
113        G_L[i] = GroupElement::vartime_multiscalar_mul(&[u_inv, u], &[G_L[i], G_R[i]]);
114      }
115
116      blind_fin = blind_fin + blind_L * u * u + blind_R * u_inv * u_inv;
117
118      L_vec.push(L.compress());
119      R_vec.push(R.compress());
120
121      a = a_L;
122      b = b_L;
123      G = G_L;
124    }
125
126    let Gamma_hat =
127      GroupElement::vartime_multiscalar_mul(&[a[0], a[0] * b[0], blind_fin], &[G[0], *Q, *H]);
128
129    (
130      BulletReductionProof { L_vec, R_vec },
131      Gamma_hat,
132      a[0],
133      b[0],
134      G[0],
135      blind_fin,
136    )
137  }
138
139  /// Computes three vectors of verification scalars \\([u\_{i}^{2}]\\), \\([u\_{i}^{-2}]\\) and \\([s\_{i}]\\) for combined multiscalar multiplication
140  /// in a parent protocol. See [inner product protocol notes](index.html#verification-equation) for details.
141  /// The verifier must provide the input length \\(n\\) explicitly to avoid unbounded allocation within the inner product proof.
142  fn verification_scalars(
143    &self,
144    n: usize,
145    transcript: &mut Transcript,
146  ) -> Result<(Vec<Scalar>, Vec<Scalar>, Vec<Scalar>), ProofVerifyError> {
147    let lg_n = self.L_vec.len();
148    if lg_n >= 32 {
149      // 4 billion multiplications should be enough for anyone
150      // and this check prevents overflow in 1<<lg_n below.
151      return Err(ProofVerifyError::InternalError);
152    }
153    if n != (1 << lg_n) {
154      return Err(ProofVerifyError::InternalError);
155    }
156
157    // 1. Recompute x_k,...,x_1 based on the proof transcript
158    let mut challenges = Vec::with_capacity(lg_n);
159    for (L, R) in self.L_vec.iter().zip(self.R_vec.iter()) {
160      transcript.append_point(b"L", L);
161      transcript.append_point(b"R", R);
162      challenges.push(transcript.challenge_scalar(b"u"));
163    }
164
165    // 2. Compute 1/(u_k...u_1) and 1/u_k, ..., 1/u_1
166    let mut challenges_inv = challenges.clone();
167    let allinv = Scalar::batch_invert(&mut challenges_inv);
168
169    // 3. Compute u_i^2 and (1/u_i)^2
170    for i in 0..lg_n {
171      challenges[i] = challenges[i].square();
172      challenges_inv[i] = challenges_inv[i].square();
173    }
174    let challenges_sq = challenges;
175    let challenges_inv_sq = challenges_inv;
176
177    // 4. Compute s values inductively.
178    let mut s = Vec::with_capacity(n);
179    s.push(allinv);
180    for i in 1..n {
181      let lg_i = (32 - 1 - (i as u32).leading_zeros()) as usize;
182      let k = 1 << lg_i;
183      // The challenges are stored in "creation order" as [u_k,...,u_1],
184      // so u_{lg(i)+1} = is indexed by (lg_n-1) - lg_i
185      let u_lg_i_sq = challenges_sq[(lg_n - 1) - lg_i];
186      s.push(s[i - k] * u_lg_i_sq);
187    }
188
189    Ok((challenges_sq, challenges_inv_sq, s))
190  }
191
192  /// This method is for testing that proof generation work,
193  /// but for efficiency the actual protocols would use `verification_scalars`
194  /// method to combine inner product verification with other checks
195  /// in a single multiscalar multiplication.
196  pub fn verify(
197    &self,
198    n: usize,
199    a: &[Scalar],
200    transcript: &mut Transcript,
201    Gamma: &GroupElement,
202    G: &[GroupElement],
203  ) -> Result<(GroupElement, GroupElement, Scalar), ProofVerifyError> {
204    let (u_sq, u_inv_sq, s) = self.verification_scalars(n, transcript)?;
205
206    let Ls = self
207      .L_vec
208      .iter()
209      .map(|p| p.decompress().ok_or(ProofVerifyError::InternalError))
210      .collect::<Result<Vec<_>, _>>()?;
211
212    let Rs = self
213      .R_vec
214      .iter()
215      .map(|p| p.decompress().ok_or(ProofVerifyError::InternalError))
216      .collect::<Result<Vec<_>, _>>()?;
217
218    let G_hat = GroupElement::vartime_multiscalar_mul(s.iter(), G.iter());
219    let a_hat = inner_product(a, &s);
220
221    let Gamma_hat = GroupElement::vartime_multiscalar_mul(
222      u_sq
223        .iter()
224        .chain(u_inv_sq.iter())
225        .chain(iter::once(&Scalar::one())),
226      Ls.iter().chain(Rs.iter()).chain(iter::once(Gamma)),
227    );
228
229    Ok((G_hat, Gamma_hat, a_hat))
230  }
231}
232
233/// Computes an inner product of two vectors
234/// \\[
235///    {\langle {\mathbf{a}}, {\mathbf{b}} \rangle} = \sum\_{i=0}^{n-1} a\_i \cdot b\_i.
236/// \\]
237/// Panics if the lengths of \\(\mathbf{a}\\) and \\(\mathbf{b}\\) are not equal.
238pub fn inner_product(a: &[Scalar], b: &[Scalar]) -> Scalar {
239  let mut out = Scalar::zero();
240  if a.len() != b.len() {
241    panic!("inner_product(a,b): lengths of vectors do not match");
242  }
243  for i in 0..a.len() {
244    out += a[i] * b[i];
245  }
246  out
247}