Skip to content

Commit f8230cd

Browse files
authored
Public Dev Sync-4 November 2020
HElib 1.2.0, November 2020 ========================= (tagged as v1.2.0) October-November 2020 --------------------- * Mitigation for potential CKKS vulnerability. * Additional API changes for parameters to the CKKS encoding functions. * New infrastructure for benchmarks * Updated ClonedPtr * Additional Ctxt tests * Bug Fixes: Co-Authored-by: Flavio Bergamaschi anadyomeni@gmx.com Co-authored-by: Jack Crawford jack.crawford@sky.com Co-Authored-by: Shai Halevi shaih@alum.mit.edu Co-authored-by: Hamish Hunt hamishun@gmail.com Co-authored-by: Victor Shoup shoup@cs.nyu.edu Co-authored-by: Enrico Steffinlongo enrylongo@gmail.com
2 parents c5a3ae5 + 9cec8a2 commit f8230cd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+2522
-589
lines changed

CHANGES.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
HElib 1.2.0, November 2020
2+
=========================
3+
(tagged as v1.2.0)
4+
5+
October-November 2020
6+
---------------------
7+
* Mitigation for potential CKKS vulnerability.
8+
* Additional API changes for parameters to the CKKS encoding functions.
9+
* New infrastructure for benchmarks
10+
* Updated ClonedPtr
11+
* Additional Ctxt tests
12+
* Bug Fixes
13+
114
HElib 1.1.0, October 2020
215
=========================
316
(tagged as v1.1.0)

INSTALL.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Building and installing HElib
22

33
The HElib build, install, and regression tests suite have been built and tested
4-
on Ubuntu 18.04, Ubuntu 20.04, Fedora 31, Fedora 32, CentOS 7.7, CentOS 8.1,
5-
and macOS Mojave 10.14.
4+
on Ubuntu 18.04, Ubuntu 20.04, Fedora 32, Fedora 33, CentOS 7.8, CentOS 8.2,
5+
macOS Mojave >=10.14.6, and macOS Catalina >=10.15.7.
66

77
There are two different ways to build and install HElib. The first one will
88
automatically download and build the GMP and NTL dependencies and pack the
@@ -23,7 +23,7 @@ dependencies to be installed by you and available in the system.
2323
- cmake >= 3.10.2
2424

2525
**macOS environment:**
26-
- Apple clang >= 11.0.0 (available with Xcode >= 11.0)
26+
- Apple clang >= 11.0.0 (available with the latest Xcode for the tested versions of macOS)
2727
- Xcode Command Line Tools (can be installed with the command `xcode-select
2828
--install` in a teminal)
2929
- cmake >= 3.17.3 (available from [CMake](https://cmake.org/) or [MacPorts

benchmarks/CMakeLists.txt

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
3131
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})
3232
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})
3333

34+
# Will be compiled into single executable
35+
option(SINGLE_EXEC "Compile into single excutable (ON/OFF)" OFF)
36+
if(SINGLE_EXEC)
37+
message(STATUS "Single executable activated.")
38+
endif()
3439

3540
# Setting up RelWithDebInfo as default CMAKE_BUILD_TYPE
3641
if (NOT CMAKE_BUILD_TYPE)
@@ -42,9 +47,31 @@ endif (NOT CMAKE_BUILD_TYPE)
4247
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
4348
"Debug" "RelWithDebInfo" "Release" "MinSizeRel")
4449

45-
find_package(helib 1.0.0 EXACT REQUIRED)
50+
find_package(helib 1.1.0 EXACT REQUIRED)
4651
find_package(benchmark REQUIRED)
4752

