-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrepr.cpp
283 lines (244 loc) · 5.13 KB
/
repr.cpp
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
#include "repr.h"
// Calculate a - b - borrow, returning the result and modifying
// the borrow value.
u64 sbb(u64 a, u64 b, u64 &borrow)
{
Repr<2> ar = {a, 1};
Repr<2> br = {b, 0};
Repr<2> borrowr = {borrow, 0};
auto const tmp = (ar - br) - borrowr;
if (tmp[1] == 0)
{
borrow = 1;
}
else
{
borrow = 0;
};
return tmp[0];
}
// Calculate a + b + carry, returning the sum and modifying the
// carry value.
u64 adc(u64 a, u64 b, u64 &carry)
{
Repr<2> ar = {a, 0};
Repr<2> br = {b, 0};
Repr<2> carryr = {carry, 0};
auto const tmp = ar + br + carryr;
carry = tmp[1];
return tmp[0];
}
// Calculate a * b + carry, returning the least significant digit
// and setting carry to the most significant digit.
u64 mul_with_carry(u64 a, u64 b, u64 &carry)
{
Repr<1> ar = {a};
Repr<1> br = {b};
Repr<2> carryr = {carry, 0};
auto const tmp = ar * br + carryr;
carry = tmp[1];
return tmp[0];
}
bool is_zero(std::vector<u64> const &repr)
{
for (auto it = repr.cbegin(); it != repr.cend(); it++)
{
if (*it != 0)
{
return false;
}
}
return true;
}
bool is_odd(std::vector<u64> const &repr)
{
if (repr.size() == 0)
{
return false;
}
return (repr[0] & 1) == 1;
}
void div2(std::vector<u64> &repr)
{
auto t = 0;
for (auto it = repr.rbegin(); it != repr.rend(); it++)
{
auto const t2 = *it << 63;
*it >>= 1;
*it |= t;
t = t2;
}
}
void sub_noborrow(std::vector<u64> &repr, u64 value)
{
u64 borrow = 0;
repr[0] = sbb(repr[0], value, borrow);
auto it = repr.begin();
it++; //Skip 0
for (; it != repr.end(); it++)
{
*it = sbb(*it, 0, borrow);
}
}
void add_nocarry(std::vector<u64> &repr, u64 value)
{
u64 carry = 0;
repr[0] = adc(repr[0], value, carry);
auto it = repr.begin();
it++; //Skip 0
for (; it != repr.end(); it++)
{
*it = adc(*it, 0, carry);
}
}
// a >= b
// Where a and b are numbers
bool greater_or_equal(std::vector<u64> const &a, std::vector<u64> const &b)
{
for (auto i = a.size(); i < b.size(); i++)
{
if (b[i] > 0)
{
return false;
}
}
for (auto i = b.size(); i < a.size(); i++)
{
if (a[i] > 0)
{
return true;
}
}
for (i32 i = ((i32)min(a.size(), b.size())) - 1; i >= 0; i--)
{
if (a[i] > b[i])
{
return true;
}
else if (a[i] < b[i])
{
return false;
}
}
// Equal
return true;
}
u32 leading_zero(u64 x)
{
unsigned n = 0;
if (x == 0)
return 64;
constexpr auto top = u64(1) << 63;
while (x < top)
{
n++;
x <<= 1;
}
return n;
}
/* Function to get no of set bits in binary
representation of positive integer n */
u32 count_ones(u64 n)
{
unsigned int count = 0;
while (n)
{
count += n & 1;
n >>= 1;
}
return count;
}
u32 num_bits(std::vector<u64> const &repr)
{
u32 bits = LIMB_BITS * repr.size();
for (auto it = repr.crbegin(); it != repr.crend(); it++)
{
auto const limb = *it;
if (limb == 0)
bits -= 64;
else
{
bits -= leading_zero(limb);
break;
}
}
return bits;
}
void add_scalar(std::vector<u64> &repr, u64 value)
{
for (usize i = 0; value > 0; i++)
{
if (i >= repr.size())
{
repr.push_back(value);
break;
}
repr[i] = adc(repr[i], value, value);
}
}
void mul_scalar(std::vector<u64> &repr, const u64 scalar)
{
u64 carry = 0;
for (usize i = 0; i < repr.size(); i++)
{
repr[i] = mul_with_carry(repr[i], scalar, carry);
}
if (carry > 0)
{
repr.push_back(carry);
}
}
void right_shift(std::vector<u64> &repr, u64 shift)
{
auto const num_libs = repr.size();
if (num_libs == 0)
{
return;
}
for (usize i = 0; i < (num_libs - 1); i++)
{
repr[i] = (repr[i] >> shift) | (repr[i + 1] << (64 - shift));
}
repr[num_libs - 1] = repr[num_libs - 1] >> shift;
}
std::vector<i64> into_ternary_wnaf(std::vector<u64> const &repr)
{
std::vector<i64> res;
if (repr.size() == 0)
{
return res;
}
std::vector<u64> e = repr;
constexpr u64 WINDOW = u64(1);
constexpr u64 MIDPOINT = u64(1) << WINDOW;
constexpr i64 MIDPOINT_I64 = MIDPOINT;
constexpr u64 MASK = u64(1) << (WINDOW + 1);
while (!is_zero(e))
{
i64 z = 0;
if (is_odd(e))
{
z = MIDPOINT_I64 - (i64(e[0] % MASK));
if (z >= 0)
{
sub_noborrow(e, z);
}
else
{
add_nocarry(e, (-z));
}
}
res.push_back(z);
div2(e);
}
return res;
}
u32 calculate_hamming_weight(std::vector<u64> const &repr)
{
auto weight = 0;
for (auto it = repr.cbegin(); it != repr.cend(); it++)
{
weight += count_ones(*it);
}
return weight;
}