openvm-cuda-backend 2.0.0

OpenVM CUDA prover backend for the SWIRL proof system
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
#![allow(dead_code)]
use openvm_cuda_common::{
    copy::{MemCopyD2H, MemCopyH2D},
    d_buffer::DeviceBuffer,
    stream::GpuDeviceCtx,
};
use openvm_stark_backend::{
    p3_util::log2_strict_usize,
    poly_common::UnivariatePoly,
    prover::sumcheck::{SumcheckCubeProof, SumcheckPrismProof},
    FiatShamirTranscript,
};
use p3_field::{ExtensionField, Field};
use tracing::{debug, info_span, instrument};

use crate::{
    cuda::{
        batch_ntt_small::batch_ntt_small,
        matrix::batch_expand_pad_wide,
        sumcheck::{fold_mle, fold_ple_from_coeffs, reduce_over_x_and_cols, sumcheck_mle_round},
    },
    error::SumcheckError,
    prelude::*,
    sponge::DuplexSpongeGpu,
};

/// GPU implementation of multilinear sumcheck
///
/// Memory strategy: Ping-pong buffers (buffer_a ↔ buffer_b) alternate each round
/// - Round 0 (even): reads buffer_a, writes buffer_b
/// - Round 1 (odd):  reads buffer_b, writes buffer_a
/// - Final result in buffer determined by parity of n
/// - Memory footprint: ~1.5 * evals.len() * sizeof(EF)
#[allow(dead_code)]
#[instrument(name = "sumcheck_multilinear_gpu", level = "info", skip_all)]
pub fn sumcheck_multilinear_gpu<F: Field>(
    transcript: &mut DuplexSpongeGpu,
    evals: &[F],
    device_ctx: &GpuDeviceCtx,
) -> Result<(SumcheckCubeProof<EF>, Vec<EF>), SumcheckError>
where
    EF: ExtensionField<F>,
{
    let n = log2_strict_usize(evals.len());
    let mut round_polys_eval = Vec::with_capacity(n);
    let mut r = Vec::with_capacity(n);

    // Compute sum claim
    let sum_claim: EF = evals.iter().copied().sum::<F>().into();
    transcript.observe_ext(sum_claim);

    // Convert to extension field
    let evals_ext: Vec<EF> = evals.iter().map(|&x| EF::from(x)).collect();

    // Setup
    let mut current_height = 1 << n;
    let width = 1;
    let num_matrices = 1;
    let d = 1; // Degree for MLE
    const WD: usize = 1; // Number of output polynomials

    // Allocate ping-pong buffers
    let total_size = width * current_height;
    let mut d_buffer_a = evals_ext.to_device_on(device_ctx)?;
    let mut d_buffer_b = DeviceBuffer::<EF>::with_capacity_on(total_size / 2, device_ctx);

    // Set up pointer arrays on device
    let mut d_input_ptrs = DeviceBuffer::<*const EF>::with_capacity_on(num_matrices, device_ctx);
    let mut d_output_ptrs = DeviceBuffer::<*mut EF>::with_capacity_on(num_matrices, device_ctx);
    let d_widths = [width as u32].to_device_on(device_ctx)?;

    // Buffer for round output [d * WD]
    let d_round_output = DeviceBuffer::<EF>::with_capacity_on(d * WD, device_ctx);

    // Sumcheck rounds
    for round in 0..n {
        // Ping-pong buffers
        let (input_buf, output_buf) = if round % 2 == 0 {
            (&d_buffer_a, &mut d_buffer_b)
        } else {
            (&d_buffer_b, &mut d_buffer_a)
        };

        // Update pointer arrays
        let input_ptr = input_buf.as_ptr();
        let output_ptr = output_buf.as_mut_ptr();

        [input_ptr].copy_to_on(&mut d_input_ptrs, device_ctx)?;
        [output_ptr].copy_to_on(&mut d_output_ptrs, device_ctx)?;

        // Call sumcheck_mle_round kernel (uses output_buf as tmp)
        unsafe {
            sumcheck_mle_round(
                &d_input_ptrs,
                &d_round_output,
                output_buf, // Reuse output buffer as tmp!
                &d_widths,
                num_matrices as u32,
                current_height as u32,
                d as u32,
                device_ctx.stream.as_raw(),
            )
            .map_err(|e| SumcheckError::SumcheckMleRound(e.into()))?;
        }

        // Copy result back to host
        let h_round_output = d_round_output.to_host_on(device_ctx)?;

        // Observe in transcript
        let s = h_round_output[0..d].to_vec();

        assert_eq!(s.len(), d);
        transcript.observe_ext(s[0]);
        round_polys_eval.push(s);

        // Sample challenge from transcript
        let r_round = transcript.sample_ext();
        debug!(%round, %r_round);
        r.push(r_round);

        // Fold using the challenge
        let output_height = (current_height >> 1) as u32;
        unsafe {
            fold_mle(
                &d_input_ptrs,
                &d_output_ptrs,
                &d_widths,
                num_matrices.try_into().unwrap(),
                output_height,
                width as u32 * output_height,
                r_round,
                device_ctx.stream.as_raw(),
            )
            .map_err(|e| SumcheckError::FoldMle(e.into()))?;
        }

        current_height >>= 1;
    }

    // After all rounds, get final evaluation claim
    let final_buf = if n % 2 == 1 { &d_buffer_b } else { &d_buffer_a };
    let eval_claim_vec = final_buf.to_host_on(device_ctx)?;
    let eval_claim = eval_claim_vec[0];

    transcript.observe_ext(eval_claim);

    Ok((
        SumcheckCubeProof {
            sum_claim,
            round_polys_eval,
            eval_claim,
        },
        r,
    ))
}

