/usr/share/doc/libntl-dev/NTL/ZZ.cpp.html is in libntl-dev 9.9.1-3.
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 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>/Volumes/unix-files/u/ntl-new/ntl-9.9.0dev/doc/ZZ.cpp.html</title>
<meta name="Generator" content="Vim/7.1">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body bgcolor="#ffffff" text="#000000"><font face="monospace">
<br>
<font color="#0000ed"><i>/*</i></font><font color="#0000ed"><i>*************************************************************************\</i></font><br>
<br>
<font color="#0000ed"><i>MODULE: ZZ</i></font><br>
<br>
<font color="#0000ed"><i>SUMMARY:</i></font><br>
<br>
<font color="#0000ed"><i>The class ZZ is used to represent signed, arbitrary length integers.</i></font><br>
<br>
<font color="#0000ed"><i>Routines are provided for all of the basic arithmetic operations, as</i></font><br>
<font color="#0000ed"><i>well as for some more advanced operations such as primality testing.</i></font><br>
<font color="#0000ed"><i>Space is automatically managed by the constructors and destructors.</i></font><br>
<br>
<font color="#0000ed"><i>This module also provides routines for generating small primes, and</i></font><br>
<font color="#0000ed"><i>fast routines for performing modular arithmetic on single-precision</i></font><br>
<font color="#0000ed"><i>numbers.</i></font><br>
<br>
<br>
<font color="#0000ed"><i>\*************************************************************************</i></font><font color="#0000ed"><i>*/</i></font><br>
<br>
<font color="#1773cc">#include </font><font color="#4a6f8b"><NTL/tools.h></font><br>
<br>
<br>
<font color="#008b00"><b>class</b></font> ZZ {<br>
<font color="#b02f60"><b>public</b></font>:<br>
<br>
<br>
ZZ(); <font color="#0000ed"><i>// initial value is 0</i></font><br>
<br>
ZZ(<font color="#008b00"><b>const</b></font> ZZ& a); <font color="#0000ed"><i>// copy constructor</i></font><br>
<font color="#008b00"><b>explicit</b></font> ZZ(<font color="#008b00"><b>long</b></font> a); <font color="#0000ed"><i>// promotion constructor</i></font><br>
<br>
~ZZ(); <font color="#0000ed"><i>// destructor</i></font><br>
<br>
ZZ& <font color="#b02f60"><b>operator</b></font>=(<font color="#008b00"><b>const</b></font> ZZ& a); <font color="#0000ed"><i>// assignment operator</i></font><br>
ZZ& <font color="#b02f60"><b>operator</b></font>=(<font color="#008b00"><b>long</b></font> a); <br>
<br>
<font color="#0000ed"><i>// typedefs to aid in generic programming</i></font><br>
<font color="#008b00"><b>typedef</b></font> ZZ_p residue_type;<br>
<font color="#008b00"><b>typedef</b></font> ZZX poly_type;<br>
<br>
<br>
<font color="#0000ed"><i>// ...</i></font><br>
<br>
};<br>
<br>
<br>
<font color="#0000ed"><i>// NOTE: A ZZ is represented as a sequence of "zzigits",</i></font><br>
<font color="#0000ed"><i>// where each zzigit is between 0 and 2^{NTL_ZZ_NBITS-1}.</i></font><br>
<br>
<font color="#0000ed"><i>// NTL_ZZ_NBITS is macros defined in <NTL/ZZ.h>.</i></font><br>
<br>
<font color="#0000ed"><i>// SIZE INVARIANT: the number of bits in a ZZ is always less than</i></font><br>
<font color="#0000ed"><i>// 2^(NTL_BITS_PER_LONG-4).</i></font><br>
<br>
<br>
<br>
<font color="#0000ed"><i>/*</i></font><font color="#0000ed"><i>*************************************************************************\</i></font><br>
<br>
<font color="#0000ed"><i> Comparison</i></font><br>
<br>
<font color="#0000ed"><i>\*************************************************************************</i></font><font color="#0000ed"><i>*/</i></font><br>
<br>
<br>
<br>
<font color="#0000ed"><i>// The usual comparison operators: </i></font><br>
<br>
<font color="#008b00"><b>long</b></font> <font color="#b02f60"><b>operator</b></font>==(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b);<br>
<font color="#008b00"><b>long</b></font> <font color="#b02f60"><b>operator</b></font>!=(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b);<br>
<font color="#008b00"><b>long</b></font> <font color="#b02f60"><b>operator</b></font><(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b);<br>
<font color="#008b00"><b>long</b></font> <font color="#b02f60"><b>operator</b></font>>(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b);<br>
<font color="#008b00"><b>long</b></font> <font color="#b02f60"><b>operator</b></font><=(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b);<br>
<font color="#008b00"><b>long</b></font> <font color="#b02f60"><b>operator</b></font>>=(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b);<br>
<br>
<font color="#0000ed"><i>// other stuff:</i></font><br>
<br>
<font color="#008b00"><b>long</b></font> sign(<font color="#008b00"><b>const</b></font> ZZ& a); <font color="#0000ed"><i>// returns sign of a (-1, 0, +1)</i></font><br>
<font color="#008b00"><b>long</b></font> IsZero(<font color="#008b00"><b>const</b></font> ZZ& a); <font color="#0000ed"><i>// test for 0</i></font><br>
<font color="#008b00"><b>long</b></font> IsOne(<font color="#008b00"><b>const</b></font> ZZ& a); <font color="#0000ed"><i>// test for 1</i></font><br>
<br>
<font color="#008b00"><b>long</b></font> compare(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b); <font color="#0000ed"><i>// returns sign of a-b (-1, 0, or 1).</i></font><br>
<br>
<font color="#0000ed"><i>// PROMOTIONS: the comparison operators and the function compare</i></font><br>
<font color="#0000ed"><i>// support promotion from long to ZZ on (a, b).</i></font><br>
<br>
<br>
<font color="#0000ed"><i>/*</i></font><font color="#0000ed"><i>*************************************************************************\</i></font><br>
<br>
<font color="#0000ed"><i> Addition</i></font><br>
<br>
<font color="#0000ed"><i>\*************************************************************************</i></font><font color="#0000ed"><i>*/</i></font><br>
<br>
<br>
<font color="#0000ed"><i>// operator notation:</i></font><br>
<br>
ZZ <font color="#b02f60"><b>operator</b></font>+(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b);<br>
ZZ <font color="#b02f60"><b>operator</b></font>-(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b);<br>
ZZ <font color="#b02f60"><b>operator</b></font>-(<font color="#008b00"><b>const</b></font> ZZ& a); <font color="#0000ed"><i>// unary -</i></font><br>
<br>
ZZ& <font color="#b02f60"><b>operator</b></font>+=(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& a); <br>
ZZ& <font color="#b02f60"><b>operator</b></font>+=(ZZ& x, <font color="#008b00"><b>long</b></font> a); <br>
<br>
ZZ& <font color="#b02f60"><b>operator</b></font>-=(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& a); <br>
ZZ& <font color="#b02f60"><b>operator</b></font>-=(ZZ& x, <font color="#008b00"><b>long</b></font> a); <br>
<br>
ZZ& <font color="#b02f60"><b>operator</b></font>++(ZZ& x); <font color="#0000ed"><i>// prefix</i></font><br>
<font color="#008b00"><b>void</b></font> <font color="#b02f60"><b>operator</b></font>++(ZZ& x, <font color="#008b00"><b>int</b></font>); <font color="#0000ed"><i>// postfix</i></font><br>
<br>
ZZ& <font color="#b02f60"><b>operator</b></font>--(ZZ& x); <font color="#0000ed"><i>// prefix</i></font><br>
<font color="#008b00"><b>void</b></font> <font color="#b02f60"><b>operator</b></font>--(ZZ& x, <font color="#008b00"><b>int</b></font>); <font color="#0000ed"><i>// postfix</i></font><br>
<br>
<br>
<br>
<font color="#0000ed"><i>// procedural versions:</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> add(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b); <font color="#0000ed"><i>// x = a + b</i></font><br>
<font color="#008b00"><b>void</b></font> sub(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b); <font color="#0000ed"><i>// x = a - b</i></font><br>
<font color="#008b00"><b>void</b></font> SubPos(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b); <font color="#0000ed"><i>// x = a-b; assumes a >= b >= 0.</i></font><br>
<font color="#008b00"><b>void</b></font> negate(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& a); <font color="#0000ed"><i>// x = -a</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> abs(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& a); <font color="#0000ed"><i>// x = |a|</i></font><br>
ZZ abs(<font color="#008b00"><b>const</b></font> ZZ& a);<br>
<br>
<font color="#0000ed"><i>// PROMOTIONS: binary +, -, as well as the procedural versions add, sub</i></font><br>
<font color="#0000ed"><i>// support promotions from long to ZZ on (a, b).</i></font><br>
<br>
<br>
<font color="#0000ed"><i>/*</i></font><font color="#0000ed"><i>*************************************************************************\</i></font><br>
<br>
<font color="#0000ed"><i> Multiplication</i></font><br>
<br>
<font color="#0000ed"><i>\*************************************************************************</i></font><font color="#0000ed"><i>*/</i></font><br>
<br>
<font color="#0000ed"><i>// operator notation:</i></font><br>
<br>
ZZ <font color="#b02f60"><b>operator</b></font>*(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b);<br>
<br>
ZZ& <font color="#b02f60"><b>operator</b></font>*=(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& a);<br>
ZZ& <font color="#b02f60"><b>operator</b></font>*=(ZZ& x, <font color="#008b00"><b>long</b></font> a);<br>
<br>
<font color="#0000ed"><i>// procedural versions:</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> mul(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b); <font color="#0000ed"><i>// x = a * b</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> sqr(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& a); <font color="#0000ed"><i>// x = a*a</i></font><br>
ZZ sqr(<font color="#008b00"><b>const</b></font> ZZ& a); <br>
<br>
<font color="#0000ed"><i>// PROMOTIONS: operator * and procedure mul support promotion</i></font><br>
<font color="#0000ed"><i>// from long to ZZ on (a, b).</i></font><br>
<br>
<font color="#0000ed"><i>/*</i></font><font color="#0000ed"><i>*************************************************************************\</i></font><br>
<br>
<font color="#0000ed"><i> Combined Multiply and Add </i></font><br>
<br>
<font color="#0000ed"><i>\*************************************************************************</i></font><font color="#0000ed"><i>*/</i></font><br>
<br>
<br>
<font color="#008b00"><b>void</b></font> MulAddTo(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b); <font color="#0000ed"><i>// x += a*b</i></font><br>
<font color="#008b00"><b>void</b></font> MulAddTo(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>long</b></font> b); <font color="#0000ed"><i>// x += a*b</i></font><br>
<br>
<br>
<font color="#008b00"><b>void</b></font> MulSubFrom(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b); <font color="#0000ed"><i>// x -= a*b</i></font><br>
<font color="#008b00"><b>void</b></font> MulSubFrom(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>long</b></font> b); <font color="#0000ed"><i>// x -= a*b</i></font><br>
<br>
<font color="#0000ed"><i>// NOTE: these are provided for both convenience and efficiency.</i></font><br>
<font color="#0000ed"><i>// The single-precision versions may be significantly</i></font><br>
<font color="#0000ed"><i>// faster than the code sequence </i></font><br>
<font color="#0000ed"><i>// mul(tmp, a, b); add(x, x, tmp);</i></font><br>
<br>
<br>
<br>
<font color="#0000ed"><i>/*</i></font><font color="#0000ed"><i>*************************************************************************\</i></font><br>
<br>
<font color="#0000ed"><i> Division</i></font><br>
<br>
<font color="#0000ed"><i>\*************************************************************************</i></font><font color="#0000ed"><i>*/</i></font><br>
<br>
<br>
<font color="#0000ed"><i>// operator notation:</i></font><br>
<br>
ZZ <font color="#b02f60"><b>operator</b></font>/(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b);<br>
ZZ <font color="#b02f60"><b>operator</b></font>/(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>long</b></font> b);<br>
<br>
ZZ <font color="#b02f60"><b>operator</b></font>%(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b);<br>
<font color="#008b00"><b>long</b></font> <font color="#b02f60"><b>operator</b></font>%(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>long</b></font> b);<br>
<br>
ZZ& <font color="#b02f60"><b>operator</b></font>/=(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& b);<br>
ZZ& <font color="#b02f60"><b>operator</b></font>/=(ZZ& x, <font color="#008b00"><b>long</b></font> b);<br>
<br>
ZZ& <font color="#b02f60"><b>operator</b></font>%=(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& b);<br>
<br>
<br>
<font color="#0000ed"><i>// procedural versions:</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> DivRem(ZZ& q, ZZ& r, <font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b);<br>
<font color="#0000ed"><i>// q = floor(a/b), r = a - b*q.</i></font><br>
<font color="#0000ed"><i>// This implies that:</i></font><br>
<font color="#0000ed"><i>// |r| < |b|, and if r != 0, sign(r) = sign(b)</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> div(ZZ& q, <font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b);<br>
<font color="#0000ed"><i>// q = floor(a/b)</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> rem(ZZ& r, <font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b);<br>
<font color="#0000ed"><i>// q = floor(a/b), r = a - b*q</i></font><br>
<br>
<br>
<font color="#0000ed"><i>// single-precision variants:</i></font><br>
<br>
<font color="#008b00"><b>long</b></font> DivRem(ZZ& q, <font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>long</b></font> b);<br>
<font color="#0000ed"><i>// q = floor(a/b), r = a - b*q, return value is r.</i></font><br>
<br>
<font color="#008b00"><b>long</b></font> rem(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>long</b></font> b);<br>
<font color="#0000ed"><i>// q = floor(a/b), r = a - b*q, return value is r.</i></font><br>
<br>
<br>
<font color="#0000ed"><i>// divisibility testing:</i></font><br>
<br>
<font color="#008b00"><b>long</b></font> divide(ZZ& q, <font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b);<br>
<font color="#008b00"><b>long</b></font> divide(ZZ& q, <font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>long</b></font> b);<br>
<font color="#0000ed"><i>// if b | a, sets q = a/b and returns 1; otherwise returns 0.</i></font><br>
<br>
<font color="#008b00"><b>long</b></font> divide(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b);<br>
<font color="#008b00"><b>long</b></font> divide(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>long</b></font> b);<br>
<font color="#0000ed"><i>// if b | a, returns 1; otherwise returns 0.</i></font><br>
<br>
<br>
<font color="#0000ed"><i>/*</i></font><font color="#0000ed"><i>*************************************************************************\</i></font><br>
<br>
<font color="#0000ed"><i> GCD's</i></font><br>
<br>
<font color="#0000ed"><i>\*************************************************************************</i></font><font color="#0000ed"><i>*/</i></font><br>
<br>
<br>
<font color="#008b00"><b>void</b></font> GCD(ZZ& d, <font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b);<br>
ZZ GCD(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b); <br>
<br>
<font color="#0000ed"><i>// d = gcd(a, b) (which is always non-negative). Uses a binary GCD</i></font><br>
<font color="#0000ed"><i>// algorithm.</i></font><br>
<br>
<br>
<br>
<font color="#008b00"><b>void</b></font> XGCD(ZZ& d, ZZ& s, ZZ& t, <font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b);<br>
<br>
<font color="#0000ed"><i>// d = gcd(a, b) = a*s + b*t.</i></font><br>
<br>
<font color="#0000ed"><i>// The coefficients s and t are defined according to the standard</i></font><br>
<font color="#0000ed"><i>// Euclidean algorithm applied to |a| and |b|, with the signs then</i></font><br>
<font color="#0000ed"><i>// adjusted according to the signs of a and b.</i></font><br>
<br>
<font color="#0000ed"><i>// The implementation may or may not Euclid's algorithm,</i></font><br>
<font color="#0000ed"><i>// but the coefficients a and t are always computed as if </i></font><br>
<font color="#0000ed"><i>// it did.</i></font><br>
<br>
<br>
<font color="#0000ed"><i>// special-purpose single-precision variants:</i></font><br>
<br>
<font color="#008b00"><b>long</b></font> GCD(<font color="#008b00"><b>long</b></font> a, <font color="#008b00"><b>long</b></font> b);<br>
<font color="#0000ed"><i>// return value is gcd(a, b) (which is always non-negative)</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> XGCD(<font color="#008b00"><b>long</b></font>& d, <font color="#008b00"><b>long</b></font>& s, <font color="#008b00"><b>long</b></font>& t, <font color="#008b00"><b>long</b></font> a, <font color="#008b00"><b>long</b></font> b);<br>
<font color="#0000ed"><i>// d = gcd(a, b) = a*s + b*t.</i></font><br>
<br>
<font color="#0000ed"><i>// The coefficients s and t are defined according to the standard</i></font><br>
<font color="#0000ed"><i>// Euclidean algorithm applied to |a| and |b|, with the signs then</i></font><br>
<font color="#0000ed"><i>// adjusted according to the signs of a and b.</i></font><br>
<br>
<br>
<br>
<font color="#0000ed"><i>/*</i></font><font color="#0000ed"><i>*************************************************************************\</i></font><br>
<br>
<font color="#0000ed"><i> Modular Arithmetic</i></font><br>
<br>
<font color="#0000ed"><i>The following routines perform arithmetic mod n, where n > 1.</i></font><br>
<br>
<font color="#0000ed"><i>All arguments (other than exponents) are assumed to be in the range</i></font><br>
<font color="#0000ed"><i>0..n-1. Some routines may check this and raise an error if this</i></font><br>
<font color="#0000ed"><i>does not hold. Others may not, and the behaviour is unpredictable</i></font><br>
<font color="#0000ed"><i>in this case.</i></font><br>
<br>
<font color="#0000ed"><i>\*************************************************************************</i></font><font color="#0000ed"><i>*/</i></font><br>
<br>
<br>
<br>
<font color="#008b00"><b>void</b></font> AddMod(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b, <font color="#008b00"><b>const</b></font> ZZ& n); <font color="#0000ed"><i>// x = (a+b)%n</i></font><br>
ZZ AddMod(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b, <font color="#008b00"><b>const</b></font> ZZ& n);<br>
<br>
<font color="#008b00"><b>void</b></font> SubMod(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b, <font color="#008b00"><b>const</b></font> ZZ& n); <font color="#0000ed"><i>// x = (a-b)%n</i></font><br>
ZZ SubMod(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b, <font color="#008b00"><b>const</b></font> ZZ& n);<br>
<br>
<font color="#008b00"><b>void</b></font> NegateMod(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& n); <font color="#0000ed"><i>// x = -a % n</i></font><br>
ZZ NegateMod(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& n);<br>
<br>
<font color="#008b00"><b>void</b></font> MulMod(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b, <font color="#008b00"><b>const</b></font> ZZ& n); <font color="#0000ed"><i>// x = (a*b)%n</i></font><br>
ZZ MulMod(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b, <font color="#008b00"><b>const</b></font> ZZ& n);<br>
<br>
<font color="#008b00"><b>void</b></font> SqrMod(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& n); <font color="#0000ed"><i>// x = a^2 % n</i></font><br>
ZZ SqrMod(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& n);<br>
<br>
<br>
<br>
<br>
<font color="#008b00"><b>void</b></font> InvMod(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& n);<br>
ZZ InvMod(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& n);<br>
<font color="#0000ed"><i>// x = a^{-1} mod n (0 <= x < n); error is raised occurs if inverse</i></font><br>
<font color="#0000ed"><i>// not defined</i></font><br>
<br>
<font color="#0000ed"><i>// If exceptions are enabled, an object of the following class </i></font><br>
<font color="#0000ed"><i>// is throw by the InvMod routine if the inverse of a mod n is</i></font><br>
<font color="#0000ed"><i>// not defined. The methods get_a() and get_n() give read-only</i></font><br>
<font color="#0000ed"><i>// access to the offending values of a and n.</i></font><br>
<font color="#0000ed"><i>// This also happens for any indirect call to InvMod, via PowerMod,</i></font><br>
<font color="#0000ed"><i>// of via inverse computations in ZZ_p.</i></font><br>
<br>
<font color="#008b00"><b>class</b></font> InvModErrorObject : <font color="#b02f60"><b>public</b></font> ArithmeticErrorObject {<br>
<font color="#b02f60"><b>public</b></font>:<br>
InvModErrorObject(<font color="#008b00"><b>const</b></font> <font color="#008b00"><b>char</b></font> *s, <font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& n);<br>
<font color="#008b00"><b>const</b></font> ZZ& get_a() <font color="#008b00"><b>const</b></font>;<br>
<font color="#008b00"><b>const</b></font> ZZ& get_n() <font color="#008b00"><b>const</b></font>;<br>
};<br>
<br>
<font color="#008b00"><b>long</b></font> InvModStatus(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& n);<br>
<font color="#0000ed"><i>// if gcd(a,n) = 1, then return-value = 0, x = a^{-1} mod n;</i></font><br>
<font color="#0000ed"><i>// otherwise, return-value = 1, x = gcd(a, n)</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> PowerMod(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& e, <font color="#008b00"><b>const</b></font> ZZ& n);<br>
ZZ PowerMod(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& e, <font color="#008b00"><b>const</b></font> ZZ& n);<br>
<br>
<font color="#008b00"><b>void</b></font> PowerMod(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>long</b></font> e, <font color="#008b00"><b>const</b></font> ZZ& n);<br>
ZZ PowerMod(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>long</b></font> e, <font color="#008b00"><b>const</b></font> ZZ& n);<br>
<br>
<font color="#0000ed"><i>// x = a^e % n (e may be negative)</i></font><br>
<br>
<br>
<font color="#0000ed"><i>// PROMOTIONS: AddMod, SubMod, and MulMod (both procedural and functional</i></font><br>
<font color="#0000ed"><i>// forms) support promotions from long to ZZ on (a, b).</i></font><br>
<br>
<br>
<br>
<br>
<a name="modarith"></a>
<br>
<font color="#0000ed"><i>/*</i></font><font color="#0000ed"><i>*************************************************************************\</i></font><br>
<br>
<font color="#0000ed"><i> Single-precision modular arithmetic</i></font><br>
<br>
<font color="#0000ed"><i>These routines implement single-precision modular arithmetic. If n is</i></font><br>
<font color="#0000ed"><i>the modulus, all inputs should be in the range 0..n-1. The number n</i></font><br>
<font color="#0000ed"><i>itself should be in the range 2..NTL_SP_BOUND-1.</i></font><br>
<br>
<font color="#0000ed"><i>Most of these routines are, of course, implemented as fast inline</i></font><br>
<font color="#0000ed"><i>functions. No checking is done that inputs are in range.</i></font><br>
<br>
<br>
<font color="#0000ed"><i>\*************************************************************************</i></font><font color="#0000ed"><i>*/</i></font><br>
<br>
<br>
<br>
<br>
<font color="#008b00"><b>long</b></font> AddMod(<font color="#008b00"><b>long</b></font> a, <font color="#008b00"><b>long</b></font> b, <font color="#008b00"><b>long</b></font> n); <font color="#0000ed"><i>// return (a+b)%n</i></font><br>
<br>
<font color="#008b00"><b>long</b></font> SubMod(<font color="#008b00"><b>long</b></font> a, <font color="#008b00"><b>long</b></font> b, <font color="#008b00"><b>long</b></font> n); <font color="#0000ed"><i>// return (a-b)%n</i></font><br>
<br>
<font color="#008b00"><b>long</b></font> NegateMod(<font color="#008b00"><b>long</b></font> a, <font color="#008b00"><b>long</b></font> n); <font color="#0000ed"><i>// return (-a)%n</i></font><br>
<br>
<font color="#008b00"><b>long</b></font> MulMod(<font color="#008b00"><b>long</b></font> a, <font color="#008b00"><b>long</b></font> b, <font color="#008b00"><b>long</b></font> n); <font color="#0000ed"><i>// return (a*b)%n</i></font><br>
<br>
<font color="#008b00"><b>long</b></font> MulMod(<font color="#008b00"><b>long</b></font> a, <font color="#008b00"><b>long</b></font> b, <font color="#008b00"><b>long</b></font> n, mulmod_t ninv); <br>
<font color="#0000ed"><i>// return (a*b)%n. </i></font><br>
<font color="#0000ed"><i>//</i></font><br>
<font color="#0000ed"><i>// Usually faster than plain MulMod when n is fixed for many</i></font><br>
<font color="#0000ed"><i>// invocations. The value ninv should be precomputed as </i></font><br>
<font color="#0000ed"><i>// mulmod_t ninv = PrepMulMod(n);</i></font><br>
<br>
mulmod_t PrepMulMod(<font color="#008b00"><b>long</b></font> n);<br>
<font color="#0000ed"><i>// Prepare auxilliary data for MulMod.</i></font><br>
<br>
<font color="#008b00"><b>long</b></font> MulModPrecon(<font color="#008b00"><b>long</b></font> a, <font color="#008b00"><b>long</b></font> b, <font color="#008b00"><b>long</b></font> n, mulmod_precon_t bninv);<br>
<font color="#0000ed"><i>// return (a*b)%n. </i></font><br>
<font color="#0000ed"><i>//</i></font><br>
<font color="#0000ed"><i>// Usually much faster than MulMod when both b and n are fixed for </i></font><br>
<font color="#0000ed"><i>// many invocations. The value bninv should be precomputed as</i></font><br>
<font color="#0000ed"><i>// mulmod_precon_t bninv = PrepMulModPrecon(b, n);</i></font><br>
<font color="#0000ed"><i>// or as</i></font><br>
<font color="#0000ed"><i>// mulmod_precon_t bninv = PrepMulModPrecon(b, n, ninv);</i></font><br>
<font color="#0000ed"><i>// where ninv = PrepMulMod(n).</i></font><br>
<br>
mulmod_precon_t PrepMulModPrecon(<font color="#008b00"><b>long</b></font> b, <font color="#008b00"><b>long</b></font> n);<br>
mulmod_precon_t PrepMulModPrecon(<font color="#008b00"><b>long</b></font> b, <font color="#008b00"><b>long</b></font> n, mulmod_t ninv);<br>
<font color="#0000ed"><i>// Prepare auxilliary data for MulModPrecon.</i></font><br>
<font color="#0000ed"><i>// In the second version, ninv = PrepMulMod(n).</i></font><br>
<br>
<br>
<br>
<font color="#008b00"><b>long</b></font> InvMod(<font color="#008b00"><b>long</b></font> a, <font color="#008b00"><b>long</b></font> n);<br>
<font color="#0000ed"><i>// computes a^{-1} mod n. Error is raised if undefined.</i></font><br>
<br>
<font color="#008b00"><b>long</b></font> InvModStatus(<font color="#008b00"><b>long</b></font>& x, <font color="#008b00"><b>long</b></font> a, <font color="#008b00"><b>long</b></font> n);<br>
<font color="#0000ed"><i>// if gcd(a,n) = 1, then return-value = 0, x = a^{-1} mod n;</i></font><br>
<font color="#0000ed"><i>// otherwise, return-value = 1, x = gcd(a, n)</i></font><br>
<br>
<font color="#008b00"><b>long</b></font> PowerMod(<font color="#008b00"><b>long</b></font> a, <font color="#008b00"><b>long</b></font> e, <font color="#008b00"><b>long</b></font> n);<br>
<font color="#0000ed"><i>// computes a^e mod n (e may be negative)</i></font><br>
<br>
<font color="#0000ed"><i>// The following are vector versions of the MulMod routines</i></font><br>
<font color="#0000ed"><i>// They each compute x[i] = (a[i] * b)% n i = 0..k-1 </i></font><br>
<br>
<font color="#008b00"><b>void</b></font> VectorMulMod(<font color="#008b00"><b>long</b></font> k, <font color="#008b00"><b>long</b></font> *x, <font color="#008b00"><b>const</b></font> <font color="#008b00"><b>long</b></font> *a, <font color="#008b00"><b>long</b></font> b, <font color="#008b00"><b>long</b></font> n);<br>
<br>
<font color="#008b00"><b>void</b></font> VectorMulMod(<font color="#008b00"><b>long</b></font> k, <font color="#008b00"><b>long</b></font> *x, <font color="#008b00"><b>const</b></font> <font color="#008b00"><b>long</b></font> *a, <font color="#008b00"><b>long</b></font> b, <font color="#008b00"><b>long</b></font> n, <br>
mulmod_t ninv);<br>
<font color="#0000ed"><i>// ninv = PrepMulMod(n)</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> VectorMulModPrecon(<font color="#008b00"><b>long</b></font> k, <font color="#008b00"><b>long</b></font> *x, <font color="#008b00"><b>const</b></font> <font color="#008b00"><b>long</b></font> *a, <font color="#008b00"><b>long</b></font> b, <font color="#008b00"><b>long</b></font> n, <br>
mulmod_precon_t bninv);<br>
<font color="#0000ed"><i>// bninv = MulModPrecon(b, n)</i></font><br>
<br>
<br>
<font color="#0000ed"><i>// The following is provided for lagacy support, but is not generally </i></font><br>
<font color="#0000ed"><i>// recommended:</i></font><br>
<br>
<font color="#008b00"><b>long</b></font> MulDivRem(<font color="#008b00"><b>long</b></font>& q, <font color="#008b00"><b>long</b></font> a, <font color="#008b00"><b>long</b></font> b, <font color="#008b00"><b>long</b></font> n, muldivrem_t bninv);<br>
<font color="#0000ed"><i>// return (a*b)%n, set q = (a*b)/n. </i></font><br>
<font color="#0000ed"><i>// The value bninv should be precomputed as </i></font><br>
<font color="#0000ed"><i>// muldivrem_t bninv = PrepMulDivRem(b, n);</i></font><br>
<font color="#0000ed"><i>// or as</i></font><br>
<font color="#0000ed"><i>// muldivrem_t bninv = PrepMulDivRem(b, n, ninv);</i></font><br>
<font color="#0000ed"><i>// where ninv = PrepMod(n).</i></font><br>
<br>
muldivrem_t PrepMulDivRem(<font color="#008b00"><b>long</b></font> b, <font color="#008b00"><b>long</b></font> n);<br>
muldivrem_t PrepMulDivRem(<font color="#008b00"><b>long</b></font> b, <font color="#008b00"><b>long</b></font> n, mulmod_t ninv);<br>
<font color="#0000ed"><i>// Prepare auxilliary data for MulDivRem.</i></font><br>
<font color="#0000ed"><i>// In the second version, ninv = PrepMulMod(n).</i></font><br>
<br>
<font color="#0000ed"><i>// NOTE: despite the similarity in the interface to MulModPrecon,</i></font><br>
<font color="#0000ed"><i>// this routine is typically implemented in a very different way,</i></font><br>
<font color="#0000ed"><i>// and usually much less efficient.</i></font><br>
<font color="#0000ed"><i>// It was initially designed for specialized, internal use</i></font><br>
<font color="#0000ed"><i>// within NTL, but has been a part of the documented NTL</i></font><br>
<font color="#0000ed"><i>// interface for some time, and remains so even after the</i></font><br>
<font color="#0000ed"><i>// v9.0 upgrade.</i></font><br>
<br>
<br>
<br>
<font color="#0000ed"><i>//</i></font><br>
<font color="#0000ed"><i>// Compatibility notes:</i></font><br>
<font color="#0000ed"><i>//</i></font><br>
<font color="#0000ed"><i>// The types mulmod_t and muldivrem_t were introduced in NTL v9.0, as were the</i></font><br>
<font color="#0000ed"><i>// functions PrepMulMod and PrepMulDivRem. Prior to this, the built-in type</i></font><br>
<font color="#0000ed"><i>// "double" played the role of these types, and the user was expected to</i></font><br>
<font color="#0000ed"><i>// compute PrepMulMod(n) as 1/double(n) and PrepMulDivRem(b, n) as</i></font><br>
<font color="#0000ed"><i>// double(b)/double(n).</i></font><br>
<font color="#0000ed"><i>// </i></font><br>
<font color="#0000ed"><i>// By abstracting these types, NTL is able to exploit a wider variety of</i></font><br>
<font color="#0000ed"><i>// implementation strategies. Some old client code may break, but the compiler</i></font><br>
<font color="#0000ed"><i>// will easily find the code that needs to be updated, and the updates are</i></font><br>
<font color="#0000ed"><i>// quite mechanical (unless the old code implicitly made use of the assumption</i></font><br>
<font color="#0000ed"><i>// that NTL_SP_NBITS <= NTL_DOUBLE_PRECISION-3).</i></font><br>
<font color="#0000ed"><i>//</i></font><br>
<font color="#0000ed"><i>// It is highly recommended that old client codes be updated. However, one may</i></font><br>
<font color="#0000ed"><i>// build NTL with the configuration option NTL_LEGACY_SP_MULMOD=on, which will</i></font><br>
<font color="#0000ed"><i>// cause the interfaces and implementations to revert to their pre-v9.0</i></font><br>
<font color="#0000ed"><i>// definitions. This option will also make the following (obslete) function</i></font><br>
<font color="#0000ed"><i>// visible:</i></font><br>
<br>
<font color="#008b00"><b>long</b></font> MulMod2(<font color="#008b00"><b>long</b></font> a, <font color="#008b00"><b>long</b></font> b, <font color="#008b00"><b>long</b></font> n, <font color="#008b00"><b>double</b></font> bninv);<br>
<font color="#0000ed"><i>// return (a*b)%n. bninv = ((double) b)/((double) n). This is faster</i></font><br>
<font color="#0000ed"><i>// if both n and b are fixed for many multiplications.</i></font><br>
<font color="#0000ed"><i>// Note: This is OBSOLETE -- use MulModPrecon.</i></font><br>
<br>
<br>
<font color="#0000ed"><i>// As of v9.2 of NTL, this new interface allows for 60-bit moduli on most</i></font><br>
<font color="#0000ed"><i>// 64-bit machines. The requirement is that a working 128-bit integer type is</i></font><br>
<font color="#0000ed"><i>// available. For current versions of gcc, clang, and icc, this is available</i></font><br>
<font color="#0000ed"><i>// vie the types __int128_t and __uint128_t. If this requirement is met (which</i></font><br>
<font color="#0000ed"><i>// is verified during NTL installation), then a "long long" implementation for</i></font><br>
<font color="#0000ed"><i>// MulMod is used. In versions 9.0 and 9.1 of NTL, a "long double"</i></font><br>
<font color="#0000ed"><i>// implementation was introduced, which utilized the 80-bit extended double</i></font><br>
<font color="#0000ed"><i>// precision hardware on x86 machines. This also allows for 60-bit moduli on</i></font><br>
<font color="#0000ed"><i>// 64-bit machines.</i></font><br>
<br>
<font color="#0000ed"><i>// If 128-bit integer types are not avalable, or if you build NTL with the</i></font><br>
<font color="#0000ed"><i>// NTL_DISABLE_LONGLONG=on flag, NTL will attempt to use the extended double</i></font><br>
<font color="#0000ed"><i>// precision hardware to still allow 60-bit moduli. If that is not possible,</i></font><br>
<font color="#0000ed"><i>// or if you build NTL with the NTL_DISABLE_LONGDOUBLE=on flag, then NTL will</i></font><br>
<font color="#0000ed"><i>// fall back to its "classical" implementation (pre-9.0) that relies on</i></font><br>
<font color="#0000ed"><i>// double-precision arithmetic and imposes a 50-bit limit on moduli. </i></font><br>
<br>
<font color="#0000ed"><i>// Note that in on 64-bit machines, either the "long long" or "long double"</i></font><br>
<font color="#0000ed"><i>// implementations could support 62-bit moduli, rather than 60-bit moduli.</i></font><br>
<font color="#0000ed"><i>// However, the restriction to 60-bits speeds up a few things, and so seems</i></font><br>
<font color="#0000ed"><i>// like a good trade off. This is subject to change in the future.</i></font><br>
<br>
<font color="#0000ed"><i>// Also note that all of these enhancements introduced since v9.0 are only</i></font><br>
<font color="#0000ed"><i>// available to builds of NTL that use GMP. Builds that don't use GMP will</i></font><br>
<font color="#0000ed"><i>// still be restricted to 50-bit moduli on 64-bit machines. </i></font><br>
<br>
<font color="#0000ed"><i>// On machines with 32-bit longs, moduli will be resricted to 30 bits,</i></font><br>
<font color="#0000ed"><i>// regardless on the implementation, which will be based on "long long"</i></font><br>
<font color="#0000ed"><i>// arithmetic (if a 64-bit integer type is available), or on double-precision</i></font><br>
<font color="#0000ed"><i>// floating point (otherwise).</i></font><br>
<br>
<font color="#0000ed"><i>// One can detect the new (v9) interface by testing if the macro</i></font><br>
<font color="#0000ed"><i>// NTL_HAVE_MULMOD_T is defined. The following code can be used to make</i></font><br>
<font color="#0000ed"><i>// new-style NTL clients work with either older (pre-9.0) versions of NTL or</i></font><br>
<font color="#0000ed"><i>// newer versions (post-9.0):</i></font><br>
<br>
<br>
<font color="#1773cc"> #ifndef NTL_HAVE_MULMOD_T</font><br>
<font color="#008b00"><b>namespace</b></font> NTL {<br>
<font color="#008b00"><b>typedef</b></font> <font color="#008b00"><b>double</b></font> mulmod_t;<br>
<font color="#008b00"><b>typedef</b></font> <font color="#008b00"><b>double</b></font> muldivrem_t;<br>
<br>
<font color="#008b00"><b>static</b></font> <font color="#008b00"><b>inline</b></font> <font color="#008b00"><b>double</b></font> PrepMulMod(<font color="#008b00"><b>long</b></font> n) <br>
{ <font color="#b02f60"><b>return</b></font> <font color="#008b00"><b>double</b></font>(<font color="#ff8b00">1L</font>)/<font color="#008b00"><b>double</b></font>(n); }<br>
<br>
<font color="#008b00"><b>static</b></font> <font color="#008b00"><b>inline</b></font> <font color="#008b00"><b>double</b></font> PrepMulDivRem(<font color="#008b00"><b>long</b></font> b, <font color="#008b00"><b>long</b></font> n, <font color="#008b00"><b>double</b></font> ninv) <br>
{ <font color="#b02f60"><b>return</b></font> <font color="#008b00"><b>double</b></font>(b)*ninv; }<br>
<br>
<font color="#008b00"><b>static</b></font> <font color="#008b00"><b>inline</b></font> <font color="#008b00"><b>double</b></font> PrepMulDivRem(<font color="#008b00"><b>long</b></font> b, <font color="#008b00"><b>long</b></font> n) <br>
{ <font color="#b02f60"><b>return</b></font> <font color="#008b00"><b>double</b></font>(b)/<font color="#008b00"><b>double</b></font>(n); }<br>
<br>
<font color="#008b00"><b>static</b></font> <font color="#008b00"><b>inline</b></font> <font color="#008b00"><b>double</b></font> PrepMulModPrecon(<font color="#008b00"><b>long</b></font> b, <font color="#008b00"><b>long</b></font> n) <br>
{ <font color="#b02f60"><b>return</b></font> PrepMulModPrecon(b, n, PrepMulMod(n)); }<br>
}<br>
<font color="#1773cc"> #endif</font><br>
<br>
<br>
<br>
<br>
<br>
<font color="#0000ed"><i>/*</i></font><font color="#0000ed"><i>*************************************************************************\</i></font><br>
<br>
<font color="#0000ed"><i> Shift Operations</i></font><br>
<br>
<font color="#0000ed"><i>LeftShift by n means multiplication by 2^n</i></font><br>
<font color="#0000ed"><i>RightShift by n means division by 2^n, with truncation toward zero</i></font><br>
<font color="#0000ed"><i> (so the sign is preserved).</i></font><br>
<br>
<font color="#0000ed"><i>A negative shift amount reverses the direction of the shift.</i></font><br>
<br>
<font color="#0000ed"><i>\*************************************************************************</i></font><font color="#0000ed"><i>*/</i></font><br>
<br>
<font color="#0000ed"><i>// operator notation:</i></font><br>
<br>
ZZ <font color="#b02f60"><b>operator</b></font><<(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>long</b></font> n);<br>
ZZ <font color="#b02f60"><b>operator</b></font>>>(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>long</b></font> n);<br>
<br>
ZZ& <font color="#b02f60"><b>operator</b></font><<=(ZZ& x, <font color="#008b00"><b>long</b></font> n);<br>
ZZ& <font color="#b02f60"><b>operator</b></font>>>=(ZZ& x, <font color="#008b00"><b>long</b></font> n);<br>
<br>
<font color="#0000ed"><i>// procedural versions:</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> LeftShift(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>long</b></font> n); <br>
ZZ LeftShift(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>long</b></font> n);<br>
<br>
<font color="#008b00"><b>void</b></font> RightShift(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>long</b></font> n); <br>
ZZ RightShift(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>long</b></font> n); <br>
<br>
<br>
<br>
<font color="#0000ed"><i>/*</i></font><font color="#0000ed"><i>*************************************************************************\</i></font><br>
<br>
<font color="#0000ed"><i> Bits and Bytes</i></font><br>
<br>
<font color="#0000ed"><i>\*************************************************************************</i></font><font color="#0000ed"><i>*/</i></font><br>
<br>
<br>
<br>
<font color="#008b00"><b>long</b></font> MakeOdd(ZZ& x);<br>
<font color="#0000ed"><i>// removes factors of 2 from x, returns the number of 2's removed</i></font><br>
<font color="#0000ed"><i>// returns 0 if x == 0</i></font><br>
<br>
<font color="#008b00"><b>long</b></font> NumTwos(<font color="#008b00"><b>const</b></font> ZZ& x);<br>
<font color="#0000ed"><i>// returns max e such that 2^e divides x if x != 0, and returns 0 if x == 0.</i></font><br>
<br>
<font color="#008b00"><b>long</b></font> IsOdd(<font color="#008b00"><b>const</b></font> ZZ& a); <font color="#0000ed"><i>// test if a is odd</i></font><br>
<br>
<font color="#008b00"><b>long</b></font> NumBits(<font color="#008b00"><b>const</b></font> ZZ& a);<br>
<font color="#008b00"><b>long</b></font> NumBits(<font color="#008b00"><b>long</b></font> a); <br>
<font color="#0000ed"><i>// returns the number of bits in binary represenation of |a|; </i></font><br>
<font color="#0000ed"><i>// NumBits(0) = 0</i></font><br>
<br>
<br>
<font color="#008b00"><b>long</b></font> bit(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>long</b></font> k);<br>
<font color="#008b00"><b>long</b></font> bit(<font color="#008b00"><b>long</b></font> a, <font color="#008b00"><b>long</b></font> k); <br>
<font color="#0000ed"><i>// returns bit k of |a|, position 0 being the low-order bit.</i></font><br>
<font color="#0000ed"><i>// If k < 0 or k >= NumBits(a), returns 0.</i></font><br>
<br>
<br>
<font color="#008b00"><b>void</b></font> trunc(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>long</b></font> k);<br>
<font color="#0000ed"><i>// x = low order k bits of |a|. </i></font><br>
<font color="#0000ed"><i>// If k <= 0, x = 0.</i></font><br>
<br>
<font color="#0000ed"><i>// two functional variants:</i></font><br>
ZZ trunc_ZZ(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>long</b></font> k); <br>
<font color="#008b00"><b>long</b></font> trunc_long(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>long</b></font> k);<br>
<br>
<font color="#008b00"><b>long</b></font> SetBit(ZZ& x, <font color="#008b00"><b>long</b></font> p);<br>
<font color="#0000ed"><i>// returns original value of p-th bit of |a|, and replaces p-th bit of</i></font><br>
<font color="#0000ed"><i>// a by 1 if it was zero; low order bit is bit 0; error if p < 0;</i></font><br>
<font color="#0000ed"><i>// the sign of x is maintained</i></font><br>
<br>
<font color="#008b00"><b>long</b></font> SwitchBit(ZZ& x, <font color="#008b00"><b>long</b></font> p);<br>
<font color="#0000ed"><i>// returns original value of p-th bit of |a|, and switches the value</i></font><br>
<font color="#0000ed"><i>// of p-th bit of a; low order bit is bit 0; error if p < 0</i></font><br>
<font color="#0000ed"><i>// the sign of x is maintained</i></font><br>
<br>
<font color="#008b00"><b>long</b></font> weight(<font color="#008b00"><b>const</b></font> ZZ& a); <font color="#0000ed"><i>// returns Hamming weight of |a|</i></font><br>
<font color="#008b00"><b>long</b></font> weight(<font color="#008b00"><b>long</b></font> a); <br>
<br>
<font color="#0000ed"><i>// bit-wise Boolean operations, procedural form:</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> bit_and(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b); <font color="#0000ed"><i>// x = |a| AND |b|</i></font><br>
<font color="#008b00"><b>void</b></font> bit_or(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b); <font color="#0000ed"><i>// x = |a| OR |b|</i></font><br>
<font color="#008b00"><b>void</b></font> bit_xor(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b); <font color="#0000ed"><i>// x = |a| XOR |b|</i></font><br>
<br>
<font color="#0000ed"><i>// bit-wise Boolean operations, operator notation:</i></font><br>
<br>
ZZ <font color="#b02f60"><b>operator</b></font>&(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b);<br>
ZZ <font color="#b02f60"><b>operator</b></font>|(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b);<br>
ZZ <font color="#b02f60"><b>operator</b></font>^(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& b);<br>
<br>
<font color="#0000ed"><i>// PROMOTIONS: the above bit-wise operations (both procedural </i></font><br>
<font color="#0000ed"><i>// and operator forms) provide promotions from long to ZZ on (a, b).</i></font><br>
<br>
ZZ& <font color="#b02f60"><b>operator</b></font>&=(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& b);<br>
ZZ& <font color="#b02f60"><b>operator</b></font>&=(ZZ& x, <font color="#008b00"><b>long</b></font> b);<br>
<br>
ZZ& <font color="#b02f60"><b>operator</b></font>|=(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& b);<br>
ZZ& <font color="#b02f60"><b>operator</b></font>|=(ZZ& x, <font color="#008b00"><b>long</b></font> b);<br>
<br>
ZZ& <font color="#b02f60"><b>operator</b></font>^=(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& b);<br>
ZZ& <font color="#b02f60"><b>operator</b></font>^=(ZZ& x, <font color="#008b00"><b>long</b></font> b);<br>
<br>
<br>
<br>
<font color="#0000ed"><i>// conversions between byte sequences and ZZ's</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> ZZFromBytes(ZZ& x, <font color="#008b00"><b>const</b></font> <font color="#008b00"><b>unsigned</b></font> <font color="#008b00"><b>char</b></font> *p, <font color="#008b00"><b>long</b></font> n);<br>
ZZ ZZFromBytes(<font color="#008b00"><b>const</b></font> <font color="#008b00"><b>unsigned</b></font> <font color="#008b00"><b>char</b></font> *p, <font color="#008b00"><b>long</b></font> n);<br>
<font color="#0000ed"><i>// x = sum(p[i]*256^i, i=0..n-1). </i></font><br>
<font color="#0000ed"><i>// NOTE: in the unusual event that a char is more than 8 bits, </i></font><br>
<font color="#0000ed"><i>// only the low order 8 bits of p[i] are used</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> BytesFromZZ(<font color="#008b00"><b>unsigned</b></font> <font color="#008b00"><b>char</b></font> *p, <font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>long</b></font> n);<br>
<font color="#0000ed"><i>// Computes p[0..n-1] such that abs(a) == sum(p[i]*256^i, i=0..n-1) mod 256^n.</i></font><br>
<br>
<font color="#008b00"><b>long</b></font> NumBytes(<font color="#008b00"><b>const</b></font> ZZ& a);<br>
<font color="#008b00"><b>long</b></font> NumBytes(<font color="#008b00"><b>long</b></font> a);<br>
<font color="#0000ed"><i>// returns # of base 256 digits needed to represent abs(a).</i></font><br>
<font color="#0000ed"><i>// NumBytes(0) == 0.</i></font><br>
<br>
<br>
<a name="prg"></a>
<br>
<font color="#0000ed"><i>/*</i></font><font color="#0000ed"><i>*************************************************************************\</i></font><br>
<br>
<font color="#0000ed"><i> Pseudo-Random Numbers</i></font><br>
<br>
<font color="#0000ed"><i>\*************************************************************************</i></font><font color="#0000ed"><i>*/</i></font><br>
<br>
<br>
<font color="#0000ed"><i>// Routines for generating pseudo-random numbers.</i></font><br>
<br>
<font color="#0000ed"><i>// These routines generate high qualtity, cryptographically strong</i></font><br>
<font color="#0000ed"><i>// pseudo-random numbers. They are implemented so that their behaviour</i></font><br>
<font color="#0000ed"><i>// is completely independent of the underlying hardware and long </i></font><br>
<font color="#0000ed"><i>// integer implementation. Note, however, that other routines </i></font><br>
<font color="#0000ed"><i>// throughout NTL use pseudo-random numbers, and because of this,</i></font><br>
<font color="#0000ed"><i>// the word size of the machine can impact the sequence of numbers</i></font><br>
<font color="#0000ed"><i>// seen by a client program.</i></font><br>
<br>
<br>
<font color="#008b00"><b>void</b></font> SetSeed(<font color="#008b00"><b>const</b></font> ZZ& s);<br>
<font color="#008b00"><b>void</b></font> SetSeed(<font color="#008b00"><b>const</b></font> <font color="#008b00"><b>unsigned</b></font> <font color="#008b00"><b>char</b></font> *data, <font color="#008b00"><b>long</b></font> dlen);<br>
<font color="#008b00"><b>void</b></font> SetSeed(<font color="#008b00"><b>const</b></font> RandomStream& s);<br>
<font color="#0000ed"><i>// Initializes generator with a "seed".</i></font><br>
<br>
<font color="#0000ed"><i>// The first version hashes the binary representation of s to obtain a key for</i></font><br>
<font color="#0000ed"><i>// a low-level RandomStream object (see below).</i></font><br>
<br>
<font color="#0000ed"><i>// The second version does the same, hashing the first dlen bytes pointed to by</i></font><br>
<font color="#0000ed"><i>// data to obtain a key for the RandomStream object.</i></font><br>
<br>
<font color="#0000ed"><i>// The third version initializes the PRG state directly with the given</i></font><br>
<font color="#0000ed"><i>// RandomStream object.</i></font><br>
<br>
<font color="#0000ed"><i>// EXCEPTIONS: strong ES</i></font><br>
<br>
<br>
<font color="#008b00"><b>void</b></font> RandomBnd(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& n);<br>
ZZ RandomBnd(<font color="#008b00"><b>const</b></font> ZZ& n);<br>
<font color="#008b00"><b>void</b></font> RandomBnd(<font color="#008b00"><b>long</b></font>& x, <font color="#008b00"><b>long</b></font> n);<br>
<font color="#008b00"><b>long</b></font> RandomBnd(<font color="#008b00"><b>long</b></font> n);<br>
<font color="#0000ed"><i>// x = pseudo-random number in the range 0..n-1, or 0 if n <= 0</i></font><br>
<font color="#0000ed"><i>// EXCEPTIONS: strong ES</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> RandomBits(ZZ& x, <font color="#008b00"><b>long</b></font> l);<br>
ZZ RandomBits_ZZ(<font color="#008b00"><b>long</b></font> l);<br>
<font color="#008b00"><b>void</b></font> RandomBits(<font color="#008b00"><b>long</b></font>& x, <font color="#008b00"><b>long</b></font> l);<br>
<font color="#008b00"><b>long</b></font> RandomBits_long(<font color="#008b00"><b>long</b></font> l);<br>
<font color="#0000ed"><i>// x = pseudo-random number in the range 0..2^l-1.</i></font><br>
<font color="#0000ed"><i>// EXCEPTIONS: strong ES</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> RandomLen(ZZ& x, <font color="#008b00"><b>long</b></font> l);<br>
ZZ RandomLen_ZZ(<font color="#008b00"><b>long</b></font> l);<br>
<font color="#008b00"><b>void</b></font> RandomLen(<font color="#008b00"><b>long</b></font>& x, <font color="#008b00"><b>long</b></font> l);<br>
<font color="#008b00"><b>long</b></font> RandomLen_long(<font color="#008b00"><b>long</b></font> l);<br>
<font color="#0000ed"><i>// x = psuedo-random number with precisely l bits,</i></font><br>
<font color="#0000ed"><i>// or 0 of l <= 0.</i></font><br>
<font color="#0000ed"><i>// EXCEPTIONS: strong ES</i></font><br>
<br>
<font color="#008b00"><b>unsigned</b></font> <font color="#008b00"><b>long</b></font> RandomBits_ulong(<font color="#008b00"><b>long</b></font> l);<br>
<font color="#0000ed"><i>// returns a pseudo-random number in the range 0..2^l-1</i></font><br>
<font color="#0000ed"><i>// EXCEPTIONS: strong ES</i></font><br>
<br>
<font color="#008b00"><b>unsigned</b></font> <font color="#008b00"><b>long</b></font> RandomWord();<br>
<font color="#0000ed"><i>// returns a word filled with pseudo-random bits.</i></font><br>
<font color="#0000ed"><i>// Equivalent to RandomBits_ulong(NTL_BITS_PER_LONG).</i></font><br>
<font color="#0000ed"><i>// EXCEPTIONS: strong ES</i></font><br>
<br>
<br>
<br>
<font color="#008b00"><b>class</b></font> RandomStream { <br>
<font color="#0000ed"><i>// The low-level pseudo-random generator (PRG).</i></font><br>
<font color="#0000ed"><i>// After initializing it with a key, one can effectively read an unbounded</i></font><br>
<font color="#0000ed"><i>// stream of pseudorandom bytes</i></font><br>
<br>
<font color="#b02f60"><b>public</b></font>:<br>
<br>
<font color="#008b00"><b>explicit</b></font> RandomStream(<font color="#008b00"><b>const</b></font> <font color="#008b00"><b>unsigned</b></font> <font color="#008b00"><b>char</b></font> *key);<br>
<font color="#0000ed"><i>// key should point to an array of NTL_PRG_KEYLEN bytes</i></font><br>
<font color="#0000ed"><i>// EXCEPTIONS: nothrow</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> get(<font color="#008b00"><b>unsigned</b></font> <font color="#008b00"><b>char</b></font> *res, <font color="#008b00"><b>long</b></font> n); <br>
<font color="#0000ed"><i>// read the next n bytes from the stream and store to location pointed to by</i></font><br>
<font color="#0000ed"><i>// res</i></font><br>
<font color="#0000ed"><i>// EXCEPTIONS: throws a LogicError exception if n is negative</i></font><br>
<br>
RandomStream(<font color="#008b00"><b>const</b></font> RandomStream&); <font color="#0000ed"><i>// default</i></font><br>
RandomStream& <font color="#b02f60"><b>operator</b></font>=(<font color="#008b00"><b>const</b></font> RandomStream&); <font color="#0000ed"><i>// default</i></font><br>
<font color="#0000ed"><i>// EXCEPTIONS: nothrow</i></font><br>
};<br>
<br>
<br>
RandomStream& GetCurrentRandomStream();<br>
<font color="#0000ed"><i>// get reference to the current PRG state. If SetSeed has not been called, it</i></font><br>
<font color="#0000ed"><i>// is called with a default value (which should be unique to each</i></font><br>
<font color="#0000ed"><i>// process/thread). NOTE: this is a reference to a thread-local object, so</i></font><br>
<font color="#0000ed"><i>// different threads will use different PRG's, and by default, each will be</i></font><br>
<font color="#0000ed"><i>// initialized with a unique seed.</i></font><br>
<font color="#0000ed"><i>// NOTE: using this reference, you can copy the current PRG state or assign a</i></font><br>
<font color="#0000ed"><i>// different value to it; however, see the helper class RandomStreamPush below,</i></font><br>
<font color="#0000ed"><i>// which may be more convenient.</i></font><br>
<font color="#0000ed"><i>// EXCEPTIONS: strong ES</i></font><br>
<br>
<br>
<br>
<font color="#008b00"><b>class</b></font> RandomStreamPush {<br>
<font color="#0000ed"><i>// RAII for saving/restoring current PRG state</i></font><br>
<font color="#b02f60"><b>public</b></font>:<br>
RandomStreamPush(); <font color="#0000ed"><i>// save a copy of the current PRG state</i></font><br>
<font color="#0000ed"><i>// EXCEPTIONS: strong ES</i></font><br>
<br>
~RandomStreamPush(); <font color="#0000ed"><i>// restore the saveed copy of the PRG state</i></font><br>
<br>
<font color="#b02f60"><b>private</b></font>: <br>
RandomStreamPush(<font color="#008b00"><b>const</b></font> RandomStreamPush&); <font color="#0000ed"><i>// disable</i></font><br>
<font color="#008b00"><b>void</b></font> <font color="#b02f60"><b>operator</b></font>=(<font color="#008b00"><b>const</b></font> RandomStreamPush&); <font color="#0000ed"><i>// disable</i></font><br>
};<br>
<br>
<br>
<font color="#008b00"><b>void</b></font> DeriveKey(<font color="#008b00"><b>unsigned</b></font> <font color="#008b00"><b>char</b></font> *key, <font color="#008b00"><b>long</b></font> klen, <br>
<font color="#008b00"><b>const</b></font> <font color="#008b00"><b>unsigned</b></font> <font color="#008b00"><b>char</b></font> *data, <font color="#008b00"><b>long</b></font> dlen);<br>
<font color="#0000ed"><i>// utility routine to derive from the byte string (data, dlen) a byte string</i></font><br>
<font color="#0000ed"><i>// (key, klen). Heuristically, if (data, dlen) has high entropy, then (key,</i></font><br>
<font color="#0000ed"><i>// klen) should be pseudorandom. This routine is also used internally to</i></font><br>
<font color="#0000ed"><i>// derive PRG keys.</i></font><br>
<font color="#0000ed"><i>// EXCEPTIONS: throws LogicError exception if klen < 0 or hlen < 0</i></font><br>
<br>
<br>
<br>
<font color="#0000ed"><i>/*</i></font><font color="#0000ed"><i>*************************************************************************\</i></font><br>
<br>
<font color="#0000ed"><i> Incremental Chinese Remaindering</i></font><br>
<br>
<font color="#0000ed"><i>\*************************************************************************</i></font><font color="#0000ed"><i>*/</i></font><br>
<br>
<font color="#008b00"><b>long</b></font> CRT(ZZ& a, ZZ& p, <font color="#008b00"><b>const</b></font> ZZ& A, <font color="#008b00"><b>const</b></font> ZZ& P);<br>
<font color="#008b00"><b>long</b></font> CRT(ZZ& a, ZZ& p, <font color="#008b00"><b>long</b></font> A, <font color="#008b00"><b>long</b></font> P);<br>
<br>
<font color="#0000ed"><i>// 0 <= A < P, (p, P) = 1; computes a' such that a' = a mod p, </i></font><br>
<font color="#0000ed"><i>// a' = A mod P, and -p*P/2 < a' <= p*P/2; sets a := a', p := p*P, and</i></font><br>
<font color="#0000ed"><i>// returns 1 if a's value has changed, otherwise 0</i></font><br>
<br>
<br>
<font color="#0000ed"><i>/*</i></font><font color="#0000ed"><i>*************************************************************************\</i></font><br>
<br>
<font color="#0000ed"><i> Rational Reconstruction</i></font><br>
<br>
<font color="#0000ed"><i>\*************************************************************************</i></font><font color="#0000ed"><i>*/</i></font><br>
<br>
<font color="#008b00"><b>long</b></font> ReconstructRational(ZZ& a, ZZ& b, <font color="#008b00"><b>const</b></font> ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& m, <br>
<font color="#008b00"><b>const</b></font> ZZ& a_bound, <font color="#008b00"><b>const</b></font> ZZ& b_bound);<br>
<br>
<font color="#0000ed"><i>// 0 <= x < m, m > 2 * a_bound * b_bound,</i></font><br>
<font color="#0000ed"><i>// a_bound >= 0, b_bound > 0</i></font><br>
<br>
<font color="#0000ed"><i>// This routine either returns 0, leaving a and b unchanged, </i></font><br>
<font color="#0000ed"><i>// or returns 1 and sets a and b so that</i></font><br>
<font color="#0000ed"><i>// (1) a = b x (mod m),</i></font><br>
<font color="#0000ed"><i>// (2) |a| <= a_bound, 0 < b <= b_bound, and</i></font><br>
<font color="#0000ed"><i>// (3) gcd(m, b) = gcd(a, b).</i></font><br>
<br>
<font color="#0000ed"><i>// If there exist a, b satisfying (1), (2), and </i></font><br>
<font color="#0000ed"><i>// (3') gcd(m, b) = 1,</i></font><br>
<font color="#0000ed"><i>// then a, b are uniquely determined if we impose the additional</i></font><br>
<font color="#0000ed"><i>// condition that gcd(a, b) = 1; moreover, if such a, b exist,</i></font><br>
<font color="#0000ed"><i>// then these values are returned by the routine.</i></font><br>
<br>
<font color="#0000ed"><i>// Unless the calling routine can *a priori* guarantee the existence</i></font><br>
<font color="#0000ed"><i>// of a, b satisfying (1), (2), and (3'),</i></font><br>
<font color="#0000ed"><i>// then to ensure correctness, the calling routine should check</i></font><br>
<font color="#0000ed"><i>// that gcd(m, b) = 1, or equivalently, gcd(a, b) = 1.</i></font><br>
<br>
<font color="#0000ed"><i>// This is implemented using a variant of Lehmer's extended</i></font><br>
<font color="#0000ed"><i>// Euclidean algorithm.</i></font><br>
<br>
<font color="#0000ed"><i>// Literature: see G. Collins and M. Encarnacion, J. Symb. Comp. 20:287-297, </i></font><br>
<font color="#0000ed"><i>// 1995; P. Wang, M. Guy, and J. Davenport, SIGSAM Bulletin 16:2-3, 1982. </i></font><br>
<br>
<br>
<font color="#0000ed"><i>/*</i></font><font color="#0000ed"><i>*************************************************************************\</i></font><br>
<br>
<font color="#0000ed"><i> Primality Testing </i></font><br>
<font color="#0000ed"><i> and Prime Number Generation</i></font><br>
<br>
<font color="#0000ed"><i>\*************************************************************************</i></font><font color="#0000ed"><i>*/</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> GenPrime(ZZ& n, <font color="#008b00"><b>long</b></font> l, <font color="#008b00"><b>long</b></font> err = <font color="#ff8b00">80</font>);<br>
ZZ GenPrime_ZZ(<font color="#008b00"><b>long</b></font> l, <font color="#008b00"><b>long</b></font> err = <font color="#ff8b00">80</font>);<br>
<font color="#008b00"><b>long</b></font> GenPrime_long(<font color="#008b00"><b>long</b></font> l, <font color="#008b00"><b>long</b></font> err = <font color="#ff8b00">80</font>);<br>
<br>
<font color="#0000ed"><i>// GenPrime generates a random prime n of length l so that the</i></font><br>
<font color="#0000ed"><i>// probability that the resulting n is composite is bounded by 2^(-err).</i></font><br>
<font color="#0000ed"><i>// This calls the routine RandomPrime below, and uses results of </i></font><br>
<font color="#0000ed"><i>// Damgard, Landrock, Pomerance to "optimize" </i></font><br>
<font color="#0000ed"><i>// the number of Miller-Rabin trials at the end.</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> GenGermainPrime(ZZ& n, <font color="#008b00"><b>long</b></font> l, <font color="#008b00"><b>long</b></font> err = <font color="#ff8b00">80</font>);<br>
ZZ GenGermainPrime_ZZ(<font color="#008b00"><b>long</b></font> l, <font color="#008b00"><b>long</b></font> err = <font color="#ff8b00">80</font>);<br>
<font color="#008b00"><b>long</b></font> GenGermainPrime_long(<font color="#008b00"><b>long</b></font> l, <font color="#008b00"><b>long</b></font> err = <font color="#ff8b00">80</font>);<br>
<br>
<font color="#0000ed"><i>// A (Sophie) Germain prime is a prime p such that p' = 2*p+1 is also a prime.</i></font><br>
<font color="#0000ed"><i>// Such primes are useful for cryptographic applications...cryptographers</i></font><br>
<font color="#0000ed"><i>// sometimes call p' a "strong" or "safe" prime.</i></font><br>
<font color="#0000ed"><i>// GenGermainPrime generates a random Germain prime n of length l</i></font><br>
<font color="#0000ed"><i>// so that the probability that either n or 2*n+1 is not a prime</i></font><br>
<font color="#0000ed"><i>// is bounded by 2^(-err).</i></font><br>
<br>
<br>
<font color="#008b00"><b>long</b></font> ProbPrime(<font color="#008b00"><b>const</b></font> ZZ& n, <font color="#008b00"><b>long</b></font> NumTrials = <font color="#ff8b00">10</font>);<br>
<font color="#008b00"><b>long</b></font> ProbPrime(<font color="#008b00"><b>long</b></font> n, <font color="#008b00"><b>long</b></font> NumTrials = <font color="#ff8b00">10</font>);<br>
<font color="#0000ed"><i>// performs up to NumTrials Miller-witness tests (after some trial division).</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> RandomPrime(ZZ& n, <font color="#008b00"><b>long</b></font> l, <font color="#008b00"><b>long</b></font> NumTrials=<font color="#ff8b00">10</font>);<br>
ZZ RandomPrime_ZZ(<font color="#008b00"><b>long</b></font> l, <font color="#008b00"><b>long</b></font> NumTrials=<font color="#ff8b00">10</font>);<br>
<font color="#008b00"><b>long</b></font> RandomPrime_long(<font color="#008b00"><b>long</b></font> l, <font color="#008b00"><b>long</b></font> NumTrials=<font color="#ff8b00">10</font>);<br>
<font color="#0000ed"><i>// n = random l-bit prime. Uses ProbPrime with NumTrials.</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> NextPrime(ZZ& n, <font color="#008b00"><b>const</b></font> ZZ& m, <font color="#008b00"><b>long</b></font> NumTrials=<font color="#ff8b00">10</font>);<br>
ZZ NextPrime(<font color="#008b00"><b>const</b></font> ZZ& m, <font color="#008b00"><b>long</b></font> NumTrials=<font color="#ff8b00">10</font>);<br>
<font color="#0000ed"><i>// n = smallest prime >= m. Uses ProbPrime with NumTrials.</i></font><br>
<br>
<font color="#008b00"><b>long</b></font> NextPrime(<font color="#008b00"><b>long</b></font> m, <font color="#008b00"><b>long</b></font> NumTrials=<font color="#ff8b00">10</font>);<br>
<font color="#0000ed"><i>// Single precision version of the above.</i></font><br>
<font color="#0000ed"><i>// Result will always be bounded by NTL_ZZ_SP_BOUND, and an</i></font><br>
<font color="#0000ed"><i>// error is raised if this cannot be satisfied.</i></font><br>
<br>
<font color="#008b00"><b>long</b></font> MillerWitness(<font color="#008b00"><b>const</b></font> ZZ& n, <font color="#008b00"><b>const</b></font> ZZ& w);<br>
<font color="#0000ed"><i>// Tests if w is a witness to compositeness a la Miller. Assumption: n is</i></font><br>
<font color="#0000ed"><i>// odd and positive, 0 <= w < n.</i></font><br>
<font color="#0000ed"><i>// Return value of 1 implies n is composite.</i></font><br>
<font color="#0000ed"><i>// Return value of 0 indicates n might be prime.</i></font><br>
<br>
<br>
<font color="#0000ed"><i>/*</i></font><font color="#0000ed"><i>*************************************************************************\</i></font><br>
<br>
<font color="#0000ed"><i> Exponentiation</i></font><br>
<br>
<font color="#0000ed"><i>\*************************************************************************</i></font><font color="#0000ed"><i>*/</i></font><br>
<br>
<br>
<font color="#008b00"><b>void</b></font> power(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>long</b></font> e); <font color="#0000ed"><i>// x = a^e (e >= 0)</i></font><br>
ZZ power(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>long</b></font> e); <br>
<br>
<font color="#008b00"><b>void</b></font> power(ZZ& x, <font color="#008b00"><b>long</b></font> a, <font color="#008b00"><b>long</b></font> e);<br>
<br>
<font color="#0000ed"><i>// two functional variants:</i></font><br>
ZZ power_ZZ(<font color="#008b00"><b>long</b></font> a, <font color="#008b00"><b>long</b></font> e);<br>
<font color="#008b00"><b>long</b></font> power_long(<font color="#008b00"><b>long</b></font> a, <font color="#008b00"><b>long</b></font> e);<br>
<br>
<font color="#008b00"><b>void</b></font> power2(ZZ& x, <font color="#008b00"><b>long</b></font> e); <font color="#0000ed"><i>// x = 2^e (e >= 0)</i></font><br>
ZZ power2_ZZ(<font color="#008b00"><b>long</b></font> e);<br>
<br>
<br>
<font color="#0000ed"><i>/*</i></font><font color="#0000ed"><i>*************************************************************************\</i></font><br>
<br>
<font color="#0000ed"><i> Square Roots</i></font><br>
<br>
<font color="#0000ed"><i>\*************************************************************************</i></font><font color="#0000ed"><i>*/</i></font><br>
<br>
<br>
<font color="#008b00"><b>void</b></font> SqrRoot(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& a); <font color="#0000ed"><i>// x = floor(a^{1/2}) (a >= 0)</i></font><br>
ZZ SqrRoot(<font color="#008b00"><b>const</b></font> ZZ& a); <br>
<br>
<font color="#008b00"><b>long</b></font> SqrRoot(<font color="#008b00"><b>long</b></font> a); <br>
<br>
<br>
<br>
<br>
<font color="#0000ed"><i>/*</i></font><font color="#0000ed"><i>*************************************************************************\</i></font><br>
<br>
<font color="#0000ed"><i> Jacobi symbol and modular square roots</i></font><br>
<br>
<font color="#0000ed"><i>\*************************************************************************</i></font><font color="#0000ed"><i>*/</i></font><br>
<br>
<br>
<font color="#008b00"><b>long</b></font> Jacobi(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& n);<br>
<font color="#0000ed"><i>// compute Jacobi symbol of a and n; assumes 0 <= a < n, n odd</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> SqrRootMod(ZZ& x, <font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& n);<br>
ZZ SqrRootMod(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>const</b></font> ZZ& n); <br>
<font color="#0000ed"><i>// computes square root of a mod n; assumes n is an odd prime, and</i></font><br>
<font color="#0000ed"><i>// that a is a square mod n, with 0 <= a < n.</i></font><br>
<br>
<br>
<br>
<br>
<font color="#0000ed"><i>/*</i></font><font color="#0000ed"><i>*************************************************************************\</i></font><br>
<br>
<font color="#0000ed"><i> Input/Output</i></font><br>
<br>
<font color="#0000ed"><i>I/O Format:</i></font><br>
<br>
<font color="#0000ed"><i>Numbers are written in base 10, with an optional minus sign.</i></font><br>
<br>
<font color="#0000ed"><i>\*************************************************************************</i></font><font color="#0000ed"><i>*/</i></font><br>
<br>
istream& <font color="#b02f60"><b>operator</b></font>>>(istream& s, ZZ& x); <br>
ostream& <font color="#b02f60"><b>operator</b></font><<(ostream& s, <font color="#008b00"><b>const</b></font> ZZ& a); <br>
<br>
<br>
<br>
<font color="#0000ed"><i>/*</i></font><font color="#0000ed"><i>*************************************************************************\</i></font><br>
<br>
<font color="#0000ed"><i> Miscellany</i></font><br>
<br>
<font color="#0000ed"><i>\*************************************************************************</i></font><font color="#0000ed"><i>*/</i></font><br>
<br>
<br>
<font color="#0000ed"><i>// The following macros are defined:</i></font><br>
<br>
<font color="#1773cc">#define NTL_ZZ_NBITS (...) </font><font color="#0000ed"><i>// number of bits in a zzigit;</i></font><br>
<font color="#0000ed"><i>// a ZZ is represented as a sequence of zzigits.</i></font><br>
<br>
<font color="#1773cc">#define NTL_SP_NBITS (...) </font><font color="#0000ed"><i>// max number of bits in a "single-precision" number</i></font><br>
<br>
<font color="#1773cc">#define NTL_WSP_NBITS (...) </font><font color="#0000ed"><i>// max number of bits in a "wide single-precision"</i></font><br>
<font color="#0000ed"><i>// number</i></font><br>
<br>
<font color="#0000ed"><i>// The following relations hold:</i></font><br>
<font color="#0000ed"><i>// 30 <= NTL_SP_NBITS <= NTL_WSP_NBITS </i></font><br>
<font color="#0000ed"><i>// <= min(NTL_ZZ_NBITS, NTL_BITS_PER_LONG-2)</i></font><br>
<br>
<font color="#0000ed"><i>// Note that NTL_ZZ_NBITS may be less than, equal to, or greater than</i></font><br>
<font color="#0000ed"><i>// NTL_BITS_PER_LONG -- no particular relationship should be assumed to hold.</i></font><br>
<font color="#0000ed"><i>// In particular, expressions like (1L << NTL_ZZ_BITS) might overflow.</i></font><br>
<font color="#0000ed"><i>//</i></font><br>
<font color="#0000ed"><i>// "single-precision" numbers are meant to be used in conjunction with the</i></font><br>
<font color="#0000ed"><i>// single-precision modular arithmetic routines.</i></font><br>
<font color="#0000ed"><i>//</i></font><br>
<font color="#0000ed"><i>// "wide single-precision" numbers are meant to be used in conjunction</i></font><br>
<font color="#0000ed"><i>// with the ZZ arithmetic routines for optimal efficiency.</i></font><br>
<br>
<font color="#0000ed"><i>// The following auxilliary macros are also defined</i></font><br>
<br>
<font color="#1773cc">#define NTL_FRADIX (...) </font><font color="#0000ed"><i>// double-precision value of 2^NTL_ZZ_NBITS</i></font><br>
<br>
<font color="#1773cc">#define NTL_SP_BOUND (</font><font color="#ff8b00">1L</font><font color="#1773cc"> << NTL_SP_NBITS)</font><br>
<font color="#1773cc">#define NTL_WSP_BOUND (</font><font color="#ff8b00">1L</font><font color="#1773cc"> << NTL_WSP_NBITS)</font><br>
<br>
<br>
<font color="#0000ed"><i>// Backward compatability notes:</i></font><br>
<font color="#0000ed"><i>//</i></font><br>
<font color="#0000ed"><i>// Prior to version 5.0, the macro NTL_NBITS was defined,</i></font><br>
<font color="#0000ed"><i>// along with the macro NTL_RADIX defined to be (1L << NTL_NBITS).</i></font><br>
<font color="#0000ed"><i>// While these macros are still available when using NTL's traditional </i></font><br>
<font color="#0000ed"><i>// long integer package (i.e., when NTL_GMP_LIP is not set), </i></font><br>
<font color="#0000ed"><i>// they are not available when using the GMP as the primary long integer </i></font><br>
<font color="#0000ed"><i>// package (i.e., when NTL_GMP_LIP is set).</i></font><br>
<font color="#0000ed"><i>// Furthermore, when writing portable programs, one should avoid these macros.</i></font><br>
<font color="#0000ed"><i>// Note that when using traditional long integer arithmetic, we have</i></font><br>
<font color="#0000ed"><i>// NTL_ZZ_NBITS = NTL_SP_NBITS = NTL_WSP_NBITS = NTL_NBITS.</i></font><br>
<font color="#0000ed"><i>//</i></font><br>
<font color="#0000ed"><i>// Prior to version 9.0, one could also assume that </i></font><br>
<font color="#0000ed"><i>// NTL_SP_NBITS <= NTL_DOUBLE_PRECISION-3;</i></font><br>
<font color="#0000ed"><i>// however, this is no longer the case (unless NTL is build with he NTL_LEGACY_SP_MULMOD</i></font><br>
<font color="#0000ed"><i>// flag turned on).</i></font><br>
<br>
<br>
<font color="#0000ed"><i>// Here are some additional functions.</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> clear(ZZ& x); <font color="#0000ed"><i>// x = 0</i></font><br>
<font color="#008b00"><b>void</b></font> set(ZZ& x); <font color="#0000ed"><i>// x = 1</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> swap(ZZ& x, ZZ& y);<br>
<font color="#0000ed"><i>// swap x and y (done by "pointer swapping", if possible).</i></font><br>
<br>
<font color="#008b00"><b>double</b></font> log(<font color="#008b00"><b>const</b></font> ZZ& a);<br>
<font color="#0000ed"><i>// returns double precision approximation to log(a)</i></font><br>
<br>
<font color="#008b00"><b>long</b></font> NextPowerOfTwo(<font color="#008b00"><b>long</b></font> m);<br>
<font color="#0000ed"><i>// returns least nonnegative k such that 2^k >= m</i></font><br>
<br>
<font color="#008b00"><b>long</b></font> ZZ::size() <font color="#008b00"><b>const</b></font>;<br>
<font color="#0000ed"><i>// a.size() returns the number of zzigits of |a|; the</i></font><br>
<font color="#0000ed"><i>// size of 0 is 0.</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> ZZ::SetSize(<font color="#008b00"><b>long</b></font> k)<br>
<font color="#0000ed"><i>// a.SetSize(k) does not change the value of a, but simply pre-allocates</i></font><br>
<font color="#0000ed"><i>// space for k zzigits.</i></font><br>
<br>
<font color="#008b00"><b>long</b></font> ZZ::SinglePrecision() <font color="#008b00"><b>const</b></font>;<br>
<font color="#0000ed"><i>// a.SinglePrecision() is a predicate that tests if abs(a) < NTL_SP_BOUND</i></font><br>
<br>
<font color="#008b00"><b>long</b></font> ZZ::WideSinglePrecision() <font color="#008b00"><b>const</b></font>;<br>
<font color="#0000ed"><i>// a.WideSinglePrecision() is a predicate that tests if abs(a) < NTL_WSP_BOUND</i></font><br>
<br>
<font color="#008b00"><b>long</b></font> digit(<font color="#008b00"><b>const</b></font> ZZ& a, <font color="#008b00"><b>long</b></font> k);<br>
<font color="#0000ed"><i>// returns k-th zzigit of |a|, position 0 being the low-order</i></font><br>
<font color="#0000ed"><i>// zzigit.</i></font><br>
<font color="#0000ed"><i>// NOTE: this routine is only available when using NTL's traditional</i></font><br>
<font color="#0000ed"><i>// long integer arithmetic, and should not be used in programs</i></font><br>
<font color="#0000ed"><i>// that are meant to be portable.</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> ZZ::kill();<br>
<font color="#0000ed"><i>// a.kill() sets a to zero and frees the space held by a.</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> ZZ::swap(ZZ& x);<br>
<font color="#0000ed"><i>// swap method (done by "pointer swapping" if possible)</i></font><br>
<br>
ZZ::ZZ(INIT_SIZE_TYPE, <font color="#008b00"><b>long</b></font> k);<br>
<font color="#0000ed"><i>// ZZ(INIT_SIZE, k) initializes to 0, but space is pre-allocated so</i></font><br>
<font color="#0000ed"><i>// that numbers x with x.size() <= k can be stored without</i></font><br>
<font color="#0000ed"><i>// re-allocation.</i></font><br>
<br>
<font color="#008b00"><b>static</b></font> <font color="#008b00"><b>const</b></font> ZZ& ZZ::zero();<br>
<font color="#0000ed"><i>// ZZ::zero() yields a read-only reference to zero, if you need it.</i></font><br>
<br>
<br>
<br>
<br>
<font color="#0000ed"><i>/*</i></font><font color="#0000ed"><i>*************************************************************************\</i></font><br>
<br>
<font color="#0000ed"><i> Small Prime Generation</i></font><br>
<br>
<font color="#0000ed"><i>primes are generated in sequence, starting at 2, and up to a maximum</i></font><br>
<font color="#0000ed"><i>that is no more than min(NTL_SP_BOUND, 2^30).</i></font><br>
<br>
<font color="#0000ed"><i>Example: print the primes up to 1000</i></font><br>
<br>
<font color="#0000ed"><i>#include <NTL/ZZ.h></i></font><br>
<br>
<font color="#0000ed"><i>main()</i></font><br>
<font color="#0000ed"><i>{</i></font><br>
<font color="#0000ed"><i> PrimeSeq s;</i></font><br>
<font color="#0000ed"><i> long p;</i></font><br>
<br>
<font color="#0000ed"><i> p = s.next();</i></font><br>
<font color="#0000ed"><i> while (p <= 1000) {</i></font><br>
<font color="#0000ed"><i> cout << p << "\n";</i></font><br>
<font color="#0000ed"><i> p = s.next();</i></font><br>
<font color="#0000ed"><i> }</i></font><br>
<font color="#0000ed"><i>}</i></font><br>
<br>
<font color="#0000ed"><i>\*************************************************************************</i></font><font color="#0000ed"><i>*/</i></font><br>
<br>
<br>
<br>
<font color="#008b00"><b>class</b></font> PrimeSeq {<br>
<font color="#b02f60"><b>public</b></font>:<br>
PrimeSeq();<br>
~PrimeSeq();<br>
<br>
<font color="#008b00"><b>long</b></font> next();<br>
<font color="#0000ed"><i>// returns next prime in the sequence. returns 0 if list of small</i></font><br>
<font color="#0000ed"><i>// primes is exhausted.</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> reset(<font color="#008b00"><b>long</b></font> b);<br>
<font color="#0000ed"><i>// resets generator so that the next prime in the sequence is the</i></font><br>
<font color="#0000ed"><i>// smallest prime >= b.</i></font><br>
<br>
<font color="#b02f60"><b>private</b></font>:<br>
PrimeSeq(<font color="#008b00"><b>const</b></font> PrimeSeq&); <font color="#0000ed"><i>// disabled</i></font><br>
<font color="#008b00"><b>void</b></font> <font color="#b02f60"><b>operator</b></font>=(<font color="#008b00"><b>const</b></font> PrimeSeq&); <font color="#0000ed"><i>// disabled</i></font><br>
<br>
};<br>
<br>
<br>
</font></body>
</html>
|