numbertheory.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) 2008-2020 Stefan Krah. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. * SUCH DAMAGE.
  26. */
  27. #include "mpdecimal.h"
  28. #include <assert.h>
  29. #include <stdlib.h>
  30. #include "bits.h"
  31. #include "numbertheory.h"
  32. #include "umodarith.h"
  33. /* Bignum: Initialize the Number Theoretic Transform. */
  34. /*
  35. * Return the nth root of unity in F(p). This corresponds to e**((2*pi*i)/n)
  36. * in the Fourier transform. We have w**n == 1 (mod p).
  37. * n := transform length.
  38. * sign := -1 for forward transform, 1 for backward transform.
  39. * modnum := one of {P1, P2, P3}.
  40. */
  41. mpd_uint_t
  42. _mpd_getkernel(mpd_uint_t n, int sign, int modnum)
  43. {
  44. mpd_uint_t umod, p, r, xi;
  45. #ifdef PPRO
  46. double dmod;
  47. uint32_t dinvmod[3];
  48. #endif
  49. SETMODULUS(modnum);
  50. r = mpd_roots[modnum]; /* primitive root of F(p) */
  51. p = umod;
  52. xi = (p-1) / n;
  53. if (sign == -1)
  54. return POWMOD(r, (p-1-xi));
  55. else
  56. return POWMOD(r, xi);
  57. }
  58. /*
  59. * Initialize and return transform parameters.
  60. * n := transform length.
  61. * sign := -1 for forward transform, 1 for backward transform.
  62. * modnum := one of {P1, P2, P3}.
  63. */
  64. struct fnt_params *
  65. _mpd_init_fnt_params(mpd_size_t n, int sign, int modnum)
  66. {
  67. struct fnt_params *tparams;
  68. mpd_uint_t umod;
  69. #ifdef PPRO
  70. double dmod;
  71. uint32_t dinvmod[3];
  72. #endif
  73. mpd_uint_t kernel, w;
  74. mpd_uint_t i;
  75. mpd_size_t nhalf;
  76. assert(ispower2(n));
  77. assert(sign == -1 || sign == 1);
  78. assert(P1 <= modnum && modnum <= P3);
  79. nhalf = n/2;
  80. tparams = mpd_sh_alloc(sizeof *tparams, nhalf, sizeof (mpd_uint_t));
  81. if (tparams == NULL) {
  82. return NULL;
  83. }
  84. SETMODULUS(modnum);
  85. kernel = _mpd_getkernel(n, sign, modnum);
  86. tparams->modnum = modnum;
  87. tparams->modulus = umod;
  88. tparams->kernel = kernel;
  89. /* wtable[] := w**0, w**1, ..., w**(nhalf-1) */
  90. w = 1;
  91. for (i = 0; i < nhalf; i++) {
  92. tparams->wtable[i] = w;
  93. w = MULMOD(w, kernel);
  94. }
  95. return tparams;
  96. }
  97. /* Initialize wtable of size three. */
  98. void
  99. _mpd_init_w3table(mpd_uint_t w3table[3], int sign, int modnum)
  100. {
  101. mpd_uint_t umod;
  102. #ifdef PPRO
  103. double dmod;
  104. uint32_t dinvmod[3];
  105. #endif
  106. mpd_uint_t kernel;
  107. SETMODULUS(modnum);
  108. kernel = _mpd_getkernel(3, sign, modnum);
  109. w3table[0] = 1;
  110. w3table[1] = kernel;
  111. w3table[2] = POWMOD(kernel, 2);
  112. }