Skip to content

Commit cd267e2

Browse files
authored
Merge pull request #330 from faberga/master
Release 1.0.0-beta2-Nov2019
2 parents 7427ed3 + 7f553a6 commit cd267e2

File tree

162 files changed

+8818
-5012
lines changed

Some content is hidden

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

162 files changed

+8818
-5012
lines changed

.clang-format

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
Language: Cpp
3+
BasedOnStyle: LLVM
4+
AlwaysBreakTemplateDeclarations: Yes # Changed from Multiline
5+
BinPackArguments: false # Changed from true
6+
BinPackParameters: false # Changed from true
7+
BreakInheritanceList: AfterColon # Changed from BeforeColon
8+
BreakConstructorInitializers: AfterColon # Changed from BeforeColon
9+
ConstructorInitializerAllOnOneLineOrOnePerLine: true # Changed from false
10+
SortIncludes: false # Changed from true - TRY to make it true, might not work
11+
SortUsingDeclarations: false # Changed from true - TRY to make it true, might not work
12+
PointerAlignment: Left # Changed from Right
13+
BreakBeforeBraces: Mozilla # Changed to break after class/struct/function/..., but not after statement (if/while/for)
14+
...

CMakeLists.txt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ set(GMP_MINIMAL_VERSION "6.0.0")
1818
# NTL minimal version to be used if NOT PACKAGE_BUILD
1919
set(NTL_MINIMAL_VERSION "11.0.0")
2020

