forked from microsoft/SEAL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator.h
2296 lines (1891 loc) · 80.7 KB
/
iterator.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
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#pragma once
#include "seal/util/common.h"
#include "seal/util/defines.h"
#include "seal/util/pointer.h"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <iterator>
#include <stdexcept>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>
namespace seal
{
class Ciphertext;
namespace util
{
class NTTTables;
/**
@par PolyIter, RNSIter, and CoeffIter
In this file we define a set of custom iterator classes ("SEAL iterators") that are used throughout Microsoft
SEAL for easier iteration over ciphertext polynomials, their RNS components, and the coefficients in the RNS
components. All SEAL iterators satisfy the C++ LegacyRandomAccessIterator requirements. SEAL iterators are ideal
to use with the SEAL_ITERATE macro, which expands to std::for_each_n in C++17 and to seal::util::seal_for_each_n
in C++14. All SEAL iterators derive from SEALIterBase.
The most important SEAL iterator classes behave as illustrated by the following diagram:
+-------------------+
| Pointer & Size | Construct +-----------------+
| or Ciphertext |------------>| (Const)PolyIter | Iterates over RNS polynomials in a ciphertext
+-------------------+ +--------+--------+ (coeff_modulus_size-many RNS components)
|
|
| Dereference
|
|
v
+----------------+ Construct +----------------+
| Pointer & Size |------------->| (Const)RNSIter | Iterates over RNS components in an RNS polynomial
+----------------+ +-------+--------+ (poly_modulus_degree-many coefficients)
|
|
| Dereference
|
|
v
+----------------+ Construct +------------------+
| Pointer & Size |------------>| (Const)CoeffIter | Iterates over coefficients (std::uint64_t) in a single
+----------------+ +---------+--------+ RNS polynomial component
|
|
| Dereference
|
|
v
+-------------------------+
| (const) std::uint64_t & |
+-------------------------+
@par PtrIter and StrideIter
PtrIter<T *> and StrideIter<T *> are both templated SEAL iterators that wrap raw pointers. The difference
between these two types is that advancing PtrIter<T *> always advances the wrapped pointer by one, whereas the
step size (stride) can be set to be anything for a StrideIter<T *>. CoeffIter is a typedef of
PtrIter<std::uint64_t *> and and RNSIter is almost the same as StrideIter<std::uint64_t *>, but still a
different type.
+----------+ Construct +-------------------+
| MyType * |------------->| PtrIter<MyType *> | Simple wrapper for raw pointers
+----------+ +----+----------+---+
| |
| |
Dereference | | PtrIter<MyType *>::ptr()
| | or implicit conversion
| |
v v
+----------+ +----------+
| MyType & | | MyType * |
+----------+ +----------+
+----------+ Construct +----------------------+
| MyType * |------------->| StrideIter<MyType *> | Simple wrapper for raw pointers with custom stride size
+----------+ +-----+----------+-----+
| |
| |
Dereference | | StrideIter<MyType *>::ptr()
| | or implicit conversion
| |
v v
+----------+ +----------+
| MyType & | | MyType * |
+----------+ +----------+
@par IterTuple
An extremely useful template class is the (variadic) IterTuple<...> that allows multiple SEAL iterators to be
zipped together. An IterTuple is itself a SEAL iterator and nested IterTuple types are used commonly in the
library. Dereferencing an IterTuple always yields an std::tuple, with each IterTuple element dereferenced.
Since an IterTuple can be constructed from an std::tuple holding the respective single-parameter constructor
arguments for each iterator, the dereferenced std::tuple can often be directly passed on to functions expecting
an IterTuple.
The individual components of an IterTuple can be accessed with the seal::util::get<i>(...) functions. The
behavior of IterTuple is summarized in the following diagram:
+-----------------------------------------+
| IterTuple<PolyIter, RNSIter, CoeffIter> |
+--------------------+--------------------+
|
|
| Dereference
|
|
v
+--------------------------------------------------+
| std::tuple<RNSIter, CoeffIter, std::uint64_t &>> |
+------+-------------------+-------------------+---+
| | |
| | |
| std::get<0> | std::get<1> | std::get<2>
| | |
| | |
v v v
+-------------+ +---------------+ +-----------------+
| RNSIter | | CoeffIter | | std::uint64_t & |
+-------------+ +---------------+ +-----------------+
Sometimes we have to use multiple nested iterator tuples. In this case accessing the nested iterators can be
tedious with nested get<...> calls. Consider the following, where encrypted1 and encrypted2 are Ciphertexts
and destination is either a Ciphertext or a PolyIter:
IterTuple<PolyIter, PolyIter> I(encrypted1, encrypted2);
IterTuple<decltype(I), PolyIter> J(I, destination);
auto encrypted1_iter = get<0>(get<0>(J));
auto encrypted2_iter = get<1>(get<0>(J));
An easier way is to use another form of get<...> that accepts multiple indices and accesses the structure in a
nested manner. For example, in the above we could also write:
auto encrypted1_iter = get<0, 0>(J));
auto encrypted2_iter = get<0, 1>(J));
Note that the innermost tuple index appears first in the list, i.e. the order is reversed from what appears in
a nested get<...> call. The reason for this reversal is that, when deducing what the iterators are, one first
examines at the innermost scope, and last the outermost scope, corresponding now to the order of the indices.
We have also provided similar functions for nested std::tuple objects, which is necessary when accessing
the dereferencing of a nested IterTuple.
@par Typedefs for common PtrIter types
It is very common to use the types PtrIter<Modulus *> and PtrIter<NTTTables *>. To simplify the
notation, we have set up typedefs for these: ModulusIter and NTTTablesIter. There are also constant versions
ConstModulusIter and ConstNTTTablesIter, wrapping pointers to constant Modulus and NTTTables instead.
@par Creating SEAL iterators
Iterators are easiest to create using the variadic iter function that, when given one or more arguments that can
naturally be converted to SEAL iterators, outputs an appropriate iterator, or iterator tuple. Consider again the
code snippet above, and how confusing the template parameters can become to write. Instead, we can simply write:
auto I = iter(encrypted1, encrypted2);
auto J = iter(I, destination);
auto encrypted1_iter = get<0, 0>(J));
auto encrypted2_iter = get<0, 1>(J));
There are three ways to create IterTuples from the iter function. The first way is by passing an IterTuple as
input, in which case iter outputs a copy of it; there should be no reason to do this. The second way is by
passing a variadic set of constructor arguments; iter will output an IterTuple consisting of SEAL iterators
that are compatible with the given constructor arguments. The third way is by passing a std::tuple consisting
of a variadic set of constructor arguments; the behavior is as in the second way.
@par Reversing direction with ReverseIter
In addition to the iterator types described above, we provide ReverseIter<SEALIter> that reverses the direction
of iteration. ReverseIter<SEALIter> dereferences to the same type as SEALIter: for example, dereferencing
ReverseIter<RNSIter> results in CoeffIter, not ReverseIter<CoeffIter>.
It is easy to create a ReverseIter from a given SEAL iterator using the function reverse_iter. For example,
reverse_iter(encrypted) will return a ReverseIter<PolyIter> if encrypted is either a PolyIter, or a Ciphertext.
When passed multiple arguments, reverse_iter returns an appropriate ReverseIter<IterTuple<...>>. For example,
reverse_iter(encrypted1, encrypted2) returns ReverseIter<IterTuple<PolyIter, PolyIter>> if encrypted1 and
encrypted2 are PolyIter or Ciphertext objects.
@par SEAL_ITERATE
SEAL iterators are made to be used with the SEAL_ITERATE macro to iterate over a certain number of steps, and
for each step call a given lambda function. In C++17 SEAL_ITERATE expands to std::for_each_n, and in C++14 it
expands to seal::util::seal_for_each_n -- a custom implementation. For example, the following snippet appears
in Evaluator::bfv_multiply:
SEAL_ITERATE(
iter(encrypted1, encrypted1_q, encrypted1_Bsk),
encrypted1_size,
behz_extend_base_convert_to_ntt);
Here an IterTuple<PolyIter, PolyIter, PolyIter> is created with the iter function; the argument types are
Ciphertext (encrypted1), PolyIter (encrypted1_q), and PolyIter (encrypted1_Bsk). The iterator is advanced
encrypted1_size times, and each time the lambda function behz_extend_base_convert_to_ntt is called with the
iterator tuple dereferenced. The lambda function starts as follows:
auto behz_extend_base_convert_to_ntt = [&](auto I) {
set_poly(get<0>(I), coeff_count, base_q_size, get<1>(I));
ntt_negacyclic_harvey_lazy(get<1>(I), base_q_size, base_q_ntt_tables);
...
});
Here the parameter I is of type IterTuple<RNSIter, RNSIter, RNSIter>. Inside the lambda function we first copy
the RNS polynomial from get<0>(I) (encrypted1) to get<1>(I) (encrypted1_q) and transform it to NTT form. We use
an overload of ntt_negacyclic_harvey_lazy that takes an RNSIter, size of the RNS base, and ConstNTTTablesIter as
arguments and converts each RNS component separately. Looking at seal/util/ntt.h we see that the function
ntt_negacyclic_harvey_lazy is again implemented using SEAL_ITERATE. Specifically, it contains the following:
SEAL_ITERATE(iter(operand, tables), coeff_modulus_size, [&](auto I) {
ntt_negacyclic_harvey_lazy(get<0>(I), get<1>(I));
});
Here iter outputs an IterTuple<RNSIter, ConstNTTTablesIter>. In this case the lambda function to be called
is defined inline. The argument I takes values IterTuple<CoeffIter, const NTTTables *>, and for each step the
CoeffIter overload of ntt_negacyclic_harvey_lazy is called, with a reference to a matching NTTTables object.
@par Coding conventions
There are two important coding conventions in the above code snippets that are to be observed:
1. Use I, J, K, ... for the lambda function parameters representing SEAL iterators. This is compact and
makes it very clear that the objects in question are SEAL iterators since such variable names should not
be used in SEAL in any other context.
2. Lambda functions passed to SEAL_ITERATE should almost always (see 3.) take a parameter of type auto. This
will produce simple looking code that performs well with the expected outcome.
3. The only exception to 2. is when SEAL_ITERATE operates on a single PtrIter<T *>: dereferencing returns a
T &, which may be important to forward by reference to the lambda function. For an example of this, see
seal::util::ntt_negacyclic_harvey in seal/util/ntt.h, where the lambda function parameter is auto &.
Note: IterTuple<PolyIter, CoeffIter> will dereference to std::tuple<RNSIter, std::uint64_t &>, which can
safely be passed by value to the lambda function. Hence, a parameter of type auto in the lambda function
will most likely work as expected.
Note: Another approach that would always behave correctly is by using a forwarding reference auto && as
the lambda function parameter. However, we feel that this unnecessarily complicates the code for a minor
benefit.
@par Iterator overloads of common functions
Some functions have overloads that directly take either CoeffIter, RNSIter, or PolyIter inputs, and apply the
operation in question to the entire structure as indicated by the iterator. For example, the function
seal::util::negate_poly_coeffmod can negate a single RNS component modulo a given Modulus (CoeffIter overload),
an entire RNS polynomial modulo an array of matching Modulus elements (RNSIter overload), or an array of RNS
polynomials (PolyIter overload).
@par Indexing with SeqIter
Sometimes inside SEAL_ITERATE lambda functions it is convenient to know the index of the iteration. This can be
done using a SeqIter<T> iterator. The template parameter is an arithmetic type for the index counter.
The easiest way to create SeqIter objects is using the seq_iter function. For example, seq_iter(0) returns a
SeqIter<int> object with initial value 0. Alternatively, the iter function will detect arithmetic types passed
to it and create SeqIter objects from them. For example, calling iter(0) is equivalent to calling seq_iter(0),
and this works also for multi-argument calls to iter. Dereferencing a SeqIter object returns the current value.
For opposite direction indexing, simply wrap a SeqIter into a ReverseIter, or call reverse_iter directly with
the start index.
@par Note on allocations
In the future we hope to use the parallel version of std::for_each_n, introduced in C++17. For this to work, be
mindful of how you use heap allocations in the lambda functions. Specifically, in heavy lambda functions it is
probably a good idea to call seal::util::allocate inside the lambda function for any allocations needed, rather
than using allocations captured from outside the lambda function.
@par Iterators to temporary allocations
In many cases one may want to allocate a temporary buffer and create an iterator pointing to it. However, care
must be taken to use the correct size parameters now both for the allocation, as well as for setting up the
iterator. For this reason, we provide a few helpful macros that set up the Pointer and only expose the iterator
to the function. For example, instead of writing the following error-prone code:
auto temp_alloc(allocate_poly_array(count, poly_modulus_degree, coeff_modulus_size, pool));
PolyIter temp(temp_alloc.get(), poly_modulus_degree, coeff_modulus_size);
we can simply write:
SEAL_ALLOCATE_GET_POLY_ITER(temp, count, poly_modulus_degree, coeff_modulus_size, pool);
However, the latter does not expose the name of the allocation itself. There are similar macros for allocating
buffers and setting up PtrIter<T *>, StrideIter<T *>, RNSIter, and CoeffIter objects as well.
*/
class SEALIterBase
{};
template <typename T, typename>
class SeqIter;
template <typename T>
class PtrIter;
template <typename T>
class StrideIter;
class RNSIter;
class ConstRNSIter;
class PolyIter;
class ConstPolyIter;
template <typename SEALIter>
class ReverseIter;
template <typename... SEALIters>
class IterTuple;
using CoeffIter = PtrIter<std::uint64_t *>;
using ConstCoeffIter = PtrIter<const std::uint64_t *>;
using ConstModulusIter = PtrIter<const Modulus *>;
using ConstNTTTablesIter = PtrIter<const NTTTables *>;
using ModulusIter = PtrIter<Modulus *>;
using NTTTablesIter = PtrIter<NTTTables *>;
namespace iterator_internal
{
template <typename Enable, typename... Ts>
struct IterType;
template <typename T>
struct IterType<std::enable_if_t<std::is_arithmetic<T>::value>, T>
{
using type = SeqIter<T, void>;
};
template <typename T>
struct IterType<
std::enable_if_t<std::is_base_of<SEALIterBase, std::decay_t<T>>::value && !std::is_pointer<T>::value>,
T>
{
using type = std::decay_t<T>;
};
template <>
struct IterType<void, Ciphertext &>
{
using type = PolyIter;
};
template <>
struct IterType<void, const Ciphertext &>
{
using type = ConstPolyIter;
};
template <typename T>
struct IterType<void, const T *>
{
using type = PtrIter<const T *>;
};
template <typename T>
struct IterType<void, T *>
{
using type = PtrIter<T *>;
};
template <typename T>
struct IterType<void, const T *&>
{
using type = PtrIter<const T *>;
};
template <typename T>
struct IterType<void, T *&>
{
using type = PtrIter<T *>;
};
template <typename T>
struct IterType<void, std::vector<T> &>
{
using type = PtrIter<T *>;
};
template <typename T>
struct IterType<void, const std::vector<T> &>
{
using type = PtrIter<const T *>;
};
template <typename T>
struct IterType<void, Pointer<T> &>
{
using type = PtrIter<T *>;
};
template <typename T>
struct IterType<void, const Pointer<T> &>
{
using type = PtrIter<T *>;
};
template <typename T>
struct IterType<void, ConstPointer<T> &>
{
using type = PtrIter<const T *>;
};
template <typename T>
struct IterType<void, const ConstPointer<T> &>
{
using type = PtrIter<const T *>;
};
} // namespace iterator_internal
template <typename T, typename = std::enable_if_t<std::is_arithmetic<T>::value>>
class SeqIter : public SEALIterBase
{
public:
using self_type = SeqIter;
// Standard iterator typedefs
using value_type = T;
using pointer = void;
using reference = const value_type &;
using iterator_category = std::random_access_iterator_tag;
using difference_type = std::ptrdiff_t;
SeqIter() = default;
SeqIter(T start) : value_(start)
{}
SeqIter(const self_type ©) = default;
self_type &operator=(const self_type &assign) = default;
SEAL_NODISCARD inline reference operator*() const noexcept
{
return value_;
}
template <typename SizeT>
SEAL_NODISCARD inline value_type operator[](SizeT n) const noexcept
{
return value_ + static_cast<T>(n);
}
inline self_type &operator++() noexcept
{
value_++;
return *this;
}
inline self_type operator++(int) noexcept
{
self_type result(value_);
value_++;
return result;
}
inline self_type &operator--() noexcept
{
value_--;
return *this;
}
inline self_type operator--(int) noexcept
{
self_type result(value_);
value_--;
return result;
}
template <typename SizeT>
inline self_type &operator+=(SizeT n) noexcept
{
value_ += static_cast<T>(n);
return *this;
}
template <typename SizeT>
SEAL_NODISCARD inline self_type operator+(SizeT n) const noexcept
{
return value_ + static_cast<T>(n);
}
template <typename SizeT>
inline self_type &operator-=(SizeT n) noexcept
{
value_ -= static_cast<T>(n);
return *this;
}
template <typename SizeT>
SEAL_NODISCARD inline self_type operator-(SizeT n) const noexcept
{
return value_ - static_cast<T>(n);
}
SEAL_NODISCARD inline difference_type operator-(const self_type &b) const noexcept
{
return static_cast<difference_type>(value_) - static_cast<difference_type>(b.value_);
}
SEAL_NODISCARD inline bool operator==(const self_type &compare) const noexcept
{
return value_ == *compare;
}
template <typename S, typename = std::enable_if_t<std::is_arithmetic<S>::value>>
SEAL_NODISCARD inline bool operator==(S compare) const noexcept
{
return value_ == compare;
}
SEAL_NODISCARD inline bool operator!=(const self_type &compare) const noexcept
{
return !(*this == compare);
}
template <typename S, typename = std::enable_if_t<std::is_arithmetic<S>::value>>
SEAL_NODISCARD inline bool operator!=(S compare) const noexcept
{
return !(*this == compare);
}
SEAL_NODISCARD inline bool operator<(const self_type &compare) const noexcept
{
return value_ < *compare;
}
template <typename S, typename = std::enable_if_t<std::is_arithmetic<S>::value>>
SEAL_NODISCARD inline bool operator<(S compare) const noexcept
{
return value_ < compare;
}
SEAL_NODISCARD inline bool operator>(const self_type &compare) const noexcept
{
return value_ > *compare;
}
template <typename S, typename = std::enable_if_t<std::is_arithmetic<S>::value>>
SEAL_NODISCARD inline bool operator>(S compare) const noexcept
{
return value_ > compare;
}
SEAL_NODISCARD inline bool operator<=(const self_type &compare) const noexcept
{
return !(value_ > *compare);
}
template <typename S, typename = std::enable_if_t<std::is_arithmetic<S>::value>>
SEAL_NODISCARD inline bool operator<=(S compare) const noexcept
{
return !(value_ > compare);
}
SEAL_NODISCARD inline bool operator>=(const self_type &compare) const noexcept
{
return !(value_ < *compare);
}
template <typename S, typename = std::enable_if_t<std::is_arithmetic<S>::value>>
SEAL_NODISCARD inline bool operator>=(S compare) const noexcept
{
return !(value_ < compare);
}
SEAL_NODISCARD inline operator T() const noexcept
{
return value_;
}
private:
T value_ = 0;
};
template <typename T>
SEAL_NODISCARD inline auto seq_iter(T value = 0) -> SeqIter<T>
{
return value;
}
template <typename T>
class PtrIter<T *> : public SEALIterBase
{
public:
using self_type = PtrIter<T *>;
// Standard iterator typedefs
using value_type = T &;
using pointer = T *;
using reference = value_type;
using iterator_category = std::random_access_iterator_tag;
using difference_type = std::ptrdiff_t;
PtrIter() = default;
template <typename S>
PtrIter(S *ptr) noexcept : ptr_(ptr)
{}
template <typename S>
PtrIter(PtrIter<S *> copy) noexcept : ptr_(copy.ptr())
{}
template <typename S>
PtrIter(std::vector<S> &arr) noexcept : PtrIter(arr.data())
{}
template <typename S>
PtrIter(const std::vector<S> &arr) noexcept : PtrIter(arr.data())
{}
template <typename S>
PtrIter(const Pointer<S> &arr) noexcept : PtrIter(arr.get())
{}
template <typename S>
inline self_type &operator=(const PtrIter<S *> &assign) noexcept
{
ptr_ = assign.ptr();
return *this;
}
SEAL_NODISCARD inline reference operator*() const noexcept
{
return *ptr_;
}
template <typename SizeT>
SEAL_NODISCARD inline reference operator[](SizeT n) const noexcept
{
return ptr_[n];
}
inline self_type &operator++() noexcept
{
ptr_++;
return *this;
}
inline self_type operator++(int) noexcept
{
self_type result(ptr_);
ptr_++;
return result;
}
inline self_type &operator--() noexcept
{
ptr_--;
return *this;
}
inline self_type operator--(int) noexcept
{
self_type result(ptr_);
ptr_--;
return result;
}
template <typename SizeT>
inline self_type &operator+=(SizeT n) noexcept
{
ptr_ += n;
return *this;
}
template <typename SizeT>
SEAL_NODISCARD inline self_type operator+(SizeT n) const noexcept
{
return ptr_ + n;
}
template <typename SizeT>
inline self_type &operator-=(SizeT n) noexcept
{
ptr_ -= n;
return *this;
}
template <typename SizeT>
SEAL_NODISCARD inline self_type operator-(SizeT n) const noexcept
{
return ptr_ - n;
}
template <typename S>
SEAL_NODISCARD inline difference_type operator-(const PtrIter<S *> &b) const noexcept
{
return std::distance(b.ptr(), ptr_);
}
template <typename S>
SEAL_NODISCARD inline bool operator==(const PtrIter<S *> &compare) const noexcept
{
return ptr_ == compare.ptr();
}
template <typename S>
SEAL_NODISCARD inline bool operator!=(const PtrIter<S *> &compare) const noexcept
{
return !(*this == compare);
}
template <typename S>
SEAL_NODISCARD inline bool operator<(const PtrIter<S *> &compare) const noexcept
{
return ptr_ < compare.ptr();
}
template <typename S>
SEAL_NODISCARD inline bool operator>(const PtrIter<S *> &compare) const noexcept
{
return ptr_ > compare.ptr();
}
template <typename S>
SEAL_NODISCARD inline bool operator<=(const PtrIter<S *> &compare) const noexcept
{
return !(ptr_ > compare.ptr());
}
template <typename S>
SEAL_NODISCARD inline bool operator>=(const PtrIter<S *> &compare) const noexcept
{
return !(ptr_ < compare.ptr());
}
SEAL_NODISCARD explicit inline operator bool() const noexcept
{
return nullptr != ptr_;
}
SEAL_NODISCARD inline reference operator->() const noexcept
{
return **this;
}
SEAL_NODISCARD inline pointer ptr() const noexcept
{
return ptr_;
}
SEAL_NODISCARD inline operator pointer() const noexcept
{
return ptr_;
}
private:
pointer ptr_ = nullptr;
};
template <typename T>
class StrideIter<T *> : public SEALIterBase
{
public:
using self_type = StrideIter;
// Standard iterator typedefs
using value_type = PtrIter<T *>;
using pointer = T *;
using reference = const value_type &;
using iterator_category = std::random_access_iterator_tag;
using difference_type = std::ptrdiff_t;
StrideIter() = default;
template <typename S>
StrideIter(S *ptr, std::size_t stride) noexcept : ptr_it_(ptr), stride_(stride)
{}
template <typename S>
StrideIter(StrideIter<S *> copy) noexcept : ptr_it_(copy.ptr()), stride_(copy.stride())
{}
template <typename S>
StrideIter(std::vector<S> &arr, std::size_t stride) noexcept : StrideIter(arr.data(), stride)
{}
template <typename S>
StrideIter(const std::vector<S> &arr, std::size_t stride) noexcept : StrideIter(arr.data(), stride)
{}
template <typename S>
StrideIter(const Pointer<S> &arr, std::size_t stride) noexcept : StrideIter(arr.get(), stride)
{}
template <typename S>
inline self_type &operator=(const StrideIter<S *> &assign) noexcept
{
ptr_it_ = assign.ptr();
stride_ = assign.stride();
return *this;
}
SEAL_NODISCARD inline reference operator*() const noexcept
{
return ptr_it_;
}
template <typename SizeT>
SEAL_NODISCARD inline value_type operator[](SizeT n) const noexcept
{
self_type result(*this);
result += static_cast<difference_type>(n);
return *result;
}
inline self_type &operator++() noexcept
{
ptr_it_ += static_cast<difference_type>(stride_);
return *this;
}
inline self_type operator++(int) noexcept
{
self_type result(*this);
ptr_it_ += static_cast<difference_type>(stride_);
return result;
}
inline self_type &operator--() noexcept
{
ptr_it_ -= static_cast<difference_type>(stride_);
return *this;
}
inline self_type operator--(int) noexcept
{
self_type result(*this);
ptr_it_ -= static_cast<difference_type>(stride_);
return result;
}
template <typename SizeT>
inline self_type &operator+=(SizeT n) noexcept
{
ptr_it_ += static_cast<difference_type>(n) * static_cast<difference_type>(stride_);
return *this;
}
template <typename SizeT>
SEAL_NODISCARD inline self_type operator+(SizeT n) const noexcept
{
self_type result(*this);
result.ptr_it_ += static_cast<difference_type>(n) * static_cast<difference_type>(stride_);
return result;
}
template <typename SizeT>
inline self_type &operator-=(SizeT n) noexcept
{
ptr_it_ -= static_cast<difference_type>(n) * static_cast<difference_type>(stride_);
return *this;
}
template <typename SizeT>
SEAL_NODISCARD inline self_type operator-(SizeT n) const noexcept
{
return *this + (-static_cast<difference_type>(n));
}
template <typename S>
SEAL_NODISCARD inline difference_type operator-(const StrideIter<S *> &b) const
{
#ifdef SEAL_DEBUG
if (!stride_)
{
throw std::logic_error("stride cannot be zero");
}
if (stride_ != b.stride())
{
throw std::invalid_argument("incompatible iterators");
}
#endif
return (ptr_it_ - *b) / static_cast<difference_type>(stride_);
}
template <typename S>
SEAL_NODISCARD inline bool operator==(const StrideIter<S *> &compare) const noexcept
{
return ptr_it_ == *compare;
}
template <typename S>
SEAL_NODISCARD inline bool operator!=(const StrideIter<S *> &compare) const noexcept
{
return !(*this == compare);
}
template <typename S>
SEAL_NODISCARD inline bool operator<(const StrideIter<S *> &compare) const noexcept
{
return ptr_it_ < *compare;
}
template <typename S>
SEAL_NODISCARD inline bool operator>(const StrideIter<S *> &compare) const noexcept
{
return ptr_it_ > *compare;
}
template <typename S>
SEAL_NODISCARD inline bool operator<=(const StrideIter<S *> &compare) const noexcept
{
return !(ptr_it_ > *compare);
}
template <typename S>
SEAL_NODISCARD inline bool operator>=(const StrideIter<S *> &compare) const noexcept
{
return !(ptr_it_ < *compare);
}
SEAL_NODISCARD explicit inline operator bool() const noexcept
{
return static_cast<bool>(ptr_it_);
}
SEAL_NODISCARD inline reference operator->() const noexcept
{
return **this;
}
SEAL_NODISCARD inline std::size_t stride() const noexcept
{
return stride_;
}
SEAL_NODISCARD inline pointer ptr() const noexcept
{
return ptr_it_.ptr();
}
SEAL_NODISCARD inline operator pointer() const noexcept
{
return ptr_it_.operator pointer();
}
private:
PtrIter<T *> ptr_it_ = {};
std::size_t stride_ = 0;
};
class RNSIter : public SEALIterBase
{
public:
friend class PolyIter;
using self_type = RNSIter;
// Standard iterator typedefs
using value_type = CoeffIter;
using pointer = void;
using reference = const value_type &;
using iterator_category = std::random_access_iterator_tag;
using difference_type = std::ptrdiff_t;
RNSIter() : ptr_it_(), step_size_(0)
{}
RNSIter(std::uint64_t *ptr, std::size_t poly_modulus_degree) : ptr_it_(ptr), step_size_(poly_modulus_degree)
{}
RNSIter(const self_type ©) = default;
self_type &operator=(const self_type &assign) = default;
template <typename S>
RNSIter(const StrideIter<S *> &stride_it) : RNSIter(stride_it.ptr(), stride_it.stride())
{}
template <typename S>
inline self_type &operator=(const StrideIter<S *> &assign)
{
ptr_it_ = assign;
step_size_ = assign.stride();
return *this;
}
SEAL_NODISCARD inline reference operator*() const noexcept
{
return ptr_it_;
}
template <typename SizeT>
SEAL_NODISCARD inline value_type operator[](SizeT n) const noexcept
{
self_type result(*this);
result += static_cast<difference_type>(n);
return *result;
}
inline self_type &operator++() noexcept