48-
# Add each benchmark source file to the executable list
49-
add_executable(helib_benchmark bench_thinboot.cpp bench_fatboot.cpp)
50-
target_link_libraries(helib_benchmark benchmark::benchmark_main helib)
53+
# Targets are simply associated with their source files.
54+
set(TRGTS bgv_basic
55+
bgv_thinboot
56+
bgv_fatboot
57+
ckks_basic)
58+
59+
# Sources derived from their targets.
60+
set(SRCS "")
61+
foreach(TRGT ${TRGTS})
62+
list(APPEND SRCS "${TRGT}.cpp")
63+
endforeach()
64+
65+
if(SINGLE_EXEC)
66+
# Add each benchmark source file to the executable list
67+
add_executable(helib_benchmark ${SRCS})
68+
# Specify the libraries each target is dependent upon
69+
target_link_libraries(helib_benchmark benchmark::benchmark_main helib)
70+
else(SINGLE_EXEC)
71+
foreach(TRGT ${TRGTS})
72+
# Add each benchmark source file to the executable list
73+
add_executable(${TRGT} ${TRGT}.cpp)
74+
# Specify the libraries each target is dependent upon
75+
target_link_libraries(${TRGT} benchmark::benchmark_main helib)
76+
endforeach()
77+
endif(SINGLE_EXEC)

benchmarks/README.md

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
# Building and running HElib's benchmarks
1+
# Some HElib benchmarks
22

3-
## Instructions
3+
## Prerequisites
44

55
### Build and install HElib
66

7-
Build and install HElib as in [INSTALL.md](../INSTALL.md).
7+
Build and install HElib as described in [INSTALL.md](../INSTALL.md).
88

99
### Build and install google benchmark
1010

1111
Download the library https://github.com/google/benchmark from github, build and
1212
then install it.
13+
1314
```
1415
git clone https://github.com/google/benchmark
1516
cd benchmark
@@ -20,11 +21,14 @@ make
2021
make install
2122
```
2223

23-
### Write a benchmark test and link to both installed libraries.
24+
## Build benchmark
25+
26+
Benchmarks are found in `HElib/benchmarks` and are grouped by themes; the file
27+
names reflect the themes they belong to. By default, the benchmarks are built as
28+
separate executables. If you prefer to build a single executable then set the
29+
`-DSINGLE_EXEC=ON`. Compiled tests appear in the `bin` directory. Now build
30+
the project.
2431

25-
Write the benchmark in an existing source file or a new one in
26-
`HElib/benchmarks`, adding the new source file to `CMakeLists.txt` if
27-
appropriate, then build the project.
2832
```
2933
cd HElib/benchmarks
3034
mkdir build
@@ -33,9 +37,25 @@ cmake -Dhelib_DIR=<helib installation dir>/share/cmake/helib -Dbenchmark_DIR=<be
3337
make
3438
```
3539

36-
When writing benchmarks use `BENCHMARK_CAPTURE(custom params)` for custom
37-
params and the extra option `BENCHMARK_CAPTURE()->Iterations(iterations)` for a
38-
specific number of iterations or `BENCHMARK_CAPTURE()->MinTime(time in
39-
seconds)` to set a minimum time for the benchmark to run for.
40+
When writing benchmarks please use `BENCHMARK_CAPTURE(<custom params>)` for
41+
custom parameters and do not forget to add the new target to `CMakeLists.txt`.
42+
Use the extra option `BENCHMARK_CAPTURE()->Iterations(<iterations>)` for a
43+
specific number of iterations or `BENCHMARK_CAPTURE()->MinTime(<time in
44+
seconds>)` to set a minimum time for the benchmark to run for.
45+
46+
NOTE: Both `Iterations` and `MinTime` cannot be used together.
4047

41-
NOTE: Both options cannot be used at the same time.
48+
## Run benchmark
49+
50+
To execute individual tests run the following
51+
52+
```
53+
./bin/<benchmark to run>
54+
```
55+
56+
If you have compiled the tests as a single executable using the
57+
`-DSINGLE_EXEC=ON` flag, you can execute the tests by running the following
58+
59+
```
60+
./bin/helib_benchmark
61+
```

