Debian Codegraph @

src/multiply_32.c

raw symbols SCIP
1 /* ===================================================================
2 *
3 * Copyright (c) 2019, Helder Eijs <[email protected]>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 * ===================================================================
30 */
31
32 #include <stddef.h>
33 #include <string.h>
34 #include <assert.h>
35 #include <stdio.h>
36 #include "multiply.h"
37
38 #if defined(USE_SSE2)
39 #if defined(HAVE_INTRIN_H)
40 #include <intrin.h>
41 #elif defined(HAVE_X86INTRIN_H)
42 #include <x86intrin.h>
43 #elif defined(HAVE_EMMINTRIN_H)
44 #include <xmmintrin.h>
45 #include <emmintrin.h>
46 #endif
47 #endif
48
49 /*
50 * Multiply a vector a[] by a scalar b. Add the result into vector t[],
51 * starting at the given offset.
52 */
53 void static inline addmul32(uint32_t* t, size_t offset, const uint32_t *a, uint32_t b, size_t t_words, size_t a_words)
54 {
55 uint32_t carry;
56 size_t i;
57 #if defined(USE_SSE2)
58 __m128i r0, r1;
59 #endif
60
61 assert(t_words >= a_words);
62
63 carry = 0;
64 i = 0;
65
66 if (a_words == 0) {
67 return;
68 }
69
70 #if defined(USE_SSE2)
71 r0 = _mm_set1_epi32((int)b); // { b, b, b, b }
72 r1 = _mm_cvtsi32_si128((int)carry); // { 0, 0, 0, carry }
73
74 for (i=0; i<(a_words ^ (a_words & 1U)); i+=2) {
75 __m128i r10, r11, r12, r13, r14, r15, r16, r17;
76
77 r10 = _mm_shuffle_epi32(
78 _mm_castpd_si128(
79 _mm_set_sd(*(double*)&a[i])
80 ),
81 _MM_SHUFFLE(2,1,2,0)); // { 0, a[i+1], 0, a[i] }
82 r11 = _mm_mul_epu32(r0, r10); // { a[i+1]*b, a[i]*b }
83 r12 = _mm_shuffle_epi32(
84 _mm_castpd_si128(
85 _mm_set_sd(*(double*)&t[i+offset])
86 ),
87 _MM_SHUFFLE(2,1,2,0)); // { 0, t[i+1], 0, t[i] }
88 r13 = _mm_add_epi64(r12, r1); // { t[i+1], t[i]+carry }
89 r14 = _mm_add_epi64(r11, r13); // { a[i+1]*b+t[i+1], a[i]*b+t[i]+carry }
90 r15 = _mm_shuffle_epi32(
91 _mm_move_epi64(r14), // { 0, a[i]*b+t[i]+carry }
92 _MM_SHUFFLE(2,1,2,2)
93 ); // { 0, H(a[i]*b+t[i]+carry), 0, 0 }
94 r16 = _mm_add_epi64(r14, r15); // { next_carry, new t[i+1], *, new t[i] }
95 r17 = _mm_shuffle_epi32(r16,
96 _MM_SHUFFLE(2,0,1,3)); // { new t[i+1], new t[i], *, new carry }
97 _mm_storeh_pd((double*)&t[i+offset],
98 _mm_castsi128_pd(r17)); // Store upper 64 bit word (also t[i+1])
99 r1 = _mm_castps_si128(_mm_move_ss(
100 _mm_castsi128_ps(r1),
101 _mm_castsi128_ps(r17)
102 ));
103 }
104 carry = (uint32_t)_mm_cvtsi128_si32(r1);
105 #endif
106
107 for (; i<a_words; i++) {
108 uint64_t prod;
109 uint32_t prodl, prodh;
110
111 prod = (uint64_t)a[i]*b;
112 prodl = (uint32_t)prod;
113 prodh = (uint32_t)(prod >> 32);
114
115 prodl += carry; prodh += prodl < carry;
116 t[i+offset] += prodl; prodh += t[i+offset] < prodl;
117 carry = prodh;
118 }
119
120 for (;i+offset<t_words; i++) {
121 t[i+offset] += carry;
122 carry = t[i+offset] < carry;
123 }
124
125 assert(carry == 0);
126 }
127
128 /*
129 * Multiply a vector a[] by a scalar b = b0 + b1*2⁶⁴.
130 * Add the result into vector t[],
131 *
132 * t[] and a[] are little-endian.
133 * Return the number of 64-bit words that we wrote into t[]
134 */
135 void addmul128(uint64_t *t, uint64_t *scratchpad, const uint64_t *a, uint64_t b0, uint64_t b1, size_t t_nw, size_t a_nw)
136 {
137 uint32_t b0l, b0h, b1l, b1h;
138 uint32_t *t32, *a32;
139 #ifndef PYCRYPTO_LITTLE_ENDIAN
140 size_t i;
141 #endif
142
143 assert(t_nw >= a_nw + 2);
144
145 if (a_nw == 0) {
146 return;
147 }
148
149 b0l = (uint32_t)b0;
150 b0h = (uint32_t)(b0 >> 32);
151 b1l = (uint32_t)b1;
152 b1h = (uint32_t)(b1 >> 32);
153
154 t32 = (uint32_t*)scratchpad;
155 a32 = (uint32_t*)(scratchpad + t_nw);
156
157 #ifdef PYCRYPTO_LITTLE_ENDIAN
158 memcpy(t32, t, sizeof(uint64_t)*t_nw);
159 memcpy(a32, a, sizeof(uint64_t)*a_nw);
160 #else
161 for (i=0; i<t_nw; i++) {
162 t32[2*i] = (uint32_t)t[i];
163 t32[2*i+1] = (uint32_t)(t[i] >> 32);
164 }
165 for (i=0; i<a_nw; i++) {
166 a32[2*i] = (uint32_t)a[i];
167 a32[2*i+1] = (uint32_t)(a[i] >> 32);
168 }
169 #endif
170
171 addmul32(t32, 0, a32, b0l, 2*t_nw, 2*a_nw);
172 addmul32(t32, 1, a32, b0h, 2*t_nw, 2*a_nw);
173 addmul32(t32, 2, a32, b1l, 2*t_nw, 2*a_nw);
174 addmul32(t32, 3, a32, b1h, 2*t_nw, 2*a_nw);
175
176 #ifdef PYCRYPTO_LITTLE_ENDIAN
177 memcpy(t, t32, sizeof(uint64_t)*t_nw);
178 #else
179 for (i=0; i<t_nw; i++) {
180 t[i] = (uint64_t)t32[2*i] + ((uint64_t)t32[2*i+1] << 32);
181 }
182 #endif
183 }
184
185 /*
186 * Square a vector a[] and store the result in t[].
187 */
188 void static inline square_32(uint32_t *t, const uint32_t *a, size_t nw)
189 {
190 size_t i, j;
191 uint32_t carry;
192
193 if (nw == 0) {
194 return;
195 }
196
197 memset(t, 0, 2*sizeof(uint32_t)*nw);
198
199 /** Compute all mix-products without doubling **/
200 for (i=0; i<nw; i++) {
201 carry = 0;
202
203 for (j=i+1; j<nw; j++) {
204 uint64_t prod;
205 uint32_t suml, sumh;
206
207 prod = (uint64_t)a[j]*a[i];
208 suml = (uint32_t)prod;
209 sumh = (uint32_t)(prod >> 32);
210
211 suml += carry;
212 sumh += suml < carry;
213
214 t[i+j] += suml;
215 carry = sumh + (t[i+j] < suml);
216 }
217
218 /** Propagate carry **/
219 for (j=i+nw; carry>0; j++) {
220 t[j] += carry;
221 carry = t[j] < carry;
222 }
223 }
224
225 /** Double mix-products and add squares **/
226 carry = 0;
227 for (i=0, j=0; i<nw; i++, j+=2) {
228 uint64_t prod;
229 uint32_t suml, sumh, tmp, tmp2;
230
231 prod = (uint64_t)a[i]*a[i];
232 suml = (uint32_t)prod;
233 sumh = (uint32_t)(prod >> 32);
234
235 suml += carry;
236 sumh += suml < carry;
237
238 sumh += (tmp = ((t[j+1] << 1) + (t[j] >> 31)));
239 carry = (t[j+1] >> 31) + (sumh < tmp);
240
241 suml += (tmp = (t[j] << 1));
242 sumh += (tmp2 = (suml < tmp));
243 carry += sumh < tmp2;
244
245 t[j] = suml;
246 t[j+1] = sumh;
247 }
248
249 assert(carry == 0);
250 }
251
252 void square(uint64_t *t, uint64_t *scratchpad, const uint64_t *a, size_t nw)
253 {
254 uint32_t *t32, *a32;
255 #ifndef PYCRYPTO_LITTLE_ENDIAN
256 size_t i;
257 #endif
258
259 t32 = (uint32_t*)scratchpad;
260 a32 = (uint32_t*)(scratchpad + 2*nw);
261
262 #ifdef PYCRYPTO_LITTLE_ENDIAN
263 memcpy(a32, a, sizeof(uint64_t)*nw);
264 #else
265 for (i=0; i<nw; i++) {
266 a32[2*i] = (uint32_t)a[i];
267 a32[2*i+1] = (uint32_t)(a[i] >> 32);
268 }
269 #endif
270
271 square_32(t32, a32, nw*2);
272
273 #ifdef PYCRYPTO_LITTLE_ENDIAN
274 memcpy(t, t32, 2*sizeof(uint64_t)*nw);
275 #else
276 for (i=0; i<2*nw; i++) {
277 t[i] = (uint64_t)t32[2*i] + ((uint64_t)t32[2*i+1] << 32);
278 }
279 #endif
280 }