forked from nucypher/nufhe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_tlwe.py
131 lines (96 loc) · 4 KB
/
test_tlwe.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
# Copyright (C) 2018 NuCypher
#
# This file is part of nufhe.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import numpy
from nufhe.keys import NuFHEParameters
from nufhe.numeric_functions import Torus32, Int32, ErrorFloat
from nufhe.tlwe import TLweParams
from nufhe.tlwe_gpu import (
TLweNoiselessTrivial,
TLweExtractLweSamples,
TLweEncryptZero,
)
from nufhe.tlwe_cpu import (
TLweNoiselessTrivialReference,
TLweExtractLweSamplesReference,
TLweEncryptZeroReference,
)
from nufhe import PerformanceParameters
from utils import get_test_array, errors_allclose
def test_tlwe_noiseless_trivial(thread):
shape = (2, 5)
params = NuFHEParameters().tgsw_params.tlwe_params
mask_size = params.mask_size
polynomial_degree = params.polynomial_degree
mu = get_test_array(shape + (polynomial_degree,), Torus32)
a = numpy.empty(shape + (mask_size + 1, polynomial_degree), Torus32)
cv = numpy.empty(shape, ErrorFloat)
a_dev = thread.empty_like(a)
cv_dev = thread.empty_like(cv)
mu_dev = thread.to_device(mu)
test = TLweNoiselessTrivial(params, shape).compile(thread)
ref = TLweNoiselessTrivialReference(params, shape)
test(a_dev, cv_dev, mu_dev)
a_test = a_dev.get()
cv_test = cv_dev.get()
ref(a, cv, mu)
assert (a_test == a).all()
assert errors_allclose(cv_test, cv)
def test_tlwe_extract_lwe_samples(thread):
shape = (2, 5)
params = NuFHEParameters().tgsw_params.tlwe_params
mask_size = params.mask_size
polynomial_degree = params.polynomial_degree
tlwe_a = get_test_array(shape + (mask_size + 1, polynomial_degree), Torus32)
a = numpy.empty(shape + (params.extracted_lweparams.size,), Torus32)
b = numpy.empty(shape, Torus32)
tlwe_a_dev = thread.to_device(tlwe_a)
a_dev = thread.empty_like(a)
b_dev = thread.empty_like(b)
test = TLweExtractLweSamples(params, shape).compile(thread)
ref = TLweExtractLweSamplesReference(params, shape)
test(a_dev, b_dev, tlwe_a_dev)
a_test = a_dev.get()
b_test = b_dev.get()
ref(a, b, tlwe_a)
assert (a_test == a).all()
assert (b_test == b).all()
def test_tlwe_encrypt_zero(thread):
nufhe_params = NuFHEParameters()
perf_params = PerformanceParameters(nufhe_params)
params = nufhe_params.tgsw_params.tlwe_params
mask_size = params.mask_size
polynomial_degree = params.polynomial_degree
noise = params.min_noise
shape = (3, 4, 5)
result_a = numpy.empty(shape + (mask_size + 1, polynomial_degree), Torus32)
result_cv = numpy.empty(shape, ErrorFloat)
noises1 = get_test_array(shape + (mask_size, polynomial_degree), Torus32)
noises2 = get_test_array(shape + (polynomial_degree,), Torus32)
key = get_test_array((mask_size, polynomial_degree), Int32, (0, 2))
test = TLweEncryptZero(params, shape, noise, perf_params).compile(thread)
ref = TLweEncryptZeroReference(params, shape, noise, perf_params)
result_a_dev = thread.empty_like(result_a)
result_cv_dev = thread.empty_like(result_cv)
noises1_dev = thread.to_device(noises1)
noises2_dev = thread.to_device(noises2)
key_dev = thread.to_device(key)
test(result_a_dev, result_cv_dev, key_dev, noises1_dev, noises2_dev)
ref(result_a, result_cv, key, noises1, noises2)
result_a_test = result_a_dev.get()
result_cv_test = result_cv_dev.get()
assert (result_a_test == result_a).all()
assert errors_allclose(result_cv_test, result_cv)