-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsmul_template.h
413 lines (341 loc) · 11.6 KB
/
jsmul_template.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/* Copyright 2008-2019 Douglas Wikstrom
*
* This file is part of Verificatum Elliptic Curve library (VEC).
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdlib.h>
#include "templates.h"
#include "jsmul_h_template.h"
void
FUNCTION_NAME(vec_jsmul_init, POSTFIX)
(FUNCTION_NAME(vec_jsmul_tab, TAB_POSTFIX) table,
CURVE *curve,
size_t len,
size_t block_width)
{
size_t i; /* Index parameters. */
size_t tab_len; /* Size of a subtable. */
/* We need the curve parameter in other implementations of
* multiplication, so to use function pointers we need to keep it here
* as well.
*/
VEC_UNUSED(curve);
/* Initialize length parameters. */
table->len = len;
table->block_width = block_width;
if (len < block_width) {
table->block_width = len;
}
table->tabs_len = (len + block_width - 1) / block_width;
/* Allocate and initialize space for pointers to tables. */
table->tabsx =
(FIELD_ELEMENT_VAR **)malloc(table->tabs_len * sizeof(FIELD_ELEMENT_VAR *));
table->tabsy =
(FIELD_ELEMENT_VAR **)malloc(table->tabs_len * sizeof(FIELD_ELEMENT_VAR *));
table->tabsz =
(FIELD_ELEMENT_VAR **)malloc(table->tabs_len * sizeof(FIELD_ELEMENT_VAR *));
tab_len = 1 << block_width;
for (i = 0; i < table->tabs_len; i++)
{
/* Last block may be more narrow than the other. */
if (i == table->tabs_len - 1
&& len - (table->tabs_len - 1) * block_width < block_width)
{
block_width = len - (table->tabs_len - 1) * block_width;
tab_len = 1 << block_width;
}
/* Allocate and initialize a table. */
table->tabsx[i] = ARRAY_MALLOC_INIT(tab_len);
table->tabsy[i] = ARRAY_MALLOC_INIT(tab_len);
table->tabsz[i] = ARRAY_MALLOC_INIT(tab_len);
}
}
int
FUNCTION_NAME(vec_jsmul_clear, POSTFIX)
(FUNCTION_NAME(vec_jsmul_tab, TAB_POSTFIX) table)
{
size_t i; /* Index variables. */
size_t tabs_len = table->tabs_len; /* Number of sub tables. */
size_t block_width = table->block_width; /* Width of each sub table. */
size_t tab_len; /* Size of each sub table. */
tab_len = 1 << block_width;
for (i = 0; i < tabs_len; i++)
{
/* Last block may have smaller width, but it is never zero. */
if (i == tabs_len - 1)
{
block_width = table->len - (tabs_len - 1) * block_width;
tab_len = 1 << block_width;
}
/* Deallocate table. */
ARRAY_CLEAR_FREE(table->tabsx[i], tab_len);
ARRAY_CLEAR_FREE(table->tabsy[i], tab_len);
ARRAY_CLEAR_FREE(table->tabsz[i], tab_len);
}
/* Deallocate table of tables. */
free(table->tabsx);
free(table->tabsy);
free(table->tabsz);
/* Hack to avoid unused-variable warnings for the case where
ARRAY_CLEAR_FREE expands to an expression not involving
tab_len. */
return tab_len;
}
void
FUNCTION_NAME(vec_jsmul_precomp, POSTFIX)
(FUNCTION_NAME(vec_jsmul_tab, TAB_POSTFIX) table,
CURVE *curve,
FIELD_ELEMENT_VAR *basesx, FIELD_ELEMENT_VAR *basesy,
FIELD_ELEMENT_VAR *basesz)
{
size_t i, j; /* Index variables. */
size_t block_width; /* Width of current subtable. */
size_t tab_len; /* Size of current subtable. */
FIELD_ELEMENT_VAR *tx; /* Temporary variable for subtable. */
FIELD_ELEMENT_VAR *ty; /* Temporary variable for subtable. */
FIELD_ELEMENT_VAR *tz; /* Temporary variable for subtable. */
int mask; /* Mask used for dynamic programming */
int one_mask; /* Mask containing a single non-zero bit. */
SCRATCH(scratch);
VEC_UNUSED(curve);
SCRATCH_INIT(scratch);
block_width = table->block_width;
tab_len = 1 << block_width;
for (i = 0; i < table->tabs_len; i++)
{
/* Last block may have smaller width, but it is never zero. */
if (i == table->tabs_len - 1)
{
block_width = table->len - (table->tabs_len - 1) * block_width;
tab_len = 1 << block_width;
}
/* Current subtable. */
tx = table->tabsx[i];
ty = table->tabsy[i];
tz = table->tabsz[i];
/* Initialize current subtable with all trivial products. */
FIELD_ELEMENT_VAR_UNIT(tx[0], ty[0], tz[0]);
mask = 1;
for (j = 0; j < block_width; j++)
{
FIELD_ELEMENT_VAR_SET(tx[mask], ty[mask], tz[mask],
basesx[j], basesy[j], basesz[j]);
mask <<= 1;
}
/* Initialize current subtable with all non-trivial products. */
for (mask = 1; ((size_t) mask) < tab_len; mask++)
{
one_mask = mask & (-mask);
JADD_VAR(scratch,
tx[mask], ty[mask], tz[mask],
curve,
tx[mask ^ one_mask],
ty[mask ^ one_mask],
tz[mask ^ one_mask],
tx[one_mask],
ty[one_mask],
tz[one_mask]);
}
basesx += block_width;
basesy += block_width;
basesz += block_width;
}
SCRATCH_CLEAR(scratch);
}
static int
getbits(mpz_t *op, int index, size_t block_width)
{
int i;
int bits = 0;
for (i = block_width - 1; i >= 0; i--)
{
bits <<= 1;
if (mpz_tstbit(op[i], index))
{
bits |= 1;
}
}
return bits;
}
void
FUNCTION_NAME(vec_jsmul_table, POSTFIX)
(FIELD_ELEMENT_VAR ropx, FIELD_ELEMENT_VAR ropy, FIELD_ELEMENT_VAR ropz,
CURVE *curve,
FUNCTION_NAME(vec_jsmul_tab, TAB_POSTFIX) table,
mpz_t *scalars,
size_t max_scalar_bitlen)
{
size_t i;
int index;
int mask;
mpz_t *scls;
FIELD_ELEMENT tmpx;
FIELD_ELEMENT tmpy;
FIELD_ELEMENT tmpz;
size_t len = table->len;
size_t tabs_len = table->tabs_len;
size_t block_width = table->block_width;
size_t last_block_width = len - (tabs_len - 1) * block_width;
FIELD_ELEMENT_VAR **tabsx = table->tabsx;
FIELD_ELEMENT_VAR **tabsy = table->tabsy;
FIELD_ELEMENT_VAR **tabsz = table->tabsz;
SCRATCH(scratch);
VEC_UNUSED(curve);
SCRATCH_INIT(scratch);
FIELD_ELEMENT_INIT(tmpx);
FIELD_ELEMENT_INIT(tmpy);
FIELD_ELEMENT_INIT(tmpz);
/* Initialize result variable. */
FIELD_ELEMENT_UNIT(tmpx, tmpy, tmpz);
/* Execute simultaneous square-and-multiply. */
for (index = max_scalar_bitlen - 1; index >= 0; index--)
{
/* Square ... */
JDBL(scratch,
tmpx, tmpy, tmpz,
curve,
tmpx, tmpy, tmpz);
/* ... and multiply. */
i = 0;
scls = scalars;
while (i < tabs_len)
{
if (i == tabs_len - 1)
{
mask = getbits(scls, index, last_block_width);
}
else
{
mask = getbits(scls, index, block_width);
}
JADD(scratch,
tmpx, tmpy, tmpz,
curve,
tmpx, tmpy, tmpz,
tabsx[i][mask], tabsy[i][mask], tabsz[i][mask]);
i++;
scls += block_width;
}
}
SCRATCH_CLEAR(scratch);
FIELD_ELEMENT_CONTRACT(ropx, ropy, ropz, tmpx, tmpy, tmpz);
FIELD_ELEMENT_CLEAR(tmpx);
FIELD_ELEMENT_CLEAR(tmpy);
FIELD_ELEMENT_CLEAR(tmpz);
}
void
FUNCTION_NAME(vec_jsmul_block_batch, POSTFIX)
(FIELD_ELEMENT ropx, FIELD_ELEMENT ropy, FIELD_ELEMENT ropz,
CURVE *curve,
FIELD_ELEMENT_VAR *basesx, FIELD_ELEMENT_VAR *basesy,
FIELD_ELEMENT_VAR *basesz,
mpz_t *scalars,
size_t len,
size_t block_width, size_t batch_len,
size_t max_scalar_bitlen)
{
size_t i;
FUNCTION_NAME(vec_jsmul_tab, TAB_POSTFIX) table;
FIELD_ELEMENT_VAR tmpx;
FIELD_ELEMENT_VAR tmpy;
FIELD_ELEMENT_VAR tmpz;
SCRATCH(scratch);
SCRATCH_INIT(scratch);
FIELD_ELEMENT_VAR_INIT(tmpx);
FIELD_ELEMENT_VAR_INIT(tmpy);
FIELD_ELEMENT_VAR_INIT(tmpz);
if (len < batch_len) {
batch_len = len;
}
FUNCTION_NAME(vec_jsmul_init, POSTFIX)(table, curve, batch_len,
block_width);
/* Initialize result to unit element. */
FIELD_ELEMENT_UNIT(ropx, ropy, ropz);
for (i = 0; i < len; i += batch_len)
{
/* Last batch may be slightly shorter. */
if (len - i < batch_len)
{
batch_len = len - i;
FUNCTION_NAME(vec_jsmul_clear, POSTFIX)(table);
FUNCTION_NAME(vec_jsmul_init, POSTFIX)(table, curve,
batch_len, block_width);
}
/* Perform computation for batch */
FUNCTION_NAME(vec_jsmul_precomp, POSTFIX)(table, curve,
basesx, basesy, basesz);
/* Compute batch. */
FUNCTION_NAME(vec_jsmul_table, POSTFIX)(tmpx, tmpy, tmpz,
curve, table,
scalars, max_scalar_bitlen);
/* Add with result so far. */
JADD(scratch,
ropx, ropy, ropz,
curve,
ropx, ropy, ropz,
tmpx, tmpy, tmpz);
/* Move on to next batch. */
basesx += batch_len;
basesy += batch_len;
basesz += batch_len;
scalars += batch_len;
}
SCRATCH_CLEAR(scratch);
FUNCTION_NAME(vec_jsmul_clear, POSTFIX)(table);
}
void
FUNCTION_NAME(vec_jsmul, POSTFIX)
(FIELD_ELEMENT ropx, FIELD_ELEMENT ropy, FIELD_ELEMENT ropz,
vec_curve *curve,
FIELD_ELEMENT_VAR *basesx, FIELD_ELEMENT_VAR *basesy,
FIELD_ELEMENT_VAR *basesz,
mpz_t *scalars,
size_t len)
{
size_t i;
size_t bitlen;
size_t max_scalar_bitlen;
size_t batch_len = 100; /* This is somewhat arbitrary, but it
makes the amortized cost for squaring
very small in comparison to the cost
for multiplications. */
size_t block_width;
/* Compute the maximal bit length among the scalars. */
max_scalar_bitlen = 0;
for (i = 0; i < len; i++)
{
bitlen = mpz_sizeinbase(scalars[i], 2);
if (bitlen > max_scalar_bitlen)
{
max_scalar_bitlen = bitlen;
}
}
/* Determine a good block width. */
block_width = vec_smul_block_width(max_scalar_bitlen, batch_len);
FUNCTION_NAME(vec_jsmul_block_batch, POSTFIX)(ropx, ropy, ropz,
curve,
basesx, basesy, basesz,
scalars,
len,
block_width, batch_len,
max_scalar_bitlen);
}