forked from microsoft/SEAL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolycore.h
188 lines (174 loc) · 6.09 KB
/
polycore.h
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#pragma once
#include "seal/util/common.h"
#include "seal/util/pointer.h"
#include "seal/util/uintcore.h"
#include <algorithm>
#include <cstdint>
#include <cstring>
#include <limits>
#include <sstream>
#include <stdexcept>
namespace seal
{
namespace util
{
SEAL_NODISCARD inline std::string poly_to_hex_string(
const std::uint64_t *value, std::size_t coeff_count, std::size_t coeff_uint64_count)
{
#ifdef SEAL_DEBUG
if (!value)
{
throw std::invalid_argument("value");
}
#endif
// First check if there is anything to print
if (!coeff_count || !coeff_uint64_count)
{
return "0";
}
std::ostringstream result;
bool empty = true;
value += util::mul_safe(coeff_count - 1, coeff_uint64_count);
while (coeff_count--)
{
if (is_zero_uint(value, coeff_uint64_count))
{
value -= coeff_uint64_count;
continue;
}
if (!empty)
{
result << " + ";
}
result << uint_to_hex_string(value, coeff_uint64_count);
if (coeff_count)
{
result << "x^" << coeff_count;
}
empty = false;
value -= coeff_uint64_count;
}
if (empty)
{
result << "0";
}
return result.str();
}
SEAL_NODISCARD inline std::string poly_to_dec_string(
const std::uint64_t *value, std::size_t coeff_count, std::size_t coeff_uint64_count, MemoryPool &pool)
{
#ifdef SEAL_DEBUG
if (!value)
{
throw std::invalid_argument("value");
}
#endif
// First check if there is anything to print
if (!coeff_count || !coeff_uint64_count)
{
return "0";
}
std::ostringstream result;
bool empty = true;
value += coeff_count - 1;
while (coeff_count--)
{
if (is_zero_uint(value, coeff_uint64_count))
{
value -= coeff_uint64_count;
continue;
}
if (!empty)
{
result << " + ";
}
result << uint_to_dec_string(value, coeff_uint64_count, pool);
if (coeff_count)
{
result << "x^" << coeff_count;
}
empty = false;
value -= coeff_uint64_count;
}
if (empty)
{
result << "0";
}
return result.str();
}
SEAL_NODISCARD inline auto allocate_poly(
std::size_t coeff_count, std::size_t coeff_uint64_count, MemoryPool &pool)
{
return allocate_uint(util::mul_safe(coeff_count, coeff_uint64_count), pool);
}
inline void set_zero_poly(std::size_t coeff_count, std::size_t coeff_uint64_count, std::uint64_t *result)
{
#ifdef SEAL_DEBUG
if (!result && coeff_count && coeff_uint64_count)
{
throw std::invalid_argument("result");
}
#endif
set_zero_uint(util::mul_safe(coeff_count, coeff_uint64_count), result);
}
SEAL_NODISCARD inline auto allocate_zero_poly(
std::size_t coeff_count, std::size_t coeff_uint64_count, MemoryPool &pool)
{
return allocate_zero_uint(util::mul_safe(coeff_count, coeff_uint64_count), pool);
}
SEAL_NODISCARD inline auto allocate_poly_array(
std::size_t poly_count, std::size_t coeff_count, std::size_t coeff_uint64_count, MemoryPool &pool)
{
return allocate_uint(util::mul_safe(poly_count, coeff_count, coeff_uint64_count), pool);
}
inline void set_zero_poly_array(
std::size_t poly_count, std::size_t coeff_count, std::size_t coeff_uint64_count, std::uint64_t *result)
{
#ifdef SEAL_DEBUG
if (!result && poly_count && coeff_count && coeff_uint64_count)
{
throw std::invalid_argument("result");
}
#endif
set_zero_uint(util::mul_safe(poly_count, coeff_count, coeff_uint64_count), result);
}
SEAL_NODISCARD inline auto allocate_zero_poly_array(
std::size_t poly_count, std::size_t coeff_count, std::size_t coeff_uint64_count, MemoryPool &pool)
{
return allocate_zero_uint(util::mul_safe(poly_count, coeff_count, coeff_uint64_count), pool);
}
inline void set_poly(
const std::uint64_t *poly, std::size_t coeff_count, std::size_t coeff_uint64_count, std::uint64_t *result)
{
#ifdef SEAL_DEBUG
if (!poly && coeff_count && coeff_uint64_count)
{
throw std::invalid_argument("poly");
}
if (!result && coeff_count && coeff_uint64_count)
{
throw std::invalid_argument("result");
}
#endif
set_uint(poly, util::mul_safe(coeff_count, coeff_uint64_count), result);
}
inline void set_poly_array(
const std::uint64_t *poly, std::size_t poly_count, std::size_t coeff_count, std::size_t coeff_uint64_count,
std::uint64_t *result)
{
#ifdef SEAL_DEBUG
if (!poly && poly_count && coeff_count && coeff_uint64_count)
{
throw std::invalid_argument("poly");
}
if (!result && poly_count && coeff_count && coeff_uint64_count)
{
throw std::invalid_argument("result");
}
#endif
set_uint(poly, util::mul_safe(poly_count, coeff_count, coeff_uint64_count), result);
}
} // namespace util
} // namespace seal