-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patharray.h
More file actions
461 lines (404 loc) · 10.1 KB
/
array.h
File metadata and controls
461 lines (404 loc) · 10.1 KB
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/* SCL --- Secure Computation Library
* Copyright (C) 2024 Anders Dalskov
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef SCL_MATH_ARRAY_H
#define SCL_MATH_ARRAY_H
#include <array>
#include <cstddef>
#include <sstream>
#include <type_traits>
#include "scl/serialization/serializer.h"
#include "scl/util/prg.h"
namespace scl {
namespace math {
/// @cond
template <typename T>
concept PrefixIncrementable = requires(T a) { ++a; };
template <typename T>
concept PrefixDecrementable = requires(T a) { --a; };
template <typename T, typename V>
concept Multipliable = requires(T a, V b) { (a) * (b); };
template <typename T>
concept Divisble = requires(T a, T b) { (a) / (b); };
template <typename T, typename V>
struct MultiplyResultTypeTrait {
using Type = decltype(std::declval<T>() * std::declval<V>());
};
template <typename T, typename V>
using MultiplyResultType = typename MultiplyResultTypeTrait<T, V>::Type;
template <typename T>
concept Invertible = requires(T& a) { a.invert(); };
/// @endcond
/**
* @brief Array of values, e.g., group elements.
* @tparam T the array element type.
* @tparam N the number of elements.
*
* Array is effectively a wrapper around <code>std::array<T, N></code> with
* added functionality that allows operating on Array objects as if they where
* group or ring elements. As such, Array behaves sort of like a direct product
* of N copies of the same group.
*/
template <typename T, std::size_t N>
class Array final {
public:
/**
* @brief Binary size of this Array.
*/
constexpr static std::size_t byteSize() {
return T::byteSize() * N;
}
/**
* @brief Read an array from a buffer.
*/
static Array<T, N> read(const unsigned char* src) {
Array<T, N> p;
for (std::size_t i = 0; i < N; ++i) {
p.m_values[i] = T::read(src + i * T::byteSize());
}
return p;
} // LCOV_EXCL_LINE
/**
* @brief Create an array filled with random elements.
*/
static Array<T, N> random(util::PRG& prg)
requires requires() { T::random(prg); }
{
Array<T, N> p;
for (std::size_t i = 0; i < N; ++i) {
p.m_values[i] = T::random(prg);
}
return p;
} // LCOV_EXCL_LINE
/**
* @brief Get an Array filled with the multiplicative identity
*/
static Array<T, N> one()
requires requires() { T::one(); }
{
return Array<T, N>(T::one());
}
/**
* @brief Get an Array filled with the additive identity.
*/
static Array<T, N> zero() {
return Array<T, N>(T::zero());
}
/**
* @brief Default constructor.
*/
Array() {}
/**
* @brief Construct an Array filled with copies of the same element.
* @param element the element.
*/
Array(const T& element) {
m_values.fill(element);
}
/**
* @brief Construct an Array filled with copies of the same element.
*
* This function will attempt to construct a \p T element using \p value, and
* then fill all slots with this value.
*/
explicit Array(int value) : Array(T{value}) {}
/**
* @brief Copy construct an Array from another array.
* @param arr the array.
*/
Array(const std::array<T, N>& arr) : m_values{arr} {}
/**
* @brief Move construct an Array from another array.
* @param arr the array.
*/
Array(std::array<T, N>&& arr) : m_values{std::move(arr)} {}
/**
* @brief Add another Array to this Array.
*/
Array& operator+=(const Array& other) {
for (std::size_t i = 0; i < N; ++i) {
m_values[i] += other.m_values[i];
}
return *this;
}
/**
* @brief Add two arrays.
*/
friend Array operator+(const Array& lhs, const Array& rhs) {
Array tmp(lhs);
return tmp += rhs;
}
/**
* @brief Prefix increment operator.
*/
Array& operator++()
requires PrefixIncrementable<T>
{
for (auto& v : m_values) {
++v;
}
return *this;
}
/**
* @brief Postfix increment operator.
*/
friend Array operator++(Array& arr, int)
requires PrefixIncrementable<T>
{
Array tmp(arr);
for (auto& v : arr.m_values) {
++v;
}
return tmp;
}
/**
* @brief Subtract an Array from this.
*/
Array& operator-=(const Array& other) {
for (std::size_t i = 0; i < N; ++i) {
m_values[i] -= other.m_values[i];
}
return *this;
}
/**
* @brief Subtract two Arrays.
*/
friend Array operator-(const Array& lhs, const Array& rhs) {
Array tmp(lhs);
return tmp -= rhs;
}
/**
* @brief Prefix decrement operator.
*/
Array& operator--()
requires PrefixDecrementable<T>
{
for (auto& v : m_values) {
--v;
}
return *this;
}
/**
* @brief Postfix decrement operator.
*/
friend Array operator--(Array& arr, int)
requires PrefixDecrementable<T>
{
Array tmp(arr);
for (auto& v : arr.m_values) {
--v;
}
return tmp;
}
/**
* @brief Negate this Array.
*/
Array& negate() {
for (std::size_t i = 0; i < N; ++i) {
m_values[i].Negate();
}
return *this;
}
/**
* @brief Multiply this Array with a scalar.
*/
template <typename S>
Array<T, N>& operator*=(const S& scalar)
requires Multipliable<T, S>
{
for (std::size_t i = 0; i < N; i++) {
m_values[i] *= scalar;
}
return *this;
}
/**
* @brief Multiply two Arrays entry-wise.
*/
template <typename S>
Array<T, N>& operator*=(const Array<S, N>& other)
requires Multipliable<T, S>
{
for (std::size_t i = 0; i < N; ++i) {
m_values[i] *= other[i];
}
return *this;
}
/**
* @brief Multiply a scalar with this Array.
*/
template <typename S>
Array<T, N> operator*(const S& scalar) const
requires Multipliable<T, S>
{
auto copy = *this;
return copy *= scalar;
}
/**
* @brief Multiply two Arrays entry-wise.
*/
template <typename V>
friend Array<MultiplyResultType<T, V>, N> operator*(const Array<T, N>& lhs,
const Array<V, N>& rhs)
requires Multipliable<T, V>
{
Array<MultiplyResultType<T, V>, N> tmp;
for (std::size_t i = 0; i < N; i++) {
tmp[i] = lhs[i] * rhs[i];
}
return tmp;
}
/**
* @brief Invert all entries in this Array.
*/
Array<T, N>& invert()
requires Invertible<T>
{
for (std::size_t i = 0; i < N; ++i) {
m_values[i].invert();
}
return *this;
}
/**
* @brief Compute the inverse of each entry in this Array.
*/
Array<T, N> Inverse() const
requires Invertible<T>
{
Array<T, N> p = *this;
return p.invert();
}
/**
* @brief Divide this Array by another Array entry-wise.
*/
Array<T, N> operator/=(const Array<T, N>& other)
requires Divisble<T>
{
for (std::size_t i = 0; i < N; ++i) {
m_values[i] /= other[i];
}
return *this;
}
/**
* @brief Divide this Array by another Array entry-wise.
*/
Array<T, N> operator/(const Array<T, N>& other) const
requires Divisble<T>
{
Array<T, N> r = *this;
r /= other;
return r;
} // LCOV_EXCL_LINE
/**
* @brief Get the value at a particular entry in the product element.
*/
T& operator[](std::size_t index) {
return m_values[index];
}
/**
* @brief Get the value at a particular entry in the product element.
*/
T operator[](std::size_t index) const {
return m_values[index];
}
/**
* @brief Compare two product elements.
*/
bool equal(const Array& other) const {
bool eq = true;
for (std::size_t i = 0; i < N; ++i) {
eq = eq && (m_values[i] == other.m_values[i]);
}
return eq;
}
/**
* @brief Equality operator for Array.
*/
friend bool operator==(const Array& lhs, const Array& rhs) {
return lhs.equal(rhs);
}
/**
* @brief In-equality operator for Array.
*/
friend bool operator!=(const Array& lhs, const Array& rhs) {
return !(lhs == rhs);
}
/**
* @brief Get a string representation of this product element.
*/
std::string toString() const {
std::stringstream ss;
ss << "P{";
for (std::size_t i = 0; i < N - 1; ++i) {
ss << m_values[i] << ", ";
}
ss << m_values[N - 1] << "}";
return ss.str();
}
/**
* @brief Stream printing operator for Array.
*/
friend std::ostream& operator<<(std::ostream& os, const Array& array) {
return os << array.toString();
}
/**
* @brief Write this product element to a buffer.
*/
void write(unsigned char* dest) const {
for (std::size_t i = 0; i < N; ++i) {
m_values[i].write(dest + i * T::byteSize());
}
}
private:
std::array<T, N> m_values;
};
} // namespace math
namespace seri {
/**
* @brief Serializer specialization for product types.
*/
template <typename GROUP, std::size_t N>
struct Serializer<math::Array<GROUP, N>> {
/**
* @brief Get the serialized size of a product type.
*/
static constexpr std::size_t sizeOf(
const math::Array<GROUP, N>& /* ignored */) {
return math::Array<GROUP, N>::byteSize();
}
/**
* @brief Write a product element to a buffer.
* @param prod the element.
* @param buf the buffer.
*/
static std::size_t write(const math::Array<GROUP, N>& prod,
unsigned char* buf) {
prod.write(buf);
return sizeOf(prod);
}
/**
* @brief Read a product element from a buffer.
* @param prod the output.
* @param buf the buffer.
*/
static std::size_t read(math::Array<GROUP, N>& prod,
const unsigned char* buf) {
prod = math::Array<GROUP, N>::read(buf);
return sizeOf(prod);
}
};
} // namespace seri
} // namespace scl
#endif // SCL_MATH_ARRAY_H