-
Notifications
You must be signed in to change notification settings - Fork 289
Expand file tree
/
Copy pathNbTheory.cpp
More file actions
396 lines (336 loc) · 11.5 KB
/
NbTheory.cpp
File metadata and controls
396 lines (336 loc) · 11.5 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
//==================================================================================
// BSD 2-Clause License
//
// Copyright (c) 2014-2022, NJIT, Duality Technologies Inc. and other contributors
//
// All rights reserved.
//
// Author TPOC: contact@openfhe.org
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//==================================================================================
/*
* This code benchmarks number theory operations.
*/
#define _USE_MATH_DEFINES
#include "lattice/lat-hal.h"
#include "benchmark/benchmark.h"
#include <iostream>
using namespace lbcrypto;
//==================================
// GCD benchmarks
// this benchmark returns a reference to a BBI which can be used for output
static BigInteger GCD_equals_small_numbers(void) { // function
BigInteger a("10403"), b("103");
BigInteger c(lbcrypto::GreatestCommonDivisor(a, b));
return (c);
}
// this benchmark sets the output label with a result from the function
static void BM_GCD1(benchmark::State& state) { // benchmark
int out = 0;
while (state.KeepRunning()) {
benchmark::DoNotOptimize(GCD_equals_small_numbers());
}
// Prevent compiler optimizations (note I haven't seen the complier optimize
// code out if we leave this out... )
std::stringstream ss;
ss << out;
state.SetLabel(ss.str().c_str()); // label attached to output
}
BENCHMARK(BM_GCD1); // register benchmark
// this benchmark returns an int. In some cases the return BBI value
// cannot be converted to an int (too big) so you need to return a
// reference to the BBI instead. this time we can use an int.
static int GCD_equals_powers_of_two_numbers(void) {
BigInteger a("1048576"), b("4096");
BigInteger c(lbcrypto::GreatestCommonDivisor(a, b));
return (c.ConvertToInt());
}
static void BM_GCD2(benchmark::State& state) { // benchmark
int out = 0;
while (state.KeepRunning()) {
out = GCD_equals_powers_of_two_numbers();
}
// Prevent compiler optimizations
std::stringstream ss;
ss << out;
state.SetLabel(ss.str().c_str());
}
BENCHMARK(BM_GCD2); // register benchmark
//===================================================
// the following benchmark MillerRabinPrimalityTest for various inputs
//
// returns boolean
static bool MRP_is_prime_small_prime(void) { // function
BigInteger prime("24469");
return (lbcrypto::MillerRabinPrimalityTest(prime));
}
static void BM_MRP1(benchmark::State& state) { // benchmark
int out = 0;
while (state.KeepRunning()) {
out = MRP_is_prime_small_prime();
}
// Prevent compiler optimizations
std::stringstream ss;
ss << out;
state.SetLabel(ss.str().c_str());
}
BENCHMARK(BM_MRP1); // register benchmark
//
static bool MRP_is_prime_big_prime(void) { // function
BigInteger prime("952229140957");
return (lbcrypto::MillerRabinPrimalityTest(prime));
}
static void BM_MRP2(benchmark::State& state) { // benchmark
bool out = 0;
while (state.KeepRunning()) {
out = MRP_is_prime_big_prime();
}
// Prevent compiler optimizations
std::stringstream ss;
ss << out;
state.SetLabel(ss.str().c_str());
}
BENCHMARK(BM_MRP2); // register benchmark
//
static bool MRP_is_not_prime_small_composite_number(void) { // function
BigInteger isNotPrime("10403");
return (lbcrypto::MillerRabinPrimalityTest(isNotPrime));
}
static void BM_MRP3(benchmark::State& state) { // benchmark
bool out = 0;
while (state.KeepRunning()) {
out = MRP_is_not_prime_small_composite_number();
}
// Prevent compiler optimizations
std::stringstream ss;
ss << out;
state.SetLabel(ss.str().c_str());
}
BENCHMARK(BM_MRP3); // register benchmark
//
static bool MRP_is_not_prime_big_composite_number(void) { // function
BigInteger isNotPrime("952229140959");
return (lbcrypto::MillerRabinPrimalityTest(isNotPrime));
}
static void BM_MRP4(benchmark::State& state) { // benchmark
bool out = 0;
while (state.KeepRunning()) {
out = MRP_is_not_prime_big_composite_number();
}
// Prevent compiler optimizations
std::stringstream ss;
ss << out;
state.SetLabel(ss.str().c_str());
}
BENCHMARK(BM_MRP4); // register benchmark
//========================================
// the following does not return anything..
static void factorize_returns_factors(void) {
BigInteger comp("53093040");
std::set<BigInteger> factors;
lbcrypto::PrimeFactorize(comp, factors);
}
static void BM_FACT1(benchmark::State& state) {
while (state.KeepRunning()) {
// note you cannot use benchmark::DoNotOptimize() here because
// factorize_returns_factors() is a void, it must return a value
factorize_returns_factors();
}
}
BENCHMARK(BM_FACT1); // register benchmark
//======================================
// Prime Modulus tests
//
static BigInteger PM_foundPrimeModulus(void) {
const uint32_t m = 2048;
const uint32_t nBits = 30;
return lbcrypto::FirstPrime<BigInteger>(nBits, m);
}
static void BM_PM1(benchmark::State& state) { // benchmark
BigInteger out;
while (state.KeepRunning()) {
out = PM_foundPrimeModulus();
}
// Prevent compiler optimizations
std::stringstream ss;
ss << out.ToString();
state.SetLabel(ss.str().c_str());
}
BENCHMARK(BM_PM1); // register benchmark
#if 0 // this benchmark has not been tested
// note this returns a refrence to BBI
static BigInteger& PM_returns_higher_bit_length(void) {
uint32_t m = 4096;
uint32_t nBits = 49;
BigInteger primeModulus = lbcrypto::FirstPrime<BigInteger>(nBits, m);
return primeModulus;
}
// saving the reference to BBI for output adds some copy overhead
static void BM_PM2(benchmark::State& state) {
BigInteger out;
while (state.KeepRunning()) {
out = PM_returns_higher_bit_length();
}
// Prevent compiler optimizations
std::stringstream ss;
ss << out.ToString();
state.SetLabel(ss.str().c_str());
}
BENCHMARK(BM_PM2); // register benchmark
#endif
// Note this benchmark returns two BBIs so we return a string and suffer
// some overhead
static std::string PROU_equals_m_not_equals_mbytwo(void) {
uint32_t m = 4096;
uint32_t nBits = 33;
BigInteger primeModulus = lbcrypto::FirstPrime<BigInteger>(nBits, m);
BigInteger primitiveRootOfUnity = lbcrypto::RootOfUnity<BigInteger>(m, primeModulus);
BigInteger M(std::to_string(m)), MbyTwo(M.DividedBy(2));
BigInteger wpowerm = primitiveRootOfUnity.ModExp(M, primeModulus);
BigInteger wpowermbytwo = primitiveRootOfUnity.ModExp(MbyTwo, primeModulus);
return (wpowerm.ToString() + " " + wpowermbytwo.ToString());
}
static void BM_PROU1(benchmark::State& state) {
std::string out;
while (state.KeepRunning()) {
out = PROU_equals_m_not_equals_mbytwo();
}
// Prevent compiler optimizations
std::stringstream ss;
ss << out;
state.SetLabel(ss.str().c_str());
}
BENCHMARK(BM_PROU1); // register benchmark
#if 0 // this takes a long time to run so comment out for quick check
// similarly this outputs 3 values with a string
static std::string PROU_equals_m_not_equals_mbytwo_mbyfour_single_input(void) {
const uint32_t n = 2048;
const uint32_t m = 2*n;
const uint32_t nBits = 43;
const int ITERATIONS = m*2;
BigInteger M(std::to_string(m)),
MbyTwo(M.DividedBy(BigInteger::TWO)),
MbyFour(MbyTwo.DividedBy(BigInteger::TWO));
BigInteger primeModulus = lbcrypto::FirstPrime<BigInteger>(nBits, m);
BigInteger wpowerm("0");
BigInteger wpowermbytwo("0");
BigInteger wpowermbyfour("0");
for (int i = 0; i < ITERATIONS; i++) {
BigInteger primitiveRootOfUnity = lbcrypto::RootOfUnity<BigInteger>(m, primeModulus);
wpowerm = primitiveRootOfUnity.ModExp(M, primeModulus);
wpowermbytwo = primitiveRootOfUnity.ModExp(MbyTwo, primeModulus);
wpowermbyfour = primitiveRootOfUnity.ModExp(MbyFour, primeModulus);
}
return(wpowerm.ToString() + " " +
wpowermbytwo.ToString() + " " +
wpowermbyfour.ToString());
}
static void BM_PROU2(benchmark::State& state) {
std::string out;
while (state.KeepRunning()) {
out = PROU_equals_m_not_equals_mbytwo_mbyfour_single_input();
}
// Prevent compiler optimizations
std::stringstream ss;
ss << out;
state.SetLabel(ss.str().c_str());
}
BENCHMARK(BM_PROU2);
#endif
// similarly this outputs 3 values with a string
static std::string PROU_equals_m_not_equals_mbytwo_mbyfour_multiple_inputs(void) {
uint32_t nqBitsArray[] = {
1,
1,
2,
4,
8,
20,
1024,
30,
2048,
31,
2048,
33,
2048,
40,
2048,
41
// const uint32_t BIT_LENGTH = 200 and const uint32_t FRAGMENTATION_FACTOR = 27
// ,2048, 51
,
4096,
32,
4096,
43
// ,4096, 53
,
8192,
33,
8192,
44
// ,8192, 55
,
16384,
34,
16384,
46
// ,16384, 57
,
32768,
35,
32768,
47
// ,32768, 59
};
int length = sizeof(nqBitsArray) / sizeof(nqBitsArray[0]);
uint32_t n, qBits, m;
BigInteger wpowerm("0");
BigInteger wpowermbytwo("0");
BigInteger wpowermbyfour("0");
for (int i = 2; i < length; i += 2) {
n = nqBitsArray[i];
qBits = nqBitsArray[i + 1];
m = 2 * n;
BigInteger M(std::to_string(m)), MbyTwo(M.DividedBy(2)), MbyFour(MbyTwo.DividedBy(2));
BigInteger primeModulus = lbcrypto::FirstPrime<BigInteger>(qBits, m);
BigInteger primitiveRootOfUnity(lbcrypto::RootOfUnity<BigInteger>(m, primeModulus));
wpowerm = primitiveRootOfUnity.ModExp(M, primeModulus);
wpowermbytwo = primitiveRootOfUnity.ModExp(MbyTwo, primeModulus);
wpowermbyfour = primitiveRootOfUnity.ModExp(MbyFour, primeModulus);
}
return (wpowerm.ToString() + " " + wpowermbytwo.ToString() + " " + wpowermbyfour.ToString());
}
static void BM_PROU3(benchmark::State& state) { // benchmark
std::string out;
while (state.KeepRunning()) {
out = PROU_equals_m_not_equals_mbytwo_mbyfour_multiple_inputs();
}
// Prevent compiler optimizations
std::stringstream ss;
ss << out;
state.SetLabel(ss.str().c_str());
}
BENCHMARK(BM_PROU3); // register benchmark
// execute the benchmarks
BENCHMARK_MAIN();