/usr/include/zn_poly/zn_poly.h is in libzn-poly-dev 0.9-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 | /*
zn_poly.h: main header file to be #included by zn_poly users
Copyright (C) 2007, 2008, David Harvey
This file is part of the zn_poly library (version 0.9).
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ZN_POLY_H
#define ZN_POLY_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdlib.h>
#include <limits.h>
#include <stdint.h>
#include <assert.h>
#define ZNP_ASSERT assert
#define ZNP_INLINE static inline
/*
Returns a string like "3.1"
*/
extern const char*
zn_poly_version_string ();
/*
Three components of "version x.y.z"
*/
#define ZNP_VERSION_MAJOR 0
#define ZNP_VERSION_MINOR 9
#define ZNP_VERSION_REVISION 0
/*
ULONG_BITS = number of bits per unsigned long
*/
#if ULONG_MAX == 4294967295U
#define ULONG_BITS 32
#elif ULONG_MAX == 18446744073709551615U
#define ULONG_BITS 64
#else
#error zn_poly requires that unsigned long is either 32 bits or 64 bits
#endif
/*
I get really sick of typing unsigned long.
*/
#ifdef ulong
#undef ulong
#endif
typedef unsigned long ulong;
#include "wide_arith.h"
/* ============================================================================
zn_mod_t stuff
============================================================================ */
/*
zn_mod_t stores precomputed information about a modulus.
The modulus can be any integer in the range 2 <= m < 2^ULONG_BITS.
A modulus m is called "slim" if m < 2^(ULONG_BITS - 1), i.e. the residues
never occupy the top bit of the word. Many routines are much faster for
slim moduli.
*/
typedef struct
{
// the modulus, must be >= 2
ulong m;
// ceil(log2(m)) = number of bits in a non-negative residue
int bits;
// reduction of B and B^2 mod m (where B = 2^ULONG_BITS)
ulong B, B2;
// sh1 and inv1 are respectively ell-1 and m' from Figure 4.1 of [GM94]
unsigned sh1;
ulong inv1;
// sh2, sh3, inv2 and m_norm are respectively N-ell, ell-1, m', d_norm
// from Figure 8.1 of [GM94]
unsigned sh2, sh3;
ulong inv2, m_norm;
// inv3 = n^(-1) mod B (only valid if m is odd)
ulong inv3;
}
zn_mod_struct;
typedef zn_mod_struct zn_mod_t[1];
/*
Initialises zn_mod_t with given modulus, performs some (fairly cheap)
precomputations.
*/
void
zn_mod_init (zn_mod_t mod, ulong m);
/*
Must be called when the modulus object goes out of scope.
*/
void
zn_mod_clear (zn_mod_t mod);
/*
Returns the modulus.
*/
ZNP_INLINE ulong
zn_mod_get (const zn_mod_t mod)
{
return mod->m;
}
/*
Return nonzero if mod is a slim modulus.
*/
ZNP_INLINE int
zn_mod_is_slim (const zn_mod_t mod)
{
return (long) mod->m >= 0;
}
/*
Returns x + y mod m.
Both x and y must be in [0, m).
*/
ZNP_INLINE ulong
zn_mod_add (ulong x, ulong y, const zn_mod_t mod)
{
ZNP_ASSERT (x < mod->m && y < mod->m);
ulong temp = mod->m - y;
if (x < temp)
return x + y;
else
return x - temp;
}
/*
Same as zn_mod_add, but only for slim moduli.
This is usually several times faster than zn_mod_add, depending on the
context; see the array-profile target for examples.
*/
ZNP_INLINE ulong
zn_mod_add_slim (ulong x, ulong y, const zn_mod_t mod)
{
ZNP_ASSERT (zn_mod_is_slim (mod));
ZNP_ASSERT (x < mod->m && y < mod->m);
ulong temp = x + y;
if (temp >= mod->m)
temp -= mod->m;
return temp;
}
/*
Returns x - y mod m.
Both x and y must be in [0, m).
*/
ZNP_INLINE ulong
zn_mod_sub (ulong x, ulong y, const zn_mod_t mod)
{
ZNP_ASSERT (x < mod->m && y < mod->m);
ulong temp = x - y;
if (x < y)
temp += mod->m;
return temp;
}
/*
Same as zn_mod_sub, but only for slim moduli.
*/
ZNP_INLINE ulong
zn_mod_sub_slim (ulong x, ulong y, const zn_mod_t mod)
{
ZNP_ASSERT (zn_mod_is_slim (mod));
ZNP_ASSERT (x < mod->m && y < mod->m);
long temp = x - y;
temp += (temp < 0) ? mod->m : 0;
return temp;
}
/*
Returns -x mod m.
x must be in [0, m).
*/
ZNP_INLINE ulong
zn_mod_neg (ulong x, const zn_mod_t mod)
{
ZNP_ASSERT (x < mod->m);
return x ? (mod->m - x) : x;
}
/*
Return x/2 mod m.
x must be in [0, m).
If the modulus is even, x must be even too.
*/
ZNP_INLINE ulong
zn_mod_divby2 (ulong x, const zn_mod_t mod)
{
ZNP_ASSERT (x < mod->m);
ZNP_ASSERT ((mod->m & 1) || !(x & 1));
ulong mask = -(x & 1);
ulong half = (mod->m >> 1) + 1; // = 1/2 mod m if m is odd
return (x >> 1) + (mask & half);
}
/*
Returns floor(x / m).
No restrictions on x.
Algorithm is essentially Figure 4.1 of [GM94].
*/
ZNP_INLINE ulong
zn_mod_quotient (ulong x, const zn_mod_t mod)
{
ulong t;
ZNP_MUL_HI (t, x, mod->inv1);
return (t + ((x - t) >> 1)) >> mod->sh1;
}
/*
Returns x mod m.
No restrictions on x.
*/
ZNP_INLINE ulong
zn_mod_reduce (ulong x, const zn_mod_t mod)
{
return x - mod->m * zn_mod_quotient (x, mod);
}
/*
Returns -x/B mod m.
m must be odd.
No restrictions on x.
*/
ZNP_INLINE ulong
zn_mod_reduce_redc (ulong x, const zn_mod_t mod)
{
ZNP_ASSERT (mod->m & 1);
ulong y = x * mod->inv3;
ulong z;
ZNP_MUL_HI (z, y, mod->m);
return z;
}
/*
Returns x1*B + x0 mod m.
Assumes x1 is already in [0, m).
Algorithm is essentially Figure 8.1 of [GM94].
*/
ZNP_INLINE ulong
zn_mod_reduce_wide (ulong x1, ulong x0, const zn_mod_t mod)
{
ZNP_ASSERT (x1 < mod->m);
ulong y1 = (x1 << mod->sh2) + ((x0 >> 1) >> mod->sh3);
ulong y0 = (x0 << mod->sh2);
ulong sign = y0 >> (ULONG_BITS - 1);
ulong z0 = y0 + (mod->m_norm & -sign);
ulong a1, a0;
ZNP_MUL_WIDE (a1, a0, mod->inv2, y1 + sign);
ZNP_ADD_WIDE (a1, a0, a1, a0, y1, z0);
ulong b1, b0;
ZNP_MUL_WIDE (b1, b0, (-a1 - 1), mod->m);
ZNP_ADD_WIDE (b1, b0, b1, b0, x1, x0);
b1 -= mod->m;
return b0 + (b1 & mod->m);
}
/*
Returns -(x1*B + x0)/B mod m.
Assumes x1 is already in [0, m), and that m is odd.
Uses essentially Montgomery's REDC algorithm [Mon85].
*/
ZNP_INLINE ulong
zn_mod_reduce_wide_redc (ulong x1, ulong x0, const zn_mod_t mod)
{
ZNP_ASSERT (mod->m & 1);
ZNP_ASSERT (x1 < mod->m);
ulong y = x0 * mod->inv3;
ulong z;
ZNP_MUL_HI (z, y, mod->m);
return zn_mod_sub (z, x1, mod);
}
/*
Returns -(x1*B + x0)/B mod m.
Assumes x1 is already in [0, m), and that m is odd, and that m is slim.
Uses essentially Montgomery's REDC algorithm [Mon85].
*/
ZNP_INLINE ulong
zn_mod_reduce_wide_redc_slim (ulong x1, ulong x0, const zn_mod_t mod)
{
ZNP_ASSERT (mod->m & 1);
ZNP_ASSERT (x1 < mod->m);
ulong y = x0 * mod->inv3;
ulong z;
ZNP_MUL_HI (z, y, mod->m);
return zn_mod_sub_slim (z, x1, mod);
}
/*
Returns x1*B + x0 mod m.
No restrictions on x0 and x1.
*/
ZNP_INLINE ulong
zn_mod_reduce2 (ulong x1, ulong x0, const zn_mod_t mod)
{
// first reduce into [0, Bm)
ulong c0, c1;
ZNP_MUL_WIDE (c1, c0, x1, mod->B);
ZNP_ADD_WIDE (c1, c0, c1, c0, 0, x0);
// (must still have c1 < m)
return zn_mod_reduce_wide (c1, c0, mod);
}
/*
Returns -(x1*B + x0)/B mod m.
m must be odd.
No restrictions on x0 and x1.
*/
ZNP_INLINE ulong
zn_mod_reduce2_redc (ulong x1, ulong x0, const zn_mod_t mod)
{
ZNP_ASSERT (mod->m & 1);
// first reduce into [0, Bm)
ulong c0, c1;
ZNP_MUL_WIDE (c1, c0, x1, mod->B);
ZNP_ADD_WIDE (c1, c0, c1, c0, 0, x0);
// (must still have c1 < m)
return zn_mod_reduce_wide_redc (c1, c0, mod);
}
/*
Returns x2*B^2 + x1*B + x0 mod m.
No restrictions on x0, x1 or x2.
*/
ZNP_INLINE ulong
zn_mod_reduce3 (ulong x2, ulong x1, ulong x0, const zn_mod_t mod)
{
// reduce B^2*x2 and B*x1 into [0, Bm)
ulong c0, c1, d0, d1;
ZNP_MUL_WIDE (c1, c0, x2, mod->B2);
ZNP_MUL_WIDE (d1, d0, x1, mod->B);
// add B^2*x2 and B*x1 and x0 mod Bm
ZNP_ADD_WIDE (c1, c0, c1, c0, 0, d0);
// (must still have c1 < m)
ZNP_ADD_WIDE (c1, c0, c1, c0, 0, x0);
if (c1 >= mod->m)
c1 -= mod->m;
c1 = zn_mod_add (c1, d1, mod);
// finally reduce it mod m
return zn_mod_reduce_wide (c1, c0, mod);
}
/*
Returns -(x2*B^2 + x1*B + x0)/B mod m.
m must be odd.
No restrictions on x0, x1 or x2.
*/
ZNP_INLINE ulong
zn_mod_reduce3_redc (ulong x2, ulong x1, ulong x0, const zn_mod_t mod)
{
ZNP_ASSERT (mod->m & 1);
// reduce B^2*x2 and B*x1 into [0, Bm)
ulong c0, c1, d0, d1;
ZNP_MUL_WIDE (c1, c0, x2, mod->B2);
ZNP_MUL_WIDE (d1, d0, x1, mod->B);
// add B^2*x2 and B*x1 and x0 mod Bm
ZNP_ADD_WIDE (c1, c0, c1, c0, 0, d0);
// (must still have c1 < m)
ZNP_ADD_WIDE (c1, c0, c1, c0, 0, x0);
if (c1 >= mod->m)
c1 -= mod->m;
c1 = zn_mod_add (c1, d1, mod);
// finally reduce it mod m
return zn_mod_reduce_wide_redc (c1, c0, mod);
}
/*
Returns x * y mod m.
x and y must be in [0, m).
*/
ZNP_INLINE ulong
zn_mod_mul (ulong x, ulong y, const zn_mod_t mod)
{
ZNP_ASSERT (x < mod->m && y < mod->m);
ulong hi, lo;
ZNP_MUL_WIDE (hi, lo, x, y);
return zn_mod_reduce_wide (hi, lo, mod);
}
/*
Returns -(x * y)/B mod m.
x and y must be in [0, m), and m must be odd.
*/
ZNP_INLINE ulong
zn_mod_mul_redc (ulong x, ulong y, const zn_mod_t mod)
{
ZNP_ASSERT (mod->m & 1);
ZNP_ASSERT (x < mod->m && y < mod->m);
ulong hi, lo;
ZNP_MUL_WIDE (hi, lo, x, y);
return zn_mod_reduce_wide_redc (hi, lo, mod);
}
/*
Returns -(x * y)/B mod m.
x and y must be in [0, m), and m must be odd and slim.
*/
ZNP_INLINE ulong
zn_mod_mul_redc_slim (ulong x, ulong y, const zn_mod_t mod)
{
ZNP_ASSERT (mod->m & 1);
ZNP_ASSERT (zn_mod_is_slim (mod));
ZNP_ASSERT (x < mod->m && y < mod->m);
ulong hi, lo;
ZNP_MUL_WIDE (hi, lo, x, y);
return zn_mod_reduce_wide_redc_slim (hi, lo, mod);
}
/*
Returns x^k mod m.
x must be in [0, m).
Negative indices are not supported (yet).
*/
ulong
zn_mod_pow (ulong x, long k, const zn_mod_t mod);
/*
Returns 1/x mod n, or 0 if x is not invertible mod m.
x must be in [0, m).
*/
ulong
zn_mod_invert (ulong x, const zn_mod_t mod);
/* ============================================================================
scalar multiplication on raw arrays
============================================================================ */
/*
Multiplies each element of op[0, n) by x, stores result at res[0, n).
res and op must be either identical or disjoint buffers.
*/
void
zn_array_scalar_mul (ulong* res, const ulong* op, size_t n, ulong x,
const zn_mod_t mod);
/* ============================================================================
polynomial multiplication on raw arrays
============================================================================ */
/*
Multiplies op1[0, n1) by op2[0, n2), stores result in res[0, n1 + n2 - 1).
op1 and op2 may alias each other, but neither may overlap res.
Must have n1 >= n2 >= 1.
Automatically selects best multiplication algorithm based on modulus
and input lengths. Automatically uses specialised squaring code if inputs
buffers are identical.
*/
void
zn_array_mul (ulong* res,
const ulong* op1, size_t n1,
const ulong* op2, size_t n2,
const zn_mod_t mod);
/*
Middle product of op1[0, n1) and op2[0, n2), stores result in
res[0, n1 - n2 + 1).
(i.e. this is the subarray of the ordinary product op1 * op2 consisting of
those coefficients with indices in the range [n2 - 1, n1).)
op1 and op2 may alias each other, but neither may overlap res.
Must have n1 >= n2 >= 1.
Automatically selects best middle product algorithm based on modulus and
input lengths.
*/
void
zn_array_mulmid (ulong* res,
const ulong* op1, size_t n1,
const ulong* op2, size_t n2,
const zn_mod_t mod);
// forward declaration (see zn_poly_internal.h)
struct ZNP_zn_array_mulmid_fft_precomp1_struct;
/*
Stores precomputed information for performing a middle product where the
first input array op1[0, n1) is invariant, and the *length* of the second
input array op2[0, n2) is invariant.
*/
typedef struct
{
// Determines which middle product algorithm we're using.
// One of the constants:
// ZNP_MULMID_ALGO_FALLBACK: fall back on zn_array_mulmid
// ZNP_MULMID_ALGO_FFT: use zn_array_mulmid_fft_precomp1
int algo;
size_t n1, n2;
const zn_mod_struct* mod;
// stores a copy of op1[0, n1) if we're using ZNP_MULMID_ALGO_FALLBACK
ulong* op1;
// precomputed data if we're using ZNP_MULMID_ALGO_FFT
struct ZNP_zn_array_mulmid_fft_precomp1_struct* precomp_fft;
}
zn_array_mulmid_precomp1_struct;
typedef zn_array_mulmid_precomp1_struct zn_array_mulmid_precomp1_t[1];
/*
Initialises res to perform middle product of op1[0, n1) by operands of
size n2.
*/
void
zn_array_mulmid_precomp1_init (zn_array_mulmid_precomp1_t res,
const ulong* op1, size_t n1, size_t n2,
const zn_mod_t mod);
/*
Performs middle product of op1[0, n1) by op2[0, n2), stores result
at res[0, n1 - n2 + 1).
*/
void
zn_array_mulmid_precomp1_execute (ulong* res, const ulong* op2,
const zn_array_mulmid_precomp1_t precomp);
/*
Deallocates op.
*/
void
zn_array_mulmid_precomp1_clear (zn_array_mulmid_precomp1_t op);
/*
Same as zn_array_mul(), but uses the Schonhage/Nussbaumer FFT algorithm,
with a few layers of naive DFT to save memory.
lgT is the number of layers of DFT. Larger values of lgT save more memory,
as long as lgT doesn't get too close to lg2(sqrt(n1 + n2)). Larger
values also make the function slower. Probably you never want to make lgT
bigger than 4; after that the savings are marginal.
The modulus must be odd.
Output may *not* overlap inputs.
NOTE: this interface is preliminary and may change in future versions.
*/
void
zn_array_mul_fft_dft (ulong* res,
const ulong* op1, size_t n1,
const ulong* op2, size_t n2,
unsigned lgT, const zn_mod_t mod);
/* ============================================================================
polynomial division on raw arrays
============================================================================ */
/*
NOTE: this interface is going to *change* in a future version of zn_poly!
Computes n terms of power series inverse of op[0, n).
Must have n >= 1.
Currently restricted to op[0] == 1.
Output may not overlap input.
*/
void
zn_array_invert (ulong* res, const ulong* op, size_t n, const zn_mod_t mod);
/* ============================================================================
other miscellaneous zn_array stuff
============================================================================ */
/*
res := -op
Inputs and outputs in [0, m).
*/
void
zn_array_neg (ulong* res, const ulong* op, size_t n, const zn_mod_t mod);
/*
res := op1 - op2.
Inputs and outputs in [0, m).
*/
void
zn_array_sub (ulong* res, const ulong* op1, const ulong* op2, size_t n,
const zn_mod_t mod);
/*
Returns zero if op1[0, n) and op2[0, n) are equal, otherwise nonzero.
*/
int
zn_array_cmp (const ulong* op1, const ulong* op2, size_t n);
/*
Copies op[0, n) to res[0, n).
Buffers must not overlap.
*/
void
zn_array_copy (ulong* res, const ulong* op, size_t n);
/*
Sets res[0, n) to zero.
*/
ZNP_INLINE void
zn_array_zero (ulong* res, size_t n)
{
for (; n; n--)
*res++ = 0;
}
#ifdef __cplusplus
}
#endif
#endif
// end of file ****************************************************************
|