Skip to content

Commit 94e507e

Browse files
authored
support for stable Rust (microsoft#51)
* support for stable Rust * add no default to stable * add no default to stable * update CI
1 parent 2bb2168 commit 94e507e

File tree

11 files changed

+54
-40
lines changed

11 files changed

+54
-40
lines changed

.github/workflows/rust.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
branches: [ master ]
88

99
jobs:
10-
build:
10+
build_nightly:
1111
runs-on: ubuntu-latest
1212
steps:
1313
- uses: actions/checkout@v2

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "spartan"
33
version = "0.6.0"
44
authors = ["Srinath Setty <[email protected]>"]
5-
edition = "2018"
5+
edition = "2021"
66
description = "High-speed zkSNARKs without trusted setup"
77
documentation = "https://docs.rs/spartan/"
88
readme = "README.md"

src/dense_mlpoly.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl IdentityPolynomial {
118118
impl DensePolynomial {
119119
pub fn new(Z: Vec<Scalar>) -> Self {
120120
DensePolynomial {
121-
num_vars: Z.len().log2() as usize,
121+
num_vars: Z.len().log_2() as usize,
122122
len: Z.len(),
123123
Z,
124124
}

src/lib.rs

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
#![allow(non_snake_case)]
2-
#![feature(test)]
3-
#![feature(int_log)]
42
#![doc = include_str!("../README.md")]
53
#![deny(missing_docs)]
64
#![allow(clippy::assertions_on_result_states)]
@@ -12,7 +10,6 @@ extern crate digest;
1210
extern crate merlin;
1311
extern crate rand;
1412
extern crate sha3;
15-
extern crate test;
1613

1714
#[cfg(feature = "multicore")]
1815
extern crate rayon;

src/math.rs

+11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ pub trait Math {
22
fn square_root(self) -> usize;
33
fn pow2(self) -> usize;
44
fn get_bits(self, num_bits: usize) -> Vec<bool>;
5+
fn log_2(self) -> usize;
56
}
67

78
impl Math for usize {
@@ -22,4 +23,14 @@ impl Math for usize {
2223
.map(|shift_amount| ((self & (1 << (num_bits - shift_amount - 1))) > 0))
2324
.collect::<Vec<bool>>()
2425
}
26+
27+
fn log_2(self) -> usize {
28+
assert_ne!(self, 0);
29+
30+
if self.is_power_of_two() {
31+
(1usize.leading_zeros() - self.leading_zeros()) as usize
32+
} else {
33+
(0usize.leading_zeros() - self.leading_zeros()) as usize
34+
}
35+
}
2536
}

src/nizk/bullet.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#![allow(clippy::too_many_arguments)]
66
use super::super::errors::ProofVerifyError;
77
use super::super::group::{CompressedGroup, GroupElement, VartimeMultiscalarMul};
8+
use super::super::math::Math;
89
use super::super::scalar::Scalar;
910
use super::super::transcript::ProofTranscript;
1011
use core::iter;
@@ -55,7 +56,7 @@ impl BulletReductionProof {
5556
// All of the input vectors must have a length that is a power of two.
5657
let mut n = G.len();
5758
assert!(n.is_power_of_two());
58-
let lg_n = n.log2() as usize;
59+
let lg_n = n.log_2() as usize;
5960

6061
// All of the input vectors must have the same length.
6162
assert_eq!(G.len(), n);

src/nizk/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
use super::commitments::{Commitments, MultiCommitGens};
33
use super::errors::ProofVerifyError;
44
use super::group::{CompressedGroup, CompressedGroupExt};
5+
use super::math::Math;
56
use super::random::RandomTape;
67
use super::scalar::Scalar;
78
use super::transcript::{AppendToTranscript, ProofTranscript};
@@ -456,8 +457,8 @@ impl DotProductProofLog {
456457
let r_delta = random_tape.random_scalar(b"r_delta");
457458
let r_beta = random_tape.random_scalar(b"r_delta");
458459
let blinds_vec = {
459-
let v1 = random_tape.random_vector(b"blinds_vec_1", 2 * n.log2() as usize);
460-
let v2 = random_tape.random_vector(b"blinds_vec_2", 2 * n.log2() as usize);
460+
let v1 = random_tape.random_vector(b"blinds_vec_1", 2 * n.log_2());
461+
let v2 = random_tape.random_vector(b"blinds_vec_2", 2 * n.log_2());
461462
(0..v1.len())
462463
.map(|i| (v1[i], v2[i]))
463464
.collect::<Vec<(Scalar, Scalar)>>()

src/product_tree.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![allow(dead_code)]
22
use super::dense_mlpoly::DensePolynomial;
33
use super::dense_mlpoly::EqPolynomial;
4+
use super::math::Math;
45
use super::scalar::Scalar;
56
use super::sumcheck::SumcheckInstanceProof;
67
use super::transcript::ProofTranscript;
@@ -36,7 +37,7 @@ impl ProductCircuit {
3637
let mut left_vec: Vec<DensePolynomial> = Vec::new();
3738
let mut right_vec: Vec<DensePolynomial> = Vec::new();
3839

39-
let num_layers = poly.len().log2() as usize;
40+
let num_layers = poly.len().log_2() as usize;
4041
let (outp_left, outp_right) = poly.split(poly.len() / 2);
4142

4243
left_vec.push(outp_left);
@@ -181,7 +182,7 @@ impl ProductCircuitEvalProof {
181182
let mut poly_C = DensePolynomial::new(EqPolynomial::new(rand.clone()).evals());
182183
assert_eq!(poly_C.len(), len / 2);
183184

184-
let num_rounds_prod = poly_C.len().log2() as usize;
185+
let num_rounds_prod = poly_C.len().log_2() as usize;
185186
let comb_func_prod = |poly_A_comp: &Scalar,
186187
poly_B_comp: &Scalar,
187188
poly_C_comp: &Scalar|
@@ -222,7 +223,7 @@ impl ProductCircuitEvalProof {
222223
len: usize,
223224
transcript: &mut Transcript,
224225
) -> (Scalar, Vec<Scalar>) {
225-
let num_layers = len.log2() as usize;
226+
let num_layers = len.log_2() as usize;
226227
let mut claim = eval;
227228
let mut rand: Vec<Scalar> = Vec::new();
228229
//let mut num_rounds = 0;
@@ -278,7 +279,7 @@ impl ProductCircuitEvalProofBatched {
278279
let mut poly_C_par = DensePolynomial::new(EqPolynomial::new(rand.clone()).evals());
279280
assert_eq!(poly_C_par.len(), len / 2);
280281

281-
let num_rounds_prod = poly_C_par.len().log2() as usize;
282+
let num_rounds_prod = poly_C_par.len().log_2() as usize;
282283
let comb_func_prod = |poly_A_comp: &Scalar,
283284
poly_B_comp: &Scalar,
284285
poly_C_comp: &Scalar|
@@ -388,7 +389,7 @@ impl ProductCircuitEvalProofBatched {
388389
len: usize,
389390
transcript: &mut Transcript,
390391
) -> (Vec<Scalar>, Vec<Scalar>, Vec<Scalar>) {
391-
let num_layers = len.log2() as usize;
392+
let num_layers = len.log_2() as usize;
392393
let mut rand: Vec<Scalar> = Vec::new();
393394
//let mut num_rounds = 0;
394395
assert_eq!(self.proof.len(), num_layers);

src/r1csinstance.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ impl R1CSCommitmentGens {
4747
num_nz_entries: usize,
4848
) -> R1CSCommitmentGens {
4949
assert!(num_inputs < num_vars);
50-
let num_poly_vars_x = num_cons.log2() as usize;
51-
let num_poly_vars_y = (2 * num_vars).log2() as usize;
50+
let num_poly_vars_x = num_cons.log_2() as usize;
51+
let num_poly_vars_y = (2 * num_vars).log_2() as usize;
5252
let gens =
5353
SparseMatPolyCommitmentGens::new(label, num_poly_vars_x, num_poly_vars_y, num_nz_entries, 3);
5454
R1CSCommitmentGens { gens }
@@ -116,8 +116,8 @@ impl R1CSInstance {
116116
assert!(num_inputs < num_vars);
117117

118118
// no errors, so create polynomials
119-
let num_poly_vars_x = num_cons.log2() as usize;
120-
let num_poly_vars_y = (2 * num_vars).log2() as usize;
119+
let num_poly_vars_x = num_cons.log_2() as usize;
120+
let num_poly_vars_y = (2 * num_vars).log_2() as usize;
121121

122122
let mat_A = (0..A.len())
123123
.map(|i| SparseMatEntry::new(A[i].0, A[i].1, A[i].2))
@@ -167,8 +167,8 @@ impl R1CSInstance {
167167
let mut csprng: OsRng = OsRng;
168168

169169
// assert num_cons and num_vars are power of 2
170-
assert_eq!((num_cons.log2() as usize).pow2(), num_cons);
171-
assert_eq!((num_vars.log2() as usize).pow2(), num_vars);
170+
assert_eq!((num_cons.log_2() as usize).pow2(), num_cons);
171+
assert_eq!((num_vars.log_2() as usize).pow2(), num_vars);
172172

173173
// num_inputs + 1 <= num_vars
174174
assert!(num_inputs < num_vars);
@@ -215,8 +215,8 @@ impl R1CSInstance {
215215
Timer::print(&format!("number_non-zero_entries_B {}", B.len()));
216216
Timer::print(&format!("number_non-zero_entries_C {}", C.len()));
217217

218-
let num_poly_vars_x = num_cons.log2() as usize;
219-
let num_poly_vars_y = (2 * num_vars).log2() as usize;
218+
let num_poly_vars_x = num_cons.log_2() as usize;
219+
let num_poly_vars_y = (2 * num_vars).log_2() as usize;
220220
let poly_A = SparseMatPolynomial::new(num_poly_vars_x, num_poly_vars_y, A);
221221
let poly_B = SparseMatPolynomial::new(num_poly_vars_x, num_poly_vars_y, B);
222222
let poly_C = SparseMatPolynomial::new(num_poly_vars_x, num_poly_vars_y, C);

src/r1csproof.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use super::dense_mlpoly::{
55
};
66
use super::errors::ProofVerifyError;
77
use super::group::{CompressedGroup, GroupElement, VartimeMultiscalarMul};
8+
use super::math::Math;
89
use super::nizk::{EqualityProof, KnowledgeProof, ProductProof};
910
use super::r1csinstance::R1CSInstance;
1011
use super::random::RandomTape;
@@ -63,7 +64,7 @@ pub struct R1CSGens {
6364

6465
impl R1CSGens {
6566
pub fn new(label: &'static [u8], _num_cons: usize, num_vars: usize) -> Self {
66-
let num_poly_vars = num_vars.log2() as usize;
67+
let num_poly_vars = num_vars.log_2() as usize;
6768
let gens_pc = PolyCommitmentGens::new(num_poly_vars, label);
6869
let gens_sc = R1CSSumcheckGens::new(label, &gens_pc.gens.gens_1);
6970
R1CSGens { gens_sc, gens_pc }
@@ -182,8 +183,10 @@ impl R1CSProof {
182183
};
183184

184185
// derive the verifier's challenge tau
185-
let (num_rounds_x, num_rounds_y) =
186-
(inst.get_num_cons().log2() as usize, z.len().log2() as usize);
186+
let (num_rounds_x, num_rounds_y) = (
187+
inst.get_num_cons().log_2() as usize,
188+
z.len().log_2() as usize,
189+
);
187190
let tau = transcript.challenge_vector(b"challenge_tau", num_rounds_x);
188191
// compute the initial evaluation table for R(\tau, x)
189192
let mut poly_tau = DensePolynomial::new(EqPolynomial::new(tau).evals());
@@ -365,7 +368,7 @@ impl R1CSProof {
365368
.comm_vars
366369
.append_to_transcript(b"poly_commitment", transcript);
367370

368-
let (num_rounds_x, num_rounds_y) = (num_cons.log2() as usize, (2 * num_vars).log2() as usize);
371+
let (num_rounds_x, num_rounds_y) = (num_cons.log_2() as usize, (2 * num_vars).log_2() as usize);
369372

370373
// derive the verifier's challenge tau
371374
let tau = transcript.challenge_vector(b"challenge_tau", num_rounds_x);
@@ -461,7 +464,7 @@ impl R1CSProof {
461464
.map(|i| SparsePolyEntry::new(i + 1, input[i]))
462465
.collect::<Vec<SparsePolyEntry>>(),
463466
);
464-
SparsePolynomial::new(n.log2() as usize, input_as_sparse_poly_entries).evaluate(&ry[1..])
467+
SparsePolynomial::new(n.log_2() as usize, input_as_sparse_poly_entries).evaluate(&ry[1..])
465468
};
466469

467470
// compute commitment to eval_Z_at_ry = (Scalar::one() - ry[0]) * self.eval_vars_at_ry + ry[0] * poly_input_eval

src/sparse_mlpoly.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl DerefsEvalProof {
9191
) -> PolyEvalProof {
9292
assert_eq!(
9393
joint_poly.get_num_vars(),
94-
r.len() + evals.len().log2() as usize
94+
r.len() + evals.len().log_2() as usize
9595
);
9696

9797
// append the claimed evaluations to transcript
@@ -100,7 +100,7 @@ impl DerefsEvalProof {
100100
// n-to-1 reduction
101101
let (r_joint, eval_joint) = {
102102
let challenges =
103-
transcript.challenge_vector(b"challenge_combine_n_to_one", evals.len().log2() as usize);
103+
transcript.challenge_vector(b"challenge_combine_n_to_one", evals.len().log_2() as usize);
104104
let mut poly_evals = DensePolynomial::new(evals);
105105
for i in (0..challenges.len()).rev() {
106106
poly_evals.bound_poly_var_bot(&challenges[i]);
@@ -166,7 +166,7 @@ impl DerefsEvalProof {
166166

167167
// n-to-1 reduction
168168
let challenges =
169-
transcript.challenge_vector(b"challenge_combine_n_to_one", evals.len().log2() as usize);
169+
transcript.challenge_vector(b"challenge_combine_n_to_one", evals.len().log_2() as usize);
170170
let mut poly_evals = DensePolynomial::new(evals);
171171
for i in (0..challenges.len()).rev() {
172172
poly_evals.bound_poly_var_bot(&challenges[i]);
@@ -300,15 +300,15 @@ impl SparseMatPolyCommitmentGens {
300300
num_nz_entries: usize,
301301
batch_size: usize,
302302
) -> SparseMatPolyCommitmentGens {
303-
let num_vars_ops = num_nz_entries.next_power_of_two().log2() as usize
304-
+ (batch_size * 5).next_power_of_two().log2() as usize;
303+
let num_vars_ops = num_nz_entries.next_power_of_two().log_2() as usize
304+
+ (batch_size * 5).next_power_of_two().log_2() as usize;
305305
let num_vars_mem = if num_vars_x > num_vars_y {
306306
num_vars_x
307307
} else {
308308
num_vars_y
309309
} + 1;
310-
let num_vars_derefs = num_nz_entries.next_power_of_two().log2() as usize
311-
+ (batch_size * 2).next_power_of_two().log2() as usize;
310+
let num_vars_derefs = num_nz_entries.next_power_of_two().log_2() as usize
311+
+ (batch_size * 2).next_power_of_two().log_2() as usize;
312312

313313
let gens_ops = PolyCommitmentGens::new(num_vars_ops, label);
314314
let gens_mem = PolyCommitmentGens::new(num_vars_mem, label);
@@ -780,7 +780,7 @@ impl HashLayerProof {
780780
evals_ops.append_to_transcript(b"claim_evals_ops", transcript);
781781
let challenges_ops = transcript.challenge_vector(
782782
b"challenge_combine_n_to_one",
783-
evals_ops.len().log2() as usize,
783+
evals_ops.len().log_2() as usize,
784784
);
785785

786786
let mut poly_evals_ops = DensePolynomial::new(evals_ops);
@@ -809,7 +809,7 @@ impl HashLayerProof {
809809
evals_mem.append_to_transcript(b"claim_evals_mem", transcript);
810810
let challenges_mem = transcript.challenge_vector(
811811
b"challenge_combine_two_to_one",
812-
evals_mem.len().log2() as usize,
812+
evals_mem.len().log_2() as usize,
813813
);
814814

815815
let mut poly_evals_mem = DensePolynomial::new(evals_mem);
@@ -954,7 +954,7 @@ impl HashLayerProof {
954954
evals_ops.append_to_transcript(b"claim_evals_ops", transcript);
955955
let challenges_ops = transcript.challenge_vector(
956956
b"challenge_combine_n_to_one",
957-
evals_ops.len().log2() as usize,
957+
evals_ops.len().log_2() as usize,
958958
);
959959

960960
let mut poly_evals_ops = DensePolynomial::new(evals_ops);
@@ -980,7 +980,7 @@ impl HashLayerProof {
980980
evals_mem.append_to_transcript(b"claim_evals_mem", transcript);
981981
let challenges_mem = transcript.challenge_vector(
982982
b"challenge_combine_two_to_one",
983-
evals_mem.len().log2() as usize,
983+
evals_mem.len().log_2() as usize,
984984
);
985985

986986
let mut poly_evals_mem = DensePolynomial::new(evals_mem);
@@ -1629,8 +1629,8 @@ mod tests {
16291629
let num_nz_entries: usize = 256;
16301630
let num_rows: usize = 256;
16311631
let num_cols: usize = 256;
1632-
let num_vars_x: usize = num_rows.log2() as usize;
1633-
let num_vars_y: usize = num_cols.log2() as usize;
1632+
let num_vars_x: usize = num_rows.log_2() as usize;
1633+
let num_vars_y: usize = num_cols.log_2() as usize;
16341634

16351635
let mut M: Vec<SparseMatEntry> = Vec::new();
16361636

0 commit comments

Comments
 (0)