rusqsieve 0.1.1

The frontier of pure-Rust Quadratic Sieve integer factorization, portable to WASM (wasm32-unknown-unknown).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
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
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
# Quadratic Sieve Rust Crate — Complete Implementation Specification

## 1. Purpose

Implement a production-quality Rust crate named `rusqsieve` that factors positive integers using a highly optimized, portable, parallelized Self-Initializing Quadratic Sieve (SIQS) implementation.

The crate must provide:

1. A fixed-capacity custom unsigned big-integer type optimized for factorization workloads.
2. A high-level blocking factorization API on supported native targets.
3. A scheduler-independent low-level API for parallel sieving and sparse linear algebra over `F_2`.
4. A raw `wasm32-unknown-unknown` C ABI with no `wasm-bindgen` dependency.
5. Independent JavaScript glue that creates Web Workers and executes Rust worker kernels concurrently.
6. A native CLI that reads one positive decimal integer from standard input and prints prime factors, including repetitions, one per line.
7. A structured progress-reporting system covering factor-base construction, relation collection, matrix processing, and linear algebra.

The implementation must be contained in one Cargo package and one Rust library crate. Platform-specific behavior must be selected with `cfg` gates.

The result is intended to be implementation-ready, benchmarkable, testable, and suitable for later architecture-specific optimization.

---

## 2. Scope and expectations

### 2.1 Required factorization strategy

Use SIQS as the main large-composite factorization algorithm.

The high-level factorization pipeline must also include inexpensive preprocessing:

- validation of the input;
- trial division by embedded small primes;
- probable-prime testing using Miller–Rabin;
- perfect-square and perfect-power detection;
- optional bounded Pollard rho for composites with an easily discoverable factor;
- recursive factorization until every returned factor passes the configured probable-prime test.

### 2.2 Practical range

The default `Natural<16>` stores up to 1024 bits. This is a capacity limit, not a claim that arbitrary 1024-bit semiprimes are practically factorable with SIQS.

The implementation must not artificially restrict SIQS to a fixed decimal-digit range, but parameter heuristics, resource limits, and documentation must make clear that SIQS is not intended to replace the Number Field Sieve for very large hard semiprimes.

### 2.3 Non-goals for the initial implementation

The initial implementation does not need:

- General Number Field Sieve.
- ECM.
- Tokio or any asynchronous Rust runtime.
- `wasm-bindgen`.
- Rust-native multithreading inside `wasm32-unknown-unknown`.
- Shared WebAssembly memory or `SharedArrayBuffer` support.
- Arbitrary-precision signed integers as a public type.
- Stable ABI compatibility across unrelated crate major versions.
- Constant-time cryptographic behavior.

---

## 3. Package and target structure

Use one Cargo package with one library target and one CLI binary target.

Recommended layout:

```text
rusqsieve/
├── Cargo.toml
├── README.md
├── LICENSE-APACHE
├── LICENSE-MPL
├── benches/
│   ├── natural.rs
│   ├── sieve.rs
│   └── linear_algebra.rs
├── js/
│   ├── index.js
│   ├── coordinator.js
│   ├── worker.js
│   └── protocol.js
├── src/
│   ├── lib.rs
│   ├── progress.rs
│   ├── primality.rs
│   ├── factors.rs
│   ├── natural/
│   │   ├── mod.rs
│   │   ├── add_sub.rs
│   │   ├── mul.rs
│   │   ├── div.rs
│   │   ├── gcd.rs
│   │   ├── sqrt.rs
│   │   ├── modular.rs
│   │   ├── parse.rs
│   │   └── format.rs
│   ├── factor/
│   │   ├── mod.rs
│   │   ├── config.rs
│   │   ├── coordinator.rs
│   │   ├── preprocessing.rs
│   │   ├── recursive.rs
│   │   └── state.rs
│   ├── qs/
│   │   ├── mod.rs
│   │   ├── config.rs
│   │   ├── parameters.rs
│   │   ├── multiplier.rs
│   │   ├── factor_base.rs
│   │   ├── polynomial.rs
│   │   ├── sieve.rs
│   │   ├── relation.rs
│   │   ├── partials.rs
│   │   ├── matrix.rs
│   │   └── extract.rs
│   ├── f2/
│   │   ├── mod.rs
│   │   ├── dense.rs
│   │   ├── sparse.rs
│   │   ├── filter.rs
│   │   ├── provenance.rs
│   │   └── block_lanczos.rs
│   ├── work/
│   │   ├── mod.rs
│   │   ├── job.rs
│   │   ├── result.rs
│   │   ├── context.rs
│   │   └── kernel.rs
│   ├── native/
│   │   ├── mod.rs
│   │   ├── pool.rs
│   │   └── driver.rs
│   ├── wasm/
│   │   ├── mod.rs
│   │   ├── abi.rs
│   │   ├── handles.rs
│   │   └── wire.rs
│   ├── arch/
│   │   ├── mod.rs
│   │   ├── portable.rs
│   │   ├── x86_64.rs
│   │   ├── aarch64.rs
│   │   └── wasm32.rs
│   └── bin/
│       └── qs-factor.rs
└── tests/
    ├── natural_properties.rs
    ├── primality.rs
    ├── relations.rs
    ├── matrix.rs
    ├── factorization.rs
    ├── determinism.rs
    └── cli.rs
```

### 3.1 Cargo configuration

Use Rust edition 2024.

The library must emit both `rlib` and `cdylib`:

```toml
[lib]
crate-type = ["rlib", "cdylib"]
```

The CLI must be gated behind a feature:

```toml
[features]
default = ["cli"]
cli = []
relation-checks = []
arch-optimized = []
wasm-simd128 = []
reference-qs = []
```

```toml
[[bin]]
name = "qs-factor"
path = "src/bin/qs-factor.rs"
required-features = ["cli"]
```

Recommended profiles:

```toml
[profile.release]
opt-level = 3
lto = "fat"
codegen-units = 1
panic = "abort"

[profile.bench]
opt-level = 3
lto = "thin"
codegen-units = 1
```

Native build:

```sh
cargo build --release
```

WebAssembly build:

```sh
cargo build \
  --release \
  --target wasm32-unknown-unknown \
  --lib \
  --no-default-features
```

### 3.2 Conditional compilation

Use the following target groups:

```rust
#[cfg(any(unix, windows))]
mod native;

#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
mod wasm;
```

Do not use only `not(target_arch = "wasm32")` to define native support. The blocking threaded API should be exposed only on explicitly supported native OS families.

The CLI source must contain:

```rust
#[cfg(target_arch = "wasm32")]
compile_error!("the qs-factor CLI is unavailable on wasm32 targets");
```

---

## 4. Crate-level design principles

1. The mathematical core must not create threads.
2. Parallel work must be represented as deterministic bounded jobs.
3. Native threads and Web Workers must execute the same Rust kernels.
4. The factorization coordinator must own global state and user-visible progress.
5. Workers must return results and metrics, not mutate coordinator state.
6. All cross-Wasm ABI data must be serialized; Rust layouts are not an ABI.
7. Wrapping big-integer operators must never silently contaminate mathematical algorithms that require exact arithmetic.
8. Every accepted QS relation must be verifiable through an explicit invariant.
9. Every matrix dependency must be verified before factor extraction.
10. Public APIs must be documented and designed for forward compatibility using `#[non_exhaustive]` where appropriate.

---

## 5. Public crate API

Recommended root exports:

```rust
pub mod f2;
pub mod low_level;
pub mod natural;
pub mod progress;
pub mod qs;

mod factor;
mod factors;
mod primality;
mod work;
mod arch;

#[cfg(any(unix, windows))]
mod native;

#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
mod wasm;

pub use factor::{
    FactorConfig,
    FactorError,
    FactorLimits,
    FactorSession,
    Parallelism,
    ProgressAction,
};

pub use factors::PrimeFactors;
pub use natural::{Natural, ParseNaturalError, WideNatural};
pub use progress::*;

#[cfg(any(unix, windows))]
pub use native::{factor, factor_with, factor_with_progress};
```

The `low_level` module should re-export stable work-unit and kernel APIs intended for custom schedulers:

```rust
pub mod low_level {
    pub use crate::f2::{
        BlockLanczos,
        DependencySet,
        MatrixOperation,
        SparseBinaryMatrix,
    };

    pub use crate::qs::{
        prepare_siqs,
        sieve_job,
        FactorBase,
        RawRelation,
        SieveContext,
        SieveScratch,
    };

    pub use crate::work::{
        execute_job,
        JobHeader,
        KernelContexts,
        MatrixMultiplyJob,
        MatrixMultiplyResult,
        SieveJob,
        SieveResult,
        WorkJob,
        WorkResult,
        WorkerScratch,
    };
}
```

