forked from data61/MP-SPDZ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ring_Element.h
211 lines (167 loc) · 5.57 KB
/
Ring_Element.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
#ifndef _Ring_Element
#define _Ring_Element
/* Defines an element of the ring modulo a prime pr
* - Note here pr is an odd prime
* We also assume that pr splits completely over the underlying
* ring. Equivalently (as the ring is of m'th roots of unity),
* we have that pr-1 is divisible by m.
* - If m is not a power of two we also require pr-1 is divisible by
* some power of two, this is to get Bluestein's FFT working
* Thus we can define both a polynomial and an evaluation
* representation
*/
enum RepType { polynomial, evaluation };
#include "FHE/FFT_Data.h"
#include "Tools/octetStream.h"
#include "Tools/random.h"
#include <FHE/Generator.h>
#include <iostream>
#include <vector>
using namespace std;
class RingWriteIterator;
class RingReadIterator;
class Ring_Element
{
RepType rep;
/* FFTD is defined as a pointer so each different Ring_Element
* can be wrt a different prime if need be
* - Recall FFTD also contains data about the Ring
*/
const FFT_Data *FFTD;
/* In either representation we hold the element as an array of
* modp's of length Ring.phi_m()
*/
vector<modp> element;
public:
// Used to basically make sure *this is able to cope
// with being assigned to by something of "type" e
void partial_assign(const Ring_Element& e)
{ rep=e.rep; FFTD=e.FFTD;
if (FFTD)
element.resize((*FFTD).phi_m());
}
void prepare(const Ring_Element& e);
void prepare_push();
void allocate();
void set_data(const FFT_Data& prd) { FFTD=&prd; }
const FFT_Data& get_FFTD() const { return *FFTD; }
const Zp_Data& get_prD() const { return (*FFTD).get_prD(); }
const bigint& get_prime() const { return (*FFTD).get_prime(); }
void assign_zero();
void assign_one();
/* Careful calling this one, as FFTD will not be defined */
Ring_Element(RepType r=polynomial) : FFTD(0) { rep=r; }
Ring_Element(const FFT_Data& prd,RepType r=polynomial);
template<class T>
Ring_Element(const FFT_Data& prd, RepType r, const vector<T>& other)
{
assert(size_t(prd.num_slots()) == other.size());
FFTD = &prd;
rep = r;
for (auto& x : other)
element.push_back({x, FFTD->get_prD()});
}
/* Functional Operators */
void negate();
friend void add(Ring_Element& ans,const Ring_Element& a,const Ring_Element& b);
friend void sub(Ring_Element& ans,const Ring_Element& a,const Ring_Element& b);
friend void mul(Ring_Element& ans,const Ring_Element& a,const Ring_Element& b);
friend void mul(Ring_Element& ans,const Ring_Element& a,const modp& b);
Ring_Element mul_by_X_i(int i) const;
Ring_Element& operator+=(const Ring_Element& other);
Ring_Element& operator-=(const Ring_Element& other);
Ring_Element& operator*=(const Ring_Element& other);
Ring_Element& operator*=(const modp& other);
void randomize(PRNG& G,bool Diag=false);
bool equals(const Ring_Element& a) const;
bool is_zero() const;
// This is a NOP in cases where we cannot do a FFT
void change_rep(RepType r);
// Converting to and from a vector of bigint/int's
// I/O is assumed to be in poly rep, so from_vec it internally alters
// the representation to the current representation
vector<bigint> to_vec_bigint() const;
void to_vec_bigint(vector<bigint>& v) const;
ConversionIterator get_iterator() const;
friend class RingReadIterator;
RingReadIterator get_copy_iterator() const;
friend class RingWriteIterator;
RingWriteIterator get_write_iterator();
template <class T>
void from(const Generator<T>& generator);
template <class T>
void from(const vector<T>& source)
{
from(Iterator<T>(source));
}
// This gets the constant term of the poly rep as a modp element
modp get_constant() const;
modp get_element(int i) const
{
if (element.empty())
return {};
else
return element[i];
}
void set_element(int i,const modp& a)
{
allocate();
element[i] = a;
}
/* Pack and unpack into an octetStream
* For unpack we assume the FFTD has been assigned correctly already
*/
void pack(octetStream& o) const;
void unpack(octetStream& o);
void check_rep();
void check_size() const;
void output(ostream& s) const;
void input(istream& s);
void check(const FFT_Data& FFTD) const;
size_t report_size(ReportType type) const;
};
class RingWriteIterator : public WriteConversionIterator
{
Ring_Element& element;
RepType rep;
public:
RingWriteIterator(Ring_Element& element) :
WriteConversionIterator(element.element, element.FFTD->get_prD()),
element(element), rep(element.rep)
{
element.rep = polynomial;
element.allocate();
}
~RingWriteIterator() { element.change_rep(rep); }
};
class RingReadIterator : public ConversionIterator
{
Ring_Element element;
public:
RingReadIterator(const Ring_Element& element) :
ConversionIterator(this->element.element, element.FFTD->get_prD()),
element(element)
{
this->element.change_rep(polynomial);
this->element.allocate();
}
};
inline void mul(Ring_Element& ans,const modp& a,const Ring_Element& b)
{ mul(ans,b,a); }
template <class T>
void Ring_Element::from(const Generator<T>& generator)
{
RepType t=rep;
rep=polynomial;
T tmp;
modp tmp2;
prepare_push();
for (int i=0; i<(*FFTD).phi_m(); i++)
{
generator.get(tmp);
tmp2.convert_destroy(tmp, (*FFTD).get_prD());
element.push_back(tmp2);
}
change_rep(t);
}
#endif