Skip to content

Commit

Permalink
Update to latest Rust and fix Clippy warnings (#37)
Browse files Browse the repository at this point in the history
* Update to latest Rust and fix Clippy warnings

* cleanup
  • Loading branch information
srinathsetty authored Oct 14, 2021
1 parent f465aed commit 19d1d63
Show file tree
Hide file tree
Showing 19 changed files with 227 additions and 249 deletions.
4 changes: 4 additions & 0 deletions .cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[build]
rustflags = [
"-C", "target-cpu=native",
]
15 changes: 11 additions & 4 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Rust
name: Build and Test Spartan

on:
push:
Expand All @@ -8,16 +8,23 @@ on:

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Install
run: rustup default nightly-2021-01-31
run: rustup default nightly
- name: Install rustfmt Components
run: rustup component add rustfmt
- name: Install clippy
run: rustup component add clippy
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
- name: Build examples
run: cargo build --examples --verbose
- name: Check Rustfmt Code Style
run: cargo fmt --all -- --check
- name: Check clippy warnings
run: cargo clippy --all-targets --all-features -- -D warnings

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "spartan"
version = "0.3.0"
version = "0.4.0"
authors = ["Srinath Setty <[email protected]>"]
edition = "2018"
description = "High-speed zkSNARKs without trusted setup"
Expand All @@ -11,7 +11,7 @@ license-file = "LICENSE"
keywords = ["zkSNARKs", "cryptography", "proofs"]

[dependencies]
curve25519-dalek = {version = "3.0.0", features = ["serde", "simd_backend"]}
curve25519-dalek = {version = "3.2.0", features = ["serde", "simd_backend"]}
merlin = "3.0.0"
rand = "0.7.3"
digest = "0.8.1"
Expand All @@ -20,7 +20,7 @@ byteorder = "1.3.4"
rayon = { version = "1.3.0", optional = true }
serde = { version = "1.0.106", features = ["derive"] }
bincode = "1.2.1"
subtle = { version = "^2.2.3", default-features = false }
subtle = { version = "2.4", default-features = false }
rand_core = { version = "0.5", default-features = false }
zeroize = { version = "1", default-features = false }
itertools = "0.10.0"
Expand Down
4 changes: 2 additions & 2 deletions benches/nizk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn nizk_prove_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("NIZK_prove_benchmark");
group.plot_config(plot_config);

let num_vars = (2 as usize).pow(s as u32);
let num_vars = (2_usize).pow(s as u32);
let num_cons = num_vars;
let num_inputs = 10;

Expand Down Expand Up @@ -49,7 +49,7 @@ fn nizk_verify_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("NIZK_verify_benchmark");
group.plot_config(plot_config);

let num_vars = (2 as usize).pow(s as u32);
let num_vars = (2_usize).pow(s as u32);
let num_cons = num_vars;
let num_inputs = 10;
let (inst, vars, inputs) = Instance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
Expand Down
6 changes: 3 additions & 3 deletions benches/snark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn snark_encode_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("SNARK_encode_benchmark");
group.plot_config(plot_config);

let num_vars = (2 as usize).pow(s as u32);
let num_vars = (2_usize).pow(s as u32);
let num_cons = num_vars;
let num_inputs = 10;
let (inst, _vars, _inputs) = Instance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
Expand All @@ -37,7 +37,7 @@ fn snark_prove_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("SNARK_prove_benchmark");
group.plot_config(plot_config);

let num_vars = (2 as usize).pow(s as u32);
let num_vars = (2_usize).pow(s as u32);
let num_cons = num_vars;
let num_inputs = 10;

Expand Down Expand Up @@ -74,7 +74,7 @@ fn snark_verify_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("SNARK_verify_benchmark");
group.plot_config(plot_config);

let num_vars = (2 as usize).pow(s as u32);
let num_vars = (2_usize).pow(s as u32);
let num_cons = num_vars;
let num_inputs = 10;
let (inst, vars, inputs) = Instance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
Expand Down
2 changes: 1 addition & 1 deletion examples/cubic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ fn produce_r1cs() -> (

// check if the instance we created is satisfiable
let res = inst.is_sat(&assignment_vars, &assignment_inputs);
assert_eq!(res.unwrap(), true, "should be satisfied");
assert!(res.unwrap(), "should be satisfied");

(
num_cons,
Expand Down
2 changes: 1 addition & 1 deletion profiler/nizk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn main() {

println!("Profiler:: NIZK");
for &s in inst_sizes.iter() {
let num_vars = (2 as usize).pow(s as u32);
let num_vars = (2_usize).pow(s as u32);
let num_cons = num_vars;
let num_inputs = 10;

Expand Down
2 changes: 1 addition & 1 deletion profiler/snark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn main() {

println!("Profiler:: SNARK");
for &s in inst_sizes.iter() {
let num_vars = (2 as usize).pow(s as u32);
let num_vars = (2_usize).pow(s as u32);
let num_cons = num_vars;
let num_inputs = 10;

Expand Down
74 changes: 36 additions & 38 deletions src/dense_mlpoly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,11 @@ impl IdentityPolynomial {

impl DensePolynomial {
pub fn new(Z: Vec<Scalar>) -> Self {
let len = Z.len();
let num_vars = len.log2();
DensePolynomial { num_vars, Z, len }
DensePolynomial {
num_vars: Z.len().log2() as usize,
len: Z.len(),
Z,
}
}

pub fn get_num_vars(&self) -> usize {
Expand All @@ -143,7 +145,7 @@ impl DensePolynomial {
}

#[cfg(feature = "multicore")]
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 @@ -187,9 +189,9 @@ impl DensePolynomial {
let R_size = right_num_vars.pow2();
assert_eq!(L_size * R_size, n);

let blinds = if random_tape.is_some() {
let blinds = if let Some(t) = random_tape {
PolyCommitmentBlinds {
blinds: random_tape.unwrap().random_vector(b"poly_blinds", L_size),
blinds: t.random_vector(b"poly_blinds", L_size),
}
} else {
PolyCommitmentBlinds {
Expand Down Expand Up @@ -352,7 +354,7 @@ impl PolyEvalProof {
&LZ,
&LZ_blind,
&R,
&Zr,
Zr,
blind_Zr,
);

Expand Down Expand Up @@ -404,7 +406,7 @@ mod tests {
use super::*;
use rand::rngs::OsRng;

fn evaluate_with_LR(Z: &Vec<Scalar>, r: &Vec<Scalar>) -> Scalar {
fn evaluate_with_LR(Z: &[Scalar], r: &[Scalar]) -> Scalar {
let eq = EqPolynomial::new(r.to_vec());
let (L, R) = eq.compute_factored_evals();

Expand All @@ -427,25 +429,26 @@ mod tests {

#[test]
fn check_polynomial_evaluation() {
let mut Z: Vec<Scalar> = Vec::new(); // Z = [1, 2, 1, 4]
Z.push(Scalar::one());
Z.push((2 as usize).to_scalar());
Z.push((1 as usize).to_scalar());
Z.push((4 as usize).to_scalar());
// Z = [1, 2, 1, 4]
let Z = vec![
Scalar::one(),
(2_usize).to_scalar(),
(1_usize).to_scalar(),
(4_usize).to_scalar(),
];

// r = [4,3]
let mut r: Vec<Scalar> = Vec::new();
r.push((4 as usize).to_scalar());
r.push((3 as usize).to_scalar());
let r = vec![(4_usize).to_scalar(), (3_usize).to_scalar()];

let eval_with_LR = evaluate_with_LR(&Z, &r);
let poly = DensePolynomial::new(Z);

let eval = poly.evaluate(&r);
assert_eq!(eval, (28 as usize).to_scalar());
assert_eq!(eval, (28_usize).to_scalar());
assert_eq!(eval_with_LR, eval);
}

pub fn compute_factored_chis_at_r(r: &Vec<Scalar>) -> (Vec<Scalar>, Vec<Scalar>) {
pub fn compute_factored_chis_at_r(r: &[Scalar]) -> (Vec<Scalar>, Vec<Scalar>) {
let mut L: Vec<Scalar> = Vec::new();
let mut R: Vec<Scalar> = Vec::new();

Expand Down Expand Up @@ -484,7 +487,7 @@ mod tests {
(L, R)
}

pub fn compute_chis_at_r(r: &Vec<Scalar>) -> Vec<Scalar> {
pub fn compute_chis_at_r(r: &[Scalar]) -> Vec<Scalar> {
let ell = r.len();
let n = ell.pow2();
let mut chis: Vec<Scalar> = Vec::new();
Expand All @@ -505,15 +508,12 @@ mod tests {

pub fn compute_outerproduct(L: Vec<Scalar>, R: Vec<Scalar>) -> Vec<Scalar> {
assert_eq!(L.len(), R.len());

let mut O: Vec<Scalar> = Vec::new();
let m = L.len();
for i in 0..m {
for j in 0..m {
O.push(L[i] * R[j]);
}
}
O
(0..L.len())
.map(|i| (0..R.len()).map(|j| L[i] * R[j]).collect::<Vec<Scalar>>())
.collect::<Vec<Vec<Scalar>>>()
.into_iter()
.flatten()
.collect::<Vec<Scalar>>()
}

#[test]
Expand Down Expand Up @@ -563,20 +563,18 @@ mod tests {

#[test]
fn check_polynomial_commit() {
let mut Z: Vec<Scalar> = Vec::new(); // Z = [1, 2, 1, 4]
Z.push((1 as usize).to_scalar());
Z.push((2 as usize).to_scalar());
Z.push((1 as usize).to_scalar());
Z.push((4 as usize).to_scalar());

let Z = vec![
(1_usize).to_scalar(),
(2_usize).to_scalar(),
(1_usize).to_scalar(),
(4_usize).to_scalar(),
];
let poly = DensePolynomial::new(Z);

// r = [4,3]
let mut r: Vec<Scalar> = Vec::new();
r.push((4 as usize).to_scalar());
r.push((3 as usize).to_scalar());
let r = vec![(4_usize).to_scalar(), (3_usize).to_scalar()];
let eval = poly.evaluate(&r);
assert_eq!(eval, (28 as usize).to_scalar());
assert_eq!(eval, (28_usize).to_scalar());

let gens = PolyCommitmentGens::new(poly.get_num_vars(), b"test-two");
let (poly_commitment, blinds) = poly.commit(&gens, None);
Expand Down
Loading

0 comments on commit 19d1d63

Please sign in to comment.