-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathPlaintext.h
113 lines (87 loc) · 3.5 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
#ifndef _PLAINTEXT_H
#define _PLAINTEXT_H
#include "NTL/ZZX.h"
#include <NTL/ZZ_pX.h>
#include <NTL/ZZ_pXFactoring.h>
#include <vector>
#include "FHEContext.h"
class Plaintext {
public:
Plaintext() : context(*activeContext) {}
Plaintext(const FHEcontext &context) : context(context) {}
Plaintext(const ZZ_pX &msg) : context(*activeContext) { Init(msg); }
Plaintext(const FHEcontext &context, const ZZ_pX &msg) : context(context) { Init(msg); }
template <typename T>
Plaintext(const T &msg) : context(*activeContext) { Init(to_ZZ_pX(msg)); }
template <typename T>
Plaintext(const FHEcontext &context, const T &msg) : context(context) { Init(to_ZZ_pX(msg)); }
Plaintext(const vector<ZZ_pX> &msgs) : context(*activeContext) { Init(msgs); }
Plaintext(const FHEcontext &context, const vector<ZZ_pX> &msgs) : context(context) { Init(msgs); }
template <typename T>
Plaintext(const vector<T> &msgs) : context(*activeContext) { Init(msgs); }
template <typename T>
Plaintext(const FHEcontext &context, const vector<T> &msgs) : context(context) { Init(msgs); }
void Init();
void Init(const ZZ_pX &msg);
void Init(const vector<ZZ_pX> &msgs);
template <typename T>
void Init(const vector<T> &msgs) {
vector<ZZ_pX> newMsgs(msgs.size());
for (unsigned i = 0; i < msgs.size(); i++) {
newMsgs[i] = to_ZZ_pX(msgs[i]);
}
EmbedInSlots(newMsgs);
}
void EmbedInSlots(const vector<ZZ_pX> &msgs, bool onlyUsable = true);
void DecodeSlots(vector<ZZ_pX> &msgBatch, bool onlyUsable = true);
void DecodeSlot(ZZ_pX &val, unsigned slot);
Plaintext &operator=(const Plaintext &other);
bool operator==(const Plaintext &other) const;
friend ostream &operator<<(ostream &os, const Plaintext &ptxt);
ZZ_pX message;
/*DEBUG functions*/
void Randomize() {
random(message, deg(context.zMstar.PhimX())-1);
}
static Plaintext Random(const FHEcontext &context) {
Plaintext ret(context);
ret.Randomize();
return ret;
}
Plaintext &operator+=(const Plaintext &other) {
assert(&context == &other.context);
message += other.message;
return *this;
}
Plaintext &operator-=(const Plaintext &other) {
assert(&context == &other.context);
message -= other.message;
return *this;
}
Plaintext &operator*=(const Plaintext &other) {
assert(&context == &other.context);
MulMod(message, message, other.message, to_ZZ_pX(context.zMstar.PhimX()));
return *this;
}
Plaintext &operator>>=(long k) {
vector<ZZ_pX> plaintextArray;
DecodeSlots(plaintextArray, false);
vector<ZZ_pX> rotatedArray = plaintextArray;
for (unsigned i = 0; i < plaintextArray.size(); i++) {
rotatedArray[(i + plaintextArray.size() - k) % plaintextArray.size()] = plaintextArray[i];
}
EmbedInSlots(rotatedArray, false);
return *this;
}
Plaintext operator+(const Plaintext &other) {
assert(&context == &other.context);
return Plaintext(context, message+other.message);
}
Plaintext operator*(const Plaintext &other) {
assert(&context == &other.context);
return Plaintext(context, MulMod(message, other.message, to_ZZ_pX(context.zMstar.PhimX())));
}
private:
const FHEcontext &context;
};
#endif