-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRlweKeys.java
234 lines (165 loc) · 5.17 KB
/
RlweKeys.java
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package rlwe;
/**************************************************************************************************
*
* Implements objects for public and private keys including key generation functions.
*
* To improve efficiency, all elements are kept in the Fourier domain and only translated back to
* compute the shared key at the end. This avoids converting back and forth each time a ring elt
* multiplication is performed.
*
**************************************************************************************************/
import java.math.BigInteger;
import java.util.Arrays;
class RlwePublicKey {
private RingElt key;
private byte domain;
public RlwePublicKey (RlwePrivateKey k, RingElt a) {
RingElt e = Sample.getSample ();
e.multBy3 (); // Mult by 3 because of mod reduction optimizations
e.ntt ();
key = a.pointwiseMultAdd (k.getS (), e);
key.correction ();
domain = Constants.FOURIER;
}
public RlwePublicKey (RlwePrivateKey k, RingElt e, RingElt a) {
e.multBy3 ();
e.ntt();
key = a.pointwiseMultAdd (k.getS (), e);
key.correction ();
domain = Constants.FOURIER;
}
public RlwePublicKey (RingElt b, byte dom) {
key = new RingElt (b);
domain = dom;
}
public RlwePublicKey (RlwePublicKey k) {
key = new RingElt (k.key);
domain = k.domain;
}
public RlwePublicKey (byte[] inBytes) {
domain = inBytes[0];
key = new RingElt (Arrays.copyOfRange (inBytes, 1, inBytes.length));
}
public RingElt getKey () {
return key;
}
public byte getDomain () {
return domain;
}
public void toFourierDomain () {
if (domain == Constants.ORDINARY) {
key.ntt ();
key.multByConst (27); // Account for modular reduction optimizations
domain = Constants.FOURIER;
}
}
public void fromFourierDomain () {
if (domain == Constants.FOURIER) {
key.nttInv();
key.multByConst (27); // Account for modular reduction optimizations
domain = Constants.ORDINARY;
}
}
public byte[] serialize () {
byte[] kba = key.toByteArray();
byte[] ba = new byte[kba.length + 1];
ba[0] = domain;
System.arraycopy (kba, 0, ba, 1, kba.length);
return ba;
}
public int hashcode () {
return Arrays.hashCode (serialize());
}
}
class RlwePrivateKey {
private RingElt s;
private byte domain;
public RlwePrivateKey (RingElt sIn, byte dom) {
s = new RingElt (sIn);
if (dom == Constants.ORDINARY)
s.ntt();
domain = Constants.FOURIER;
}
public RlwePrivateKey () {
s = Sample.getSample ();
domain = Constants.ORDINARY;
}
public RlwePrivateKey (byte[] inBytes) {
domain = inBytes[0];
s = new RingElt (Arrays.copyOfRange (inBytes, 1, inBytes.length));
}
public RingElt getS () {
return s;
}
public void toFourierDomain () {
if (domain == Constants.ORDINARY) {
s.ntt();
domain = Constants.FOURIER;
}
}
public void fromFourierDomain () {
if (domain == Constants.FOURIER) {
s.nttInv();
domain = Constants.ORDINARY;
}
}
public byte[] serialize () {
byte[] sba = s.toByteArray();
byte[] ba = new byte[sba.length + 1];
ba[0] = domain;
System.arraycopy (sba, 0, ba, 1, sba.length);
return ba;
}
}
class RlweKeyPair {
private RlwePublicKey pubKey;
private final RlwePrivateKey privKey;
public RlweKeyPair (RlwePrivateKey prKey, RingElt a, byte transmitDomain) {
privKey = prKey;
privKey.toFourierDomain();
pubKey = new RlwePublicKey (prKey, a);
if (transmitDomain == Constants.ORDINARY)
pubKey.fromFourierDomain();
}
public RlweKeyPair (RlwePrivateKey prKey, RingElt e, RingElt a, byte transmitDomain) {
privKey = prKey;
privKey.toFourierDomain();
pubKey = new RlwePublicKey (prKey, e, a);
if (transmitDomain == Constants.ORDINARY)
pubKey.fromFourierDomain();
}
public RlweKeyPair (byte[] inKey, RingElt a, byte transmitDomain) {
privKey = new RlwePrivateKey (inKey);
privKey.toFourierDomain();
pubKey = new RlwePublicKey (privKey, a);
if (transmitDomain == Constants.ORDINARY)
pubKey.fromFourierDomain();
}
public RlweKeyPair (byte[] inKey, RingElt e, RingElt a, byte transmitDomain) {
privKey = new RlwePrivateKey (inKey);
privKey.toFourierDomain();
pubKey = new RlwePublicKey (privKey, e, a);
if (transmitDomain == Constants.ORDINARY)
pubKey.fromFourierDomain();
}
public RlweKeyPair (RingElt a, byte transmitDomain) {
privKey = new RlwePrivateKey ();
privKey.toFourierDomain();
pubKey = new RlwePublicKey (privKey, a);
if (transmitDomain == Constants.ORDINARY)
pubKey.fromFourierDomain();
}
public RlwePrivateKey getPrivateKey () {
return privKey;
}
public RlwePublicKey getPublicKey () {
return pubKey;
}
// Generate a new public key with the same private key but new error term
public void genNewPubKey (RingElt a) {
byte domain = pubKey.getDomain ();
pubKey = new RlwePublicKey (privKey, a);
if (domain == Constants.ORDINARY)
pubKey.fromFourierDomain ();
}
}