---

## 6. `Natural` fixed-capacity big integer

### 6.1 Representation

```rust
#[repr(transparent)]
#[derive(Clone, Eq, PartialEq, Hash)]
pub struct Natural<const PARTS_64: usize = 16> {
    parts: [u64; PARTS_64],
}
```

Representation rules:

- Limb order is little-endian.
- `parts[0]` is the least-significant limb.
- Capacity is exactly `PARTS_64 * 64` bits.
- Unused high limbs must be zero.
- The type represents unsigned integers only.
- `Natural` should not implement `Copy` by default. Explicit cloning avoids accidental copying of large values.

### 6.2 Constants and basic access

```rust
impl<const P: usize> Natural<P> {
    pub const BITS: usize = P * 64;
    pub const ZERO: Self;
    pub const ONE: Self;
    pub const MAX: Self;

    pub const fn from_u64(value: u64) -> Self;
    pub const fn as_parts(&self) -> &[u64; P];
    pub fn as_mut_parts(&mut self) -> &mut [u64; P];

    pub fn is_zero(&self) -> bool;
    pub fn is_one(&self) -> bool;
    pub fn is_even(&self) -> bool;
    pub fn is_odd(&self) -> bool;
    pub fn bit_len(&self) -> usize;
    pub fn trailing_zeros(&self) -> usize;
    pub fn bit(&self, index: usize) -> bool;
}
```

### 6.3 Parsing

Provide a const-compatible decimal parser:

```rust
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ParseNaturalError {
    Empty,
    InvalidDigit { index: usize, byte: u8 },
    Overflow,
}
```

```rust
impl<const P: usize> Natural<P> {
    pub const fn from_decimal(value: &str) -> Result<Self, ParseNaturalError>;
}
```

Implement `FromStr` by delegating to `from_decimal`:

```rust
impl<const P: usize> core::str::FromStr for Natural<P> {
    type Err = ParseNaturalError;
}
```

Parsing requirements:

- Accept only ASCII decimal digits.
- Reject empty strings.
- Do not accept leading `+` or `-`.
- Leading zeroes are allowed.
- Overflow must be detected exactly.
- Parsing must not allocate.

### 6.4 Compile-time literal macro

Export:

```rust
natural!("966680312498850986629904881784491804947363701071", 16)
```

Recommended implementation:

```rust
#[macro_export]
macro_rules! natural {
    ($value:literal, $parts:literal) => {{
        const VALUE: $crate::Natural<$parts> =
            match $crate::Natural::<$parts>::from_decimal($value) {
                Ok(value) => value,
                Err(_) => panic!("invalid or overflowing Natural literal"),
            };
        VALUE
    }};
}
```

Malformed or overflowing literals must fail at compile time.

### 6.5 Formatting

Implement:

- `Display` as canonical decimal without leading zeroes.
- `Debug` as a capacity-qualified hexadecimal representation.
- `LowerHex` and `UpperHex`.

Suggested `Debug` form:

```text
Natural<16>(0x1234abcd)
```

Decimal formatting should repeatedly divide a scratch copy by `10^19`, storing `u64` chunks.

### 6.6 Byte conversion

```rust
pub fn from_be_bytes(bytes: &[u8]) -> Result<Self, CapacityError>;
pub fn from_le_bytes(bytes: &[u8]) -> Result<Self, CapacityError>;

pub fn write_be_bytes(&self, out: &mut [u8]) -> Result<usize, BufferTooSmall>;
pub fn write_le_bytes(&self, out: &mut [u8]) -> Result<usize, BufferTooSmall>;
```

Also provide fixed-width byte serialization if useful internally.

### 6.7 Operator semantics

Ordinary arithmetic operators use fixed-width wrapping semantics modulo `2^(64 * P)`:

- `Add`, `AddAssign`
- `Sub`, `SubAssign`
- `Mul`, `MulAssign`
- `BitAnd`, `BitAndAssign`
- `BitOr`, `BitOrAssign`
- `BitXor`, `BitXorAssign`
- `Not`
- `Shl`, `ShlAssign`
- `Shr`, `ShrAssign`
- `Div`, `DivAssign`
- `Rem`, `RemAssign`

Preferred operand implementations:

```rust
&Natural + &Natural -> Natural
Natural += &Natural
```

Owned forms may delegate to borrowed forms.

Division by zero must panic for operator traits, matching ordinary Rust integer operator behavior. Fallible methods must return `None` or a typed error.

### 6.8 Exact and overflow-aware arithmetic

Provide:

```rust
pub fn overflowing_add(&self, rhs: &Self) -> (Self, bool);
pub fn overflowing_sub(&self, rhs: &Self) -> (Self, bool);
pub fn overflowing_mul(&self, rhs: &Self) -> (Self, bool);

pub fn checked_add(&self, rhs: &Self) -> Option<Self>;
pub fn checked_sub(&self, rhs: &Self) -> Option<Self>;
pub fn checked_mul(&self, rhs: &Self) -> Option<Self>;

pub fn wrapping_add(&self, rhs: &Self) -> Self;
pub fn wrapping_sub(&self, rhs: &Self) -> Self;
pub fn wrapping_mul(&self, rhs: &Self) -> Self;
```

Mathematical algorithms must use checked, widening, or modular operations as appropriate. Do not rely on wrapping operator behavior internally where overflow would invalidate a proof or invariant.

### 6.9 Widening multiplication

Stable generic const expressions cannot be assumed for `[u64; 2 * P]`. Use:

```rust
#[derive(Clone, Eq, PartialEq)]
pub struct WideNatural<const P: usize> {
    low: [u64; P],
    high: [u64; P],
}
```

Provide:

```rust
pub fn widening_mul(&self, rhs: &Self) -> WideNatural<P>;
pub fn widening_square(&self) -> WideNatural<P>;

impl<const P: usize> WideNatural<P> {
    pub fn low(&self) -> Natural<P>;
    pub fn high(&self) -> Natural<P>;
    pub fn overflowing_narrow(self) -> (Natural<P>, bool);
}
```

### 6.10 Division and number-theoretic methods

Provide:

```rust
pub fn div_rem(&self, divisor: &Self) -> Option<(Self, Self)>;
pub fn div_rem_u64(&self, divisor: u64) -> Option<(Self, u64)>;

pub fn gcd(&self, rhs: &Self) -> Self;
pub fn extended_gcd(&self, rhs: &Self) -> ExtendedGcdResult<P>;

pub fn sqrt_rem(&self) -> (Self, Self);
pub fn floor_sqrt(&self) -> Self;
pub fn ceil_sqrt(&self) -> Self;
pub fn is_square(&self) -> bool;

pub fn checked_pow_u32(&self, exponent: u32) -> Option<Self>;
pub fn perfect_power(&self) -> Option<(Self, u32)>;
```

A signed internal type may be used privately for extended GCD coefficients, but it need not be public.

### 6.11 Limb kernels

Portable arithmetic primitives should be slice-oriented:

```rust
fn add_n(out: &mut [u64], a: &[u64], b: &[u64]) -> u64;
fn sub_n(out: &mut [u64], a: &[u64], b: &[u64]) -> u64;
fn add_word(out: &mut [u64], word: u64) -> u64;
fn mul_word(out: &mut [u64], a: &[u64], word: u64) -> u64;
fn mul_schoolbook(out: &mut [u64], a: &[u64], b: &[u64]);
fn square_schoolbook(out: &mut [u64], a: &[u64]);
fn shl_bits(out: &mut [u64], input: &[u64], shift: u32) -> u64;
fn shr_bits(out: &mut [u64], input: &[u64], shift: u32) -> u64;
```

Use `u128` as the portable multiply-accumulate primitive.

Begin with schoolbook multiplication, specialized squaring, and normalized long division. Add Karatsuba only after benchmarks show a benefit at relevant operand sizes.

### 6.12 Modular arithmetic

Use a reusable Montgomery context for odd moduli:

```rust
pub struct Montgomery<const P: usize> {
    modulus: Natural<P>,
    n0_inverse: u64,
    r2: Natural<P>,
    one: Natural<P>,
}
```

Required operations:

