forked from kimlaine/bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
polyarith.h
147 lines (136 loc) · 5.37 KB
/
polyarith.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#pragma once
#include <cstdint>
#include "seal/util/uintcore.h"
#include "seal/util/uintarith.h"
#include "seal/util/uintarithmod.h"
#include "seal/util/polycore.h"
#include "seal/util/pointer.h"
namespace seal
{
namespace util
{
inline void right_shift_poly_coeffs(
const std::uint64_t *poly, std::size_t coeff_count,
std::size_t coeff_uint64_count, int shift_amount,
std::uint64_t *result)
{
#ifdef SEAL_DEBUG
if (poly == nullptr && coeff_count > 0 && coeff_uint64_count > 0)
{
throw std::invalid_argument("poly");
}
#endif
while (coeff_count--)
{
right_shift_uint(poly, shift_amount, coeff_uint64_count, result);
poly += coeff_uint64_count;
result += coeff_uint64_count;
}
}
inline void negate_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 == nullptr && coeff_count > 0 && coeff_uint64_count > 0)
{
throw std::invalid_argument("poly");
}
if (result == nullptr && coeff_count > 0 && coeff_uint64_count > 0)
{
throw std::invalid_argument("result");
}
#endif
while(coeff_count--)
{
negate_uint(poly, coeff_uint64_count, result);
poly += coeff_uint64_count;
result += coeff_uint64_count;
}
}
inline void add_poly_poly(const std::uint64_t *operand1,
const std::uint64_t *operand2, std::size_t coeff_count,
std::size_t coeff_uint64_count, std::uint64_t *result)
{
#ifdef SEAL_DEBUG
if (operand1 == nullptr && coeff_count > 0 && coeff_uint64_count > 0)
{
throw std::invalid_argument("operand1");
}
if (operand2 == nullptr && coeff_count > 0 && coeff_uint64_count > 0)
{
throw std::invalid_argument("operand2");
}
if (result == nullptr && coeff_count > 0 && coeff_uint64_count > 0)
{
throw std::invalid_argument("result");
}
#endif
while(coeff_count--)
{
add_uint_uint(operand1, operand2, coeff_uint64_count, result);
operand1 += coeff_uint64_count;
operand2 += coeff_uint64_count;
result += coeff_uint64_count;
}
}
inline void sub_poly_poly(const std::uint64_t *operand1,
const std::uint64_t *operand2, std::size_t coeff_count,
std::size_t coeff_uint64_count, std::uint64_t *result)
{
#ifdef SEAL_DEBUG
if (operand1 == nullptr && coeff_count > 0 && coeff_uint64_count > 0)
{
throw std::invalid_argument("operand1");
}
if (operand2 == nullptr && coeff_count > 0 && coeff_uint64_count > 0)
{
throw std::invalid_argument("operand2");
}
if (result == nullptr && coeff_count > 0 && coeff_uint64_count > 0)
{
throw std::invalid_argument("result");
}
#endif
while(coeff_count--)
{
sub_uint_uint(operand1, operand2, coeff_uint64_count, result);
operand1 += coeff_uint64_count;
operand2 += coeff_uint64_count;
result += coeff_uint64_count;
}
}
void multiply_poly_poly(
const std::uint64_t *operand1, std::size_t operand1_coeff_count,
std::size_t operand1_coeff_uint64_count, const std::uint64_t *operand2,
std::size_t operand2_coeff_count, std::size_t operand2_coeff_uint64_count,
std::size_t result_coeff_count, std::size_t result_coeff_uint64_count,
std::uint64_t *result, MemoryPool &pool);
inline void poly_infty_norm(const std::uint64_t *poly,
std::size_t coeff_count, std::size_t coeff_uint64_count,
std::uint64_t *result)
{
set_zero_uint(coeff_uint64_count, result);
while(coeff_count--)
{
if (is_greater_than_uint_uint(poly, result, coeff_uint64_count))
{
set_uint_uint(poly, coeff_uint64_count, result);
}
poly += coeff_uint64_count;
}
}
void poly_eval_poly(const std::uint64_t *poly_to_eval,
std::size_t poly_to_eval_coeff_count,
std::size_t poly_to_eval_coeff_uint64_count, const std::uint64_t *value,
std::size_t value_coeff_count, std::size_t value_coeff_uint64_count,
std::size_t result_coeff_count, std::size_t result_coeff_uint64_count,
std::uint64_t *result, MemoryPool &pool);
void exponentiate_poly(const std::uint64_t *poly, std::size_t poly_coeff_count,
std::size_t poly_coeff_uint64_count, const std::uint64_t *exponent,
std::size_t exponent_uint64_count, std::size_t result_coeff_count,
std::size_t result_coeff_uint64_count, std::uint64_t *result, MemoryPool &pool);
}
}