benchmarks/bgv_basic.cpp

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/* Copyright (C) 2020 IBM Corp.
2+
* This program is Licensed under the Apache License, Version 2.0
3+
* (the "License"); you may not use this file except in compliance
4+
* with the License. You may obtain a copy of the License at
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
* Unless required by applicable law or agreed to in writing, software
7+
* distributed under the License is distributed on an "AS IS" BASIS,
8+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
* See the License for the specific language governing permissions and
10+
* limitations under the License. See accompanying LICENSE file.
11+
*/
12+
13+
#include <benchmark/benchmark.h>
14+
#include <iostream>
15+
16+
#include <helib/helib.h>
17+
18+
#include "bgv_common.h"
19+
20+
namespace {
21+
22+
static void adding_two_ciphertexts(benchmark::State& state, Meta& meta)
23+
{
24+
helib::Ptxt<helib::BGV> ptxt1(meta.data->context);
25+
helib::Ptxt<helib::BGV> ptxt2(meta.data->context);
26+
27+
ptxt1.random();
28+
ptxt2.random();
29+
30+
helib::Ctxt ctxt1(meta.data->publicKey);
31+
helib::Ctxt ctxt2(meta.data->publicKey);
32+
33+
meta.data->publicKey.Encrypt(ctxt1, ptxt1);
34+
meta.data->publicKey.Encrypt(ctxt2, ptxt1);
35+
// Benchmark adding ciphertexts
36+
for (auto _ : state)
37+
ctxt1 += ctxt2;
38+
std::cout << "Additions performed = " << state.iterations() << std::endl;
39+
}
40+
41+
static void multiplying_two_ciphertexts(benchmark::State& state, Meta& meta)
42+
{
43+
helib::Ptxt<helib::BGV> ptxt1(meta.data->context);
44+
helib::Ptxt<helib::BGV> ptxt2(meta.data->context);
45+
46+
ptxt1.random();
47+
ptxt2.random();
48+
49+
helib::Ctxt ctxt1(meta.data->publicKey);
50+
helib::Ctxt ctxt2(meta.data->publicKey);
51+
52+
meta.data->publicKey.Encrypt(ctxt1, ptxt1);
53+
meta.data->publicKey.Encrypt(ctxt2, ptxt2);
54+
// Benchmark adding ciphertexts
55+
for (auto _ : state)
56+
ctxt1.multiplyBy(ctxt2);
57+
std::cout << "Multiplications performed = " << state.iterations()
58+
<< std::endl;
59+
}
60+
61+
static void encrypting_ciphertexts(benchmark::State& state, Meta& meta)
62+
{
63+
helib::Ptxt<helib::BGV> ptxt(meta.data->context);
64+
ptxt.random();
65+
66+
helib::Ctxt ctxt(meta.data->publicKey);
67+
68+
// Benchmark encrypting ciphertexts
69+
for (auto _ : state)
70+
meta.data->publicKey.Encrypt(ctxt, ptxt);
71+
std::cout << "Encryptions performed = " << state.iterations() << std::endl;
72+
}
73+
74+
static void decrypting_ciphertexts(benchmark::State& state, Meta& meta)
75+
{
76+
helib::Ptxt<helib::BGV> ptxt(meta.data->context);
77+
ptxt.random();
78+
79+
helib::Ctxt ctxt(meta.data->publicKey);
80+
81+
meta.data->publicKey.Encrypt(ctxt, ptxt);
82+
helib::Ptxt<helib::BGV> decrypted_result(meta.data->context);
83+
84+
// Benchmark decrypting ciphertexts
85+
for (auto _ : state)
86+
meta.data->secretKey.Decrypt(decrypted_result, ctxt);
87+
std::cout << "Decryptions performed = " << state.iterations() << std::endl;
88+
}
89+
90+
Meta fn;
91+
Params tiny_params(/*m=*/257, /*p=*/2, /*r=*/1, /*L=*/5800);
92+
BENCHMARK_CAPTURE(adding_two_ciphertexts, tiny_params, fn(tiny_params))
93+
->Unit(benchmark::kMillisecond)
94+
->Iterations(200);
95+
BENCHMARK_CAPTURE(multiplying_two_ciphertexts, tiny_params, fn(tiny_params))
96+
->Unit(benchmark::kMillisecond)
97+
->Iterations(200);
98+
BENCHMARK_CAPTURE(encrypting_ciphertexts, tiny_params, fn(tiny_params))
99+
->Unit(benchmark::kMillisecond)
100+
->Iterations(200);
101+
BENCHMARK_CAPTURE(decrypting_ciphertexts, tiny_params, fn(tiny_params))
102+
->Unit(benchmark::kMillisecond)
103+
->Iterations(200);
104+
105+
Params small_params(/*m=*/8009, /*p=*/2, /*r=*/1, /*L=*/5800);
106+
BENCHMARK_CAPTURE(adding_two_ciphertexts, small_params, fn(small_params))
107+
->Unit(benchmark::kMillisecond)
108+
->Iterations(200);
109+
BENCHMARK_CAPTURE(multiplying_two_ciphertexts, small_params, fn(small_params))
110+
->Unit(benchmark::kMillisecond)
111+
->MinTime(200);
112+
BENCHMARK_CAPTURE(encrypting_ciphertexts, small_params, fn(small_params))
113+
->Unit(benchmark::kMillisecond)
114+
->Iterations(200);
115+
BENCHMARK_CAPTURE(decrypting_ciphertexts, small_params, fn(small_params))
116+
->Unit(benchmark::kMillisecond)
117+
->Iterations(200);
118+
119+
Params big_params(/*m=*/32003, /*p=*/2, /*r=*/1, /*L=*/5800);
120+
BENCHMARK_CAPTURE(adding_two_ciphertexts, big_params, fn(big_params))
121+
->Unit(benchmark::kMillisecond)
122+
->Iterations(200);
123+
BENCHMARK_CAPTURE(multiplying_two_ciphertexts, big_params, fn(big_params))
124+
->Unit(benchmark::kMillisecond)
125+
->MinTime(200);
126+
BENCHMARK_CAPTURE(encrypting_ciphertexts, big_params, fn(big_params))
127+
->Unit(benchmark::kMillisecond)
128+
->MinTime(200);
129+
BENCHMARK_CAPTURE(decrypting_ciphertexts, big_params, fn(big_params))
130+
->Unit(benchmark::kMillisecond)
131+
->MinTime(200);
132+
133+
} // namespace