```rust
impl<const P: usize> Montgomery<P> {
    pub fn new(modulus: Natural<P>) -> Result<Self, MontgomeryError>;
    pub fn encode(&self, value: &Natural<P>) -> Natural<P>;
    pub fn decode(&self, value: &Natural<P>) -> Natural<P>;
    pub fn mul(&self, lhs: &Natural<P>, rhs: &Natural<P>) -> Natural<P>;
    pub fn square(&self, value: &Natural<P>) -> Natural<P>;
    pub fn pow(&self, base: &Natural<P>, exponent: &Natural<P>) -> Natural<P>;
    pub fn inv(&self, value: &Natural<P>) -> Option<Natural<P>>;
}
```

Also provide specialized small-modulus functions for SIQS factor-base work:

```rust
pub fn jacobi_u64(a: u64, n: u64) -> i8;
pub fn legendre_u32(n_mod_p: u32, p: u32) -> i8;
pub fn tonelli_shanks_u32(n_mod_p: u32, p: u32) -> Option<u32>;
```

---

## 7. Primality testing

### 7.1 API

```rust
#[derive(Clone, Debug)]
pub struct PrimalityConfig {
    pub rounds: NonZero<u32>,
    pub witnesses: WitnessPolicy,
}

#[derive(Clone, Debug)]
pub enum WitnessPolicy {
    FirstPrimes,
    Seeded { seed: [u8; 32] },
}
```

```rust
pub fn is_probable_prime<const P: usize>(
    n: &Natural<P>,
    config: &PrimalityConfig,
) -> bool;
```

### 7.2 Requirements

- Handle all values below 4 correctly.
- Reject even composites immediately.
- Perform trial division by a small fixed prime set first.
- Use strong Miller–Rabin tests.
- Witness generation must be reproducible with a configured seed.
- Do not claim deterministic primality for arbitrary-width inputs unless a mathematically sufficient deterministic witness set is used for the exact range.
- Documentation must use the term “probable prime.”

---

## 8. Prime factor result type

```rust
pub struct PrimeFactors<const PARTS_64: usize = 16> {
    map: BTreeMap<Natural<PARTS_64>, NonZero<usize>>,
}
```

Required methods:

```rust
impl<const P: usize> PrimeFactors<P> {
    pub fn new() -> Self;

    pub fn iter(
        &self,
    ) -> impl ExactSizeIterator<Item = (&Natural<P>, NonZero<usize>)>;

    pub fn get(&self, prime: &Natural<P>) -> Option<NonZero<usize>>;
    pub fn len(&self) -> usize;
    pub fn is_empty(&self) -> bool;
    pub fn into_map(self) -> BTreeMap<Natural<P>, NonZero<usize>>;
    pub fn verify_product(&self, original: &Natural<P>) -> bool;
}
```

Mutation should remain crate-private so these invariants hold:

- every key is at least 2;
- every exponent is nonzero;
- every key passes the configured probable-prime policy;
- the represented product equals the original input.

The natural iteration order is ascending.

---

## 9. High-level factorization API

### 9.1 Native blocking API

On `unix` and `windows`:

```rust
pub fn factor<const P: usize>(
    input: Natural<P>,
) -> Result<PrimeFactors<P>, FactorError>;

pub fn factor_with<const P: usize>(
    input: Natural<P>,
    config: FactorConfig,
) -> Result<PrimeFactors<P>, FactorError>;

pub fn factor_with_progress<const P: usize, F>(
    input: Natural<P>,
    config: FactorConfig,
    observer: F,
) -> Result<PrimeFactors<P>, FactorError>
where
    F: FnMut(&ProgressSnapshot) -> ProgressAction;
```

The API should consume `Natural` to avoid an unnecessary initial clone.

### 9.2 Configuration

```rust
#[derive(Clone, Debug)]
pub struct FactorConfig {
    pub parallelism: Parallelism,
    pub primality: PrimalityConfig,
    pub trial_division_limit: u32,
    pub small_factor_method: SmallFactorMethod,
    pub qs: QsConfig,
    pub limits: FactorLimits,
    pub seed: [u8; 32],
    pub progress_reporting: ProgressReportingConfig,
}
```

```rust
#[derive(Clone, Copy, Debug)]
pub enum Parallelism {
    Auto,
    Exact(NonZero<usize>),
}
```

```rust
#[derive(Clone, Debug)]
pub enum SmallFactorMethod {
    None,
    PollardRho {
        attempts: u32,
        iteration_limit: u64,
    },
}
```

`Parallelism::Auto` resolves using `std::thread::available_parallelism()` on native targets.

### 9.3 Resource limits

```rust
#[derive(Clone, Debug)]
pub struct FactorLimits {
    pub max_relations: Option<usize>,
    pub max_partial_relations: Option<usize>,
    pub max_matrix_nonzeros: Option<usize>,
    pub max_memory_bytes: Option<usize>,
    pub max_polynomial_batches: Option<u64>,
    pub max_pollard_rho_iterations: Option<u64>,
}
```

Native-only drivers may additionally enforce wall-clock cancellation, but portable kernels must not depend on clocks.

### 9.4 Recursive pipeline

The coordinator must implement this conceptual algorithm:

```text
factor_node(n):
    if n == 0: error
    if n == 1: return

    divide out configured small primes

    if n == 1: return
    if is_probable_prime(n): insert n; return

    if n is a perfect power a^k:
        factor a recursively
        multiply exponents by k
        return

    try bounded Pollard rho if enabled

    if no factor found:
        factor = SIQS(n)

    recursively factor factor
    recursively factor n / factor
```

Returned factors must be sorted by `Natural` order through `BTreeMap`.

---

## 10. Factorization session and scheduler-independent coordination

### 10.1 Session type

```rust
pub struct FactorSession<const P: usize = 16> {
    // private coordinator state
}
```

Required API:

```rust
impl<const P: usize> FactorSession<P> {
    pub fn new(
        input: Natural<P>,
        config: FactorConfig,
    ) -> Result<Self, FactorError>;

    pub fn phase(&self) -> SessionPhase;
    pub fn progress(&self) -> &ProgressSnapshot;
    pub fn progress_revision(&self) -> u64;

    pub fn advance_local(
        &mut self,
        budget: LocalWorkBudget,
    ) -> Result<AdvanceOutcome, FactorError>;

    pub fn take_jobs(
        &mut self,
        maximum: usize,
    ) -> Result<Vec<WorkJob>, FactorError>;

    pub fn submit(
        &mut self,
        result: WorkResult,
    ) -> Result<SubmitOutcome, FactorError>;

    pub fn is_finished(&self) -> bool;

    pub fn take_factors(
        self,
    ) -> Result<PrimeFactors<P>, FactorError>;
}
```

### 10.2 Session phases

```rust
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum SessionPhase {
    Preprocessing,
    BuildingFactorBase,
    RelationCollection,
    CombiningRelations,
    MatrixConstruction,
    MatrixFiltering,
    MatrixSolving,
    FactorExtraction,
    PrimalityTesting,
    Complete,
    Failed,
}
```

### 10.3 Bounded local work

Coordinator-only phases must be incremental:

```rust
#[derive(Clone, Copy, Debug)]
pub struct LocalWorkBudget {
    pub factor_base_candidates: usize,
    pub relations_to_combine: usize,
    pub matrix_items: usize,
    pub elimination_steps: usize,
    pub primality_steps: usize,
}
```

```rust
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum AdvanceOutcome {
    Progressed,
    NeedsWorkers,
    Complete,
}
```

The WebAssembly coordinator must be able to yield back to JavaScript frequently. No local coordinator method should perform an unbounded multi-second task without returning.

---

## 11. Progress reporting

### 11.1 Principles

- Progress state is owned by `FactorSession`.
- Worker threads and Web Workers return metrics with results.
- Only the coordinator updates `ProgressSnapshot`.
- Frontends decide when and how to render progress.
- Progress snapshots must contain counters, not preformatted strings.
- Exact, estimated, and unknown totals must be distinguished.
- Phase transitions must always increment the revision.

### 11.2 Snapshot

```rust
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct ProgressSnapshot {
    pub revision: u64,
    pub task_id: u64,
    pub input_bits: usize,
    pub phase: ProgressPhase,
    pub amount: ProgressAmount,
    pub detail: ProgressDetail,
}
```

### 11.3 Generic progress amount

```rust
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ProgressAmount {
    pub completed: u64,
    pub total: ProgressTotal,
    pub unit: ProgressUnit,
}
```

```rust
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ProgressTotal {
    Exact(u64),
    Estimated(u64),
    Unknown,
}
```

```rust
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ProgressUnit {
    Candidates,
    Primes,
    Polynomials,
    SievePositions,
    Relations,
    MatrixRows,
    MatrixColumns,
    MatrixNonzeros,
    Iterations,
    MatrixProducts,
    Tasks,
}
```

