Repository navigation
Expand file tree
/
Copy pathsingle_threaded.patch
More file actions
159 lines (155 loc) · 6.15 KB
/
Copy pathsingle_threaded.patch
File metadata and controls
159 lines (155 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
diff --git a/crates/gkr/src/lib.rs b/crates/gkr/src/lib.rs
index 1373733..55a6ce9 100644
--- a/crates/gkr/src/lib.rs
+++ b/crates/gkr/src/lib.rs
@@ -1,7 +1,6 @@
use common::{BitMatrix, BitTable, SecurityLevel};
use field::{F128, Wide256};
use num_traits::{ConstOne, ConstZero};
-use rayon::prelude::*;
use std::ops::Range;
use transcript::{ProverState, VerifierState};
@@ -183,16 +182,13 @@ fn round_sums<const AT_ONE: bool>(
// Two lanes per step: independent multiplication chains. Chunking also
// reduces variance in benchmarking.
left0
- .par_chunks_exact(2)
- .zip(left1.par_chunks_exact(2))
- .zip(right0.par_chunks_exact(2))
- .zip(right1.par_chunks_exact(2))
- .zip(eq.par_chunks_exact(2))
- // Chunked by 2, so the element-count threshold below is halved in
- // terms of chunks.
- .with_min_len(PARALLEL_MIN_LANES / 2)
+ .chunks_exact(2)
+ .zip(left1.chunks_exact(2))
+ .zip(right0.chunks_exact(2))
+ .zip(right1.chunks_exact(2))
+ .zip(eq.chunks_exact(2))
.fold(
- || (Wide256::zero(), Wide256::zero()),
+ (Wide256::zero(), Wide256::zero()),
|(mut sum_b, mut sum_inf), ((((l0, l1), r0), r1), e)| {
// `e · L(b) · R(b)` into `sum_b` and `e · ΔL · ΔR` into
// `sum_inf`, with `e` joining the left factor through a reduced
@@ -212,21 +208,16 @@ fn round_sums<const AT_ONE: bool>(
(sum_b, sum_inf)
},
)
- .reduce(
- || (Wide256::zero(), Wide256::zero()),
- |(x_b, x_inf), (y_b, y_inf)| (x_b + y_b, x_inf + y_inf),
- )
}
/// Binds the variable that splits each pair of halves to `r`, writing the
/// result over the low halves.
fn bind(r: Field, left0: &mut [Field], left1: &[Field], right0: &mut [Field], right1: &[Field]) {
left0
- .par_iter_mut()
- .zip(right0.par_iter_mut())
- .zip(left1.par_iter())
- .zip(right1.par_iter())
- .with_min_len(PARALLEL_MIN_LANES)
+ .iter_mut()
+ .zip(right0.iter_mut())
+ .zip(left1.iter())
+ .zip(right1.iter())
.for_each(|(((l0, r0), &l1), &r1)| {
// Recalculating the step rather than writing it in the high half
// in `round_sums` to save on writes. No benchmarking has been done
@@ -306,15 +297,10 @@ impl LutLayers {
(1 << self.log_trees).min(PARALLEL_MIN_LANES)
}
- /// Round 1's positions `0..half` in pieces, as a parallel iterator
- /// taking at least `PARALLEL_MIN_LANES` positions per task, whatever the
- /// number of trees.
- fn pieces(&self, half: usize) -> impl IndexedParallelIterator<Item = Range<usize>> + use<> {
+ /// Round 1's positions `0..half` in pieces.
+ fn pieces(&self, half: usize) -> impl Iterator<Item = Range<usize>> + use<> {
let piece = self.piece();
- (0..half / piece)
- .into_par_iter()
- .with_min_len(PARALLEL_MIN_LANES / piece)
- .map(move |p| p * piece..(p + 1) * piece)
+ (0..half / piece).map(move |p| p * piece..(p + 1) * piece)
}
/// The pairs that round 1 binds at `positions`, which lie in one lookup
@@ -429,7 +415,7 @@ fn bucket_sums(
lut_layers: &LutLayers,
eq_tables: &EqTables,
point: &[Field],
- buckets: impl Fn(&LutLayers, Range<usize>, &[Field]) -> (Wide256, Wide256) + Sync,
+ buckets: impl Fn(&LutLayers, Range<usize>, &[Field]) -> (Wide256, Wide256),
) -> (Wide256, Wide256) {
let top = point.len() - 1;
let (half, s) = (1 << top, lut_layers.log_trees);
@@ -446,8 +432,8 @@ fn bucket_sums(
Wide256::mul(h, sum_inf.reduce()),
)
})
- .reduce(
- || (Wide256::zero(), Wide256::zero()),
+ .fold(
+ (Wide256::zero(), Wide256::zero()),
|(x_b, x_inf), (y_b, y_inf)| (x_b + y_b, x_inf + y_inf),
)
}
@@ -546,8 +532,8 @@ fn layer2_sums<const AT_ONE: bool>(lut_layers: &LutLayers, eq: &[Field]) -> (Wid
})
})
})
- .reduce(
- || (Wide256::zero(), Wide256::zero()),
+ .fold(
+ (Wide256::zero(), Wide256::zero()),
|(x_b, x_inf), (y_b, y_inf)| (x_b + y_b, x_inf + y_inf),
)
}
@@ -569,8 +555,8 @@ fn lut_bind<const LAYER: usize>(
right: &mut [Field],
) {
let (half, piece) = (left.len(), lut_layers.piece());
- left.par_chunks_mut(piece)
- .zip(right.par_chunks_mut(piece))
+ left.chunks_mut(piece)
+ .zip(right.chunks_mut(piece))
.zip(lut_layers.pieces(half))
.for_each(|((left, right), positions)| {
let (luts, pairs) = lut_layers.pairs::<LAYER>(positions);
@@ -848,12 +834,10 @@ fn lower_luts(images: &[Field]) -> Vec<[Field; 16]> {
let (a, rest) = images.split_at(quart);
let (b, rest) = rest.split_at(quart);
let (c, d) = rest.split_at(quart);
- a.par_iter()
+ a.iter()
.zip(b)
.zip(c)
.zip(d)
- // A lookup table writes 16 entries.
- .with_min_len(PARALLEL_MIN_LANES / 16)
.map(|(((&a, &b), &c), &d)| create_lut([a, b, c, d]))
.collect()
}
@@ -866,7 +850,7 @@ fn layer3(lut_layers: &LutLayers) -> Vec<Field> {
let half = lut_layers.nodes::<LAYER2>() / 2;
let mut layer3 = Field::zeroed_vec(half);
layer3
- .par_chunks_mut(lut_layers.piece())
+ .chunks_mut(lut_layers.piece())
.zip(lut_layers.pieces(half))
.for_each(|(nodes, positions)| {
let ([lut0, lut1, ..], pairs) = lut_layers.pairs::<LAYER1>(positions);
@@ -891,12 +875,7 @@ fn fold_layers(mut layer: Vec<Field>, groups: usize) -> (Vec<Field>, Vec<Vec<Fie
// Node `k` multiplies node `k` of the left half and node `k` of the
// right half below it.
let (l, r) = layer.split_at(layer.len() / 2);
- let next = l
- .par_iter()
- .zip(r)
- .with_min_len(PARALLEL_MIN_LANES)
- .map(|(&a, &b)| a * b)
- .collect();
+ let next = l.iter().zip(r).map(|(&a, &b)| a * b).collect();
witnesses.push(layer);
layer = next;
}