-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCiphertext.java
More file actions
188 lines (148 loc) · 4.45 KB
/
Ciphertext.java
File metadata and controls
188 lines (148 loc) · 4.45 KB
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
package ckks;
public class Ciphertext {
private Context context;
private Polynomial b, a;
private Polynomial d0, d1, d2;
private double scale;
private int level;
public Ciphertext(Context context) {
this.context = context;
}
public Ciphertext(Ciphertext src) {
this.context = src.context;
if (src.isEmpty())
return;
if (src.isLinear()) {
this.b = new Polynomial(src.b);
this.a = new Polynomial(src.a);
} else {
this.d0 = new Polynomial(src.d0);
this.d1 = new Polynomial(src.d1);
this.d2 = new Polynomial(src.d2);
}
this.scale = src.scale;
this.level = src.level;
}
public void init(Polynomial b, Polynomial a, int level,
double scale) {
this.b = b;
this.a = a;
this.level = level;
this.scale = scale;
}
public void afterMult(Polynomial d0, Polynomial d1, Polynomial d2,
double scale) {
this.b = null;
this.a = null;
this.d0 = d0;
this.d1 = d1;
this.d2 = d2;
this.scale = scale;
}
public void afterRelinearization(Polynomial b, Polynomial a) {
this.d0 = null;
this.d1 = null;
this.d2 = null;
this.b = b;
this.a = a;
}
public void afterRescale(Polynomial b, Polynomial a, double scale,
int level) {
this.d0 = null;
this.d1 = null;
this.d2 = null;
this.b = b;
this.a = a;
this.scale = scale;
this.level = level;
}
public Polynomial getB() {
return b;
}
public Polynomial getA() {
return a;
}
public Polynomial getD0() {
return d0;
}
public Polynomial getD1() {
return d1;
}
public Polynomial getD2() {
return d2;
}
public boolean isEmpty() {
return b == null && d0 == null;
}
public boolean isLinear() {
return b != null;
}
public double getScale() {
return scale;
}
public void setScale(double scale) {
this.scale = scale;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public void debugPrint() {
if (isLinear()) {
System.out.println("Ciphertext is linear");
System.out.println("b:");
b.debugPrint();
System.out.println("a:");
a.debugPrint();
} else {
System.out.println("Ciphertext is not linear");
System.out.println("d0:");
d0.debugPrint();
System.out.println("d1:");
d1.debugPrint();
System.out.println("d2:");
d2.debugPrint();
}
}
public long[] serialize() {
if (d0 != null)
throw new IllegalArgumentException("Ciphertext::serialize supported only for linear ciphertext.");
long[][] bCrt = b.getCrt();
long[][] aCrt = a.getCrt();
long[] res = new long[(2 + bCrt.length * bCrt[0].length) + (2 + aCrt.length * aCrt[0].length) + 2];
int idx = 0;
res[idx++] = bCrt.length;
res[idx++] = bCrt[0].length;
for (int i = 0; i < bCrt.length; i++)
for (int j = 0; j < bCrt[0].length; j++)
res[idx++] = bCrt[i][j];
res[idx++] = aCrt.length;
res[idx++] = aCrt[0].length;
for (int i = 0; i < aCrt.length; i++)
for (int j = 0; j < aCrt[0].length; j++)
res[idx++] = aCrt[i][j];
res[idx++] = (long) scale;
res[idx] = level;
return res;
}
public void deserialize(long[] serialization) {
int idx = 0;
long[][] bCrt = new long[(int) serialization[idx++]][(int) serialization[idx++]];
for (int i = 0; i < bCrt.length; i++)
for (int j = 0; j < bCrt[0].length; j++)
bCrt[i][j] = serialization[idx++];
long[][] aCrt = new long[(int) serialization[idx++]][(int) serialization[idx++]];
for (int i = 0; i < aCrt.length; i++)
for (int j = 0; j < aCrt[0].length; j++)
aCrt[i][j] = serialization[idx++];
b = new Polynomial(context, bCrt);
a = new Polynomial(context, aCrt);
d0 = null;
d1 = null;
d2 = null;
scale = serialization[idx++];
level = (int) serialization[idx];
}
}