`fraction()` must return `None` for unknown or zero totals. It must not clamp values above 1.0 when an estimate is exceeded.

### 11.4 Progress phases

```rust
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum ProgressPhase {
    Preprocessing,
    BuildingFactorBase,
    Sieving,
    CombiningRelations,
    BuildingMatrix,
    FilteringMatrix,
    LinearAlgebra,
    ExtractingFactor,
    PrimalityTesting,
    Complete,
}
```

### 11.5 Factor-base progress

```rust
#[derive(Clone, Copy, Debug)]
pub struct FactorBaseProgress {
    pub bound: u32,
    pub searched_through: u32,
    pub primes_tested: u64,
    pub primes_accepted: u64,
    pub nonresidue_primes: u64,
}
```

Primary meter:

```text
completed = searched_through
total = Exact(bound)
unit = Candidates
```

### 11.6 Sieving progress

```rust
#[derive(Clone, Copy, Debug)]
pub struct SievingProgress {
    pub polynomial_families_completed: u64,
    pub polynomials_completed: u64,
    pub sieve_positions_processed: u64,
    pub candidates_tested: u64,

    pub full_relations: u64,
    pub single_large_prime_relations: u64,
    pub double_large_prime_relations: u64,
    pub usable_relations: u64,
    pub target_relations: u64,

    pub active_workers: usize,
    pub outstanding_jobs: usize,
}
```

Primary meter:

```text
completed = usable_relations
total = Estimated(target_relations)
unit = Relations
```

The target may increase after filtering or rank analysis.

### 11.7 Relation-combination progress

```rust
#[derive(Clone, Copy, Debug)]
pub struct RelationProgress {
    pub partial_relations_examined: u64,
    pub partial_relations_total: u64,
    pub graph_vertices: u64,
    pub graph_edges: u64,
    pub cycles_found: u64,
    pub combined_relations: u64,
}
```

### 11.8 Matrix progress

```rust
#[derive(Clone, Copy, Debug)]
pub struct MatrixProgress {
    pub stage: MatrixStage,

    pub original_rows: u64,
    pub original_columns: u64,
    pub original_nonzeros: u64,

    pub current_rows: u64,
    pub current_columns: u64,
    pub current_nonzeros: u64,

    pub items_processed: u64,
    pub items_total: Option<u64>,

    pub singleton_rows_removed: u64,
    pub duplicate_columns_removed: u64,
    pub structured_eliminations: u64,
}
```

```rust
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum MatrixStage {
    CountingParities,
    ConstructingCsr,
    ConstructingCsc,
    RemovingSingletons,
    RemovingDuplicates,
    StructuredElimination,
    Finalizing,
}
```

### 11.9 Linear-algebra progress

```rust
#[derive(Clone, Copy, Debug)]
pub struct LinearAlgebraProgress {
    pub solver: LinearAlgebraSolver,
    pub stage: LinearAlgebraStage,

    pub matrix_rows: u64,
    pub matrix_columns: u64,
    pub matrix_nonzeros: u64,

    pub iteration: u64,
    pub estimated_iterations: Option<u64>,

    pub matrix_products_completed: u64,
    pub matrix_products_estimated: Option<u64>,

    pub dependencies_found: u32,
}
```

```rust
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum LinearAlgebraSolver {
    DenseGaussian,
    BlockLanczos,
}
```

```rust
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum LinearAlgebraStage {
    Initializing,
    Iterating,
    RecoveringDependencies,
    VerifyingDependencies,
    Complete,
}
```

### 11.10 Progress detail enum

```rust
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum ProgressDetail {
    None,
    FactorBase(FactorBaseProgress),
    Sieving(SievingProgress),
    Relations(RelationProgress),
    Matrix(MatrixProgress),
    LinearAlgebra(LinearAlgebraProgress),
    Complete(CompleteProgress),
}
```

### 11.11 Native observer contract

```rust
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ProgressAction {
    Continue,
    Cancel,
}
```

The observer:

- runs only on the caller/coordinator thread;
- is never invoked by worker threads;
- is never invoked while an internal mutex is held;
- is invoked immediately on phase changes;
- is throttled for repeated same-phase updates;
- receives one final `Complete` snapshot before success;
- can cancel by returning `ProgressAction::Cancel`.

```rust
#[derive(Clone, Copy, Debug)]
pub struct ProgressReportingConfig {
    pub minimum_interval: Duration,
}
```

A 100 ms default is appropriate on native targets.

Portable snapshots must not contain `Instant` or wall-clock timestamps. Frontends calculate rates and ETAs.

---

## 12. SIQS design

### 12.1 Configuration

```rust
#[derive(Clone, Debug)]
pub struct QsConfig {
    pub multiplier: MultiplierChoice,
    pub factor_base_bound: AutoOr<u32>,
    pub sieve_half_width: AutoOr<u32>,
    pub polynomial_batch_size: u32,
    pub large_primes: LargePrimeConfig,
    pub relation_surplus: RelationSurplus,
    pub sieve_score_scale: u8,
    pub candidate_slack: u8,
    pub matrix: MatrixConfig,
}
```

```rust
#[derive(Clone, Copy, Debug)]
pub enum AutoOr<T> {
    Auto,
    Value(T),
}
```

```rust
#[derive(Clone, Debug)]
pub struct LargePrimeConfig {
    pub single_limit: AutoOr<u64>,
    pub double_product_limit: AutoOr<u64>,
    pub enable_double: bool,
}
```

```rust
#[derive(Clone, Copy, Debug)]
pub struct RelationSurplus {
    pub absolute: usize,
    pub percent: u8,
}
```

Parameter heuristics must live in a versioned module such as `qs/parameters.rs`, not be scattered throughout the implementation.

### 12.2 Multiplier selection

Implement a small multiplier search that scores candidate square-free multipliers using:

- resulting residue properties modulo small primes;
- expected factor-base density;
- bit-length growth penalty;
- special handling of powers of two.

If a candidate multiplier shares a nontrivial GCD with `n`, return that GCD immediately as a factor.

### 12.3 Factor base

Do not use a fixed prime table as the complete factor base.

Requirements:

- Embed a compact small-prime table for trial division and bootstrap prime generation.
- Dynamically generate primes up to the selected factor-base bound.
- For each odd prime `p`:
  - if `n mod p == 0`, return `p` as a factor;
  - otherwise retain `p` only if the working value is a quadratic residue modulo `p`;
  - compute and store a modular square root.
- Handle `p = 2` separately.

```rust
pub struct FactorBaseEntry {
    pub prime: u32,
    pub log_prime: u8,
    pub sqrt_n: u32,
}
```

```rust
pub struct FactorBase {
    entries: Box<[FactorBaseEntry]>,
}
```

Factor-base construction must be incremental:

```rust
pub struct FactorBaseBuilder<const P: usize> {
    // private state
}
```

```rust
impl<const P: usize> FactorBaseBuilder<P> {
    pub fn new(
        n: Natural<P>,
        bound: u32,
    ) -> Result<Self, FactorBaseError>;

    pub fn step(
        &mut self,
        candidate_budget: usize,
    ) -> Result<FactorBaseBuildStatus, FactorBaseError>;

    pub fn progress(&self) -> FactorBaseProgress;
    pub fn finish(self) -> Result<FactorBase, FactorBaseError>;
}
```

### 12.4 SIQS context

```rust
pub struct SieveContext<const P: usize> {
    pub(crate) n: Natural<P>,
    pub(crate) working_n: Natural<P>,
    pub(crate) multiplier: u32,
    pub(crate) factor_base: FactorBase,
    pub(crate) polynomial_plan: PolynomialPlan,
    pub(crate) config: QsConfig,
    pub(crate) context_id: u64,
}
```

Preparation API:

```rust
pub fn prepare_siqs<const P: usize>(
    n: &Natural<P>,
    config: &QsConfig,
) -> Result<SieveContext<P>, QsError>;
```

### 12.5 Polynomial generation

Use SIQS polynomial families. Polynomial identifiers must deterministically derive all coefficients from:

- input;
- factor base;
- configured seed;
- family number;
- polynomial index.

Workers must not make independent random choices.

The polynomial representation must support efficient root updates between related polynomials.

### 12.6 Sieving

Use a byte score array:

```rust
pub struct SieveScratch {
    scores: Vec<u8>,
    candidates: Vec<u32>,
    factor_scratch: Vec<(u32, u16)>,
    bucket_storage: Vec<BucketEntry>,
}
```

For each polynomial:

1. Initialize or approximate `log |Q(x)|` scores.
2. Add scaled `log(p)` at both modular roots.
3. Include selected prime-power contributions where profitable.
4. Use direct stepping for small factor-base primes.
5. Use bucket sieving for large-stride primes.
6. Scan candidates above a configurable threshold.
7. Trial-divide candidate values.
8. Classify full, single-large-prime, and double-large-prime relations.
9. Emit metrics and relations.

Do not begin with handwritten SIMD. First implement a correct scalar path with data layouts friendly to auto-vectorization. Add target-specific optimizations only behind dispatch after benchmarks identify real hot spots.

### 12.7 Relation invariant

```rust
pub struct RawRelation<const P: usize> {
    pub square_root: Natural<P>,
    pub sign: bool,
    pub factors: Box<[PrimePower]>,
    pub large_primes: LargePrimePart,
    pub source: RelationSource,
}
```

```rust
pub struct PrimePower {
    pub factor_base_index: u32,
    pub exponent: u16,
}
```

```rust
pub enum LargePrimePart {
    None,
    One(u64),
    Two(u64, u64),
}
```

Every relation must satisfy:

```text
square_root^2 congruent to
    (-1)^sign
    * product(factor_base[index]^exponent)
    * product(large_primes)
mod n
```

Provide:

```rust
pub fn verify_relation<const P: usize>(
    context: &SieveContext<P>,
    relation: &RawRelation<P>,
) -> bool;
```

Enable verification in tests and under the `relation-checks` feature.

### 12.8 Large-prime relation combination

Use a graph:

- vertices are large primes;
- one-large-prime relations connect a distinguished root vertex to the prime;
- two-large-prime relations connect their two primes;
- cycles create combinations in which every large-prime exponent is even.

Preserve provenance rather than immediately multiplying all values.

```rust
pub struct CombinedRelation {
    pub sources: Box<[RelationId]>,
    pub parity: SparseParity,
}
```

Resource-limit graph growth and partial-relation retention.

### 12.9 Relation collection stopping rule

The collection target must be based on:

- matrix row count;
- configured absolute and percentage surplus;
- observed filtering losses;
- any rank deficiencies found later.

If filtering or linear algebra reveals insufficient independent relations, the session must return to relation collection with a higher estimated target and a new work generation.

---

## 13. Parallel work-unit API

### 13.1 Job identity

```rust
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct JobHeader {
    pub job_id: u64,
    pub generation: u64,
    pub context_id: u64,
}
```

- `job_id` uniquely identifies a job within the session.
- `generation` identifies the current scheduling generation.
- `context_id` identifies the immutable worker context.
- Late results from obsolete generations must be ignored safely.

### 13.2 Jobs

```rust
#[derive(Clone, Debug)]
pub enum WorkJob {
    Sieve(SieveJob),
    MatrixMultiply(MatrixMultiplyJob),
}
```

```rust
#[derive(Clone, Debug)]
pub struct SieveJob {
    pub header: JobHeader,
    pub family: u64,
    pub first_polynomial: u32,
    pub polynomial_count: u32,
}
```

```rust
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum MatrixOperation {
    Matrix,
    Transpose,
}
```

```rust
#[derive(Clone, Debug)]
pub struct MatrixMultiplyJob {
    pub header: JobHeader,
    pub operation: MatrixOperation,
    pub output_start: u32,
    pub output_end: u32,
    pub input: Box<[u64]>,
}
```

### 13.3 Results and metrics

```rust
#[derive(Debug)]
pub enum WorkResult {
    Sieve(SieveResult),
    MatrixMultiply(MatrixMultiplyResult),
    Failed(WorkFailure),
}
```

```rust
#[derive(Clone, Copy, Debug, Default)]
pub struct SieveJobMetrics {
    pub polynomial_families: u64,
    pub polynomials: u64,
    pub sieve_positions: u64,
    pub candidates_tested: u64,
    pub full_relations: u64,
    pub single_large_prime_relations: u64,
    pub double_large_prime_relations: u64,
}
```

```rust
pub struct SieveResult<const P: usize = 16> {
    pub header: JobHeader,
    pub relations: Vec<RawRelation<P>>,
    pub metrics: SieveJobMetrics,
}
```

```rust
#[derive(Clone, Copy, Debug, Default)]
pub struct MatrixJobMetrics {
    pub output_items: u64,
    pub nonzeros_visited: u64,
}
```

```rust
pub struct MatrixMultiplyResult {
    pub header: JobHeader,
    pub output_start: u32,
    pub words: Box<[u64]>,
    pub metrics: MatrixJobMetrics,
}
```

### 13.4 Worker kernel

```rust
pub struct WorkerScratch {
    pub sieve: SieveScratch,
    pub matrix: MatrixScratch,
    pub arithmetic: ArithmeticScratch,
}
```

```rust
pub fn execute_job<const P: usize>(
    contexts: &KernelContexts<P>,
    job: WorkJob,
    scratch: &mut WorkerScratch,
) -> Result<WorkResult, KernelError>;
```

Native worker threads and Wasm worker exports must call this function or its direct sub-kernels.

---

## 14. Native parallel driver

### 14.1 Thread pool

Create long-lived worker threads once per factorization call or session driver.

Do not spawn a thread for each polynomial batch or matrix multiplication.

```rust
struct NativePool {
    workers: Vec<Worker>,
    command_tx: Sender<WorkerCommand>,
    result_rx: Receiver<WorkResult>,
    outstanding: usize,
}
```

Each worker owns `WorkerScratch` and reuses allocated memory.

### 14.2 Driver loop

The blocking driver must:

1. Create the session.
2. Resolve parallelism.
3. Create the worker pool.
4. Run bounded local coordinator work.
5. Drain completed results before blocking.
6. Request jobs up to available worker capacity.
7. Dispatch jobs.
8. Submit results to the session.
9. Render or publish progress on the coordinator thread.
10. Stop workers and return factors or an error.

Callbacks must never run while holding pool or session locks.

### 14.3 Cancellation

Cancellation may come from:

- `ProgressAction::Cancel`;
- resource-limit exhaustion;
- a native deadline implemented by the driver;
- explicit future session APIs.

When cancelled:

- stop creating jobs;
- signal workers to stop after the current bounded job;
- drain or discard late results;
- return `FactorError::Cancelled`.

---

## 15. Sparse linear algebra over `F_2`

### 15.1 Matrix definition

Rows:

- row 0 represents the sign `-1`;
- one row per factor-base prime.

Columns:

- one per usable full or combined relation.

A matrix bit is 1 when the corresponding exponent is odd.

### 15.2 Storage

```rust
pub struct SparseBinaryMatrix {
    rows: u32,
    columns: u32,

    csr_offsets: Box<[u32]>,
    csr_columns: Box<[u32]>,

    csc_offsets: Box<[u32]>,
    csc_rows: Box<[u32]>,

    provenance: Box<[CombinationId]>,
}
```

Maintain both CSR and CSC forms to support efficient `M*x` and `M^T*x` kernels.

Validate index sizes before converting from `usize` to `u32`.

### 15.3 Filtering

Perform, at minimum:

1. Remove zero columns.
2. Repeatedly remove singleton rows and their only columns.
3. Remove duplicate columns.
4. Optionally perform bounded structured elimination on low-weight rows.
5. Preserve provenance for every transformed column.

Use a provenance DAG:

```rust
enum CombinationNode {
    Raw(RelationId),
    Xor(CombinationId, CombinationId),
}
```

Do not store a full original-relation bit vector for every intermediate column.

### 15.4 Block vectors

Represent 64 parallel binary vectors with one `u64` per matrix position:

```rust
pub struct F2BlockVector {
    words: Box<[u64]>,
}
```

### 15.5 Sparse multiplication

```rust
impl SparseBinaryMatrix {
    pub fn mul_m_rows(
        &self,
        input_by_column: &[u64],
        row_range: Range<usize>,
        output_by_row: &mut [u64],
    );

    pub fn mul_mt_columns(
        &self,
        input_by_row: &[u64],
        column_range: Range<usize>,
        output_by_column: &mut [u64],
    );
}
```

Partition by disjoint output ranges. Workers must not need atomics for output accumulation.

### 15.6 Solvers

```rust
#[derive(Clone, Copy, Debug)]
pub enum MatrixSolver {
    Auto,
    DenseGaussian,
    BlockLanczos,
}
```

Dense Gaussian elimination is required for:

- small matrices;
- tests;
- reference behavior;
- debugging block Lanczos.

Production sparse matrices should use block Lanczos.

### 15.7 Block Lanczos state machine

```rust
pub struct BlockLanczos {
    // private state
}
```

