webgpu-groth16 0.1.1

Groth16 GPU prover aimed primarily at browser environments
Documentation
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//! MSM (Multi-Scalar Multiplication) GPU pipeline dispatcher.
//!
//! Executes the 5-kernel Pippenger MSM pipeline in a single command encoder:
//!
//! ```text
//! bases ──► [to_montgomery] ──► bases(mont)
//!//! bucket_data ───────────────────────┤
//!//!                            [aggregate_buckets] ──► agg_output
//!//!                               (if has_chunks)          ▼
//!                                              [reduce_sub_buckets] ──► aggregated_buckets
//!//! bucket_values ─────────────────────────────────────────────────────────────┤
//!//!                                                                    [weight_buckets]
//!//!//!                                                                    [subsum_phase1] ──► partial_sums
//!//!//!                                                                                     [subsum_phase2] ──► window_sums
//! ```
//!
//! When sub-bucket chunking is active (`has_chunks`), aggregate writes to an
//! intermediate buffer and a reduce pass sums sub-bucket partials into the
//! final per-bucket buffer before weighting.

use wgpu::util::DeviceExt;

use super::curve::GpuCurve;
use super::{GpuContext, MsmBuffers, compute_pass};

impl<C: GpuCurve> GpuContext<C> {
    #[allow(clippy::too_many_arguments)]
    pub fn execute_msm(
        &self,
        is_g2: bool,
        bufs: &MsmBuffers<'_>,
        num_active_buckets: u32,
        num_dispatched: u32,
        has_chunks: bool,
        num_windows: u32,
        skip_montgomery: bool,
    ) {
        let bases_buf = bufs.bases;
        let base_indices_buf = bufs.base_indices;
        let bucket_pointers_buf = bufs.bucket_pointers;
        let bucket_sizes_buf = bufs.bucket_sizes;
        let aggregated_buckets_buf = bufs.aggregated_buckets;
        let bucket_values_buf = bufs.bucket_values;
        let window_starts_buf = bufs.window_starts;
        let window_counts_buf = bufs.window_counts;
        let window_sums_buf = bufs.window_sums;

        let point_gpu_bytes: u64 = if is_g2 {
            C::G2_GPU_BYTES as u64
        } else {
            C::G1_GPU_BYTES as u64
        };

        // When chunking is active, aggregate writes to a larger intermediate
        // buffer and a reduce pass sums sub-buckets into the final
        // aggregated_buckets buffer.
        let intermediate_buf = if has_chunks {
            Some(self.device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("MSM Intermediate Sub-Buckets"),
                size: num_dispatched as u64 * point_gpu_bytes,
                usage: wgpu::BufferUsages::STORAGE,
                mapped_at_creation: false,
            }))
        } else {
            None
        };

        // The buffer the aggregate kernel writes to: intermediate (chunked) or
        // final (unchunked).
        let agg_output_buf =
            intermediate_buf.as_ref().unwrap_or(aggregated_buckets_buf);

        let agg_bind_group =
            self.device.create_bind_group(&wgpu::BindGroupDescriptor {
                label: Some("MSM Agg Bind Group"),
                layout: &self.msm_agg_bind_group_layout,
                entries: &[
                    wgpu::BindGroupEntry {
                        binding: 0,
                        resource: bases_buf.as_entire_binding(),
                    },
                    wgpu::BindGroupEntry {
                        binding: 1,
                        resource: base_indices_buf.as_entire_binding(),
                    },
                    wgpu::BindGroupEntry {
                        binding: 2,
                        resource: bucket_pointers_buf.as_entire_binding(),
                    },
                    wgpu::BindGroupEntry {
                        binding: 3,
                        resource: bucket_sizes_buf.as_entire_binding(),
                    },
                    wgpu::BindGroupEntry {
                        binding: 4,
                        resource: agg_output_buf.as_entire_binding(),
                    },
                    wgpu::BindGroupEntry {
                        binding: 5,
                        resource: bucket_values_buf.as_entire_binding(),
                    },
                ],
            });

        let mut encoder = self.device.create_command_encoder(
            &wgpu::CommandEncoderDescriptor {
                label: Some("MSM Encoder"),
            },
        );

        #[cfg(feature = "profiling")]
        let mut profiler_guard = self.profiler.lock().unwrap();
        #[cfg(feature = "profiling")]
        let mut scope = profiler_guard
            .scope(if is_g2 { "msm_g2" } else { "msm_g1" }, &mut encoder);

        // Pre-pass: convert bases to Montgomery form in-place so aggregate
        // can skip per-point to_montgomery calls (saves 3 muls/load for G1, 6
        // for G2). Skipped when using persistent bases that are already
        // in Montgomery form.
        if !skip_montgomery {
            let mont_bind_group =
                self.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: Some("MSM Bases Mont Bind Group"),
                    layout: &self.montgomery_bind_group_layout,
                    entries: &[wgpu::BindGroupEntry {
                        binding: 0,
                        resource: bases_buf.as_entire_binding(),
                    }],
                });
            let point_size: u64 = if is_g2 {
                C::G2_GPU_BYTES as u64
            } else {
                C::G1_GPU_BYTES as u64
            };
            let num_bases = (bases_buf.size() / point_size) as u32;
            let mut cpass =
                compute_pass!(scope, encoder, "to_montgomery_bases");
            cpass.set_pipeline(if is_g2 {
                &self.msm_to_mont_g2_pipeline
            } else {
                &self.msm_to_mont_g1_pipeline
            });
            cpass.set_bind_group(0, &mont_bind_group, &[]);
            cpass.dispatch_workgroups(
                num_bases.div_ceil(C::MSM_WORKGROUP_SIZE),
                1,
                1,
            );
        }

        {
            let mut cpass = compute_pass!(scope, encoder, "bucket_aggregation");
            cpass.set_pipeline(if is_g2 {
                &self.msm_agg_g2_pipeline
            } else {
                &self.msm_agg_g1_pipeline
            });
            cpass.set_bind_group(0, &agg_bind_group, &[]);
            cpass.dispatch_workgroups(
                num_dispatched.div_ceil(C::MSM_WORKGROUP_SIZE).max(1),
                1,
                1,
            );
        }

        // When sub-bucket chunking is active, reduce sub-bucket partial sums
        // into the final per-bucket aggregated results.
        if has_chunks {
            let reduce_starts_buf = bufs
                .reduce_starts
                .expect("reduce_starts required when has_chunks");
            let reduce_counts_buf = bufs
                .reduce_counts
                .expect("reduce_counts required when has_chunks");
            let reduce_bind_group =
                self.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: Some("MSM Reduce Sub-Buckets BG"),
                    layout: &self.msm_reduce_bind_group_layout,
                    entries: &[
                        wgpu::BindGroupEntry {
                            binding: 0,
                            resource: agg_output_buf.as_entire_binding(),
                        },
                        wgpu::BindGroupEntry {
                            binding: 1,
                            resource: reduce_starts_buf.as_entire_binding(),
                        },
                        wgpu::BindGroupEntry {
                            binding: 2,
                            resource: reduce_counts_buf.as_entire_binding(),
                        },
                        wgpu::BindGroupEntry {
                            binding: 3,
                            resource: aggregated_buckets_buf
                                .as_entire_binding(),
                        },
                    ],
                });
            let mut cpass = compute_pass!(scope, encoder, "reduce_sub_buckets");
            cpass.set_pipeline(if is_g2 {
                &self.msm_reduce_g2_pipeline
            } else {
                &self.msm_reduce_g1_pipeline
            });
            cpass.set_bind_group(0, &reduce_bind_group, &[]);
            cpass.dispatch_workgroups(
                num_active_buckets.div_ceil(C::MSM_WORKGROUP_SIZE).max(1),
                1,
                1,
            );
        }

        // Weight each bucket sum by its bucket value in a separate kernel.
        // When chunking is active, use original bucket values (not sub-bucket
        // values).
        let weight_values_buf = if has_chunks {
            bufs.orig_bucket_values
                .expect("orig_bucket_values required when has_chunks")
        } else {
            bucket_values_buf
        };
        {
            let weight_bind_group =
                self.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: Some(if is_g2 {
                        "MSM Weight G2 BG"
                    } else {
                        "MSM Weight G1 BG"
                    }),
                    layout: if is_g2 {
                        &self.msm_weight_g2_bind_group_layout
                    } else {
                        &self.msm_weight_g1_bind_group_layout
                    },
                    entries: &[
                        wgpu::BindGroupEntry {
                            binding: 0,
                            resource: aggregated_buckets_buf
                                .as_entire_binding(),
                        },
                        wgpu::BindGroupEntry {
                            binding: 1,
                            resource: weight_values_buf.as_entire_binding(),
                        },
                    ],
                });
            let mut cpass = compute_pass!(scope, encoder, "bucket_weighting");
            cpass.set_pipeline(if is_g2 {
                &self.msm_weight_g2_pipeline
            } else {
                &self.msm_weight_g1_pipeline
            });
            cpass.set_bind_group(0, &weight_bind_group, &[]);
            cpass.dispatch_workgroups(
                num_active_buckets.div_ceil(C::MSM_WORKGROUP_SIZE).max(1),
                1,
                1,
            );
        }

        // Both G1 and G2: two-pass multi-workgroup tree reduction.
        // Phase 1: chunks_per_window workgroups per window each sum a
        // contiguous          slice of weighted buckets → partial_sums
        // buffer. Phase 2: one workgroup per window reduces
        // partial_sums → final window_sums.
        //
        // When chunking is active, subsum must use original window metadata
        // (which maps to num_active_buckets layout in aggregated_buckets_buf).
        {
            let chunks_per_window = if is_g2 {
                C::G2_SUBSUM_CHUNKS_PER_WINDOW
            } else {
                C::G1_SUBSUM_CHUNKS_PER_WINDOW
            };
            let subsum_window_starts = if has_chunks {
                bufs.orig_window_starts
                    .expect("orig_window_starts required when has_chunks")
            } else {
                window_starts_buf
            };
            let subsum_window_counts = if has_chunks {
                bufs.orig_window_counts
                    .expect("orig_window_counts required when has_chunks")
            } else {
                window_counts_buf
            };

            let partial_sums_buf =
                self.device.create_buffer(&wgpu::BufferDescriptor {
                    label: Some("MSM Partial Sums"),
                    size: (num_windows * chunks_per_window) as u64
                        * point_gpu_bytes,
                    usage: wgpu::BufferUsages::STORAGE,
                    mapped_at_creation: false,
                });
            let subsum_params: [u32; 4] = [chunks_per_window, 0, 0, 0];
            let subsum_params_buf = self.device.create_buffer_init(
                &wgpu::util::BufferInitDescriptor {
                    label: Some("Subsum Params"),
                    contents: bytemuck::cast_slice(&subsum_params),
                    usage: wgpu::BufferUsages::UNIFORM,
                },
            );

            let phase1_bind_group =
                self.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: Some("MSM Subsum Phase1 BG"),
                    layout: &self.msm_subsum_phase1_bind_group_layout,
                    entries: &[
                        wgpu::BindGroupEntry {
                            binding: 0,
                            resource: aggregated_buckets_buf
                                .as_entire_binding(),
                        },
                        wgpu::BindGroupEntry {
                            binding: 1,
                            resource: subsum_window_starts.as_entire_binding(),
                        },
                        wgpu::BindGroupEntry {
                            binding: 2,
                            resource: subsum_window_counts.as_entire_binding(),
                        },
                        wgpu::BindGroupEntry {
                            binding: 3,
                            resource: partial_sums_buf.as_entire_binding(),
                        },
                        wgpu::BindGroupEntry {
                            binding: 4,
                            resource: subsum_params_buf.as_entire_binding(),
                        },
                    ],
                });

            let phase2_bind_group =
                self.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: Some("MSM Subsum Phase2 BG"),
                    layout: &self.msm_subsum_phase2_bind_group_layout,
                    entries: &[
                        wgpu::BindGroupEntry {
                            binding: 0,
                            resource: partial_sums_buf.as_entire_binding(),
                        },
                        wgpu::BindGroupEntry {
                            binding: 1,
                            resource: window_sums_buf.as_entire_binding(),
                        },
                        wgpu::BindGroupEntry {
                            binding: 2,
                            resource: subsum_params_buf.as_entire_binding(),
                        },
                    ],
                });

            // Phase 1: many workgroups per window → partial sums.
            {
                let mut cpass =
                    compute_pass!(scope, encoder, "tree_reduction_ph1");
                cpass.set_pipeline(if is_g2 {
                    &self.msm_subsum_phase1_g2_pipeline
                } else {
                    &self.msm_subsum_phase1_g1_pipeline
                });
                cpass.set_bind_group(0, &phase1_bind_group, &[]);
                cpass.dispatch_workgroups(
                    num_windows * chunks_per_window,
                    1,
                    1,
                );
            }

            // Phase 2: reduce partial sums → final window sums.
            {
                let mut cpass =
                    compute_pass!(scope, encoder, "tree_reduction_ph2");
                cpass.set_pipeline(if is_g2 {
                    &self.msm_subsum_phase2_g2_pipeline
                } else {
                    &self.msm_subsum_phase2_g1_pipeline
                });
                cpass.set_bind_group(0, &phase2_bind_group, &[]);
                cpass.dispatch_workgroups(num_windows, 1, 1);
            }
        }

        #[cfg(feature = "profiling")]
        {
            drop(scope);
            profiler_guard.resolve_queries(&mut encoder);
        }

        self.queue.submit(Some(encoder.finish()));
    }

    /// Convert a bases buffer to Montgomery form in-place (one-time, for
    /// persistent bases).
    pub fn convert_to_montgomery(&self, buf: &wgpu::Buffer, is_g2: bool) {
        let mont_bind_group =
            self.device.create_bind_group(&wgpu::BindGroupDescriptor {
                label: Some("Convert To Montgomery BG"),
                layout: &self.montgomery_bind_group_layout,
                entries: &[wgpu::BindGroupEntry {
                    binding: 0,
                    resource: buf.as_entire_binding(),
                }],
            });
        let point_size: u64 = if is_g2 {
            C::G2_GPU_BYTES as u64
        } else {
            C::G1_GPU_BYTES as u64
        };
        let num_bases = (buf.size() / point_size) as u32;
        let mut encoder = self.device.create_command_encoder(
            &wgpu::CommandEncoderDescriptor {
                label: Some("Convert To Montgomery Encoder"),
            },
        );
        {
            let mut cpass =
                encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
                    label: Some("to_montgomery"),
                    timestamp_writes: None,
                });
            cpass.set_pipeline(if is_g2 {
                &self.msm_to_mont_g2_pipeline
            } else {
                &self.msm_to_mont_g1_pipeline
            });
            cpass.set_bind_group(0, &mont_bind_group, &[]);
            cpass.dispatch_workgroups(
                num_bases.div_ceil(C::MSM_WORKGROUP_SIZE),
                1,
                1,
            );
        }
        self.queue.submit(Some(encoder.finish()));
    }
}