boojum-cuda 0.154.6

Boojum-CUDA is a library implementing GPU-accelerated cryptographic functionality for the zkSync prover
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
use crate::device_structures::{
    DeviceMatrixChunkImpl, DeviceMatrixChunkMutImpl, DeviceMatrixImpl, DeviceMatrixMutImpl,
    DeviceRepr, DeviceVectorChunkImpl, DeviceVectorChunkMutImpl, DeviceVectorImpl,
    DeviceVectorMutImpl, MutPtrAndStride, PtrAndStride,
};
use crate::extension_field::{ExtensionField, VectorizedExtensionField};
use crate::ops_cub::device_radix_sort::{get_sort_pairs_temp_storage_bytes, sort_pairs};
use crate::ops_cub::device_run_length_encode::{encode, get_encode_temp_storage_bytes};
use crate::ops_cub::device_scan::{get_scan_temp_storage_bytes, scan_in_place, ScanOperation};
use crate::ops_simple::{set_by_val, set_to_zero};
use crate::utils::{get_grid_block_dims_for_threads_count, WARP_SIZE};
use crate::BaseField;
use era_cudart::execution::{CudaLaunchConfig, Dim3, KernelFunction};
use era_cudart::paste::paste;
use era_cudart::result::CudaResult;
use era_cudart::slice::{DeviceSlice, DeviceVariable};
use era_cudart::stream::CudaStream;
use era_cudart::{
    cuda_kernel, cuda_kernel_declaration, cuda_kernel_signature_arguments_and_function,
};
use std::fmt::Debug;
use std::mem;

type BF = BaseField;
type EF = VectorizedExtensionField;

fn get_launch_dims(count: u32) -> (Dim3, Dim3) {
    get_grid_block_dims_for_threads_count(WARP_SIZE * 4, count)
}

cuda_kernel!(
    GetPowerOf,
    get_powers_of_kernel,
    log_degree: u32,
    offset: u32,
    inverse: bool,
    bit_reverse: bool,
    result: *mut BF,
    count: u32,
);

fn get_powers_of(
    kernel_function: GetPowerOfSignature,
    log_degree: u32,
    offset: u32,
    inverse: bool,
    bit_reverse: bool,
    result: &mut DeviceSlice<BF>,
    stream: &CudaStream,
) -> CudaResult<()> {
    assert!(result.len() <= u32::MAX as usize);
    let count = result.len() as u32;
    let (grid_dim, block_dim) = get_launch_dims(count);
    let result = result.as_mut_ptr();
    let config = CudaLaunchConfig::basic(grid_dim, block_dim, stream);
    let args = GetPowerOfArguments::new(log_degree, offset, inverse, bit_reverse, result, count);
    GetPowerOfFunction(kernel_function).launch(&config, &args)
}

macro_rules! get_powers_of_impl {
    ($x:ident) => {
        paste! {
            get_powers_of_kernel!([<get_powers_of_ $x _kernel>]);
            pub fn [<get_powers_of_ $x>](
                log_degree: u32,
                offset: u32,
                inverse: bool,
                bit_reverse: bool,
                result: &mut DeviceSlice<BF>,
                stream: &CudaStream,
            ) -> CudaResult<()> {
                get_powers_of(
                    [<get_powers_of_ $x _kernel>],
                    log_degree,
                    offset,
                    inverse,
                    bit_reverse,
                    result,
                    stream,
                )
            }

        }
    };
}

get_powers_of_impl!(w);
get_powers_of_impl!(g);

cuda_kernel_signature_arguments_and_function!(
    GetPowersByVal<T>,
    base: T,
    offset: u32,
    bit_reverse: bool,
    result: *mut T,
    count: u32,
);

macro_rules! get_powers_by_val_kernel {
    ($type:ty) => {
        paste! {
            cuda_kernel_declaration!(
                [<get_powers_by_val_ $type:lower _kernel>](
                    base: $type,
                    offset: u32,
                    bit_reverse: bool,
                    result: *mut $type,
                    count: u32,
                )
            );
        }
    };
}

pub trait GetPowersByVal: Sized {
    const KERNEL_FUNCTION: GetPowersByValSignature<Self>;
}

pub fn get_powers_by_val<T: GetPowersByVal>(
    base: T,
    offset: u32,
    bit_reverse: bool,
    result: &mut DeviceSlice<T>,
    stream: &CudaStream,
) -> CudaResult<()> {
    assert!(result.len() <= u32::MAX as usize);
    let count = result.len() as u32;
    let (grid_dim, block_dim) = get_launch_dims(count);
    let result = result.as_mut_ptr();
    let config = CudaLaunchConfig::basic(grid_dim, block_dim, stream);
    let args = GetPowersByValArguments::new(base, offset, bit_reverse, result, count);
    GetPowersByValFunction(T::KERNEL_FUNCTION).launch(&config, &args)
}

macro_rules! get_powers_by_val_impl {
    ($type:ty) => {
        paste! {
            get_powers_by_val_kernel!($type);
            impl GetPowersByVal for $type {
                const KERNEL_FUNCTION: GetPowersByValSignature<Self> = [<get_powers_by_val_ $type:lower _kernel>];
            }
        }
    };
}

get_powers_by_val_impl!(BF);
get_powers_by_val_impl!(EF);

cuda_kernel_signature_arguments_and_function!(
    GetPowersByRef<T>,
    base: *const T,
    offset: u32,
    bit_reverse: bool,
    result: *mut T,
    count: u32,
);

macro_rules! get_powers_by_ref_kernel {
    ($type:ty) => {
        paste! {
            cuda_kernel_declaration!(
                [<get_powers_by_ref_ $type:lower _kernel>](
                    base: *const $type,
                    offset: u32,
                    bit_reverse: bool,
                    result: *mut $type,
                    count: u32,
                )
            );
        }
    };
}

pub trait GetPowersByRef: Sized {
    const KERNEL_FUNCTION: GetPowersByRefSignature<Self>;
}

pub fn get_powers_by_ref<T: GetPowersByRef>(
    base: &DeviceVariable<T>,
    offset: u32,
    bit_reverse: bool,
    result: &mut DeviceSlice<T>,
    stream: &CudaStream,
) -> CudaResult<()> {
    assert!(result.len() <= u32::MAX as usize);
    let count = result.len() as u32;
    let (grid_dim, block_dim) = get_launch_dims(count);
    let base = base.as_ptr();
    let result = result.as_mut_ptr();
    let config = CudaLaunchConfig::basic(grid_dim, block_dim, stream);
    let args = GetPowersByRefArguments::new(base, offset, bit_reverse, result, count);
    GetPowersByRefFunction(T::KERNEL_FUNCTION).launch(&config, &args)
}

macro_rules! get_powers_by_ref_impl {
    ($type:ty) => {
        paste! {
            get_powers_by_ref_kernel!($type);
            impl GetPowersByRef for $type {
                const KERNEL_FUNCTION: GetPowersByRefSignature<Self> = [<get_powers_by_ref_ $type:lower _kernel>];
            }
        }
    };
}

get_powers_by_ref_impl!(BF);
get_powers_by_ref_impl!(EF);

cuda_kernel!(
    OmegaShift,
    omega_shift_kernel(
        src: *const BF,
        log_degree: u32,
        offset: u32,
        inverse: bool,
        shift: u32,
        dst: *mut BF,
        count: u32,
    )
);

fn launch_omega_shift(args: &OmegaShiftArguments, stream: &CudaStream) -> CudaResult<()> {
    let (grid_dim, block_dim) = get_launch_dims(args.count);
    let config = CudaLaunchConfig::basic(grid_dim, block_dim, stream);
    OmegaShiftFunction::default().launch(&config, args)
}

pub fn omega_shift(
    src: &DeviceSlice<BF>,
    log_degree: u32,
    offset: u32,
    inverse: bool,
    shift: u32,
    dst: &mut DeviceSlice<BF>,
    stream: &CudaStream,
) -> CudaResult<()> {
    assert_eq!(src.len(), dst.len());
    assert!(dst.len() <= u32::MAX as usize);
    let count = dst.len() as u32;
    let src = src.as_ptr();
    let dst = dst.as_mut_ptr();
    let args = OmegaShiftArguments::new(src, log_degree, offset, inverse, shift, dst, count);
    launch_omega_shift(&args, stream)
}

pub fn omega_shift_in_place(
    values: &mut DeviceSlice<BF>,
    log_degree: u32,
    offset: u32,
    inverse: bool,
    shift: u32,
    stream: &CudaStream,
) -> CudaResult<()> {
    assert!(values.len() <= u32::MAX as usize);
    let count = values.len() as u32;
    let src = values.as_ptr();
    let dst = values.as_mut_ptr();
    let args = OmegaShiftArguments::new(src, log_degree, offset, inverse, shift, dst, count);
    launch_omega_shift(&args, stream)
}

cuda_kernel!(
    BitReverse,
    bit_reverse_kernel,
    src: PtrAndStride<<BF as DeviceRepr>::Type>,
    dst: MutPtrAndStride<<BF as DeviceRepr>::Type>,
    log_count: u32,
);

bit_reverse_kernel!(bit_reverse_naive_kernel);
bit_reverse_kernel!(bit_reverse_kernel);

fn launch_bit_reverse(
    rows: usize,
    cols: usize,
    src: PtrAndStride<BF>,
    dst: MutPtrAndStride<BF>,
    stream: &CudaStream,
) -> CudaResult<()> {
    assert!(rows.is_power_of_two());
    assert!(rows <= u32::MAX as usize);
    assert!(cols <= u32::MAX as usize);
    let log_count = rows.trailing_zeros();
    let half_log_count = log_count >> 1;
    const LOG_TILE_DIM: u32 = 5;
    let args = BitReverseArguments::new(src, dst, log_count);
    let (function, config) = if half_log_count < LOG_TILE_DIM {
        let (mut grid_dim, block_dim) = get_launch_dims(1 << log_count);
        grid_dim.y = cols as u32;
        let config = CudaLaunchConfig::basic(grid_dim, block_dim, stream);
        (BitReverseFunction(bit_reverse_naive_kernel), config)
    } else {
        const TILE_DIM: u32 = 1 << LOG_TILE_DIM;
        const BLOCK_ROWS: u32 = 2;
        assert!(half_log_count >= LOG_TILE_DIM);
        let tiles_per_dim = 1 << (half_log_count - LOG_TILE_DIM);
        let grid_dim_x = tiles_per_dim * (tiles_per_dim + 1) / 2;
        let grid_dim_y = log_count - (half_log_count << 1) + 1;
        let grid_dim_z = cols as u32;
        let grid_dim = (grid_dim_x, grid_dim_y, grid_dim_z);
        let block_dim = (TILE_DIM, BLOCK_ROWS, 2);
        let config = CudaLaunchConfig::basic(grid_dim, block_dim, stream);
        (BitReverseFunction(bit_reverse_kernel), config)
    };
    function.launch(&config, &args)
}

