-
Notifications
You must be signed in to change notification settings - Fork 7
/
rlwe_test.py
218 lines (157 loc) · 8.75 KB
/
rlwe_test.py
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
import unittest
import numpy as np
from discrete_gauss import DiscreteGaussian
from polynomial import PolynomialRing, Polynomial, get_centered_remainder
from rlwe import RLWE
class TestRLWE(unittest.TestCase):
def setUp(self):
self.n = 1024
self.q = 536870909
self.sigma = 3.2
self.discrete_gaussian = DiscreteGaussian(self.sigma)
self.t = 257
# Note that t and q do not have to be prime nor coprime.
self.rlwe = RLWE(self.n, self.q, self.t, self.discrete_gaussian)
def test_rlwe_initialization(self):
self.assertEqual(self.rlwe.R.denominator, PolynomialRing(self.n).denominator)
self.assertEqual(self.rlwe.R.n, PolynomialRing(self.n).n)
self.assertEqual(self.rlwe.R.Q, PolynomialRing(self.n).Q)
self.assertEqual(self.rlwe.Rq.denominator, PolynomialRing(self.n, self.q).denominator)
self.assertEqual(self.rlwe.Rq.n, PolynomialRing(self.n, self.q).n)
self.assertEqual(self.rlwe.Rq.Q, PolynomialRing(self.n, self.q).Q)
self.assertEqual(self.rlwe.Rt.denominator, PolynomialRing(self.n, self.t).denominator)
self.assertEqual(self.rlwe.Rt.n, PolynomialRing(self.n, self.t).n)
self.assertEqual(self.rlwe.Rt.Q, PolynomialRing(self.n, self.t).Q)
self.assertEqual(self.rlwe.distribution, self.discrete_gaussian)
def test_sample_from_chi_key_distribution(self):
key = self.rlwe.SampleFromChiKeyDistribution()
# Ensure that the degree of the sampled poly is equal or less than d (it might be less if the leading coefficient sampled is 0)
count = 0
for coeff in key.coefficients:
count += 1
self.assertTrue(count <= self.rlwe.R.n)
# Ensure that the key is a polynomial in ring R
self.assertEqual(key.ring, self.rlwe.R)
# Ensure that the coefficients of the key are within {-1, 0, 1}
for coeff in key.coefficients:
self.assertTrue(coeff == -1 or coeff == 0 or coeff == 1)
def test_sample_from_chi_error_distribution(self):
error = self.rlwe.SampleFromChiErrorDistribution()
# Ensure that the degree of the sample polynomial is at most n-1, which means the has at most n coefficients
count = 0
for coeff in error.coefficients:
count += 1
self.assertTrue(count <= self.rlwe.R.n)
# Ensure that the error is a polynomial in ring R
self.assertEqual(error.ring, self.rlwe.R)
# Ensure that the secret key is sampled from the error distribution by checking that each coefficient is within the range of the error distribution
for coeff in error.coefficients:
self.assertTrue(coeff >= -6*self.sigma and coeff <= 6*self.sigma)
def test_secret_key_gen(self):
secret_key = self.rlwe.SecretKeyGen()
# Ensure that the degree of the sample polynomial is at most n-1, which means the has at most n coefficients
count = 0
for coeff in secret_key.coefficients:
count += 1
self.assertTrue(count <= self.rlwe.R.n)
# Ensure that the secret key is a polynomial in ring R
self.assertEqual(secret_key.ring, self.rlwe.R)
# Ensure that the coefficients of the key are within {-1, 0, 1}
for coeff in secret_key.coefficients:
self.assertTrue(coeff == -1 or coeff == 0 or coeff == 1)
def test_public_key_gen(self):
secret_key = self.rlwe.SecretKeyGen()
public_key = self.rlwe.PublicKeyGen(secret_key)
self.assertIsInstance(public_key, tuple)
self.assertEqual(len(public_key), 2)
self.assertIsInstance(public_key[0], Polynomial)
self.assertIsInstance(public_key[1], Polynomial)
# Ensure that the coefficients of the public key are within Z_q = (-q/2, q/2]
for coeff in public_key[0].coefficients:
self.assertTrue(coeff > -self.q // 2 and coeff <= self.q // 2)
for coeff in public_key[1].coefficients:
self.assertTrue(coeff > -self.q // 2 and coeff <= self.q // 2)
# Ensure that the public key is a polynomial in Rq
self.assertEqual(public_key[0].ring, self.rlwe.Rq)
self.assertEqual(public_key[1].ring, self.rlwe.Rq)
# TODO: add public key's value check
def test_message_sample(self):
secret_key = self.rlwe.SecretKeyGen()
public_key = self.rlwe.PublicKeyGen(secret_key)
message = self.rlwe.Rt.sample_polynomial()
# Ensure that message is a polynomial in Rt
self.assertEqual(message.ring, self.rlwe.Rt)
def test_wrong_message_sample(self):
secret_key = self.rlwe.SecretKeyGen()
public_key = self.rlwe.PublicKeyGen(secret_key)
message = self.rlwe.Rq.sample_polynomial()
# message must be in Rt, but I sampled it from Rq. So it must throw an error.
with self.assertRaisesRegex(AssertionError, "The message must be in Rt."):
self.rlwe.Encrypt(public_key, message)
def test_valid_encryption(self):
secret_key = self.rlwe.SecretKeyGen()
public_key = self.rlwe.PublicKeyGen(secret_key)
message = self.rlwe.Rt.sample_polynomial()
ciphertext, error = self.rlwe.Encrypt(public_key, message)
# Ensure that the coefficients of the public key are within Z_q = (-q/2, q/2]
for coeff in ciphertext[0].coefficients:
self.assertTrue(coeff > -self.q // 2 and coeff <= self.q // 2)
for coeff in ciphertext[1].coefficients:
self.assertTrue(coeff > -self.q // 2 and coeff <= self.q // 2)
# Ensure that the ciphertext is a polynomial in Rq
self.assertEqual(ciphertext[0].ring, self.rlwe.Rq)
self.assertEqual(ciphertext[1].ring, self.rlwe.Rq)
## TODO: add ciphertext value check
def test_valid_decryption(self):
secret_key = self.rlwe.SecretKeyGen()
public_key = self.rlwe.PublicKeyGen(secret_key)
message = self.rlwe.Rt.sample_polynomial()
ciphertext, error = self.rlwe.Encrypt(public_key, message)
dec = self.rlwe.Decrypt(secret_key, ciphertext, error)
# ensure that message and dec are of the same degree
self.assertEqual(len(message.coefficients), len(dec.coefficients))
# ensure that message and dec are of the same coefficients
for i in range(len(message.coefficients)):
self.assertEqual(message.coefficients[i], dec.coefficients[i])
def test_eval_add(self):
secret_key = self.rlwe.SecretKeyGen()
public_key = self.rlwe.PublicKeyGen(secret_key)
message1 = self.rlwe.Rt.sample_polynomial()
message2 = self.rlwe.Rt.sample_polynomial()
message_sum = np.polyadd(message1.coefficients, message2.coefficients)
ciphertext1, error1 = self.rlwe.Encrypt(public_key, message1)
ciphertext2, error2 = self.rlwe.Encrypt(public_key, message2)
ciphertext_sum = self.rlwe.EvalAdd(ciphertext1, ciphertext2)
# ciphertext_sum must be a tuple of two polynomials in Rq
self.assertEqual(ciphertext_sum[0].ring, self.rlwe.Rq)
self.assertEqual(ciphertext_sum[1].ring, self.rlwe.Rq)
# decrypt ciphertext_sum
dec = self.rlwe.Decrypt(secret_key, ciphertext_sum, error1 + error2)
# reduce the coefficients of message_sum by the t using the centered remainder
for i in range(len(message_sum)):
message_sum[i] = get_centered_remainder(message_sum[i], self.t)
# ensure that message_sum and dec are the same
for i in range(len(message_sum)):
self.assertEqual(message_sum[i], dec.coefficients[i])
def test_eval_const_add(self):
secret_key = self.rlwe.SecretKeyGen()
public_key = self.rlwe.PublicKeyGen(secret_key)
message1 = self.rlwe.Rt.sample_polynomial()
message2 = self.rlwe.Rt.sample_polynomial()
message_sum = np.polyadd(message1.coefficients, message2.coefficients)
ciphertext1, error1 = self.rlwe.Encrypt(public_key, message1)
const_ciphertext = self.rlwe.EncryptConst(public_key, message2)
ciphertext_sum = self.rlwe.EvalAdd(ciphertext1, const_ciphertext)
# ciphertext_sum must be a tuple of two polynomials in Rq
self.assertEqual(ciphertext_sum[0].ring, self.rlwe.Rq)
self.assertEqual(ciphertext_sum[1].ring, self.rlwe.Rq)
# decrypt ciphertext_sum
dec = self.rlwe.Decrypt(secret_key, ciphertext_sum, error1)
# reduce the coefficients of message_sum by the t using the centered remainder
for i in range(len(message_sum)):
message_sum[i] = get_centered_remainder(message_sum[i], self.t)
# ensure that message_sum and dec are the same
for i in range(len(message_sum)):
self.assertEqual(message_sum[i], dec.coefficients[i])
if __name__ == "__main__":
unittest.main()