ligerito 0.6.2

Ligerito polynomial commitment scheme over binary extension fields
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
448
449
450
451
452
453
454
455
456
457
//! GPU-accelerated sumcheck polynomial construction - V2
//!
//! This version scales to n=20, n=24, n=28 by eliminating massive buffer allocations.
//!
//! Architecture:
//! - GPU: Computes 148 dot products in parallel → 148 contributions (2.4 KB)
//! - CPU: Accumulates contributions into basis_poly (reuses single temp buffer)
//!
//! Memory usage: O(num_queries) instead of O(num_queries × 2^n)
//! - n=20: 2.4 KB instead of 2.4 GB
//! - n=24: 2.4 KB instead of 38 GB

use super::device::GpuDevice;
use super::shaders;
use binary_fields::BinaryFieldElement;
use bytemuck::{Pod, Zeroable};
use wgpu::{BindGroup, BindGroupLayout, Buffer, BufferUsages, ComputePipeline};

/// Sumcheck parameters
#[repr(C)]
#[derive(Copy, Clone, Debug)]
struct SumcheckParams {
    n: u32,
    num_queries: u32,
    k: u32,
    row_size: u32,
}

unsafe impl Pod for SumcheckParams {}
unsafe impl Zeroable for SumcheckParams {}

/// GPU sumcheck - scalable hybrid architecture
pub struct GpuSumcheck {
    device: GpuDevice,
    contribution_pipeline: Option<ComputePipeline>,
    bind_group_layout: Option<BindGroupLayout>,
}

impl GpuSumcheck {
    pub fn new(device: GpuDevice) -> Self {
        Self {
            device,
            contribution_pipeline: None,
            bind_group_layout: None,
        }
    }