pub fn bit_reverse(
    src: &(impl DeviceMatrixChunkImpl<BF> + ?Sized),
    dst: &mut (impl DeviceMatrixChunkMutImpl<BF> + ?Sized),
    stream: &CudaStream,
) -> CudaResult<()> {
    let rows = dst.rows();
    let cols = dst.cols();
    assert_eq!(src.rows(), rows);
    assert_eq!(src.cols(), cols);
    let src = src.as_ptr_and_stride();
    let dst = dst.as_mut_ptr_and_stride();
    launch_bit_reverse(rows, cols, src, dst, stream)
}

pub fn bit_reverse_in_place(
    values: &mut (impl DeviceMatrixChunkMutImpl<BF> + ?Sized),
    stream: &CudaStream,
) -> CudaResult<()> {
    let rows = values.rows();
    let cols = values.cols();
    let src = values.as_ptr_and_stride();
    let dst = values.as_mut_ptr_and_stride();
    launch_bit_reverse(rows, cols, src, dst, stream)
}

cuda_kernel_signature_arguments_and_function!(
    BatchInv<T: DeviceRepr>,
    src: PtrAndStride<<T as DeviceRepr>::Type>,
    dst: MutPtrAndStride<<T as DeviceRepr>::Type>,
    count: u32,
);

macro_rules! batch_inv_kernel {
    ($type:ty) => {
        paste! {
            cuda_kernel_declaration!(
                [<batch_inv_ $type:lower _kernel>](
                    src: PtrAndStride<<$type as DeviceRepr>::Type>,
                    dst: MutPtrAndStride<<$type as DeviceRepr>::Type>,
                    count: u32,
                )
            );
        }
    };
}

pub trait BatchInv: DeviceRepr {
    const BATCH_SIZE: u32;
    const KERNEL_FUNCTION: BatchInvSignature<Self>;
}

pub fn launch_batch_inv<T: BatchInv>(
    src: PtrAndStride<<T as DeviceRepr>::Type>,
    dst: MutPtrAndStride<<T as DeviceRepr>::Type>,
    count: u32,
    stream: &CudaStream,
) -> CudaResult<()> {
    let block_dim = WARP_SIZE * 4;
    let grid_dim = (count + T::BATCH_SIZE * block_dim - 1) / (T::BATCH_SIZE * block_dim);
    let config = CudaLaunchConfig::basic(grid_dim, block_dim, stream);
    let args = BatchInvArguments::<T>::new(src, dst, count);
    BatchInvFunction::<T>(T::KERNEL_FUNCTION).launch(&config, &args)
}

pub fn batch_inv<T: BatchInv>(
    src: &DeviceSlice<T>,
    dst: &mut DeviceSlice<T>,
    stream: &CudaStream,
) -> CudaResult<()> {
    assert_eq!(src.len(), dst.len());
    assert!(dst.len() <= u32::MAX as usize);
    launch_batch_inv::<T>(
        DeviceVectorImpl::as_ptr_and_stride(src),
        DeviceVectorMutImpl::as_mut_ptr_and_stride(dst),
        dst.len() as u32,
        stream,
    )
}

pub fn batch_inv_in_place<T: BatchInv>(
    values: &mut DeviceSlice<T>,
    stream: &CudaStream,
) -> CudaResult<()> {
    assert!(values.len() <= u32::MAX as usize);
    launch_batch_inv::<T>(
        DeviceVectorImpl::as_ptr_and_stride(values),
        DeviceVectorMutImpl::as_mut_ptr_and_stride(values),
        values.len() as u32,
        stream,
    )
}

macro_rules! batch_inv_impl {
    ($type:ty, $batch_size:expr) => {
        paste! {
            batch_inv_kernel!($type);
            impl BatchInv for $type {
                const BATCH_SIZE: u32 = $batch_size;
                const KERNEL_FUNCTION: BatchInvSignature<Self> = [<batch_inv_ $type:lower _kernel>];
            }
        }
    };
}

batch_inv_impl!(BF, 10);
batch_inv_impl!(EF, 6);

cuda_kernel!(
    PackVariableIndexes,
    pack_variable_indexes_kernel(src: *const u64, dst: *mut u32, count: u32)
);

pub fn pack_variable_indexes(
    src: &DeviceSlice<u64>,
    dst: &mut DeviceSlice<u32>,
    stream: &CudaStream,
) -> CudaResult<()> {
    assert_eq!(src.len(), dst.len());
    assert!(src.len() <= u32::MAX as usize);
    let count = src.len() as u32;
    let (grid_dim, block_dim) = get_launch_dims(count);
    let src = src.as_ptr();
    let dst = dst.as_mut_ptr();
    let config = CudaLaunchConfig::basic(grid_dim, block_dim, stream);
    let args = PackVariableIndexesArguments::new(src, dst, count);
    PackVariableIndexesFunction::default().launch(&config, &args)
}

cuda_kernel!(
    Select,
    select_kernel(
        indexes: *const u32,
        src: *const BF,
        dst: *mut BF,
        count: u32,
    )
);

pub fn select(
    indexes: &DeviceSlice<u32>,
    src: &DeviceSlice<BF>,
    dst: &mut DeviceSlice<BF>,
    stream: &CudaStream,
) -> CudaResult<()> {
    assert!(indexes.len() <= u32::MAX as usize);
    assert!(src.len() <= u32::MAX as usize);
    assert!(dst.len() <= u32::MAX as usize);
    let count = indexes.len() as u32;
    let (grid_dim, block_dim) = get_launch_dims(count);
    let indexes = indexes.as_ptr();
    let src = src.as_ptr();
    let dst = dst.as_mut_ptr();
    let config = CudaLaunchConfig::basic(grid_dim, block_dim, stream);
    let args = SelectArguments::new(indexes, src, dst, count);
    SelectFunction::default().launch(&config, &args)
}

cuda_kernel!(
    MarkEndsOfRuns,
    mark_ends_of_runs_kernel(
        num_runs_out: *const u32,
        run_offsets: *const u32,
        result: *mut u32,
        count: u32,
    )
);

pub fn mark_ends_of_runs(
    num_runs_out: &DeviceVariable<u32>,
    run_offsets: &DeviceSlice<u32>,
    result: &mut DeviceSlice<u32>,
    stream: &CudaStream,
) -> CudaResult<()> {
    assert!(run_offsets.len() <= u32::MAX as usize);
    assert_eq!(run_offsets.len(), result.len());
    let count = run_offsets.len() as u32;
    let (grid_dim, block_dim) = get_launch_dims(count);
    let num_runs_out = num_runs_out.as_ptr();
    let run_offsets = run_offsets.as_ptr();
    let result = result.as_mut_ptr();
    let config = CudaLaunchConfig::basic(grid_dim, block_dim, stream);
    let args = MarkEndsOfRunsArguments::new(num_runs_out, run_offsets, result, count);
    MarkEndsOfRunsFunction::default().launch(&config, &args)
}

cuda_kernel!(
    GeneratePermutationMatrix,
    generate_permutation_matrix_kernel(
        unique_variable_indexes: *const u32,
        run_indexes: *const u32,
        num_runs_out: *const u32,
        run_offsets: *const u32,
        cell_indexes: *const u32,
        scalars: *const BF,
        result: *mut BF,
        columns_count: u32,
        log_rows_count: u32,
        )
);

#[allow(clippy::too_many_arguments)]
fn generate_permutation_matrix_raw(
    unique_variable_indexes: &DeviceSlice<u32>,
    run_indexes: &DeviceSlice<u32>,
    num_runs_out: &DeviceVariable<u32>,
    run_offsets: &DeviceSlice<u32>,
    cell_indexes: &DeviceSlice<u32>,
    scalars: &DeviceSlice<BF>,
    result: &mut DeviceSlice<BF>,
    stream: &CudaStream,
) -> CudaResult<()> {
    assert!(run_indexes.len() <= u32::MAX as usize);
    assert_eq!(run_indexes.len(), run_offsets.len());
    assert_eq!(run_indexes.len(), unique_variable_indexes.len());
    assert!(cell_indexes.len() <= u32::MAX as usize);
    assert!(scalars.len() <= u32::MAX as usize);
    let columns_count = scalars.len() as u32;
    assert!(result.len() <= u32::MAX as usize);
    let count = result.len() as u32;
    let (grid_dim, block_dim) = get_launch_dims(count);
    assert_eq!(count % columns_count, 0);
    let rows_count = count / columns_count;
    assert!(rows_count.is_power_of_two());
    let log_rows_count = rows_count.ilog2();
    let unique_variable_indexes = unique_variable_indexes.as_ptr();
    let run_indexes = run_indexes.as_ptr();
    let num_runs_out = num_runs_out.as_ptr();
    let run_offsets = run_offsets.as_ptr();
    let cell_indexes = cell_indexes.as_ptr();
    let scalars = scalars.as_ptr();
    let result = result.as_mut_ptr();
    let config = CudaLaunchConfig::basic(grid_dim, block_dim, stream);
    let args = GeneratePermutationMatrixArguments::new(
        unique_variable_indexes,
        run_indexes,
        num_runs_out,
        run_offsets,
        cell_indexes,
        scalars,
        result,
        columns_count,
        log_rows_count,
    );
    GeneratePermutationMatrixFunction::default().launch(&config, &args)
}

pub fn get_generate_permutation_matrix_temp_storage_bytes(num_cells: usize) -> CudaResult<usize> {
    let cell_size = mem::size_of::<u32>();
    let num_bytes = num_cells * cell_size;
    let sort_pairs_tsb =
        get_sort_pairs_temp_storage_bytes::<u32, u32>(false, num_cells as u32, 0, 32)?;
    assert!(sort_pairs_tsb <= 3 * num_bytes + cell_size);
    let encode_tsb = get_encode_temp_storage_bytes::<u32>(num_cells as i32)?;
    assert!(encode_tsb <= num_bytes);
    let scan_tsb =
        get_scan_temp_storage_bytes::<u32>(ScanOperation::Sum, false, false, num_cells as i32)?;
    assert!(scan_tsb <= 2 * num_bytes);
    Ok(4 * num_bytes + cell_size)
}

