-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathdecryptor.test.ts
182 lines (177 loc) · 7.65 KB
/
decryptor.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
import { BatchEncoder } from '../implementation/batch-encoder'
import { Context } from '../implementation/context'
import { EncryptionParameters } from '../implementation/encryption-parameters'
import { Encryptor } from '../implementation/encryptor'
import { Evaluator } from '../implementation/evaluator'
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 { SecretKey } from '../implementation/secret-key'
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 bfvSecretKey: SecretKey
let bfvPublicKey: PublicKey
let bfvEncryptor: Encryptor
let bfvEvaluator: Evaluator
let ckksEncParms: EncryptionParameters
beforeAll(async () => {
seal = await SEAL()
const securityLevel = seal.SecurityLevel.tc128
const polyModulusDegree = 4096
const bitSizes = Int32Array.from([46, 16, 46])
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)
bfvSecretKey = bfvKeyGenerator.secretKey()
bfvPublicKey = bfvKeyGenerator.createPublicKey()
bfvEncryptor = seal.Encryptor(bfvContext, bfvPublicKey)
bfvEvaluator = seal.Evaluator(bfvContext)
ckksEncParms = seal.EncryptionParameters(seal.SchemeType.ckks)
ckksEncParms.setPolyModulusDegree(polyModulusDegree)
ckksEncParms.setCoeffModulus(coeffModulus)
})
describe('Decryptor', () => {
test('It should be a factory', () => {
expect(seal.Decryptor).toBeDefined()
expect(typeof seal.Decryptor.constructor).toBe('function')
expect(seal.Decryptor).toBeInstanceOf(Object)
expect(seal.Decryptor.constructor).toBe(Function)
expect(seal.Decryptor.constructor.name).toBe('Function')
})
test('It should construct an instance', () => {
const Constructor = jest.fn(seal.Decryptor)
Constructor(bfvContext, bfvSecretKey)
expect(Constructor).toHaveBeenCalledWith(bfvContext, bfvSecretKey)
})
test('It should fail to construct an instance', () => {
const newParms = seal.EncryptionParameters(seal.SchemeType.bfv)
newParms.setPolyModulusDegree(2048)
newParms.setCoeffModulus(
seal.CoeffModulus.BFVDefault(2048, seal.SecurityLevel.tc128)
)
newParms.setPlainModulus(seal.PlainModulus.Batching(2048, 20))
const newContext = seal.Context(newParms)
const newKeyGenerator = seal.KeyGenerator(newContext)
const newSecretKey = newKeyGenerator.secretKey()
const Constructor = jest.fn(seal.Decryptor)
expect(() => Constructor(bfvContext, newSecretKey)).toThrow()
expect(Constructor).toHaveBeenCalledWith(bfvContext, newSecretKey)
})
test('It should have properties', () => {
const item = seal.Decryptor(bfvContext, bfvSecretKey)
// Test properties
expect(item).toHaveProperty('instance')
expect(item).toHaveProperty('unsafeInject')
expect(item).toHaveProperty('delete')
expect(item).toHaveProperty('decrypt')
expect(item).toHaveProperty('invariantNoiseBudget')
})
test('It should have an instance', () => {
const item = seal.Decryptor(bfvContext, bfvSecretKey)
expect(item.instance).toBeDefined()
})
test('It should inject', () => {
const item = seal.Decryptor(bfvContext, bfvSecretKey)
const newItem = seal.Decryptor(bfvContext, bfvSecretKey)
newItem.delete()
const spyOn = jest.spyOn(newItem, 'unsafeInject')
newItem.unsafeInject(item.instance)
expect(spyOn).toHaveBeenCalledWith(item.instance)
expect(newItem.instance).toEqual(item.instance)
})
test('It should delete the old instance and inject', () => {
const item = seal.Decryptor(bfvContext, bfvSecretKey)
const newItem = seal.Decryptor(bfvContext, bfvSecretKey)
const spyOn = jest.spyOn(newItem, 'unsafeInject')
newItem.unsafeInject(item.instance)
expect(spyOn).toHaveBeenCalledWith(item.instance)
expect(newItem.instance).toEqual(item.instance)
})
test("It should delete it's instance", () => {
const item = seal.Decryptor(bfvContext, bfvSecretKey)
const spyOn = jest.spyOn(item, 'delete')
item.delete()
expect(spyOn).toHaveBeenCalled()
expect(item.instance).toBeUndefined()
})
test('It should skip deleting twice', () => {
const item = seal.Decryptor(bfvContext, bfvSecretKey)
item.delete()
const spyOn = jest.spyOn(item, 'delete')
item.delete()
expect(spyOn).toHaveBeenCalled()
expect(item.instance).toBeUndefined()
})
test('It should encrypt a ciphertext to a destination plain', () => {
const item = seal.Decryptor(bfvContext, bfvSecretKey)
const arr = Int32Array.from({ length: batchEncoder.slotCount }).fill(5)
const plain = seal.PlainText()
const cipher = seal.CipherText()
batchEncoder.encode(arr, plain)
bfvEncryptor.encrypt(plain, cipher)
const plainResult = seal.PlainText()
const spyOn = jest.spyOn(item, 'decrypt')
item.decrypt(cipher, plainResult)
expect(spyOn).toHaveBeenCalledWith(cipher, plainResult)
const decoded = batchEncoder.decode(plainResult, true)
expect(decoded).toEqual(arr)
})
test('It should encrypt a ciphertext and return a plain', () => {
const item = seal.Decryptor(bfvContext, bfvSecretKey)
const arr = Int32Array.from({ length: batchEncoder.slotCount }).fill(5)
const plain = seal.PlainText()
const cipher = seal.CipherText()
batchEncoder.encode(arr, plain)
bfvEncryptor.encrypt(plain, cipher)
const spyOn = jest.spyOn(item, 'decrypt')
const plainResult = item.decrypt(cipher) as PlainText
expect(spyOn).toHaveBeenCalledWith(cipher)
expect(plainResult).toBeDefined()
expect(typeof plainResult.constructor).toBe('function')
expect(plainResult).toBeInstanceOf(Object)
expect(plainResult.constructor).toBe(Object)
expect(plainResult.instance.constructor.name).toBe('Plaintext')
const decoded = batchEncoder.decode(plainResult, true)
expect(decoded).toEqual(arr)
})
test('It should return the invariant noise budget', () => {
const item = seal.Decryptor(bfvContext, bfvSecretKey)
const arr = Int32Array.from({ length: batchEncoder.slotCount }).fill(5)
const plain = seal.PlainText()
const cipher = seal.CipherText()
batchEncoder.encode(arr, plain)
bfvEncryptor.encrypt(plain, cipher)
const spyOn = jest.spyOn(item, 'invariantNoiseBudget')
const noise = item.invariantNoiseBudget(cipher)
expect(typeof noise).toBe('number')
expect(spyOn).toHaveBeenCalledWith(cipher)
})
test('It should fail to return the invariant noise budget', () => {
const item = seal.Decryptor(bfvContext, bfvSecretKey)
const arr = Int32Array.from({ length: batchEncoder.slotCount }).fill(5)
const plain = seal.PlainText()
const cipher = seal.CipherText()
batchEncoder.encode(arr, plain)
bfvEncryptor.encrypt(plain, cipher)
bfvEvaluator.cipherTransformToNtt(cipher, cipher)
const spyOn = jest.spyOn(item, 'invariantNoiseBudget')
expect(() => item.invariantNoiseBudget(cipher)).toThrow()
expect(spyOn).toHaveBeenCalledWith(cipher)
})
})