Skip to content

Commit

Permalink
Address Clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
srinathsetty committed Jul 24, 2020
1 parent eb969d5 commit 2bfc333
Show file tree
Hide file tree
Showing 18 changed files with 385 additions and 431 deletions.
2 changes: 1 addition & 1 deletion profiler/nizk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use merlin::Transcript;

pub fn main() {
// the list of number of variables (and constraints) in an R1CS instance
let inst_sizes = vec![12, 16, 20];
let inst_sizes = vec![10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];

println!("Profiler:: NIZK");
for &s in inst_sizes.iter() {
Expand Down
2 changes: 1 addition & 1 deletion profiler/snark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use merlin::Transcript;

pub fn main() {
// the list of number of variables (and constraints) in an R1CS instance
let inst_sizes = vec![12, 16, 20];
let inst_sizes = vec![10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];

println!("Profiler:: SNARK");
for &s in inst_sizes.iter() {
Expand Down
30 changes: 2 additions & 28 deletions src/commitments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,39 +71,13 @@ impl Commitments for Scalar {
impl Commitments for Vec<Scalar> {
fn commit(&self, blind: &Scalar, gens_n: &MultiCommitGens) -> GroupElement {
assert!(gens_n.n == self.len());
GroupElement::vartime_multiscalar_mul(self, &gens_n.G) + blind * &gens_n.h
GroupElement::vartime_multiscalar_mul(self, &gens_n.G) + blind * gens_n.h
}
}

impl Commitments for [Scalar] {
fn commit(&self, blind: &Scalar, gens_n: &MultiCommitGens) -> GroupElement {
assert_eq!(gens_n.n, self.len());
GroupElement::vartime_multiscalar_mul(self, &gens_n.G) + blind * &gens_n.h
}
}

impl Commitments for Vec<bool> {
fn commit(&self, blind: &Scalar, gens_n: &MultiCommitGens) -> GroupElement {
assert!(gens_n.n == self.len());
let mut comm = blind * &gens_n.h;
for i in 0..self.len() {
if self[i] {
comm = comm + gens_n.G[i];
}
}
comm
}
}

impl Commitments for [bool] {
fn commit(&self, blind: &Scalar, gens_n: &MultiCommitGens) -> GroupElement {
assert!(gens_n.n == self.len());
let mut comm = blind * &gens_n.h;
for i in 0..self.len() {
if self[i] {
comm = comm + gens_n.G[i];
}
}
comm
GroupElement::vartime_multiscalar_mul(self, &gens_n.G) + blind * gens_n.h
}
}
58 changes: 27 additions & 31 deletions src/dense_mlpoly.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::too_many_arguments)]
use super::commitments::{Commitments, MultiCommitGens};
use super::errors::ProofVerifyError;
use super::group::{CompressedGroup, GroupElement, VartimeMultiscalarMul};
Expand All @@ -17,7 +18,7 @@ use rayon::prelude::*;
pub struct DensePolynomial {
num_vars: usize, //the number of variables in the multilinear polynomial
len: usize,
Z: Vec<Scalar>, // a vector that holds the evaluations of the polynomial in all the 2^num_vars Boolean inputs
Z: Vec<Scalar>, // evaluations of the polynomial in all the 2^num_vars Boolean inputs
}

pub struct PolyCommitmentGens {
Expand Down Expand Up @@ -73,7 +74,7 @@ impl EqPolynomial {
EqPolynomial { r }
}

pub fn evaluate(&self, rx: &Vec<Scalar>) -> Scalar {
pub fn evaluate(&self, rx: &[Scalar]) -> Scalar {
assert_eq!(self.r.len(), rx.len());
(0..rx.len())
.map(|i| self.r[i] * rx[i] + (Scalar::one() - self.r[i]) * (Scalar::one() - rx[i]))
Expand All @@ -87,12 +88,10 @@ impl EqPolynomial {
let mut size = 1;
for j in 0..ell {
// in each iteration, we double the size of chis
size = size * 2;
size *= 2;
for i in (0..size).rev().step_by(2) {
// copy each element from the prior iteration twice
let scalar = evals[i / 2];
// evals[i - 1] = scalar * (Scalar::one() - tau[j]);
// evals[i] = scalar * tau[j];
evals[i] = scalar * self.r[j];
evals[i - 1] = scalar - evals[i];
}
Expand Down Expand Up @@ -124,7 +123,7 @@ impl IdentityPolynomial {
IdentityPolynomial { size_point }
}

pub fn evaluate(&self, r: &Vec<Scalar>) -> Scalar {
pub fn evaluate(&self, r: &[Scalar]) -> Scalar {
let len = r.len();
assert_eq!(len, self.size_point);
(0..len)
Expand Down Expand Up @@ -178,7 +177,7 @@ impl DensePolynomial {
}

#[cfg(not(feature = "rayon_par"))]
fn commit_inner(&self, blinds: &Vec<Scalar>, gens: &MultiCommitGens) -> PolyCommitment {
fn commit_inner(&self, blinds: &[Scalar], gens: &MultiCommitGens) -> PolyCommitment {
let L_size = blinds.len();
let R_size = self.Z.len() / L_size;
assert_eq!(L_size * R_size, self.Z.len());
Expand Down Expand Up @@ -207,56 +206,57 @@ impl DensePolynomial {
let R_size = right_num_vars.pow2();
assert_eq!(L_size * R_size, n);

let blinds = match hiding {
true => PolyCommitmentBlinds {
let blinds = if hiding {
PolyCommitmentBlinds {
blinds: random_tape.unwrap().random_vector(b"poly_blinds", L_size),
},
false => PolyCommitmentBlinds {
}
} else {
PolyCommitmentBlinds {
blinds: vec![Scalar::zero(); L_size],
},
}
};

(self.commit_inner(&blinds.blinds, &gens.gens.gens_n), blinds)
}

pub fn bound(&self, L: &Vec<Scalar>) -> Vec<Scalar> {
pub fn bound(&self, L: &[Scalar]) -> Vec<Scalar> {
let (left_num_vars, right_num_vars) = EqPolynomial::compute_factored_lens(self.get_num_vars());
let L_size = left_num_vars.pow2();
let R_size = right_num_vars.pow2();
(0..R_size)
.map(|i| (0..L_size).map(|j| &L[j] * &self.Z[j * R_size + i]).sum())
.map(|i| (0..L_size).map(|j| L[j] * self.Z[j * R_size + i]).sum())
.collect::<Vec<Scalar>>()
}

pub fn bound_poly_var_top(&mut self, r: &Scalar) {
let n = self.len() / 2;
for i in 0..n {
self.Z[i] = &self.Z[i] + r * (&self.Z[i + n] - &self.Z[i]);
self.Z[i] = self.Z[i] + r * (self.Z[i + n] - self.Z[i]);
}
self.num_vars = self.num_vars - 1;
self.num_vars -= 1;
self.len = n;
}

pub fn bound_poly_var_bot(&mut self, r: &Scalar) {
let n = self.len() / 2;
for i in 0..n {
self.Z[i] = &self.Z[2 * i] + r * (&self.Z[2 * i + 1] - &self.Z[2 * i]);
self.Z[i] = self.Z[2 * i] + r * (self.Z[2 * i + 1] - self.Z[2 * i]);
}
self.num_vars = self.num_vars - 1;
self.num_vars -= 1;
self.len = n;
}

pub fn dotproduct(&self, other: &DensePolynomial) -> Scalar {
assert_eq!(self.len(), other.len());
let mut res = Scalar::zero();
for i in 0..self.len() {
res = &res + &self.Z[i] * &other[i];
res += self.Z[i] * other[i];
}
res
}

// returns Z(r) in O(n) time
pub fn evaluate(&self, r: &Vec<Scalar>) -> Scalar {
pub fn evaluate(&self, r: &[Scalar]) -> Scalar {
// r must have a value for each variable
assert_eq!(r.len(), self.get_num_vars());
let chis = EqPolynomial::new(r.to_vec()).evals();
Expand All @@ -274,21 +274,17 @@ impl DensePolynomial {
let other_vec = other.vec();
assert_eq!(other_vec.len(), self.len);
self.Z.extend(other_vec);
self.num_vars = self.num_vars + 1;
self.len = 2 * self.len;
self.num_vars += 1;
self.len *= 2;
assert_eq!(self.Z.len(), self.len);
}

pub fn merge<'a, I>(polys: I) -> DensePolynomial
where
I: IntoIterator<Item = &'a DensePolynomial>,
{
//assert!(polys.len() > 0);
//let num_vars = polys[0].num_vars();
let mut Z: Vec<Scalar> = Vec::new();
for poly in polys.into_iter() {
//assert_eq!(poly.get_num_vars(), num_vars); // ensure each polynomial has the same number of variables
//assert_eq!(poly.len, poly.vec().len()); // ensure no variable is already bound
Z.extend(poly.vec());
}

Expand All @@ -298,7 +294,7 @@ impl DensePolynomial {
DensePolynomial::new(Z)
}

pub fn from_usize(Z: &Vec<usize>) -> Self {
pub fn from_usize(Z: &[usize]) -> Self {
DensePolynomial::new(
(0..Z.len())
.map(|i| Scalar::from(Z[i] as u64))
Expand Down Expand Up @@ -339,7 +335,7 @@ impl PolyEvalProof {
pub fn prove(
poly: &DensePolynomial,
blinds_opt: Option<&PolyCommitmentBlinds>,
r: &Vec<Scalar>, // point at which the polynomial is evaluated
r: &[Scalar], // point at which the polynomial is evaluated
Zr: &Scalar, // evaluation of \widetilde{Z}(r)
blind_Zr_opt: Option<&Scalar>, // specifies a blind for Zr
gens: &PolyCommitmentGens,
Expand Down Expand Up @@ -401,7 +397,7 @@ impl PolyEvalProof {
&self,
gens: &PolyCommitmentGens,
transcript: &mut Transcript,
r: &Vec<Scalar>, // point at which the polynomial is evaluated
r: &[Scalar], // point at which the polynomial is evaluated
C_Zr: &CompressedGroup, // commitment to \widetilde{Z}(r)
comm: &PolyCommitment,
) -> Result<(), ProofVerifyError> {
Expand All @@ -425,8 +421,8 @@ impl PolyEvalProof {
&self,
gens: &PolyCommitmentGens,
transcript: &mut Transcript,
r: &Vec<Scalar>, // point at which the polynomial is evaluated
Zr: &Scalar, // evaluation \widetilde{Z}(r)
r: &[Scalar], // point at which the polynomial is evaluated
Zr: &Scalar, // evaluation \widetilde{Z}(r)
comm: &PolyCommitment,
) -> Result<(), ProofVerifyError> {
// compute a commitment to Zr with a blind of zero
Expand Down
2 changes: 1 addition & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt;
use core::fmt;

pub struct ProofVerifyError;

Expand Down
30 changes: 14 additions & 16 deletions src/nizk/bullet.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#![allow(non_snake_case)]

#![allow(clippy::type_complexity)]
#![allow(clippy::too_many_arguments)]
use super::super::errors::ProofVerifyError;
use super::super::group::{CompressedGroup, GroupElement, VartimeMultiscalarMul};
use super::super::math::Math;
use super::super::scalar::Scalar;
use super::super::transcript::ProofTranscript;
use core::iter;
use merlin::Transcript;
use serde::{Deserialize, Serialize};
use std::iter;

#[derive(Debug, Serialize, Deserialize)]
pub struct BulletReductionProof {
Expand All @@ -29,12 +30,12 @@ impl BulletReductionProof {
pub fn prove(
transcript: &mut Transcript,
Q: &GroupElement,
G_vec: &Vec<GroupElement>,
G_vec: &[GroupElement],
H: &GroupElement,
a_vec: &Vec<Scalar>,
b_vec: &Vec<Scalar>,
a_vec: &[Scalar],
b_vec: &[Scalar],
blind: &Scalar,
blinds_vec: &Vec<(Scalar, Scalar)>,
blinds_vec: &[(Scalar, Scalar)],
) -> (
BulletReductionProof,
GroupElement,
Expand All @@ -46,9 +47,9 @@ impl BulletReductionProof {
// Create slices G, H, a, b backed by their respective
// vectors. This lets us reslice as we compress the lengths
// of the vectors in the main loop below.
let mut G = &mut G_vec.clone()[..];
let mut a = &mut a_vec.clone()[..];
let mut b = &mut b_vec.clone()[..];
let mut G = &mut G_vec.to_owned()[..];
let mut a = &mut a_vec.to_owned()[..];
let mut b = &mut b_vec.to_owned()[..];

// All of the input vectors must have a length that is a power of two.
let mut n = G.len();
Expand All @@ -72,7 +73,7 @@ impl BulletReductionProof {
let mut blind_fin = *blind;

while n != 1 {
n = n / 2;
n /= 2;
let (a_L, a_R) = a.split_at_mut(n);
let (b_L, b_R) = b.split_at_mut(n);
let (G_L, G_R) = G.split_at_mut(n);
Expand Down Expand Up @@ -110,7 +111,7 @@ impl BulletReductionProof {
G_L[i] = GroupElement::vartime_multiscalar_mul(&[u_inv, u], &[G_L[i], G_R[i]]);
}

blind_fin = blind_fin + blind_L * &u * &u + blind_R * &u_inv * &u_inv;
blind_fin = blind_fin + blind_L * u * u + blind_R * u_inv * u_inv;

L_vec.push(L.compress());
R_vec.push(R.compress());
Expand All @@ -124,10 +125,7 @@ impl BulletReductionProof {
GroupElement::vartime_multiscalar_mul(&[a[0], a[0] * b[0], blind_fin], &[G[0], *Q, *H]);

(
BulletReductionProof {
L_vec: L_vec,
R_vec: R_vec,
},
BulletReductionProof { L_vec, R_vec },
Gamma_hat,
a[0],
b[0],
Expand Down Expand Up @@ -196,7 +194,7 @@ impl BulletReductionProof {
pub fn verify(
&self,
n: usize,
a: &Vec<Scalar>,
a: &[Scalar],
transcript: &mut Transcript,
Gamma: &GroupElement,
G: &[GroupElement],
Expand Down
Loading

0 comments on commit 2bfc333

Please sign in to comment.