pub fn generate_permutation_matrix(
    temp_storage: &mut DeviceSlice<u8>,
    variable_indexes: &DeviceSlice<u32>,
    scalars: &DeviceSlice<BF>,
    result: &mut DeviceSlice<BF>,
    stream: &CudaStream,
) -> CudaResult<()> {
    let num_cells = variable_indexes.len();
    let cell_size = mem::size_of::<u32>();
    let num_bytes = num_cells * cell_size;
    assert_eq!(result.len(), num_cells);
    assert_eq!(mem::size_of::<BF>(), 2 * cell_size);
    let result_as_u32 = unsafe { result.transmute_mut() };
    let (sorted_variable_indexes, unsorted_cell_indexes) = result_as_u32.split_at_mut(num_cells);
    set_by_val(1u32, unsorted_cell_indexes, stream)?;
    scan_in_place(
        ScanOperation::Sum,
        false,
        false,
        temp_storage,
        unsorted_cell_indexes,
        stream,
    )?;
    let (sorted_cell_indexes, temp_storage) = temp_storage.split_at_mut(num_bytes);
    let sorted_cell_indexes = unsafe { sorted_cell_indexes.transmute_mut() };
    sort_pairs(
        false,
        temp_storage,
        variable_indexes,
        sorted_variable_indexes,
        unsorted_cell_indexes,
        sorted_cell_indexes,
        0,
        32,
        stream,
    )?;
    let (unique_variable_indexes, temp_storage) = temp_storage.split_at_mut(num_bytes);
    let unique_variable_indexes = unsafe { unique_variable_indexes.transmute_mut() };
    let (run_lengths, temp_storage) = temp_storage.split_at_mut(num_bytes);
    let run_lengths = unsafe { run_lengths.transmute_mut() };
    let (temp_storage, num_runs_out) = temp_storage.split_at_mut(num_bytes);
    let num_runs_out = unsafe { &mut num_runs_out.transmute_mut()[0] };
    encode(
        temp_storage,
        sorted_variable_indexes,
        unique_variable_indexes,
        run_lengths,
        num_runs_out,
        stream,
    )?;
    let run_offsets = run_lengths;
    let run_indexes = unsafe { temp_storage.transmute_mut() };
    let temp_storage = unsafe { result.transmute_mut() };
    scan_in_place(
        ScanOperation::Sum,
        false,
        false,
        temp_storage,
        run_offsets,
        stream,
    )?;
    set_to_zero(run_indexes, stream)?;
    mark_ends_of_runs(num_runs_out, run_offsets, run_indexes, stream)?;
    scan_in_place(
        ScanOperation::Sum,
        false,
        false,
        temp_storage,
        run_indexes,
        stream,
    )?;
    generate_permutation_matrix_raw(
        unique_variable_indexes,
        run_indexes,
        num_runs_out,
        run_offsets,
        sorted_cell_indexes,
        scalars,
        result,
        stream,
    )
}

cuda_kernel!(
    SetValuesFromPacketBits,
    set_values_from_packed_bits_kernel(packed_bits: *const u32, result: *mut BF, count: u32,)
);

pub fn set_values_from_packed_bits(
    packed_bits: &DeviceSlice<u32>,
    result: &mut DeviceSlice<BF>,
    stream: &CudaStream,
) -> CudaResult<()> {
    assert!(packed_bits.len() <= u32::MAX as usize);
    assert!(result.len() <= u32::MAX as usize);
    let words_count = packed_bits.len() as u32;
    let count = result.len() as u32;
    let (grid_dim, block_dim) = get_launch_dims(count);
    assert!(words_count * 32 >= count);
    assert!((words_count - 1) * 32 < count);
    let packed_bits = packed_bits.as_ptr();
    let result = result.as_mut_ptr();
    let config = CudaLaunchConfig::basic(grid_dim, block_dim, stream);
    let args = SetValuesFromPacketBitsArguments::new(packed_bits, result, count);
    SetValuesFromPacketBitsFunction::default().launch(&config, &args)
}

cuda_kernel!(
    Fold,
    fold_kernel(
        coset_inverse: BF,
        challenge: *const <ExtensionField as DeviceRepr>::Type,
        root_offset: u32,
        src: PtrAndStride<<EF as DeviceRepr>::Type>,
        dst: MutPtrAndStride<<EF as DeviceRepr>::Type>,
        count: u32,
    )
);

pub fn fold<S, D>(
    coset_inverse: BF,
    challenge: &DeviceVariable<ExtensionField>,
    root_offset: usize,
    src: &S,
    dst: &mut D,
    stream: &CudaStream,
) -> CudaResult<()>
where
    S: DeviceVectorChunkImpl<EF> + Debug + ?Sized,
    D: DeviceVectorChunkMutImpl<EF> + Debug + ?Sized,
{
    assert!(root_offset < 1 << 32);
    let root_offset = root_offset as u32;
    assert!(src.rows().is_power_of_two());
    assert!(dst.rows().is_power_of_two());
    let log_count = dst.rows().trailing_zeros();
    assert_eq!(src.rows().trailing_zeros(), log_count + 1);
    assert!(log_count < 32);
    let (grid_dim, block_dim) = get_launch_dims(1 << log_count);
    let challenge = challenge.as_ptr() as *const <ExtensionField as DeviceRepr>::Type;
    let src = src.as_ptr_and_stride();
    let dst = dst.as_mut_ptr_and_stride();
    let config = CudaLaunchConfig::basic(grid_dim, block_dim, stream);
    let args = FoldArguments::new(coset_inverse, challenge, root_offset, src, dst, log_count);
    FoldFunction::default().launch(&config, &args)
}

cuda_kernel!(
    PartialProductsOfFGCHunk,
    partial_products_f_g_chunk_kernel(
        num: MutPtrAndStride<<EF as DeviceRepr>::Type>,
        denom: MutPtrAndStride<<EF as DeviceRepr>::Type>,
        variable_cols_chunk: PtrAndStride<BF>,
        sigma_cols_chunk: PtrAndStride<BF>,
        omega_values: PtrAndStride<BF>,
        non_residues_by_beta_chunk: *const <ExtensionField as DeviceRepr>::Type,
        beta_c0: *const BF,
        beta_c1: *const BF,
        gamma_c0: *const BF,
        gamma_c1: *const BF,
        num_cols_this_chunk: u32,
        count: u32,
    )
);

#[allow(clippy::too_many_arguments)]
pub fn partial_products_f_g_chunk(
    num: &mut (impl DeviceVectorMutImpl<EF> + ?Sized),
    denom: &mut (impl DeviceVectorMutImpl<EF> + ?Sized),
    variable_cols_chunk: &(impl DeviceMatrixImpl<BF> + ?Sized),
    sigma_cols_chunk: &(impl DeviceMatrixImpl<BF> + ?Sized),
    omega_values: &(impl DeviceVectorImpl<BF> + ?Sized),
    non_residues_by_beta_chunk: &(impl DeviceVectorImpl<ExtensionField> + ?Sized),
    beta_c0: &DeviceVariable<BF>,
    beta_c1: &DeviceVariable<BF>,
    gamma_c0: &DeviceVariable<BF>,
    gamma_c1: &DeviceVariable<BF>,
    num_cols_per_product: usize,
    stream: &CudaStream,
) -> CudaResult<()> {
    let num_cols = variable_cols_chunk.cols();
    assert!(num_cols <= u32::MAX as usize); // a silly check, but why not
    assert!(num_cols <= num_cols_per_product);
    let count = variable_cols_chunk.stride();
    assert!(count <= u32::MAX as usize);
    assert_eq!(num.slice().len(), count);
    assert_eq!(denom.slice().len(), count);
    assert_eq!(sigma_cols_chunk.cols(), num_cols);
    assert_eq!(sigma_cols_chunk.stride(), count);
    assert_eq!(omega_values.slice().len(), count);
    assert_eq!(non_residues_by_beta_chunk.slice().len(), num_cols);
    let num = num.as_mut_ptr_and_stride();
    let denom = denom.as_mut_ptr_and_stride();
    let variable_cols_chunk = variable_cols_chunk.as_ptr_and_stride();
    let sigma_cols_chunk = sigma_cols_chunk.as_ptr_and_stride();
    let omega_values = omega_values.as_ptr_and_stride();
    let non_residues_by_beta_chunk = non_residues_by_beta_chunk.as_ptr();
    let beta_c0 = beta_c0.as_ptr();
    let beta_c1 = beta_c1.as_ptr();
    let gamma_c0 = gamma_c0.as_ptr();
    let gamma_c1 = gamma_c1.as_ptr();
    let num_cols = num_cols as u32;
    let count = count as u32;
    let (grid_dim, block_dim) = get_launch_dims(count);
    let config = CudaLaunchConfig::basic(grid_dim, block_dim, stream);
    let args = PartialProductsOfFGCHunkArguments::new(
        num,
        denom,
        variable_cols_chunk,
        sigma_cols_chunk,
        omega_values,
        non_residues_by_beta_chunk,
        beta_c0,
        beta_c1,
        gamma_c0,
        gamma_c1,
        num_cols,
        count,
    );
    PartialProductsOfFGCHunkFunction::default().launch(&config, &args)
}

cuda_kernel!(
    PartialProductsQuotientTerms,
    partial_products_quotient_terms_kernel(
        partial_products: PtrAndStride<<EF as DeviceRepr>::Type>,
        z_poly: PtrAndStride<<EF as DeviceRepr>::Type>,
        variable_cols: PtrAndStride<BF>,
        sigma_cols: PtrAndStride<BF>,
        omega_values: PtrAndStride<BF>,
        powers_of_alpha: *const <ExtensionField as DeviceRepr>::Type,
        non_residues_by_beta: *const <ExtensionField as DeviceRepr>::Type,
        beta_c0: *const BF,
        beta_c1: *const BF,
        gamma_c0: *const BF,
        gamma_c1: *const BF,
        quotient: MutPtrAndStride<<EF as DeviceRepr>::Type>,
        num_cols: u32,
        num_cols_per_product: u32,
        count: u32,
    )
);