benchmarks/bgv_common.h

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/* Copyright (C) 2020 IBM Corp.
2+
* This program is Licensed under the Apache License, Version 2.0
3+
* (the "License"); you may not use this file except in compliance
4+
* with the License. You may obtain a copy of the License at
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
* Unless required by applicable law or agreed to in writing, software
7+
* distributed under the License is distributed on an "AS IS" BASIS,
8+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
* See the License for the specific language governing permissions and
10+
* limitations under the License. See accompanying LICENSE file.
11+
*/
12+
13+
#include <memory>
14+
15+
#include <helib/helib.h>
16+
17+
struct Params
18+
{
19+
const long m, p, r, L;
20+
const std::vector<long> gens;
21+
const std::vector<long> ords;
22+
Params(long _m,
23+
long _p,
24+
long _r,
25+
long _L,
26+
const std::vector<long>& _gens = {},
27+
const std::vector<long>& _ords = {}) :
28+
m(_m), p(_p), r(_r), L(_L), gens(_gens), ords(_ords)
29+
{}
30+
Params(const Params& other) :
31+
Params(other.m, other.p, other.r, other.L, other.gens, other.ords)
32+
{}
33+
bool operator!=(Params& other) const { return !(*this == other); }
34+
bool operator==(Params& other) const
35+
{
36+
return m == other.m && p == other.p && r == other.r && L == other.L &&
37+
gens == other.gens && ords == other.ords;
38+
}
39+
};
40+
41+
struct ContextAndKeys
42+
{
43+
const Params params;
44+
45+
helib::Context context;
46+
helib::SecKey secretKey;
47+
const helib::PubKey publicKey;
48+
const helib::EncryptedArray& ea;
49+
50+
ContextAndKeys(Params& _params) :
51+
params(_params),
52+
context(params.m, params.p, params.r, params.gens, params.ords),
53+
secretKey((helib::buildModChain(context, params.L, /*c=*/2), context)),
54+
publicKey((secretKey.GenSecKey(),
55+
helib::addSome1DMatrices(secretKey),
56+
secretKey)),
57+
ea(*(context.ea))
58+
{
59+
context.printout();
60+
}
61+
};
62+
63+
struct Meta
64+
{
65+
std::unique_ptr<ContextAndKeys> data;
66+
Meta& operator()(Params& params)
67+
{
68+
// Only change if nullptr or different.
69+
if (data == nullptr || data->params != params)
70+
data = std::make_unique<ContextAndKeys>(params);
71+
return *this;
72+
}
73+
};

0 commit comments

Comments
 (0)