```rust
pub enum LanczosRequest<'a> {
    MultiplyM { input: &'a [u64] },
    MultiplyMt { input: &'a [u64] },
    Complete,
}
```

```rust
impl BlockLanczos {
    pub fn begin(matrix: &SparseBinaryMatrix) -> Self;
    pub fn request(&self) -> LanczosRequest<'_>;

    pub fn submit_product(
        &mut self,
        product: &[u64],
    ) -> Result<LanczosProgress, LinearAlgebraError>;

    pub fn dependencies(&self) -> Option<&DependencySet>;
}
```

Implement a published, mathematically sound block-Lanczos recurrence. Do not invent an informal variation.

Every returned dependency must be checked by multiplying it through the original parity matrix.

### 15.8 Factor extraction

For each verified dependency:

1. Reconstruct the selected original relations through provenance.
2. Compute the congruence of squares.
3. Form `x` and `y` modulo `n`.
4. Try `gcd(|x-y|, n)` and `gcd(x+y, n)`.
5. Reject trivial factors 1 and `n`.
6. Continue with more dependencies if necessary.

Use exact or modular arithmetic that cannot silently wrap incorrectly.

---

## 16. WebAssembly architecture

### 16.1 General model

The portable baseline uses independent Wasm instances:

```text
main page
  └── coordinator worker
        ├── compute worker 0
        ├── compute worker 1
        ├── ...
        └── compute worker N
```

Each compute worker:

- instantiates the same `.wasm` module;
- has independent linear memory;
- imports an immutable phase context;
- receives serialized jobs;
- executes the common Rust kernel;
- returns serialized results.

The coordinator worker:

- owns `FactorSession<16>`;
- performs bounded local work;
- exports worker contexts;
- schedules jobs;
- merges results;
- publishes progress;
- returns factors.

### 16.2 Wasm capacity

The raw ABI uses one concrete type:

```rust
pub(crate) const WASM_PARTS_64: usize = 16;
pub(crate) type WasmNatural = Natural<WASM_PARTS_64>;
```

The Rust library API remains generic.

### 16.3 No Rust threads on Wasm

Do not call `std::thread::spawn` on `wasm32-unknown-unknown`.

JavaScript creates Web Workers and selects concurrency using:

```javascript
const parallelism = Math.max(
  1,
  Math.min(
    options.parallelism ?? Number.MAX_SAFE_INTEGER,
    navigator.hardwareConcurrency || 1,
  ),
);
```

Treat this value as a hint and allow an explicit cap.

### 16.4 Raw ABI requirements

- Use only `extern "C"` exports.
- Use Rust 2024 `#[unsafe(no_mangle)]` syntax.
- Use `u32`, `i32`, and carefully documented `u64` values.
- Do not expose Rust structs, slices, `Vec`, `BTreeMap`, or enum layouts.
- Use opaque handles and serialized packets.
- Validate every pointer, length, handle, version, and index.
- Never trust JavaScript-provided memory ranges.

### 16.5 Memory and buffers

Required exports:

```rust
#[unsafe(no_mangle)]
pub extern "C" fn qs_abi_version() -> u32;

#[unsafe(no_mangle)]
pub extern "C" fn qs_alloc(size: u32, align: u32) -> u32;

#[unsafe(no_mangle)]
pub extern "C" fn qs_dealloc(
    pointer: u32,
    size: u32,
    align: u32,
);

#[unsafe(no_mangle)]
pub extern "C" fn qs_buffer_pointer(handle: u32) -> u32;

#[unsafe(no_mangle)]
pub extern "C" fn qs_buffer_length(handle: u32) -> u32;

#[unsafe(no_mangle)]
pub extern "C" fn qs_buffer_free(handle: u32);
```

After an export that may grow Wasm memory, JavaScript must recreate typed-array views before reading data.

### 16.6 Session ABI

```rust
#[unsafe(no_mangle)]
pub extern "C" fn qs_session_new(
    input_pointer: u32,
    input_length: u32,
    config_pointer: u32,
    config_length: u32,
) -> u32;

#[unsafe(no_mangle)]
pub extern "C" fn qs_session_free(session: u32);

#[unsafe(no_mangle)]
pub extern "C" fn qs_session_phase(session: u32) -> u32;

#[unsafe(no_mangle)]
pub extern "C" fn qs_session_advance_local(
    session: u32,
    budget_pointer: u32,
    budget_length: u32,
) -> i32;

#[unsafe(no_mangle)]
pub extern "C" fn qs_session_export_context(session: u32) -> u32;

#[unsafe(no_mangle)]
pub extern "C" fn qs_session_take_jobs(
    session: u32,
    maximum_jobs: u32,
) -> u32;

#[unsafe(no_mangle)]
pub extern "C" fn qs_session_submit(
    session: u32,
    result_pointer: u32,
    result_length: u32,
) -> i32;

#[unsafe(no_mangle)]
pub extern "C" fn qs_session_take_factors(session: u32) -> u32;

#[unsafe(no_mangle)]
pub extern "C" fn qs_session_error(session: u32) -> u32;

#[unsafe(no_mangle)]
pub extern "C" fn qs_session_progress(session: u32) -> u32;
```

A returned `u32` is an object or buffer handle unless explicitly documented otherwise.

### 16.7 Worker ABI

```rust
#[unsafe(no_mangle)]
pub extern "C" fn qs_worker_context_import(
    pointer: u32,
    length: u32,
) -> u32;

#[unsafe(no_mangle)]
pub extern "C" fn qs_worker_context_free(context: u32);

#[unsafe(no_mangle)]
pub extern "C" fn qs_worker_execute(
    context: u32,
    job_pointer: u32,
    job_length: u32,
) -> u32;
```

The worker ABI must only decode, validate, dispatch, serialize, and return. It must contain no independent factorization policy.

### 16.8 Handle registry

Use generation-checked handles.

```rust
struct Slot<T> {
    generation: u16,
    value: Option<T>,
}
```

Pack generation and slot index into `u32` where practical. Keep typed registries for sessions, contexts, and buffers to prevent type confusion.

### 16.9 Wire format

Every packet begins with:

```rust
#[repr(C)]
struct PacketHeader {
    magic: [u8; 4],
    kind: u16,
    version: u16,
    payload_length: u32,
}
```

Use magic `b"QSV1"` for ABI version 1.

Rules:

- all multibyte fields are little-endian;
- no unaligned Rust pointer casting;
- decode field by field;
- validate payload length and offset arithmetic;
- reject unknown mandatory enum values;
- allow forward-compatible optional trailing data only when version rules explicitly permit it;
- define maximum packet sizes.

### 16.10 JavaScript API

`js/index.js` should expose a high-level function:

```javascript
export async function factor(input, options = {})
```

Accepted input should include decimal strings and byte arrays.

Return a structured result preserving multiplicity, for example:

```javascript
{
  factors: ["2", "2", "3", "5"],
  grouped: [
    { prime: "2", exponent: 2 },
    { prime: "3", exponent: 1 },
    { prime: "5", exponent: 1 },
  ],
}
```

Support:

- `options.parallelism`;
- `options.onProgress(snapshot)`;
- cancellation through `AbortSignal`;
- resource-limit configuration;
- deterministic seed configuration.

The JS coordinator must call progress updates:

- after bounded local work;
- after each submitted worker result;
- periodically while jobs are outstanding.

---

## 17. CLI specification

### 17.1 Behavior

Binary name:

```text
qs-factor
```

The CLI:

1. Reads all of standard input as UTF-8 text.
2. Trims surrounding whitespace.
3. Requires exactly one unsigned decimal integer.
4. Parses it as `Natural<16>`.
5. Factors it using the native blocking API.
6. Prints prime factors in ascending order, repeated according to multiplicity, one per line.
7. Writes progress only to standard error.
8. Writes errors only to standard error.
9. Returns exit status 0 on success and nonzero on failure.

Example:

Input:

```text
360
```

Output on stdout:

```text
2
2
2
3
3
5
```

### 17.2 Edge cases

- Input `0`: error and nonzero exit.
- Input `1`: success with no stdout output.
- Prime input: one line.
- Prime power: repeated identical lines.
- Leading and trailing whitespace: accepted.
- Internal whitespace or multiple numbers: rejected.
- `+123`, `-123`, and `1_000`: rejected.
- Values above 1024 bits: rejected with a clear message.

### 17.3 Progress mode

Support:

```text
--progress auto|always|never
```

Default: `auto`.

- `auto`: show progress only if stderr is a terminal.
- `always`: always show progress.
- `never`: never show progress.

Use `std::io::IsTerminal`.