#[allow(clippy::too_many_arguments)]
pub fn partial_products_quotient_terms(
    partial_products: &(impl DeviceMatrixImpl<EF> + ?Sized),
    z_poly: &(impl DeviceVectorImpl<EF> + ?Sized),
    variable_cols: &(impl DeviceMatrixImpl<BF> + ?Sized),
    sigma_cols: &(impl DeviceMatrixImpl<BF> + ?Sized),
    omega_values: &(impl DeviceVectorImpl<BF> + ?Sized),
    powers_of_alpha: &(impl DeviceVectorImpl<ExtensionField> + ?Sized),
    non_residues_by_beta: &(impl DeviceVectorImpl<ExtensionField> + ?Sized),
    beta_c0: &DeviceVariable<BF>,
    beta_c1: &DeviceVariable<BF>,
    gamma_c0: &DeviceVariable<BF>,
    gamma_c1: &DeviceVariable<BF>,
    quotient: &mut (impl DeviceVectorMutImpl<EF> + ?Sized),
    num_cols_per_product: usize,
    stream: &CudaStream,
) -> CudaResult<()> {
    let num_cols = variable_cols.cols();
    assert!(num_cols <= u32::MAX as usize);
    let count = variable_cols.stride();
    assert!(count <= u32::MAX as usize);
    assert_eq!(count.count_ones(), 1);
    let expected_num_partial_products =
        ((num_cols + num_cols_per_product - 1) / num_cols_per_product) - 1;
    // Handling empty partial products would require special-case logic.
    // For now we don't need it. Assert as a reminder.
    assert!(partial_products.cols() > 0);
    assert_eq!(partial_products.cols(), expected_num_partial_products);
    assert_eq!(partial_products.stride(), count);
    assert_eq!(z_poly.slice().len(), count);
    assert_eq!(sigma_cols.cols(), num_cols);
    assert_eq!(sigma_cols.stride(), count);
    assert_eq!(omega_values.slice().len(), count);
    assert_eq!(
        powers_of_alpha.slice().len(),
        expected_num_partial_products + 1
    );
    assert_eq!(non_residues_by_beta.slice().len(), num_cols);
    assert_eq!(quotient.slice().len(), count);
    let partial_products = partial_products.as_ptr_and_stride();
    let z_poly = z_poly.as_ptr_and_stride();
    let variable_cols = variable_cols.as_ptr_and_stride();
    let sigma_cols = sigma_cols.as_ptr_and_stride();
    let omega_values = omega_values.as_ptr_and_stride();
    let powers_of_alpha = powers_of_alpha.as_ptr();
    let non_residues_by_beta = non_residues_by_beta.as_ptr();
    let beta_c0 = beta_c0.as_ptr();
    let beta_c1 = beta_c1.as_ptr();
    let gamma_c0 = gamma_c0.as_ptr();
    let gamma_c1 = gamma_c1.as_ptr();
    let quotient = quotient.as_mut_ptr_and_stride();
    let num_cols = num_cols as u32;
    let num_cols_per_product = num_cols_per_product as u32;
    let log_count: u32 = count.trailing_zeros();
    let (grid_dim, block_dim) = get_launch_dims(count as u32);
    let config = CudaLaunchConfig::basic(grid_dim, block_dim, stream);
    let args = PartialProductsQuotientTermsArguments::new(
        partial_products,
        z_poly,
        variable_cols,
        sigma_cols,
        omega_values,
        powers_of_alpha,
        non_residues_by_beta,
        beta_c0,
        beta_c1,
        gamma_c0,
        gamma_c1,
        quotient,
        num_cols,
        num_cols_per_product,
        log_count,
    );
    PartialProductsQuotientTermsFunction::default().launch(&config, &args)
}

cuda_kernel!(
    LookupAggregatedTableValues,
    lookup_aggregated_table_values_kernel(
        table_cols: PtrAndStride<BF>,
        beta_c0: *const BF,
        beta_c1: *const BF,
        powers_of_gamma: *const <ExtensionField as DeviceRepr>::Type,
        aggregated_table_values: MutPtrAndStride<<EF as DeviceRepr>::Type>,
        num_cols: u32,
        count: u32,
    )
);

pub fn lookup_aggregated_table_values(
    table_cols: &(impl DeviceMatrixImpl<BF> + ?Sized),
    beta_c0: &DeviceVariable<BF>,
    beta_c1: &DeviceVariable<BF>,
    powers_of_gamma: &(impl DeviceVectorImpl<ExtensionField> + ?Sized),
    aggregated_table_values: &mut (impl DeviceVectorMutImpl<EF> + ?Sized),
    num_cols: usize,
    stream: &CudaStream,
) -> CudaResult<()> {
    assert!(num_cols <= u32::MAX as usize);
    let count = table_cols.stride();
    assert!(count <= u32::MAX as usize);
    assert_eq!(table_cols.cols(), num_cols);
    assert_eq!(table_cols.stride(), count);
    assert_eq!(powers_of_gamma.slice().len(), num_cols);
    assert_eq!(aggregated_table_values.slice().len(), count);
    let table_cols = table_cols.as_ptr_and_stride();
    let beta_c0 = beta_c0.as_ptr();
    let beta_c1 = beta_c1.as_ptr();
    let powers_of_gamma = powers_of_gamma.as_ptr();
    let aggregated_table_values = aggregated_table_values.as_mut_ptr_and_stride();
    let num_cols = num_cols as u32;
    let count = count as u32;
    let (grid_dim, block_dim) = get_launch_dims(count);
    let config = CudaLaunchConfig::basic(grid_dim, block_dim, stream);
    let args = LookupAggregatedTableValuesArguments::new(
        table_cols,
        beta_c0,
        beta_c1,
        powers_of_gamma,
        aggregated_table_values,
        num_cols,
        count,
    );
    LookupAggregatedTableValuesFunction::default().launch(&config, &args)
}

cuda_kernel!(
    LookupSubargsAAndB,
    lookup_subargs_a_and_b_kernel(
        variable_cols: PtrAndStride<BF>,
        subargs_a: MutPtrAndStride<<EF as DeviceRepr>::Type>,
        subargs_b: MutPtrAndStride<<EF as DeviceRepr>::Type>,
        beta_c0: *const BF,
        beta_c1: *const BF,
        powers_of_gamma: *const <ExtensionField as DeviceRepr>::Type,
        table_id_col: PtrAndStride<BF>,
        aggregated_table_values_inv: PtrAndStride<<EF as DeviceRepr>::Type>,
        multiplicity_cols: PtrAndStride<BF>,
        num_subargs_a: u32,
        num_subargs_b: u32,
        num_cols_per_subarg: u32,
        count: u32,
    )
);

#[allow(clippy::too_many_arguments)]
pub fn lookup_subargs_a_and_b(
    variable_cols: &(impl DeviceMatrixImpl<BF> + ?Sized),
    subargs_a: &mut (impl DeviceMatrixMutImpl<EF> + ?Sized),
    subargs_b: &mut (impl DeviceMatrixMutImpl<EF> + ?Sized),
    beta_c0: &DeviceVariable<BF>,
    beta_c1: &DeviceVariable<BF>,
    powers_of_gamma: &(impl DeviceVectorImpl<ExtensionField> + ?Sized),
    table_id_col: &(impl DeviceVectorImpl<BF> + ?Sized),
    aggregated_table_values_inv: &(impl DeviceVectorImpl<EF> + ?Sized),
    multiplicity_cols: &(impl DeviceMatrixImpl<BF> + ?Sized),
    num_cols_per_subarg: usize,
    stream: &CudaStream,
) -> CudaResult<()> {
    let num_cols = variable_cols.cols();
    assert!(num_cols <= u32::MAX as usize);
    let count = variable_cols.stride();
    assert!(count <= u32::MAX as usize);
    let num_subargs_a = num_cols / num_cols_per_subarg;
    // num_cols should be an even multiple of num_cols_per_subarg
    assert_eq!(num_subargs_a * num_cols_per_subarg, num_cols);
    let num_subargs_b = subargs_b.cols();
    assert_eq!(num_subargs_b, 1);
    assert_eq!(subargs_a.cols(), num_subargs_a);
    assert_eq!(subargs_a.stride(), count);
    assert_eq!(subargs_b.stride(), count);
    assert_eq!(powers_of_gamma.slice().len(), num_cols_per_subarg + 1);
    assert_eq!(table_id_col.slice().len(), count);
    assert_eq!(aggregated_table_values_inv.slice().len(), count);
    assert_eq!(multiplicity_cols.cols(), num_subargs_b);
    assert_eq!(multiplicity_cols.stride(), count);
    let variable_cols = variable_cols.as_ptr_and_stride();
    let subargs_a = subargs_a.as_mut_ptr_and_stride();
    let subargs_b = subargs_b.as_mut_ptr_and_stride();
    let beta_c0 = beta_c0.as_ptr();
    let beta_c1 = beta_c1.as_ptr();
    let powers_of_gamma = powers_of_gamma.as_ptr();
    let table_id_col = table_id_col.as_ptr_and_stride();
    let aggregated_table_values_inv = aggregated_table_values_inv.as_ptr_and_stride();
    let multiplicity_cols = multiplicity_cols.as_ptr_and_stride();
    let num_subargs_a = num_subargs_a as u32;
    let num_subargs_b = num_subargs_b as u32;
    let num_cols_per_subarg = num_cols_per_subarg as u32;
    let count = count as u32;
    let (grid_dim, block_dim) = get_launch_dims(count);
    let config = CudaLaunchConfig::basic(grid_dim, block_dim, stream);
    let args = LookupSubargsAAndBArguments::new(
        variable_cols,
        subargs_a,
        subargs_b,
        beta_c0,
        beta_c1,
        powers_of_gamma,
        table_id_col,
        aggregated_table_values_inv,
        multiplicity_cols,
        num_subargs_a,
        num_subargs_b,
        num_cols_per_subarg,
        count,
    );
    LookupSubargsAAndBFunction::default().launch(&config, &args)
}

cuda_kernel!(
    LookupQuotientAAndB,
    lookup_quotient_a_and_b_kernel(
        variable_cols: PtrAndStride<BF>,
        table_cols: PtrAndStride<BF>,
        subargs_a: PtrAndStride<<EF as DeviceRepr>::Type>,
        subargs_b: PtrAndStride<<EF as DeviceRepr>::Type>,
        beta_c0: *const BF,
        beta_c1: *const BF,
        powers_of_gamma: *const <ExtensionField as DeviceRepr>::Type,
        powers_of_alpha: *const <ExtensionField as DeviceRepr>::Type,
        table_id_col: PtrAndStride<BF>,
        multiplicity_cols: PtrAndStride<BF>,
        quotient: MutPtrAndStride<<EF as DeviceRepr>::Type>,
        num_subargs_a: u32,
        num_subargs_b: u32,
        num_cols_per_subarg: u32,
        count: u32,
    )
);

