forked from data61/MP-SPDZ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plaintext.h
196 lines (151 loc) · 6 KB
/
Plaintext.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
#ifndef _Plaintext
#define _Plaintext
/*
* Defines plaintext either as a vector of field elements (gfp or gf2n_short)
* or as a vector of int (for gf2n_short) or bigints (for gfp)
*
* The first format is the actual data we want, the second format
* is the data we use to encrypt/decrypt. Passing between the two
* depends on whether we have a gfp or a gf2n_short base type.
* - The former uses the FFT (in the case of special m/p values
* and using FFT_Data), or naive methods (for general m/p values
* and using PPData), the second uses a precomputed linear
* map
*
* We hold data however in vector format, as this is easier to
* deal with
*/
#include "FHE/Generator.h"
#include "Math/fixint.h"
#include <vector>
using namespace std;
class FHE_PK;
class Rq_Element;
// Forward declaration as apparently this is needed for friends in templates
template<class T,class FD,class S> class Plaintext;
template<class T,class FD,class S> void add(Plaintext<T,FD,S>& z,const Plaintext<T,FD,S>& x,const Plaintext<T,FD,S>& y);
template<class T,class FD,class S> void sub(Plaintext<T,FD,S>& z,const Plaintext<T,FD,S>& x,const Plaintext<T,FD,S>& y);
template<class T,class FD,class S> void mul(Plaintext<T,FD,S>& z,const Plaintext<T,FD,S>& x,const Plaintext<T,FD,S>& y);
template<class T,class FD,class S> void sqr(Plaintext<T,FD,S>& z,const Plaintext<T,FD,S>& x);
enum condition { Full, Diagonal, Bits };
enum PT_Type { Polynomial, Evaluation, Both };
template<class T,class FD,class _>
class Plaintext
{
typedef typename FD::poly_type S;
int n_slots;
int degree;
mutable vector<T> a; // The thing in evaluation/FFT form
mutable vector<S> b; // Now in polynomial form
mutable PT_Type type;
/* We keep pointers to the basic data here
* - FD is of type FFT_Data or PPData if T is of type gfp
* - FD is of type P2Data if T is of type gf2n_short
*/
const FD *Field_Data;
void set_sizes();
public:
const FD& get_field() const { return *Field_Data; }
unsigned int num_slots() const { return n_slots; }
void assign(const Plaintext& p)
{ Field_Data=p.Field_Data;
a=p.a; b=p.b; type=p.type;
n_slots = p.n_slots;
degree = p.degree;
}
Plaintext(const FD& FieldD, PT_Type type = Polynomial)
{ Field_Data=&FieldD; set_sizes(); allocate(type); }
Plaintext(const FD& FieldD, const Rq_Element& other);
void allocate(PT_Type type) const;
void allocate() const { allocate(type); }
void allocate_slots(const bigint& value);
int get_min_alloc();
// Access evaluation representation
T element(int i) const
{ if (type==Polynomial)
{ from_poly(); }
return a[i];
}
void set_element(int i,const T& e)
{ if (type==Polynomial)
{ throw not_implemented(); }
a.resize(n_slots);
a[i]=e;
type=Evaluation;
}
// Access poly representation
const S& coeff(int i) const
{ if (type!=Polynomial)
{ to_poly(); }
return b[i];
}
void set_coeff(int i,const S& e)
{
to_poly();
type=Polynomial;
b[i]=e;
}
const S& operator[](int i) const { return coeff(i); }
// Assumes v is of the correct length (phi_m) for the given ring
// and is already reduced modulo the correct prime etc
void set_poly(const vector<S>& v)
{ type=Polynomial; b=v; }
const vector<S>& get_poly() const
{
to_poly();
return b;
}
Iterator<S> get_iterator() const { to_poly(); return b; }
void from(const Generator<bigint>& source) const;
// This sets a poly from a vector of bigint's which needs centering
// modulo mod, before assigning (used in decryption)
// vv[i] is already assumed reduced modulo mod though but in
// range [0,...,mod)
void set_poly_mod(const vector<bigint>& vv,const bigint& mod)
{
set_poly_mod(Iterator<bigint>(vv), mod);
}
void set_poly_mod(const Generator<bigint>& generator, const bigint& mod);
// Converts between Evaluation,Polynomial and Both representations
// Marked as const because does not change value, only changes the
// internal representation
void from_poly() const;
void to_poly() const;
void randomize(PRNG& G,condition cond=Full);
void randomize(PRNG& G, bigint B, bool Diag=false, bool binary=false, PT_Type type=Polynomial);
void randomize(PRNG& G, int n_bits, bool Diag=false, bool binary=false, PT_Type type=Polynomial);
void assign_zero(PT_Type t = Evaluation);
void assign_one(PT_Type t = Evaluation);
void assign_constant(T constant, PT_Type t = Evaluation);
friend void add<>(Plaintext& z,const Plaintext& x,const Plaintext& y);
friend void sub<>(Plaintext& z,const Plaintext& x,const Plaintext& y);
friend void mul<>(Plaintext& z,const Plaintext& x,const Plaintext& y);
friend void sqr<>(Plaintext& z,const Plaintext& x);
Plaintext operator+(const Plaintext& x) const
{ Plaintext res(*Field_Data); add(res, *this, x); return res; }
Plaintext operator-(const Plaintext& x) const
{ Plaintext res(*Field_Data); sub(res, *this, x); return res; }
void mul(const Plaintext& x, const Plaintext& y)
{ x.from_poly(); y.from_poly(); ::mul(*this, x, y); }
Plaintext operator*(const Plaintext& x)
{ Plaintext res(*Field_Data); res.mul(*this, x); return res; }
Plaintext& operator+=(const Plaintext& y);
Plaintext& operator-=(const Plaintext& y)
{ to_poly(); y.to_poly(); ::sub(*this, *this, y); return *this; }
void negate();
Rq_Element mul_by_X_i(int i, const FHE_PK& pk) const;
bool equals(const Plaintext& x) const;
bool operator!=(const Plaintext& x) { return !equals(x); }
bool is_diagonal() const { throw not_implemented(); }
bool is_binary() const { throw not_implemented(); }
/* 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);
size_t report_size(ReportType type);
void print_evaluation(int n_elements, string desc = "") const;
};
template <class FD>
using Plaintext_ = Plaintext<typename FD::T, FD, typename FD::S>;
#endif