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