/usr/share/doc/libntl-dev/NTL/tour-ex1.html is in libntl-dev 6.2.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 | <html>
<head>
<title>
A Tour of NTL: Examples: Big Integers </title>
</head>
<center>
<img src="arrow1.gif" alt="[Previous]" align=bottom>
<a href="tour-examples.html"><img src="arrow2.gif" alt="[Up]" align=bottom></a>
<a href="tour-ex2.html"> <img src="arrow3.gif" alt="[Next]" align=bottom></a>
</center>
<h1>
<p align=center>
A Tour of NTL: Examples: Big Integers
</p>
</h1>
<p> <hr> <p>
The first example makes use of the class
<tt>ZZ</tt>,
which
represents "big integers": signed, arbitrary length integers.
This program reads two big integers <tt>a</tt> and <tt>b</tt>,
and prints <tt>(a+1)*(b+1)</tt>.
<!-- STARTPLAIN
#include <NTL/ZZ.h>
using namespace std;
using namespace NTL;
int main()
{
ZZ a, b, c;
cin >> a;
cin >> b;
c = (a+1)*(b+1);
cout << c << "\n";
}
ENDPLAIN -->
<!-- STARTPRETTY {{{ -->
<p><p><table cellPadding=10px><tr><td><font color="#000000">
<font face="monospace">
<font color="#1874cd">#include </font><font color="#4a708b"><NTL/ZZ.h></font><br>
<br>
using namespace std;<br>
using namespace NTL;<br>
<br>
<font color="#008b00"><b>int</b></font> main()<br>
{<br>
ZZ a, b, c; <br>
<br>
cin >> a; <br>
cin >> b; <br>
c = (a+<font color="#ff8c00">1</font>)*(b+<font color="#ff8c00">1</font>);<br>
cout << c << <font color="#4a708b">"</font><font color="#8a2be2">\n</font><font color="#4a708b">"</font>;<br>
}<br>
</font>
</font></td></tr></table><p><p>
<!-- }}} ENDPRETTY -->
This program declares three variables <tt>a</tt>, <tt>b</tt>,
and <tt>c</tt> of type <tt>ZZ</tt>.
The values <tt>a</tt> and <tt>b</tt> are read from standard input.
The value <tt>c</tt> is then computed as <tt>(a+1)*(b+1)</tt>.
Finally, the value of <tt>c</tt> is printed to the standard output.
<p>
Note that one can compute with <tt>ZZ</tt>s much as with ordinary
<tt>int</tt>s, in that most of the standard arithmetic and
assignment operators can be used in a direct and natural way.
The <tt>C++</tt> compiler and the NTL library routines
automatically take care
of all the bookkeeping involved
with memory management and temporary objects.
<p>
Note that by default, all of NTL's components
are in the namespace <tt>NTL</tt>;
with the "using directive"
<pre>
using namespace NTL;
</pre>
in the above example, one can access
these components directly.
More details on namespaces and NTL <a href="tour-stdcxx.html">here</a>.
<p> <hr> <p>
Here's a program that reads a list of integers from standard
input and prints the sum of their squares.
<!-- STARTPLAIN
#include <NTL/ZZ.h>
using namespace std;
using namespace NTL;
int main()
{
ZZ acc, val;
acc = 0;
while (SkipWhiteSpace(cin)) {
cin >> val;
acc += val*val;
}
cout << acc << "\n";
}
ENDPLAIN -->
<!-- STARTPRETTY {{{ -->
<p><p><table cellPadding=10px><tr><td><font color="#000000">
<font face="monospace">
<font color="#1874cd">#include </font><font color="#4a708b"><NTL/ZZ.h></font><br>
<br>
<br>
using namespace std;<br>
using namespace NTL;<br>
<br>
<br>
<font color="#008b00"><b>int</b></font> main()<br>
{<br>
ZZ acc, val;<br>
<br>
acc = <font color="#ff8c00">0</font>;<br>
<font color="#b03060"><b>while</b></font> (SkipWhiteSpace(cin)) {<br>
cin >> val;<br>
acc += val*val;<br>
}<br>
<br>
cout << acc << <font color="#4a708b">"</font><font color="#8a2be2">\n</font><font color="#4a708b">"</font>;<br>
}<br>
</font>
</font></td></tr></table><p><p>
<!-- }}} ENDPRETTY -->
The function <tt>SkipWhiteSpace</tt> is defined by NTL.
It skips over white space, and returns 1 if there is something
following it.
This function is useful, because
NTL's input operators raise an error if an input
is missing or ill-formed.
This is different from the standard I/O library,
which does not raise an error.
Personally, I find that not raising an error, or at least
an exception, is a bad idea, since the caller of the I/O
routine must constantly check the status of the input
stream.
<p>
<hr>
<p>
Here's a simple modular exponentiation routine for computing
<tt>a^e mod n</tt>.
NTL already provides a more sophisticated one, though.
<!-- STARTPLAIN
ZZ PowerMod(const ZZ& a, const ZZ& e, const ZZ& n)
{
if (e == 0) return ZZ(1);
long k = NumBits(e);
ZZ res;
res = 1;
for (long i = k-1; i >= 0; i~~) {
res = (res*res) % n;
if (bit(e, i) == 1) res = (res*a) % n;
}
if (e < 0)
return InvMod(res, n);
else
return res;
}
ENDPLAIN -->
<!-- STARTPRETTY {{{ -->
<p><p><table cellPadding=10px><tr><td><font color="#000000">
<font face="monospace">
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="#b03060"><b>if</b></font> (e == <font color="#ff8c00">0</font>) <font color="#b03060"><b>return</b></font> ZZ(<font color="#ff8c00">1</font>);<br>
<br>
<font color="#008b00"><b>long</b></font> k = NumBits(e);<br>
<br>
ZZ res;<br>
res = <font color="#ff8c00">1</font>;<br>
<br>
<font color="#b03060"><b>for</b></font> (<font color="#008b00"><b>long</b></font> i = k-<font color="#ff8c00">1</font>; i >= <font color="#ff8c00">0</font>; i--) {<br>
res = (res*res) % n;<br>
<font color="#b03060"><b>if</b></font> (bit(e, i) == <font color="#ff8c00">1</font>) res = (res*a) % n;<br>
}<br>
<br>
<font color="#b03060"><b>if</b></font> (e < <font color="#ff8c00">0</font>)<br>
<font color="#b03060"><b>return</b></font> InvMod(res, n);<br>
<font color="#b03060"><b>else</b></font><br>
<font color="#b03060"><b>return</b></font> res;<br>
}<br>
</font>
</font></td></tr></table><p><p>
<!-- }}} ENDPRETTY -->
Note that as an alternative, we could implement the inner loop
as follows:
<!-- STARTPLAIN
res = SqrMod(res, n);
if (bit(e, i) == 1) res = MulMod(res, a, n);
ENDPLAIN -->
<!-- STARTPRETTY {{{ -->
<p><p><table cellPadding=10px><tr><td><font color="#000000">
<font face="monospace">
res = SqrMod(res, n);<br>
<font color="#b03060"><b>if</b></font> (bit(e, i) == <font color="#ff8c00">1</font>) res = MulMod(res, a, n);<br>
</font>
</font></td></tr></table><p><p>
<!-- }}} ENDPRETTY -->
We could also write this as:
<!-- STARTPLAIN
SqrMod(res, res, n);
if (bit(e, i) == 1) MulMod(res, res, a, n);
ENDPLAIN -->
<!-- STARTPRETTY {{{ -->
<p><p><table cellPadding=10px><tr><td><font color="#000000">
<font face="monospace">
SqrMod(res, res, n);<br>
<font color="#b03060"><b>if</b></font> (bit(e, i) == <font color="#ff8c00">1</font>) MulMod(res, res, a, n);<br>
</font>
</font></td></tr></table><p><p>
<!-- }}} ENDPRETTY -->
This illustrates an important point about NTL's programming interface.
For every function in NTL, there is a procedural version that
stores its result in its first argument.
The reason for using the procedural variant is efficieny:
on every iteration through the above loop, the functional form
of <tt>SqrMod</tt> will cause a temporary <tt>ZZ</tt> object to
be created and destroyed, whereas the procedural version
will not create any temporaries.
Where performance is critical, the procedural version
is to be preferred.
Although it is usually silly to get too worked up about performance,
it may be reasonable to argue that modular exponentiation
is an important enough routine that it should be as fast as possible.
<p>
Note that when the functional version of a function
can be naturally named with an operator, this is done.
So for example, NTL provides a 3-argument <tt>mul</tt> routine
for <tt>ZZ</tt> multiplication, and a functional version
whose name is <tt>operator *</tt>, and not <tt>mul</tt>.
<p>
While we are taking about temporaries, consider the first version
of the inner loop.
Execution of the statement
<!-- STARTPLAIN
res = (res*res) % n;
ENDPLAIN -->
<!-- STARTPRETTY {{{ -->
<p><p><table cellPadding=10px><tr><td><font color="#000000">
<font face="monospace">
res = (res*res) % n;<br>
</font>
</font></td></tr></table><p><p>
<!-- }}} ENDPRETTY -->
will result in the creation of two temporary objects,
one for the product, and one for the result of the mod operation,
whose value is copied into <tt>res</tt>.
Of course, the compiler automatically generates the code for
cleaning up temporaries and other local objects at the right time.
The programmer does not have to worry about this.
<p> <hr> <p>
This example is a bit more interesting.
The following program prompts the user for an input,
and applies a simple probabilistic primality test.
Note that NTL already provides a slightly more sophisticated
primality test.
<!-- STARTPLAIN
#include <NTL/ZZ.h>
using namespace std;
using namespace NTL;
long witness(const ZZ& n, const ZZ& x)
{
ZZ m, y, z;
long j, k;
if (x == 0) return 0;
// compute m, k such that n-1 = 2^k * m, m odd:
k = 1;
m = n/2;
while (m % 2 == 0) {
k++;
m /= 2;
}
z = PowerMod(x, m, n); // z = x^m % n
if (z == 1) return 0;
j = 0;
do {
y = z;
z = (y*y) % n;
j++;
} while (j < k && z != 1);
return z != 1 || y != n-1;
}
long PrimeTest(const ZZ& n, long t)
{
if (n <= 1) return 0;
// first, perform trial division by primes up to 2000
PrimeSeq s; // a class for quickly generating primes in sequence
long p;
p = s.next(); // first prime is always 2
while (p && p < 2000) {
if ((n % p) == 0) return (n == p);
p = s.next();
}
// second, perform t Miller-Rabin tests
ZZ x;
long i;
for (i = 0; i < t; i++) {
x = RandomBnd(n); // random number between 0 and n-1
if (witness(n, x))
return 0;
}
return 1;
}
int main()
{
ZZ n;
cout << "n: ";
cin >> n;
if (PrimeTest(n, 10))
cout << n << " is probably prime\n";
else
cout << n << " is composite\n";
}
ENDPLAIN -->
<!-- STARTPRETTY {{{ -->
<p><p><table cellPadding=10px><tr><td><font color="#000000">
<font face="monospace">
<font color="#1874cd">#include </font><font color="#4a708b"><NTL/ZZ.h></font><br>
<br>
<font color="#b03060"><b>using</b></font> <font color="#008b00"><b>namespace</b></font> std;<br>
<font color="#b03060"><b>using</b></font> <font color="#008b00"><b>namespace</b></font> NTL;<br>
<br>
<font color="#008b00"><b>long</b></font> witness(<font color="#008b00"><b>const</b></font> ZZ& n, <font color="#008b00"><b>const</b></font> ZZ& x)<br>
{<br>
ZZ m, y, z;<br>
<font color="#008b00"><b>long</b></font> j, k;<br>
<br>
<font color="#b03060"><b>if</b></font> (x == <font color="#ff8c00">0</font>) <font color="#b03060"><b>return</b></font> <font color="#ff8c00">0</font>;<br>
<br>
<font color="#0000ee"><i>// compute m, k such that n-1 = 2^k * m, m odd:</i></font><br>
<br>
k = <font color="#ff8c00">1</font>;<br>
m = n/<font color="#ff8c00">2</font>;<br>
<font color="#b03060"><b>while</b></font> (m % <font color="#ff8c00">2</font> == <font color="#ff8c00">0</font>) {<br>
k++;<br>
m /= <font color="#ff8c00">2</font>;<br>
}<br>
<br>
z = PowerMod(x, m, n); <font color="#0000ee"><i>// z = x^m % n</i></font><br>
<font color="#b03060"><b>if</b></font> (z == <font color="#ff8c00">1</font>) <font color="#b03060"><b>return</b></font> <font color="#ff8c00">0</font>;<br>
<br>
j = <font color="#ff8c00">0</font>;<br>
<font color="#b03060"><b>do</b></font> {<br>
y = z;<br>
z = (y*y) % n; <br>
j++;<br>
} <font color="#b03060"><b>while</b></font> (j < k && z != <font color="#ff8c00">1</font>);<br>
<br>
<font color="#b03060"><b>return</b></font> z != <font color="#ff8c00">1</font> || y != n-<font color="#ff8c00">1</font>;<br>
}<br>
<br>
<br>
<font color="#008b00"><b>long</b></font> PrimeTest(<font color="#008b00"><b>const</b></font> ZZ& n, <font color="#008b00"><b>long</b></font> t)<br>
{<br>
<font color="#b03060"><b>if</b></font> (n <= <font color="#ff8c00">1</font>) <font color="#b03060"><b>return</b></font> <font color="#ff8c00">0</font>;<br>
<br>
<font color="#0000ee"><i>// first, perform trial division by primes up to 2000</i></font><br>
<br>
PrimeSeq s; <font color="#0000ee"><i>// a class for quickly generating primes in sequence</i></font><br>
<font color="#008b00"><b>long</b></font> p;<br>
<br>
p = s.next(); <font color="#0000ee"><i>// first prime is always 2</i></font><br>
<font color="#b03060"><b>while</b></font> (p && p < <font color="#ff8c00">2000</font>) {<br>
<font color="#b03060"><b>if</b></font> ((n % p) == <font color="#ff8c00">0</font>) <font color="#b03060"><b>return</b></font> (n == p);<br>
p = s.next(); <br>
}<br>
<br>
<font color="#0000ee"><i>// second, perform t Miller-Rabin tests</i></font><br>
<br>
ZZ x;<br>
<font color="#008b00"><b>long</b></font> i;<br>
<br>
<font color="#b03060"><b>for</b></font> (i = <font color="#ff8c00">0</font>; i < t; i++) {<br>
x = RandomBnd(n); <font color="#0000ee"><i>// random number between 0 and n-1</i></font><br>
<br>
<font color="#b03060"><b>if</b></font> (witness(n, x)) <br>
<font color="#b03060"><b>return</b></font> <font color="#ff8c00">0</font>;<br>
}<br>
<br>
<font color="#b03060"><b>return</b></font> <font color="#ff8c00">1</font>;<br>
}<br>
<br>
<font color="#008b00"><b>int</b></font> main()<br>
{<br>
ZZ n;<br>
<br>
cout << <font color="#4a708b">"n: "</font>;<br>
cin >> n;<br>
<br>
<font color="#b03060"><b>if</b></font> (PrimeTest(n, <font color="#ff8c00">10</font>))<br>
cout << n << <font color="#4a708b">" is probably prime</font><font color="#8a2be2">\n</font><font color="#4a708b">"</font>;<br>
<font color="#b03060"><b>else</b></font><br>
cout << n << <font color="#4a708b">" is composite</font><font color="#8a2be2">\n</font><font color="#4a708b">"</font>;<br>
}<br>
</font>
</font></td></tr></table><p><p>
<!-- }}} ENDPRETTY -->
Note that in NTL, there are typically a number of ways to
compute the same thing.
For example, consider the computation of <tt>m</tt> and <tt>k</tt>
in function <tt>witness</tt>.
We could have written it thusly:
<!-- STARTPLAIN
k = 1;
m = n >> 1;
while (!IsOdd(m)) {
k++;
m >>= 1;
}
ENDPLAIN -->
<!-- STARTPRETTY {{{ -->
<p><p><table cellPadding=10px><tr><td><font color="#000000">
<font face="monospace">
k = <font color="#ff8c00">1</font>;<br>
m = n >> <font color="#ff8c00">1</font>;<br>
<font color="#b03060"><b>while</b></font> (!IsOdd(m)) {<br>
k++;<br>
m >>= <font color="#ff8c00">1</font>;<br>
}<br>
</font>
</font></td></tr></table><p><p>
<!-- }}} ENDPRETTY -->
It turns out that this is actually not significantly more
efficient than the original version, because the implementation
optimizes multiplication and division by 2.
<p>
The following is more efficient:
<!-- STARTPLAIN
k = 1;
while (bit(n, k) == 0) k++;
m = n >> k;
ENDPLAIN -->
<!-- STARTPRETTY {{{ -->
<p><p><table cellPadding=10px><tr><td><font color="#000000">
<font face="monospace">
k = <font color="#ff8c00">1</font>;<br>
<font color="#b03060"><b>while</b></font> (bit(n, k) == <font color="#ff8c00">0</font>) k++;<br>
m = n >> k;<br>
</font>
</font></td></tr></table><p><p>
<!-- }}} ENDPRETTY -->
As it happens, there is a built-in NTL routine that does just what we want:
<!-- STARTPLAIN
m = n-1;
k = MakeOdd(m);
ENDPLAIN -->
<!-- STARTPRETTY {{{ -->
<p><p><table cellPadding=10px><tr><td><font color="#000000">
<font face="monospace">
m = n-<font color="#ff8c00">1</font>;<br>
k = MakeOdd(m);<br>
</font>
</font></td></tr></table><p><p>
<!-- }}} ENDPRETTY -->
<p> <hr> <p>
Having seen a number of examples involving <tt>ZZ</tt>s,
let's look at the <tt>ZZ</tt> interface in a bit more detail.
<p>
<b>
Constructors, assignment, and conversions
</b>
<p>
When you declare an object of type <tt>ZZ</tt>,
the default constructor initializes to the value <tt>0</tt>.
As we have already seen, there is an assignment operator that
allows one to copy the value of one <tt>ZZ</tt> to another.
Note that these copies (like almost all copies in NTL) are "deep",
i.e., the actual data is copied, and not just a pointer.
Of course, if the amount of space allocated by the destination
of the assignment is insufficient to hold the value of the source,
space is automatically re-allocated.
<p>
One can also assign a value of type <tt>long</tt> to a <tt>ZZ</tt>:
<!-- STARTPLAIN
ZZ x;
x = 1;
ENDPLAIN -->
<!-- STARTPRETTY {{{ -->
<p><p><table cellPadding=10px><tr><td><font color="#000000">
<font face="monospace">
ZZ x;<br>
x = <font color="#ff8c00">1</font>;<br>
</font>
</font></td></tr></table><p><p>
<!-- }}} ENDPRETTY -->
<p>
Note that one cannot write
<!-- STARTPLAIN
ZZ x = 1; // error
ENDPLAIN -->
<!-- STARTPRETTY {{{ -->
<p><p><table cellPadding=10px><tr><td><font color="#000000">
<font face="monospace">
ZZ x = <font color="#ff8c00">1</font>; <font color="#0000ee"><i>// error</i></font><br>
</font>
</font></td></tr></table><p><p>
<!-- }}} ENDPRETTY -->
to initialize a <tt>ZZ</tt>.
Instead, one could write
<!-- STARTPLAIN
ZZ x = ZZ(1);
ZZ y(1);
ZZ z{1}; // C++11 only
ENDPLAIN -->
<!-- STARTPRETTY {{{ -->
<p><p><table cellPadding=10px><tr><td><font color="#000000">
<font face="monospace">
ZZ x = ZZ(<font color="#ff8c00">1</font>);<br>
ZZ y(<font color="#ff8c00">1</font>);<br>
ZZ z{<font color="#ff8c00">1</font>}; <font color="#0000ee"><i>// C++11 only</i></font><br>
</font>
</font></td></tr></table><p><p>
<!-- }}} ENDPRETTY -->
Using the comstructor that allows one to <i>explicitly</i>
construct a <tt>ZZ</tt> from a <tt>long</tt>.
<p>
Alternatively, one could write this as:
<!-- STARTPLAIN
ZZ x = conv<ZZ>(1);
ENDPLAIN -->
<!-- STARTPRETTY {{{ -->
<p><p><table cellPadding=10px><tr><td><font color="#000000">
<font face="monospace">
ZZ x = conv<ZZ>(<font color="#ff8c00">1</font>);<br>
</font>
</font></td></tr></table><p><p>
<!-- }}} ENDPRETTY -->
This is an example of one of NTL's conversion routines.
For very large constants, one can write:
<!-- STARTPLAIN
ZZ x = conv<ZZ>("99999999999999999999");
ENDPLAIN -->
<!-- STARTPRETTY {{{ -->
<p><p><table cellPadding=10px><tr><td><font color="#000000">
<font face="monospace">
ZZ x = conv<ZZ>(<font color="#4a708b">"99999999999999999999"</font>);<br>
</font>
</font></td></tr></table><p><p>
<!-- }}} ENDPRETTY -->
These examples illustrate conversion rountines in their
functional forms.
<!-- STARTPLAIN
ZZ x;
conv(x, 1);
conv(x, "99999999999999999999");
ENDPLAIN -->
<!-- STARTPRETTY {{{ -->
<p><p><table cellPadding=10px><tr><td><font color="#000000">
<font face="monospace">
ZZ x;<br>
conv(x, <font color="#ff8c00">1</font>);<br>
conv(x, <font color="#4a708b">"99999999999999999999"</font>);<br>
</font>
</font></td></tr></table><p><p>
<!-- }}} ENDPRETTY -->
<p>
<b>
Functionality
</b>
<p>
All of the basic arithmetic operators are supported,
including comparison, arithmetic, shift, and bit-wise logical operations.
One can mix <tt>ZZ</tt>s and <tt>long</tt>s in any expresion in
a natural way.
NTL does not support implicit type conversion;
rather, for basic operations, it simply overloads the operators
or functions in a way to achieve a kind of "promotion logic":
if one input is a <tt>ZZ</tt> and the other is a <tt>long</tt>
(or something that implicitly converts to a <tt>long</tt>, like
an <tt>int</tt>), the <tt>long</tt> input is effectively converted
to a <tt>ZZ</tt>.
Moreover, wherever possible, the implementation does this
as efficiently as possible, and usually avoids the creation
of a temporary <tt>ZZ</tt>.
<p>
There are also procedural versions for all the basic arithmetic
operations:
<pre>
add, sub, negate, mul, sqr, div, rem, DivRem,
LeftShift, RightShift,
bit_and, bit_or, bit_xor
</pre>
<p>
There are many other routines.
Here is a brief summary:
<ul>
<li>
<tt>GCD</tt> -- computes greatest common divisor of two integers
<li>
<tt>XGCD</tt> -- extended Euclidean algorithm
<li>
<tt>AddMod</tt>, <tt>SubMod</tt>, <tt>NegateMod</tt>,
<tt>MulMod</tt>, <tt>SqrMod</tt>, <tt>InvMod</tt>,
<tt>PowerMod</tt> -- routines for modular arithmetic,
including inversion and exponentiation
<li>
<tt>NumBits</tt> -- length of binary representation
<li>
<tt>bit</tt> -- extract a bit
<li>
<tt>ZZFromBytes</tt>, <tt>BytesFromZZ</tt> --
convert between octet strings and <tt>ZZ</tt>s
<li>
<tt>RandomBnd</tt>, <tt>RandomBits</tt>, <tt>RandomLen</tt> --
routines for generating pseudo-random numbers
<li>
<tt>GenPrime</tt>, <tt>ProbPrime</tt> -- routines for generating primes
and testing primality
<li>
<tt>power</tt> -- (non-modular) exponentiation
<li>
<tt>SqrRoot</tt> -- integer part of square root
<li>
<tt>Jacobi</tt>, <tt>SqrRootMod</tt> -- Jacobi symbol and modular
square root
</ul>
<p>
Most of these functions also have pure <tt>long</tt> versions as
well, and as usual, there are both functional and procedural
variants.
<p>
There are other functions as well.
See <a href="ZZ.cpp.html"><tt>ZZ.txt</tt></a> for complete details.
Also see <a href="tools.cpp.html"><tt>tools.txt</tt></a> for some basic
services provided by NTL.
<p>
<center>
<img src="arrow1.gif" alt="[Previous]" align=bottom>
<a href="tour-examples.html"><img src="arrow2.gif" alt="[Up]" align=bottom></a>
<a href="tour-ex2.html"> <img src="arrow3.gif" alt="[Next]" align=bottom></a>
</center>
</body>
</html>
|