Line data Source code
1 : /*
2 : * Copyright (c) 2008-2012 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 :
28 :
29 : #include "mpdecimal.h"
30 : #include <stdio.h>
31 : #include "bits.h"
32 : #include "constants.h"
33 : #include "fnt.h"
34 : #include "fourstep.h"
35 : #include "numbertheory.h"
36 : #include "sixstep.h"
37 : #include "umodarith.h"
38 : #include "convolute.h"
39 :
40 :
41 : /* Bignum: Fast convolution using the Number Theoretic Transform. Used for
42 : the multiplication of very large coefficients. */
43 :
44 :
45 : /* Convolute the data in c1 and c2. Result is in c1. */
46 : int
47 0 : fnt_convolute(mpd_uint_t *c1, mpd_uint_t *c2, mpd_size_t n, int modnum)
48 : {
49 : int (*fnt)(mpd_uint_t *, mpd_size_t, int);
50 : int (*inv_fnt)(mpd_uint_t *, mpd_size_t, int);
51 : #ifdef PPRO
52 : double dmod;
53 : uint32_t dinvmod[3];
54 : #endif
55 : mpd_uint_t n_inv, umod;
56 : mpd_size_t i;
57 :
58 :
59 0 : SETMODULUS(modnum);
60 0 : n_inv = POWMOD(n, (umod-2));
61 :
62 0 : if (ispower2(n)) {
63 0 : if (n > SIX_STEP_THRESHOLD) {
64 0 : fnt = six_step_fnt;
65 0 : inv_fnt = inv_six_step_fnt;
66 : }
67 : else {
68 0 : fnt = std_fnt;
69 0 : inv_fnt = std_inv_fnt;
70 : }
71 : }
72 : else {
73 0 : fnt = four_step_fnt;
74 0 : inv_fnt = inv_four_step_fnt;
75 : }
76 :
77 0 : if (!fnt(c1, n, modnum)) {
78 0 : return 0;
79 : }
80 0 : if (!fnt(c2, n, modnum)) {
81 0 : return 0;
82 : }
83 0 : for (i = 0; i < n-1; i += 2) {
84 0 : mpd_uint_t x0 = c1[i];
85 0 : mpd_uint_t y0 = c2[i];
86 0 : mpd_uint_t x1 = c1[i+1];
87 0 : mpd_uint_t y1 = c2[i+1];
88 0 : MULMOD2(&x0, y0, &x1, y1);
89 0 : c1[i] = x0;
90 0 : c1[i+1] = x1;
91 : }
92 :
93 0 : if (!inv_fnt(c1, n, modnum)) {
94 0 : return 0;
95 : }
96 0 : for (i = 0; i < n-3; i += 4) {
97 0 : mpd_uint_t x0 = c1[i];
98 0 : mpd_uint_t x1 = c1[i+1];
99 0 : mpd_uint_t x2 = c1[i+2];
100 0 : mpd_uint_t x3 = c1[i+3];
101 0 : MULMOD2C(&x0, &x1, n_inv);
102 0 : MULMOD2C(&x2, &x3, n_inv);
103 0 : c1[i] = x0;
104 0 : c1[i+1] = x1;
105 0 : c1[i+2] = x2;
106 0 : c1[i+3] = x3;
107 : }
108 :
109 0 : return 1;
110 : }
111 :
112 : /* Autoconvolute the data in c1. Result is in c1. */
113 : int
114 0 : fnt_autoconvolute(mpd_uint_t *c1, mpd_size_t n, int modnum)
115 : {
116 : int (*fnt)(mpd_uint_t *, mpd_size_t, int);
117 : int (*inv_fnt)(mpd_uint_t *, mpd_size_t, int);
118 : #ifdef PPRO
119 : double dmod;
120 : uint32_t dinvmod[3];
121 : #endif
122 : mpd_uint_t n_inv, umod;
123 : mpd_size_t i;
124 :
125 :
126 0 : SETMODULUS(modnum);
127 0 : n_inv = POWMOD(n, (umod-2));
128 :
129 0 : if (ispower2(n)) {
130 0 : if (n > SIX_STEP_THRESHOLD) {
131 0 : fnt = six_step_fnt;
132 0 : inv_fnt = inv_six_step_fnt;
133 : }
134 : else {
135 0 : fnt = std_fnt;
136 0 : inv_fnt = std_inv_fnt;
137 : }
138 : }
139 : else {
140 0 : fnt = four_step_fnt;
141 0 : inv_fnt = inv_four_step_fnt;
142 : }
143 :
144 0 : if (!fnt(c1, n, modnum)) {
145 0 : return 0;
146 : }
147 0 : for (i = 0; i < n-1; i += 2) {
148 0 : mpd_uint_t x0 = c1[i];
149 0 : mpd_uint_t x1 = c1[i+1];
150 0 : MULMOD2(&x0, x0, &x1, x1);
151 0 : c1[i] = x0;
152 0 : c1[i+1] = x1;
153 : }
154 :
155 0 : if (!inv_fnt(c1, n, modnum)) {
156 0 : return 0;
157 : }
158 0 : for (i = 0; i < n-3; i += 4) {
159 0 : mpd_uint_t x0 = c1[i];
160 0 : mpd_uint_t x1 = c1[i+1];
161 0 : mpd_uint_t x2 = c1[i+2];
162 0 : mpd_uint_t x3 = c1[i+3];
163 0 : MULMOD2C(&x0, &x1, n_inv);
164 0 : MULMOD2C(&x2, &x3, n_inv);
165 0 : c1[i] = x0;
166 0 : c1[i+1] = x1;
167 0 : c1[i+2] = x2;
168 0 : c1[i+3] = x3;
169 : }
170 :
171 0 : return 1;
172 : }
173 :
174 :
|