21-
# Setting up Debug as default CMAKE_BUILD_TYPE
22-
# TODO: set to Release when assertions are removed
21+
# Setting up RelWithDebInfo as default CMAKE_BUILD_TYPE
2322
if (NOT CMAKE_BUILD_TYPE)
24-
# Setting Debug as default since many checks are performed as assert (possibly with side effects)
25-
set(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose the type of build, options are: Debug RelWithDebInfo Release MinSizeRel" FORCE)
23+
# Setting RelWithDebInfo as it will compile with -O2 -g
24+
set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Choose the type of build, options are: Debug RelWithDebInfo Release MinSizeRel" FORCE)
2625
endif (NOT CMAKE_BUILD_TYPE)
2726

2827
# Adding possible gui values to CMAKE_BUILD_TYPE variable
@@ -36,7 +35,7 @@ option(BUILD_SHARED "Build as shared library" OFF)
3635

3736
option(PACKAGE_BUILD "Download dependencies and build as a self-contained package" OFF)
3837

39-
option(ENABLE_THREADS "Enable threads support. Requires NTL built with NTL_THREADS=on" OFF)
38+
option(ENABLE_THREADS "Enable threads support. Requires NTL built with NTL_THREADS=on" ON)
4039

4140
option(ENABLE_TEST "Enable tests" OFF)
4241
option(PEDANTIC_BUILD "Use -Wall -Wpedantic -Wextra -Werror during build" OFF)
@@ -86,7 +85,7 @@ if (PACKAGE_BUILD)
8685
# GMP version to be used (and eventually downloaded) if PACKAGE_BUILD
8786
set(FETCHED_GMP_VERSION "6.1.2")
8887
# NTL version to be used (and eventually downloaded) if PACKAGE_BUILD
89-
set(FETCHED_NTL_VERSION "11.3.0")
88+
set(FETCHED_NTL_VERSION "11.4.1")
9089
# Setting up default compilation output directory
9190
if (NOT PACKAGE_DIR)
9291
set(PACKAGE_DIR "helib_pack")

Dockerfile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ FROM ubuntu:18.04
33
COPY ./ /root/HElib/
44

55
RUN apt update && \
6-
apt install -y build-essential wget cmake m4 libgmp-dev file && \
6+
apt install -y build-essential wget git cmake m4 libgmp-dev file && \
77
cd ~ && \
8-
wget https://www.shoup.net/ntl/ntl-11.3.2.tar.gz && \
9-
tar xf ntl-11.3.2.tar.gz && \
10-
cd ntl-11.3.2/src && \
8+
wget https://www.shoup.net/ntl/ntl-11.4.1.tar.gz && \
9+
tar xf ntl-11.4.1.tar.gz && \
10+
cd ntl-11.4.1/src && \
1111
./configure SHARED=on NTL_GMP_LIP=on NTL_THREADS=on NTL_THREAD_BOOST=on && \
1212
make -j4 && \
1313
make install && \

INSTALL.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ CentOS 7.6, and macOS High Sierra 10.13.
99
- GNU make
1010
- g++ >= 5.4.0 or clang >= 3.8
1111
- pthreads
12+
- git (if you want to build the tests)
1213

1314
## Option 1: package build (recommended for most users)
1415

@@ -137,8 +138,8 @@ or some other system-wide path, step 5 needs to be run as root.
137138
Many distributions come with GMP pre-installed.
138139
If not, you can install GMP as follows.
139140

140-
1. Download GMP from http://www.gmplib.org -- make sure that you get GMP
141-
>=6.0.0 (current version is 6.1.2).
141+
1. Download GMP from http://www.gmplib.org -- make sure that you get GMP >=6.0.0
142+
(current version is 6.1.2).
142143
2. Decompress and cd into the gmp directory (e.g., `gmp-6.1.2`).
143144
3. GMP is compiled in the standard unix way:
144145
```
@@ -157,9 +158,9 @@ step 3.
157158

158159
You can install NTL as follows:
159160

160-
1. Download NTL >=11.0.0 (current version is 11.3.0) from
161+
1. Download NTL >=11.0.0 (current version is 11.4.1) from
161162
http://www.shoup.net/ntl/download.html
162-
2. Decompress and cd into the directory, e.g., `ntl-11.3.0/src`
163+
2. Decompress and cd into the directory, e.g., `ntl-11.4.1/src`
163164
3. NTL is configured, built and installed in the standard Unix way (but
164165
remember to specify the following flags to `configure`):
165166
```
@@ -178,8 +179,8 @@ to the `./configure` step.
178179
## HElib build options
179180

180181
### Generic options
181-
- `BUILD_SHARED=ON/OFF` (default is ON): Build as shared library.
182-
Note that buildinh HElib (regardless of BUILD_SHARED) will fail if NTL
182+
- `BUILD_SHARED=ON/OFF` (default is OFF): Build as shared library.
183+
Note that building HElib (regardless of BUILD_SHARED) will fail if NTL
183184
is not built as a shared library. The default for NTL is static library,
184185
to build NTL as a shared library use `./configure SHARED=on` in step 1.
185186
- `CMAKE_BUILD_TYPE`: Choose the type of build, options are: Debug,
@@ -228,4 +229,4 @@ find_package(helib)
228229
## Example
229230

230231
A full working example of a cmake-based project which uses HElib can be found
231-
in the `example_program` directory.
232+
in the `examples/example_program` directory.

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
HElib
22
=====
3+
***September-November 2019:*** Significant refactoring and cleanup of codebase.
4+
New `helib` namespace. New `examples` and `benchmarks` directory trees.
5+
Improvements to bootstrapping.
6+
Better tests for bootsrapping and binary arithmetic in BGV.
7+
Docs and example code for binary arithmetic.
8+
Overall code and performance improvements in `NumbTh.cpp`.
9+
HElib now avoids *very bad* generators.
10+
Bug fixes.
311

4-
***August 2019:*** Improved noise managemebt in HElib.
12+
***August 2019:*** Improved noise management in HElib.
513
Better and more robust bootstrapping algorithm.
614
Ubuntu 18.04 Docker image with HElib and its dependencies.
715
See install instructions in [DOCKER_USAGE.md](DOCKER_USAGE.md)

benchmarks/CMakeLists.txt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
2+
3+
## Use -std=c++11 as default.
4+
set(CMAKE_CXX_STANDARD 11)
5+
## Disable C++ extensions
6+
set(CMAKE_CXX_EXTENSIONS OFF)
7+
## Require full C++ standard
8+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
9+
10+
project(helib_benchmarks
11+
LANGUAGES CXX)
12+
13+
# Define standard installation directories (GNU)
14+
include(GNUInstallDirs)
15+
16+
# Setting compiler output directories
17+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
18+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
19+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})
20+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})
21+
22+
23+
# Setting up RelWithDebInfo as default CMAKE_BUILD_TYPE
24+
if (NOT CMAKE_BUILD_TYPE)
25+
# Setting RelWithDebInfo as it will compile with -02 -g
26+
set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Choose the type of build, options are: Debug RelWithDebInfo Release MinSizeRel" FORCE)
27+
endif (NOT CMAKE_BUILD_TYPE)
28+
29+
# Adding possible gui values to CMAKE_BUILD_TYPE variable
30+
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
31+
"Debug" "RelWithDebInfo" "Release" "MinSizeRel")
32+
33+
find_package(helib 1.0.0 EXACT REQUIRED)
34+
find_package(benchmark REQUIRED)
35+
36+
# Add each benchmark source file to the executable list
37+
add_executable(helib_benchmark bench_thinboot.cpp bench_fatboot.cpp)
38+
target_link_libraries(helib_benchmark benchmark::benchmark_main helib)

benchmarks/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Building and running HElib's benchmarks
2+
3+
## Instructions
4+
5+
### Build and install HElib
6+
7+
Build and install HElib as in [INSTALL.md](../INSTALL.md).
8+
9+
### Build and install google benchmark
10+
11+
Download the library https://github.com/google/benchmark from github, build and
12+
then install it.
13+
```
14+
git clone https://github.com/google/benchmark
15+
cd benchmark
16+
mkdir build
17+
cd build
18+
cmake -DBENCHMARK_ENABLE_GTEST_TESTS=OFF -DBENCHMARK_ENABLE_INSTALL=ON -DCMAKE_INSTALL_PREFIX=<installation dir> ..
19+
make
20+
make install
21+
```
22+
23+
### Write a benchmark test and link to both installed libraries.
24+
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.
28+
```
29+
cd HElib/benchmarks
30+
mkdir build
31+
cd build
32+
cmake -Dhelib_DIR=<helib installation dir>/share/cmake/helib -Dbenchmark_DIR=<benchmark installation dir>/lib/cmake/benchmark/ ..
33+
make
34+
```
35+
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+
41+
NOTE: Both options cannot be used at the same time.

