This file is indexed.

/usr/share/singular/LIB/crypto.lib is in singular-data 4.0.3+ds-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
//////////////////////////////////////////////////////////////////////////////
version="version crypto.lib 4.0.0.1 Aug_2014 "; // $Id: dfb2cb269d4d1328eba1d57527e2e2fafd159acf $
category="Teaching";
info="
LIBRARY:  crypto.lib     Procedures for teaching cryptography
AUTHORS:                 Gerhard Pfister, pfister@mathematik.uni-kl.de
@*                       David Brittinger, dativ@gmx.net

OVERVIEW:
     The library contains procedures to compute the discrete logarithm,
      primality-tests, factorization included elliptic curves.
      The library is intended to be used for teaching purposes but not
      for serious computations. Sufficiently high printlevel allows to
      control each step, thus illustrating the algorithms at work.


PROCEDURES:
 decimal(s);                number corresponding to the hexadecimal number s
 eexgcdN(L)                 T with sum_i L[i]*T[i]=T[n+1]=gcd(L[1],...,L[n])
 lcmN(a,b)                  compute lcm(a,b)
 powerN(m,d,n)              compute m^d mod n
 chineseRem(T,L)            compute x such that x = T[i] mod L[i]
 Jacobi(a,n)                the generalized Legendre symbol of a and n
 primList(n)                the list of all primes <=n
 primL(q)                   first primes p_1,...,p_r such that q<p_1*...*p_r
 intPart(x)                 the integral part of a rational number
 intRoot(m)                 the integral part of the square root of m
 squareRoot(a,p)            the square root of a in Z/p, p prime
 solutionsMod2(M)           basis solutions of Mx=0 over Z/2
 powerX(q,i,I)              q-th power of the i-th variable modulo I
 babyGiant(b,y,p)           discrete logarithm x: b^x=y mod p
 rho(b,y,p)                 discrete logarithm x: b^x=y mod p
 MillerRabin(n,k)           probabilistic primaly-test of Miller-Rabin
 SolowayStrassen(n,k)       probabilistic primaly-test of Soloway-Strassen
 PocklingtonLehmer(N,[])    primaly-test of Pocklington-Lehmer
 PollardRho(n,k,a,[])       Pollard's rho factorization
 pFactor(n,B,P)             Pollard's p-factorization
 quadraticSieve(n,c,B,k)    quadratic sieve factorization
 isOnCurve(N,a,b,P)         P is on the curve y^2z=x^3+a*xz^2+b*z^3  over Z/N
 ellipticAdd(N,a,b,P,Q)     P+Q, addition on elliptic curves
 ellipticMult(N,a,b,P,k)    k*P on elliptic curves
 ellipticRandomCurve(N)     generates y^2z=x^3+a*xz^2+b*z^3  over Z/N randomly
 ellipticRandomPoint(N,a,b) random point on y^2z=x^3+a*xz^2+b*z^3  over Z/N
 countPoints(N,a,b)         number of points of y^2=x^3+a*x+b  over Z/N
 ellipticAllPoints(N,a,b)   points of y^2=x^3+a*x+b  over Z/N
 ShanksMestre(q,a,b,[])     number of points of y^2=x^3+a*x+b  over Z/N
 Schoof(N,a,b)              number of points of y^2=x^3+a*x+b  over Z/N
 generateG(a,b,m)           m-th division polynomial of y^2=x^3+a*x+b  over Z/N
 factorLenstraECM(N,S,B,[]) Lenstra's factorization
 ECPP(N)                    primaly-test of Goldwasser-Kilian
 calculate_ordering(num1, primitive, mod1)    Calculates x so that primitive^x == num1 mod mod1
  is_primitive_root(primitive, mod1) Checks if primitive is a primitive root modulo mod1
  find_first_primitive_root(mod1) Returns the first primitive root modulo mod1, starting with 1
  binary_add(binary_list)         Adds a 1 to a binary encoded list
  inverse_modulus(num,mod1)       Finds a t so that t*num = 1 mod mod1
  is_prime(n)                     Checks if n is prime
  proc find_biggest_index(a)      Returns the index of the biggest element of a
  find_index(a,e)                 Returns the list index of element e in list a. Returns 0 if e is not in a
  subset_sum01(list knapsack, int solution)                          solves the subset-sum-knapsack-problem by calculating all subsets and choosing the right solution
  subset_sum02(list knapsack, int sol)                            solves the subset-sum-knapsack-problem with a naive greedy algorithm
  unbounded_knapsack(list knapsack, list profit, int capacity)                solves the unbounded_knapsack-problem, needing a list of knapsack weights, a list of profits and a capacity
  multidimensional_knapsack(matrix m, list capacities, list profits)              solves the multidimensional_knapsack-problem by using the PECH algorithm, needing a weight matrix m, a list of capacities and a list of profits
  naccache_stern_generation(int key, int primenum)                      generates a hard knapsack for the Naccache-Stern Kryptosystem for given key and prime modulus
  naccache_stern_encryption(list knapsack, list message, int primenum)            encrypts a message with the Naccache-Stern Kryptosystem, using a hard knapsack, a message encoded as binary list and a prime modulus
  naccache_stern_decryption(list knapsack, int key, int primenum, int message)        decrypts a message with the Naccache-Stern Kryptosystem, using the easy knapsack, the key, the prime modulus and the message encoded as integer
  m_merkle_hellman_transformation(list knapsack, int primitive, int mod1)            generates a hard knapsack for the multiplicative Merkle-Hellman Kryptosystem for a given easy knapsack and a primitive root for a modulus mod1
  m_merkle_hellman_encryption(list knapsack, list message)                  encrypts a message with the multiplicative Merkle-Hellman Kryptosystem, using a hard knapsack and a message encoded as binary list
  m_merkle_hellman_decryption(list knapsack, bigint primitive, bigint mod1, int message)    decrypts a message with the multiplicative Merkle-Hellman Kryptosystem, using the easy knapsack, the key given by the primitive root, the modulus mod1 and the message encoded as integer
  merkle_hellman_transformation(list knapsack, int key, int mod1                generates a hard knapsack for the  Merkle-Hellman Kryptosystem for a given easy knapsack , a multiplicator key and a modulus mod1
  merkle_hellman_encryption(list knapsack, list message)                    encrypts a message with the Merkle-Hellman Kryptosystem, using a hard knapsack and a message encoded as binary list
  merkle_hellman_decryption(list knapsack, int key, int mod1, int message)          decrypts a message with the multiplicative Merkle-Hellman Kryptosystem, using the hard knapsack, the key, the modulus mod1 and the message encoded as integer
  super_increasing_knapsack(int ksize)                            Creates the smallest super-increasing knapsack of given size ksize
  h_increasing_knapsack(int ksize, int h)                            Creates the smallest h-increasing knapsack of given size ksize and h
  injective_knapsack(int ksize, int kmaxelement)                        Creates all list of all injective knapsacks of given size ksize and maximal element kmaxelement
  calculate_max_sum(list a)                                  Calculates the maximal sum of a given knapsack a
  set_is_injective(list a)                                    Checks if knapsack a is injective
  is_h_injective(list a, int h)                                Checks if knapsack a is h-injective
  is_fix_injective(list a)                                  Checks if knapsack a is fix-injective
  three_elements(list out, int iterations)                          Creates the smallest injective knapsack with a given injective_knapsack by using the three-elements-algorithm with a given number of iterations

              [parameters in square brackets are optional]
";

LIB "poly.lib";
LIB "atkins.lib";

///////////////////////////////////////////////////////////////////////////////


//=============================================================================
//=========================== basic prozedures ================================
//=============================================================================

proc decimal(string s)
"USAGE:  decimal(s); s = string
RETURN: the (decimal) number corresponding to the hexadecimal number s
EXAMPLE:example decimal; shows an example
"
{
   int n=size(s);
   int i;
   bigint k;
   bigint t=16;
   bigint m=0;
   for(i=1;i<=n;i++)
   {
      k=0;
      if(s[i]=="1"){k=1;}
      if(s[i]=="2"){k=2;}
      if(s[i]=="3"){k=3;}
      if(s[i]=="4"){k=4;}
      if(s[i]=="5"){k=5;}
      if(s[i]=="6"){k=6;}
      if(s[i]=="7"){k=7;}
      if(s[i]=="8"){k=8;}
      if(s[i]=="9"){k=9;}
      if(s[i]=="a"){k=10;}
      if(s[i]=="b"){k=11;}
      if(s[i]=="c"){k=12;}
      if(s[i]=="d"){k=13;}
      if(s[i]=="e"){k=14;}
      if(s[i]=="f"){k=15;}
      m=m*t+k;
   }
   return(m);
}
example
{ "EXAMPLE:"; echo = 2;
   string s  ="8edfe37dae96cfd2466d77d3884d4196";
   decimal(s);
}

proc eexgcdN(list L)
"USAGE:  eexgcdN(L);
RETURN: list T such that sum_i L[i]*T[i]=T[n+1]=gcd(L[1],...,L[n])
EXAMPLE:example eexgcdN; shows an example
"
{
   if(size(L)==2)
   {
     list LL=extgcd(L[1],L[2]);return(list(LL[2],LL[3],LL[1]));
   }
   bigint p=L[size(L)];
   L=delete(L,size(L));
   list T=eexgcdN(L);
   list S=extgcd(T[size(T)],p);
   int i;
   for(i=1;i<=size(T)-1;i++)
   {
      T[i]=T[i]*S[2];
   }
   p=T[size(T)];
   T[size(T)]=S[3];
   T[size(T)+1]=S[1];
   return(T);
}
example
{ "EXAMPLE:"; echo = 2;
   eexgcdN(list(24,15,21));
}

proc lcmN(bigint a, bigint b)
"USAGE:  lcmN(a,b);
RETURN: lcm(a,b);
EXAMPLE:example lcmN; shows an example
"
{
   return (a*b/gcd(a,b));
}
example
{ "EXAMPLE:"; echo = 2;
   lcmN(24,15);
}

proc powerN(bigint m, bigint d, bigint n)
"USAGE:  powerN(m,d,n);
RETURN: m^d mod n
EXAMPLE:example powerN; shows an example
"
{
   if(d==0){return(bigint(1));}
   int i;
   if(n==0)
   {
      for(i=12;i>=2;i--)
      {
         if((d mod i)==0){return(powerN(m,d div i,n)^i);}
      }
      return(m*powerN(m,d-1,n));
   }
   for(i=12;i>=2;i--)
   {
      if((d mod i)==0)
      {
        bigint rr=powerN(m,d div i,n)^i mod n;
        if (rr<0) { rr=rr+n;}
        return(rr);
      }
   }
   return(m*powerN(m,d-1,n) mod n);
}
example
{ "EXAMPLE:"; echo = 2;
   powerN(24,15,7);
}

proc chineseRem(list T,list L)
"USAGE:  chineseRem(T,L);
RETURN: x such that x = T[i] mod L[i]
NOTE:   chinese remainder theorem
EXAMPLE:example chineseRem; shows an example
"
{
   int i;
   int n=size(L);
   bigint N=1;
   for(i=1;i<=n;i++)
   {
      N=N*L[i];
   }
   list M;
   for(i=1;i<=n;i++)
   {
      M[i]=N div L[i];
   }
   list S=eexgcdN(M);
   bigint x;
   for(i=1;i<=n;i++)
   {
      x=x+S[i]*M[i]*T[i];
   }
   x=x mod N;
   return(x);
}
example
{ "EXAMPLE:"; echo = 2;
   chineseRem(list(24,15,7),list(2,3,5));
}

proc Jacobi(bigint a, bigint n)
"USAGE:  Jacobi(a,n);
RETURN: the generalized Legendre symbol
NOTE: if n is an odd prime then Jacobi(a,n)=0,1,-1 if n|a, a=x^2 mod n,else
EXAMPLE:example Jacobi; shows an example
"
{
   int i;
   int z=1;
   bigint t=1;
   bigint k;

   if((((n-1) div 2) mod 2)!=0){z=-1;}
   if(a<0){return(z*Jacobi(-a,n));}
   a=a mod n;
   if(n==1){return(1);}
   if(a==0){return(0);}

   while(a!=0)
   {
      while((a mod 2)==0)
      {
         a=a div 2;
         if(((n mod 8)==3)||((n mod 8)==5)){t=-t;}
      }
      k=a;a=n;n=k;
      if(((a mod 4)==3)&&((n mod 4)==3)){t=-t;}
      a=a mod n;
   }
   if (n==1){return(t);}
   return(0);
}
example
{ "EXAMPLE:"; echo = 2;
   Jacobi(13580555397810650806,5792543);
}

proc primList(int n)
"USAGE:  primList(n);
RETURN: the list of all primes <=n
EXAMPLE:example primList; shows an example
"
{
   int i,j;
   list re;
   re[1]=2;
   re[2]=3;
   for(i=5;i<=n;i=i+2)
   {
     j=1;
     while(j<=size(re))
     {
        if((i mod re[j])==0){break;}
        j++;
     }
     if(j==size(re)+1){re[size(re)+1]=i;}
   }
   return(re);
}
example
{ "EXAMPLE:"; echo = 2;
   list L=primList(100);
   size(L);
   L[size(L)];
}

proc primL(bigint q)
"USAGE:  primL(q);
RETURN: list of the first primes p_1,...,p_r such that q>p_1*...*p_(r-1)
        and q<p_1*...*p_r
EXAMPLE:example primL; shows an example
"
{
   int i,j;
   list re;
   re[1]=2;
   re[2]=3;
   bigint s=6;
   i=3;
   while(s<=q)
   {
     i=i+2;
     j=1;
     while(j<=size(re))
     {
        if((i mod re[j])==0){break;}
        j++;
     }
     if(j==size(re)+1)
     {
        re[size(re)+1]=i;
        s=s*i;
     }
   }
   return(re);
}
example
{ "EXAMPLE:"; echo = 2;
   primL(20);
}

proc intPart(number x)
"USAGE:  intPart(x);
RETURN: the integral part of a rational number
EXAMPLE:example intPart; shows an example
"
{
  if (x>=0)
  {
    return(bigint((numerator(x)-(bigint(numerator(x)) mod bigint(denominator(x)))))
         div bigint(denominator(x)));
  }
  else
  {
    return(bigint((numerator(x)-(bigint(numerator(x)) mod bigint(denominator(x)+denominator(x)))))
         div bigint(denominator(x)));
  }
}
example
{ "EXAMPLE:"; echo = 2;
   ring r=0,x,dp;
   intPart(7/3);
}

proc intRoot(bigint mm)
"USAGE:  intRoot(m);
RETURN: the integral part of the square root of m
EXAMPLE:example intRoot; shows an example
"
{
   ring R = 0,@x,dp;
   number m=mm;
   number x=1;
   number t=x^2;
   number s=(x+1)^2;
   while(((m>t)&&(m>s))||((m<t)&&(m<s)))
   {
      if (x==0) { t=0; }
      else
      {
        x=intPart(x/2+m/(2*x));  //Newton step
        t=x^2;
      }
      if(t>m)
      {
         s=(x-1)^2;
      }
      else
      {
         s=(x+1)^2;
      }
   }
   if(t>m){return(bigint(x-1));}
   if(s==m){return(bigint(x+1));}
   return(bigint(x));
}
example
{ "EXAMPLE:"; echo = 2;
   intRoot(20);
}

proc squareRoot(bigint a, bigint p)
"USAGE:  squareRoot(a,p);
RETURN: the square root of a in Z/p, p prime
NOTE:   assumes the Jacobi symbol is 1 or p=2.
EXAMPLE:example squareRoot; shows an example
"
{
   if(p==2){return(a);}
   if((a mod p)==0){return(0);}
   if (a<0) { a=a+p; }
   if(powerN(a,p-1,p)!=1)
   {
      "p is not prime";
      return(bigint(-5));
   }
   bigint n=random(1,2147483647) mod p;
   if(n==0){n=n+1;}
   bigint j=Jacobi(n,p);
   if(j==0)
   {
      "p is not prime";
      return(bigint(-5));
   }
   if(j==1)
   {
      return(squareRoot(a,p));
   }
   bigint q=p-1;
   bigint e=0;
   bigint two=2;
   bigint z,m,t;
   while((q mod 2)==0)
   {
      e=e+1;
      q=q div 2;
   }
   bigint y=powerN(n,q,p);
   bigint r=e;
   bigint x=powerN(a,(q-1) div 2,p);
   bigint b=a*x^2 mod p;
   x=a*x mod p;

   while(((b-1) mod p)!=0)
   {
      m=0;z=b;
      while(((z-1) mod p)!=0)
      {
         z=z^2 mod p;
         m=m+1;
      }
      t=powerN(y,powerN(two,r-m-1,p),p);
      y=t^2 mod p;
      r=m;
      x=x*t mod p;
      b=b*y mod p;
   }
   return(x);
}
example
{ "EXAMPLE:"; echo = 2;
   squareRoot(8315890421938608,32003);
}


proc solutionsMod2(bigintmat MM)
"USAGE:  solutionsMod2(M);
RETURN: an intmat containing a basis of the vector space of solutions of the
        linear system of equations defined by M over the prime field of
        characteristic 2
EXAMPLE:example solutionsMod2; shows an example
"
{
   ring Rhelp=2,z,(c,dp);
   int i,j;
   matrix M[nrows(MM)][ncols(MM)];
   for(i=1;i<=nrows(MM);i++)
   {
      for(j=1;j<=ncols(MM);j++)
      {
         M[i,j]=MM[i,j];
      }
   }
   matrix S=syz(M);
   intmat v[nrows(S)][ncols(S)];
   for(i=1;i<=nrows(S);i++)
   {
      for(j=1;j<=ncols(S);j++)
      {
         if(S[i,j]==1){v[i,j]=1;}
      }
   }
   return(v);
}
example
{ "EXAMPLE:"; echo = 2;
   bigintmat M[3][3]=1,2,3,4,5,6,7,6,5;
   solutionsMod2(M);
}

proc powerX(int q, int i, ideal I)
"USAGE:  powerX(q,i,I);
RETURN: the q-th power of the i-th variable modulo I
ASSUME: I is a standard basis
EXAMPLE:example powerX; shows an example
"
{
   if(q<=181){return(reduce(var(i)^int(q),I));}
   if((q mod 5)==0){return(reduce(powerX(q div 5,i,I)^5,I));}
   if((q mod 4)==0){return(reduce(powerX(q div 4,i,I)^4,I));}
   if((q mod 3)==0){return(reduce(powerX(q div 3,i,I)^3,I));}
   if((q mod 2)==0){return(reduce(powerX(q div 2,i,I)^2,I));}
   return(reduce(var(i)*powerX(q-1,i,I),I));
}
example
{ "EXAMPLE:"; echo = 2;
   ring R = 0,(x,y),dp;
   powerX(100,2,std(ideal(x3-1,y2-x)));
}

//======================================================================
//=========================== Discrete Logarithm =======================
//======================================================================

//============== Shank's baby step - giant step ========================

proc babyGiant(bigint b, bigint y, bigint p)
"USAGE:  babyGiant(b,y,p);
RETURN: the discrete logarithm x: b^x=y mod p
NOTE:   This procedure works based on Shank's baby step - giant step method.
EXAMPLE:example babyGiant; shows an example
"
{
   int i,j,m;
   list l;
   bigint h=1;
   bigint x;

//choose m minimal such that m^2>p
   for(i=1;i<=p;i++){if(i^2>p) break;}
   m=i;

//baby-step:  compute the table y*b^i for 1<=i<=m
   for(i=1;i<=m;i++){l[i]=y*b^i mod p;}

//giant-step: compute b^(m+j), 1<=j<=m and search in the baby-step table
//for an i with y*b^i=b^(m*j). If found then x=m*j-i
   bigint g=b^m mod p;
   while(j<m)
   {
      j++;
      h=h*g mod p;
      for(i=1;i<=m;i++)
      {
         if(h==l[i])
         {
            x=m*j-i;
            j=m;
            break;
         }
     }
   }
   return(x);
}
example
{ "EXAMPLE:"; echo = 2;
   bigint b=2;
   bigint y=10;
   bigint p=101;
   babyGiant(b,y,p);
}

//==============  Pollards rho  =================================

proc rho(bigint b, bigint y, bigint p)
"USAGE:  rho(b,y,p);
RETURN: the discrete logarithm x=log_b(y): b^x=y mod p
NOTE: Pollard's rho:
       choose random f_0 in 0,...,p-2 ,e_0=0, define x_0=b^f_0, define
       x_i=y^e_i*b^f_i as below. For i large enough there is i with
       x_(i/2)=x_i. Let s:=e_(i/2)-e_i mod p-1 and t:=f_i-f_(i/2) mod p-1,
       d=gcd(s,p-1)=u*s+v*(p-1) then x=tu/d +j*(p-1)/d for some j (to be
       found by trying)
EXAMPLE:example rho; shows an example
"
{
   int i=1;
   int j;
   bigint s,t;
   list e,f,x;

   e[1]=0;
   f[1]=random(0,2147483629) mod (p-1);
   x[1]=powerN(b,f[1],p);
   while(i)
   {
      if((x[i] mod 3)==1)
      {
         x[i+1]=y*x[i] mod p;
         e[i+1]=e[i]+1 mod (p-1);
         f[i+1]=f[i];
      }
      if((x[i] mod 3)==2)
      {
         x[i+1]=x[i]^2 mod p;
         e[i+1]=e[i]*2 mod (p-1);
         f[i+1]=f[i]*2 mod (p-1);
      }
      if((x[i] mod 3)==0)
      {
         x[i+1]=x[i]*b mod p;
         e[i+1]=e[i];
         f[i+1]=f[i]+1 mod (p-1);
      }
      i++;
      for(j=i-1;j>=1;j--)
      {
         if(x[i]==x[j])
         {
            s=(e[j]-e[i]) mod (p-1);
            t=(f[i]-f[j]) mod (p-1);
            if(s!=0)
            {
               i=0;
            }
            else
            {
               e[1]=0;
               f[1]=random(0,2147483629) mod (p-1);
               x[1]=powerN(b,f[1],p);
               i=1;
            }
            break;
         }
      }
   }

   list w=extgcd(s,p-1);
   bigint u=w[2];
   bigint d=w[1];

   bigint a=(t*u div d) mod (p-1);

   bigint pn=powerN(b,a,p);
   if (pn<0) { pn=pn+p;}
   while(powerN(b,a,p)!=y)
   {
      a=(a+(p-1) div d) mod (p-1);
      if (a<0) { a=a+p-1; }
   }
   return(a);
}
example
{ "EXAMPLE:"; echo = 2;
   bigint b=2;
   bigint y=10;
   bigint p=101;
   rho(b,y,p);
}
//====================================================================
//====================== Primality Tests =============================
//====================================================================

//================================= Miller-Rabin =====================

proc MillerRabin(bigint n, int k)
"USAGE:  MillerRabin(n,k);
RETURN: 1 if n is prime, 0 else
NOTE: probabilistic test of Miller-Rabin with k loops to test if n is prime.
       Using the theorem: If n is prime, n-1=2^s*r, r odd, then
       powerN(a,r,n)=1 or powerN(a,r*2^i,n)=-1 for some i
EXAMPLE:example MillerRabin; shows an example
"
{
   if(n<0){n=-n;}
   if((n==2)||(n==3)){return(1);}
   if((n mod 2)==0){return(0);}

   int i;
   bigint a,b,j,r,s;
   r=n-1;
   s=0;
   while((r mod 2)==0)
   {
      s=s+1;
      r=r div 2;
   }
   while(i<k)
   {
      i++;
      a=random(2,2147483629) mod n; if(a==0){a=3;}
      if(gcd(a,n)!=1){return(0);}
      b=powerN(a,r,n);
      if(b!=1)
      {
         j=0;
         while(j<s)
         {
           if(((b+1) mod n)==0) break;
           b=powerN(b,2,n);
           j=j+1;
         }
         if(j==s){return(0);}
      }
   }
   return(1);
}
example
{ "EXAMPLE:"; echo = 2;
   bigint x=2;
   x=x^787-1;
   MillerRabin(x,3);
}

//======================= Soloway-Strassen  ==========================

proc SolowayStrassen(bigint n, int k)
"USAGE:  SolowayStrassen(n,k);
RETURN: 1 if n is prime, 0 else
NOTE: probabilistic test of Soloway-Strassen with k loops to test if n is
       prime using the theorem: If n is prime then
       powerN(a,(n-1)/2,n)=Jacobi(a,n) mod n
EXAMPLE:example SolowayStrassen; shows an example
"
{
   if(n<0){n=-n;}
   if((n==2)||(n==3)){return(1);}
   if((n mod 2)==0){return(0);}

   bigint a,pn,jn;
   int i;
   while(i<k)
   {
      i++;
      a=random(2,2147483629) mod n; if(a==0){a=3;}
      if(gcd(a,n)!=1){return(0);}
      pn=powerN(a,(n-1) div 2,n);
      if (pn<0) { pn=pn+n;}
      jn=Jacobi(a,n) mod n;
      if (jn<0) { jn=jn+n;}
      if(pn!=jn){return(0);}
   }
   return(1);
}
example
{ "EXAMPLE:"; echo = 2;
   bigint h=10;
   bigint p=h^100+267;
   //p=h^100+43723;
   //p=h^200+632347;
   SolowayStrassen(h,3);
}


/*
ring R=0,z,dp;
number p=398075086424064937397125500550386491199064362342526708406385189575946388957261768583317;
number q=472772146107435302536223071973048224632914695302097116459852171130520711256363590397527;
number n=p*q;
SolowayStrassen(n,3);
*/

//===================== Pocklington-Lehmer ==============================

proc PocklingtonLehmer(bigint N, list #)
"USAGE: PocklingtonLehmer(N); optional: PocklingtonLehmer(N,L);
        L a list of the first k primes
RETURN:message N is not prime or {A,{p},{a_p}} as certificate for N being prime
NOTE:assumes that it is possible to factorize N-1=A*B such that gcd(A,B)=1,
      the factorization of A is completely known and A^2>N .
      N is prime if and only if for each prime factor p of A we can find
      a_p such that a_p^(N-1)=1 mod N and gcd(a_p^((N-1)/p)-1,N)=1
EXAMPLE:example PocklingtonLehmer; shows an example
"
{
   bigint m=intRoot(N);
   if(size(#)>0)
   {
      list S=PollardRho(N-1,10000,1,#);
   }
   else
   {
      list S=PollardRho(N-1,10000,1);
   }
   int i,j;
   bigint A=1;
   bigint p,a,g;
   list PA;
   list re;

   while(i<size(S))
   {
      p=S[i+1];
      A=A*p;
      PA[i+1]=p;
      if(A>m){break;}

      while(1)
      {
        p=p*S[i+1];
        if(((N-1) mod p)==0)
        {
           A=A*p;
        }
        else
        {
           break;
        }
      }
      i++;
   }
   if(A<=m)
   {
     A=N div A;
     PA=list(S[size(S)]);
   }
   for(i=1;i<=size(PA);i++)
   {
      a=1;
      while(a<N-1)
      {
         a=a+1;
         if(powerN(a,N-1,N)!=1){return("not prime");}
         g=gcd(powerN(a,(N-1) div PA[i],N),N);
         if(g==1)
         {
           re[size(re)+1]=list(PA[i],a);
           break;
         }
         if(g<N){"not prime";return(g);}
      }
   }
   return(list(A,re));
}
example
{ "EXAMPLE:"; echo = 2;
   bigint N=105554676553297;
   PocklingtonLehmer(N);
   list L=primList(1000);
   PocklingtonLehmer(N,L);
}

//=======================================================================
//======================= Factorization =================================
//=======================================================================

//======================= Pollards rho  =================================

proc PollardRho(bigint n, int k, int allFactors, list #)
"USAGE:  PollardRho(n,k,allFactors); optional: PollardRho(n,k,allFactors,L);
         L a list of the first k primes
RETURN: a list of factors of n (which could be just n),if allFactors=0@*
        a list of all factors of n ,if allFactors=1
NOTE: probabilistic rho-algorithm of Pollard to find a factor of n in k loops.
      Creates a sequence x_i such that (x_i)^2=(x_2i)^2 mod n for some i,
      computes gcd(x_i-x_2i,n) to find a divisor. To define the sequence
      choose x,a and define x_n+1=x_n^2+a mod n, x_1=x.
      If allFactors is 1, it tries to find recursively all prime factors
      using the Soloway-Strassen test.
SEE ALSO: primefactors
EXAMPLE:example PollardRho; shows an example
"
{
    int i,j;
    list L=primList(100);
    list re,se;
    if(n<0){n=-n;}
    if(n==1){return(re);}

//this is optional: test whether a prime of the list # devides n
    if(size(#)>0)
    {
       L=#;
    }
    for(i=1;i<=size(L);i++)
    {
       if((n mod L[i])==0)
       {
          re[size(re)+1]=L[i];
          while((n mod L[i])==0)
          {
             n=n div L[i];
          }
       }
       if(n==1){return(re);}
    }
    int e=size(re);
//here the rho-algorithm starts
    bigint a,d,x,y;
    while(n>1)
    {
       a=random(2,2147483629);
       x=random(2,2147483629);
       y=x;
       d=1;
       i=0;
       while(i<k)
       {
          i++;
          x=powerN(x,2,n); x=(x+a) mod n;
          y=powerN(y,2,n); y=(y+a) mod n;
          y=powerN(y,2,n); y=(y+a) mod n;
          d=gcd(x-y,n);
          if(d>1)
          {
             re[size(re)+1]=d;
             while((n mod d)==0)
             {
               n=n div d;
             }
             break;
          }
          if(i==k)
          {
             re[size(re)+1]=n;
             n=1;
          }
       }
    }
    if(allFactors)      //want to obtain all prime factors
    {
       i=e;
       while(i<size(re))
       {
          i++;

          if(!SolowayStrassen(re[i],5))
          {
             se=PollardRho(re[i],2*k,1);
             re[i]=se[size(se)];
             for(j=1;j<=size(se)-1;j++)
             {
                re[size(re)+1]=se[j];
             }
             i--;
          }
       }
    }
    return(re);
}
example
{ "EXAMPLE:"; echo = 2;
   bigint h=10;
   bigint p=h^30+4;
   PollardRho(p,5000,0);
}

//======================== Pollards p-factorization ================
proc pFactor(bigint n,int B, list P)
"USAGE:  pFactor(n,B,P); n to be factorized, B a bound , P a list of primes
RETURN: a list of factors of n or n if no factor found
NOTE: Pollard's p-factorization
       creates the product k of powers of primes (bounded by B)  from
       the list P with the idea that for a prime divisor p of n we have
       p-1|k, and then p divides gcd(a^k-1,n) for some random a
EXAMPLE:example pFactor; shows an example
"
{
   int i;
   bigint k=1;
   bigint w;
   while(i<size(P))
   {
      i++;
      w=P[i];
      if(w>B) break;
      while(w*P[i]<=B)
      {
         w=w*P[i];
      }
      k=k*w;
   }
   bigint a=random(2,2147483629);
   bigint d=gcd(powerN(a,k,n)-1,n);
   if((d>1)&&(d<n)){return(d);}
   return(n);
}
example
{ "EXAMPLE:"; echo = 2;
   list L=primList(1000);
   pFactor(1241143,13,L);
   bigint h=10;
   h=h^30+25;
   pFactor(h,20,L);
}

//==================== quadratic sieve ==============================

proc quadraticSieve(bigint n, int c, list B, int k)
"USAGE:  quadraticSieve(n,c,B,k); n to be factorized, [-c,c] the
         sieve-intervall, B a list of primes,
         k for using the first k elements in B
RETURN: a list of factors of n or the message: no divisor found
NOTE: The idea being used is to find x,y such that x^2=y^2 mod n then
      gcd(x-y,n) can be a proper divisor of n
EXAMPLE:example quadraticSieve; shows an example
"
{
   bigint f,d;
   int i,j,l,s,p;
   list S,tmp;
   intvec v;
   v[k]=0;

//compute the integral part of the square root of n
   bigint m=intRoot(n);

//consider the function f(X)=(X+m)^2-n and compute for s in [-c,c] the values
   while(i<=2*c)
   {
      f=(i-c+m)^2-n;
      tmp[1]=i-c+m;
      tmp[2]=f;
      tmp[3]=v;
      S[i+1]=tmp;
      i++;
   }

//the sieve with p in B
//find all s in [-c,c] such that f(s) has all prime divisors in the first
//k elements of B and the decomposition of f(s). They are characterized
//by 1 or -1 at the second place of S[j]:
//S[j]=j-c+m,f(j-c)/p_1^v_1*...*p_k^v_k, v_1,...,v_k maximal
   for(i=1;i<=k;i++)
   {
      p=B[i];
      if((p>2)&&(Jacobi(n,p)==-1)){i++;continue;}//n is no quadratic rest mod p
      j=1;
      while(j<=p)
      {
         if(j>2*c+1) break;
         f=S[j][2];
         v=S[j][3];
         s=0;
         while((f mod p)==0)
         {
            s++;
            f=f div p;
         }
         if(s)
         {
           S[j][2]=f;
           v[i]=s;
           S[j][3]=v;
           l=j;
           while(l+p<=2*c+1)
           {
              l=l+p;
              f=S[l][2];
              v=S[l][3];
              s=0;
              while((f mod p)==0)
              {
                s++;
                f=f div p;
              }
              S[l][2]=f;
              v[i]=s;
              S[l][3]=v;
           }
         }
         j++;
      }
   }
   list T;
   for(j=1;j<=2*c+1;j++)
   {
      if((S[j][2]==1)||(S[j][2]==-1))
      {
         T[size(T)+1]=S[j];
      }
   }

//the system of equations for the exponents {l_s} for the f(s) such
//product f(s)^l_s is a square (l_s are 1 or 0)
   bigintmat M[k+1][size(T)];
   for(j=1;j<=size(T);j++)
   {
      if(T[j][2]==-1){M[1,j]=1;}
      for(i=1;i<=k;i++)
      {
         M[i+1,j]=T[j][3][i];
      }
   }
   intmat G=solutionsMod2(M);

//construction of x and y such that x^2=y^2 mod n and d=gcd(x-y,n)
//y=square root of product f(s)^l_s
//x=product s+m
   bigint x=1;
   bigint y=1;

   for(i=1;i<=ncols(G);i++)
   {
      kill v;
      intvec v;
      v[k]=0;
      for(j=1;j<=size(T);j++)
      {
         x=x*T[j][1]^G[j,i] mod n;
         if((T[j][2]==-1)&&(G[j,i]==1)){y=-y;}
         v=v+G[j,i]*T[j][3];

      }
      for(l=1;l<=k;l++)
      {
         y=y*B[l]^(v[l] div 2) mod n;
      }
      d=gcd(x-y,n);
      if((d>1)&&(d<n)){return(d);}
   }
   return("no divisor found");
}
example
{ "EXAMPLE:"; echo = 2;
   list L=primList(5000);
   quadraticSieve(7429,3,L,4);
   quadraticSieve(1241143,100,L,50);
}

//======================================================================
//==================== elliptic curves  ================================
//======================================================================

//================= elementary operations ==============================

proc isOnCurve(bigint N, bigint a, bigint b, list P)
"USAGE:  isOnCurve(N,a,b,P);
RETURN: 1 or 0 (depending on whether P is on the curve or not)
NOTE: checks whether P=(P[1]:P[2]:P[3]) is a point on the elliptic
      curve defined by y^2z=x^3+a*xz^2+b*z^3  over Z/N
EXAMPLE:example isOnCurve; shows an example
"
{
   if(((P[2]^2*P[3]-P[1]^3-a*P[1]*P[3]^2-b*P[3]^3) mod N)!=0){return(0);}
   return(1);
}
example
{ "EXAMPLE:"; echo = 2;
   isOnCurve(32003,5,7,list(10,16,1));
}

proc ellipticAdd(bigint N, bigint a, bigint b, list P, list Q)
"USAGE:  ellipticAdd(N,a,b,P,Q);
RETURN: list L, representing the point P+Q
NOTE: P=(P[1]:P[2]:P[3]), Q=(Q[1]:Q[2]:Q[3]) points on the elliptic curve
      defined by y^2z=x^3+a*xz^2+b*z^3  over Z/N
EXAMPLE:example ellipticAdd; shows an example
"
{
   if(N==2){ERROR("not implemented for 2");}
   int i;
   for(i=1;i<=3;i++)
   {
      P[i]=P[i] mod N; if (P[i]<0) { P[i]=P[i]+N:}
      Q[i]=Q[i] mod N; if (Q[i]<0) { Q[i]=Q[i]+N;}
   }
   list Resu;
   Resu[1]=bigint(0);
   Resu[2]=bigint(1);
   Resu[3]=bigint(0);
   list Error;
   Error[1]=0;
   //test for ellictic curve
   bigint D=4*a^3+27*b^2;
   bigint g=gcd(D,N);
   if(g==N){return(Error);}
   if(g!=1)
   {
      P[4]=g;
      return(P);
   }
   if(((P[1]==0)&&(P[2]==0)&&(P[3]==0))||((Q[1]==0)&&(Q[2]==0)&&(Q[3]==0)))
   {
      Error[1]=-2;
      return(Error);
   }
   if(!isOnCurve(N,a,b,P)||!isOnCurve(N,a,b,Q))
   {
      Error[1]=-1;
      return(Error);
   }
   if(P[3]==0){return(Q);}
   if(Q[3]==0){return(P);}
   list I=extgcd(P[3],N);
   if(I[1]!=1)
   {
      P[4]=I[1];
      return(P);
   }
   P[1]=P[1]*I[2] mod N;
   P[2]=P[2]*I[2] mod N;
   I=extgcd(Q[3],N);
   if(I[1]!=1)
   {
      P[4]=I[1];
      return(P);
   }
   Q[1]=Q[1]*I[2] mod N;
   Q[2]=Q[2]*I[2] mod N;
   if((P[1]==Q[1])&&(((P[2]+Q[2]) mod N)==0)){return(Resu);}
   bigint L;
   if((P[1]==Q[1])&&(P[2]==Q[2]))
   {
      I=extgcd(2*Q[2],N);
      if(I[1]!=1)
      {
         P[4]=I[1];
         return(P);
      }
      L=I[2]*(3*Q[1]^2+a) mod N;
   }
   else
   {
      I=extgcd(Q[1]-P[1],N);
      if(I[1]!=1)
      {
         P[4]=I[1];
         return(P);
      }
      L=(Q[2]-P[2])*I[2] mod N;
   }
   Resu[1]=(L^2-P[1]-Q[1]) mod N;
   if (Resu[1]<0) { Resu[1]=Resu[1]+N; }
   Resu[2]=(L*(P[1]-Resu[1])-P[2]) mod N;
   if (Resu[2]<0) { Resu[2]=Resu[2]+N; }
   Resu[3]=bigint(1);
   return(Resu);
}
example
{ "EXAMPLE:"; echo = 2;
   bigint N=11;
   bigint a=1;
   bigint b=6;
   list P,Q;
   P[1]=2;
   P[2]=4;
   P[3]=1;
   Q[1]=3;
   Q[2]=5;
   Q[3]=1;
   ellipticAdd(N,a,b,P,Q);
}

proc ellipticMult(bigint N, bigint a, bigint b, list P, bigint k)
"USAGE:  ellipticMult(N,a,b,P,k);
RETURN: a list L representing the point k*P
NOTE:  P=(P[1]:P[2]:P[3]) a point on the elliptic curve defined by
       y^2z=x^3+a*xz^2+b*z^3  over Z/N
EXAMPLE:example ellipticMult; shows an example
"
{
   if(P[3]==0){return(P);}
   list resu;
   resu[1]=bigint(0);
   resu[2]=bigint(1);
   resu[3]=bigint(0);

   if(k==0){return(resu);}
   if(k==1){return(P);}
   if(k==2){return(ellipticAdd(N,a,b,P,P));}
   if(k==-1)
   {
      resu=P;
      resu[2]=N-P[2];
      return(resu);
   }
   if(k<0)
   {
      resu=ellipticMult(N,a,b,P,-k);
      return(ellipticMult(N,a,b,resu,-1));
   }
   if((k mod 2)==0)
   {
      resu=ellipticMult(N,a,b,P,k div 2);
      return(ellipticAdd(N,a,b,resu,resu));
   }
   resu=ellipticMult(N,a,b,P,k-1);
   return(ellipticAdd(N,a,b,resu,P));
}
example
{ "EXAMPLE:"; echo = 2;
   bigint N=11;
   bigint a=1;
   bigint b=6;
   list P;
   P[1]=2;
   P[2]=4;
   P[3]=1;
   ellipticMult(N,a,b,P,3);
}

//================== Random for elliptic curves =====================

proc ellipticRandomCurve(bigint N)
"USAGE:  ellipticRandomCurve(N);
RETURN: a list of two random numbers a,b and 4a^3+27b^2 mod N
NOTE:   y^2z=x^3+a*xz^2+b^2*z^3 defines an elliptic curve over Z/N
EXAMPLE:example ellipticRandomCurve; shows an example
"
{
   int k;
   while(k<=10)
   {
     k++;
     bigint a=random(1,2147483647) mod N;
     bigint b=random(1,2147483647) mod N;
     //test for ellictic curve
     bigint D=4*a^3+27*b^4; //the constant term is b^2
     bigint g=gcd(D,N);
     if(g<N){return(list(a,b,g));}
   }
   ERROR("no random curve found");
}
example
{ "EXAMPLE:"; echo = 2;
   ellipticRandomCurve(32003);
}

proc ellipticRandomPoint(bigint N, bigint a, bigint b)
"USAGE:  ellipticRandomPoint(N,a,b);
RETURN: a list representing  a random point (x:y:z) of the elliptic curve
        defined by y^2z=x^3+a*xz^2+b*z^3  over Z/N
EXAMPLE:example ellipticRandomPoint; shows an example
"
{
   bigint x=random(1,2147483647) mod N;
   bigint h=x^3+a*x+b;
   h=h mod N;
   list resu;
   resu[1]=x;
   resu[2]=0;
   resu[3]=1;
   if(h==0){return(resu);}

   bigint n=Jacobi(h,N);
   if(n==0)
   {
      resu=-5;
      "N is not prime";
      return(resu);
   }
   if(n==1)
   {
      resu[2]=squareRoot(h,N);
      return(resu);
   }
   return(ellipticRandomPoint(N,a,b));
}
example
{ "EXAMPLE:"; echo = 2;
   ellipticRandomPoint(32003,3,181);
}



//====================================================================
//======== counting the points of an elliptic curve  =================
//====================================================================

//==================   the trivial approaches  =======================
proc countPoints(bigint N, bigint a, bigint b)
"USAGE:  countPoints(N,a,b);
RETURN: the number of points of the elliptic curve defined by
        y^2=x^3+a*x+b  over Z/N
NOTE: trivial approach
EXAMPLE:example countPoints; shows an example
"
{
  bigint x;
  bigint r=N+1;
  while(x<N)
  {
     r=r+Jacobi((x^3+a*x+b) mod N,N);
     x=x+1;
  }
  return(r);
}
example
{ "EXAMPLE:"; echo = 2;
   countPoints(181,71,150);
}

proc ellipticAllPoints(bigint N, bigint a, bigint b)
"USAGE:  ellipticAllPoints(N,a,b);
RETURN: list of points (x:y:z) of the elliptic curve defined by
        y^2z=x^3+a*xz^2+b*z^3  over Z/N
EXAMPLE:example ellipticAllPoints; shows an example
"
{
   list resu,point;
   point[1]=0;
   point[2]=1;
   point[3]=0;
   resu[1]=point;
   point[3]=1;
   bigint x,h,n;
   while(x<N)
   {
      h=(x^3+a*x+b) mod N;
      if(h==0)
      {
         point[1]=x;
         point[2]=0;
         resu[size(resu)+1]=point;
      }
      else
      {
         n=Jacobi(h,N);
         if(n==1)
         {
            n=squareRoot(h,N);
            point[1]=x;
            point[2]=n;
            resu[size(resu)+1]=point;
            point[2]=N-n;
            resu[size(resu)+1]=point;
         }
      }
      x=x+1;
   }
   return(resu);
}
example
{ "EXAMPLE:"; echo = 2;
   list L=ellipticAllPoints(181,71,150);
   size(L);
   L[size(L)];
}

//================ the algorithm of Shanks and Mestre =================

proc ShanksMestre(bigint q, bigint a, bigint b, list #)
"USAGE:  ShanksMestre(q,a,b); optional: ShanksMestre(q,a,b,s); s the number
         of loops in the algorithm (default s=1)
RETURN: the number of points of the elliptic curve defined by
         y^2=x^3+a*x+b  over Z/N
NOTE: algorithm of Shanks and Mestre (baby-step-giant-step)
EXAMPLE:example ShanksMestre; shows an example
"
{
   bigint n=intRoot(4*q);
   bigint m=intRoot(intRoot(16*q))+1;
   bigint d;
   int i,j,k,s;
   list B,K,T,P,Q,R,mP;
   B[1]=list(0,1,0);
   if(size(#)>0)
   {
      s=#[1];
   }
   else
   {
      s=1;
   }
   while(k<s)
   {
      P =ellipticRandomPoint(q,a,b);
      Q =ellipticMult(q,a,b,P,n+q+1);

      while(j<m)
      {
         j++;
         B[j+1]=ellipticAdd(q,a,b,P,B[j]);  //baby-step list
      }
      mP=ellipticAdd(q,a,b,P,B[j]);
      mP[2]=q-mP[2];
      while(i<m)                            //giant-step
      {
         j=0;
         while(j<m)
         {
            j++;
            if((Q[1]==B[j][1])&&(Q[2]==B[j][2])&&(Q[3]==B[j][3]))
            {
               T[1]=P;
               T[2]=q+1+n-(i*m+j-1);
               K[size(K)+1]=T;
               if(size(K)>1)
               {
                  if(K[size(K)][2]!=K[size(K)-1][2])
                  {
                     d=gcd(K[size(K)][2],K[size(K)-1][2]);
                     if(ellipticMult(q,a,b,K[size(K)],d)[3]==0)
                     {
                       K[size(K)][2]=K[size(K)-1][2];
                     }
                  }
               }
               i=int(m);
               break;
            }
         }
         i=i+1;
         Q=ellipticAdd(q,a,b,mP,Q);
      }
      k++;
   }
   if(size(K)>0)
   {
      int te=1;
      for(i=1;i<=size(K)-1;i++)
      {
         if(K[size(K)][2]!=K[i][2])
         {
           if(ellipticMult(q,a,b,K[i],K[size(K)][2])[3]!=0)
           {
             te=0;
             break;
           }
         }
      }
      if(te)
      {
         return(K[size(K)][2]);
      }
   }
   return(ShanksMestre(q,a,b,s));
}
example
{ "EXAMPLE:"; echo = 2;
   ShanksMestre(32003,71,602);
}

//==================== Schoof's algorithm =============================

proc Schoof(bigint N,bigint a, bigint b)
"USAGE:  Schoof(N,a,b);
RETURN: the number of points of the elliptic curve defined by
        y^2=x^3+a*x+b  over Z/N
NOTE:  algorithm of Schoof
EXAMPLE:example Schoof; shows an example
"
{
   int pr=printlevel;
//test for ellictic curve
   bigint D=4*a^3+27*b^2;
   bigint G=gcd(D,N);
   if(G==N){ERROR("not an elliptic curve");}
   if(G!=1){ERROR("not a prime");}

//=== small N
  // if((N<=500)&&(pr<5)){return(countPoints(int(N),a,b));}

//=== the general case
   bigint q=intRoot(4*N);
   list L=primL(2*q);
   int r=size(L);
   list T;
   int i,j;
   for(j=1;j<=r;j++)
   {
      T[j]=(testElliptic(int(N),a,b,L[j])+int(q)) mod L[j];
   }
   if(pr>=5)
   {
      "===================================================================";
      "Chinese remainder :";
      for(i=1;i<=size(T);i++)
      {
         " x =",T[i]," mod ",L[i];
      }
      "gives t+ integral part of the square root of q (to be positive)";
      chineseRem(T,L);
      "we obtain t = ",chineseRem(T,L)-q;
      "===================================================================";
   }
   bigint t=chineseRem(T,L)-q;
   return(N+1-t);
}
example
{ "EXAMPLE:"; echo = 2;
   Schoof(32003,71,602);
}

/*
needs 518 sec
Schoof(2147483629,17,3567);
2147168895
*/


proc generateG(number a,number b, int m)
"USAGE:  generateG(a,b,m);
RETURN: m-th division polynomial
NOTE: generate the so-called division polynomials, i.e., the recursively defined
polynomials p_m=generateG(a,b,m) in Z[x, y] such that, for a point (x:y:1) on the
elliptic curve defined by y^2=x^3+a*x+b  over Z/N the point@*
m*P=(x-(p_(m-1)*p_(m+1))/p_m^2 :(p_(m+2)*p_(m-1)^2-p_(m-2)*p_(m+1)^2)/4y*p_m^3 :1)
and m*P=0 if and only if p_m(P)=0
EXAMPLE:example generateG; shows an example
"
{
   if(m==0){return(poly(0));}
   if(m==1){return(poly(1));}
   if(m==2){return(2*var(1));}
   if(m==3){return(3*var(2)^4+6*a*var(2)^2+12*b*var(2)-a^2);}
   if(m==4)
   {
      return(4*var(1)*(var(2)^6+5*a*var(2)^4+20*b*var(2)^3-5*a^2*var(2)^2
        -4*a*b*var(2)-8*b^2-a^3));
   }
   if((m mod 2)==0)
   {
      return((generateG(a,b,m div 2+2)*generateG(a,b,m div 2-1)^2
        -generateG(a,b,m div 2-2)*generateG(a,b,m div 2+1)^2)
        *generateG(a,b,m div 2)/(2*var(1)));
   }
   return(generateG(a,b,(m-1) div 2+2)*generateG(a,b,(m-1) div 2)^3
     -generateG(a,b,(m-1) div 2-1)*generateG(a,b,(m-1) div 2+1)^3);
}
example
{ "EXAMPLE:"; echo = 2;
   ring R = 0,(x,y),dp;
   generateG(7,15,4);
}


static proc testElliptic(int q,bigint aa,bigint bb,int l)
"USAGE:  testElliptic(q,a,b,l);
RETURN: an integer t, the trace of the Frobenius
NOTE: the kernel for the Schoof algorithm: looks for the t such that for all
      points (x:y:1) in C[l]={P in C | l*P=0},C the elliptic curve defined by
      y^2=x^3+a*x+b  over Z/q with group structure induced by 0=(0:1:0),
      (x:y:1)^(q^2)-t*(x:y:1)^q -ql*(x:y:1)=(0:1:0), ql= q mod l, trace of
      Frobenius.
EXAMPLE:example testElliptic; shows an example
"
{
   int pr=printlevel;
   ring S=q,(y,x),(L(100000),lp);
   number a=aa;
   number b=bb;
   poly F=y2-x3-a*x-b;       // the curve C
   poly G=generateG(a,b,l);
   ideal I=std(ideal(F,G));  // the points C[l]
   poly xq=powerX(q,2,I);
   poly yq=powerX(q,1,I);
   poly xq2=reduce(subst(xq,x,xq,y,yq),I);
   poly yq2=reduce(subst(yq,x,xq,y,yq),I);
   ideal J;
   int ql=q mod l;
   if(ql==0){ERROR("q is not prime");}
   int t;
   poly F1,F2,G1,G2,P1,P2,Q1,Q2,H1,H2,L1,L2;

   if(pr>=5)
   {
      "===================================================================";
      "q=",q;
      "l=",l;
      "q mod l=",ql;
      "the Groebner basis for C[l]:";I;
      "x^q mod I = ",xq;
      "x^(q^2) mod I = ",xq2;
      "y^q mod I = ",yq;
      "y^(q^2) mod I = ",yq2;
      pause();
   }
   //==== l=2 =============================================================
   if(l==2)
   {
      xq=powerX(q,2,std(x3+a*x+b));
      J=std(ideal(xq-x,x3+a*x+b));
      if(deg(J[1])==0){t=1;}
      if(pr>=5)
      {
         "===================================================================";
         "the case l=2";
         "the gcd(x^q-x,x^3+ax+b)=",J[1];
         pause();
      }
      return(t);
   }
   //=== (F1/G1,F2/G2)=[ql](x,y) ==========================================
   if(ql==1)
   {
      F1=x;G1=1;F2=y;G2=1;
   }
   else
   {
      G1=reduce(generateG(a,b,ql)^2,I);
      F1=reduce(x*G1-generateG(a,b,ql-1)*generateG(a,b,ql+1),I);
      G2=reduce(4*y*generateG(a,b,ql)^3,I);
      F2=reduce(generateG(a,b,ql+2)*generateG(a,b,ql-1)^2
               -generateG(a,b,ql-2)*generateG(a,b,ql+1)^2,I);

   }
   if(pr>=5)
   {
      "===================================================================";
      "the point ql*(x,y)=(F1/G1,F2/G2)";
      "F1=",F1;
      "G1=",G1;
      "F2=",F2;
      "G2=",G2;
      pause();
   }
   //==== the case t=0 :  the equations for (x,y)^(q^2)=-[ql](x,y) ===
   J[1]=xq2*G1-F1;
   J[2]=yq2*G2+F2;
   if(pr>=5)
   {
      "===================================================================";
      "the case t=0 mod l";
      "the equations for (x,y)^(q^2)=-[ql](x,y) :";
      J;
      "the test, if they vanish for all points in C[l]:";
      reduce(J,I);
      pause();
   }
   //=== test if all points of C[l] satisfy  (x,y)^(q^2)=-[ql](x,y)
   //=== if so: t mod l =0 is returned
   if(size(reduce(J,I))==0){return(0);}

   //==== test for (x,y)^(q^2)=[ql](x,y) for some point

   J=xq2*G1-F1,yq2*G2-F2;
   J=std(J+I);
   if(pr>=5)
   {
      "===================================================================";
      "test if (x,y)^(q^2)=[ql](x,y) for one point";
      "if so, the Frobenius has an eigenvalue 2ql/t: (x,y)^q=(2ql/t)*(x,y)";
      "it follows that t^2=4q mod l";
      "if w is one square root of q mod l";
      "t =2w mod l or -2w mod l ";
      "-------------------------------------------------------------------";
      "the equations for (x,y)^(q^2)=[ql](x,y) :";
      xq2*G1-F1,yq2*G2-F2;
      "the test if one point satisfies them";
      J;
      pause();
   }
   if(deg(J[1])>0)
   {
      int w=int(squareRoot(q,l));
      //=== +/-2w mod l zurueckgeben, wenn (x,y)^q=+/-[w](x,y)
      //==== the case t>0 :  (Q1/P1,Q2/P2)=[w](x,y) ==============
      if(w==1)
      {
         Q1=x;P1=1;Q2=y;P2=1;
      }
      else
      {
         P1=reduce(generateG(a,b,w)^2,I);
         Q1=reduce(x*G1-generateG(a,b,w-1)*generateG(a,b,w+1),I);
         P2=reduce(4*y*generateG(a,b,w)^3,I);
         Q2=reduce(generateG(a,b,w+2)*generateG(a,b,w-1)^2
               -generateG(a,b,w-2)*generateG(a,b,w+1)^2,I);
      }
      J=xq*P1-Q1,yq*P2-Q2;
      J=std(I+J);
      if(pr>=5)
      {
      "===================================================================";
      "the Frobenius has an eigenvalue, one of the roots of  w^2=q mod l:";
      "one root is:";w;
      "test, if it is the eigenvalue (if not it must be -w):";
      "the equations for (x,y)^q=w*(x,y)";I;xq*P1-Q1,yq*P2-Q2;
      "the Groebner basis";
       J;
         pause();
      }
      if(deg(J[1])>0){return(2*w mod l);}
      return(-2*w mod l);
   }

   //==== the case t>0 :  (Q1/P1,Q2/P2)=(x,y)^(q^2)+[ql](x,y) =====
   P1=reduce(G1*G2^2*(F1-xq2*G1)^2,I);
   Q1=reduce((F2-yq2*G2)^2*G1^3-F1*G2^2*(F1-xq2*G1)^2-xq2*P1,I);
   P2=reduce(P1*G2*(F1-xq2*G1),I);
   Q2=reduce((xq2*P1-Q1)*(F2-yq2*G2)*G1-yq2*P2,I);

   if(pr>=5)
   {
      "we are in the general case:";
      "(x,y)^(q^2)!=ql*(x,y) and (x,y)^(q^2)!=-ql*(x,y) ";
      "the point (Q1/P1,Q2/P2)=(x,y)^(q^2)+[ql](x,y)";
      "Q1=",Q1;
      "P1=",P1;
      "Q2=",Q2;
      "P2=",P2;
      pause();
   }
   while(t<(l-1) div 2)
   {
      t++;
      //====  (H1/L1,H2/L2)=[t](x,y)^q ===============================
      if(t==1)
      {
         H1=xq;L1=1;
         H2=yq;L2=1;
      }
      else
      {
         H1=x*generateG(a,b,t)^2-generateG(a,b,t-1)*generateG(a,b,t+1);
         H1=subst(H1,x,xq,y,yq);
         H1=reduce(H1,I);
         L1=generateG(a,b,t)^2;
         L1=subst(L1,x,xq,y,yq);
         L1=reduce(L1,I);
         H2=generateG(a,b,t+2)*generateG(a,b,t-1)^2
           -generateG(a,b,t-2)*generateG(a,b,t+1)^2;
         H2=subst(H2,x,xq,y,yq);
         H2=reduce(H2,I);
         L2=4*y*generateG(a,b,t)^3;
         L2=subst(L2,x,xq,y,yq);
         L2=reduce(L2,I);
      }
      J=Q1*L1-P1*H1,Q2*L2-P2*H2;
      if(pr>=5)
      {
      "we test now the different t, 0<t<=(l-1)/2:";
      "the point (H1/L1,H2/L2)=[t](x,y)^q :";
      "H1=",H1;
      "L1=",L1;
      "H2=",H2;
      "L2=",L2;
      "the equations for (x,y)^(q^2)+[ql](x,y)=[t](x,y)^q :";J;
      "the test";reduce(J,I);
      "the test for l-t (the x-cordinate is the same):";
       Q1*L1-P1*H1,Q2*L2+P2*H2;
       reduce(ideal(Q1*L1-P1*H1,Q2*L2+P2*H2),I);
         pause();
      }
      if(size(reduce(J,I))==0){return(t);}
      J=Q1*L1-P1*H1,Q2*L2+P2*H2;
      if(size(reduce(J,I))==0){return(l-t);}
   }
   ERROR("something is wrong in testElliptic");
}
example
{ "EXAMPLE:"; echo = 2;
   testElliptic(1267985441,338474977,64740730,3);
}

//============================================================================
//================== Factorization and Primality Test ========================
//============================================================================

//============= Lenstra's ECM Factorization ==================================

proc factorLenstraECM(bigint N, list S, int B, list #)
"USAGE:  factorLenstraECM(N,S,B); optional: factorLenstraECM(N,S,B,d);
         d+1 the number of loops in the algorithm (default d=0)
RETURN: a factor of N or the message no factor found
NOTE: - computes a factor of N using Lenstra's ECM factorization@*
      - the idea is that the fact that N is not prime is dedected using
        the operations on the elliptic curve
      - is similarly to Pollard's p-1-factorization
EXAMPLE:example factorLenstraECM; shows an example
"
{
   list L,P;
   bigint g,M,w;
   int i,j,k,d;
   int l=size(S);
   if(size(#)>0)
   {
      d=#[1];
   }

   while(i<=d)
   {
      L=ellipticRandomCurve(N);
      if(L[3]>1){return(L[3]);} //the discriminant was not invertible
      P=list(0,L[2],1);
      j=0;
      M=1;
      while(j<l)
      {
         j++;
         w=S[j];
         if(w>B) break;
         while(w*S[j]<B)
         {
           w=w*S[j];
         }
         M=M*w;
         P=ellipticMult(N,L[1],L[2]^2,P,w);
         if(size(P)==4){return(P[4]);}  //some inverse did not exsist
         if(P[3]==0){break;}            //the case M*P=0
      }
      i++;
   }
   return("no factor found");
}
example
{ "EXAMPLE:"; echo = 2;
   list L=primList(1000);
   factorLenstraECM(181*32003,L,10,5);
   bigint h=10;
   h=h^30+25;
   factorLenstraECM(h,L,4,3);
}

//================= ECPP (Goldwasser-Kilian) a primaly-test =============

proc ECPP(bigint N)
"USAGE:  ECPP(N);
RETURN: message:N is not prime or {L,P,m,q} as certificate for N being prime@*
         L a list (y^2=x^3+L[1]*x+L[2] defines an elliptic curve C)@*
         P a list ((P[1]:P[2]:P[3]) is a point of C)@*
         m,q integers
ASSUME: gcd(N,6)=1
NOTE:   The basis of the algorithm is the following theorem:
         Given C, an elliptic curve over Z/N, P a point of C(Z/N),
         m an integer, q a prime with the following properties:
         - q|m
         - q>(4-th root(N) +1)^2
         - m*P=0=(0:1:0)
         - (m/q)*P=(x:y:z) and z a unit in Z/N
         Then N is prime.
EXAMPLE:example ECPP; shows an example
"
{
   list L,S,P;
   bigint m,q;
   int i;

   bigint n=intRoot(intRoot(N));
   n=(n+1)^2;                         //lower bound for q
   while(1)
   {
      L=ellipticRandomCurve(N);       //a random elliptic curve C
      m=ShanksMestre(N,L[1],L[2],3);  //number of points of the curve C
      S=PollardRho(m,10000,1);        //factorization of m
      for(i=1;i<=size(S);i++)         //search for q between the primes
      {
         q=S[i];
         if(n<q){break;}
      }
      if(n<q){break;}
   }
   bigint u=m/q;
   while(1)
   {
      P=ellipticRandomPoint(N,L[1],L[2]);  //a random point on C
      "P=",P;
      if(ellipticMult(N,L[1],L[2],P,m)[3]!=0){"N is not prime";return(-5);}
      if(ellipticMult(N,L[1],L[2],P,u)[3]!=0)
      {
         L=delete(L,3);
         return(list(L,P,m,q));
      }
   }
}
example
{ "EXAMPLE:"; echo = 2;
   bigint N=1267985441;
   ECPP(N);
}

static proc wordToNumber(string s)
{
   int i;
   intvec v;
   bigint n;
   bigint t=27;
   for(i=size(s);i>0;i--)
   {
      if(s[i]=="a"){v[i]=0;}
      if(s[i]=="b"){v[i]=1;}
      if(s[i]=="c"){v[i]=2;}
      if(s[i]=="d"){v[i]=3;}
      if(s[i]=="e"){v[i]=4;}
      if(s[i]=="f"){v[i]=5;}
      if(s[i]=="g"){v[i]=6;}
      if(s[i]=="h"){v[i]=7;}
      if(s[i]=="i"){v[i]=8;}
      if(s[i]=="j"){v[i]=9;}
      if(s[i]=="k"){v[i]=10;}
      if(s[i]=="l"){v[i]=11;}
      if(s[i]=="m"){v[i]=12;}
      if(s[i]=="n"){v[i]=13;}
      if(s[i]=="o"){v[i]=14;}
      if(s[i]=="p"){v[i]=15;}
      if(s[i]=="q"){v[i]=16;}
      if(s[i]=="r"){v[i]=17;}
      if(s[i]=="s"){v[i]=18;}
      if(s[i]=="t"){v[i]=19;}
      if(s[i]=="u"){v[i]=20;}
      if(s[i]=="v"){v[i]=21;}
      if(s[i]=="w"){v[i]=22;}
      if(s[i]=="x"){v[i]=23;}
      if(s[i]=="y"){v[i]=24;}
      if(s[i]=="z"){v[i]=25;}
      if(s[i]==" "){v[i]=26;}
   }
   for(i=1;i<=size(s);i++)
   {
      n=n+v[i]*t^(i-1);
   }
   return(n);
}

static proc numberToWord(bigint n)
{
   int i,j;
   string v;
   list s;
   bigint t=27;
   bigint mm;
   bigint nn=n;
   while(nn>t)
   {
      j++;
      mm=nn mod t;
      s[j]=mm;
      nn=(nn-mm) div t;
   }
   j++;
   s[j]=nn;
   for(i=1;i<=j;i++)
   {
      if(s[i]==0){v=v+"a";}
      if(s[i]==1){v=v+"b";}
      if(s[i]==2){v=v+"c";}
      if(s[i]==3){v=v+"d";}
      if(s[i]==4){v=v+"e";}
      if(s[i]==5){v=v+"f";}
      if(s[i]==6){v=v+"g";}
      if(s[i]==7){v=v+"h";}
      if(s[i]==8){v=v+"i";}
      if(s[i]==9){v=v+"j";}
      if(s[i]==10){v=v+"k";}
      if(s[i]==11){v=v+"l";}
      if(s[i]==12){v=v+"m";}
      if(s[i]==13){v=v+"n";}
      if(s[i]==14){v=v+"o";}
      if(s[i]==15){v=v+"p";}
      if(s[i]==16){v=v+"q";}
      if(s[i]==17){v=v+"r";}
      if(s[i]==18){v=v+"s";}
      if(s[i]==19){v=v+"t";}
      if(s[i]==20){v=v+"u";}
      if(s[i]==21){v=v+"v";}
      if(s[i]==22){v=v+"w";}
      if(s[i]==23){v=v+"x";}
      if(s[i]==24){v=v+"y";}
      if(s[i]==25){v=v+"z";}
      if(s[i]==26){v=v+" ";}
   }
   return(v);
}

proc code(string s)
"USAGE:  code(s); s a string
ASSUME:  s contains only small letters and space
COMPUTE: a bigint, RSA-coding of the string s
RETURN:  return RSA-coding of the string s as string
EXAMPLE: code;  shows an example
"
{
   ring r=0,x,dp;
   bigint
p=398075086424064937397125500550386491199064362342526708406385189575946388957261768583317;
   bigint
q=472772146107435302536223071973048224632914695302097116459852171130520711256363590397527;
   bigint n=p*q;
   bigint phi=(p-1)*(q-1);
   bigint e=1234567891;
   //bigint d=extgcd(e,phi)[2];
   bigint m=wordToNumber(s);
   bigint c=powerN(m,e,n);
   string cc=string(c);
   return(cc);
}
example
{"EXAMPLE:";  echo = 2;
  string s="i go to school";
  code(s);
}

proc decodeString(string g)
"USAGE:  decodeString(s); s a string
ASSUME:  s is a string of a bigint, the output of code
COMPUTE: a string, RSA-decoding of the string s
RETURN:  return RSA-decoding of the string s as string
EXAMPLE: decodeString;  shows an example
"
{
   bigint
p=398075086424064937397125500550386491199064362342526708406385189575946388957261768583317;
   bigint
q=472772146107435302536223071973048224632914695302097116459852171130520711256363590397527;
   bigint n=p*q;
   bigint phi=(p-1)*(q-1);
   bigint e=1234567891;
   bigint d=extgcd(e,phi)[2];
   execute("bigint c="+g+";");
   bigint f=powerN(c,d,n);
   string s=numberToWord(f);
   return(s);
}
example
{"EXAMPLE:";  echo = 2;
  string
s="78638618599886548153321853785991541374544958648147340831959482696082179852616053583234149080198937632782579537867262780982185252913122030800897193851413140758915381848932565";
  string t=decodeString(s);
  t;
}
/*----------------------------------------------------------------------------
 * set stuff
 * -------------------------------------------------------------------------*/
static proc set_multiply_list_content(list h)
"USAGE:   set_multiply_list_content(h)
RETURN:  An integer c als product of all elements in h
EXAMPLE: example set_multiply_list_content; shows an example;
"
{
  int c = 1;
  for (int i=1;i<=size(h);i++)
  {
    c = c*h[i];
  }
  return(c);
}
example
{
  "EXAMPLE:"; echo = 2;
  list h=2,4,5;
  set_multiply_list_content(h);
}

static proc set_delete_certain_element(list a, int e)
"USAGE:   set_delete_certain_element(a,e)
RETURN:  A list a without element e. If e was not in the list before, a will not be changed
EXAMPLE: example set_delete_certain_element; shows an example.
"
{
  list output_list = a;
  for (int i=1;i<=size(a);i++)
  {
    if (a[i]==e)
    {
      output_list = delete(output_list,i);
    }
  }
  return(output_list);
}
example
{
  "EXAMPLE:"; echo = 2;
  list h=2,4,5;
  set_delete_certain_element(h,4);
  set_delete_certain_element(h,10);
}

static proc set_bubblesort_int(list output_list)
"USAGE:   set_bubblesort_int(output_list)
RETURN:  An ascending sorted list with integer values.
EXAMPLE: set_bubblesort_int; shows an example.
"
{
  output_list = bubblesort(output_list);
  //Cast every value into an integer
  for (int i=1; i<=size(output_list);i++)
  {
    output_list[i] = int(output_list[i]);
  }
  return(output_list);
}
example
{
  "EXAMPLE:"; echo = 2;
  list output_list=10,4,5,24,9;
  set_bubblesort_int(output_list);
}

static proc set_is_set(list a)
"USAGE:   set_is_set(a)
RETURN:  1 if the list is a set, 0 the list contains any duplicated elements
EXAMPLE: set_is_set; shows an example.
"
{
  int i,v;
  for (v=1; v<=size(a); v++)
  {
    for (i=v+1; i<=size(a); i++)
    {
      if (a[i]==a[v])
      {
        return(0);
      }
    }
  }
  return(1);
}
example
{
  "EXAMPLE:"; echo = 2;
  list set = 1,5,10,2;
  list noset = 1,5,10,2,5;
  set_is_set(set);
  set_is_set(noset);
}

static proc set_contains(list a, int e)
"USAGE:   set_contains(a,e)
RETURN:  1 if the list contains e, 0 otherwise
EXAMPLE: set_contains; shows an example.
"
{
  for (int v=1; v<=size(a); v++)
  {
    if (a[v]==e)
    {
      return(1);
    }
  }
  return(0);
}
example
{
  "EXAMPLE:"; echo = 2;
  list set = 1,5,10,2;
  set_contains(set,5);
  set_contains(set,40);
}

static proc set_delete_duplicates(list a)
"USAGE:   set_delete_duplicates(a)
RETURN:  a list a without any duplicated elements
EXAMPLE: set_delete_duplicates; shows an example.
"
{
  int i;
  list output_list = a[1];
  for (i=1; i<=size(a); i++)
  {
    if (set_contains(output_list,a[i])==0)
    {
      output_list = insert(output_list,a[i]);
    }
  }
  return(output_list);
}
example
{
  "EXAMPLE:"; echo = 2;
  list set = 1,5,10,2,10,5;
  set_delete_duplicates(set);
}

static proc set_equals(list a,list b)
"USAGE:   set_equals(a, b)
RETURN:  1 if the lists are equal from a set-structure standpoint, 0 otherwise
EXAMPLE: set_equals; shows an example.
"
{
  //Checks if the lists have the same length
  if (size(a)!=size(b))
  {
    return(0);
  }

  //Sorts the lists
  a = set_bubblesort_int(a);
  b = set_bubblesort_int(b);

  //Checks every single element of both lists
  for (int i=1; i<=size(a); i++)
  {
    if (a[i]!=b[i])
    {
      return(0);
    }
  }
  return(1);
}
example
{
  "EXAMPLE:"; echo = 2;
  list set1 = 1,5,10,2;
  list set2 = 10,2,5,1;
  list set3 = 1,5,9,2;
  set_equals(set1,set2);
  set_equals(set1,set3);
}

static proc set_insert(list a, int e)
"USAGE:   set_insert(a,e)
RETURN:  list a containing element e
EXAMPLE: set_insert; shows an example.
"
{
  if(set_contains(a,e))
  {
    return(a);
  }
  else
  {
    a=insert(a,e);
    return(a);
  }
}
example
{
  "EXAMPLE:"; echo = 2;
  list set = 1,5,10,2;
  set_insert(set,5);
  set_insert(set,22);
}

static proc set_union(list a, list b)
"USAGE:   set_union(a, b)
RETURN:  list a as union of a and b
EXAMPLE: set_union; shows an example.
"
{
  for (int i=1; i<=size(b); i++)
  {
    if (set_contains(a,b[i])==0)
    {
      a = insert(a,b[i]);
    }
  }
  return(a);
}
example
{
  "EXAMPLE:"; echo = 2;
  list set1 = 1,5,10,2;
  list set2 = 5,10,93,58,29;
  set_union(set1,set2);
}

static proc set_section(list a, list b)
"USAGE:   set_section(a, b)
RETURN:  list output_list as intersection of a and b
EXAMPLE: set_section; shows an example.
"
{
  list output_list;
  for (int i=1; i<=size(a); i++)
  {
    if (set_contains(b,a[i])==1)
    {
      output_list = insert(output_list,a[i]);
    }
  }
  return(output_list);
}
example
{
  "EXAMPLE:"; echo = 2;
  list set1 = 1,5,10,2;
  list set2 = 5,10,93,58,29;
  set_section(set1,set2);
}

static proc set_list_delete_duplicates(list a)
"USAGE:   set_list_delete_duplicates(a)
RETURN:  list output_list with no duplicated lists
EXAMPLE: set_list_delete_duplicates; shows an example.
"
{
  int v;
  int i;
  int counter;
  int out_size;
  list output_list=insert(output_list,a[1]);
  //Create a new list and try to insert every list element from a into that list. If a list is already inserted into the
  //new list, do nothing.
  for (i=2; i<=size(a); i++)
  {
    out_size = size(output_list);
    counter = 0;

    for (v=1; v<=out_size; v++)
    {
      if (set_equals(output_list[v],a[i])==1)
      {
        counter++;
      }
    }
    if (counter==0)
    {
      output_list = insert(output_list,a[i]);
    }
  }
  return(output_list);
}
example
{
  "EXAMPLE:"; echo = 2;
  list set1 = 1,5,10,2;
  list set2 = 1,10,2,5;
  list set3 = 1,2,3;
  list superset = set1,set2,set3;
  set_list_delete_duplicates(superset);
}

static proc set_construct_increasing_set(int maxelement)
"USAGE:   set_construct_increasing_set(maxelement)
RETURN:  list output_list with increasing elements from 1 to maxelement
EXAMPLE: set_construct_increasing_set; shows an example.
"
{
  list output_list;
  for (int i=1; i<=maxelement; i++)
  {
    output_list = insert(output_list, i);
  }
  return(output_list);
}
example
{
  "EXAMPLE:"; echo = 2;
  set_construct_increasing_set(10);
}

static proc set_addtoall(list a, int element)
"USAGE:   set_addtoall(a,alement)
RETURN:  Transformed list with e_i+element for every element e_i in list a
EXAMPLE: set_addtoall; shows an example.
"
{
  list output_list;
  int c;
  for (int i=1; i<=size(a); i++)
  {
    c = a[i]+element;
    output_list = insert(output_list,c);
  }
  return(set_turn(output_list));
}
example
{
  "EXAMPLE:"; echo = 2;
  list set = 1,5,10,2;
  set_addtoall(set,5);
}

static proc set_turn(list a)
"USAGE:   set_turn(a)
RETURN:  Turned list a
EXAMPLE: set_turn; shows an example.
"
{
  list output_list;
  for (int i=1; i<=size(a); i++)
  {
    output_list[size(a)+1-i] = a[i];
  }
  return(output_list);
}
example
{
  "EXAMPLE:"; echo = 2;
  list set = 1,5,10;
  set_turn(set);
}

static proc set_subset_set(list a)
"USAGE:   set_subset_set(a)
RETURN:  Set of subsets
EXAMPLE: set_subset_set; shows an example.
"
{
  int v;
  int choice;
  list output_list;
  list start = a[1];
  list insertion_list;
  int output_length;
  output_list = insert(output_list,start);

  for (int i=2; i<=size(a); i++)
  {
    choice = 0;
    start = a[i];
    output_list = insert(output_list,start);
    output_length = size(output_list);
    for (v=2; v<=output_length; v++)
    {
      insertion_list = set_insert(output_list[v],a[i]);
      output_list = insert(output_list, insertion_list,size(output_list));
    }
  }
  return(output_list);
}
example
{
  "EXAMPLE:"; echo = 2;
  list set = 1,5,10;
  set_subset_set(set);
}
/* -----------------------------------------------------------------
 * knapsack_utilities: Utility functions needed for knapsack
 * -----------------------------------------------------------------*/
proc calculate_ordering(bigint num1, bigint primitive, bigint mod1)
"USAGE:   calculate_ordering(num1, primitive, mod1)
RETURN:  x so that primitive^x == num1 mod mod1
EXAMPLE: example calculate_ordering; shows an example;
"
{
  for (int i=1;i<=int((mod1-2));i++)
  {
    if ((primitive^i%mod1)==num1)
    {
      return(i);
    }
  }
  return(0);
}
example
{
  "EXAMPLE:"; echo = 2;
  bigint mod1 = 33;
  bigint primitive = 14;
  bigint num1 = 5;
  calculate_ordering(num1,primitive,mod1);
}

proc is_primitive_root(bigint primitive, bigint mod1)
"USAGE:   is_primitive_root(primitive, mod1)
RETURN:  1 if primitive is a primitive root modulo mod1, 0 otherwise
EXAMPLE: example is_primitive_root; shows an example;
"
{
  list output_list;
  for (int i=1;i<=int((mod1-1));i++)
  {
    output_list = set_insert(output_list,int((primitive^i%mod1)));
  }
  if (bigint(size(output_list))==bigint(mod1-1))
  {
    return(1);
  }
  else
  {
    return(0);
  }
}
example
{
  "EXAMPLE:"; echo = 2;
  is_primitive_root(3,7);
  is_primitive_root(2,7);
}

proc find_first_primitive_root(bigint mod1)
"USAGE:   find_first_primitive_root(mod1)
RETURN:  First primitive root modulo mod1, 0 if no root can be found.
EXAMPLE: example find_first_primitive_root; shows an example;
"
{
  for (int i=0;i<=int(mod1-1);i++)
  {
    if (is_primitive_root(bigint(i),mod1)==1)
    {
      return(i);
    }
  }
  return(0);
}
example
{
  "EXAMPLE:"; echo = 2;
  ring r = 0,x,lp;
  find_first_primitive_root(7);
  find_first_primitive_root(557);
}

proc binary_add(list binary_list)
"USAGE:   binary_add(binary_list)
RETURN:  binary encoded list, increased by 1
EXAMPLE: example binary_add; shows an example;
"
{
  int residual=1;
  int position = size(binary_list);
  while((residual==1)&&(position!=0))
  {
    if(binary_list[position]==0)
    {
      binary_list[position]=1;
      residual=0;
    }
    else
    {
      binary_list[position]=0;
      position--;
    }
  }
  if (position==0)
  {
    binary_list = insert(binary_list,1);
  }
  return(binary_list);
}
example
{
  "EXAMPLE:"; echo = 2;
  ring r = 0,x,lp;
  list binary_list = 1,0,1,1,1;
  binary_add(binary_list);
}

proc inverse_modulus(int num, int mod1)
"USAGE:   inverse_modulus(num, mod1)
RETURN:  inverse element of num modulo mod1
EXAMPLE: example inverse_modulus; shows an example;
"
{
  if (num>=mod1)
  {
    return(0);
  }
  else
  {
    for (int i=1;i<mod1;i++)
    {
      if ((i*num%mod1)==1)
      {
        return(i);
      }
    }
  }
}
example
{
  "EXAMPLE:"; echo = 2;
  ring r = 0,x,lp;
  int mod1 = 13;
  int num = 5;
  inverse_modulus(num,mod1);
}

proc is_prime(int n)
"USAGE:   is_prime(n)
RETURN:  1 if n is prime, 0 otherwise
EXAMPLE: example is_prime; shows an example;
"
{
  int prime1 = 1;
  for (int i=n-1;i>1;i--)
  {
    if(n%i==0)
    {
        prime1 = 0;
    }
  }
  return(prime1);
}
example
{
  "EXAMPLE:"; echo = 2;
  ring r = 0,x,lp;
  is_prime(10);
  is_prime(7);
}

proc find_biggest_index(list a)
"USAGE:   find_biggest_index( a)
RETURN:  Returns the index of the biggest element of a
EXAMPLE: example find_biggest_index; shows an example;
"
{
  list sortedlist = bubblesort(a);
  return(find_index(a,sortedlist[1]));
}

proc find_index(list a, bigint e)
"USAGE:   find_index(a, e)
RETURN:  Returns the list index of element e in list a. Returns 0 if e is not in a
EXAMPLE: example find_index; shows an example;
"
{
  for(int i=1;i<=size(a);i++)
  {
    if (bigint(a[i])==e)
    {
      return(i);
    }
  }
  return(0);
}
example
{
  "EXAMPLE:"; echo = 2;
  list a = 1,5,20,6,37;
  find_index(a,20);
  find_index(a,6);
  find_index(a,100);
}
/* ------------------------------------------------------------------
 * Knapsack Algorithmus such as solving several knapsack problems,
 * kryptographic algorithms and algorithms for creatings suitable knapsacks
 * ---------------------------------------------------------------- */
proc subset_sum01(list knapsack, int solution)
"USAGE:   subset_sum01(knapsack,solution)
RETURN:  binary list of the positions of the elements included in the subset sum or 0 if no solution exists
NOTE: This will return the first solution of the ssk-problem, given be the smallest binary encoding. It wont return several solutions if they exist
EXAMPLE: example subset_sum01; shows an example;
"
{
  int i;
  int v;
  int comparable;
  //Check if the knapsack is a set
  if (set_is_set(knapsack)==1)
  {
    //Create a binary list full of zeroes
    list binary_list;
    for (i=1;i<=size(knapsack);i++)
    {
      binary_list = insert(binary_list,0);
    }
    binary_list = binary_add(binary_list);

    for(i=1;i<=2^(size(knapsack));i++)
    {
      comparable = 0;
      //Create the Subset-Sum for the actual binary coding of binary_list
      for (v=1;v<=size(knapsack);v++)
      {
        comparable = comparable+knapsack[v]*binary_list[v];
      }
      //Check if the sum equals the solution
      if (comparable==solution)
      {
        return(binary_list);
      }
      else
      {
        binary_list = binary_add(binary_list);
      }
    }
    return(0);
  }
  else
  {
    return(0);
  }
}
example
{
  "EXAMPLE:"; echo = 2;
  list h=1,4,7,32;
  subset_sum01(h,20);
  subset_sum01(h,11);
  subset_sum01(h,33);
}

proc subset_sum02(list knapsack, int sol)
"USAGE:   subset_sum02(knapsack,sol)
RETURN:  binary list of the positions of the elements included in the subset sum or 0 if no solution exists
EXAMPLE: example subset_sum02; shows an example;
"
{
  list summands;
  int calcu;
  int i;
  //Create a sorted copy of the knapsack, calling it worksack
  list worksack = set_bubblesort_int(knapsack);
  int counter = 1;
  while((counter<=size(worksack))&&(sol>0))
  {
    //Try to substract an element of the knapsack from the capacity. Create a list with all the summands used
    calcu = sol-worksack[counter];
    if (calcu>=0)
    {
      sol = sol-worksack[counter];
      summands = insert(summands,int(worksack[counter]));
    }
    counter++;
  }
  if(sol>0)
  {
    return(0);
  }

  //Get the index of the summands of the original knapsack and change the bits in the binary list wich will be the solution
  list binary_list;
  for (i=1;i<=size(knapsack);i++)
  {
    binary_list = insert(binary_list,0);
  }

  for (i=1; i<=size(knapsack);i++)
  {
    if (set_contains(summands,knapsack[i])==1)
    {
      binary_list[i]=1;
    }
  }
  return(binary_list);
}
example
{
  "EXAMPLE:"; echo = 2;
  list h=1,4,7,32;
  subset_sum02(h,20);
  subset_sum02(h,11);
  subset_sum02(h,33);
}

proc unbounded_knapsack(list knapsack, list profit, int capacity)
"USAGE:   unbounded_knapsack(knapsack,profit,capacity)
RETURN:  list of maximum profit of each iteration. For example, output_list[2] contains the maximum profit that can be achieved if the knapsack has capacity 2.
EXAMPLE: unbounded_knapsack; shows an example;
"
{
  int i;
  int v;
  list output_list;
  for (i=1;i<=capacity+1;i++)
  {
    output_list = insert(output_list,0);
  }
  for (i=1;i<=capacity+1;i++)
  {
    for(v=1;v<=size(knapsack);v++)
    {
      if (knapsack[v]<i)
      {
        if(output_list[i]<(output_list[i-knapsack[v]]+profit[v]))
        {
          output_list[i] = output_list[i-knapsack[v]]+profit[v];
        }
      }
    }
  }
  return(output_list);
}
example
{
  "EXAMPLE:"; echo = 2;
  list h=1,4,7,32;
  list knapsack = 5,2;
  list profit = 10,3;
  int capacity = 5;
  unbounded_knapsack(knapsack,profit,capacity);
}

proc multidimensional_knapsack(matrix m, list capacities, list profits)
"USAGE:   multidimensional_knapsack(m,capacities,profits)
RETURN:  binary list of the positions of the elements included in the optimal selection
EXAMPLE: example multidimensional_knapsack; shows an example;
"
{
  int index;
  list output_list;
  list nolist;
  list y_list;
  list minmax_list;
  int i;
  int v;
  int checkint;
  //create the output_list full of zeroes with the length of all given selections
  for (i=1;i<=size(profits);i++)
  {
    output_list = insert(output_list,0);
  }

  //Create the List E with all indices of the output_list that haven't been used yet.
  list E = set_turn(set_construct_increasing_set(size(profits)));

  //Repeat till every index in E is used.
  while(size(E)>0)
  {
    y_list = nolist;
    for (i=1;i<=size(E);i++)
    {
      //Create all possible elements of y_i (y_list). minimax_list will be replaced of an empty list in each iteration
      minmax_list = nolist;
      for (v=1; v<=size(capacities);v++)
      {
        if (set_contains(E,v)==1)
        {
        minmax_list = insert(minmax_list, bigint(capacities[v])/bigint(m[v,E[i]]));
        }
      }
      //Sort the elements so that it is easy to pick the smallest one
      minmax_list = bubblesort(minmax_list);

      //insert Element y_i into y_list, wich is the smallest of (b_i/w_ij) like in the PECH algorithm description.
      y_list = insert(y_list,minmax_list[size(minmax_list)],size(y_list));


    }

    //Check if all y_i in y_list are smaller than 1. If so, every additional selection will exceed the capacity and the algorithm stops.
    checkint=0;
    for(i=1;i<=size(y_list);i++)
    {
      if (y_list[i]>=1)
      {
        checkint=1;
      }
    }
    if (checkint==0)
    {
      return(output_list);
    }


    //Find the index of the selection and update the binary output_list
    minmax_list = nolist;
    for (i=1;i<=size(E);i++)
    {
      minmax_list = insert(minmax_list, profits[E[i]]*y_list[i],size(minmax_list));
    }
    index = find_biggest_index(minmax_list);

    output_list[E[index]]=1;

    //Update the capacities by substracting the weights of the selection
    for (i=1;i<=size(capacities);i++)
    {
      capacities[i] = capacities[i]- m[i,E[index]];
    }
    E = set_delete_certain_element(E,index);

  }
  return(output_list);
}
example
{
  "EXAMPLE:"; echo = 2;
  ring r = 0,x,lp;
  matrix m[3][3] = 1,4,10,7,8,3,1,9,7;
  list c = 12,17,10;
  list p = 3,2,5;
  multidimensional_knapsack(m,c,p);
}

proc naccache_stern_generation(int key, int primenum)
"USAGE:   naccache_stern_generation(key, primenum)
RETURN:  a hard knapsack list
EXAMPLE: example naccache_stern_generation; shows an example;
"
{
  //Check if primenum is a prime and the gcd-Condition holds
  if ((is_prime(primenum)==0)||(gcd(key,(primenum-1))!=1))
  {
    return(0);
  }
  else
  {
    int i;
    int p;
    list primelist;
    int primecounter=2;
    //Generate the knapsack containing the smallest prime numbers so that primenum exceeds the product of all of them
    while(set_multiply_list_content(primelist)<primenum)
    {
      if (is_prime(primecounter)==1)
      {
      primelist = insert(primelist,primecounter);
      }
      primecounter++;
    }
    primelist = delete(primelist,1);


    //Generate the hard knapsack of the length of the prime numbers containing zeroes.
    list hardknapsack;
    for (i=1;i<=size(primelist);i++)
    {
      hardknapsack = insert(hardknapsack,0);
    }

    //Create the elements of the hard knapsack
    primecounter = 1;
    bigint calcu;
    i=0;
    while (i<size(primelist))
    {
      //Create some v_i^key%primenum and store it in calcu
      calcu = bigint(primecounter)^key%bigint(primenum);

      //If calcu is one of the prime numbers in the knapsack, find the index and insert v_i in the hard knapsack at that given index
      if(set_contains(primelist,int(calcu))==1)
      {
        p=find_index(primelist,int(calcu));
        hardknapsack[p] = primecounter;
        i++;
      }
      primecounter++;
    }
  return(hardknapsack);
  }
}
example
{
  "EXAMPLE:"; echo = 2;
  naccache_stern_generation(5,292);
  naccache_stern_generation(5,293);
}

proc naccache_stern_encryption(list knapsack, list message, int primenum)
"USAGE:   naccache_stern_encryption(knapsack, message, primenum)
RETURN:  an encrypted message as integer
EXAMPLE: example naccache_stern_encryption; shows an example;
"
{
  bigint solution = 1;
  if (size(knapsack)==size(message))
  {
    for(int i=1;i<=size(knapsack);i++)
    {
      solution = solution*((bigint(knapsack[i])^message[i])%bigint(primenum));
    }
    return(solution);
  }
  else
  {
    return(0);
  }

}
example
{
  "EXAMPLE:"; echo = 2;
  //Please note that the values for primenum and hardknapsack have been obtained from the example of naccache_stern_generation!
  list hardknapsack = 85,164,117,44;
  int primenum = 293;
  list message = 1,0,1,0;
  naccache_stern_encryption(hardknapsack,message,primenum);
}

proc naccache_stern_decryption(list knapsack, int key, int primenum, int message)
"USAGE:   naccache_stern_decryption(knapsack, key, primenum, message)
RETURN:  decrypted binary list
EXAMPLE: example naccache_stern_decryption; shows an example;
"
{
  //create a binary list with the length of the knapsack containing zeros
  int k = int(bigint(message)^key%bigint(primenum));
  int i;
  list binary_list;
  for (i=1;i<=size(knapsack);i++)
  {
    binary_list = insert(binary_list,0);
  }

  //create primelist like in int naccache_stern_generation process
  list primelist;
  int primecounter=2;
  while(size(primelist)<size(knapsack))
  {
    if (is_prime(primecounter)==1)
    {
      primelist = insert(primelist,primecounter);
    }
      primecounter++;
  }

  //find divisors of k and update the binarylist in a way that the positions of the divisors in primelist are marked
  for (i=1;i<=size(primelist);i++)
  {
    if(k%primelist[i]==0)
    {
      binary_list[i]=1;
    }
  }
  return(binary_list);

}
example
{
  "EXAMPLE:"; echo = 2;
  //Please note that the values have been obtained from the example of naccache_stern_generation and naccache_stern_encryption!
  int primenum = 293;
  int message = 9945;
  int key = 5;
  list hardknapsack = 85,164,117,44;
  naccache_stern_decryption(hardknapsack,key,primenum,message);
}

proc m_merkle_hellman_transformation(list knapsack, int primitive, int mod1)
"USAGE:   m_merkle_hellman_transformation(knapsack, primitive, mod1)
RETURN:  list containing a hard knapsack
EXAMPLE: example m_merkle_hellman_transformation; shows an example;
"
{
  bigint new_element;
  list output_list;
  //calculate the primitiv root of every element in knapsack and insert it into a new knapsack
  for (int i=size(knapsack);i>=1;i--)
  {
    new_element = calculate_ordering(knapsack[i],primitive,mod1);
    output_list = insert(output_list,int(new_element));
  }
  return(output_list);
}
example
{
  "EXAMPLE:"; echo = 2;
  //Please note that the values for primenum and hardknapsack have been obtained from the example of naccache_stern_generation and naccache_stern_encryption!
  list knapsack = 2,3,5,7;
  int mod1 = 211;
  int primitive = 2;
  m_merkle_hellman_transformation(knapsack,primitive,mod1);
}

proc m_merkle_hellman_encryption(list knapsack, list message)
"USAGE:    m_merkle_hellman_encryption(knapsack, message)
RETURN:  an encrypted message as integer
NOTE: This works in the same way as merkle_hellman_encryption. The additional function is created to keep consistency with the needed functions for every kryptosystem.
EXAMPLE: example m_merkle_hellman_encryption; shows an example;
"
{
  return(merkle_hellman_encryption(knapsack,message));
}
example
{
  "EXAMPLE:"; echo = 2;
  //Please note that the values for primenum and hardknapsack have been obtained from the example of m_merkle_hellman_transformation!
  list knapsack = 1,43,132,139;
  list message = 1,0,0,1;
  m_merkle_hellman_encryption(knapsack,message);
}

proc m_merkle_hellman_decryption(list knapsack, bigint primitive, bigint mod1, int message)
"USAGE:  m_merkle_hellman_decryption(knapsack, primitive, mod1, message)
RETURN:  decrypted binary list
EXAMPLE: example merkle_hellman_decryption; shows an example;
"
{
  //Convert message
  int factorizing = int((primitive^message)%mod1);
  int i;

  //Create binary list of length of the knapsack, containing zeroes.
  list binary_list;
  for (i=1;i<=size(knapsack);i++)
  {
    binary_list = insert(binary_list,0);
  }

  //factorize the converted message, mark the factor positions in knapsack as bits in binary_list
  for (i=1;i<=size(knapsack);i++)
  {
    if(factorizing%knapsack[i]==0)
    {
      binary_list[i]=1;
    }
  }
  return(binary_list);
}
example
{
  "EXAMPLE:"; echo = 2;
  //Please note that the values  have been obtained from the example of m_merkle_hellman_encryption and m_merkle_hellman_transformation!
  list knapsack = 2,3,5,7;
  int message = 140;
  bigint primitive = 2;
  bigint mod1 = 211;
  m_merkle_hellman_decryption(knapsack,primitive,mod1,message);
}

proc merkle_hellman_transformation(list knapsack, int key, int mod1)
"USAGE:   merkle_hellman_transformation(knapsack, key, mod1)
RETURN:  hard knapsack
EXAMPLE: example merkle_hellman_transformation; shows an example;
"
{
  list output_list;
  int new_element;
  //transform every element in the knapsack with normal strong modular multiplication
  for (int i=size(knapsack);i>=1;i--)
  {
    new_element=knapsack[i]*key%mod1;
    output_list = insert(output_list,new_element);
  }
  return(output_list);
}
example
{
  "EXAMPLE:"; echo = 2;
  list knapsack = 1,3,5,12;
  int key = 3;
  int mod1 = 23;
  merkle_hellman_transformation(knapsack,key,mod1);
}

proc merkle_hellman_encryption(list knapsack, list message)
"USAGE:   merkle_hellman_encryption(knapsack, message)
RETURN:  encrypted integer
EXAMPLE: example merkle_hellman_encryption; shows an example;
"
{
  int solution = 0;
  if (size(knapsack)!=size(message)||(set_is_set(knapsack)==0))
  {
    return(0);
  }
  else
  {
    for (int i=1;i<=size(knapsack);i++)
    {
      solution = solution+knapsack[i]*message[i];
    }
    return(solution);
  }
}
example
{
  "EXAMPLE:"; echo = 2;
  //Please note that the values  have been obtained from the example of merkle_hellman_transformation!
  list hardknapsack =3,9,15,13;
  list message = 0,1,0,1;
  merkle_hellman_encryption(hardknapsack,message);
}

proc merkle_hellman_decryption(list knapsack, int key, int mod1, int message)
"USAGE:   merkle_hellman_decryption(knapsack, key, mod1, message)
RETURN:  decrypted binary list
EXAMPLE: example merkle_hellman_decryption; shows an example;
"
{
  int new_element;
  int t = inverse_modulus(key,mod1);
  int transformed_message;
  list binary_list;
  if ((set_is_set(knapsack)==1)&&(key<mod1))
  {
    //reconstruct easy knapsack be multiplying with the inverse modulus t
    list easy_knapsack;
    for (int i=size(knapsack);i>=1;i--)
    {
      new_element=knapsack[i]*t%mod1;
      easy_knapsack = insert(easy_knapsack,new_element);
    }

    //solve the easy knapsack problem with subset_sum01 or subset_sum02
    transformed_message = (message*t)%mod1;
    transformed_message;
    binary_list = subset_sum01(easy_knapsack,transformed_message);
    return(binary_list)
  }
  else
  {
    return(0)
  }
}
example
{
  "EXAMPLE:"; echo = 2;
  //Please note that the values  have been obtained from the example of merkle_hellman_decryption and merkle_hellman_transformation!
  list hardknapsack =3,9,15,13;
  int key = 3;
  int message = 22;
  int mod1 = 23;
  merkle_hellman_decryption(hardknapsack, key, mod1, message);
}

proc super_increasing_knapsack(int ksize)
"USAGE:   super_increasing_knapsack(ksize)
RETURN:  super-increasing knapsack list
EXAMPLE: super_increasing_knapsack; shows an example;
"
{
  list output_list = insert(output_list,1);
  int next_element;

  for (int i=2; i<=ksize; i++)
  {
    next_element = calculate_max_sum(output_list)+1;
    output_list = insert(output_list,next_element);
  }
  return(output_list);
}
example
{
  "EXAMPLE:"; echo = 2;
  super_increasing_knapsack(10);
}

proc h_increasing_knapsack(int ksize, int h)
"USAGE:   h_increasing_knapsack(ksize, h)
RETURN:  h-increasing knapsack list
EXAMPLE: h_increasing_knapsack; shows an example;
"
{
  int v;
  if (ksize<=h+1)
  {
    return(set_turn(super_increasing_knapsack(ksize)))
  }
  else
  {
    list out = set_turn(super_increasing_knapsack(h+1));
    int next_element;
    for (int i=h+2; i<=ksize; i++)
    {
      next_element = 0;
      for (v=i-h; v<=i-1; v++)
      {
        next_element = next_element+out[v];
      }
      next_element++;
      out = insert(out,next_element,size(out));
    }
    return(out);
  }
}
example
{
  "EXAMPLE:"; echo = 2;
  h_increasing_knapsack(10,5);
}

proc injective_knapsack(int ksize, int kmaxelement)
"USAGE:   injective_knapsack(ksize, kmaxelement)
RETURN:  list of injective knapsacks with maximal element kmaxelement and size ksize
EXAMPLE: injective_knapsack; shows an example;
"
{
  //Create a List of size ksize with the greatest possible elements keeping the set structure
  list list_of_lists;
  list A = insert(A,kmaxelement);
  int i;
  for (i=2;i<=ksize;i++)
  {
    A = insert(A,kmaxelement-(i-1));
  }
  A = set_turn(A);
  list_of_lists = insert(list_of_lists,A);

  //Create all possible sets containing the possible elements of A
  int residual;
  int position;
  while(A[1]==kmaxelement)
  {
    residual=3;
    position = ksize;
    while((residual!=0))
    {
      if(A[position]==1)
      {
        A[position]=kmaxelement-position+1;
        residual=1;
        position--;
      }
      else
      {
        A[position]=A[position]-1;
        residual=0;
      }
    }
    //Insert the list into the overall list if its a set
    if (set_is_set(A)==1)
    {
      list_of_lists = insert(list_of_lists,A);
    }
  }
  //delete the first element since it is smaller than kmaxelement
  list_of_lists = delete(list_of_lists,1);
  //delete duplicates
  list_of_lists = set_list_delete_duplicates(list_of_lists);

  //Check if the remaining knapsacks are injective
  list output_list;
  for(i=1;i<=size(list_of_lists);i++)
  {
    if (set_is_injective(list_of_lists[i])==1)
    {
      output_list=insert(output_list,list_of_lists[i]);
    }
  }
  return(output_list);
}
example
{
  "EXAMPLE:"; echo = 2;
  injective_knapsack(3,9);
}

proc calculate_max_sum(list a)
"USAGE:   calculate_max_sum(a)
RETURN:  sum of all elements in a
EXAMPLE: calculate_max_sum; shows an example;
"
{
  int sum = a[1];
  for (int i=2; i<=size(a);i++)
  {
    sum = sum+a[i];
  }
  return(sum);
}
example
{
  "EXAMPLE:"; echo = 2;
  list a = 1,5,3,2,12;
  calculate_max_sum(a);
}

proc set_is_injective(list a)
"USAGE:   set_is_injective(a)
RETURN:  1 if a is injective, 0 otherwise
EXAMPLE: set_is_injective; shows an example;
"
{
  //Create all subsets of the set a
  list subsum = set_subset_set(a);
  list checklist=calculate_max_sum(subsum[1]);
  int calculator;
  for (int i=2; i<=size(subsum);i++)
  {
    //calculate the maximal subset_sum for every subset. Check if there are duplicated subset_sums. If so, a is not injective
    calculator = calculate_max_sum(subsum[i]);
    if (set_contains(checklist, calculator))
    {
      return(0);
    }
    else
    {
      checklist = insert(checklist,calculator);
    }
  }
  return(1);
}
example
{
  "EXAMPLE:"; echo = 2;
  list inj = 1,5,7,41;
  list non_inj = 1,2,3,4;
  set_is_injective(inj);
  set_is_injective(non_inj);
}

proc is_h_injective(list a, int h)
"USAGE:   is_h_injective(a, h)
RETURN:  1 if a is h-injective, 0 otherwise
EXAMPLE: is_h_injective; shows an example;
"
{
  //Create all sets of subsets
  list subsetlist = set_subset_set(a);
  list h_subsetlist;
  //delete every list with elements more than h+1 since they are not needed to check h-injectivity
  for (int i=1; i<=size(subsetlist); i++)
  {
    if(size(subsetlist[i])<=h)
    {
      h_subsetlist = insert(h_subsetlist,subsetlist[i]);
    }
  }

  //Check if the remaining max_sums do not occure more than once
  list checklist=calculate_max_sum(h_subsetlist[1]);
  int calculator;
  for (i=2; i<=size(h_subsetlist);i++)
  {
    calculator = calculate_max_sum(h_subsetlist[i]);
    if (set_contains(checklist, calculator)==1)
    {
      return(0);
    }
    else
    {
      checklist = insert(checklist,calculator);
    }
  }
  return(1);

}
example
{
  "EXAMPLE:"; echo = 2;
  list h_inj = 1,2,4,10,17;
  is_h_injective(h_inj,3);
  //1+2+4+10=17
  is_h_injective(h_inj,4);
}

proc is_fix_injective(list a)
"USAGE:   is_fix_injective(a)
RETURN:  1 if a is fix-injective, 0 otherwise
EXAMPLE: is_fix_injective; shows an example;
"
{
  //Generation of the list-list-list
  list subsetlist = set_subset_set(a);
  list alreadycreatedlist;
  list listoflists;
  list emptylist1;
  list worklist;
  int i;
  int v;
  list checklist;
  int calculator;

  int set_destination;

  //create list of lists wich contain the lists of a certain length as elements
  for (i = 1; i<= size(subsetlist); i++)
  {
    //Determine the size of the acutal list to choose where to insert it in the listoflists
    set_destination = size(subsetlist[i]);
    if (set_contains(alreadycreatedlist,set_destination)==1)
    {
      //There is already an element with the same set size, so just insert it
      listoflists[set_destination] = insert(listoflists[set_destination],subsetlist[i]);
    }
    else
    {
      //There is not yet an element with the same set size, so create a new one
      listoflists[set_destination] = insert(emptylist1,subsetlist[i]);
      alreadycreatedlist = set_insert(alreadycreatedlist,set_destination );
    }
  }

  //Check for injectivity of each seperate list. Works as in injectivity or h-injectivity
  for (v=1; v<=size(listoflists); v++)
  {
    worklist = listoflists[v];

    checklist=calculate_max_sum(worklist[1]);
    for (i=2; i<=size(worklist); i++)
    {
      calculator = calculate_max_sum(worklist[i]);
      if (set_contains(checklist, calculator)==1)
      {
        return(0);
      }
      else
      {
        checklist = insert(checklist,calculator);
      }
    }
  }
  return(1);
}
example
{
  "EXAMPLE:"; echo = 2;
  //this is fix-injective because 17=10+2+4+1 with different numbers of addens.
  list fix_inj = 1,2,4,10,17;
  //this is not fix-injective because 4+1=2+3.
  list not_fix_inj = 1,2,3,4;
  is_fix_injective(fix_inj);
  is_fix_injective(not_fix_inj);
}

proc three_elements(list out, int iterations)
"USAGE:   three_elements(out, iterations)
RETURN:  Injective_knapsack created with the three elements method
EXAMPLE: three_elements; shows an example;
"
{
  int a;
  int b;
  int c;
  int subsum;
  int adden = 1;
  int condition = 0;
  out = set_turn(out);
  if (set_is_injective(out)==0)
  {
    return(0);
  }
  else
  {
    for (int i=1; i<=iterations; i++)
    {
      while(condition==0)
      {
        subsum = calculate_max_sum(out);
        a = 2*subsum+adden;
        b = a+subsum+adden;
        c = b+subsum+1;
        if ((a+b)>(c+subsum))
        {
          condition=1;
        }
        else
        {
          adden++;
        }
      }
      adden =1;
      condition=0;
      out=set_insert(out, a);
      out=set_insert(out, b);
      out=set_insert(out, c);
    }
    return(out);
  }
}
example
{
  "EXAMPLE:"; echo = 2;
  //this is fix-injective because 17=10+2+4+1 with different numbers of addens.
  list super_increasing = 1,2,4,10,20;
  list a = three_elements(super_increasing,2);
  a;
  set_is_injective(a);
}
/*
//===============================================================
//=======  Example for DSA  =====================================
//===============================================================
Suppose a file test is given.It contains "Oscar".

//Hash-function MD5 under Linux

md5sum test           8edfe37dae96cfd2466d77d3884d4196

//================================================================

ring R=0,x,dp;

number q=2^19+21;          //524309
number o=2*3*23*number(7883)*number(16170811);

number p=o*q+1;            //9223372036869000547
number b=2;
number g=power(2,o,p);     //8308467587808723131

number a=111111;
number A=power(g,a,p);     //8566038811843553785

number h =decimal("8edfe37dae96cfd2466d77d3884d4196");

                           //189912871665444375716340628395668619670
h= h mod q;                //259847

number k=123456;

number ki=exgcd(k,q)[1];    //50804
                            //inverse von k mod q

number r= power(g,k,p) mod q;    //76646

number s=ki*(h+a*r) mod q;       //2065

//========== signatur is (r,s)=(76646,2065) =====================
//==================== verification  ============================

number si=exgcd(s,q)[1];  //inverse von s mod q
number e1=si*h mod q;
number e2=si*r mod q;
number rr=((power(g,e1,p)*power(A,e2,p)) mod p) mod q;    //76646

//===============================================================
//=======  Example for knapsack  ================================
//===============================================================
ring R=(5^5,t),x,dp;
R;
//   # ground field : 3125
//   primitive element : t
//   minpoly        : 1*t^5+4*t^1+2*t^0
//   number of vars : 1
//        block   1 : ordering dp
//                  : names    x
//        block   2 : ordering C

proc findEx(number n, number g)
{
   int i;
   for(i=0;i<=size(basering)-1;i++)
   {
      if(g^i==n){return(i);}
   }
}

number g=t^3;                       //choice of the primitive root

findEx(t+1,g);
//2091
findEx(t+2,g);
//2291
findEx(t+3,g);
//1043

intvec b=1,2091,2291,1043;          // k=4
int z=199;
intvec v=1043+z,1+z,2091+z,2291+z;  //permutation   pi=(0123)
v;
1242,200,2290,2490

//(1101)=(e_3,e_2,e_1,e_0)
//encoding 2490+2290+1242=6022 und 1+1+0+1=3

//(6022,3) decoding: c-z*c'=6022-199*3=5425

ring S=5,x,dp;
poly F=x5+4x+2;
poly G=reduce((x^3)^5425,std(F));
G;
//x3+x2+x+1

factorize(G);
//[1]:
//   _[1]=1
//   _[2]=x+1
//   _[3]=x-2
//   _[4]=x+2
//[2]:
//   1,1,1,1

//factors x+1,x+2,x+3, i.e.  (1110)=(e_pi(3),e_pi(2),e_pi(1),e_pi(0))

//pi(0)=1,pi(1)=2,pi(2)=3,pi(3)=0 gives:  (1101)

*/