-
Notifications
You must be signed in to change notification settings - Fork 24
/
cipher-text.test.ts
447 lines (442 loc) · 17.6 KB
/
cipher-text.test.ts
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
import { BatchEncoder } from '../implementation/batch-encoder'
import { CKKSEncoder } from '../implementation/ckks-encoder'
import { Context } from '../implementation/context'
import { EncryptionParameters } from '../implementation/encryption-parameters'
import { Encryptor } from '../implementation/encryptor'
import { KeyGenerator } from '../implementation/key-generator'
import { Modulus } from '../implementation/modulus'
import { PlainText } from '../implementation/plain-text'
import { PublicKey } from '../implementation/public-key'
import { SEALLibrary } from '../implementation/seal'
import { Vector } from '../implementation/vector'
import SEAL from '../throws_wasm_node_umd'
let seal: SEALLibrary
let bfvContext: Context
let coeffModulus: Vector
let plainModulus: Modulus
let bfvEncParms: EncryptionParameters
let batchEncoder: BatchEncoder
let bfvKeyGenerator: KeyGenerator
let bfvPublicKey: PublicKey
let bfvEncryptor: Encryptor
let ckksContext: Context
let ckksEncParms: EncryptionParameters
let ckksEncoder: CKKSEncoder
let ckksKeyGenerator: KeyGenerator
let ckksPublicKey: PublicKey
let ckksEncryptor: Encryptor
beforeAll(async () => {
seal = await SEAL()
const securityLevel = seal.SecurityLevel.tc128
const polyModulusDegree = 1024
const bitSizes = Int32Array.from([27])
const bitSize = 20
coeffModulus = seal.CoeffModulus.Create(polyModulusDegree, bitSizes)
plainModulus = seal.PlainModulus.Batching(polyModulusDegree, bitSize)
bfvEncParms = seal.EncryptionParameters(seal.SchemeType.bfv)
bfvEncParms.setPolyModulusDegree(polyModulusDegree)
bfvEncParms.setCoeffModulus(coeffModulus)
bfvEncParms.setPlainModulus(plainModulus)
bfvContext = seal.Context(bfvEncParms, true, securityLevel)
batchEncoder = seal.BatchEncoder(bfvContext)
bfvKeyGenerator = seal.KeyGenerator(bfvContext)
bfvPublicKey = bfvKeyGenerator.createPublicKey()
bfvEncryptor = seal.Encryptor(bfvContext, bfvPublicKey)
ckksEncParms = seal.EncryptionParameters(seal.SchemeType.ckks)
ckksEncParms.setPolyModulusDegree(polyModulusDegree)
ckksEncParms.setCoeffModulus(coeffModulus)
ckksContext = seal.Context(ckksEncParms, true, securityLevel)
ckksEncoder = seal.CKKSEncoder(ckksContext)
ckksKeyGenerator = seal.KeyGenerator(ckksContext)
ckksPublicKey = ckksKeyGenerator.createPublicKey()
ckksEncryptor = seal.Encryptor(ckksContext, ckksPublicKey)
})
describe('CipherText', () => {
test('It should be a factory', () => {
expect(seal.CipherText).toBeDefined()
expect(typeof seal.CipherText.constructor).toBe('function')
expect(seal.CipherText).toBeInstanceOf(Object)
expect(seal.CipherText.constructor).toBe(Function)
expect(seal.CipherText.constructor.name).toBe('Function')
})
test('It should construct an instance', () => {
const Constructor = jest.fn(seal.CipherText)
Constructor()
expect(Constructor).toHaveBeenCalledWith()
})
test('It should construct an instance with a bfvContext', () => {
const Constructor = jest.fn(seal.CipherText)
Constructor({ context: bfvContext })
expect(Constructor).toHaveBeenCalledWith({ context: bfvContext })
})
test('It should construct an instance with a bfvContext, parmsId', () => {
const Constructor = jest.fn(seal.CipherText)
const parmsId = bfvContext.firstParmsId
Constructor({ context: bfvContext, parmsId })
expect(Constructor).toHaveBeenCalledWith({
context: bfvContext,
parmsId
})
})
test('It should construct an instance with a bfvContext, parmsId, sizeCapacity', () => {
const Constructor = jest.fn(seal.CipherText)
const parmsId = bfvContext.firstParmsId
Constructor({ context: bfvContext, parmsId, sizeCapacity: 2 })
expect(Constructor).toHaveBeenCalledWith({
context: bfvContext,
parmsId,
sizeCapacity: 2
})
})
test('It should fail to construct an instance from invalid parameters', () => {
const Constructor = jest.fn(seal.CipherText)
expect(() =>
Constructor({ context: bfvContext, sizeCapacity: 2 })
).toThrow()
expect(Constructor).toHaveBeenCalledWith({
context: bfvContext,
sizeCapacity: 2
})
})
test('It should fail to construct an instance from bad parameters', () => {
const Constructor = jest.fn(seal.CipherText)
const parmsId = bfvContext.firstParmsId
expect(() =>
Constructor({ context: bfvContext, parmsId, sizeCapacity: -2 })
).toThrow()
expect(Constructor).toHaveBeenCalledWith({
context: bfvContext,
parmsId,
sizeCapacity: -2
})
})
test('It should have properties', () => {
const cipher = seal.CipherText()
// Test properties
expect(cipher).toHaveProperty('instance')
expect(cipher).toHaveProperty('unsafeInject')
expect(cipher).toHaveProperty('delete')
expect(cipher).toHaveProperty('reserve')
expect(cipher).toHaveProperty('resize')
expect(cipher).toHaveProperty('release')
expect(cipher).toHaveProperty('coeffModulusSize')
expect(cipher).toHaveProperty('polyModulusDegree')
expect(cipher).toHaveProperty('size')
expect(cipher).toHaveProperty('sizeCapacity')
expect(cipher).toHaveProperty('isTransparent')
expect(cipher).toHaveProperty('isNttForm')
expect(cipher).toHaveProperty('parmsId')
expect(cipher).toHaveProperty('scale')
expect(cipher).toHaveProperty('setScale')
expect(cipher).toHaveProperty('pool')
expect(cipher).toHaveProperty('save')
expect(cipher).toHaveProperty('saveArray')
expect(cipher).toHaveProperty('load')
expect(cipher).toHaveProperty('loadArray')
expect(cipher).toHaveProperty('copy')
expect(cipher).toHaveProperty('clone')
expect(cipher).toHaveProperty('move')
})
test('It should have an instance', () => {
const cipher = seal.CipherText()
expect(cipher.instance).toBeDefined()
})
test('It should inject', () => {
const cipher = seal.CipherText()
const newCipher = seal.CipherText()
newCipher.delete()
const spyOn = jest.spyOn(newCipher, 'unsafeInject')
newCipher.unsafeInject(cipher.instance)
expect(spyOn).toHaveBeenCalledWith(cipher.instance)
})
test('It should delete the old instance and inject', () => {
const cipher = seal.CipherText()
const newCipher = seal.CipherText()
const spyOn = jest.spyOn(newCipher, 'unsafeInject')
newCipher.unsafeInject(cipher.instance)
expect(spyOn).toHaveBeenCalledWith(cipher.instance)
})
test("It should delete it's instance", () => {
const cipher = seal.CipherText()
const spyOn = jest.spyOn(cipher, 'delete')
cipher.delete()
expect(spyOn).toHaveBeenCalled()
expect(cipher.instance).toBeUndefined()
})
test('It should skip deleting twice', () => {
const cipher = seal.CipherText()
cipher.delete()
const spyOn = jest.spyOn(cipher, 'delete')
cipher.delete()
expect(spyOn).toHaveBeenCalled()
expect(cipher.instance).toBeUndefined()
})
test('It should reserve memory', () => {
const cipher = seal.CipherText()
const spyOn = jest.spyOn(cipher, 'reserve')
cipher.reserve(bfvContext, 2)
expect(spyOn).toHaveBeenCalledWith(bfvContext, 2)
})
test('It should fail to reserve memory', () => {
const cipher = seal.CipherText()
const spyOn = jest.spyOn(cipher, 'reserve')
expect(() => cipher.reserve(bfvContext, 50000)).toThrow()
expect(spyOn).toHaveBeenCalledWith(bfvContext, 50000)
})
test('It should resize', () => {
const cipher = seal.CipherText()
cipher.reserve(bfvContext, 2)
expect(cipher.sizeCapacity).toEqual(2)
const spyOn = jest.spyOn(cipher, 'resize')
cipher.resize(5)
expect(spyOn).toHaveBeenCalledWith(5)
})
test('It should fail to resize', () => {
const cipher = seal.CipherText()
cipher.reserve(bfvContext, 2)
expect(cipher.sizeCapacity).toEqual(2)
const spyOn = jest.spyOn(cipher, 'resize')
expect(() => cipher.resize(1)).toThrow()
expect(spyOn).toHaveBeenCalledWith(1)
})
test('It should release allocated memory', () => {
const cipher = seal.CipherText()
cipher.reserve(bfvContext, 2)
expect(cipher.sizeCapacity).toEqual(2)
const spyOn = jest.spyOn(cipher, 'release')
cipher.release()
expect(spyOn).toHaveBeenCalledWith()
})
test('It should return the coeff mod size', () => {
const cipher = seal.CipherText()
cipher.reserve(bfvContext, 2)
expect(typeof cipher.coeffModulusSize).toBe('number')
})
test('It should return the poly modulus degree', () => {
const arr = Int32Array.from({ length: batchEncoder.slotCount }).fill(5)
const cipher = seal.CipherText()
const plain = batchEncoder.encode(arr) as PlainText
bfvEncryptor.encrypt(plain, cipher)
expect(typeof cipher.polyModulusDegree).toBe('number')
})
test('It should return the size', () => {
const arr = Int32Array.from({ length: batchEncoder.slotCount }).fill(5)
const cipher = seal.CipherText()
const plain = batchEncoder.encode(arr) as PlainText
bfvEncryptor.encrypt(plain, cipher)
expect(typeof cipher.size).toBe('number')
})
test('It should return the size capacity', () => {
const arr = Int32Array.from({ length: batchEncoder.slotCount }).fill(5)
const cipher = seal.CipherText()
const plain = batchEncoder.encode(arr) as PlainText
bfvEncryptor.encrypt(plain, cipher)
expect(typeof cipher.sizeCapacity).toBe('number')
})
test('It should return if the cipher is transparent', () => {
const arr = Int32Array.from({ length: batchEncoder.slotCount }).fill(5)
const cipher = seal.CipherText()
const plain = batchEncoder.encode(arr) as PlainText
bfvEncryptor.encrypt(plain, cipher)
expect(typeof cipher.isTransparent).toBe('boolean')
})
test('It should return if the cipher is not in NTT form', () => {
const arr = Int32Array.from({ length: batchEncoder.slotCount }).fill(5)
const cipher = seal.CipherText()
const plain = batchEncoder.encode(arr) as PlainText
bfvEncryptor.encrypt(plain, cipher)
expect(typeof cipher.isNttForm).toBe('boolean')
})
test('It should return a parms id type', () => {
const arr = Int32Array.from({ length: batchEncoder.slotCount }).fill(5)
const cipher = seal.CipherText()
const plain = batchEncoder.encode(arr) as PlainText
bfvEncryptor.encrypt(plain, cipher)
const parms = cipher.parmsId
const values = parms.values
expect(values.constructor).toBe(BigUint64Array)
values.forEach(x => {
expect(typeof x).toBe('bigint')
})
})
test('It should return the scale', () => {
const arr = Float64Array.from({ length: ckksEncoder.slotCount }).fill(5)
const cipher = seal.CipherText()
const plain = ckksEncoder.encode(arr, Math.pow(2, 20)) as PlainText
ckksEncryptor.encrypt(plain, cipher)
expect(typeof cipher.scale).toBe('number')
})
test('It should set the scale', () => {
const arr = Float64Array.from({ length: ckksEncoder.slotCount }).fill(5)
const cipher = seal.CipherText()
const scale1 = Math.pow(2, 20)
const scale2 = 2097152.32
const plain = ckksEncoder.encode(arr, Math.pow(2, 20)) as PlainText
ckksEncryptor.encrypt(plain, cipher)
expect(cipher.scale).toEqual(scale1)
cipher.setScale(scale2)
expect(cipher.scale).toEqual(scale2)
})
test('It should return the currently used memory pool handle', () => {
const arr = Float64Array.from({ length: ckksEncoder.slotCount }).fill(5)
const cipher = seal.CipherText()
const plain = ckksEncoder.encode(arr, Math.pow(2, 20)) as PlainText
ckksEncryptor.encrypt(plain, cipher)
const pool = cipher.pool
expect(pool.constructor.name).toBe('MemoryPoolHandle')
})
test('It should save to a string', () => {
const arr = Int32Array.from({ length: batchEncoder.slotCount }).fill(5)
const cipher = seal.CipherText()
const plain = batchEncoder.encode(arr) as PlainText
bfvEncryptor.encrypt(plain, cipher)
const spyOn = jest.spyOn(cipher, 'save')
const str = cipher.save()
expect(spyOn).toHaveBeenCalled()
expect(typeof str).toBe('string')
})
test('It should save to an array', () => {
const arr = Int32Array.from({ length: batchEncoder.slotCount }).fill(5)
const cipher = seal.CipherText()
const plain = batchEncoder.encode(arr) as PlainText
bfvEncryptor.encrypt(plain, cipher)
const spyOn = jest.spyOn(cipher, 'saveArray')
const array = cipher.saveArray()
expect(spyOn).toHaveBeenCalled()
expect(array.constructor).toBe(Uint8Array)
})
test('It should load from a string', () => {
const arr = Int32Array.from({ length: batchEncoder.slotCount }).fill(5)
const cipher = seal.CipherText()
const plain = batchEncoder.encode(arr) as PlainText
bfvEncryptor.encrypt(plain, cipher)
const str = cipher.save()
cipher.delete()
const newCipher = seal.CipherText()
const spyOn = jest.spyOn(newCipher, 'load')
newCipher.load(bfvContext, str)
expect(spyOn).toHaveBeenCalledWith(bfvContext, str)
expect(newCipher.save()).toBe(str)
})
test('It should load from a typed array', () => {
const arr = Int32Array.from({ length: batchEncoder.slotCount }).fill(5)
const cipher = seal.CipherText()
const plain = batchEncoder.encode(arr) as PlainText
bfvEncryptor.encrypt(plain, cipher)
const array = cipher.saveArray(seal.ComprModeType.zstd)
cipher.delete()
const newCipher = seal.CipherText()
const spyOn = jest.spyOn(newCipher, 'loadArray')
newCipher.loadArray(bfvContext, array)
expect(spyOn).toHaveBeenCalledWith(bfvContext, array)
expect(newCipher.saveArray()).toEqual(array)
})
test('It should fail to load from a string', () => {
const newCipher = seal.CipherText()
const spyOn = jest.spyOn(newCipher, 'load')
expect(() =>
newCipher.load(
bfvContext,
'XqEQAwUBAAAoAAAAAAAAAHicY2CgCHywj1sowMwKZEmgyQAAOaoCXw=='
)
).toThrow()
expect(spyOn).toHaveBeenCalledWith(
bfvContext,
'XqEQAwUBAAAoAAAAAAAAAHicY2CgCHywj1sowMwKZEmgyQAAOaoCXw=='
)
})
test('It should fail to load from a Uint8Array', () => {
const newCipher = seal.CipherText()
const spyOn = jest.spyOn(newCipher, 'loadArray')
expect(() =>
newCipher.loadArray(
bfvContext,
Uint8Array.from([
94, 161, 16, 3, 5, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 120, 156, 99, 96,
160, 8, 124, 176, 143, 91, 40, 192, 204, 10, 100, 73, 160, 201, 0, 0,
57, 170, 2, 95
])
)
).toThrow()
expect(spyOn).toHaveBeenCalledWith(
bfvContext,
Uint8Array.from([
94, 161, 16, 3, 5, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 120, 156, 99, 96,
160, 8, 124, 176, 143, 91, 40, 192, 204, 10, 100, 73, 160, 201, 0, 0,
57, 170, 2, 95
])
)
})
test('It should copy another instance', () => {
const arr = Int32Array.from({ length: batchEncoder.slotCount }).fill(5)
const cipher = seal.CipherText()
const plain = batchEncoder.encode(arr) as PlainText
bfvEncryptor.encrypt(plain, cipher)
const newCipher = seal.CipherText()
const spyOn = jest.spyOn(newCipher, 'copy')
newCipher.copy(cipher)
expect(spyOn).toHaveBeenCalledWith(cipher)
expect(newCipher.save()).toEqual(cipher.save())
})
test('It should fail to copy another instance', () => {
const arr = Int32Array.from({ length: batchEncoder.slotCount }).fill(5)
const cipher = seal.CipherText()
const plain = batchEncoder.encode(arr) as PlainText
bfvEncryptor.encrypt(plain, cipher)
const newCipher = seal.CipherText()
cipher.delete()
const spyOn = jest.spyOn(newCipher, 'copy')
expect(() => newCipher.copy(cipher)).toThrow()
expect(spyOn).toHaveBeenCalledWith(cipher)
})
test('It should clone itself', () => {
const arr = Int32Array.from({ length: batchEncoder.slotCount }).fill(5)
const cipher = seal.CipherText()
const plain = batchEncoder.encode(arr) as PlainText
bfvEncryptor.encrypt(plain, cipher)
const spyOn = jest.spyOn(cipher, 'clone')
const newCipher = cipher.clone()
expect(spyOn).toHaveBeenCalledWith()
expect(newCipher).toBeDefined()
expect(typeof newCipher.constructor).toBe('function')
expect(newCipher).toBeInstanceOf(Object)
expect(newCipher.constructor).toBe(Object)
expect(newCipher.instance.constructor.name).toBe('Ciphertext')
expect(newCipher.save()).toEqual(cipher.save())
})
test('It should fail to clone itself', () => {
const arr = Int32Array.from({ length: batchEncoder.slotCount }).fill(5)
const cipher = seal.CipherText()
const plain = batchEncoder.encode(arr) as PlainText
bfvEncryptor.encrypt(plain, cipher)
cipher.delete()
const spyOn = jest.spyOn(cipher, 'clone')
expect(() => cipher.clone()).toThrow()
expect(spyOn).toHaveBeenCalledWith()
})
test('It should move another instance into itself and delete the old', () => {
const arr = Int32Array.from({ length: batchEncoder.slotCount }).fill(5)
const cipher = seal.CipherText()
const plain = batchEncoder.encode(arr) as PlainText
bfvEncryptor.encrypt(plain, cipher)
const str = cipher.save()
const newCipher = seal.CipherText()
const spyOn = jest.spyOn(newCipher, 'move')
newCipher.move(cipher)
expect(spyOn).toHaveBeenCalledWith(cipher)
expect(cipher.instance).toBeUndefined()
expect(() => cipher.size).toThrow(TypeError)
expect(newCipher.save()).toEqual(str)
})
test('It should fail to move another instance into itself and delete the old', () => {
const arr = Int32Array.from({ length: batchEncoder.slotCount }).fill(5)
const cipher = seal.CipherText()
const plain = batchEncoder.encode(arr) as PlainText
bfvEncryptor.encrypt(plain, cipher)
const newCipher = seal.CipherText()
cipher.delete()
const spyOn = jest.spyOn(newCipher, 'move')
expect(() => newCipher.move(cipher)).toThrow()
expect(spyOn).toHaveBeenCalledWith(cipher)
})
})