Debian Codegraph @

src/bignum.c

raw symbols SCIP
1 #ifndef __BIGNUM_C
2 #define __BIGNUM_C
3
4 #include "common.h"
5 #include "multiply.h"
6
7 #if defined(USE_SSE2)
8 #if defined(HAVE_INTRIN_H)
9 #include <intrin.h>
10 #elif defined(HAVE_X86INTRIN_H)
11 #include <x86intrin.h>
12 #elif defined(HAVE_EMMINTRIN_H)
13 #include <xmmintrin.h>
14 #include <emmintrin.h>
15 #endif
16 #endif
17
18 static inline unsigned is_odd(uint64_t x)
19 {
20 return 1 == (x & 1);
21 }
22
23 static inline unsigned is_even(uint64_t x)
24 {
25 return !is_odd(x);
26 }
27
28 /**
29 * Check if a multi-word integer x is greater than or equal to y.
30 *
31 * @param x The first term
32 * @param y The second term
33 * @param nw The number of words that make up x and y
34 * @return 1 if x>=y, 0 if x<y
35 */
36 STATIC int ge(const uint64_t *x, const uint64_t *y, size_t nw)
37 {
38 unsigned mask = (unsigned)-1;
39 unsigned result = 0;
40 size_t i, j;
41
42 i = nw - 1;
43 for (j=0; j<nw; j++, i--) {
44 unsigned greater, lower;
45
46 greater = x[i] > y[i];
47 lower = x[i] < y[i];
48 result |= mask & (greater | (lower << 1));
49 mask &= (greater ^ lower) - 1;
50 }
51
52 return result<2;
53 }
54
55 /*
56 * Subtract a multi-word integer b from a.
57 *
58 * @param out The location where the multi-word result is stored
59 * @param a Number to subtract from
60 * @param b Number to subtract
61 * @param nw The number of words of both a and b
62 * @result 0 if there is no borrow, 1 otherwise
63 */
64 STATIC unsigned sub(uint64_t *out, const uint64_t *a, const uint64_t *b, size_t nw)
65 {
66 size_t i;
67 unsigned borrow1 , borrow2;
68
69 borrow2 = 0;
70 for (i=0; i<nw; i++) {
71 borrow1 = b[i] > a[i];
72 out[i] = a[i] - b[i];
73
74 borrow1 |= borrow2 > out[i];
75 out[i] -= borrow2;
76
77 borrow2 = borrow1;
78 }
79
80 return borrow2;
81 }
82
83 /*
84 * Multiply a multi-word integer a by a 64-bit scalar k and
85 * then add the result to the multi-word integer t.
86 *
87 * @param t The multi-word integer accumulator
88 * @param tw The number of words of t
89 * @param a The multi-word integer to multiply with the scalar
90 * @param aw The number of words of a
91 * @param k The 64-bit scalar multiplier
92 */
93 STATIC void addmul(uint64_t *t, size_t tw, const uint64_t *a, size_t aw, uint64_t k)
94 {
95 size_t i;
96 uint64_t carry;
97
98 carry = 0;
99 for (i=0; i<aw; i++) {
100 uint64_t prod_lo, prod_hi;
101
102 DP_MULT(a[i], k, prod_lo, prod_hi);
103
104 prod_lo += carry;
105 prod_hi += prod_lo < carry;
106
107 t[i] += prod_lo;
108 prod_hi += t[i] < prod_lo;
109
110 carry = prod_hi;
111 }
112
113 for (; carry; i++) {
114 t[i] += carry;
115 carry = t[i] < carry;
116 }
117
118 assert(i <= tw);
119 }
120
121 /**
122 * Multiply two multi-word integers.
123 *
124 * @param t The location where the result is stored. It is twice as big as
125 * either a (or b). It is an array of 2*nw words).
126 * @param scratchpad Temporary area. It is an array of 3*nw words.
127 * @param a The first term, array of nw words.
128 * @param b The second term, array of nw words.
129 * @param nw The number of words of both a and b.
130 *
131 */
132 STATIC void product(uint64_t *t, uint64_t *scratchpad, const uint64_t *a, const uint64_t *b, size_t nw)
133 {
134 size_t i;
135
136 memset(t, 0, 2*sizeof(uint64_t)*nw);
137
138 for (i=0; i<(nw ^ (nw & 1)); i+=2) {
139 addmul128(&t[i], scratchpad, a, b[i], b[i+1], 2*nw-i, nw);
140 }
141
142 if (is_odd(nw)) {
143 addmul(&t[nw-1], nw+2, a, nw, b[nw-1]);
144 }
145 }
146
147 /*
148 * Select a number out of two, in constant time.
149 *
150 * @param out The location where the multi-word result is stored
151 * @param a The first choice, selected if cond is true (non-zero)
152 * @param b The second choice, selected if cond is false (zero)
153 * @param cond The flag that drives the selection
154 * @param words The number of words of a, b, and out
155 * @return 0 for success, the appropriate code otherwise.
156 */
157 STATIC int mod_select(uint64_t *out, const uint64_t *a, const uint64_t *b, unsigned cond, size_t words)
158 {
159 uint64_t mask;
160 #if defined(USE_SSE2)
161 unsigned pairs, i;
162 __m128i r0, r1, r2, r3, r4, r5;
163
164 pairs = (unsigned)words / 2;
165 mask = (uint64_t)((cond != 0) - 1); /* 0 for a, 1s for b */
166
167 #if SYS_BITS == 64
168 r0 = _mm_set1_epi64x((int64_t)mask);
169 #else
170 r0 = _mm_loadl_epi64((__m128i*)&mask);
171 r0 = _mm_unpacklo_epi64(r0, r0);
172 #endif
173 for (i=0; i<pairs; i++, a+=2, b+=2, out+=2) {
174 r1 = _mm_loadu_si128((__m128i const*)b);
175 r2 = _mm_loadu_si128((__m128i const*)a);
176 r3 = _mm_and_si128(r0, r1);
177 r4 = _mm_andnot_si128(r0, r2);
178 r5 = _mm_or_si128(r3, r4);
179 _mm_storeu_si128((__m128i*)out, r5);
180 }
181
182 if (words & 1) {
183 *out = (*b & mask) ^ (*a & ~mask);
184 }
185 #else
186 unsigned i;
187
188 mask = (uint64_t)((cond != 0) - 1);
189 for (i=0; i<words; i++) {
190 *out++ = (*b++ & mask) ^ (*a++ & ~mask);
191 }
192 #endif
193
194 return 0;
195 }
196
197 /*
198 * Add two multi-word numbers with modulo arithmetic.
199 *
200 * @param out The locaton where the multi-word result (nw words) is stored
201 * @param a The first term (nw words)
202 * @param b The second term (nw words)
203 * @param modulus The modulus (nw words)
204 * @param tmp1 A temporary area (nw words)
205 * @param tmp2 A temporary area (nw words)
206 * @param nw The number of 64-bit words in all parameters
207 */
208 STATIC void add_mod(uint64_t* out, const uint64_t* a, const uint64_t* b, const uint64_t *modulus, uint64_t *tmp1, uint64_t *tmp2, size_t nw)
209 {
210 unsigned i;
211 unsigned carry, borrow1, borrow2;
212
213 /*
214 * Compute sum in tmp1[], and subtract modulus[]
215 * from tmp1[] into tmp2[].
216 */
217 borrow2 = 0;
218 for (i=0, carry=0; i<nw; i++) {
219 tmp1[i] = a[i] + carry;
220 carry = tmp1[i] < carry;
221 tmp1[i] += b[i];
222 carry += tmp1[i] < b[i];
223
224 borrow1 = modulus[i] > tmp1[i];
225 tmp2[i] = tmp1[i] - modulus[i];
226 borrow1 |= borrow2 > tmp2[i];
227 tmp2[i] -= borrow2;
228 borrow2 = borrow1;
229 }
230
231 /*
232 * If there is no borrow or if there is carry,
233 * tmp1[] is larger than modulus, so we must return tmp2[].
234 */
235 mod_select(out, tmp2, tmp1, carry | (borrow2 ^ 1), nw);
236 }
237
238 /*
239 * Subtract two multi-word numbers with modulo arithmetic.
240 *
241 * @param out The locaton where the multi-word result (nw words) is stored
242 * @param a The number it will be subtracted from (nw words)
243 * @param b The number to subtract (nw wordS)
244 * @param modulus The modulus (nw words)
245 * @param tmp1 A temporary area (nw words)
246 * @param tmp2 A temporary area (nw words)
247 * @param nw The number of 64-bit words in all parameters
248 * @return 0 for success, the relevant error code otherwise
249 */
250 int sub_mod(uint64_t *out, const uint64_t *a, const uint64_t *b, const uint64_t *modulus, uint64_t *tmp1, uint64_t *tmp2, size_t nw)
251 {
252 unsigned i;
253 unsigned carry, borrow1 , borrow2;
254
255 /*
256 * Compute difference in tmp1[], and add modulus[]
257 * to tmp1[] into tmp2[].
258 */
259 borrow2 = 0;
260 carry = 0;
261 for (i=0; i<nw; i++) {
262 borrow1 = b[i] > a[i];
263 tmp1[i] = a[i] - b[i];
264 borrow1 |= borrow2 > tmp1[i];
265 tmp1[i] -= borrow2;
266 borrow2 = borrow1;
267
268 tmp2[i] = tmp1[i] + carry;
269 carry = tmp2[i] < carry;
270 tmp2[i] += modulus[i];
271 carry += tmp2[i] < modulus[i];
272 }
273
274 /*
275 * If there is no borrow, tmp[] is smaller than modulus.
276 */
277 mod_select(out, tmp2, tmp1, borrow2, nw);
278
279 return 0;
280 }
281
282 #endif /* __BIGNUM_C **/