-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathPlaintextSpace.cpp
146 lines (119 loc) · 3.57 KB
/
PlaintextSpace.cpp
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
#ifndef _PLAINTEXT_SPACE_H_
#define _PLAINTEXT_SPACE_H_
#include "PlaintextSpace.h"
#include "Util.h"
#include <algorithm>
#include "assert.h"
static inline unsigned ZZToLong(const ZZ &num) {
unsigned data;
BytesFromZZ((unsigned char *)&data, num, sizeof(unsigned));
return data;
}
PlaintextSpace::~PlaintextSpace() {
for (unsigned i = 0; i < factors.size(); i++) {
delete factors[i];
delete crtCoeffs[i];
}
}
void PlaintextSpace::Init(const ZZX &PhiX, const ZZ &p) {
ZZ_p::init(p);
this->PhiX = to_ZZ_pX(PhiX);
this->p = p;
vec_ZZ_pX crtFactors;
SFCanZass(crtFactors, this->PhiX);
totalSlots = crtFactors.length();
factors.resize(totalSlots);
for (unsigned i = 0; i < totalSlots; i++) {
factors[i] = new ZZ_pX(crtFactors[i]);
}
usableSlots = 1;
unsigned tmp = totalSlots;
while (tmp > 1) {
usableSlots <<= 1;
tmp >>= 1;
}
crtCoeffs.resize(totalSlots);
for (unsigned i = 0; i < totalSlots; i++) {
crtCoeffs[i] = new ZZ_pX();
ZZ_pX te = this->PhiX / crtFactors[i];
te %= crtFactors[i];
InvMod(*crtCoeffs[i], te, crtFactors[i]);
*crtCoeffs[i] *= (this->PhiX / crtFactors[i]);
}
FindSlots(totalSlots);
}
void PlaintextSpace::Init(const ZZX &PhiX, const ZZ &p, unsigned generator) {
this->generator = generator;
Init(PhiX, p);
}
unsigned PlaintextSpace::GetUsableSlots() const {
return usableSlots;
}
unsigned PlaintextSpace::GetTotalSlots() const {
return totalSlots;
}
void PlaintextSpace::FindSlots(unsigned nSlots) {
vector<ZZ_pX> crt(nSlots);
for (unsigned i = 0; i < nSlots; i++) {
crt[i] = to_ZZ_pX(i+1);
}
ZZ_pX permX;
EmbedInSlots(permX, crt, false);
FrobeniusMap(permX, generator);
DecodeSlots(crt, permX, false);
vector<unsigned> perm(nSlots);
for (unsigned i = 0; i < nSlots; i++) {
perm[i] = ZZToLong(rep(crt[i].rep[0])) - 1;
}
ReorderSlots(perm);
}
void PlaintextSpace::ReorderSlots(vector<unsigned> &perm) {
// EDF doesn't always return the factors in a particular order, so
// we place the 0 slot first to make it deterministic
unsigned zeroInd = 0;
for (; zeroInd < perm.size() && perm[zeroInd] != 0; zeroInd++);
vector<ZZ_pX*> factorsP;
vector<ZZ_pX*> crtCoeffsP;
factorsP.push_back(factors[zeroInd]);
crtCoeffsP.push_back(crtCoeffs[zeroInd]);
for (unsigned i = perm[zeroInd]; i != zeroInd; i = perm[i]) {
factorsP.push_back(factors[i]);
crtCoeffsP.push_back(crtCoeffs[i]);
}
assert(factorsP.size() == perm.size());
swap(crtCoeffs, crtCoeffsP);
swap(factors, factorsP);
crtCoeffsP.clear();
factorsP.clear();
}
void PlaintextSpace::EmbedInSlots(ZZ_pX &embedded, const vector<ZZ_pX> &msgs,
bool onlyUsable) const {
embedded = ZZ_pX::zero();
unsigned msgInd = 0;
for (unsigned i = 0; i < totalSlots && msgInd < msgs.size(); i++) {
if (onlyUsable && i >= usableSlots) break;
embedded += *crtCoeffs[i] * msgs[msgInd++];
}
embedded %= PhiX;
}
void PlaintextSpace::DecodeSlots(vector<ZZ_pX> &msgBatch, const ZZ_pX &msg,
bool onlyUsable) const {
msgBatch.resize(totalSlots);
for (unsigned i = 0; i < totalSlots; i++) {
if (onlyUsable && i >= usableSlots) break;
DecodeSlot(msgBatch[i], msg, i);
}
}
void PlaintextSpace::DecodeSlot(ZZ_pX &val, const ZZ_pX &msg, unsigned ind) const {
rem(val, msg, *factors[ind]);
}
void PlaintextSpace::FrobeniusMap(ZZ_pX &poly, long k) {
for (long i = deg(poly); i >= 0; i--) {
SetCoeff(poly, k*i, poly.rep[i]);
if (i > 0) {
poly.rep[i] = 0;
}
}
rem(poly, poly, PhiX);
}
#endif