    async fn init_pipelines(&mut self) -> Result<(), String> {
        if self.contribution_pipeline.is_some() {
            return Ok(());
        }

        // Load shader (hybrid GPU+CPU architecture)
        let shader_source = format!(
            "{}\n\n{}",
            shaders::BINARY_FIELD_SHADER,
            include_str!("shaders/sumcheck.wgsl")
        );

        let shader_module = self
            .device
            .device
            .create_shader_module(wgpu::ShaderModuleDescriptor {
                label: Some("Sumcheck Shader"),
                source: wgpu::ShaderSource::Wgsl(shader_source.into()),
            });

        // Create bind group layout
        let bind_group_layout =
            self.device
                .device
                .create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                    label: Some("Sumcheck Bind Group Layout"),
                    entries: &[
                        // 0: opened_rows (read)
                        wgpu::BindGroupLayoutEntry {
                            binding: 0,
                            visibility: wgpu::ShaderStages::COMPUTE,
                            ty: wgpu::BindingType::Buffer {
                                ty: wgpu::BufferBindingType::Storage { read_only: true },
                                has_dynamic_offset: false,
                                min_binding_size: None,
                            },
                            count: None,
                        },
                        // 1: v_challenges (read)
                        wgpu::BindGroupLayoutEntry {
                            binding: 1,
                            visibility: wgpu::ShaderStages::COMPUTE,
                            ty: wgpu::BindingType::Buffer {
                                ty: wgpu::BufferBindingType::Storage { read_only: true },
                                has_dynamic_offset: false,
                                min_binding_size: None,
                            },
                            count: None,
                        },
                        // 2: alpha_pows (read)
                        wgpu::BindGroupLayoutEntry {
                            binding: 2,
                            visibility: wgpu::ShaderStages::COMPUTE,
                            ty: wgpu::BindingType::Buffer {
                                ty: wgpu::BufferBindingType::Storage { read_only: true },
                                has_dynamic_offset: false,
                                min_binding_size: None,
                            },
                            count: None,
                        },
                        // 3: sorted_queries (read)
                        wgpu::BindGroupLayoutEntry {
                            binding: 3,
                            visibility: wgpu::ShaderStages::COMPUTE,
                            ty: wgpu::BindingType::Buffer {
                                ty: wgpu::BufferBindingType::Storage { read_only: true },
                                has_dynamic_offset: false,
                                min_binding_size: None,
                            },
                            count: None,
                        },
                        // 4: contributions (write) - SMALL BUFFER!
                        wgpu::BindGroupLayoutEntry {
                            binding: 4,
                            visibility: wgpu::ShaderStages::COMPUTE,
                            ty: wgpu::BindingType::Buffer {
                                ty: wgpu::BufferBindingType::Storage { read_only: false },
                                has_dynamic_offset: false,
                                min_binding_size: None,
                            },
                            count: None,
                        },
                        // 5: params (uniform)
                        wgpu::BindGroupLayoutEntry {
                            binding: 5,
                            visibility: wgpu::ShaderStages::COMPUTE,
                            ty: wgpu::BindingType::Buffer {
                                ty: wgpu::BufferBindingType::Uniform,
                                has_dynamic_offset: false,
                                min_binding_size: None,
                            },
                            count: None,
                        },
                    ],
                });

        let pipeline_layout =
            self.device
                .device
                .create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
                    label: Some("Sumcheck V2 Pipeline Layout"),
                    bind_group_layouts: &[&bind_group_layout],
                    push_constant_ranges: &[],
                });

        let contribution_pipeline =
            self.device
                .device
                .create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
                    label: Some("Sumcheck Contribution Pipeline"),
                    layout: Some(&pipeline_layout),
                    module: &shader_module,
                    entry_point: "sumcheck_contribution",
                    compilation_options: Default::default(),
                });

        self.contribution_pipeline = Some(contribution_pipeline);
        self.bind_group_layout = Some(bind_group_layout);

        Ok(())
    }

    /// Compute sumcheck with GPU contributions + CPU accumulation
    pub async fn induce_sumcheck_poly<T, U>(
        &mut self,
        n: usize,
        sks_vks: &[T],
        opened_rows: &[Vec<T>],
        v_challenges: &[U],
        sorted_queries: &[usize],
        alpha: U,
    ) -> Result<(Vec<U>, U), String>
    where
        T: BinaryFieldElement + Pod,
        U: BinaryFieldElement + Pod + From<T>,
    {
        let num_queries = opened_rows.len();
        if num_queries == 0 {
            return Ok((vec![U::zero(); 1 << n], U::zero()));
        }

        let k = v_challenges.len();
        let row_size = 1 << k;

        // Row size check (shader supports up to 256 elements with 4KB buffer)
        if row_size > 256 {
            #[cfg(not(target_arch = "wasm32"))]
            println!(
                "Row size {} exceeds GPU shader limit (256), falling back to CPU",
                row_size
            );

            use crate::sumcheck_polys::induce_sumcheck_poly as cpu_induce;
            return Ok(cpu_induce(
                n,
                sks_vks,
                opened_rows,
                v_challenges,
                sorted_queries,
                alpha,
            ));
        }

        self.init_pipelines().await?;

        // Step 1: GPU computes contributions in parallel
        let contributions = self
            .compute_contributions_gpu(n, opened_rows, v_challenges, sorted_queries, alpha)
            .await?;

        // Step 2: CPU accumulates contributions into basis_poly
        // This matches the CPU implementation's efficient memory pattern
        let (basis_poly, enforced_sum) =
            self.accumulate_contributions_cpu(n, sks_vks, &contributions, sorted_queries);

        Ok((basis_poly, enforced_sum))
    }

    /// GPU: Compute 148 contributions in parallel
    async fn compute_contributions_gpu<T, U>(
        &self,
        n: usize,
        opened_rows: &[Vec<T>],
        v_challenges: &[U],
        sorted_queries: &[usize],
        alpha: U,
    ) -> Result<Vec<U>, String>
    where
        T: BinaryFieldElement + Pod,
        U: BinaryFieldElement + Pod + From<T>,
    {
        let num_queries = opened_rows.len();
        let k = v_challenges.len();
        let row_size = 1 << k;

        // Flatten opened_rows for GPU
        let mut flattened_rows: Vec<U> = Vec::with_capacity(num_queries * row_size);
        for row in opened_rows {
            for &elem in row {
                flattened_rows.push(U::from(elem));
            }
        }

        // Precompute alpha powers
        let alpha_pows = crate::sumcheck_polys::precompute_alpha_powers(alpha, num_queries);

        // Create GPU buffers
        use wgpu::util::{BufferInitDescriptor, DeviceExt};

        let opened_rows_buffer = self
            .device
            .device
            .create_buffer_init(&BufferInitDescriptor {
                label: Some("Opened Rows"),
                contents: bytemuck::cast_slice(&flattened_rows),
                usage: BufferUsages::STORAGE | BufferUsages::COPY_SRC,
            });

        let v_challenges_buffer = self
            .device
            .device
            .create_buffer_init(&BufferInitDescriptor {
                label: Some("V Challenges"),
                contents: bytemuck::cast_slice(v_challenges),
                usage: BufferUsages::STORAGE | BufferUsages::COPY_SRC,
            });

        let alpha_pows_buffer = self
            .device
            .device
            .create_buffer_init(&BufferInitDescriptor {
                label: Some("Alpha Powers"),
                contents: bytemuck::cast_slice(&alpha_pows),
                usage: BufferUsages::STORAGE | BufferUsages::COPY_SRC,
            });

        let sorted_queries_u32: Vec<u32> = sorted_queries.iter().map(|&q| q as u32).collect();
        let sorted_queries_buffer = self
            .device
            .device
            .create_buffer_init(&BufferInitDescriptor {
                label: Some("Sorted Queries"),
                contents: bytemuck::cast_slice(&sorted_queries_u32),
                usage: BufferUsages::STORAGE | BufferUsages::COPY_SRC,
            });

        // Output buffer: SMALL! Only num_queries contributions
        let contributions_buffer = self.device.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Contributions"),
            size: (num_queries * std::mem::size_of::<U>()) as u64,
            usage: BufferUsages::STORAGE | BufferUsages::COPY_SRC,
            mapped_at_creation: false,
        });

        let params = SumcheckParams {
            n: n as u32,
            num_queries: num_queries as u32,
            k: k as u32,
            row_size: row_size as u32,
        };

        let params_buffer = self
            .device
            .device
            .create_buffer_init(&BufferInitDescriptor {
                label: Some("Params"),
                contents: bytemuck::bytes_of(&params),
                usage: BufferUsages::UNIFORM | BufferUsages::COPY_DST,
            });

        // Create bind group
        let bind_group = self
            .device
            .device
            .create_bind_group(&wgpu::BindGroupDescriptor {
                label: Some("Sumcheck V2 Bind Group"),
                layout: self.bind_group_layout.as_ref().unwrap(),
                entries: &[
                    wgpu::BindGroupEntry {
                        binding: 0,
                        resource: opened_rows_buffer.as_entire_binding(),
                    },
                    wgpu::BindGroupEntry {
                        binding: 1,
                        resource: v_challenges_buffer.as_entire_binding(),
                    },
                    wgpu::BindGroupEntry {
                        binding: 2,
                        resource: alpha_pows_buffer.as_entire_binding(),
                    },
                    wgpu::BindGroupEntry {
                        binding: 3,
                        resource: sorted_queries_buffer.as_entire_binding(),
                    },
                    wgpu::BindGroupEntry {
                        binding: 4,
                        resource: contributions_buffer.as_entire_binding(),
                    },
                    wgpu::BindGroupEntry {
                        binding: 5,
                        resource: params_buffer.as_entire_binding(),
                    },
                ],
            });

        // Dispatch compute shader
        let mut encoder =
            self.device
                .device
                .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                    label: Some("Sumcheck V2 Encoder"),
                });

        {
            let mut compute_pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("Sumcheck Contribution Pass"),
                timestamp_writes: None,
            });

            compute_pass.set_pipeline(self.contribution_pipeline.as_ref().unwrap());
            compute_pass.set_bind_group(0, &bind_group, &[]);
            compute_pass.dispatch_workgroups(num_queries as u32, 1, 1);
        }

        // Read back contributions
        let staging_buffer = self.device.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Staging Buffer"),
            size: (num_queries * std::mem::size_of::<U>()) as u64,
            usage: BufferUsages::COPY_DST | BufferUsages::MAP_READ,
            mapped_at_creation: false,
        });

        encoder.copy_buffer_to_buffer(
            &contributions_buffer,
            0,
            &staging_buffer,
            0,
            (num_queries * std::mem::size_of::<U>()) as u64,
        );

        self.device.queue.submit([encoder.finish()]);

        let buffer_slice = staging_buffer.slice(..);
        let (sender, receiver) = futures::channel::oneshot::channel();
        buffer_slice.map_async(wgpu::MapMode::Read, move |result| {
            sender.send(result).ok();
        });

        self.device.device.poll(wgpu::Maintain::Wait);
        receiver
            .await
            .map_err(|_| "Failed to receive mapping".to_string())?
            .map_err(|e| format!("Buffer mapping failed: {:?}", e))?;

        let data = buffer_slice.get_mapped_range();
        let contributions: Vec<U> = bytemuck::cast_slice(&data).to_vec();
        drop(data);
        staging_buffer.unmap();

        Ok(contributions)
    }

    /// CPU: Accumulate contributions into basis_poly (efficient memory pattern)
    fn accumulate_contributions_cpu<T, U>(
        &self,
        n: usize,
        sks_vks: &[T],
        contributions: &[U],
        sorted_queries: &[usize],
    ) -> (Vec<U>, U)
    where
        T: BinaryFieldElement,
        U: BinaryFieldElement + From<T>,
    {
        use crate::utils::evaluate_scaled_basis_inplace;

        let basis_size = 1 << n;
        let mut basis_poly = vec![U::zero(); basis_size];
        let mut enforced_sum = U::zero();

        // Reuse these buffers across iterations (same as CPU version!)
        let mut local_sks_x = vec![T::zero(); sks_vks.len()];
        let mut local_basis = vec![U::zero(); basis_size];

        for (i, (&contribution, &query)) in
            contributions.iter().zip(sorted_queries.iter()).enumerate()
        {
            enforced_sum = enforced_sum.add(&contribution);

            let query_mod = query % basis_size;
            let qf = T::from_bits(query_mod as u64);

            // Compute scaled basis (reuses buffers!)
            evaluate_scaled_basis_inplace(
                &mut local_sks_x,
                &mut local_basis,
                sks_vks,
                qf,
                contribution,
            );

            // Accumulate into basis_poly
            for (j, &val) in local_basis.iter().enumerate() {
                basis_poly[j] = basis_poly[j].add(&val);
            }
        }

        (basis_poly, enforced_sum)
    }
}