#[allow(clippy::too_many_arguments)]
pub fn lookup_quotient_a_and_b(
    variable_cols: &(impl DeviceMatrixImpl<BF> + ?Sized),
    table_cols: &(impl DeviceMatrixImpl<BF> + ?Sized),
    subargs_a: &(impl DeviceMatrixImpl<EF> + ?Sized),
    subargs_b: &(impl DeviceMatrixImpl<EF> + ?Sized),
    beta_c0: &DeviceVariable<BF>,
    beta_c1: &DeviceVariable<BF>,
    powers_of_gamma: &(impl DeviceVectorImpl<ExtensionField> + ?Sized),
    powers_of_alpha: &(impl DeviceVectorImpl<ExtensionField> + ?Sized),
    table_id_col: &(impl DeviceVectorImpl<BF> + ?Sized),
    multiplicity_cols: &(impl DeviceMatrixImpl<BF> + ?Sized),
    quotient: &mut (impl DeviceVectorMutImpl<EF> + ?Sized),
    num_cols_per_subarg: usize,
    stream: &CudaStream,
) -> CudaResult<()> {
    let num_cols = variable_cols.cols();
    assert!(num_cols <= u32::MAX as usize);
    let count = variable_cols.stride();
    assert!(count <= u32::MAX as usize);
    let num_subargs_a = num_cols / num_cols_per_subarg;
    // num_cols should be an even multiple of num_cols_per_subarg
    assert_eq!(num_subargs_a * num_cols_per_subarg, num_cols);
    let num_subargs_b = subargs_b.cols();
    assert_eq!(num_subargs_b, 1);
    assert_eq!(table_cols.cols(), num_cols_per_subarg + 1);
    assert_eq!(table_cols.stride(), count);
    assert_eq!(subargs_a.cols(), num_subargs_a);
    assert_eq!(subargs_a.stride(), count);
    assert_eq!(subargs_b.stride(), count);
    assert_eq!(powers_of_gamma.slice().len(), num_cols_per_subarg + 1);
    assert_eq!(powers_of_alpha.slice().len(), num_subargs_a + num_subargs_b);
    assert_eq!(table_id_col.slice().len(), count);
    assert_eq!(multiplicity_cols.cols(), num_subargs_b);
    assert_eq!(multiplicity_cols.stride(), count);
    assert_eq!(quotient.slice().len(), count);
    let variable_cols = variable_cols.as_ptr_and_stride();
    let table_cols = table_cols.as_ptr_and_stride();
    let subargs_a = subargs_a.as_ptr_and_stride();
    let subargs_b = subargs_b.as_ptr_and_stride();
    let beta_c0 = beta_c0.as_ptr();
    let beta_c1 = beta_c1.as_ptr();
    let powers_of_gamma = powers_of_gamma.as_ptr();
    let powers_of_alpha = powers_of_alpha.as_ptr();
    let table_id_col = table_id_col.as_ptr_and_stride();
    let multiplicity_cols = multiplicity_cols.as_ptr_and_stride();
    let quotient = quotient.as_mut_ptr_and_stride();
    let num_subargs_a = num_subargs_a as u32;
    let num_subargs_b = num_subargs_b as u32;
    let num_cols_per_subarg = num_cols_per_subarg as u32;
    let count = count as u32;
    let (grid_dim, block_dim) = get_launch_dims(count);
    let config = CudaLaunchConfig::basic(grid_dim, block_dim, stream);
    let args = LookupQuotientAAndBArguments::new(
        variable_cols,
        table_cols,
        subargs_a,
        subargs_b,
        beta_c0,
        beta_c1,
        powers_of_gamma,
        powers_of_alpha,
        table_id_col,
        multiplicity_cols,
        quotient,
        num_subargs_a,
        num_subargs_b,
        num_cols_per_subarg,
        count,
    );
    LookupQuotientAAndBFunction::default().launch(&config, &args)
}

cuda_kernel!(
    DeepQuotientExceptPublicInputs,
    deep_quotient_except_public_inputs_kernel(
        variable_cols: PtrAndStride<BF>,
        witness_cols: PtrAndStride<BF>,
        constant_cols: PtrAndStride<BF>,
        permutation_cols: PtrAndStride<BF>,
        z_poly: PtrAndStride<<EF as DeviceRepr>::Type>,
        partial_products: PtrAndStride<<EF as DeviceRepr>::Type>,
        multiplicity_cols: PtrAndStride<BF>,
        lookup_a_polys: PtrAndStride<<EF as DeviceRepr>::Type>,
        lookup_b_polys: PtrAndStride<<EF as DeviceRepr>::Type>,
        table_cols: PtrAndStride<BF>,
        quotient_constraint_polys: PtrAndStride<<EF as DeviceRepr>::Type>,
        evaluations_at_z: *const <ExtensionField as DeviceRepr>::Type,
        evaluations_at_z_omega: *const <ExtensionField as DeviceRepr>::Type,
        evaluations_at_zero: *const <ExtensionField as DeviceRepr>::Type,
        challenges: *const <ExtensionField as DeviceRepr>::Type,
        denom_at_z: PtrAndStride<<EF as DeviceRepr>::Type>,
        denom_at_z_omega: PtrAndStride<<EF as DeviceRepr>::Type>,
        denom_at_zero: PtrAndStride<BF>,
        quotient: MutPtrAndStride<<EF as DeviceRepr>::Type>,
        num_variable_cols: u32,
        num_witness_cols: u32,
        num_constants_cols: u32,
        num_permutation_cols: u32,
        num_partial_products: u32,
        num_multiplicity_cols: u32,
        num_lookup_a_polys: u32,
        num_lookup_b_polys: u32,
        num_table_cols: u32,
        num_quotient_constraint_polys: u32,
        z_omega_challenge_offset: u32,
        zero_challenge_offset: u32,
        count: u32,
    )
);

#[allow(clippy::too_many_arguments)]
pub fn deep_quotient_except_public_inputs(
    variable_cols: &(impl DeviceMatrixImpl<BF> + ?Sized),
    witness_cols: &(impl DeviceMatrixImpl<BF> + ?Sized),
    constant_cols: &(impl DeviceMatrixImpl<BF> + ?Sized),
    permutation_cols: &(impl DeviceMatrixImpl<BF> + ?Sized),
    z_poly: &(impl DeviceVectorImpl<EF> + ?Sized),
    partial_products: &(impl DeviceMatrixImpl<EF> + ?Sized),
    multiplicity_cols: &(impl DeviceMatrixImpl<BF> + ?Sized),
    lookup_a_polys: &(impl DeviceMatrixImpl<EF> + ?Sized),
    lookup_b_polys: &(impl DeviceMatrixImpl<EF> + ?Sized),
    table_cols: &(impl DeviceMatrixImpl<BF> + ?Sized),
    quotient_constraint_polys: &(impl DeviceMatrixImpl<EF> + ?Sized),
    evaluations_at_z: &(impl DeviceVectorImpl<ExtensionField> + ?Sized),
    evaluations_at_z_omega: &(impl DeviceVectorImpl<ExtensionField> + ?Sized),
    evaluations_at_zero: &(impl DeviceVectorImpl<ExtensionField> + ?Sized),
    challenges: &(impl DeviceVectorImpl<ExtensionField> + ?Sized),
    denom_at_z: &(impl DeviceVectorImpl<EF> + ?Sized),
    denom_at_z_omega: &(impl DeviceVectorImpl<EF> + ?Sized),
    denom_at_zero: &(impl DeviceVectorImpl<BF> + ?Sized),
    quotient: &mut (impl DeviceVectorMutImpl<EF> + ?Sized),
    stream: &CudaStream,
) -> CudaResult<()> {
    let count = variable_cols.stride();
    assert!(count <= u32::MAX as usize);
    assert_eq!(witness_cols.stride(), count);
    assert_eq!(constant_cols.stride(), count);
    assert_eq!(permutation_cols.stride(), count);
    assert_eq!(z_poly.slice().len(), count);
    assert_eq!(partial_products.stride(), count);
    assert_eq!(multiplicity_cols.stride(), count);
    assert_eq!(lookup_a_polys.stride(), count);
    assert_eq!(lookup_b_polys.stride(), count);
    assert_eq!(table_cols.stride(), count);
    assert_eq!(quotient_constraint_polys.stride(), count);
    assert_eq!(evaluations_at_z_omega.slice().len(), 1);
    assert_eq!(
        evaluations_at_zero.slice().len(),
        lookup_a_polys.cols() + lookup_b_polys.cols()
    );
    assert_eq!(denom_at_z.slice().len(), count);
    assert_eq!(denom_at_z_omega.slice().len(), count);
    assert_eq!(quotient.slice().len(), count);
    let mut num_terms_at_z = 0;
    num_terms_at_z +=
        variable_cols.cols() + witness_cols.cols() + constant_cols.cols() + permutation_cols.cols();
    num_terms_at_z += 1; // z_poly
    num_terms_at_z += partial_products.cols();
    num_terms_at_z += multiplicity_cols.cols();
    num_terms_at_z += lookup_a_polys.cols();
    num_terms_at_z += lookup_b_polys.cols();
    num_terms_at_z += table_cols.cols();
    num_terms_at_z += quotient_constraint_polys.cols();
    assert_eq!(evaluations_at_z.slice().len(), num_terms_at_z);
    assert_eq!(evaluations_at_z_omega.slice().len(), 1);
    assert_eq!(
        evaluations_at_zero.slice().len(),
        lookup_a_polys.cols() + lookup_b_polys.cols()
    );
    let mut num_terms_from_evals = 0;
    num_terms_from_evals += evaluations_at_z.slice().len();
    num_terms_from_evals += evaluations_at_z_omega.slice().len();
    num_terms_from_evals += evaluations_at_zero.slice().len();
    assert_eq!(challenges.slice().len(), num_terms_from_evals);
    let num_variable_cols = variable_cols.cols() as u32;
    let num_witness_cols = witness_cols.cols() as u32;
    let num_constant_cols = constant_cols.cols() as u32;
    let num_permutation_cols = permutation_cols.cols() as u32;
    let num_partial_products = partial_products.cols() as u32;
    let num_multiplicity_cols = multiplicity_cols.cols() as u32;
    if num_multiplicity_cols > 0 {
        assert_eq!(denom_at_zero.slice().len(), count)
    } else {
        assert_eq!(denom_at_zero.slice().len(), 0)
    }
    let num_lookup_a_polys = lookup_a_polys.cols() as u32;
    let num_lookup_b_polys = lookup_b_polys.cols() as u32;
    let num_table_cols = table_cols.cols() as u32;
    let num_quotient_constraint_polys = quotient_constraint_polys.cols() as u32;
    let variable_cols = variable_cols.as_ptr_and_stride();
    let witness_cols = witness_cols.as_ptr_and_stride();
    let constant_cols = constant_cols.as_ptr_and_stride();
    let permutation_cols = permutation_cols.as_ptr_and_stride();
    let z_poly = z_poly.as_ptr_and_stride();
    let partial_products = partial_products.as_ptr_and_stride();
    let multiplicity_cols = multiplicity_cols.as_ptr_and_stride();
    let lookup_a_polys = lookup_a_polys.as_ptr_and_stride();
    let lookup_b_polys = lookup_b_polys.as_ptr_and_stride();
    let table_cols = table_cols.as_ptr_and_stride();
    let quotient_constraint_polys = quotient_constraint_polys.as_ptr_and_stride();
    let evaluations_at_z = evaluations_at_z.as_ptr();
    let evaluations_at_z_omega = evaluations_at_z_omega.as_ptr();
    let evaluations_at_zero = evaluations_at_zero.as_ptr();
    let challenges = challenges.as_ptr();
    let denom_at_z = denom_at_z.as_ptr_and_stride();
    let denom_at_z_omega = denom_at_z_omega.as_ptr_and_stride();
    let denom_at_zero = denom_at_zero.as_ptr_and_stride();
    let quotient = quotient.as_mut_ptr_and_stride();
    let z_omega_challenge_offset = 1
        + num_partial_products
        + num_multiplicity_cols
        + num_lookup_a_polys
        + num_lookup_b_polys
        + num_table_cols
        + num_quotient_constraint_polys;
    let zero_challenge_offset = num_lookup_a_polys
        + num_lookup_b_polys
        + num_table_cols
        + num_quotient_constraint_polys
        + 1;
    let count = count as u32;
    let (grid_dim, block_dim) = get_launch_dims(count);
    let config = CudaLaunchConfig::basic(grid_dim, block_dim, stream);
    let args = DeepQuotientExceptPublicInputsArguments::new(
        variable_cols,
        witness_cols,
        constant_cols,
        permutation_cols,
        z_poly,
        partial_products,
        multiplicity_cols,
        lookup_a_polys,
        lookup_b_polys,
        table_cols,
        quotient_constraint_polys,
        evaluations_at_z,
        evaluations_at_z_omega,
        evaluations_at_zero,
        challenges,
        denom_at_z,
        denom_at_z_omega,
        denom_at_zero,
        quotient,
        num_variable_cols,
        num_witness_cols,
        num_constant_cols,
        num_permutation_cols,
        num_partial_products,
        num_multiplicity_cols,
        num_lookup_a_polys,
        num_lookup_b_polys,
        num_table_cols,
        num_quotient_constraint_polys,
        z_omega_challenge_offset,
        zero_challenge_offset,
        count,
    );
    DeepQuotientExceptPublicInputsFunction::default().launch(&config, &args)
}

