-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjmulsw_template.h
181 lines (147 loc) · 4.38 KB
/
jmulsw_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
/* 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.
*/
#ifdef FIELD_ELEMENT
#include "templates.h"
/*
* Multiplication with sliding window. In theory this could be
* slightly faster using wNAFs, but the difference would be very
* small.
*/
void
FUNCTION_NAME(vec_jmulsw, POSTFIX)
(FIELD_ELEMENT RX, FIELD_ELEMENT RY, FIELD_ELEMENT RZ,
CURVE *curve,
FIELD_ELEMENT_VAR X, FIELD_ELEMENT_VAR Y, FIELD_ELEMENT_VAR Z,
mpz_t scalar)
{
int i;
int j;
int b;
int size;
int width;
float cost;
float new_cost;
int bit_length;
int block;
FIELD_ELEMENT_VAR *xtab;
FIELD_ELEMENT_VAR *ytab;
FIELD_ELEMENT_VAR *ztab;
SCRATCH(scratch);
VEC_UNUSED(curve);
SCRATCH_INIT(scratch);
/* Determine bit length. */
bit_length = (int)mpz_sizeinbase(scalar, 2);
/* Determine optimal width of table. */
width = 1;
new_cost = bit_length / 2;
do {
width++;
cost = new_cost;
new_cost = (1 << (width - 1)) +
((float)((1 << width) - 1) * bit_length) / ((1 << width) * width);
} while (new_cost < cost);
width--;
size = (1 << (width - 1));
/* Precompute table. */
xtab = ARRAY_MALLOC_INIT(size);
ytab = ARRAY_MALLOC_INIT(size);
ztab = ARRAY_MALLOC_INIT(size);
/* Double (X, Y, Z) to compute table. */
JDBL_VAR(scratch,
xtab[size - 1], ytab[size - 1], ztab[size - 1],
curve,
X, Y, Z);
/* Initialize with basis. */
FIELD_ELEMENT_VAR_SET(xtab[0], ytab[0], ztab[0], X, Y, Z);
/* Build table */
for (i = 1; i < size; i++) {
JADD_VAR(scratch,
xtab[i], ytab[i], ztab[i],
curve,
xtab[i - 1], ytab[i - 1], ztab[i - 1],
xtab[size - 1], ytab[size - 1], ztab[size - 1]);
}
/* Initialize with basis. */
FIELD_ELEMENT_UNIT(RX, RY, RZ);
/* Compute output. */
i = bit_length;
while (i >= 0)
{
/* Double until we encounter a one, i.e., the starting block of
a block of bits. */
while (i >= 0 && !mpz_tstbit(scalar, i))
{
JDBL(scratch,
RX, RY, RZ,
curve,
RX, RY, RZ);
i--;
}
/* Check if we did not encounter any one and should return. */
if (i < 0)
{
break;
}
/* Find end point of block of bits. We know that we will find a
one now, since i >= 0. */
j = i - width + 1;
if (j < 0)
{
j = 0;
}
while (j < i && !mpz_tstbit(scalar, j))
{
j++;
}
/* Extract block of bits starting and ending with ones. Note
that we drop the least significant bit, since this is not
used in the table lookup. */
block = 0;
for (b = i; b > j; b--)
{
block = (block << 1) | mpz_tstbit(scalar, b);
}
/* Double for the block. */
while (i >= j)
{
JDBL(scratch,
RX, RY, RZ,
curve,
RX, RY, RZ);
i--;
}
/* Add with block. */
JADD(scratch,
RX, RY, RZ,
curve,
RX, RY, RZ,
xtab[block], ytab[block], ztab[block]);
}
ARRAY_CLEAR_FREE(ztab, size);
ARRAY_CLEAR_FREE(ytab, size);
ARRAY_CLEAR_FREE(xtab, size);
SCRATCH_CLEAR(scratch);
}
#endif