Do not require a heavy CLI parsing dependency unless justified. A small manual parser is acceptable.

### 17.4 Progress rendering

Progress output must remain on stderr so stdout is machine-readable.

Suggested forms:

```text
building factor base  [================>----------]  62.4%
                      31,842 primes tested, 15,911 accepted
```

```text
sieving               [=============>-------------]  9,421/~15,250 relations
                      48,192 polynomials, 3,811 partials, 16 workers
```

```text
linear algebra        [=========>-----------------]  iteration 118/~275
                      14,208 x 15,463, 1,184,992 nonzeros
```

For unknown totals, use a spinner and counters rather than a fake percentage.

The CLI may calculate elapsed time, throughput, and ETA from snapshot deltas, but these values do not belong in the portable core snapshot.

### 17.5 Terminal hygiene

- Avoid progress control sequences when stderr is not a terminal unless `always` is selected.
- Finish or clear the current progress line before printing an error.
- Finish progress rendering before printing factors.
- Handle broken pipe on stdout gracefully where practical.

---

## 18. Errors

```rust
#[derive(Debug)]
#[non_exhaustive]
pub enum FactorError {
    ZeroHasNoPrimeFactorization,
    CapacityExceeded,
    ResourceLimit(ResourceLimitKind),
    NoNontrivialFactor,
    InsufficientRelations,
    LinearAlgebra(LinearAlgebraError),
    InvalidRelation,
    InvalidDependency,
    Cancelled,
    Stalled,
    InternalInvariant(&'static str),
}
```

All public error types should implement `Display` and `std::error::Error` when `std` is available.

Use typed errors for parsing, arithmetic capacity, SIQS setup, worker decoding, matrix construction, and ABI validation.

Do not use panics for ordinary malformed input, resource exhaustion, or unlucky factorization failure.

Panics are reserved for internal programming errors and operator semantics such as division by zero.

---

## 19. Architecture-specific optimization

### 19.1 Portable baseline

The complete crate must work correctly without architecture-specific features.

### 19.2 Dispatch structure

```rust
mod arch {
    mod portable;

    #[cfg(all(feature = "arch-optimized", target_arch = "x86_64"))]
    mod x86_64;

    #[cfg(all(feature = "arch-optimized", target_arch = "aarch64"))]
    mod aarch64;

    #[cfg(all(feature = "wasm-simd128", target_arch = "wasm32"))]
    mod wasm32;
}
```

### 19.3 Candidate optimizations

Only implement after profiling:

- x86-64 BMI2/ADX Montgomery multiplication;
- AVX2 or AVX-512 XOR and score scanning;
- AArch64 carry-chain and NEON kernels;
- Wasm `simd128` score scanning and XOR;
- cache-blocked sparse matrix traversal;
- improved bucket-sieve layouts;
- prefetching for sparse products.

Every optimized path must have randomized differential tests against the portable implementation.

Runtime feature detection is required where one native binary supports multiple CPU generations.

---

## 20. Determinism

With identical:

- input;
- configuration;
- seed;
- parallelism-independent job generation policy;

The implementation should produce deterministic mathematical results and deterministic job contents.

Relation arrival order may vary under parallel execution. Therefore:

- assign deterministic relation IDs at coordinator acceptance time or canonicalize relation ordering before matrix construction;
- sort or canonicalize partial-relation graph inputs where needed;
- ensure tests do not depend on nondeterministic thread completion order;
- ensure returned prime factors are always ordered through `BTreeMap`.

The exact progress timing need not be deterministic.

---

## 21. Memory management

### 21.1 Reuse

- Reuse sieve arrays per worker.
- Reuse candidate buffers.
- Reuse arithmetic scratch buffers.
- Reuse block-Lanczos vectors.
- Avoid per-prime heap allocation.
- Batch relation submissions to reduce synchronization overhead.

### 21.2 Limits

Before large allocations:

- compute sizes with checked arithmetic;
- enforce configured memory limits;
- reject matrix dimensions exceeding index representation;
- avoid attempting allocations that obviously exceed addressable Wasm memory.

### 21.3 Relation storage

Store parity sparsely and full exponents compactly. Avoid storing redundant large `Natural` values when a relation can be represented by polynomial identity plus a modular square-root contribution.

Preserve enough information to reconstruct the congruence of squares exactly.

---

## 22. Testing requirements

### 22.1 `Natural`

Test:

- exhaustive `Natural<1>` arithmetic against `u64` where feasible;
- randomized `Natural<2>` through `Natural<8>` against `num-bigint` in dev-dependencies;
- carry and borrow chains;
- all-`u64::MAX` limbs;
- multiplication and squaring;
- normalized division edge cases;
- decimal round trips;
- byte-order round trips;
- shifts around 0, 63, 64, capacity-1, capacity, and above capacity;
- GCD and extended GCD identities;
- square root and remainder;
- perfect-power detection;
- Montgomery multiplication and exponentiation;
- exact overflow flags.

`num-bigint` may be a dev-dependency only and must not be used by the production implementation.

### 22.2 Primality

Test:

- values 0 through a large small range against a simple sieve;
- primes and composites near limb boundaries;
- Carmichael numbers;
- strong pseudoprimes to early bases;
- deterministic seeded witness generation;
- known large primes and composites.

### 22.3 Relations

For every generated test relation, verify the relation invariant modulo `n`.

Create deterministic fixtures containing:

- `n`;
- multiplier;
- factor base;
- polynomial identifier;
- coefficients;
- modular roots;
- candidate positions;
- factorizations;
- emitted relations.

Native and Wasm worker kernels should produce byte-identical serialized results for the same fixture where architecture-independent serialization is expected.

### 22.4 Partial relation graph

Test:

- one-large-prime pairing;
- double-large-prime cycles;
- duplicate edges;
- disconnected components;
- cycle provenance;
- resource limits;
- deterministic combination results.

### 22.5 Matrix

Generate random sparse matrices and verify:

```text
M * dependency = 0
```

Compare dense Gaussian and block Lanczos on small matrices.

Include pathological matrices:

- zero rows;
- zero columns;
- duplicate columns;
- all singleton rows;
- rank zero;
- rank one;
- rectangular matrices;
- dimensions 63, 64, and 65;
- extremely sparse rows;
- dense small matrices.

### 22.6 End-to-end factorization

Test:

- prime inputs;
- products of two similarly sized primes;
- highly unbalanced semiprimes;
- repeated prime powers;
- perfect squares;
- Carmichael numbers;
- numbers with many tiny factors;
- values near capacity boundaries;
- `0`, `1`, and `2`;
- deterministic results across parallelism settings.

For every success:

1. Verify every returned factor is probable prime under the configured policy.
2. Multiply factors with exponents using checked/widening arithmetic.
3. Verify exact equality with the original input.

### 22.7 Progress tests

Test:

- revision monotonically increases;
- phase transitions occur in valid order;
- exact totals are never contradicted;
- estimated totals may be raised;
- stale job results do not affect progress;
- cancellation through observer works;
- completion snapshot is emitted exactly once;
- callbacks execute only on the coordinator thread.

### 22.8 CLI tests

Integration tests must cover:

- factorization of 360;
- prime input;
- input 1;
- input 0;
- invalid characters;
- multiple numbers;
- overflow;
- progress disabled when redirected in auto mode;
- progress never contaminates stdout;
- `--progress never` and `--progress always`.

### 22.9 Wasm tests

Provide browser or Node-compatible tests for:

- ABI version;
- allocation and buffer lifetime;
- invalid handles;
- stale handles;
- malformed packets;
- session creation;
- context import;
- worker execution;
- progress packet decoding;
- multi-worker factorization;
- cancellation;
- deterministic result grouping.

---

## 23. Benchmarks

Add benchmarks for:

### 23.1 Arithmetic

- addition and subtraction by limb count;
- schoolbook multiplication;
- squaring;
- division;
- Montgomery multiplication;
- modular exponentiation;
- GCD.

### 23.2 SIQS

- factor-base construction;
- root generation;
- direct sieve stepping;
- bucket sieve;
- candidate scanning;
- candidate trial division;
- relation serialization.

### 23.3 Linear algebra

- CSR `M*x`;
- CSC `M^T*x`;
- filtering;
- dense elimination;
- block-Lanczos iteration.

### 23.4 End-to-end

Use a curated set of composites with increasing difficulty. Record:

- total time;
- relation collection time;
- matrix time;
- memory use where measurable;
- scaling from 1 to available cores.

Benchmarks must not become correctness tests with fragile timing assertions.

---

## 24. Documentation

The crate documentation and README must include:

- what SIQS is;
- practical limitations;
- the distinction between storage capacity and feasible factorization size;
- native usage;
- progress callback usage;
- CLI usage;
- raw Wasm build instructions;
- JS worker usage;
- deterministic seed behavior;
- probable-prime semantics;
- low-level scheduler API;
- safety and resource-limit notes.

Every public item must have rustdoc.

Include at least these examples:

### 24.1 Native factorization

```rust
use rusqsieve::{factor, Natural};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let n: Natural<16> =
        "966680312498850986629904881784491804947363701071".parse()?;

    let factors = factor(n)?;

    for (prime, exponent) in factors.iter() {
        println!("{prime}^{exponent}");
    }

    Ok(())
}
```

### 24.2 Progress observer

```rust
use rusqsieve::{
    factor_with_progress,
    FactorConfig,
    Natural,
    ProgressAction,
};

let n: Natural<16> = "360".parse()?;

let factors = factor_with_progress(
    n,
    FactorConfig::default(),
    |snapshot| {
        eprintln!("{:?}: {:?}", snapshot.phase, snapshot.amount);
        ProgressAction::Continue
    },
)?;
```

### 24.3 Compile-time literal

```rust
use rusqsieve::{natural, Natural};

const RSA_160: Natural<16> = natural!(
    "966680312498850986629904881784491804947363701071",
    16
);
```

---

## 25. Safety requirements

- Use `#![forbid(unsafe_op_in_unsafe_fn)]`.
- Keep unsafe code isolated in small architecture or ABI modules.
- Document every unsafe block with a concrete safety argument.
- No unsafe code is needed for portable arithmetic.
- Wasm pointer validation must use checked arithmetic and memory bounds.
- Architecture-specific intrinsics must be gated by compile-time or runtime feature checks.
- Do not use `transmute` for wire decoding.
- Do not create references from untrusted Wasm pointers until bounds and alignment are validated.

---

## 26. Code-quality requirements

- Format with `rustfmt`.
- Pass `cargo clippy --all-targets --all-features` with no unjustified warnings.
- Avoid giant functions; separate policy, math kernels, scheduling, and serialization.
- Use explicit typed state machines rather than boolean flag collections.
- Avoid hidden global mutable state.
- No random behavior without an explicit deterministic seed source.
- No terminal or JS concerns inside mathematical modules.
- No threading code inside SIQS or linear-algebra kernels.
- No platform-specific code in public mathematical types.
- Keep dependencies minimal and justified.

Suggested production dependencies: ideally none initially. Development dependencies may include property-testing, benchmarking, and a reference bigint implementation.

---

## 27. Implementation sequence

Codex should implement in the following order and keep the crate compiling after each phase.

### Phase 1: crate skeleton

- Cargo configuration.
- Module layout.
- Public error and config stubs.
- Native and Wasm cfg gates.
- CLI target skeleton.

Acceptance:

- native `cargo check` passes;
- Wasm `cargo check --target wasm32-unknown-unknown --lib --no-default-features` passes.

### Phase 2: `Natural`

- representation;
- parsing;
- formatting;
- comparison;
- bit operations;
- add/subtract;
- shifts;
- widening multiply and square;
- division;
- GCD;
- square root;
- perfect powers;
- Montgomery arithmetic.

Acceptance:

- property tests against reference bigint pass;
- const literal macro works;
- no production bigint dependency.

### Phase 3: primality and result type

- Miller–Rabin;
- deterministic seeded witnesses;
- `PrimeFactors`;
- product verification.

Acceptance:

- pseudoprime and Carmichael tests pass.

### Phase 4: high-level preprocessing

- trial division;
- perfect powers;
- optional Pollard rho;
- recursive factor tree;
- session state machine skeleton;
- progress snapshots.

Acceptance:

- easy composites factor without SIQS;
- progress and cancellation tests pass.

### Phase 5: reference QS

Behind `reference-qs`:

- simple polynomial QS;
- full relations only;
- dense elimination;
- serial execution.

Acceptance:

- small semiprimes factor;
- relations verify.

### Phase 6: SIQS relation collection

- multiplier selection;
- dynamic factor base;
- SIQS polynomial families;
- logarithmic sieving;
- deterministic sieve jobs;
- single-large-prime support;
- relation metrics.

Acceptance:

- serial SIQS factors moderate fixtures;
- relation invariants pass.

### Phase 7: native parallelism

- persistent thread pool;
- job scheduling;
- reusable worker scratch;
- coordinator-only progress callback;
- cancellation.

Acceptance:

- factors are identical for parallelism 1 and N;
- no callback executes on a worker thread;
- speedup is measurable on suitable fixtures.

### Phase 8: double-large-prime graph and matrix filtering

- partial graph;
- cycle extraction;
- sparse parity matrix;
- CSR and CSC;
- singleton filtering;
- duplicate removal;
- provenance DAG.

Acceptance:

- combined relations verify;
- filtered dependency reconstruction is correct.

### Phase 9: block Lanczos

- serial state machine;
- dense oracle comparisons;
- dependency verification;
- factor extraction;
- parallel sparse products.

Acceptance:

- random sparse matrix tests pass;
- end-to-end SIQS succeeds with block Lanczos.

### Phase 10: Wasm ABI and JS glue

- raw ABI;
- generation-checked handles;
- packet codec;
- coordinator session exports;
- worker context and execution exports;
- JS worker pool;
- progress callback;
- cancellation.

Acceptance:

- browser/Node integration tests factor the same fixtures as native;
- malformed packets fail safely;
- multi-worker execution works.

### Phase 11: CLI

- stdin parser;
- factor output;
- progress modes;
- terminal rendering;
- integration tests.

Acceptance:

```sh
printf '360\n' | qs-factor --progress never
```

prints exactly:

```text
2
2
2
3
3
5
```

### Phase 12: optimization

- benchmark;
- profile;
- optimize demonstrated bottlenecks;
- add optional architecture-specific paths;
- retain portable differential tests.

---

## 28. Final acceptance criteria

The implementation is complete when all of the following hold:

1. One Cargo package builds as an `rlib` on native and a raw `cdylib` Wasm module.
2. Native builds expose blocking factorization APIs.
3. `wasm32-unknown-unknown` builds expose only the intended raw ABI and do not attempt Rust thread creation.
4. `Natural<16>` correctly stores and operates on 1024-bit unsigned integers.
5. Compile-time decimal literals reject invalid or overflowing input.
6. High-level factorization returns probable-prime factors in a `BTreeMap` wrapper.
7. Repeated factors are represented through nonzero exponents.
8. SIQS relation collection can run serially or as deterministic parallel jobs.
9. Native execution uses persistent worker threads up to configured parallelism.
10. JavaScript creates independent Web Workers and executes common Rust worker kernels.
11. Sparse matrix construction and filtering preserve relation provenance.
12. Dense elimination works for small matrices.
13. Block Lanczos works for production sparse matrices.
14. Every accepted relation and dependency can be verified.
15. Progress snapshots cover factor-base construction, sieving, relation processing, matrix construction/filtering, and linear algebra.
16. Native progress callbacks run only on the coordinator thread.
17. The CLI prints only factors on stdout and progress/errors on stderr.
18. Input `1` succeeds with empty factor output; input `0` fails.
19. Native and Wasm factorizations produce equivalent grouped prime factors.
20. Tests, clippy, formatting, and documentation checks pass.

---

## 29. Instructions to Codex

Implement the full crate described above, not a toy demonstration.

Important constraints:

- Do not replace the custom `Natural` type with `num-bigint` or another production bigint dependency.
- Do not replace SIQS with trial division or Pollard rho alone.
- Do not use Tokio.
- Do not use `wasm-bindgen`.
- Do not create Rust threads on `wasm32-unknown-unknown`.
- Do not split the implementation into multiple Cargo packages.
- Do not expose Rust object layouts through the Wasm ABI.
- Do not omit relation verification, provenance, or dependency verification.
- Do not fake progress percentages when totals are unknown.
- Do not call user progress observers from worker threads.
- Do not silently use wrapping arithmetic in exact mathematical code.
- Do not add architecture-specific unsafe code before the portable implementation and tests are complete.

When a full optimized implementation cannot be completed in one pass, preserve this architecture and implement coherent compiling phases rather than substituting simplified incompatible APIs.

At each phase:

1. Keep native and Wasm library builds compiling.
2. Add tests before moving to the next subsystem.
3. Document invariants in code.
4. Prefer correctness and verifiability over premature micro-optimization.
5. Leave clear `TODO` markers only for optimizations, not for required correctness behavior.