cuda_kernel!(
    DeepQuotientPublicInput,
    deep_quotient_public_input_kernel(
        values: PtrAndStride<BF>,
        expected_value: BF,
        challenge: *const <ExtensionField as DeviceRepr>::Type,
        quotient: MutPtrAndStride<<EF as DeviceRepr>::Type>,
        count: u32,
    )
);

pub fn deep_quotient_public_input(
    values: &(impl DeviceVectorImpl<BF> + ?Sized),
    expected_value: BF,
    challenge: &(impl DeviceVectorImpl<ExtensionField> + ?Sized),
    quotient: &mut (impl DeviceVectorMutImpl<EF> + ?Sized),
    stream: &CudaStream,
) -> CudaResult<()> {
    let count = values.slice().len();
    assert!(count <= u32::MAX as usize);
    assert_eq!(values.slice().len(), count);
    assert_eq!(challenge.slice().len(), 1);
    assert_eq!(quotient.slice().len(), count);
    let values = values.as_ptr_and_stride();
    let challenge = challenge.as_ptr();
    let quotient = quotient.as_mut_ptr_and_stride();
    let count = count as u32;
    let (grid_dim, block_dim) = get_launch_dims(count);
    let config = CudaLaunchConfig::basic(grid_dim, block_dim, stream);
    let args =
        DeepQuotientPublicInputArguments::new(values, expected_value, challenge, quotient, count);
    DeepQuotientPublicInputFunction::default().launch(&config, &args)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::context::{Context, OMEGA_LOG_ORDER};
    use crate::device_structures::{
        DeviceMatrix, DeviceMatrixMut, DeviceVectorChunk, DeviceVectorChunkMut,
    };
    use crate::extension_field::test_helpers::{transmute_gf_vec, ExtensionFieldTest};
    use crate::extension_field::{convert, ExtensionField};
    use crate::ops_cub::device_run_length_encode::{encode, get_encode_temp_storage_bytes};
    use crate::ops_cub::device_scan::{get_scan_temp_storage_bytes, scan_in_place, ScanOperation};
    use crate::ops_simple::set_to_zero;
    use boojum::cs::implementations::utils::{
        domain_generator_for_size, precompute_twiddles_for_fft,
    };
    use boojum::field::goldilocks::GoldilocksField;
    use boojum::field::{Field, PrimeField};
    use boojum::worker::Worker;
    use era_cudart::memory::{memory_copy_async, DeviceAllocation};
    use era_cudart::result::CudaResult;
    use era_cudart::slice::DeviceSlice;
    use era_cudart::stream::CudaStream;
    use itertools::Itertools;
    use rand::distributions::{Distribution, Uniform};
    use rand::{thread_rng, Rng};
    use serial_test::serial;
    use std::alloc::Global;
    use std::cmp::max;
    use std::fmt::Debug;
    use std::mem;

    fn assert_equal<T: PartialEq + Debug>((a, b): (T, T)) {
        assert_eq!(a, b);
    }

    type GetPowersOfWOrGDeviceFn =
        fn(u32, u32, bool, bool, &mut DeviceSlice<BF>, &CudaStream) -> CudaResult<()>;

    fn test_get_powers_of_w_or_g(
        log_degree: u32,
        inverse: bool,
        generator: BF,
        device_fn: GetPowersOfWOrGDeviceFn,
    ) {
        let n = 1 << log_degree;
        let context = Context::create(12, 12).unwrap();
        let mut h_result = vec![BF::ZERO; n];
        let mut d_result = DeviceAllocation::alloc(n).unwrap();
        let stream = CudaStream::default();
        device_fn(
            log_degree,
            0,
            inverse,
            false,
            &mut d_result[..n / 2],
            &stream,
        )
        .unwrap();
        device_fn(
            log_degree,
            n as u32 / 2,
            inverse,
            false,
            &mut d_result[n / 2..],
            &stream,
        )
        .unwrap();
        memory_copy_async(&mut h_result, &d_result, &stream).unwrap();
        stream.synchronize().unwrap();
        let mut generator = generator;
        if inverse {
            generator = generator.inverse().unwrap();
        }
        h_result
            .into_iter()
            .enumerate()
            .map(|(i, x)| (generator.pow_u64(i as u64), x))
            .for_each(assert_equal);
        context.destroy().unwrap();
    }

    fn test_get_powers_of_w(inverse: bool) {
        const LOG_DEGREE: u32 = 16;
        let generator = domain_generator_for_size((1 << LOG_DEGREE) as u64);
        test_get_powers_of_w_or_g(LOG_DEGREE, inverse, generator, super::get_powers_of_w);
    }

    #[test]
    #[serial]
    fn get_powers_of_w() {
        test_get_powers_of_w(false);
    }

    #[test]
    #[serial]
    fn get_powers_of_w_inverse() {
        test_get_powers_of_w(true);
    }

    fn test_get_powers_of_g(inverse: bool) {
        const LOG_DEGREE: u32 = 16;
        let generator = BF::multiplicative_generator().pow_u64(1 << (OMEGA_LOG_ORDER - LOG_DEGREE));
        test_get_powers_of_w_or_g(LOG_DEGREE, inverse, generator, super::get_powers_of_g);
    }

    #[test]
    #[serial]
    fn get_powers_of_g() {
        test_get_powers_of_g(false);
    }

    #[test]
    #[serial]
    fn get_powers_of_g_inverse() {
        test_get_powers_of_g(true);
    }

    fn test_get_powers_bf(by_val: bool) {
        const N: usize = 1 << 16;
        let mut h_result = vec![BF::ZERO; N];
        let mut d_result = DeviceAllocation::alloc(N).unwrap();
        let stream = CudaStream::default();
        let base = GoldilocksField(42);
        let mut d_base = DeviceAllocation::alloc(1).unwrap();
        memory_copy_async(&mut d_base, &[base], &stream).unwrap();
        let b = &d_base[0];
        let (d_result_0, d_result_1) = d_result.split_at_mut(N / 2);
        if by_val {
            get_powers_by_val(base, 0, false, d_result_0, &stream).unwrap();
            get_powers_by_val(base, N as u32 / 2, false, d_result_1, &stream).unwrap();
        } else {
            get_powers_by_ref(b, 0, false, d_result_0, &stream).unwrap();
            get_powers_by_ref(b, N as u32 / 2, false, d_result_1, &stream).unwrap();
        }
        memory_copy_async(&mut h_result, &d_result, &stream).unwrap();
        stream.synchronize().unwrap();
        h_result
            .into_iter()
            .enumerate()
            .map(|(i, x)| (base.pow_u64(i as u64), x))
            .for_each(assert_equal);
    }

    #[test]
    fn get_powers_by_val_bf() {
        test_get_powers_bf(true);
    }

    #[test]
    fn get_powers_by_ref_bf() {
        test_get_powers_bf(false);
    }

    fn test_get_powers_ef(by_val: bool) {
        const N: usize = 1 << 16;
        let mut h_result_0 = transmute_gf_vec::<EF>(vec![BF::ZERO; N * 2]);
        let mut h_result_1 = transmute_gf_vec::<EF>(vec![BF::ZERO; N * 2]);
        let mut d_result_0 = DeviceAllocation::alloc(N).unwrap();
        let mut d_result_1 = DeviceAllocation::alloc(N).unwrap();
        let stream = CudaStream::default();
        let base_ef =
            ExtensionField::from_coeff_in_base([GoldilocksField(42), GoldilocksField(42)]);
        let base_vf = unsafe { mem::transmute::<ExtensionField, EF>(base_ef) };
        let mut d_base = DeviceAllocation::alloc(1).unwrap();
        memory_copy_async(&mut d_base, &[base_vf], &stream).unwrap();
        let b = &d_base[0];
        if by_val {
            get_powers_by_val(base_vf, 0, false, &mut d_result_0, &stream).unwrap();
            get_powers_by_val(base_vf, N as u32, false, &mut d_result_1, &stream).unwrap();
        } else {
            get_powers_by_ref(b, 0, false, &mut d_result_0, &stream).unwrap();
            get_powers_by_ref(b, N as u32, false, &mut d_result_1, &stream).unwrap();
        }
        memory_copy_async(&mut h_result_0, &d_result_0, &stream).unwrap();
        memory_copy_async(&mut h_result_1, &d_result_1, &stream).unwrap();
        stream.synchronize().unwrap();
        let i_0 = EF::get_iterator(&h_result_0);
        let i_1 = EF::get_iterator(&h_result_1);
        i_0.chain(i_1)
            .enumerate()
            .map(|(i, x)| (base_ef.pow_u64(i as u64), x))
            .for_each(assert_equal);
    }

    #[test]
    fn get_powers_by_val_ef() {
        test_get_powers_ef(true);
    }

    #[test]
    fn get_powers_by_ref_ef() {
        test_get_powers_ef(false);
    }

    fn test_omega_shift(in_place: bool, inverse: bool) {
        const LOG_DEGREE: u32 = 16;
        const N: usize = 1 << LOG_DEGREE;
        const SHIFT: u32 = 42;
        let context = Context::create(12, 12).unwrap();
        let h_src = Uniform::new(0, BF::ORDER)
            .sample_iter(&mut thread_rng())
            .take(N)
            .map(GoldilocksField)
            .collect_vec();
        let mut h_dst = vec![BF::ZERO; N];
        let stream = CudaStream::default();
        if in_place {
            let mut d_values = DeviceAllocation::alloc(N).unwrap();
            memory_copy_async(&mut d_values, &h_src, &stream).unwrap();
            super::omega_shift_in_place(
                &mut d_values[..N / 2],
                LOG_DEGREE,
                0,
                inverse,
                SHIFT,
                &stream,
            )
            .unwrap();
            super::omega_shift_in_place(
                &mut d_values[N / 2..],
                LOG_DEGREE,
                N as u32 / 2,
                inverse,
                SHIFT,
                &stream,
            )
            .unwrap();
            memory_copy_async(&mut h_dst, &d_values, &stream).unwrap();
        } else {
            let mut d_src = DeviceAllocation::alloc(N).unwrap();
            let mut d_dst = DeviceAllocation::alloc(N).unwrap();
            memory_copy_async(&mut d_src, &h_src, &stream).unwrap();
            super::omega_shift(
                &d_src[..N / 2],
                LOG_DEGREE,
                0,
                inverse,
                SHIFT,
                &mut d_dst[..N / 2],
                &stream,
            )
            .unwrap();
            super::omega_shift(
                &d_src[N / 2..],
                LOG_DEGREE,
                N as u32 / 2,
                inverse,
                SHIFT,
                &mut d_dst[N / 2..],
                &stream,
            )
            .unwrap();
            memory_copy_async(&mut h_dst, &d_dst, &stream).unwrap();
        }
        stream.synchronize().unwrap();
        let mut generator: BF = domain_generator_for_size((1 << LOG_DEGREE) as u64);
        if inverse {
            generator = generator.inverse().unwrap();
        }
        h_src
            .into_iter()
            .enumerate()
            .map(|(i, x)| x * generator.pow_u64(SHIFT as u64 * i as u64))
            .zip(h_dst)
            .for_each(assert_equal);
        context.destroy().unwrap();
    }

    #[test]
    #[serial]
    fn omega_shift() {
        test_omega_shift(false, false);
    }

    #[test]
    #[serial]
    fn omega_shift_inverse() {
        test_omega_shift(false, true);
    }

    #[test]
    #[serial]
    fn omega_shift_in_place() {
        test_omega_shift(true, false);
    }

    #[test]
    #[serial]
    fn omega_shift_in_place_inverse() {
        test_omega_shift(true, true);
    }

    fn test_bit_reverse(in_place: bool) {
        const LOG_ROWS: usize = 16;
        const ROWS: usize = 1 << LOG_ROWS;
        const COLS: usize = 16;
        const N: usize = COLS << LOG_ROWS;
        let h_src = Uniform::new(0, BF::ORDER)
            .sample_iter(&mut thread_rng())
            .take(N)
            .map(GoldilocksField)
            .collect_vec();
        let mut h_dst = vec![BF::ZERO; N];
        let stream = CudaStream::default();
        if in_place {
            let mut d_values = DeviceAllocation::alloc(N).unwrap();
            memory_copy_async(&mut d_values, &h_src, &stream).unwrap();
            let mut matrix = DeviceMatrixMut::new(&mut d_values, ROWS);
            super::bit_reverse_in_place(&mut matrix, &stream).unwrap();
            memory_copy_async(&mut h_dst, &d_values, &stream).unwrap();
        } else {
            let mut d_src = DeviceAllocation::alloc(N).unwrap();
            let mut d_dst = DeviceAllocation::alloc(N).unwrap();
            memory_copy_async(&mut d_src, &h_src, &stream).unwrap();
            let src_matrix = DeviceMatrix::new(&d_src, ROWS);
            let mut dst_matrix = DeviceMatrixMut::new(&mut d_dst, ROWS);
            super::bit_reverse(&src_matrix, &mut dst_matrix, &stream).unwrap();
            memory_copy_async(&mut h_dst, &d_dst, &stream).unwrap();
        }
        stream.synchronize().unwrap();
        h_src
            .into_iter()
            .chunks(ROWS)
            .into_iter()
            .zip(h_dst.chunks(ROWS))
            .for_each(|(s, d)| {
                s.enumerate()
                    .map(|(i, x)| (x, d[i.reverse_bits() >> (usize::BITS - LOG_ROWS as u32)]))
                    .for_each(assert_equal);
            });
    }

    #[test]
    #[serial]
    fn bit_reverse() {
        test_bit_reverse(false);
    }

    #[test]
    #[serial]
    fn bit_reverse_in_place() {
        test_bit_reverse(true);
    }

    fn test_batch_inv_bf(in_place: bool) {
        const LOG_N: usize = 16;
        const N: usize = 1 << LOG_N;
        let h_src = Uniform::new(0, BF::ORDER)
            .sample_iter(&mut thread_rng())
            .take(N)
            .map(GoldilocksField)
            .collect_vec();
        let mut h_dst = vec![BF::ZERO; N];
        let stream = CudaStream::default();
        if in_place {
            let mut d_values = DeviceAllocation::alloc(N).unwrap();
            memory_copy_async(&mut d_values, &h_src, &stream).unwrap();
            batch_inv_in_place::<BF>(&mut d_values, &stream).unwrap();
            memory_copy_async(&mut h_dst, &d_values, &stream).unwrap();
        } else {
            let mut d_src = DeviceAllocation::alloc(N).unwrap();
            let mut d_dst = DeviceAllocation::alloc(N).unwrap();
            memory_copy_async(&mut d_src, &h_src, &stream).unwrap();
            batch_inv::<BF>(&d_src, &mut d_dst, &stream).unwrap();
            memory_copy_async(&mut h_dst, &d_dst, &stream).unwrap();
        }
        stream.synchronize().unwrap();
        h_src
            .into_iter()
            .map(|x| x.inverse().unwrap_or_default())
            .zip(h_dst)
            .for_each(assert_equal);
    }

    #[test]
    fn batch_inv_bf() {
        test_batch_inv_bf(false);
    }

    #[test]
    fn batch_inv_in_place_bf() {
        test_batch_inv_bf(true);
    }

    fn test_batch_inv_ef(in_place: bool) {
        const LOG_N: usize = 16;
        const N: usize = 1 << LOG_N;
        let h_src_bf = Uniform::new(0, BF::ORDER)
            .sample_iter(&mut thread_rng())
            .take(2 * N)
            .map(GoldilocksField)
            .collect_vec();
        let mut h_dst_bf = vec![BF::ZERO; 2 * N];
        let stream = CudaStream::default();
        if in_place {
            let mut d_values_bf = DeviceAllocation::alloc(2 * N).unwrap();
            memory_copy_async(&mut d_values_bf, &h_src_bf, &stream).unwrap();
            let d_values_ef = unsafe { d_values_bf.transmute_mut::<EF>() };
            batch_inv_in_place::<EF>(d_values_ef, &stream).unwrap();
            memory_copy_async(&mut h_dst_bf, &d_values_bf, &stream).unwrap();
        } else {
            let mut d_src_bf = DeviceAllocation::alloc(2 * N).unwrap();
            let mut d_dst_bf = DeviceAllocation::alloc(2 * N).unwrap();
            memory_copy_async(&mut d_src_bf, &h_src_bf, &stream).unwrap();
            let d_src_ef = unsafe { d_src_bf.transmute::<EF>() };
            let d_dst_ef = unsafe { d_dst_bf.transmute_mut::<EF>() };
            batch_inv::<EF>(d_src_ef, d_dst_ef, &stream).unwrap();
            memory_copy_async(&mut h_dst_bf, &d_dst_bf, &stream).unwrap();
        }
        stream.synchronize().unwrap();
        let h_src_c0 = &h_src_bf[0..N];
        let h_src_c1 = &h_src_bf[N..2 * N];
        let h_dst_c0 = &h_dst_bf[0..N];
        let h_dst_c1 = &h_dst_bf[N..2 * N];
        for (((src_c0, src_c1), dst_c0), dst_c1) in
            h_src_c0.iter().zip(h_src_c1).zip(h_dst_c0).zip(h_dst_c1)
        {
            let control = ExtensionField::from_coeff_in_base([*src_c0, *src_c1]);
            let control = control.inverse().unwrap_or_default();
            let result = ExtensionField::from_coeff_in_base([*dst_c0, *dst_c1]);
            assert_eq!(control, result)
        }
    }

    #[test]
    fn batch_inv_ef() {
        test_batch_inv_ef(false);
    }

    #[test]
    fn batch_inv_ef_in_place() {
        test_batch_inv_ef(true);
    }

    #[test]
    fn pack_variable_indexes() {
        const LOG_N: usize = 16;
        const N: usize = 1 << LOG_N;
        let mut h_src = Uniform::new(0, 1u64 << 31)
            .sample_iter(&mut thread_rng())
            .take(N)
            .collect_vec();
        let mut h_dst = vec![0u32; N];
        h_src[0] = 1 << 63;
        h_src[N - 1] = 1 << 63;
        let mut d_src = DeviceAllocation::alloc(N).unwrap();
        let mut d_dst = DeviceAllocation::alloc(N).unwrap();
        let stream = CudaStream::default();
        memory_copy_async(&mut d_src, &h_src, &stream).unwrap();
        super::pack_variable_indexes(&d_src, &mut d_dst, &stream).unwrap();
        memory_copy_async(&mut h_dst, &d_dst, &stream).unwrap();
        stream.synchronize().unwrap();
        h_src
            .into_iter()
            .map(|x| if x == (1 << 63) { 1 << 31 } else { x as u32 })
            .zip(h_dst)
            .for_each(assert_equal);
    }

    #[test]
    fn select() {
        const LOG_N: usize = 16;
        const N: usize = 1 << LOG_N;
        const PLACEHOLDER: u32 = 1 << 31;
        let h_indexes = (0..N)
            .map(|i| {
                if i == 0 {
                    PLACEHOLDER
                } else {
                    (i.reverse_bits() >> (usize::BITS - LOG_N as u32)) as u32
                }
            })
            .collect_vec();
        let h_src = Uniform::new(0, BF::ORDER)
            .sample_iter(&mut thread_rng())
            .take(N)
            .map(GoldilocksField)
            .collect_vec();
        let mut h_dst = vec![BF::ZERO; N];
        let mut d_indexes = DeviceAllocation::alloc(N).unwrap();
        let mut d_src = DeviceAllocation::alloc(N).unwrap();
        let mut d_dst = DeviceAllocation::alloc(N).unwrap();
        let stream = CudaStream::default();
        memory_copy_async(&mut d_indexes, &h_indexes, &stream).unwrap();
        memory_copy_async(&mut d_src, &h_src, &stream).unwrap();
        super::select(&d_indexes, &d_src, &mut d_dst, &stream).unwrap();
        memory_copy_async(&mut h_dst, &d_dst, &stream).unwrap();
        stream.synchronize().unwrap();
        h_indexes
            .into_iter()
            .map(|i| {
                if i == PLACEHOLDER {
                    BF::ZERO
                } else {
                    h_src[i as usize]
                }
            })
            .zip(h_dst)
            .for_each(assert_equal);
    }

    #[test]
    fn mark_ends_of_runs() {
        const LOG_DEGREE: u32 = 16;
        const N: usize = 1 << LOG_DEGREE;
        const RANGE_MAX: u32 = 2;
        let temp_storage_bytes = max(
            get_encode_temp_storage_bytes::<u32>(N as i32).unwrap(),
            get_scan_temp_storage_bytes::<u32>(ScanOperation::Sum, false, false, N as i32).unwrap(),
        );
        let mut d_temp_storage = DeviceAllocation::alloc(temp_storage_bytes).unwrap();
        let mut rng = thread_rng();
        let h_in = Uniform::new(0, RANGE_MAX)
            .sample_iter(&mut rng)
            .take(N)
            .collect_vec();
        let mut h_result = vec![0u32; N];
        let mut d_in = DeviceAllocation::alloc(N).unwrap();
        let mut d_unique_out = DeviceAllocation::alloc(N).unwrap();
        let mut d_counts_out = DeviceAllocation::alloc(N).unwrap();
        let mut d_num_runs_out = DeviceAllocation::alloc(1).unwrap();
        let mut d_result = DeviceAllocation::alloc(N).unwrap();
        let stream = CudaStream::default();
        memory_copy_async(&mut d_in, &h_in, &stream).unwrap();
        encode(
            &mut d_temp_storage,
            &d_in,
            &mut d_unique_out,
            &mut d_counts_out,
            &mut d_num_runs_out[0],
            &stream,
        )
        .unwrap();
        let mut d_offsets = d_counts_out;
        scan_in_place(
            ScanOperation::Sum,
            false,
            false,
            &mut d_temp_storage,
            &mut d_offsets,
            &stream,
        )
        .unwrap();
        set_to_zero(&mut d_result, &stream).unwrap();
        super::mark_ends_of_runs(&d_num_runs_out[0], &d_offsets, &mut d_result, &stream).unwrap();
        memory_copy_async(&mut h_result, &d_result, &stream).unwrap();
        stream.synchronize().unwrap();
        for i in 0..N {
            let expected = if i == N - 1 || h_in[i] != h_in[i + 1] {
                1
            } else {
                0
            };
            assert_eq!(h_result[i], expected);
        }
    }

    #[test]
    #[serial]
    fn generate_permutation_matrix() {
        const LOG_NUM_ROWS: usize = 8;
        const NUM_ROWS: usize = 1 << LOG_NUM_ROWS;
        const NUM_COLS: usize = 8;
        const NUM_CELLS: usize = NUM_COLS << LOG_NUM_ROWS;
        const RANGE_MAX: usize = NUM_ROWS - 1;
        const PLACEHOLDER: u32 = 1 << 31;
        let context = Context::create(12, 12).unwrap();
        let h_variable_indexes = (0..NUM_CELLS)
            .map(|i| (i % RANGE_MAX) as u32)
            .map(|i| if i == 0 { PLACEHOLDER } else { i })
            .collect_vec();
        let h_scalars = Uniform::new(0, BF::ORDER)
            .sample_iter(&mut thread_rng())
            .take(NUM_COLS)
            .map(GoldilocksField)
            .collect_vec();
        let mut h_permutation_matrix = vec![BF::ZERO; NUM_CELLS];
        let mut h_twiddles = vec![BF::ZERO; NUM_ROWS];
        let mut d_variable_indexes = DeviceAllocation::alloc(NUM_CELLS).unwrap();
        let mut d_scalars = DeviceAllocation::alloc(NUM_COLS).unwrap();
        let mut d_permutation_matrix = DeviceAllocation::alloc(NUM_CELLS).unwrap();
        let mut d_twiddles = DeviceAllocation::alloc(NUM_ROWS).unwrap();
        let temp_storage_bytes =
            get_generate_permutation_matrix_temp_storage_bytes(NUM_CELLS).unwrap();
        let mut d_temp_storage = DeviceAllocation::alloc(temp_storage_bytes).unwrap();
        let stream = CudaStream::default();
        memory_copy_async(&mut d_variable_indexes, &h_variable_indexes, &stream).unwrap();
        memory_copy_async(&mut d_scalars, &h_scalars, &stream).unwrap();
        super::generate_permutation_matrix(
            &mut d_temp_storage,
            &d_variable_indexes,
            &d_scalars,
            &mut d_permutation_matrix,
            &stream,
        )
        .unwrap();
        memory_copy_async(&mut h_permutation_matrix, &d_permutation_matrix, &stream).unwrap();
        super::get_powers_of_w(
            LOG_NUM_ROWS as u32,
            0,
            false,
            false,
            &mut d_twiddles,
            &stream,
        )
        .unwrap();
        memory_copy_async(&mut h_twiddles, &d_twiddles, &stream).unwrap();
        stream.synchronize().unwrap();
        for (col, &scalar) in h_scalars.iter().enumerate() {
            for (row, &twiddle) in h_twiddles.iter().enumerate() {
                let source_index = col * NUM_ROWS + row;
                let target_index = if h_variable_indexes[source_index] == PLACEHOLDER {
                    source_index
                } else {
                    let mut idx = source_index + RANGE_MAX;
                    if idx >= NUM_CELLS {
                        idx = source_index % RANGE_MAX;
                    }
                    idx
                };
                let expected = twiddle * scalar;
                let actual = h_permutation_matrix[target_index];
                assert_eq!(expected, actual);
            }
        }
        context.destroy().unwrap();
    }

    #[test]
    fn set_values_from_packed_bits() {
        const LOG_N: usize = 16;
        const N: usize = 1 << LOG_N;
        const PACKED_N: usize = N / u32::BITS as usize;
        let rng = &mut thread_rng();
        let h_packed_bits = (0..PACKED_N).map(|_| rng.gen()).collect_vec();
        let mut h_result = vec![BF::ZERO; N];
        let mut d_packed_bits = DeviceAllocation::alloc(PACKED_N).unwrap();
        let mut d_result = DeviceAllocation::alloc(N).unwrap();
        let stream = CudaStream::default();
        memory_copy_async(&mut d_packed_bits, &h_packed_bits, &stream).unwrap();
        super::set_values_from_packed_bits(&d_packed_bits, &mut d_result, &stream).unwrap();
        memory_copy_async(&mut h_result, &d_result, &stream).unwrap();
        stream.synchronize().unwrap();
        h_packed_bits
            .into_iter()
            .flat_map(|word| (0..u32::BITS).map(move |i| (word >> i) & 1))
            .map(|bit| if bit == 1 { BF::ONE } else { BF::ZERO })
            .zip(h_result)
            .for_each(assert_equal);
    }

    #[test]
    #[serial]
    fn fold() {
        const LOG_N: usize = 16;
        const N: usize = 1 << LOG_N;
        let context = Context::create(12, 12).unwrap();
        let worker = Worker::new_with_num_threads(1);
        type F = BF;
        let roots = precompute_twiddles_for_fft::<F, F, Global, true>(N * 2, &worker, &mut ());
        let coset_inverse = <F as PrimeField>::multiplicative_generator()
            .inverse()
            .unwrap();
        let uniform = Uniform::new(0, BF::ORDER);
        let mut rng = thread_rng();
        let h_src = uniform
            .sample_iter(&mut rng)
            .map(GoldilocksField)
            .array_chunks()
            .map(ExtensionField::from_coeff_in_base)
            .take(N * 2)
            .collect_vec();
        let h_challenge = uniform
            .sample_iter(&mut rng)
            .map(GoldilocksField)
            .array_chunks()
            .map(ExtensionField::from_coeff_in_base)
            .next()
            .unwrap();
        let mut h_dst = vec![ExtensionField::ZERO; N];
        let stream = CudaStream::default();
        let mut d_challenge = DeviceAllocation::alloc(1).unwrap();
        let mut d_src_ef = DeviceAllocation::alloc(N * 2).unwrap();
        let mut d_src_vf = DeviceAllocation::alloc(N * 2).unwrap();
        let mut d_dst_vf = DeviceAllocation::<VectorizedExtensionField>::alloc(N).unwrap();
        let mut d_dst_ef = DeviceAllocation::alloc(N).unwrap();
        memory_copy_async(&mut d_challenge, &[h_challenge], &stream).unwrap();
        memory_copy_async(&mut d_src_ef, &h_src, &stream).unwrap();
        convert(&d_src_ef, &mut d_src_vf, &stream).unwrap();
        let src = DeviceVectorChunk::new(&d_src_vf, 0, N);
        let mut dst = DeviceVectorChunkMut::new(&mut d_dst_vf, 0, N / 2);
        super::fold(coset_inverse, &d_challenge[0], 0, &src, &mut dst, &stream).unwrap();
        let src = DeviceVectorChunk::new(&d_src_vf, N, N);
        let mut dst = DeviceVectorChunkMut::new(&mut d_dst_vf, N / 2, N / 2);
        super::fold(
            coset_inverse,
            &d_challenge[0],
            N / 2,
            &src,
            &mut dst,
            &stream,
        )
        .unwrap();
        convert(&d_dst_vf, &mut d_dst_ef, &stream).unwrap();
        memory_copy_async(&mut h_dst, &d_dst_ef, &stream).unwrap();
        stream.synchronize().unwrap();
        context.destroy().unwrap();
        h_src
            .into_iter()
            .chunks(2)
            .into_iter()
            .enumerate()
            .map(|(i, chunk)| {
                let v = chunk.collect_vec();
                let mut sum = v[0];
                sum.add_assign(&v[1]);
                let mut diff = v[0];
                diff.sub_assign(&v[1]);
                diff.mul_assign_by_base(&coset_inverse);
                diff.mul_assign_by_base(&roots[i]);
                diff.mul_assign(&h_challenge);
                sum.add_assign(&diff);
                sum
            })
            .zip(h_dst)
            .for_each(assert_equal);
    }
}