sp1-gpu-sys 6.8.0

FFI bindings and CUDA build system for SP1-GPU
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
#pragma once

#include <cuda_runtime.h>
#include <ntt.hpp>

#include <cstdint>
#include <map>
#include <mutex>

namespace nvidia_ntt {

constexpr uint32_t PRIME = cupqc::KoalaBear;
constexpr uint32_t BLOCK_SIZE = 128;

template <uint32_t N, cupqc::nttDirection D>
using StandardNtt = decltype(
    cupqc::Algorithm<cupqc::algorithm::NTT>() + cupqc::Direction<D>() +
    cupqc::Precision<uint32_t>() + cupqc::Size<N>() + cupqc::Block() +
    cupqc::BlockDim<BLOCK_SIZE>());

template <uint32_t N, uint32_t M, cupqc::nttDirection D>
using StagedNtt = decltype(
    cupqc::Algorithm<cupqc::algorithm::NTT>() + cupqc::Direction<D>() +
    cupqc::Precision<uint32_t>() + cupqc::Size<N>() + cupqc::SubSize<M>() +
    cupqc::Block() + cupqc::BlockDim<BLOCK_SIZE>());

constexpr uint32_t primitive_root(uint32_t log_size) {
    uint64_t root = cupqc::KoalaBear_primitive_root_24;
    for (uint32_t log = 24; log > log_size; --log) {
        root = root * root % PRIME;
    }
    return static_cast<uint32_t>(root);
}

inline uint32_t mod_pow(uint64_t base, uint32_t exponent) {
    uint64_t result = 1;
    while (exponent != 0) {
        if ((exponent & 1) != 0) {
            result = result * base % PRIME;
        }
        base = base * base % PRIME;
        exponent >>= 1;
    }
    return static_cast<uint32_t>(result);
}

template <class Ntt>
__global__ void make_twiddles(uint32_t* twiddles, uint32_t root) {
    Ntt().make_twiddles(twiddles, PRIME, root);
}

template <class Ntt>
__global__ void twiddles_to_montgomery(uint32_t* twiddles) {
    Ntt().transform_twiddles_to_mont(twiddles, PRIME);
}

struct TwiddleCache {
    uint32_t* forward = nullptr;
    uint32_t* inverse = nullptr;
    bool initialized = false;
};

template <uint32_t N, class ForwardNtt, class InverseNtt>
cudaError_t get_twiddles(
    uint32_t*& forward,
    uint32_t*& inverse,
    const cudaStream_t stream) {
    static std::mutex mutex;
    static std::map<int, TwiddleCache> caches;
    int device;
    cudaError_t error = cudaGetDevice(&device);
    if (error != cudaSuccess) {
        return error;
    }
    std::lock_guard lock(mutex);
    TwiddleCache& cache = caches[device];

    if (!cache.initialized) {
        error = cudaMalloc(reinterpret_cast<void**>(&cache.forward), N * sizeof(uint32_t));
        if (error == cudaSuccess) {
            error = cudaMalloc(reinterpret_cast<void**>(&cache.inverse), N * sizeof(uint32_t));
        }

        constexpr uint32_t log_size = __builtin_ctz(N);
        const uint32_t root = primitive_root(log_size);
        if (error == cudaSuccess) {
            make_twiddles<ForwardNtt><<<1, 1, 0, stream>>>(cache.forward, root);
            error = cudaGetLastError();
        }
        if (error == cudaSuccess) {
            make_twiddles<InverseNtt><<<1, 1, 0, stream>>>(
                cache.inverse, mod_pow(root, PRIME - 2));
            error = cudaGetLastError();
        }
        if (error == cudaSuccess) {
            twiddles_to_montgomery<ForwardNtt><<<1, ForwardNtt::BlockDim, 0, stream>>>(
                cache.forward);
            error = cudaGetLastError();
        }
        if (error == cudaSuccess) {
            twiddles_to_montgomery<InverseNtt><<<1, InverseNtt::BlockDim, 0, stream>>>(
                cache.inverse);
            error = cudaGetLastError();
        }
        if (error == cudaSuccess) {
            error = cudaStreamSynchronize(stream);
        }
        if (error != cudaSuccess) {
            cudaFree(cache.forward);
            cudaFree(cache.inverse);
            cache.forward = nullptr;
            cache.inverse = nullptr;
            return error;
        }
        cache.initialized = true;
    }

    forward = cache.forward;
    inverse = cache.inverse;
    return cudaSuccess;
}

template <class Ntt, bool INVERSE>
__global__ void standard_ntt(
    uint32_t* data,
    const uint32_t stride,
    const uint32_t* twiddles,
    const uint32_t n_inv) {
    extern __shared__ uint32_t shared[];
    uint32_t* polynomial = data + blockIdx.x * stride;
    Ntt ntt;
    ntt.load(shared, polynomial);
    __syncthreads();
    if constexpr (INVERSE) {
        ntt.execute(shared, twiddles, PRIME, n_inv);
    } else {
        ntt.execute(shared, twiddles, PRIME);
    }
    __syncthreads();
    ntt.store(shared, polynomial);
}

template <uint32_t N, uint32_t M, class Ntt, bool INVERSE>
__global__ void staged_ntt_first(
    uint32_t* data,
    const uint32_t stride,
    const uint32_t* twiddles) {
    data += blockIdx.y * stride;
    extern __shared__ uint32_t shared[];
    Ntt ntt;
    ntt.stage_1_load(shared, data, blockIdx.x);
    __syncthreads();
    ntt.stage_1_execute(shared, twiddles, PRIME);
    __syncthreads();
    ntt.stage_1_store(shared, data, blockIdx.x);
}

__device__ __forceinline__ fr_t lde_root(uint32_t power, const fr_t* roots) {
    if (power == 0) {
        return fr_t::one();
    }

    constexpr uint32_t window_bits = 5;
    constexpr uint32_t window_size = 1U << window_bits;
    uint32_t window = 0;
    while ((power & (window_size - 1)) == 0) {
        power >>= window_bits;
        ++window;
    }

    fr_t root = roots[window * window_size + (power & (window_size - 1))];
    while (power >>= window_bits) {
        ++window;
        root *= roots[window * window_size + (power & (window_size - 1))];
    }
    return root;
}

template <uint32_t N, uint32_t M, class Ntt>
__global__ void staged_ntt_first_coset(
    uint32_t* output,
    const fr_t* input,
    const uint32_t input_size,
    const fr_t* gen_powers,
    const fr_t shift,
    const bool perform_shift,
    const uint32_t* twiddles) {
    output += blockIdx.y * N;
    input += blockIdx.y * input_size;
    extern __shared__ uint32_t shared[];
    constexpr uint32_t chunk_size = N / M;

    // cuPQC uses this strided input layout for forward stage one.
    for (uint32_t offset = threadIdx.x; offset < chunk_size; offset += blockDim.x) {
        const uint32_t index = blockIdx.x + M * offset;
        fr_t value = fr_t::zero();
        if (index < input_size) {
            value = input[index];
            if (perform_shift) {
                value = value * lde_root(index, gen_powers) * (shift^index);
            }
        }
        shared[offset] = value.val;
    }

    __syncthreads();
    Ntt ntt;
    ntt.stage_1_execute(shared, twiddles, PRIME);
    __syncthreads();
    ntt.stage_1_store(shared, output, blockIdx.x);
}

template <uint32_t N, uint32_t M, class Ntt, bool INVERSE>
__global__ void staged_ntt_second(
    uint32_t* data,
    const uint32_t stride,
    const uint32_t* twiddles,
    const uint32_t n_inv) {
    data += blockIdx.y * stride;
    extern __shared__ uint32_t shared[];
    Ntt ntt;
    ntt.stage_2_load(shared, data, blockIdx.x);
    __syncthreads();
    if constexpr (INVERSE) {
        ntt.stage_2_execute(shared, twiddles, PRIME, n_inv);
    } else {
        ntt.stage_2_execute(shared, twiddles, PRIME);
    }
    __syncthreads();
    ntt.stage_2_store(shared, data, blockIdx.x);
}

template <uint32_t N>
cudaError_t launch_standard(
    uint32_t* data,
    const uint32_t polynomial_count,
    const uint32_t stride,
    const bool inverse,
    const cudaStream_t stream) {
    using ForwardNtt = StandardNtt<N, cupqc::nttDirection::FORWARD>;
    using InverseNtt = StandardNtt<N, cupqc::nttDirection::INVERSE>;
    uint32_t* forward_twiddles;
    uint32_t* inverse_twiddles;
    cudaError_t error = get_twiddles<N, ForwardNtt, InverseNtt>(
        forward_twiddles, inverse_twiddles, stream);
    if (error != cudaSuccess) {
        return error;
    }
    if (polynomial_count == 0) {
        return cudaSuccess;
    }

    constexpr size_t shared_memory = cupqc::ntt_shared_workspace_size<N, uint32_t>();
    const uint32_t n_inv = cupqc::n_inv<N>(PRIME);
    if (inverse) {
        standard_ntt<InverseNtt, true><<<polynomial_count, BLOCK_SIZE, shared_memory, stream>>>(
            data, stride, inverse_twiddles, n_inv);
    } else {
        standard_ntt<ForwardNtt, false><<<polynomial_count, BLOCK_SIZE, shared_memory, stream>>>(
            data, stride, forward_twiddles, n_inv);
    }
    return cudaGetLastError();
}

__global__ void prepare_size_512(
    const uint32_t* data,
    uint32_t* workspace,
    const uint32_t stride,
    const bool inverse) {
    const uint32_t index = blockIdx.x * blockDim.x + threadIdx.x;
    const uint32_t polynomial = index / 1024;
    const uint32_t offset = index % 1024;
    if (inverse) {
        workspace[index] = (offset & 1) == 0 ? data[polynomial * stride + offset / 2] : 0;
    } else {
        workspace[index] = offset < 512 ? data[polynomial * stride + offset] : 0;
    }
}

__global__ void finish_size_512(
    uint32_t* data,
    const uint32_t* workspace,
    const uint32_t stride,
    const bool inverse) {
    const uint32_t index = blockIdx.x * blockDim.x + threadIdx.x;
    const uint32_t polynomial = index / 512;
    const uint32_t offset = index % 512;
    uint32_t value = workspace[polynomial * 1024 + (inverse ? 2 * offset : offset)];
    if (inverse) {
        value = value >= PRIME - value ? value - (PRIME - value) : value + value;
    }
    data[polynomial * stride + offset] = value;
}

inline cudaError_t launch_size_512(
    uint32_t* data,
    const uint32_t polynomial_count,
    const uint32_t stride,
    const bool inverse,
    const cudaStream_t stream) {
    if (polynomial_count == 0) {
        return launch_standard<1024>(nullptr, 0, 0, inverse, stream);
    }
    uint32_t* workspace = nullptr;
    cudaError_t error = cudaMallocAsync(
        reinterpret_cast<void**>(&workspace), polynomial_count * 1024 * sizeof(uint32_t), stream);
    constexpr uint32_t threads = 256;
    if (error == cudaSuccess) {
        prepare_size_512<<<polynomial_count * 4, threads, 0, stream>>>(
            data, workspace, stride, inverse);
        error = cudaGetLastError();
    }
    if (error == cudaSuccess) {
        error = launch_standard<1024>(workspace, polynomial_count, 1024, inverse, stream);
    }
    if (error == cudaSuccess) {
        finish_size_512<<<polynomial_count * 2, threads, 0, stream>>>(
            data, workspace, stride, inverse);
        error = cudaGetLastError();
    }
    const cudaError_t free_error =
        workspace == nullptr ? cudaSuccess : cudaFreeAsync(workspace, stream);
    return error == cudaSuccess ? free_error : error;
}

template <uint32_t N, uint32_t M>
cudaError_t launch_staged(
    uint32_t* data,
    const uint32_t polynomial_count,
    const uint32_t stride,
    const bool inverse,
    const cudaStream_t stream) {
    using ForwardNtt = StagedNtt<N, M, cupqc::nttDirection::FORWARD>;
    using InverseNtt = StagedNtt<N, M, cupqc::nttDirection::INVERSE>;
    uint32_t* forward_twiddles;
    uint32_t* inverse_twiddles;
    cudaError_t error = get_twiddles<N, ForwardNtt, InverseNtt>(
        forward_twiddles, inverse_twiddles, stream);
    if (error != cudaSuccess) {
        return error;
    }
    if (polynomial_count == 0) {
        return cudaSuccess;
    }

    const uint32_t n_inv = cupqc::n_inv<N>(PRIME);
    if (inverse) {
        constexpr size_t first_shared =
            cupqc::inv_stage_1_ntt_shared_workspace_size<N, M, uint32_t>();
        constexpr size_t second_shared =
            cupqc::inv_stage_2_ntt_shared_workspace_size<N, M, uint32_t>();
        staged_ntt_first<N, M, InverseNtt, true>
            <<<dim3(N / M, polynomial_count), BLOCK_SIZE, first_shared, stream>>>(
                data, stride, inverse_twiddles);
        error = cudaGetLastError();
        if (error == cudaSuccess) {
            staged_ntt_second<N, M, InverseNtt, true>
                <<<dim3(M, polynomial_count), BLOCK_SIZE, second_shared, stream>>>(
                    data, stride, inverse_twiddles, n_inv);
            error = cudaGetLastError();
        }
    } else {
        constexpr size_t first_shared =
            cupqc::fwd_stage_1_ntt_shared_workspace_size<N, M, uint32_t>();
        constexpr size_t second_shared =
            cupqc::fwd_stage_2_ntt_shared_workspace_size<N, M, uint32_t>();
        staged_ntt_first<N, M, ForwardNtt, false>
            <<<dim3(M, polynomial_count), BLOCK_SIZE, first_shared, stream>>>(
                data, stride, forward_twiddles);
        error = cudaGetLastError();
        if (error == cudaSuccess) {
            staged_ntt_second<N, M, ForwardNtt, false>
                <<<dim3(N / M, polynomial_count), BLOCK_SIZE, second_shared, stream>>>(
                    data, stride, forward_twiddles, n_inv);
            error = cudaGetLastError();
        }
    }
    return error;
}

template <uint32_t N, uint32_t M>
cudaError_t launch_staged_coset(
    uint32_t* output,
    const uint32_t polynomial_count,
    const fr_t* input,
    const uint32_t input_size,
    const fr_t* gen_powers,
    const fr_t shift,
    const bool perform_shift,
    const cudaStream_t stream) {
    using ForwardNtt = StagedNtt<N, M, cupqc::nttDirection::FORWARD>;
    using InverseNtt = StagedNtt<N, M, cupqc::nttDirection::INVERSE>;
    uint32_t* forward_twiddles;
    uint32_t* inverse_twiddles;
    cudaError_t error = get_twiddles<N, ForwardNtt, InverseNtt>(
        forward_twiddles, inverse_twiddles, stream);
    if (error != cudaSuccess || polynomial_count == 0) {
        return error;
    }

    constexpr size_t first_shared =
        cupqc::fwd_stage_1_ntt_shared_workspace_size<N, M, uint32_t>();
    constexpr size_t second_shared =
        cupqc::fwd_stage_2_ntt_shared_workspace_size<N, M, uint32_t>();
    staged_ntt_first_coset<N, M, ForwardNtt>
        <<<dim3(M, polynomial_count), BLOCK_SIZE, first_shared, stream>>>(
            output, input, input_size, gen_powers, shift, perform_shift,
            forward_twiddles);
    error = cudaGetLastError();
    if (error == cudaSuccess) {
        staged_ntt_second<N, M, ForwardNtt, false>
            <<<dim3(N / M, polynomial_count), BLOCK_SIZE, second_shared, stream>>>(
                output, N, forward_twiddles, cupqc::n_inv<N>(PRIME));
        error = cudaGetLastError();
    }
    return error;
}

__global__ void size_two_ntt(fr_t* data, const uint32_t stride, const bool inverse) {
    fr_t* polynomial = data + blockIdx.x * stride;
    const fr_t first = polynomial[0];
    const fr_t second = polynomial[1];
    polynomial[0] = first + second;
    polynomial[1] = first - second;
    if (inverse) {
        const fr_t inverse_two = fr_t::from_canonical_u32((PRIME + 1) / 2);
        polynomial[0] *= inverse_two;
        polynomial[1] *= inverse_two;
    }
}

inline cudaError_t batch_ntt(
    fr_t* data,
    const uint32_t log_size,
    const uint32_t polynomial_count,
    const uint32_t stride,
    const bool inverse,
    const cudaStream_t stream) {
    if (log_size == 0) {
        return cudaSuccess;
    }
    if (log_size == 1) {
        if (polynomial_count == 0) {
            return cudaSuccess;
        }
        size_two_ntt<<<polynomial_count, 1, 0, stream>>>(data, stride, inverse);
        return cudaGetLastError();
    }

    uint32_t* values = reinterpret_cast<uint32_t*>(data);
#define STANDARD_CASE(LOG)                                                                    \
    case LOG:                                                                                 \
        return launch_standard<(1U << LOG)>(values, polynomial_count, stride, inverse, stream)
#define STAGED_CASE(LOG, SUB_SIZE)                                                             \
    case LOG:                                                                                 \
        return launch_staged<(1U << LOG), SUB_SIZE>(                                           \
            values, polynomial_count, stride, inverse, stream)
    switch (log_size) {
        STANDARD_CASE(2);
        STANDARD_CASE(3);
        STANDARD_CASE(4);
        STANDARD_CASE(5);
        STANDARD_CASE(6);
        STANDARD_CASE(7);
        STANDARD_CASE(8);
        // cuPQC 0.6.0 omits its size-512 twiddle symbols. A size-1024 NTT gives
        // the same transform at its even evaluation points.
        case 9:
            return launch_size_512(values, polynomial_count, stride, inverse, stream);
        STANDARD_CASE(10);
        STANDARD_CASE(11);
        STANDARD_CASE(12);
        STANDARD_CASE(13);
        STAGED_CASE(14, 256);
        STAGED_CASE(15, 256);
        STAGED_CASE(16, 256);
        STAGED_CASE(17, 512);
        STAGED_CASE(18, 512);
        STAGED_CASE(19, 1024);
        STAGED_CASE(20, 1024);
        STAGED_CASE(21, 2048);
        STAGED_CASE(22, 2048);
        STAGED_CASE(23, 1024);
        STAGED_CASE(24, 4096);
        default:
            return cudaErrorInvalidValue;
    }
#undef STAGED_CASE
#undef STANDARD_CASE
}

inline cudaError_t batch_coset_ntt(
    fr_t* data,
    const fr_t* input,
    const uint32_t log_size,
    const uint32_t log_blowup,
    const uint32_t polynomial_count,
    const fr_t* gen_powers,
    const fr_t shift,
    const bool perform_shift,
    const cudaStream_t stream) {
    const uint32_t stride = 1U << log_size;
    const uint32_t input_size = stride >> log_blowup;
#define STAGED_COSET_CASE(LOG, SUB_SIZE)                                                       \
    case LOG:                                                                                 \
        return launch_staged_coset<(1U << LOG), SUB_SIZE>(                                    \
            reinterpret_cast<uint32_t*>(data), polynomial_count, input, input_size,            \
            gen_powers, shift, perform_shift, stream)
    switch (log_size) {
        STAGED_COSET_CASE(14, 256);
        STAGED_COSET_CASE(15, 256);
        STAGED_COSET_CASE(16, 256);
        STAGED_COSET_CASE(17, 512);
        STAGED_COSET_CASE(18, 512);
        STAGED_COSET_CASE(19, 1024);
        STAGED_COSET_CASE(20, 1024);
        STAGED_COSET_CASE(21, 2048);
        STAGED_COSET_CASE(22, 2048);
        STAGED_COSET_CASE(23, 1024);
        STAGED_COSET_CASE(24, 4096);
        default:
            return cudaErrorInvalidValue;
    }
#undef STAGED_COSET_CASE
}

}  // namespace nvidia_ntt