Repository navigation
Expand file tree
/
Copy pathgalois.cpp
More file actions
180 lines (163 loc) · 7.36 KB
/
Copy pathgalois.cpp
File metadata and controls
180 lines (163 loc) · 7.36 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#include "seal/context.h"
#include "seal/memorymanager.h"
#include "seal/util/galois.h"
#include <limits>
#include <stdexcept>
#include <vector>
#include "gtest/gtest.h"
using namespace seal;
using namespace seal::util;
using namespace std;
namespace sealtest
{
namespace util
{
TEST(GaloisToolTest, Create)
{
auto pool = MemoryManager::GetPool();
ASSERT_THROW(GaloisTool galois_tool(0, pool), invalid_argument);
ASSERT_THROW(GaloisTool galois_tool(18, pool), invalid_argument);
ASSERT_NO_THROW(GaloisTool galois_tool(1, pool));
ASSERT_NO_THROW(GaloisTool galois_tool(17, pool));
}
TEST(GaloisToolTest, EltFromStep)
{
auto pool = MemoryManager::GetPool();
{
GaloisTool galois_tool(3, pool);
ASSERT_EQ(15U, galois_tool.get_elt_from_step(0));
ASSERT_EQ(3U, galois_tool.get_elt_from_step(1));
ASSERT_EQ(3U, galois_tool.get_elt_from_step(-3));
ASSERT_EQ(9U, galois_tool.get_elt_from_step(2));
ASSERT_EQ(9U, galois_tool.get_elt_from_step(-2));
ASSERT_EQ(11U, galois_tool.get_elt_from_step(3));
ASSERT_EQ(11U, galois_tool.get_elt_from_step(-1));
}
}
TEST(GaloisToolTest, EltFromStepRejectsExtremeSteps)
{
auto pool = MemoryManager::GetPool();
GaloisTool galois_tool(3, pool);
// The magnitude of these steps exceeds half the polynomial degree. INT_MIN has no
// representable negation as a signed integer and must be handled like any other
// out-of-range step.
ASSERT_THROW(
static_cast<void>(galois_tool.get_elt_from_step((numeric_limits<int>::min)())), invalid_argument);
ASSERT_THROW(
static_cast<void>(galois_tool.get_elt_from_step((numeric_limits<int>::max)())), invalid_argument);
ASSERT_THROW(static_cast<void>(galois_tool.get_elt_from_step(4)), invalid_argument);
ASSERT_THROW(static_cast<void>(galois_tool.get_elt_from_step(-4)), invalid_argument);
}
TEST(GaloisToolTest, EltsFromSteps)
{
auto pool = MemoryManager::GetPool();
{
GaloisTool galois_tool(3, pool);
auto elts = galois_tool.get_elts_from_steps({ 0, 1, -3, 2, -2, 3, -1 });
uint32_t elts_true[7]{ 15, 3, 3, 9, 9, 11, 11 };
for (size_t i = 0; i < elts.size(); i++)
{
ASSERT_EQ(elts_true[i], elts[i]);
}
}
}
TEST(GaloisToolTest, EltsAll)
{
auto pool = MemoryManager::GetPool();
{
GaloisTool galois_tool(3, pool);
auto elts = galois_tool.get_elts_all();
uint32_t elts_true[5]{ 15, 3, 11, 9, 9 };
for (size_t i = 0; i < elts.size(); i++)
{
ASSERT_EQ(elts_true[i], elts[i]);
}
}
}
TEST(GaloisToolTest, IndexFromElt)
{
ASSERT_EQ(7, GaloisTool::GetIndexFromElt(15));
ASSERT_EQ(1, GaloisTool::GetIndexFromElt(3));
ASSERT_EQ(4, GaloisTool::GetIndexFromElt(9));
ASSERT_EQ(5, GaloisTool::GetIndexFromElt(11));
ASSERT_EQ(SEAL_POLY_MOD_DEGREE_MAX - 1, GaloisTool::GetIndexFromElt((SEAL_POLY_MOD_DEGREE_MAX << 1) - 1));
ASSERT_THROW(static_cast<void>(GaloisTool::GetIndexFromElt(0)), invalid_argument);
ASSERT_THROW(static_cast<void>(GaloisTool::GetIndexFromElt(2)), invalid_argument);
ASSERT_THROW(
static_cast<void>(GaloisTool::GetIndexFromElt((SEAL_POLY_MOD_DEGREE_MAX << 1) + 1)), invalid_argument);
ASSERT_THROW(static_cast<void>(GaloisTool::GetIndexFromElt(UINT32_MAX)), invalid_argument);
}
TEST(GaloisToolTest, ApplyGalois)
{
EncryptionParameters parms(scheme_type::ckks);
parms.set_poly_modulus_degree(8);
parms.set_coeff_modulus({ 17 });
SEALContext context(parms, false, sec_level_type::none);
auto context_data = context.key_context_data();
auto galois_tool = context_data->galois_tool();
uint64_t in[8]{ 0, 1, 2, 3, 4, 5, 6, 7 };
uint64_t out[8];
uint64_t out_true[8]{ 0, 14, 6, 1, 13, 7, 2, 12 };
galois_tool->apply_galois(in, 3, Modulus(17), out);
for (size_t i = 0; i < 8; i++)
{
ASSERT_EQ(out_true[i], out[i]);
}
}
TEST(GaloisToolTest, ApplyGaloisNTT)
{
EncryptionParameters parms(scheme_type::ckks);
parms.set_poly_modulus_degree(8);
parms.set_coeff_modulus({ 17 });
SEALContext context(parms, false, sec_level_type::none);
auto context_data = context.key_context_data();
auto galois_tool = context_data->galois_tool();
uint64_t in[8]{ 0, 1, 2, 3, 4, 5, 6, 7 };
uint64_t out[8];
uint64_t out_true[8]{ 4, 5, 7, 6, 1, 0, 2, 3 };
const_cast<GaloisTool *>(galois_tool)->apply_galois_ntt(in, 3, out);
for (size_t i = 0; i < 8; i++)
{
ASSERT_EQ(out_true[i], out[i]);
}
}
TEST(GaloisToolTest, PermutationTableNTT)
{
EncryptionParameters parms(scheme_type::ckks);
parms.set_poly_modulus_degree(8);
parms.set_coeff_modulus({ 17 });
SEALContext context(parms, false, sec_level_type::none);
auto galois_tool = context.key_context_data()->galois_tool();
// As in ApplyGaloisNTT
const uint32_t *table = galois_tool->get_permutation_table_ntt(3);
uint32_t table_true[8]{ 4, 5, 7, 6, 1, 0, 2, 3 };
for (size_t i = 0; i < 8; i++)
{
ASSERT_EQ(table_true[i], table[i]);
}
// Cached permutations, as applied by apply_galois_ntt
uint64_t in[8]{ 9, 10, 11, 12, 13, 14, 15, 16 };
for (uint32_t galois_elt = 1; galois_elt < 16; galois_elt += 2)
{
table = galois_tool->get_permutation_table_ntt(galois_elt);
ASSERT_EQ(table, galois_tool->get_permutation_table_ntt(galois_elt));
uint64_t out[8];
galois_tool->apply_galois_ntt(in, galois_elt, out);
vector<bool> seen(8, false);
for (size_t i = 0; i < 8; i++)
{
ASSERT_LT(table[i], 8U);
ASSERT_FALSE(seen[table[i]]);
seen[table[i]] = true;
ASSERT_EQ(in[table[i]], out[i]);
}
}
ASSERT_THROW(static_cast<void>(galois_tool->get_permutation_table_ntt(0)), invalid_argument);
ASSERT_THROW(static_cast<void>(galois_tool->get_permutation_table_ntt(2)), invalid_argument);
ASSERT_THROW(static_cast<void>(galois_tool->get_permutation_table_ntt(16)), invalid_argument);
ASSERT_THROW(static_cast<void>(galois_tool->get_permutation_table_ntt(17)), invalid_argument);
}
} // namespace util
} // namespace sealtest