/// GPU implementation of prismalinear sumcheck with univariate skip
///
/// Memory strategy:
/// - Round 0: Uses DFT/iDFT pipeline, reuses buffers between steps
/// - Rounds 1..n: Standard MLE rounds with ping-pong buffers
/// - d_evals gets modified by iDFT in round 0, then reused for fold_ple
/// - Memory footprint: if evals.len() = 2^(l_skip +n), then 2 * evals.len() * sizeof(F) + 1.5 * 2^n
///   * sizeof(EF)
///
/// NOTE: batch_ntt expects a concrete type BabyBear, so I removed generic type parameters for now
#[allow(dead_code)]
#[instrument(name = "sumcheck_prismalinear_gpu", level = "info", skip_all)]
pub fn sumcheck_prismalinear_gpu(
    transcript: &mut DuplexSpongeGpu,
    l_skip: usize,
    evals: &[F],
    device_ctx: &GpuDeviceCtx,
) -> Result<(SumcheckPrismProof<EF>, Vec<EF>), SumcheckError> {
    let prism_dim = p3_util::log2_strict_usize(evals.len());
    assert!(prism_dim >= l_skip);
    let n = prism_dim - l_skip;

    let mut round_polys_eval = Vec::with_capacity(n);
    let mut r = Vec::with_capacity(n + 1);

    // Compute sum claim
    let sum_claim: EF = evals.iter().copied().sum::<F>().into();
    transcript.observe_ext(sum_claim);

    // Setup
    let domain_size = 1 << l_skip;
    let num_x = 1 << n;
    let width = 1;
    let d = 1; // Degree for simple case
    let s_deg = d * (domain_size - 1); // d * (2^l_skip - 1)
    let log_large_domain = p3_util::log2_ceil_usize(s_deg + 1);
    let large_domain_size = 1 << log_large_domain;

    // ========== Round 0: Special PLE round ==========
    let _round0_span = info_span!("sumcheck_prismalinear.round0").entered();

    let mut d_coeffs = evals.to_device_on(device_ctx)?;
    let mut d_s0_coeffs = DeviceBuffer::<F>::with_capacity_on(large_domain_size, device_ctx);

    // Step 1: iDFT on skip domain (reinterpreting dimensions)
    // Input: [height=2^(l_skip+n), width=1]
    // Treat as: [height=2^l_skip, width=2^n]
    unsafe {
        batch_ntt_small(
            &mut d_coeffs,
            l_skip,
            num_x * width,
            true,
            device_ctx.stream.as_raw(),
        )
        .map_err(|e| SumcheckError::BatchNttSmall(e.into()))?;
    }

    if domain_size == large_domain_size {
        unsafe {
            // Step 2-5: Sum over all x and columns
            reduce_over_x_and_cols(
                &d_coeffs,
                &d_s0_coeffs,
                num_x as u32,
                width as u32,
                large_domain_size as u32,
                device_ctx.stream.as_raw(),
            )
            .map_err(|e| SumcheckError::ReduceOverXAndCols(e.into()))?;
        }
    } else {
        let mut d_coeffs_large =
            DeviceBuffer::<F>::with_capacity_on(num_x * width * large_domain_size, device_ctx);

        // Step 2: Copy and pad each column to large domain size
        unsafe {
            batch_expand_pad_wide(
                d_coeffs_large.as_mut_ptr(),
                d_coeffs.as_ptr(),
                (num_x * width) as u32,
                large_domain_size as u32,
                domain_size as u32,
                device_ctx.stream.as_raw(),
            )
            .map_err(|e| SumcheckError::BatchExpandPadWide(e.into()))?;
        }

        // Step 3: DFT on large domain
        unsafe {
            batch_ntt_small(
                &mut d_coeffs_large,
                log_large_domain,
                num_x * width,
                false,
                device_ctx.stream.as_raw(),
            )
            .map_err(|e| SumcheckError::BatchNttSmall(e.into()))?;
        }

        // Step 4: Sum over all x and columns
        unsafe {
            reduce_over_x_and_cols(
                &d_coeffs_large,
                &d_s0_coeffs,
                num_x as u32,
                width as u32,
                large_domain_size as u32,
                device_ctx.stream.as_raw(),
            )
            .map_err(|e| SumcheckError::ReduceOverXAndCols(e.into()))?;
        }
        drop(d_coeffs_large);

        // Step 5: iDFT to get coefficients
        unsafe {
            batch_ntt_small(
                &mut d_s0_coeffs,
                log_large_domain,
                1,
                true,
                device_ctx.stream.as_raw(),
            )
            .map_err(|e| SumcheckError::BatchNttSmall(e.into()))?;
        }
    }
    // Step 6: Copy to host and convert to extension field
    let s0_coeffs_host: Vec<F> = d_s0_coeffs.to_host_on(device_ctx)?;
    drop(d_s0_coeffs);
    let s0_coeffs_ext: Vec<EF> = s0_coeffs_host[0..=s_deg]
        .iter()
        .map(|&x| EF::from(x))
        .collect();

    // Step 7: Create polynomial and observe in transcript
    let s_0 = UnivariatePoly::new(s0_coeffs_ext.clone());
    for &coeff in &s0_coeffs_ext {
        transcript.observe_ext(coeff);
    }

    // Step 8: Sample challenge r_0
    let r_0 = transcript.sample_ext();
    debug!(round = 0, r_round = %r_0);
    r.push(r_0);

    // ========== Fold PLE: Evaluate at r_0 ==========

    let d_folded = DeviceBuffer::<EF>::with_capacity_on(num_x, device_ctx);
    unsafe {
        fold_ple_from_coeffs(
            d_coeffs.as_ptr(),     // Original input (base field, but modified by iDFT)
            d_folded.as_mut_ptr(), // Output in extension field
            num_x as u32,
            width as u32,
            domain_size as u32,
            r_0,
            device_ctx.stream.as_raw(),
        )
        .map_err(|e| SumcheckError::FoldPleFromCoeffs(e.into()))?;
    }
    drop(d_coeffs);
    drop(_round0_span);

    // ========== Rounds 1..n: Regular MLE rounds ==========
    let _mle_rounds_span = info_span!("sumcheck_prismalinear.mle_rounds").entered();

    let mut current_height = num_x; // After fold_ple, height is 2^n
    let num_matrices = 1;

    // Allocate ping-pong buffers for MLE rounds
    let mut d_buffer_a = d_folded; // Reuse folded result
    let mut d_buffer_b = DeviceBuffer::<EF>::with_capacity_on(current_height / 2, device_ctx);

    let mut d_input_ptrs = DeviceBuffer::<*const EF>::with_capacity_on(num_matrices, device_ctx);
    let mut d_output_ptrs = DeviceBuffer::<*mut EF>::with_capacity_on(num_matrices, device_ctx);
    let d_widths = [width as u32].to_device_on(device_ctx)?;
    let d_round_output = DeviceBuffer::<EF>::with_capacity_on(d, device_ctx);

    for round in 1..=n {
        let (input_buf, output_buf) = if round % 2 == 1 {
            (&d_buffer_a, &mut d_buffer_b)
        } else {
            (&d_buffer_b, &mut d_buffer_a)
        };

        let input_ptr = input_buf.as_ptr();
        let output_ptr = output_buf.as_mut_ptr();

        [input_ptr].copy_to_on(&mut d_input_ptrs, device_ctx)?;
        [output_ptr].copy_to_on(&mut d_output_ptrs, device_ctx)?;

        // Sumcheck MLE round
        unsafe {
            sumcheck_mle_round(
                &d_input_ptrs,
                &d_round_output,
                output_buf,
                &d_widths,
                num_matrices as u32,
                current_height as u32,
                d as u32,
                device_ctx.stream.as_raw(),
            )
            .map_err(|e| SumcheckError::SumcheckMleRound(e.into()))?;
        }

        let h_round_output = d_round_output.to_host_on(device_ctx)?;
        let s = h_round_output[0..d].to_vec();
        assert_eq!(s.len(), d);
        transcript.observe_ext(s[0]);
        round_polys_eval.push(s);

        let r_round = transcript.sample_ext();
        debug!(%round, %r_round);
        r.push(r_round);

        // Fold MLE
        let output_height = (current_height >> 1) as u32;
        unsafe {
            fold_mle(
                &d_input_ptrs,
                &d_output_ptrs,
                &d_widths,
                num_matrices.try_into().unwrap(),
                output_height,
                width as u32 * output_height,
                r_round,
                device_ctx.stream.as_raw(),
            )
            .map_err(|e| SumcheckError::FoldMle(e.into()))?;
        }

        current_height >>= 1;
    }
    drop(_mle_rounds_span);

    // Get final evaluation claim
    let final_buf = if n % 2 == 1 { &d_buffer_b } else { &d_buffer_a };
    let eval_claim_vec = final_buf.to_host_on(device_ctx)?;
    let eval_claim = eval_claim_vec[0];

    transcript.observe_ext(eval_claim);

    Ok((
        SumcheckPrismProof {
            sum_claim,
            s_0,
            round_polys_eval,
            eval_claim,
        },
        r,
    ))
}