1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
26
27
28
29
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
53
54
55
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
111
112
113
114
115
116
117
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
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
206
207
208
209
210
211
212
213
214
215
216
219
228
229
230
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
377
378
379
382
383
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
484
485
486
487
488
489
490
491
492
493
494
495
496
497
500
501
502
503
506
507
508
514
515
522
523
524
525
526
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
546
547
551
552
553
554
555
556
557
558
559
560
561
562
563
564
567
568
572
573
579
580
581
582
583
584
585
590
591
592
593
594
595
596
597
598
601
602
603
604
605
606
607
608
609
610
611
612
615
619
620
621
625
629
630
631
632
633
634
635
638
639
640
641
642
643
644
648
649
650
652
653
654
655
660
661
662
663
666
667
668
670
671
674
675
676
677
678
679
680
681
682
683
684
685
686
693
694
695
701
702
703
704
705
706
707
708
709
712
713
714
717
718
719
726
727
728
729
730
731
732
733
734
735
738
739
740
745
746
747
748
749
750
751
754
755
756
757
758
759
760
761
762
767
768
769
770
771
772
773
774
775
776
777
778
781
782
783
784
787
788
789
792
795
796
797
801
802
803
806
809
810
811
812
813
814
815
816
819
820
829
830
833
836
837
838
839
840
841
844
845
854
855
858
861
862
863
864
865
866
869
870
873
874
877
878
879
882
883
886
887
890
891
892
893
894
895
896
897
898
899
903
904
905
910
911
912
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
938
939
942
943
944
945
946
947
948
949
950
959
960
961
962
963
964
965
966
967
968
969
970
975
976
977
978
979
980
981
984
985
986
988
989
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1009
1010
1011
1012
1013
1014
1015
1016
1017
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1045
1046
1049
1050
1051
1057
1058
1059
1063
1064
1065
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
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1145
1146
1147
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1169
1170
1171
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1193
1194
1195
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1224
1225
1226
1229
1230
1231
1232
1233
1234
1235
1236
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1263
1266
1267
1268
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1292
1293
1297
1298
1299
1300
1301
1302
1303
1304
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
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
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1411
1412
1417
1418
1419
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
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1479
1480
1481
1482
1483
1484
1485
1486
1487
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1530
1531
1532
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1554
1557
1558
1559
1560
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1579
1580
1581
1582
1583
1584
1585
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1604
1605
1606
1610
1611
1612
1615
1616
1617
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1641
1642
1643
1654
1655
1656
1659
1660
1661
1662
1663
1664
1665
1666
1669
1670
1682
1683
1684
1685
1686
1690
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
1724
1725
1726
1737
1738
1741
1742
1743
1746
1747
1750
1751
1754
1755
1758
1759
1760
1761
1762
1763
1764
1765
1776
1777
1778
1779
1780
1781
1782
1785
1786
1792
1793
1794
1795
1796
1797
1798
1801
1802
1804
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1845
1846
1849
1850
1853
1854
1855
1856
1859
1862
1865
1866
1867
1868
1869
1870
1873
1874
1875
1876
1877
1882
1883
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
1932
1933
1934
1938
1939
1940
1941
1944
1945
1946
1955
1956
1957
1960
1961
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1984
1985
1988
1991
1992
1993
1994
1995
1998
1999
2000
2003
2004
2005
2008
2016
2017
2018
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2052
2055
2056
2057
2066
2067
2068
2069
2070
2071
2072
2073
2074
2082
2083
2084
2087
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2105
2108
2109
2110
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2147
2148
2149
2152
2153
2154
2155
2161
2162
2163
2164
2165
2167
2168
2169
2170
2171
2172
2173
2174
2178
2179
2180
2181
2182
2183
2184
2187
2188
2189
2190
2191
2194
2195
2196
2199
2200
2201
2204
2205
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2226
2227
2230
2231
2234
2235
2236
2237
2238
2239
2240
2241
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2272
2273
2274
2275
2276
2277
2280
2281
2282
2283
2284
2288
2289
2290
2291
2292
2293
2294
2295
2298
2299
2300
2301
2302
2303
2324
2325
2326
2327
2328
2329
2332
2333
2334
2337
2338
2339
2342
2343
2344
2353
2354
2355
2360
2361
2362
2363
2364
2365
2366
2367
2368
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2387
2388
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
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
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2490
2491
2492
2493
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2516
2517
2518
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2541
2542
2543
2544
2545
2546
2547
2548
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
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
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2640
2641
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
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
2747
2748
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2765
2766
2767
2770
2771
2772
2775
2776
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
2814
2815
2816
2817
2818
2819
2820
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2839
2840
2846
2847
2848
2849
2850
2851
2852
2853
2856
2857
2860
2863
2864
2865
2866
2867
2868
2869
2870
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2911
2912
2913
2914
2915
2916
2919
2920
2921
2922
2923
2928
2929
2932
2933
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2966
2967
2968
2969
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3005
3006
3007
3008
3009
3024
3025
3026
3030
3031
3032
3033
3036
3037
3038
3047
3048
3049
3050
3051
3056
3058
3059
3060
3061
3062
3063
3066
3067
3068
3069
3071
3072
3074
3075
3076
3077
3078
3081
3082
3084
3085
3086
3087
3092
3093
3095
3096
3097
3098
3099
3102
3103
3109
3110
3111
3112
3117
3119
3120
3121
3122
3125
3131
3132
3133
3136
3137
3145
3146
3147
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3185
3188
3189
3190
3199
3200
3201
3202
3203
3204
3205
3206
3207
3215
3216
3217
3220
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3238
3241
3242
3243
3244
3253
3254
3255
3256
3267
3268
3269
3270
3271
3272
3273
3274
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
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3329
3330
3331
3335
3336
3337
3338
3341
3342
3343
3344
3345
3346
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3367
3368
3369
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
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
3430
3431
3432
3433
3434
3435
3436
3437
/* ... */
#include "os.h"
#include "stdarg.h"
#ifndef CHAR_BIT
#define CHAR_BIT 8
#endif
#define BN_MP_INVMOD_C
#define BN_S_MP_EXPTMOD_C
/* ... */
#define BN_S_MP_MUL_DIGS_C
#define BN_MP_INVMOD_SLOW_C
#define BN_S_MP_SQR_C
#define BN_S_MP_MUL_HIGH_DIGS_C
/* ... */
#ifdef LTM_FAST
#define BN_MP_MUL_D_C
#define BN_MP_EXPTMOD_FAST_C
#define BN_MP_MONTGOMERY_SETUP_C
#define BN_FAST_MP_MONTGOMERY_REDUCE_C
#define BN_MP_MONTGOMERY_CALC_NORMALIZATION_C
#define BN_MP_MUL_2_C
#define BN_FAST_S_MP_SQR_C7 defines
/* ... */
#else
#define BN_MP_DIV_SMALL
#define BN_MP_INIT_MULTI_C
#define BN_MP_CLEAR_MULTI_C
#define BN_MP_ABS_C/* ... */
#endif
/* ... */
#define LTM_NO_NEG_EXP
#ifndef MIN
#define MIN(x,y) ((x)<(y)?(x):(y))
#endif
#ifndef MAX
#define MAX(x,y) ((x)>(y)?(x):(y))
#endif
#define OPT_CAST(x) (x *)
typedef unsigned long mp_digit;
typedef u64 mp_word;
#define DIGIT_BIT 28
#define MP_28BIT
#define XMALLOC os_malloc
#define XFREE os_free
#define XREALLOC os_realloc
#define MP_MASK ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))
#define MP_LT -1
#define MP_EQ 0
#define MP_GT 1
#define MP_ZPOS 0
#define MP_NEG 1
#define MP_OKAY 0
#define MP_MEM -2
#define MP_VAL -3
#define MP_YES 1
#define MP_NO 0 16 defines
typedef int mp_err;
#define MP_LOW_MEM
#ifndef MP_PREC
#ifndef MP_LOW_MEM
#define MP_PREC 32
#else
#define MP_PREC 8
#endif/* ... */
#endif
#define MP_WARRAY (1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1))
typedef struct {
int used, alloc, sign;
mp_digit *dp;
}{ ... } mp_int;
#define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
#define mp_iseven(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO)
#define mp_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO)
#define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1)
static int s_mp_exptmod(mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode);
static int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs);
static int s_mp_sqr(mp_int * a, mp_int * b);
static int s_mp_mul_high_digs(mp_int * a, mp_int * b, mp_int * c, int digs);
static int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs);
#ifdef BN_MP_INIT_MULTI_C
static int mp_init_multi(mp_int *mp, ...);
#endif
#ifdef BN_MP_CLEAR_MULTI_C
static void mp_clear_multi(mp_int *mp, ...);
#endif
static int mp_lshd(mp_int * a, int b);
static void mp_set(mp_int * a, mp_digit b);
static void mp_clamp(mp_int * a);
static void mp_exch(mp_int * a, mp_int * b);
static void mp_rshd(mp_int * a, int b);
static void mp_zero(mp_int * a);
static int mp_mod_2d(mp_int * a, int b, mp_int * c);
static int mp_div_2d(mp_int * a, int b, mp_int * c, mp_int * d);
static int mp_init_copy(mp_int * a, mp_int * b);
static int mp_mul_2d(mp_int * a, int b, mp_int * c);
#ifndef LTM_NO_NEG_EXP
static int mp_div_2(mp_int * a, mp_int * b);
static int mp_invmod(mp_int * a, mp_int * b, mp_int * c);
static int mp_invmod_slow(mp_int * a, mp_int * b, mp_int * c);/* ... */
#endif
static int mp_copy(mp_int * a, mp_int * b);
static int mp_count_bits(mp_int * a);
static int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d);
static int mp_mod(mp_int * a, mp_int * b, mp_int * c);
static int mp_grow(mp_int * a, int size);
static int mp_cmp_mag(mp_int * a, mp_int * b);
#ifdef BN_MP_ABS_C
static int mp_abs(mp_int * a, mp_int * b);
#endif
static int mp_sqr(mp_int * a, mp_int * b);
static int mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d);
static int mp_reduce_2k_setup_l(mp_int *a, mp_int *d);
static int mp_2expt(mp_int * a, int b);
static int mp_reduce_setup(mp_int * a, mp_int * b);
static int mp_reduce(mp_int * x, mp_int * m, mp_int * mu);
static int mp_init_size(mp_int * a, int size);
#ifdef BN_MP_EXPTMOD_FAST_C
static int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode);
#endif
#ifdef BN_FAST_S_MP_SQR_C
static int fast_s_mp_sqr (mp_int * a, mp_int * b);
#endif
#ifdef BN_MP_MUL_D_C
static int mp_mul_d (mp_int * a, mp_digit b, mp_int * c);
#endif
static void
bn_reverse (unsigned char *s, int len)
{
int ix, iy;
unsigned char t;
ix = 0;
iy = len - 1;
while (ix < iy) {
t = s[ix];
s[ix] = s[iy];
s[iy] = t;
++ix;
--iy;
}{...}
}{ ... }
static int
s_mp_add (mp_int * a, mp_int * b, mp_int * c)
{
mp_int *x;
int olduse, res, min, max;
/* ... */
if (a->used > b->used) {
min = b->used;
max = a->used;
x = a;
}{...} else {
min = a->used;
max = b->used;
x = b;
}{...}
if (c->alloc < max + 1) {
if ((res = mp_grow (c, max + 1)) != MP_OKAY) {
return res;
}{...}
}{...}
olduse = c->used;
c->used = max + 1;
{
register mp_digit u, *tmpa, *tmpb, *tmpc;
register int i;
tmpa = a->dp;
tmpb = b->dp;
tmpc = c->dp;
u = 0;
for (i = 0; i < min; i++) {
*tmpc = *tmpa++ + *tmpb++ + u;
u = *tmpc >> ((mp_digit)DIGIT_BIT);
*tmpc++ &= MP_MASK;
}{...}
/* ... */
if (min != max) {
for (; i < max; i++) {
*tmpc = x->dp[i] + u;
u = *tmpc >> ((mp_digit)DIGIT_BIT);
*tmpc++ &= MP_MASK;
}{...}
}{...}
*tmpc++ = u;
for (i = c->used; i < olduse; i++) {
*tmpc++ = 0;
}{...}
}{...}
mp_clamp (c);
return MP_OKAY;
}{ ... }
static int
s_mp_sub (mp_int * a, mp_int * b, mp_int * c)
{
int olduse, res, min, max;
min = b->used;
max = a->used;
if (c->alloc < max) {
if ((res = mp_grow (c, max)) != MP_OKAY) {
return res;
}{...}
}{...}
olduse = c->used;
c->used = max;
{
register mp_digit u, *tmpa, *tmpb, *tmpc;
register int i;
tmpa = a->dp;
tmpb = b->dp;
tmpc = c->dp;
u = 0;
for (i = 0; i < min; i++) {
*tmpc = *tmpa++ - *tmpb++ - u;
/* ... */
u = *tmpc >> ((mp_digit)(CHAR_BIT * sizeof (mp_digit) - 1));
*tmpc++ &= MP_MASK;
}{...}
for (; i < max; i++) {
*tmpc = *tmpa++ - u;
u = *tmpc >> ((mp_digit)(CHAR_BIT * sizeof (mp_digit) - 1));
*tmpc++ &= MP_MASK;
}{...}
for (i = c->used; i < olduse; i++) {
*tmpc++ = 0;
}{...}
}{...}
mp_clamp (c);
return MP_OKAY;
}{ ... }
static int
mp_init (mp_int * a)
{
int i;
a->dp = OPT_CAST(mp_digit) XMALLOC (sizeof (mp_digit) * MP_PREC);
if (a->dp == NULL) {
return MP_MEM;
}{...}
for (i = 0; i < MP_PREC; i++) {
a->dp[i] = 0;
}{...}
/* ... */
a->used = 0;
a->alloc = MP_PREC;
a->sign = MP_ZPOS;
return MP_OKAY;
}{ ... }
static void
mp_clear (mp_int * a)
{
int i;
if (a->dp != NULL) {
for (i = 0; i < a->used; i++) {
a->dp[i] = 0;
}{...}
XFREE(a->dp);
a->dp = NULL;
a->alloc = a->used = 0;
a->sign = MP_ZPOS;
}{...}
}{ ... }
static int
mp_add (mp_int * a, mp_int * b, mp_int * c)
{
int sa, sb, res;
sa = a->sign;
sb = b->sign;
if (sa == sb) {
c->sign = sa;
res = s_mp_add (a, b, c);
}{...} else {
if (mp_cmp_mag (a, b) == MP_LT) {
c->sign = sb;
res = s_mp_sub (b, a, c);
}{...} else {
c->sign = sa;
res = s_mp_sub (a, b, c);
}{...}
}{...}
return res;
}{ ... }
static int
mp_sub (mp_int * a, mp_int * b, mp_int * c)
{
int sa, sb, res;
sa = a->sign;
sb = b->sign;
if (sa != sb) {
c->sign = sa;
res = s_mp_add (a, b, c);
}{...} else {
if (mp_cmp_mag (a, b) != MP_LT) {
c->sign = sa;
res = s_mp_sub (a, b, c);
}{...} else {
c->sign = (sa == MP_ZPOS) ? MP_NEG : MP_ZPOS;
res = s_mp_sub (b, a, c);
}{...}
}{...}
return res;
}{ ... }
static int
mp_mul (mp_int * a, mp_int * b, mp_int * c)
{
int res, neg;
neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
#ifdef BN_MP_TOOM_MUL_C
if (MIN (a->used, b->used) >= TOOM_MUL_CUTOFF) {
res = mp_toom_mul(a, b, c);
}{...} else/* ... */
#endif
#ifdef BN_MP_KARATSUBA_MUL_C
if (MIN (a->used, b->used) >= KARATSUBA_MUL_CUTOFF) {
res = mp_karatsuba_mul (a, b, c);
}{...} else
#endif
{
/* ... */
#ifdef BN_FAST_S_MP_MUL_DIGS_C
int digs = a->used + b->used + 1;
if ((digs < MP_WARRAY) &&
MIN(a->used, b->used) <=
(1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
res = fast_s_mp_mul_digs (a, b, c, digs);
}{...} else/* ... */
#endif
#ifdef BN_S_MP_MUL_DIGS_C
res = s_mp_mul (a, b, c);
#else
#error mp_mul could fail
res = MP_VAL;/* ... */
#endif
}{...}
c->sign = (c->used > 0) ? neg : MP_ZPOS;
return res;
}{ ... }
static int
mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
{
int res;
mp_int t;
if ((res = mp_init (&t)) != MP_OKAY) {
return res;
}{...}
if ((res = mp_mul (a, b, &t)) != MP_OKAY) {
mp_clear (&t);
return res;
}{...}
res = mp_mod (&t, c, d);
mp_clear (&t);
return res;
}{ ... }
static int
mp_mod (mp_int * a, mp_int * b, mp_int * c)
{
mp_int t;
int res;
if ((res = mp_init (&t)) != MP_OKAY) {
return res;
}{...}
if ((res = mp_div (a, b, NULL, &t)) != MP_OKAY) {
mp_clear (&t);
return res;
}{...}
if (t.sign != b->sign) {
res = mp_add (b, &t, c);
}{...} else {
res = MP_OKAY;
mp_exch (&t, c);
}{...}
mp_clear (&t);
return res;
}{ ... }
/* ... */
static int
mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
{
#if defined(BN_MP_DR_IS_MODULUS_C)||defined(BN_MP_REDUCE_IS_2K_C)||defined(BN_MP_EXPTMOD_FAST_C)
int dr = 0;
#endif
if (P->sign == MP_NEG) {
return MP_VAL;
}{...}
if (X->sign == MP_NEG) {
#ifdef LTM_NO_NEG_EXP
return MP_VAL;
#else
#ifdef BN_MP_INVMOD_C
mp_int tmpG, tmpX;
int err;
if ((err = mp_init(&tmpG)) != MP_OKAY) {
return err;
}{...}
if ((err = mp_invmod(G, P, &tmpG)) != MP_OKAY) {
mp_clear(&tmpG);
return err;
}{...}
if ((err = mp_init(&tmpX)) != MP_OKAY) {
mp_clear(&tmpG);
return err;
}{...}
if ((err = mp_abs(X, &tmpX)) != MP_OKAY) {
mp_clear_multi(&tmpG, &tmpX, NULL);
return err;
}{...}
err = mp_exptmod(&tmpG, &tmpX, P, Y);
mp_clear_multi(&tmpG, &tmpX, NULL);
return err;/* ... */
#else
#error mp_exptmod would always fail
return MP_VAL;/* ... */
#endif/* ... */
#endif
}{...}
#if defined(BN_MP_REDUCE_IS_2K_L_C) && defined(BN_MP_REDUCE_2K_L_C) && defined(BN_S_MP_EXPTMOD_C)
if (mp_reduce_is_2k_l(P) == MP_YES) {
return s_mp_exptmod(G, X, P, Y, 1);
}{...}
#endif/* ... */
#ifdef BN_MP_DR_IS_MODULUS_C
dr = mp_dr_is_modulus(P);/* ... */
#endif
#ifdef BN_MP_REDUCE_IS_2K_C
if (dr == 0) {
dr = mp_reduce_is_2k(P) << 1;
}{...}
#endif/* ... */
#ifdef BN_MP_EXPTMOD_FAST_C
if (mp_isodd (P) == 1 || dr != 0) {
return mp_exptmod_fast (G, X, P, Y, dr);
}{...} else {
#endif
#ifdef BN_S_MP_EXPTMOD_C
return s_mp_exptmod (G, X, P, Y, 0);/* ... */
#else
#error mp_exptmod could fail
return MP_VAL;/* ... */
#endif
#ifdef BN_MP_EXPTMOD_FAST_C
}{...}
#endif
}{ ... }
static int
mp_cmp (mp_int * a, mp_int * b)
{
if (a->sign != b->sign) {
if (a->sign == MP_NEG) {
return MP_LT;
}{...} else {
return MP_GT;
}{...}
}{...}
if (a->sign == MP_NEG) {
return mp_cmp_mag(b, a);
}{...} else {
return mp_cmp_mag(a, b);
}{...}
}{ ... }
static int
mp_cmp_d(mp_int * a, mp_digit b)
{
if (a->sign == MP_NEG) {
return MP_LT;
}{...}
if (a->used > 1) {
return MP_GT;
}{...}
if (a->dp[0] > b) {
return MP_GT;
}{...} else if (a->dp[0] < b) {
return MP_LT;
}{...} else {
return MP_EQ;
}{...}
}{ ... }
#ifndef LTM_NO_NEG_EXP
static int
mp_invmod (mp_int * a, mp_int * b, mp_int * c)
{
if (b->sign == MP_NEG || mp_iszero(b) == 1) {
return MP_VAL;
}{...}
#ifdef BN_FAST_MP_INVMOD_C
if (mp_isodd (b) == 1) {
return fast_mp_invmod (a, b, c);
}{...}
/* ... */#endif
#ifdef BN_MP_INVMOD_SLOW_C
return mp_invmod_slow(a, b, c);
#endif
#ifndef BN_FAST_MP_INVMOD_C
#ifndef BN_MP_INVMOD_SLOW_C
#error mp_invmod would always fail
#endif/* ... */
#endif
return MP_VAL;
}{...}
/* ... */#endif
static int
mp_unsigned_bin_size (mp_int * a)
{
int size = mp_count_bits (a);
return (size / 8 + ((size & 7) != 0 ? 1 : 0));
}{ ... }
#ifndef LTM_NO_NEG_EXP
static int
mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c)
{
mp_int x, y, u, v, A, B, C, D;
int res;
if (b->sign == MP_NEG || mp_iszero(b) == 1) {
return MP_VAL;
}{...}
if ((res = mp_init_multi(&x, &y, &u, &v,
&A, &B, &C, &D, NULL)) != MP_OKAY) {
return res;
}{...}
if ((res = mp_mod(a, b, &x)) != MP_OKAY) {
goto LBL_ERR;
}{...}
if ((res = mp_copy (b, &y)) != MP_OKAY) {
goto LBL_ERR;
}{...}
if (mp_iseven (&x) == 1 && mp_iseven (&y) == 1) {
res = MP_VAL;
goto LBL_ERR;
}{...}
if ((res = mp_copy (&x, &u)) != MP_OKAY) {
goto LBL_ERR;
}{...}
if ((res = mp_copy (&y, &v)) != MP_OKAY) {
goto LBL_ERR;
}{...}
mp_set (&A, 1);
mp_set (&D, 1);
top:
while (mp_iseven (&u) == 1) {
if ((res = mp_div_2 (&u, &u)) != MP_OKAY) {
goto LBL_ERR;
}{...}
if (mp_isodd (&A) == 1 || mp_isodd (&B) == 1) {
if ((res = mp_add (&A, &y, &A)) != MP_OKAY) {
goto LBL_ERR;
}{...}
if ((res = mp_sub (&B, &x, &B)) != MP_OKAY) {
goto LBL_ERR;
}{...}
}{...}
if ((res = mp_div_2 (&A, &A)) != MP_OKAY) {
goto LBL_ERR;
}{...}
if ((res = mp_div_2 (&B, &B)) != MP_OKAY) {
goto LBL_ERR;
}{...}
}{...}
while (mp_iseven (&v) == 1) {
if ((res = mp_div_2 (&v, &v)) != MP_OKAY) {
goto LBL_ERR;
}{...}
if (mp_isodd (&C) == 1 || mp_isodd (&D) == 1) {
if ((res = mp_add (&C, &y, &C)) != MP_OKAY) {
goto LBL_ERR;
}{...}
if ((res = mp_sub (&D, &x, &D)) != MP_OKAY) {
goto LBL_ERR;
}{...}
}{...}
if ((res = mp_div_2 (&C, &C)) != MP_OKAY) {
goto LBL_ERR;
}{...}
if ((res = mp_div_2 (&D, &D)) != MP_OKAY) {
goto LBL_ERR;
}{...}
}{...}
if (mp_cmp (&u, &v) != MP_LT) {
if ((res = mp_sub (&u, &v, &u)) != MP_OKAY) {
goto LBL_ERR;
}{...}
if ((res = mp_sub (&A, &C, &A)) != MP_OKAY) {
goto LBL_ERR;
}{...}
if ((res = mp_sub (&B, &D, &B)) != MP_OKAY) {
goto LBL_ERR;
}{...}
}{...} else {
if ((res = mp_sub (&v, &u, &v)) != MP_OKAY) {
goto LBL_ERR;
}{...}
if ((res = mp_sub (&C, &A, &C)) != MP_OKAY) {
goto LBL_ERR;
}{...}
if ((res = mp_sub (&D, &B, &D)) != MP_OKAY) {
goto LBL_ERR;
}{...}
}{...}
if (mp_iszero (&u) == 0)
goto top;
if (mp_cmp_d (&v, 1) != MP_EQ) {
res = MP_VAL;
goto LBL_ERR;
}{...}
while (mp_cmp_d(&C, 0) == MP_LT) {
if ((res = mp_add(&C, b, &C)) != MP_OKAY) {
goto LBL_ERR;
}{...}
}{...}
while (mp_cmp_mag(&C, b) != MP_LT) {
if ((res = mp_sub(&C, b, &C)) != MP_OKAY) {
goto LBL_ERR;
}{...}
}{...}
mp_exch (&C, c);
res = MP_OKAY;
LBL_ERR:mp_clear_multi (&x, &y, &u, &v, &A, &B, &C, &D, NULL);
return res;
}{...}
/* ... */#endif
static int
mp_cmp_mag (mp_int * a, mp_int * b)
{
int n;
mp_digit *tmpa, *tmpb;
if (a->used > b->used) {
return MP_GT;
}{...}
if (a->used < b->used) {
return MP_LT;
}{...}
tmpa = a->dp + (a->used - 1);
tmpb = b->dp + (a->used - 1);
for (n = 0; n < a->used; ++n, --tmpa, --tmpb) {
if (*tmpa > *tmpb) {
return MP_GT;
}{...}
if (*tmpa < *tmpb) {
return MP_LT;
}{...}
}{...}
return MP_EQ;
}{ ... }
static int
mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c)
{
int res;
if (a->alloc < 2) {
if ((res = mp_grow(a, 2)) != MP_OKAY) {
return res;
}{...}
}{...}
mp_zero (a);
while (c-- > 0) {
if ((res = mp_mul_2d (a, 8, a)) != MP_OKAY) {
return res;
}{...}
#ifndef MP_8BIT
a->dp[0] |= *b++;
a->used += 1;/* ... */
#else
a->dp[0] = (*b & MP_MASK);
a->dp[1] |= ((*b++ >> 7U) & 1);
a->used += 2;/* ... */
#endif
}{...}
mp_clamp (a);
return MP_OKAY;
}{ ... }
static int
mp_to_unsigned_bin (mp_int * a, unsigned char *b)
{
int x, res;
mp_int t;
if ((res = mp_init_copy (&t, a)) != MP_OKAY) {
return res;
}{...}
x = 0;
while (mp_iszero (&t) == 0) {
#ifndef MP_8BIT
b[x++] = (unsigned char) (t.dp[0] & 255);
#else
b[x++] = (unsigned char) (t.dp[0] | ((t.dp[1] & 0x01) << 7));
#endif
if ((res = mp_div_2d (&t, 8, &t, NULL)) != MP_OKAY) {
mp_clear (&t);
return res;
}{...}
}{...}
bn_reverse (b, x);
mp_clear (&t);
return MP_OKAY;
}{ ... }
static int
mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d)
{
mp_digit D, r, rr;
int x, res;
mp_int t;
if (b <= 0) {
res = mp_copy (a, c);
if (d != NULL) {
mp_zero (d);
}{...}
return res;
}{...}
if ((res = mp_init (&t)) != MP_OKAY) {
return res;
}{...}
if (d != NULL) {
if ((res = mp_mod_2d (a, b, &t)) != MP_OKAY) {
mp_clear (&t);
return res;
}{...}
}{...}
if ((res = mp_copy (a, c)) != MP_OKAY) {
mp_clear (&t);
return res;
}{...}
if (b >= (int)DIGIT_BIT) {
mp_rshd (c, b / DIGIT_BIT);
}{...}
D = (mp_digit) (b % DIGIT_BIT);
if (D != 0) {
register mp_digit *tmpc, mask, shift;
mask = (((mp_digit)1) << D) - 1;
shift = DIGIT_BIT - D;
tmpc = c->dp + (c->used - 1);
r = 0;
for (x = c->used - 1; x >= 0; x--) {
rr = *tmpc & mask;
*tmpc = (*tmpc >> D) | (r << shift);
--tmpc;
r = rr;
}{...}
}{...}
mp_clamp (c);
if (d != NULL) {
mp_exch (&t, d);
}{...}
mp_clear (&t);
return MP_OKAY;
}{ ... }
static int
mp_init_copy (mp_int * a, mp_int * b)
{
int res;
if ((res = mp_init (a)) != MP_OKAY) {
return res;
}{...}
return mp_copy (b, a);
}{ ... }
static void
mp_zero (mp_int * a)
{
int n;
mp_digit *tmp;
a->sign = MP_ZPOS;
a->used = 0;
tmp = a->dp;
for (n = 0; n < a->alloc; n++) {
*tmp++ = 0;
}{...}
}{ ... }
static int
mp_copy (mp_int * a, mp_int * b)
{
int res, n;
if (a == b) {
return MP_OKAY;
}{...}
if (b->alloc < a->used) {
if ((res = mp_grow (b, a->used)) != MP_OKAY) {
return res;
}{...}
}{...}
{
register mp_digit *tmpa, *tmpb;
tmpa = a->dp;
tmpb = b->dp;
for (n = 0; n < a->used; n++) {
*tmpb++ = *tmpa++;
}{...}
for (; n < b->used; n++) {
*tmpb++ = 0;
}{...}
}{...}
b->used = a->used;
b->sign = a->sign;
return MP_OKAY;
}{ ... }
static void
mp_rshd (mp_int * a, int b)
{
int x;
if (b <= 0) {
return;
}{...}
if (a->used <= b) {
mp_zero (a);
return;
}{...}
{
register mp_digit *bottom, *top;
bottom = a->dp;
top = a->dp + b;
/* ... */
for (x = 0; x < (a->used - b); x++) {
*bottom++ = *top++;
}{...}
for (; x < a->used; x++) {
*bottom++ = 0;
}{...}
}{...}
a->used -= b;
}{ ... }
/* ... */
static void
mp_exch (mp_int * a, mp_int * b)
{
mp_int t;
t = *a;
*a = *b;
*b = t;
}{ ... }
/* ... */
static void
mp_clamp (mp_int * a)
{
/* ... */
while (a->used > 0 && a->dp[a->used - 1] == 0) {
--(a->used);
}{...}
if (a->used == 0) {
a->sign = MP_ZPOS;
}{...}
}{ ... }
static int
mp_grow (mp_int * a, int size)
{
int i;
mp_digit *tmp;
if (a->alloc < size) {
size += (MP_PREC * 2) - (size % MP_PREC);
/* ... */
tmp = OPT_CAST(mp_digit) XREALLOC (a->dp, sizeof (mp_digit) * size);
if (tmp == NULL) {
return MP_MEM;
}{...}
a->dp = tmp;
i = a->alloc;
a->alloc = size;
for (; i < a->alloc; i++) {
a->dp[i] = 0;
}{...}
}{...}
return MP_OKAY;
}{ ... }
#ifdef BN_MP_ABS_C
/* ... */
static int
mp_abs (mp_int * a, mp_int * b)
{
int res;
if (a != b) {
if ((res = mp_copy (a, b)) != MP_OKAY) {
return res;
}{...}
}{...}
b->sign = MP_ZPOS;
return MP_OKAY;
}{ ... }
#endif/* ... */
static void
mp_set (mp_int * a, mp_digit b)
{
mp_zero (a);
a->dp[0] = b & MP_MASK;
a->used = (a->dp[0] != 0) ? 1 : 0;
}{ ... }
#ifndef LTM_NO_NEG_EXP
static int
mp_div_2(mp_int * a, mp_int * b)
{
int x, res, oldused;
if (b->alloc < a->used) {
if ((res = mp_grow (b, a->used)) != MP_OKAY) {
return res;
}{...}
}{...}
oldused = b->used;
b->used = a->used;
{
register mp_digit r, rr, *tmpa, *tmpb;
tmpa = a->dp + b->used - 1;
tmpb = b->dp + b->used - 1;
r = 0;
for (x = b->used - 1; x >= 0; x--) {
rr = *tmpa & 1;
*tmpb-- = (*tmpa-- >> 1) | (r << (DIGIT_BIT - 1));
r = rr;
}{...}
tmpb = b->dp + b->used;
for (x = b->used; x < oldused; x++) {
*tmpb++ = 0;
}{...}
}{...}
b->sign = a->sign;
mp_clamp (b);
return MP_OKAY;
}{...}
/* ... */#endif
static int
mp_mul_2d (mp_int * a, int b, mp_int * c)
{
mp_digit d;
int res;
if (a != c) {
if ((res = mp_copy (a, c)) != MP_OKAY) {
return res;
}{...}
}{...}
if (c->alloc < (int)(c->used + b/DIGIT_BIT + 1)) {
if ((res = mp_grow (c, c->used + b / DIGIT_BIT + 1)) != MP_OKAY) {
return res;
}{...}
}{...}
if (b >= (int)DIGIT_BIT) {
if ((res = mp_lshd (c, b / DIGIT_BIT)) != MP_OKAY) {
return res;
}{...}
}{...}
d = (mp_digit) (b % DIGIT_BIT);
if (d != 0) {
register mp_digit *tmpc, shift, mask, r, rr;
register int x;
mask = (((mp_digit)1) << d) - 1;
shift = DIGIT_BIT - d;
tmpc = c->dp;
r = 0;
for (x = 0; x < c->used; x++) {
rr = (*tmpc >> shift) & mask;
*tmpc = ((*tmpc << d) | r) & MP_MASK;
++tmpc;
r = rr;
}{...}
if (r != 0) {
c->dp[(c->used)++] = r;
}{...}
}{...}
mp_clamp (c);
return MP_OKAY;
}{ ... }
#ifdef BN_MP_INIT_MULTI_C
static int
mp_init_multi(mp_int *mp, ...)
{
mp_err res = MP_OKAY;
int n = 0;
mp_int* cur_arg = mp;
va_list args;
va_start(args, mp);
while (cur_arg != NULL) {
if (mp_init(cur_arg) != MP_OKAY) {
/* ... */
va_list clean_args;
va_end(args);
cur_arg = mp;
va_start(clean_args, mp);
while (n--) {
mp_clear(cur_arg);
cur_arg = va_arg(clean_args, mp_int*);
}{...}
va_end(clean_args);
res = MP_MEM;
break;
}{...}
n++;
cur_arg = va_arg(args, mp_int*);
}{...}
va_end(args);
return res;
}{ ... }
/* ... */#endif
#ifdef BN_MP_CLEAR_MULTI_C
static void
mp_clear_multi(mp_int *mp, ...)
{
mp_int* next_mp = mp;
va_list args;
va_start(args, mp);
while (next_mp != NULL) {
mp_clear(next_mp);
next_mp = va_arg(args, mp_int*);
}{...}
va_end(args);
}{ ... }
/* ... */#endif
static int
mp_lshd (mp_int * a, int b)
{
int x, res;
if (b <= 0) {
return MP_OKAY;
}{...}
if (a->alloc < a->used + b) {
if ((res = mp_grow (a, a->used + b)) != MP_OKAY) {
return res;
}{...}
}{...}
{
register mp_digit *top, *bottom;
a->used += b;
top = a->dp + a->used - 1;
bottom = a->dp + a->used - 1 - b;
/* ... */
for (x = a->used - 1; x >= b; x--) {
*top-- = *bottom--;
}{...}
top = a->dp;
for (x = 0; x < b; x++) {
*top++ = 0;
}{...}
}{...}
return MP_OKAY;
}{ ... }
static int
mp_count_bits (mp_int * a)
{
int r;
mp_digit q;
if (a->used == 0) {
return 0;
}{...}
r = (a->used - 1) * DIGIT_BIT;
q = a->dp[a->used - 1];
while (q > ((mp_digit) 0)) {
++r;
q >>= ((mp_digit) 1);
}{...}
return r;
}{ ... }
static int
mp_mod_2d (mp_int * a, int b, mp_int * c)
{
int x, res;
if (b <= 0) {
mp_zero (c);
return MP_OKAY;
}{...}
if (b >= (int) (a->used * DIGIT_BIT)) {
res = mp_copy (a, c);
return res;
}{...}
if ((res = mp_copy (a, c)) != MP_OKAY) {
return res;
}{...}
for (x = (b / DIGIT_BIT) + ((b % DIGIT_BIT) == 0 ? 0 : 1); x < c->used; x++) {
c->dp[x] = 0;
}{...}
c->dp[b / DIGIT_BIT] &=
(mp_digit) ((((mp_digit) 1) << (((mp_digit) b) % DIGIT_BIT)) - ((mp_digit) 1));
mp_clamp (c);
return MP_OKAY;
}{ ... }
#ifdef BN_MP_DIV_SMALL
static int
mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d)
{
mp_int ta, tb, tq, q;
int res, n, n2;
if (mp_iszero (b) == 1) {
return MP_VAL;
}{...}
if (mp_cmp_mag (a, b) == MP_LT) {
if (d != NULL) {
res = mp_copy (a, d);
}{...} else {
res = MP_OKAY;
}{...}
if (c != NULL) {
mp_zero (c);
}{...}
return res;
}{...}
if ((res = mp_init_multi(&ta, &tb, &tq, &q, NULL)) != MP_OKAY) {
return res;
}{...}
mp_set(&tq, 1);
n = mp_count_bits(a) - mp_count_bits(b);
if (((res = mp_abs(a, &ta)) != MP_OKAY) ||
((res = mp_abs(b, &tb)) != MP_OKAY) ||
((res = mp_mul_2d(&tb, n, &tb)) != MP_OKAY) ||
((res = mp_mul_2d(&tq, n, &tq)) != MP_OKAY)) {
goto LBL_ERR;
}{...}
while (n-- >= 0) {
if (mp_cmp(&tb, &ta) != MP_GT) {
if (((res = mp_sub(&ta, &tb, &ta)) != MP_OKAY) ||
((res = mp_add(&q, &tq, &q)) != MP_OKAY)) {
goto LBL_ERR;
}{...}
}{...}
if (((res = mp_div_2d(&tb, 1, &tb, NULL)) != MP_OKAY) ||
((res = mp_div_2d(&tq, 1, &tq, NULL)) != MP_OKAY)) {
goto LBL_ERR;
}{...}
}{...}
n = a->sign;
n2 = (a->sign == b->sign ? MP_ZPOS : MP_NEG);
if (c != NULL) {
mp_exch(c, &q);
c->sign = (mp_iszero(c) == MP_YES) ? MP_ZPOS : n2;
}{...}
if (d != NULL) {
mp_exch(d, &ta);
d->sign = (mp_iszero(d) == MP_YES) ? MP_ZPOS : n;
}{...}
LBL_ERR:
mp_clear_multi(&ta, &tb, &tq, &q, NULL);
return res;
}{ ... }
/* ... */#else
/* ... */
static int
mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
{
mp_int q, x, y, t1, t2;
int res, n, t, i, norm, neg;
if (mp_iszero (b) == 1) {
return MP_VAL;
}{...}
if (mp_cmp_mag (a, b) == MP_LT) {
if (d != NULL) {
res = mp_copy (a, d);
}{...} else {
res = MP_OKAY;
}{...}
if (c != NULL) {
mp_zero (c);
}{...}
return res;
}{...}
if ((res = mp_init_size (&q, a->used + 2)) != MP_OKAY) {
return res;
}{...}
q.used = a->used + 2;
if ((res = mp_init (&t1)) != MP_OKAY) {
goto LBL_Q;
}{...}
if ((res = mp_init (&t2)) != MP_OKAY) {
goto LBL_T1;
}{...}
if ((res = mp_init_copy (&x, a)) != MP_OKAY) {
goto LBL_T2;
}{...}
if ((res = mp_init_copy (&y, b)) != MP_OKAY) {
goto LBL_X;
}{...}
neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
x.sign = y.sign = MP_ZPOS;
norm = mp_count_bits(&y) % DIGIT_BIT;
if (norm < (int)(DIGIT_BIT-1)) {
norm = (DIGIT_BIT-1) - norm;
if ((res = mp_mul_2d (&x, norm, &x)) != MP_OKAY) {
goto LBL_Y;
}{...}
if ((res = mp_mul_2d (&y, norm, &y)) != MP_OKAY) {
goto LBL_Y;
}{...}
}{...} else {
norm = 0;
}{...}
n = x.used - 1;
t = y.used - 1;
if ((res = mp_lshd (&y, n - t)) != MP_OKAY) {
goto LBL_Y;
}{...}
while (mp_cmp (&x, &y) != MP_LT) {
++(q.dp[n - t]);
if ((res = mp_sub (&x, &y, &x)) != MP_OKAY) {
goto LBL_Y;
}{...}
}{...}
mp_rshd (&y, n - t);
for (i = n; i >= (t + 1); i--) {
if (i > x.used) {
continue;
}{...}
/* ... */
if (x.dp[i] == y.dp[t]) {
q.dp[i - t - 1] = ((((mp_digit)1) << DIGIT_BIT) - 1);
}{...} else {
mp_word tmp;
tmp = ((mp_word) x.dp[i]) << ((mp_word) DIGIT_BIT);
tmp |= ((mp_word) x.dp[i - 1]);
tmp /= ((mp_word) y.dp[t]);
if (tmp > (mp_word) MP_MASK)
tmp = MP_MASK;
q.dp[i - t - 1] = (mp_digit) (tmp & (mp_word) (MP_MASK));
}{...}
/* ... */
q.dp[i - t - 1] = (q.dp[i - t - 1] + 1) & MP_MASK;
do {
q.dp[i - t - 1] = (q.dp[i - t - 1] - 1) & MP_MASK;
mp_zero (&t1);
t1.dp[0] = (t - 1 < 0) ? 0 : y.dp[t - 1];
t1.dp[1] = y.dp[t];
t1.used = 2;
if ((res = mp_mul_d (&t1, q.dp[i - t - 1], &t1)) != MP_OKAY) {
goto LBL_Y;
}{...}
t2.dp[0] = (i - 2 < 0) ? 0 : x.dp[i - 2];
t2.dp[1] = (i - 1 < 0) ? 0 : x.dp[i - 1];
t2.dp[2] = x.dp[i];
t2.used = 3;
}{...} while (mp_cmp_mag(&t1, &t2) == MP_GT);
if ((res = mp_mul_d (&y, q.dp[i - t - 1], &t1)) != MP_OKAY) {
goto LBL_Y;
}{...}
if ((res = mp_lshd (&t1, i - t - 1)) != MP_OKAY) {
goto LBL_Y;
}{...}
if ((res = mp_sub (&x, &t1, &x)) != MP_OKAY) {
goto LBL_Y;
}{...}
if (x.sign == MP_NEG) {
if ((res = mp_copy (&y, &t1)) != MP_OKAY) {
goto LBL_Y;
}{...}
if ((res = mp_lshd (&t1, i - t - 1)) != MP_OKAY) {
goto LBL_Y;
}{...}
if ((res = mp_add (&x, &t1, &x)) != MP_OKAY) {
goto LBL_Y;
}{...}
q.dp[i - t - 1] = (q.dp[i - t - 1] - 1UL) & MP_MASK;
}{...}
}{...}
/* ... */
x.sign = x.used == 0 ? MP_ZPOS : a->sign;
if (c != NULL) {
mp_clamp (&q);
mp_exch (&q, c);
c->sign = neg;
}{...}
if (d != NULL) {
mp_div_2d (&x, norm, &x, NULL);
mp_exch (&x, d);
}{...}
res = MP_OKAY;
LBL_Y:mp_clear (&y);
LBL_X:mp_clear (&x);
LBL_T2:mp_clear (&t2);
LBL_T1:mp_clear (&t1);
LBL_Q:mp_clear (&q);
return res;
}{...}
/* ... */
#endif
#ifdef MP_LOW_MEM
#define TAB_SIZE 32
#else
#define TAB_SIZE 256
#endif
static int
s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
{
mp_int M[TAB_SIZE], res, mu;
mp_digit buf;
int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize;
int (*redux)(mp_int*,mp_int*,mp_int*);
x = mp_count_bits (X);
if (x <= 7) {
winsize = 2;
}{...} else if (x <= 36) {
winsize = 3;
}{...} else if (x <= 140) {
winsize = 4;
}{...} else if (x <= 450) {
winsize = 5;
}{...} else if (x <= 1303) {
winsize = 6;
}{...} else if (x <= 3529) {
winsize = 7;
}{...} else {
winsize = 8;
}{...}
#ifdef MP_LOW_MEM
if (winsize > 5) {
winsize = 5;
}{...}
#endif/* ... */
if ((err = mp_init(&M[1])) != MP_OKAY) {
return err;
}{...}
for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
if ((err = mp_init(&M[x])) != MP_OKAY) {
for (y = 1<<(winsize-1); y < x; y++) {
mp_clear (&M[y]);
}{...}
mp_clear(&M[1]);
return err;
}{...}
}{...}
if ((err = mp_init (&mu)) != MP_OKAY) {
goto LBL_M;
}{...}
if (redmode == 0) {
if ((err = mp_reduce_setup (&mu, P)) != MP_OKAY) {
goto LBL_MU;
}{...}
redux = mp_reduce;
}{...} else {
if ((err = mp_reduce_2k_setup_l (P, &mu)) != MP_OKAY) {
goto LBL_MU;
}{...}
redux = mp_reduce_2k_l;
}{...}
/* ... */
if ((err = mp_mod (G, P, &M[1])) != MP_OKAY) {
goto LBL_MU;
}{...}
/* ... */
if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) {
goto LBL_MU;
}{...}
for (x = 0; x < (winsize - 1); x++) {
if ((err = mp_sqr (&M[1 << (winsize - 1)],
&M[1 << (winsize - 1)])) != MP_OKAY) {
goto LBL_MU;
}{...}
if ((err = redux (&M[1 << (winsize - 1)], P, &mu)) != MP_OKAY) {
goto LBL_MU;
}{...}
}{...}
/* ... */
for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) {
if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) {
goto LBL_MU;
}{...}
if ((err = redux (&M[x], P, &mu)) != MP_OKAY) {
goto LBL_MU;
}{...}
}{...}
if ((err = mp_init (&res)) != MP_OKAY) {
goto LBL_MU;
}{...}
mp_set (&res, 1);
mode = 0;
bitcnt = 1;
buf = 0;
digidx = X->used - 1;
bitcpy = 0;
bitbuf = 0;
for (;;) {
if (--bitcnt == 0) {
if (digidx == -1) {
break;
}{...}
buf = X->dp[digidx--];
bitcnt = (int) DIGIT_BIT;
}{...}
y = (buf >> (mp_digit)(DIGIT_BIT - 1)) & 1;
buf <<= (mp_digit)1;
/* ... */
if (mode == 0 && y == 0) {
continue;
}{...}
if (mode == 1 && y == 0) {
if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
goto LBL_RES;
}{...}
if ((err = redux (&res, P, &mu)) != MP_OKAY) {
goto LBL_RES;
}{...}
continue;
}{...}
bitbuf |= (y << (winsize - ++bitcpy));
mode = 2;
if (bitcpy == winsize) {
for (x = 0; x < winsize; x++) {
if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
goto LBL_RES;
}{...}
if ((err = redux (&res, P, &mu)) != MP_OKAY) {
goto LBL_RES;
}{...}
}{...}
if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) {
goto LBL_RES;
}{...}
if ((err = redux (&res, P, &mu)) != MP_OKAY) {
goto LBL_RES;
}{...}
bitcpy = 0;
bitbuf = 0;
mode = 1;
}{...}
}{...}
if (mode == 2 && bitcpy > 0) {
for (x = 0; x < bitcpy; x++) {
if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
goto LBL_RES;
}{...}
if ((err = redux (&res, P, &mu)) != MP_OKAY) {
goto LBL_RES;
}{...}
bitbuf <<= 1;
if ((bitbuf & (1 << winsize)) != 0) {
if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) {
goto LBL_RES;
}{...}
if ((err = redux (&res, P, &mu)) != MP_OKAY) {
goto LBL_RES;
}{...}
}{...}
}{...}
}{...}
mp_exch (&res, Y);
err = MP_OKAY;
LBL_RES:mp_clear (&res);
LBL_MU:mp_clear (&mu);
LBL_M:
mp_clear(&M[1]);
for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
mp_clear (&M[x]);
}{...}
return err;
}{ ... }
static int
mp_sqr (mp_int * a, mp_int * b)
{
int res;
#ifdef BN_MP_TOOM_SQR_C
if (a->used >= TOOM_SQR_CUTOFF) {
res = mp_toom_sqr(a, b);
}{...} else/* ... */
#endif
#ifdef BN_MP_KARATSUBA_SQR_C
if (a->used >= KARATSUBA_SQR_CUTOFF) {
res = mp_karatsuba_sqr (a, b);
}{...} else
#endif
{
#ifdef BN_FAST_S_MP_SQR_C
if ((a->used * 2 + 1) < MP_WARRAY &&
a->used <
(1 << (sizeof(mp_word) * CHAR_BIT - 2*DIGIT_BIT - 1))) {
res = fast_s_mp_sqr (a, b);
}{...} else/* ... */
#endif
#ifdef BN_S_MP_SQR_C
res = s_mp_sqr (a, b);
#else
#error mp_sqr could fail
res = MP_VAL;/* ... */
#endif
}{...}
b->sign = MP_ZPOS;
return res;
}{ ... }
/* ... */
static int
mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d)
{
mp_int q;
int p, res;
if ((res = mp_init(&q)) != MP_OKAY) {
return res;
}{...}
p = mp_count_bits(n);
top:
if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) {
goto ERR;
}{...}
if ((res = mp_mul(&q, d, &q)) != MP_OKAY) {
goto ERR;
}{...}
if ((res = s_mp_add(a, &q, a)) != MP_OKAY) {
goto ERR;
}{...}
if (mp_cmp_mag(a, n) != MP_LT) {
s_mp_sub(a, n, a);
goto top;
}{...}
ERR:
mp_clear(&q);
return res;
}{ ... }
static int
mp_reduce_2k_setup_l(mp_int *a, mp_int *d)
{
int res;
mp_int tmp;
if ((res = mp_init(&tmp)) != MP_OKAY) {
return res;
}{...}
if ((res = mp_2expt(&tmp, mp_count_bits(a))) != MP_OKAY) {
goto ERR;
}{...}
if ((res = s_mp_sub(&tmp, a, d)) != MP_OKAY) {
goto ERR;
}{...}
ERR:
mp_clear(&tmp);
return res;
}{ ... }
/* ... */
static int
mp_2expt (mp_int * a, int b)
{
int res;
mp_zero (a);
if ((res = mp_grow (a, b / DIGIT_BIT + 1)) != MP_OKAY) {
return res;
}{...}
a->used = b / DIGIT_BIT + 1;
a->dp[b / DIGIT_BIT] = ((mp_digit)1) << (b % DIGIT_BIT);
return MP_OKAY;
}{ ... }
/* ... */
static int
mp_reduce_setup (mp_int * a, mp_int * b)
{
int res;
if ((res = mp_2expt (a, b->used * 2 * DIGIT_BIT)) != MP_OKAY) {
return res;
}{...}
return mp_div (a, b, a, NULL);
}{ ... }
/* ... */
static int
mp_reduce (mp_int * x, mp_int * m, mp_int * mu)
{
mp_int q;
int res, um = m->used;
if ((res = mp_init_copy (&q, x)) != MP_OKAY) {
return res;
}{...}
mp_rshd (&q, um - 1);
if (((unsigned long) um) > (((mp_digit)1) << (DIGIT_BIT - 1))) {
if ((res = mp_mul (&q, mu, &q)) != MP_OKAY) {
goto CLEANUP;
}{...}
}{...} else {
#ifdef BN_S_MP_MUL_HIGH_DIGS_C
if ((res = s_mp_mul_high_digs (&q, mu, &q, um)) != MP_OKAY) {
goto CLEANUP;
}{...}
#elif/* ... */ defined(BN_FAST_S_MP_MUL_HIGH_DIGS_C)
if ((res = fast_s_mp_mul_high_digs (&q, mu, &q, um)) != MP_OKAY) {
goto CLEANUP;
}{...}
#else/* ... */
{
#error mp_reduce would always fail
res = MP_VAL;
goto CLEANUP;
}/* ... */
{...}#endif
}{...}
mp_rshd (&q, um + 1);
if ((res = mp_mod_2d (x, DIGIT_BIT * (um + 1), x)) != MP_OKAY) {
goto CLEANUP;
}{...}
if ((res = s_mp_mul_digs (&q, m, &q, um + 1)) != MP_OKAY) {
goto CLEANUP;
}{...}
if ((res = mp_sub (x, &q, x)) != MP_OKAY) {
goto CLEANUP;
}{...}
if (mp_cmp_d (x, 0) == MP_LT) {
mp_set (&q, 1);
if ((res = mp_lshd (&q, um + 1)) != MP_OKAY) {
goto CLEANUP;
}{...}
if ((res = mp_add (x, &q, x)) != MP_OKAY) {
goto CLEANUP;
}{...}
}{...}
while (mp_cmp (x, m) != MP_LT) {
if ((res = s_mp_sub (x, m, x)) != MP_OKAY) {
goto CLEANUP;
}{...}
}{...}
CLEANUP:
mp_clear (&q);
return res;
}{ ... }
/* ... */
static int
s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
{
mp_int t;
int res, pa, pb, ix, iy;
mp_digit u;
mp_word r;
mp_digit tmpx, *tmpt, *tmpy;
if (((digs) < MP_WARRAY) &&
MIN (a->used, b->used) <
(1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
return fast_s_mp_mul_digs (a, b, c, digs);
}{...}
if ((res = mp_init_size (&t, digs)) != MP_OKAY) {
return res;
}{...}
t.used = digs;
pa = a->used;
for (ix = 0; ix < pa; ix++) {
u = 0;
pb = MIN (b->used, digs - ix);
tmpx = a->dp[ix];
tmpt = t.dp + ix;
tmpy = b->dp;
for (iy = 0; iy < pb; iy++) {
r = ((mp_word)*tmpt) +
((mp_word)tmpx) * ((mp_word)*tmpy++) +
((mp_word) u);
*tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK));
u = (mp_digit) (r >> ((mp_word) DIGIT_BIT));
}{...}
if (ix + iy < digs) {
*tmpt = u;
}{...}
}{...}
mp_clamp (&t);
mp_exch (&t, c);
mp_clear (&t);
return MP_OKAY;
}{ ... }
/* ... */
static int
fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
{
int olduse, res, pa, ix, iz;
mp_digit W[MP_WARRAY];
register mp_word _W;
if (c->alloc < digs) {
if ((res = mp_grow (c, digs)) != MP_OKAY) {
return res;
}{...}
}{...}
pa = MIN(digs, a->used + b->used);
_W = 0;
for (ix = 0; ix < pa; ix++) {
int tx, ty;
int iy;
mp_digit *tmpx, *tmpy;
ty = MIN(b->used-1, ix);
tx = ix - ty;
tmpx = a->dp + tx;
tmpy = b->dp + ty;
/* ... */
iy = MIN(a->used-tx, ty+1);
for (iz = 0; iz < iy; ++iz) {
_W += ((mp_word)*tmpx++)*((mp_word)*tmpy--);
}{...}
W[ix] = ((mp_digit)_W) & MP_MASK;
_W = _W >> ((mp_word)DIGIT_BIT);
}{...}
olduse = c->used;
c->used = pa;
{
register mp_digit *tmpc;
tmpc = c->dp;
for (ix = 0; ix < pa+1; ix++) {
*tmpc++ = W[ix];
}{...}
for (; ix < olduse; ix++) {
*tmpc++ = 0;
}{...}
}{...}
mp_clamp (c);
return MP_OKAY;
}{ ... }
static int
mp_init_size (mp_int * a, int size)
{
int x;
size += (MP_PREC * 2) - (size % MP_PREC);
a->dp = OPT_CAST(mp_digit) XMALLOC (sizeof (mp_digit) * size);
if (a->dp == NULL) {
return MP_MEM;
}{...}
a->used = 0;
a->alloc = size;
a->sign = MP_ZPOS;
for (x = 0; x < size; x++) {
a->dp[x] = 0;
}{...}
return MP_OKAY;
}{ ... }
static int
s_mp_sqr (mp_int * a, mp_int * b)
{
mp_int t;
int res, ix, iy, pa;
mp_word r;
mp_digit u, tmpx, *tmpt;
pa = a->used;
if ((res = mp_init_size (&t, 2*pa + 1)) != MP_OKAY) {
return res;
}{...}
t.used = 2*pa + 1;
for (ix = 0; ix < pa; ix++) {
r = ((mp_word) t.dp[2*ix]) +
((mp_word)a->dp[ix])*((mp_word)a->dp[ix]);
t.dp[ix+ix] = (mp_digit) (r & ((mp_word) MP_MASK));
u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
tmpx = a->dp[ix];
tmpt = t.dp + (2*ix + 1);
for (iy = ix + 1; iy < pa; iy++) {
r = ((mp_word)tmpx) * ((mp_word)a->dp[iy]);
/* ... */
r = ((mp_word) *tmpt) + r + r + ((mp_word) u);
*tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK));
u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
}{...}
while (u != ((mp_digit) 0)) {
r = ((mp_word) *tmpt) + ((mp_word) u);
*tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK));
u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
}{...}
}{...}
mp_clamp (&t);
mp_exch (&t, b);
mp_clear (&t);
return MP_OKAY;
}{ ... }
/* ... */
static int
s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
{
mp_int t;
int res, pa, pb, ix, iy;
mp_digit u;
mp_word r;
mp_digit tmpx, *tmpt, *tmpy;
#ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C
if (((a->used + b->used + 1) < MP_WARRAY)
&& MIN (a->used, b->used) < (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
return fast_s_mp_mul_high_digs (a, b, c, digs);
}{...}
#endif/* ... */
if ((res = mp_init_size (&t, a->used + b->used + 1)) != MP_OKAY) {
return res;
}{...}
t.used = a->used + b->used + 1;
pa = a->used;
pb = b->used;
for (ix = 0; ix < pa; ix++) {
u = 0;
tmpx = a->dp[ix];
tmpt = &(t.dp[digs]);
tmpy = b->dp + (digs - ix);
for (iy = digs - ix; iy < pb; iy++) {
r = ((mp_word)*tmpt) +
((mp_word)tmpx) * ((mp_word)*tmpy++) +
((mp_word) u);
*tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK));
u = (mp_digit) (r >> ((mp_word) DIGIT_BIT));
}{...}
*tmpt = u;
}{...}
mp_clamp (&t);
mp_exch (&t, c);
mp_clear (&t);
return MP_OKAY;
}{ ... }
#ifdef BN_MP_MONTGOMERY_SETUP_C
static int
mp_montgomery_setup (mp_int * n, mp_digit * rho)
{
mp_digit x, b;
/* ... */
b = n->dp[0];
if ((b & 1) == 0) {
return MP_VAL;
}{...}
x = (((b + 2) & 4) << 1) + b;
x *= 2 - b * x;
#if !defined(MP_8BIT)
x *= 2 - b * x;
#endif
#if defined(MP_64BIT) || !(defined(MP_8BIT) || defined(MP_16BIT))
x *= 2 - b * x;
#endif
#ifdef MP_64BIT
x *= 2 - b * x;
#endif
*rho = (unsigned long)(((mp_word)1 << ((mp_word) DIGIT_BIT)) - x) & MP_MASK;
return MP_OKAY;
}{...}
/* ... */#endif
#ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C
/* ... */
int
fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
{
int ix, res, olduse;
mp_word W[MP_WARRAY];
olduse = x->used;
if (x->alloc < n->used + 1) {
if ((res = mp_grow (x, n->used + 1)) != MP_OKAY) {
return res;
}{...}
}{...}
/* ... */
{
register mp_word *_W;
register mp_digit *tmpx;
_W = W;
tmpx = x->dp;
for (ix = 0; ix < x->used; ix++) {
*_W++ = *tmpx++;
}{...}
for (; ix < n->used * 2 + 1; ix++) {
*_W++ = 0;
}{...}
}{...}
/* ... */
for (ix = 0; ix < n->used; ix++) {
/* ... */
register mp_digit mu;
mu = (mp_digit) (((W[ix] & MP_MASK) * rho) & MP_MASK);
/* ... */
{
register int iy;
register mp_digit *tmpn;
register mp_word *_W;
tmpn = n->dp;
_W = W + ix;
for (iy = 0; iy < n->used; iy++) {
*_W++ += ((mp_word)mu) * ((mp_word)*tmpn++);
}{...}
}{...}
W[ix + 1] += W[ix] >> ((mp_word) DIGIT_BIT);
}{...}
/* ... */
{
register mp_digit *tmpx;
register mp_word *_W, *_W1;
_W1 = W + ix;
_W = W + ++ix;
for (; ix <= n->used * 2 + 1; ix++) {
*_W++ += *_W1++ >> ((mp_word) DIGIT_BIT);
}{...}
/* ... */
tmpx = x->dp;
_W = W + n->used;
for (ix = 0; ix < n->used + 1; ix++) {
*tmpx++ = (mp_digit)(*_W++ & ((mp_word) MP_MASK));
}{...}
/* ... */
for (; ix < olduse; ix++) {
*tmpx++ = 0;
}{...}
}{...}
x->used = n->used + 1;
mp_clamp (x);
if (mp_cmp_mag (x, n) != MP_LT) {
return s_mp_sub (x, n, x);
}{...}
return MP_OKAY;
}{...}
/* ... */#endif
#ifdef BN_MP_MUL_2_C
static int
mp_mul_2(mp_int * a, mp_int * b)
{
int x, res, oldused;
if (b->alloc < a->used + 1) {
if ((res = mp_grow (b, a->used + 1)) != MP_OKAY) {
return res;
}{...}
}{...}
oldused = b->used;
b->used = a->used;
{
register mp_digit r, rr, *tmpa, *tmpb;
tmpa = a->dp;
tmpb = b->dp;
r = 0;
for (x = 0; x < a->used; x++) {
/* ... */
rr = *tmpa >> ((mp_digit)(DIGIT_BIT - 1));
*tmpb++ = ((*tmpa++ << ((mp_digit)1)) | r) & MP_MASK;
/* ... */
r = rr;
}{...}
if (r != 0) {
*tmpb = 1;
++(b->used);
}{...}
/* ... */
tmpb = b->dp + b->used;
for (x = b->used; x < oldused; x++) {
*tmpb++ = 0;
}{...}
}{...}
b->sign = a->sign;
return MP_OKAY;
}{...}
/* ... */#endif
#ifdef BN_MP_MONTGOMERY_CALC_NORMALIZATION_C
/* ... */
static int
mp_montgomery_calc_normalization (mp_int * a, mp_int * b)
{
int x, bits, res;
bits = mp_count_bits (b) % DIGIT_BIT;
if (b->used > 1) {
if ((res = mp_2expt (a, (b->used - 1) * DIGIT_BIT + bits - 1)) != MP_OKAY) {
return res;
}{...}
}{...} else {
mp_set(a, 1);
bits = 1;
}{...}
for (x = bits - 1; x < (int)DIGIT_BIT; x++) {
if ((res = mp_mul_2 (a, a)) != MP_OKAY) {
return res;
}{...}
if (mp_cmp_mag (a, b) != MP_LT) {
if ((res = s_mp_sub (a, b, a)) != MP_OKAY) {
return res;
}{...}
}{...}
}{...}
return MP_OKAY;
}{...}
/* ... */#endif
#ifdef BN_MP_EXPTMOD_FAST_C
/* ... */
static int
mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
{
mp_int M[TAB_SIZE], res;
mp_digit buf, mp;
int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize;
/* ... */
int (*redux)(mp_int*,mp_int*,mp_digit);
x = mp_count_bits (X);
if (x <= 7) {
winsize = 2;
}{...} else if (x <= 36) {
winsize = 3;
}{...} else if (x <= 140) {
winsize = 4;
}{...} else if (x <= 450) {
winsize = 5;
}{...} else if (x <= 1303) {
winsize = 6;
}{...} else if (x <= 3529) {
winsize = 7;
}{...} else {
winsize = 8;
}{...}
#ifdef MP_LOW_MEM
if (winsize > 5) {
winsize = 5;
}{...}
/* ... */#endif
if ((err = mp_init(&M[1])) != MP_OKAY) {
return err;
}{...}
for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
if ((err = mp_init(&M[x])) != MP_OKAY) {
for (y = 1<<(winsize-1); y < x; y++) {
mp_clear (&M[y]);
}{...}
mp_clear(&M[1]);
return err;
}{...}
}{...}
if (redmode == 0) {
#ifdef BN_MP_MONTGOMERY_SETUP_C
if ((err = mp_montgomery_setup (P, &mp)) != MP_OKAY) {
goto LBL_M;
}{...}
/* ... */#else
err = MP_VAL;
goto LBL_M;/* ... */
#endif
#ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C
if (((P->used * 2 + 1) < MP_WARRAY) &&
P->used < (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
redux = fast_mp_montgomery_reduce;
}{...} else
#endif
{
#ifdef BN_MP_MONTGOMERY_REDUCE_C
redux = mp_montgomery_reduce;/* ... */
#else
err = MP_VAL;
goto LBL_M;/* ... */
#endif
}{...}
}{...} else if (redmode == 1) {
#if defined(BN_MP_DR_SETUP_C) && defined(BN_MP_DR_REDUCE_C)
mp_dr_setup(P, &mp);
redux = mp_dr_reduce;/* ... */
#else
err = MP_VAL;
goto LBL_M;/* ... */
#endif
}{...} else {
#if defined(BN_MP_REDUCE_2K_SETUP_C) && defined(BN_MP_REDUCE_2K_C)
if ((err = mp_reduce_2k_setup(P, &mp)) != MP_OKAY) {
goto LBL_M;
}{...}
redux = mp_reduce_2k;/* ... */
#else
err = MP_VAL;
goto LBL_M;/* ... */
#endif
}{...}
if ((err = mp_init (&res)) != MP_OKAY) {
goto LBL_M;
}{...}
/* ... */
if (redmode == 0) {
#ifdef BN_MP_MONTGOMERY_CALC_NORMALIZATION_C
if ((err = mp_montgomery_calc_normalization (&res, P)) != MP_OKAY) {
goto LBL_RES;
}{...}
/* ... */#else
err = MP_VAL;
goto LBL_RES;/* ... */
#endif
if ((err = mp_mulmod (G, &res, P, &M[1])) != MP_OKAY) {
goto LBL_RES;
}{...}
}{...} else {
mp_set(&res, 1);
if ((err = mp_mod(G, P, &M[1])) != MP_OKAY) {
goto LBL_RES;
}{...}
}{...}
if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) {
goto LBL_RES;
}{...}
for (x = 0; x < (winsize - 1); x++) {
if ((err = mp_sqr (&M[1 << (winsize - 1)], &M[1 << (winsize - 1)])) != MP_OKAY) {
goto LBL_RES;
}{...}
if ((err = redux (&M[1 << (winsize - 1)], P, mp)) != MP_OKAY) {
goto LBL_RES;
}{...}
}{...}
for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) {
if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) {
goto LBL_RES;
}{...}
if ((err = redux (&M[x], P, mp)) != MP_OKAY) {
goto LBL_RES;
}{...}
}{...}
mode = 0;
bitcnt = 1;
buf = 0;
digidx = X->used - 1;
bitcpy = 0;
bitbuf = 0;
for (;;) {
if (--bitcnt == 0) {
if (digidx == -1) {
break;
}{...}
buf = X->dp[digidx--];
bitcnt = (int)DIGIT_BIT;
}{...}
y = (mp_digit)(buf >> (DIGIT_BIT - 1)) & 1;
buf <<= (mp_digit)1;
/* ... */
if (mode == 0 && y == 0) {
continue;
}{...}
if (mode == 1 && y == 0) {
if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
goto LBL_RES;
}{...}
if ((err = redux (&res, P, mp)) != MP_OKAY) {
goto LBL_RES;
}{...}
continue;
}{...}
bitbuf |= (y << (winsize - ++bitcpy));
mode = 2;
if (bitcpy == winsize) {
for (x = 0; x < winsize; x++) {
if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
goto LBL_RES;
}{...}
if ((err = redux (&res, P, mp)) != MP_OKAY) {
goto LBL_RES;
}{...}
}{...}
if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) {
goto LBL_RES;
}{...}
if ((err = redux (&res, P, mp)) != MP_OKAY) {
goto LBL_RES;
}{...}
bitcpy = 0;
bitbuf = 0;
mode = 1;
}{...}
}{...}
if (mode == 2 && bitcpy > 0) {
for (x = 0; x < bitcpy; x++) {
if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
goto LBL_RES;
}{...}
if ((err = redux (&res, P, mp)) != MP_OKAY) {
goto LBL_RES;
}{...}
bitbuf <<= 1;
if ((bitbuf & (1 << winsize)) != 0) {
if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) {
goto LBL_RES;
}{...}
if ((err = redux (&res, P, mp)) != MP_OKAY) {
goto LBL_RES;
}{...}
}{...}
}{...}
}{...}
if (redmode == 0) {
/* ... */
if ((err = redux(&res, P, mp)) != MP_OKAY) {
goto LBL_RES;
}{...}
}{...}
mp_exch (&res, Y);
err = MP_OKAY;
LBL_RES:mp_clear (&res);
LBL_M:
mp_clear(&M[1]);
for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
mp_clear (&M[x]);
}{...}
return err;
}{...}
/* ... */#endif
#ifdef BN_FAST_S_MP_SQR_C
/* ... */
static int
fast_s_mp_sqr (mp_int * a, mp_int * b)
{
int olduse, res, pa, ix, iz;
mp_digit W[MP_WARRAY], *tmpx;
mp_word W1;
pa = a->used + a->used;
if (b->alloc < pa) {
if ((res = mp_grow (b, pa)) != MP_OKAY) {
return res;
}{...}
}{...}
W1 = 0;
for (ix = 0; ix < pa; ix++) {
int tx, ty, iy;
mp_word _W;
mp_digit *tmpy;
_W = 0;
ty = MIN(a->used-1, ix);
tx = ix - ty;
tmpx = a->dp + tx;
tmpy = a->dp + ty;
/* ... */
iy = MIN(a->used-tx, ty+1);
/* ... */
iy = MIN(iy, (ty-tx+1)>>1);
for (iz = 0; iz < iy; iz++) {
_W += ((mp_word)*tmpx++)*((mp_word)*tmpy--);
}{...}
_W = _W + _W + W1;
if ((ix&1) == 0) {
_W += ((mp_word)a->dp[ix>>1])*((mp_word)a->dp[ix>>1]);
}{...}
W[ix] = (mp_digit)(_W & MP_MASK);
W1 = _W >> ((mp_word)DIGIT_BIT);
}{...}
olduse = b->used;
b->used = a->used+a->used;
{
mp_digit *tmpb;
tmpb = b->dp;
for (ix = 0; ix < pa; ix++) {
*tmpb++ = W[ix] & MP_MASK;
}{...}
for (; ix < olduse; ix++) {
*tmpb++ = 0;
}{...}
}{...}
mp_clamp (b);
return MP_OKAY;
}{...}
/* ... */#endif
#ifdef BN_MP_MUL_D_C
static int
mp_mul_d (mp_int * a, mp_digit b, mp_int * c)
{
mp_digit u, *tmpa, *tmpc;
mp_word r;
int ix, res, olduse;
if (c->alloc < a->used + 1) {
if ((res = mp_grow (c, a->used + 1)) != MP_OKAY) {
return res;
}{...}
}{...}
olduse = c->used;
c->sign = a->sign;
tmpa = a->dp;
tmpc = c->dp;
u = 0;
for (ix = 0; ix < a->used; ix++) {
r = ((mp_word) u) + ((mp_word)*tmpa++) * ((mp_word)b);
*tmpc++ = (mp_digit) (r & ((mp_word) MP_MASK));
u = (mp_digit) (r >> ((mp_word) DIGIT_BIT));
}{...}
*tmpc++ = u;
++ix;
while (ix++ < olduse) {
*tmpc++ = 0;
}{...}
c->used = a->used + 1;
mp_clamp(c);
return MP_OKAY;
}{...}
/* ... */#endif