benchmarks/bench_fatboot.cpp

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/* Copyright (C) 2019 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+
#include <helib/debugging.h>
18+
19+
void squareWithFatBoot(FHEPubKey& pk, Ctxt& c) {
20+
if (c.bitCapacity() <= 50) {
21+
pk.reCrypt(c);
22+
}
23+
c.square();
24+
}
25+
26+
static void BM_fatboot(benchmark::State& state,
27+
long m,
28+
long p,
29+
long r,
30+
long c,
31+
long bits,
32+
long t,
33+
int c_m,
34+
std::vector<long> mvector,
35+
std::vector<long> gens,
36+
std::vector<long> ords)
37+
{
38+
NTL::Vec<long> mvec = convert<NTL::Vec<long>>(mvector);
39+
std::cout << "m=" << m
40+
<< ", p=" << p
41+
<< ", r=" << r
42+
<< ", bits=" << bits
43+
<< ", c=" << c
44+
<< ", skHwt=" << t
45+
<< ", c_m=" << c_m
46+
<< ", mvec=" << mvec
47+
<< ", gens=" << gens
48+
<< ", ords=" << ords
49+
<< std::endl;
50+
std::cout << "Initialising context object..." << std::endl;
51+
FHEcontext context(m, p, r, gens, ords);
52+
context.zMStar.set_cM(c_m/100.0);
53+
54+
std::cout << "Building modulus chain..." << std::endl;
55+
buildModChain(context, bits, c, /*willBeBootstrappable=*/true, /*skHwt*/t);
56+
57+
// Make bootstrappable
58+
context.makeBootstrappable(mvec, t, /*build_cache=*/0, /*alsoThick=*/true);
59+
60+
// Print the context
61+
context.zMStar.printout();
62+
std::cout << std::endl;
63+
std::cout << "Security: " << context.securityLevel() << std::endl;
64+
65+
std::cout << "Creating secret key..." << std::endl;
66+
FHESecKey secret_key(context);
67+
secret_key.GenSecKey();
68+
std::cout << "Generating key-switching matrices..." << std::endl;
69+
addSome1DMatrices(secret_key);
70+
addFrbMatrices(secret_key);
71+
72+
// Generate bootstrapping data
73+
secret_key.genRecryptData();
74+
75+
// NOTE: For some reason the reCrypt method is not marked const so
76+
// I had to remove the const from the public key
77+
FHEPubKey& public_key = secret_key;
78+
const EncryptedArray& ea = *(context.ea);
79+
80+
long nslots = ea.size();
81+
std::cout << "Number of slots: " << nslots << std::endl;
82+
83+
std::vector<long> ptxt(nslots);
84+
for (int i = 0; i < nslots; ++i) {
85+
ptxt[i] = std::rand() % 2; // Random 0s and 1s
86+
}
87+
88+
Ctxt ctxt(public_key);
89+
ea.encrypt(ctxt, public_key, ptxt);
90+
for (auto _ : state)
91+
squareWithFatBoot(public_key, ctxt);
92+
std::cout << "Multiplications performed = " << state.iterations() <<std::endl;
93+
}
94+
95+
96+
BENCHMARK_CAPTURE(BM_fatboot, tiny_params,
97+
/*m =*/ 31*41,
98+
/*p =*/ 2,
99+
/*r =*/ 1,
100+
/*c =*/ 2,
101+
/*bits =*/ 580,
102+
/*t =*/ 64,
103+
/*c_m =*/ 100,
104+
/*mvec =*/ std::vector<long> {31, 41},
105+
/*gens =*/ std::vector<long> {1026, 249},
106+
/*ords =*/ std::vector<long> {30, -2}
107+
)->Unit(benchmark::kMillisecond)->Iterations(200);
108+
109+
BENCHMARK_CAPTURE(BM_fatboot, small_params,
110+
/*m =*/ 31775,
111+
/*p =*/ 2,
112+
/*r =*/ 1,
113+
/*c =*/ 2,
114+
/*bits =*/ 580,
115+
/*t =*/ 64,
116+
/*c_m =*/ 100,
117+
/*mvec =*/ std::vector<long> {41, 775},
118+
/*gens =*/ std::vector<long> {6976, 24806},
119+
/*ords =*/ std::vector<long> {40, 30}
120+
)->Unit(benchmark::kMillisecond)->MinTime(200);
121+
122+
BENCHMARK_CAPTURE(BM_fatboot, big_params,
123+
/*m =*/ 35113,
124+
/*p =*/ 2,
125+
/*r =*/ 1,
126+
/*c =*/ 2,
127+
/*bits =*/ 580,
128+
/*t =*/ 64,
129+
/*c_m =*/ 100,
130+
/*mvec =*/ std::vector<long> {37, 949},
131+
/*gens =*/ std::vector<long> {16134, 8548},
132+
/*ords =*/ std::vector<long> {36, 24}
133+
)->Unit(benchmark::kMillisecond)->MinTime(200);

0 commit comments

Comments
 (0)