Skip to content

Commit bc88d28

Browse files
authored
Revert "refactor: improve visibilities (#65)"
This reverts commit f25d18a.
1 parent f25d18a commit bc88d28

File tree

4 files changed

+91
-75
lines changed

4 files changed

+91
-75
lines changed

Cargo.toml

+5-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ license-file = "LICENSE"
1111
keywords = ["zkSNARKs", "cryptography", "proofs"]
1212

1313
[dependencies]
14-
curve25519-dalek = { version = "4.1.1", features = [
14+
curve25519-dalek = { version = "3.2.0", features = [
1515
"serde",
16+
"u64_backend",
1617
"alloc",
1718
], default-features = false }
1819
merlin = { version = "3.0.0", default-features = false }
@@ -57,8 +58,9 @@ harness = false
5758
required-features = ["std"]
5859

5960
[features]
60-
default = ["std"]
61+
default = ["std", "simd_backend"]
6162
std = [
63+
"curve25519-dalek/std",
6264
"digest/std",
6365
"merlin/std",
6466
"rand/std",
@@ -70,5 +72,6 @@ std = [
7072
"itertools/use_std",
7173
"flate2/rust_backend",
7274
]
75+
simd_backend = ["curve25519-dalek/simd_backend"]
7376
multicore = ["rayon"]
7477
profile = ["colored"]

src/commitments.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::group::{GroupElement, VartimeMultiscalarMul, GROUP_BASEPOINT_COMPRESSED};
22
use super::scalar::Scalar;
3-
use digest::{ExtendableOutput, Input, XofReader};
3+
use digest::XofReader;
4+
use digest::{ExtendableOutput, Input};
45
use sha3::Shake256;
56

67
#[derive(Debug)]

src/r1csinstance.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -110,24 +110,21 @@ impl R1CSInstance {
110110
let num_poly_vars_x = num_cons.log_2();
111111
let num_poly_vars_y = (2 * num_vars).log_2();
112112

113-
let mat_A = A
114-
.iter()
115-
.map(|(row, col, val)| SparseMatEntry::new(*row, *col, *val))
113+
let mat_A = (0..A.len())
114+
.map(|i| SparseMatEntry::new(A[i].0, A[i].1, A[i].2))
116115
.collect::<Vec<SparseMatEntry>>();
117-
let mat_B = B
118-
.iter()
119-
.map(|(row, col, val)| SparseMatEntry::new(*row, *col, *val))
116+
let mat_B = (0..B.len())
117+
.map(|i| SparseMatEntry::new(B[i].0, B[i].1, B[i].2))
120118
.collect::<Vec<SparseMatEntry>>();
121-
let mat_C = C
122-
.iter()
123-
.map(|(row, col, val)| SparseMatEntry::new(*row, *col, *val))
119+
let mat_C = (0..C.len())
120+
.map(|i| SparseMatEntry::new(C[i].0, C[i].1, C[i].2))
124121
.collect::<Vec<SparseMatEntry>>();
125122

126123
let poly_A = SparseMatPolynomial::new(num_poly_vars_x, num_poly_vars_y, mat_A);
127124
let poly_B = SparseMatPolynomial::new(num_poly_vars_x, num_poly_vars_y, mat_B);
128125
let poly_C = SparseMatPolynomial::new(num_poly_vars_x, num_poly_vars_y, mat_C);
129126

130-
Self {
127+
R1CSInstance {
131128
num_cons,
132129
num_vars,
133130
num_inputs,
@@ -260,7 +257,11 @@ impl R1CSInstance {
260257
assert_eq!(Az.len(), self.num_cons);
261258
assert_eq!(Bz.len(), self.num_cons);
262259
assert_eq!(Cz.len(), self.num_cons);
263-
(0..self.num_cons).all(|i| Az[i] * Bz[i] == Cz[i])
260+
let res: usize = (0..self.num_cons)
261+
.map(|i| usize::from(Az[i] * Bz[i] != Cz[i]))
262+
.sum();
263+
264+
res == 0
264265
}
265266

266267
pub fn multiply_vec(

src/sparse_mlpoly.rs

+72-61
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,18 @@ impl Derefs {
5151
pub fn new(row_ops_val: Vec<DensePolynomial>, col_ops_val: Vec<DensePolynomial>) -> Self {
5252
assert_eq!(row_ops_val.len(), col_ops_val.len());
5353

54-
// combine all polynomials into a single polynomial (used below to produce a single commitment)
55-
let comb = DensePolynomial::merge(row_ops_val.iter().chain(col_ops_val.iter()));
54+
let derefs = {
55+
// combine all polynomials into a single polynomial (used below to produce a single commitment)
56+
let comb = DensePolynomial::merge(row_ops_val.iter().chain(col_ops_val.iter()));
57+
58+
Derefs {
59+
row_ops_val,
60+
col_ops_val,
61+
comb,
62+
}
63+
};
5664

57-
Derefs {
58-
row_ops_val,
59-
col_ops_val,
60-
comb,
61-
}
65+
derefs
6266
}
6367

6468
pub fn commit(&self, gens: &PolyCommitmentGens) -> DerefsCommitment {
@@ -339,7 +343,7 @@ impl AppendToTranscript for SparseMatPolyCommitment {
339343

340344
impl SparseMatPolynomial {
341345
pub fn new(num_vars_x: usize, num_vars_y: usize, M: Vec<SparseMatEntry>) -> Self {
342-
Self {
346+
SparseMatPolynomial {
343347
num_vars_x,
344348
num_vars_y,
345349
M,
@@ -373,9 +377,8 @@ impl SparseMatPolynomial {
373377
assert_eq!(sparse_polys[i].num_vars_y, sparse_polys[0].num_vars_y);
374378
}
375379

376-
let N = sparse_polys
377-
.iter()
378-
.map(|sparse_poly| sparse_poly.get_num_nz_entries())
380+
let N = (0..sparse_polys.len())
381+
.map(|i| sparse_polys[i].get_num_nz_entries())
379382
.max()
380383
.unwrap();
381384

@@ -427,10 +430,13 @@ impl SparseMatPolynomial {
427430
assert_eq!(self.num_vars_x.pow2(), eval_table_rx.len());
428431
assert_eq!(self.num_vars_y.pow2(), eval_table_ry.len());
429432

430-
self
431-
.M
432-
.iter()
433-
.map(|SparseMatEntry { row, col, val }| eval_table_rx[*row] * eval_table_ry[*col] * val)
433+
(0..self.M.len())
434+
.map(|i| {
435+
let row = self.M[i].row;
436+
let col = self.M[i].col;
437+
let val = &self.M[i].val;
438+
eval_table_rx[row] * eval_table_ry[col] * val
439+
})
434440
.sum()
435441
}
436442

@@ -442,22 +448,25 @@ impl SparseMatPolynomial {
442448
let eval_table_rx = EqPolynomial::new(rx.to_vec()).evals();
443449
let eval_table_ry = EqPolynomial::new(ry.to_vec()).evals();
444450

445-
polys
446-
.iter()
447-
.map(|poly| poly.evaluate_with_tables(&eval_table_rx, &eval_table_ry))
451+
(0..polys.len())
452+
.map(|i| polys[i].evaluate_with_tables(&eval_table_rx, &eval_table_ry))
448453
.collect::<Vec<Scalar>>()
449454
}
450455

451456
pub fn multiply_vec(&self, num_rows: usize, num_cols: usize, z: &[Scalar]) -> Vec<Scalar> {
452457
assert_eq!(z.len(), num_cols);
453458

454-
self.M.iter().fold(
455-
vec![Scalar::zero(); num_rows],
456-
|mut Mz, SparseMatEntry { row, col, val }| {
457-
Mz[*row] += val * z[*col];
459+
(0..self.M.len())
460+
.map(|i| {
461+
let row = self.M[i].row;
462+
let col = self.M[i].col;
463+
let val = &self.M[i].val;
464+
(row, val * z[col])
465+
})
466+
.fold(vec![Scalar::zero(); num_rows], |mut Mz, (r, v)| {
467+
Mz[r] += v;
458468
Mz
459-
},
460-
)
469+
})
461470
}
462471

463472
pub fn compute_eval_table_sparse(
@@ -468,13 +477,13 @@ impl SparseMatPolynomial {
468477
) -> Vec<Scalar> {
469478
assert_eq!(rx.len(), num_rows);
470479

471-
self.M.iter().fold(
472-
vec![Scalar::zero(); num_cols],
473-
|mut M_evals, SparseMatEntry { row, col, val }| {
474-
M_evals[*col] += rx[*row] * val;
475-
M_evals
476-
},
477-
)
480+
let mut M_evals: Vec<Scalar> = vec![Scalar::zero(); num_cols];
481+
482+
for i in 0..self.M.len() {
483+
let entry = &self.M[i];
484+
M_evals[entry.col] += rx[entry.row] * entry.val;
485+
}
486+
M_evals
478487
}
479488

480489
pub fn multi_commit(
@@ -697,18 +706,18 @@ impl HashLayerProof {
697706
let (rand_mem, rand_ops) = rand;
698707

699708
// decommit ops-addr at rand_ops
700-
let eval_ops_addr_vec = addr_timestamps
701-
.ops_addr
702-
.iter()
703-
.map(|addr| addr.evaluate(rand_ops))
704-
.collect();
709+
let mut eval_ops_addr_vec: Vec<Scalar> = Vec::new();
710+
for i in 0..addr_timestamps.ops_addr.len() {
711+
let eval_ops_addr = addr_timestamps.ops_addr[i].evaluate(rand_ops);
712+
eval_ops_addr_vec.push(eval_ops_addr);
713+
}
705714

706715
// decommit read_ts at rand_ops
707-
let eval_read_ts_vec = addr_timestamps
708-
.read_ts
709-
.iter()
710-
.map(|addr| addr.evaluate(rand_ops))
711-
.collect();
716+
let mut eval_read_ts_vec: Vec<Scalar> = Vec::new();
717+
for i in 0..addr_timestamps.read_ts.len() {
718+
let eval_read_ts = addr_timestamps.read_ts[i].evaluate(rand_ops);
719+
eval_read_ts_vec.push(eval_read_ts);
720+
}
712721

713722
// decommit audit-ts at rand_mem
714723
let eval_audit_ts = addr_timestamps.audit_ts.evaluate(rand_mem);
@@ -729,15 +738,11 @@ impl HashLayerProof {
729738
let (rand_mem, rand_ops) = rand;
730739

731740
// decommit derefs at rand_ops
732-
let eval_row_ops_val = derefs
733-
.row_ops_val
734-
.iter()
735-
.map(|row| row.evaluate(rand_ops))
741+
let eval_row_ops_val = (0..derefs.row_ops_val.len())
742+
.map(|i| derefs.row_ops_val[i].evaluate(rand_ops))
736743
.collect::<Vec<Scalar>>();
737-
let eval_col_ops_val = derefs
738-
.col_ops_val
739-
.iter()
740-
.map(|col| col.evaluate(rand_ops))
744+
let eval_col_ops_val = (0..derefs.col_ops_val.len())
745+
.map(|i| derefs.col_ops_val[i].evaluate(rand_ops))
741746
.collect::<Vec<Scalar>>();
742747
let proof_derefs = DerefsEvalProof::prove(
743748
derefs,
@@ -1576,9 +1581,15 @@ impl SparsePolynomial {
15761581

15771582
fn compute_chi(a: &[bool], r: &[Scalar]) -> Scalar {
15781583
assert_eq!(a.len(), r.len());
1579-
a.iter().zip(r.iter()).fold(Scalar::one(), |sum, (a, r)| {
1580-
sum * if *a { *r } else { Scalar::one() - r }
1581-
})
1584+
let mut chi_i = Scalar::one();
1585+
for j in 0..r.len() {
1586+
if a[j] {
1587+
chi_i *= r[j];
1588+
} else {
1589+
chi_i *= Scalar::one() - r[j];
1590+
}
1591+
}
1592+
chi_i
15821593
}
15831594

15841595
// Takes O(n log n). TODO: do this in O(n) where n is the number of entries in Z
@@ -1609,15 +1620,15 @@ mod tests {
16091620
let num_vars_x: usize = num_rows.log_2();
16101621
let num_vars_y: usize = num_cols.log_2();
16111622

1612-
let M = (0..num_nz_entries)
1613-
.map(|_i| {
1614-
SparseMatEntry::new(
1615-
(csprng.next_u64() % (num_rows as u64)) as usize,
1616-
(csprng.next_u64() % (num_cols as u64)) as usize,
1617-
Scalar::random(&mut csprng),
1618-
)
1619-
})
1620-
.collect();
1623+
let mut M: Vec<SparseMatEntry> = Vec::new();
1624+
1625+
for _i in 0..num_nz_entries {
1626+
M.push(SparseMatEntry::new(
1627+
(csprng.next_u64() % (num_rows as u64)) as usize,
1628+
(csprng.next_u64() % (num_cols as u64)) as usize,
1629+
Scalar::random(&mut csprng),
1630+
));
1631+
}
16211632

16221633
let poly_M = SparseMatPolynomial::new(num_vars_x, num_vars_y, M);
16231634
let gens = SparseMatPolyCommitmentGens::new(

0 commit comments

Comments
 (0)