-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathbatch-encoder.test.ts
244 lines (242 loc) · 9.51 KB
/
batch-encoder.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
import { BatchEncoderTypes } from '../implementation/batch-encoder'
import { Context } from '../implementation/context'
import { EncryptionParameters } from '../implementation/encryption-parameters'
import { Modulus } from '../implementation/modulus'
import { SEALLibrary } from '../implementation/seal'
import { Vector } from '../implementation/vector'
import SEAL from '../throws_wasm_node_umd'
let seal: SEALLibrary
let context: Context
let coeffModulus: Vector
let plainModulus: Modulus
let encParms: EncryptionParameters
beforeAll(async () => {
seal = await SEAL()
const schemeType = seal.SchemeType.bfv
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)
encParms = seal.EncryptionParameters(schemeType)
encParms.setPolyModulusDegree(polyModulusDegree)
encParms.setCoeffModulus(coeffModulus)
encParms.setPlainModulus(plainModulus)
context = seal.Context(encParms, true, securityLevel)
})
describe('BatchEncoder', () => {
test('It should be a factory', () => {
expect(seal.BatchEncoder).toBeDefined()
expect(typeof seal.BatchEncoder.constructor).toBe('function')
expect(seal.BatchEncoder).toBeInstanceOf(Object)
expect(seal.BatchEncoder.constructor).toBe(Function)
expect(seal.BatchEncoder.constructor.name).toBe('Function')
})
test('It should construct an instance', () => {
const Constructor = jest.fn(seal.BatchEncoder)
Constructor(context)
expect(Constructor).toHaveBeenCalledWith(context)
})
test('It should fail to construct an instance', () => {
const Constructor = jest.fn(seal.BatchEncoder)
expect(() => Constructor(null as unknown as Context)).toThrow()
expect(Constructor).toHaveBeenCalledWith(null)
})
test('It should have properties', () => {
const item = seal.BatchEncoder(context)
// Test properties
expect(item).toHaveProperty('instance')
expect(item).toHaveProperty('unsafeInject')
expect(item).toHaveProperty('delete')
expect(item).toHaveProperty('encode')
expect(item).toHaveProperty('decode')
expect(item).toHaveProperty('slotCount')
})
test('It should have an instance', () => {
const item = seal.BatchEncoder(context)
expect(item.instance).toBeDefined()
})
test('It should inject', () => {
const item = seal.BatchEncoder(context)
const newItem = seal.BatchEncoder(context)
newItem.delete()
const spyOn = jest.spyOn(newItem, 'unsafeInject')
newItem.unsafeInject(item.instance)
expect(spyOn).toHaveBeenCalledWith(newItem.instance)
})
test('It should delete the old instance and inject', () => {
const item = seal.BatchEncoder(context)
const newItem = seal.BatchEncoder(context)
const spyOn = jest.spyOn(newItem, 'unsafeInject')
newItem.unsafeInject(item.instance)
expect(spyOn).toHaveBeenCalledWith(newItem.instance)
})
test("It should delete it's instance", () => {
const item = seal.BatchEncoder(context)
const spyOn = jest.spyOn(item, 'delete')
item.delete()
expect(spyOn).toHaveBeenCalled()
expect(item.instance).toBeUndefined()
})
test('It should skip deleting twice', () => {
const item = seal.BatchEncoder(context)
item.delete()
const spyOn = jest.spyOn(item, 'delete')
item.delete()
expect(spyOn).toHaveBeenCalled()
expect(item.instance).toBeUndefined()
})
test('It should encode an int32 array to a plaintext destination', () => {
const item = seal.BatchEncoder(context)
const arr = Int32Array.from({ length: item.slotCount }, (_, i) => -i)
const plain = seal.PlainText()
const spyOn = jest.spyOn(item, 'encode')
item.encode(arr, plain)
expect(spyOn).toHaveBeenCalledWith(arr, plain)
})
test('It should encode an int32 array and return a plaintext', () => {
const item = seal.BatchEncoder(context)
const arr = Int32Array.from({ length: item.slotCount }, (_, i) => -i)
const spyOn = jest.spyOn(item, 'encode')
const plain = item.encode(arr)
expect(spyOn).toHaveBeenCalledWith(arr)
expect(plain).toBeDefined()
})
test('It should encode an int64 array to a plaintext destination', () => {
const item = seal.BatchEncoder(context)
const arr = BigInt64Array.from({ length: item.slotCount }, (_, i) =>
BigInt(-i)
)
const plain = seal.PlainText()
const spyOn = jest.spyOn(item, 'encode')
item.encode(arr, plain)
expect(spyOn).toHaveBeenCalledWith(arr, plain)
})
test('It should encode an int64 array and return a plaintext', () => {
const item = seal.BatchEncoder(context)
const arr = BigInt64Array.from({ length: item.slotCount }, (_, i) =>
BigInt(-i)
)
const spyOn = jest.spyOn(item, 'encode')
const plain = item.encode(arr)
expect(spyOn).toHaveBeenCalledWith(arr)
expect(plain).toBeDefined()
})
test('It should encode an uint32 array to a plaintext destination', () => {
const item = seal.BatchEncoder(context)
const arr = Uint32Array.from({ length: item.slotCount }, (_, i) => i)
const plain = seal.PlainText()
const spyOn = jest.spyOn(item, 'encode')
item.encode(arr, plain)
expect(spyOn).toHaveBeenCalledWith(arr, plain)
})
test('It should encode an uint32 array and return a plaintext', () => {
const item = seal.BatchEncoder(context)
const arr = Uint32Array.from({ length: item.slotCount }, (_, i) => i)
const spyOn = jest.spyOn(item, 'encode')
const plain = item.encode(arr)
expect(spyOn).toHaveBeenCalledWith(arr)
expect(plain).toBeDefined()
})
test('It should encode an uint64 array to a plaintext destination', () => {
const item = seal.BatchEncoder(context)
const arr = BigUint64Array.from({ length: item.slotCount }, (_, i) =>
BigInt(i)
)
const plain = seal.PlainText()
const spyOn = jest.spyOn(item, 'encode')
item.encode(arr, plain)
expect(spyOn).toHaveBeenCalledWith(arr, plain)
})
test('It should encode an uint64 array and return a plaintext', () => {
const item = seal.BatchEncoder(context)
const arr = BigUint64Array.from({ length: item.slotCount }, (_, i) =>
BigInt(i)
)
const spyOn = jest.spyOn(item, 'encode')
const plain = item.encode(arr)
expect(spyOn).toHaveBeenCalledWith(arr)
expect(plain).toBeDefined()
})
test('It should fail on unsupported array type', () => {
const item = seal.BatchEncoder(context)
const arr = Float64Array.from({ length: item.slotCount }, (_, i) => i)
const spyOn = jest.spyOn(item, 'encode')
expect(() => item.encode(arr as unknown as BatchEncoderTypes)).toThrow()
expect(spyOn).toHaveBeenCalledWith(arr)
})
test('It should fail on encoding bad data', () => {
const item = seal.BatchEncoder(context)
const arr = Int32Array.from({ length: item.slotCount * 2 }, (_, i) => i)
const spyOn = jest.spyOn(item, 'encode')
expect(() => item.encode(arr)).toThrow()
expect(spyOn).toHaveBeenCalledWith(arr)
})
test('It should decode an int32 array', () => {
const item = seal.BatchEncoder(context)
const arr = Int32Array.from({ length: item.slotCount }, (_, i) => -i)
const plain = seal.PlainText()
item.encode(arr, plain)
const spyOn = jest.spyOn(item, 'decode')
const decoded = item.decode(plain, true)
expect(spyOn).toHaveBeenCalledWith(plain, true)
expect(decoded).toEqual(arr)
})
test('It should decode an int64 array', () => {
const item = seal.BatchEncoder(context)
const arr = BigInt64Array.from({ length: item.slotCount }, (_, i) =>
BigInt(-i)
)
const plain = seal.PlainText()
item.encode(arr, plain)
const spyOn = jest.spyOn(item, 'decodeBigInt')
const decoded = item.decodeBigInt(plain, true)
expect(spyOn).toHaveBeenCalledWith(plain, true)
expect(decoded).toEqual(arr)
})
test('It should decode an uint32 array', () => {
const item = seal.BatchEncoder(context)
const arr = Uint32Array.from({ length: item.slotCount }, (_, i) => i)
const plain = seal.PlainText()
item.encode(arr, plain)
const spyOn = jest.spyOn(item, 'decode')
const decoded = item.decode(plain, false)
expect(spyOn).toHaveBeenCalledWith(plain, false)
expect(decoded).toEqual(arr)
})
test('It should decode a uint64 array', () => {
const item = seal.BatchEncoder(context)
const arr = BigUint64Array.from({ length: item.slotCount }, (_, i) =>
BigInt(i)
)
const plain = seal.PlainText()
item.encode(arr, plain)
const spyOn = jest.spyOn(item, 'decodeBigInt')
const decoded = item.decodeBigInt(plain, false)
expect(spyOn).toHaveBeenCalledWith(plain, false)
expect(decoded).toEqual(arr)
})
test('It should fail to decode an uint32 array', () => {
const item = seal.BatchEncoder(context)
const arr = Uint32Array.from({ length: item.slotCount }, (_, i) => i)
const plain = seal.PlainText()
item.encode(arr, plain)
const spyOn = jest.spyOn(item, 'decode')
plain.delete()
expect(() => item.decode(plain, false)).toThrow()
expect(spyOn).toHaveBeenCalledWith(plain, false)
})
test('It should fail to decode an uint64 array', () => {
const item = seal.BatchEncoder(context)
const arr = BigUint64Array.from({ length: item.slotCount }, (_, i) =>
BigInt(i)
)
const plain = seal.PlainText()
item.encode(arr, plain)
const spyOn = jest.spyOn(item, 'decodeBigInt')
plain.delete()
expect(() => item.decodeBigInt(plain, false)).toThrow()
expect(spyOn).toHaveBeenCalledWith(plain, false)
})
})