Skip to content

Commit

Permalink
repo-sync-2023-08-25T17:34:40+0800 (secretflow#92)
Browse files Browse the repository at this point in the history
* repo-sync-2023-08-25T17:34:40+0800

* repo-sync-2023-08-25T17:44:49+0800

* repo-sync-2023-08-25T17:47:11+0800

* Update algo_choice.po
  • Loading branch information
usafchn authored Aug 25, 2023
1 parent 0ca4390 commit e9e6587
Show file tree
Hide file tree
Showing 17 changed files with 169 additions and 46 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ bazel test heu/...
Build python lib

```shell
# require Python 3.8
# build and install python library
sh build_wheel_entrypoint.sh
```
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

SECRETFLOW_GIT = "https://github.com/secretflow"

YACL_COMMIT_ID = "ff20dff1476071ca885c69bee94d2b3bdf85034c"
YACL_COMMIT_ID = "1e44d9b077ed01eb17416474eb02d2c2a3231cc4"

git_repository(
name = "yacl",
Expand Down
5 changes: 4 additions & 1 deletion build_wheel_entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -e


python setup.py bdist_wheel
rm -rf dist

python setup.py bdist_wheel
python3 -m pip install dist/*.whl --force-reinstall

# check import ok
python -c "import heu"
python -c "from heu import numpy"
3 changes: 1 addition & 2 deletions docs/development/phe_dev.rst
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,9 @@ HEU 单元测试使用 `GoogleTest <github.com/google/googletest>`_ 框架,有

.. code-block:: shell
# Please switch to Python 3.8 environment
pip install pybind11 # Only need to run once
sh heu/pylib/reinstall.sh
sh build_wheel_entrypoint.sh
基本使用:

Expand Down
8 changes: 4 additions & 4 deletions docs/getting_started/algo_choice.rst
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,9 @@ ElGamal 是一个基于 Diffie–Hellman 密钥交换的非对称加密算法,
* - 算法类型
- 同态特性取决于底层循环群 G 的定义,根据 G 的不同 Elgamal 可能为加法同态、乘法同态或没有同态特性。
* - 安全性
- 取决于 G 上离散对数问题(DLP)的困难性
- 如果定义在 G 上的 Decisional Diffie–Hellman assumption (DDH) 是困难的,则算法是语义安全(Semantic Security)的,不可区分性满足 IND-CPA
* - 困难假设
- 离散对数难题 DLP
- CDH & DDH
* - 安全强度(Security Strength)
- 取决于 G

Expand All @@ -343,9 +343,9 @@ ElGamal 是一个基于 Diffie–Hellman 密钥交换的非对称加密算法,
* - 算法类型
- 取决于明文到 EC Group 的映射方式,如果映射妥当,则 EC ElGamal 满足加法同态特性
* - 安全性
- 取决于椭圆曲线离散对数问题(ECDLP)的困难性
- IND-CPA 安全,语义安全(Semantic Security)
* - 困难假设
- 椭圆曲线离散对数难题 ECDLP
- 定义在椭圆曲线上的 CDH & DDH
* - 安全强度(Security Strength)
- 取决于具体选择的曲线,`此处 <http://safecurves.cr.yp.to/rho.html>`__ 列出了一部分曲线的安全强度

Expand Down
5 changes: 2 additions & 3 deletions docs/getting_started/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,13 @@ HEU 安装包已经发布到了 PyPi,您可以通过 pip 工具安装 HEU
- `Bazel <https://docs.bazel.build/versions/main/install.html>`_
- GCC >= 11

安装
编译和安装

.. code-block:: bash
git clone --recursive [email protected]:secretflow/heu.git
cd heu
bazel build //heu/pylib -c opt
pip install bazel-bin/heu/pylib/(cat bazel-bin/heu/pylib/pylib.name) --force-reinstall
sh build_wheel_entrypoint.sh
测试运行
Expand Down
15 changes: 7 additions & 8 deletions docs/locale/en/LC_MESSAGES/getting_started/algo_choice.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: HEU \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2023-07-26 11:37+0800\n"
"POT-Creation-Date: 2023-08-21 18:43+0800\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -207,6 +207,7 @@ msgstr "security"

#: ../../getting_started/algo_choice.rst:68
#: ../../getting_started/algo_choice.rst:205
#: ../../getting_started/algo_choice.rst:346
msgid "IND-CPA 安全,语义安全(Semantic Security)"
msgstr "IND-CPA Security, Semantic Security"

Expand Down Expand Up @@ -901,11 +902,13 @@ msgid "同态特性取决于底层循环群 G 的定义,根据 G 的不同 Elg
msgstr ""

#: ../../getting_started/algo_choice.rst:333
msgid "取决于 G 上离散对数问题(DLP)的困难性"
msgid ""
"如果定义在 G 上的 Decisional Diffie–Hellman assumption (DDH) "
"是困难的,则算法是语义安全(Semantic Security)的,不可区分性满足 IND-CPA"
msgstr ""

#: ../../getting_started/algo_choice.rst:335
msgid "离散对数难题 DLP"
msgid "CDH & DDH"
msgstr ""

#: ../../getting_started/algo_choice.rst:337
Expand All @@ -924,12 +927,8 @@ msgstr "Information of EC ElGamal"
msgid "取决于明文到 EC Group 的映射方式,如果映射妥当,则 EC ElGamal 满足加法同态特性"
msgstr ""

#: ../../getting_started/algo_choice.rst:346
msgid "取决于椭圆曲线离散对数问题(ECDLP)的困难性"
msgstr ""

#: ../../getting_started/algo_choice.rst:348
msgid "椭圆曲线离散对数难题 ECDLP"
msgid "定义在椭圆曲线上的 CDH & DDH"
msgstr ""

#: ../../getting_started/algo_choice.rst:350
Expand Down
11 changes: 6 additions & 5 deletions docs/locale/en/LC_MESSAGES/getting_started/installation.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: HEU \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2023-02-02 20:22+0800\n"
"POT-Creation-Date: 2023-08-23 11:26+0800\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand All @@ -17,6 +17,7 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.10.3\n"

#: ../../getting_started/installation.rst:2
#: ../../getting_started/installation.rst:16
msgid "安装"
msgstr "Installation"
Expand Down Expand Up @@ -80,18 +81,18 @@ msgid "GCC >= 11"
msgstr ""

#: ../../getting_started/installation.rst:40
msgid "安装:"
msgid "编译和安装:"
msgstr "Compile and install:"

#: ../../getting_started/installation.rst:51
#: ../../getting_started/installation.rst:50
msgid "测试运行"
msgstr "Test running"

#: ../../getting_started/installation.rst:53
#: ../../getting_started/installation.rst:52
msgid "安装完成后,建议执行以下命令检查 HEU 是否工作正常"
msgstr "After installation, it is a good idea to test HEU using:"

#: ../../getting_started/installation.rst:59
#: ../../getting_started/installation.rst:58
msgid ""
"上述命令,如果没有任何输出,则说明安装成功;反之如果报错,则说明安装未成功,请参考 :doc:`FAQ "
"</getting_started/faq>`"
Expand Down
3 changes: 1 addition & 2 deletions heu/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package(default_visibility = ["//visibility:public"])



### global configs ###

config_setting(
name = "enable_clustar_fpga",
Expand Down
98 changes: 98 additions & 0 deletions heu/experimental/gpu-ckks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# SEAL-GPU

SEAL-GPU is an easy-to-use SEAL library using GPU acceleration developed by the software and hardware combination group at AntGroup. SEAL-GPU provides APIs similar with SEAL, and will provide richer functions and interfaces later.

> Just for GPU Part
**//// Link GPULIB FIRST ////**

## Environment

* cmake version >= 3.24
* gnu version >= 9
* cuda version >= 11
* gmp required
* ntl required

## gpu_lib

This folder provides GPU library devolped by cuda.

## How to build

* `-DSEAL_USE_GPU` should be set `ON` to enhance GPU boost. If set `OFF`, SEAL-GPU will appear nothing different from the raw SEAL.
* `-DCMAKE_INSTALL_PREFIX` should be set as the SEAL-GPU's install path.
* `-DCMAKE_PREFIX_PATH` is the path where SEAL-GPU can find the `GPULIB` package.

Or simply build step-by-step:
```sh
cmake -S . -B build -DSEAL_USE_GPU=ON -DCMAKE_INSTALL_PREFIX=<INSTALL_PATH> -DCMAKE_PREFIX_PATH=<GPULIB_PATH>
cmake --build build
sudo cmake --install build
```

## Examples

* Calling GPULIB to do the `relinearization` in SEAL can be done in 3 steps:

1. Use a `SEALContext` instance to construct the context used by GPULIB, and enable RMM based memory pool.

```c++
auto gpu_context = context.convert2GPU();
gpu_context.EnableMemoryPool();
```

2. Copy the `RelinKeys` instance to GPU memory.

```c++
auto gpu_evk = static_cast<const KSwitchKeys&>(relin_keys).convert2GPU(context);
```

3. Suppose you have a `Ciphertext` instance in SEAL whose size is precisely 3, a `relinearize_inplace` or `relinearize` can reduce its size to 2 via GPULIB. The method needs aforementioned `gpu_context` and `gpu_evk` as arguments.

> Test end-to-end performance in this step.

```C++
evaluator.relinearize_inplace(x_encrypted, gpu_evk, gpu_context);
```

* Calling GPULIB to do the `rotation` in SEAL can be done in 3 steps:

1. Use a `SEALContext` instance to construct the context used by GPULIB, and enable RMM based memory pool.

```c++
auto gpu_context = context.convert2GPU();
gpu_context.EnableMemoryPool();
```

2. Copy the `GaloisKeys` instance to GPU memory. I store the `pair<size_t, ckks::EvaluationKey> (elt, gpu_evk)` in an `unordered_map`.

```C++
std::unordered_map<size_t, ckks::EvaluationKey> gpu_evks;

for (size_t i = 0; i < galois_keys.data().size(); i++)
{
if (galois_keys.has_key(i))
{
gpu_evks.insert(make_pair(i,
static_cast<const KSwitchKeys&>(galois_keys).convert2GPU(context, GaloisKeys::get_index(i))));
}
}
```

3. Use the `rotate_vector` method to rotate a `Ciphertext` instance via GPULIB. The method needs aforementioned `gpu_context` and `gpu_evks` as arguments.

> Test end-to-end performance in this step.
```c++
evaluator.rotate_vector(encrypted, step, gpu_evks, rotated, gpu_context);
```

* `example` works when `poly_modulus_degree` is `8192` and `16384`. Other configurations have not been tested.
* Support rotation.
* A new example :matrix multiply vector is already add in SEAL-GPU examples, which is an end-to-end gpu compute example.

## Flaws & TODOs

* **The version is not completely end-to-end!**
* Some new functions will be finished.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/file.h>
Expand Down
1 change: 0 additions & 1 deletion heu/library/benchmark/np_bench.cc
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ class NpBenchmarks {

void Encrypt(benchmark::State& state, size_t idx) {
std::call_once(flag_, []() { fmt::print("{:-^80}\n", ""); });

// encrypt
const auto& encryptor = he_kit_->GetEncryptor();
for (auto _ : state) {
Expand Down
4 changes: 2 additions & 2 deletions heu/library/phe/base/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ yacl_cc_library(
srcs = ["schema.cc"],
hdrs = ["schema.h"],
deps = [
"//heu/library/algorithms/elgamal",
"//heu/library/algorithms/mock",
"//heu/library/algorithms/ou",
"//heu/library/algorithms/paillier_clustar_fpga",
"//heu/library/algorithms/paillier_float",
"//heu/library/algorithms/paillier_zahlen",
"//heu/library/algorithms/paillier_ipcl",
"//heu/library/algorithms/elgamal",
"//heu/library/algorithms/paillier_zahlen",
],
)

Expand Down
2 changes: 1 addition & 1 deletion heu/library/phe/base/schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ std::string format_as(SchemaType i);
INVOKE(true, func_or_macro, ::heu::lib::algorithms::paillier_z, ##__VA_ARGS__) \
INVOKE(true, func_or_macro, ::heu::lib::algorithms::paillier_f, ##__VA_ARGS__) \
INVOKE(ENABLE_CLUSTAR_FPGA, func_or_macro, ::heu::lib::algorithms::paillier_clustar_fpga, ##__VA_ARGS__) \
INVOKE(true, func_or_macro, ::heu::lib::algorithms::elgamal, ##__VA_ARGS__)
INVOKE(true, func_or_macro, ::heu::lib::algorithms::elgamal, ##__VA_ARGS__) \

// [SPI: Please register your algorithm here] || progress: (4 of 5)
// If you add a new schema, change this !!
Expand Down
25 changes: 21 additions & 4 deletions heu/pylib/numpy_binding/bind_numpy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -237,20 +237,37 @@ void PyBindNumpy(pybind11::module& m) {
[](phe::SchemaType schema_type, size_t key_size) {
return hnp::HeKit(phe::HeKit(schema_type, key_size));
},
py::arg("schema_type") = phe::SchemaType::ZPaillier,
py::arg("key_size") = 2048, py::return_value_policy::move,
py::arg("schema_type"), py::arg("key_size"),
py::return_value_policy::move,
"Setup phe (numpy) environment by schema type and key size");

m.def(
"setup",
[](const std::string& schema_string, size_t key_size) -> hnp::HeKit {
[](const std::string& schema_string, size_t key_size) {
return hnp::HeKit(
phe::HeKit(phe::ParseSchemaType(schema_string), key_size));
},
py::arg("schema_string") = "z-paillier", py::arg("key_size") = 2048,
py::arg("schema_string"), py::arg("key_size"),
py::return_value_policy::move,
"Setup phe (numpy) environment by schema string and key size");

m.def(
"setup",
[](phe::SchemaType schema_type) {
return hnp::HeKit(phe::HeKit(schema_type));
},
py::arg("schema_type") = phe::SchemaType::ZPaillier,
py::return_value_policy::move,
"Setup phe (numpy) environment by schema type");

m.def(
"setup",
[](const std::string& schema_string) {
return hnp::HeKit(phe::HeKit(phe::ParseSchemaType(schema_string)));
},
py::arg("schema_string") = "z-paillier", py::return_value_policy::move,
"Setup phe (numpy) environment by schema string");

// api for evaluator party
auto dhe_kit = py::class_<hnp::DestinationHeKit, phe::HeKitPublicBase>(
m, "DestinationHeKit");
Expand Down
Loading

0 comments on commit e9e6587

Please sign in to comment.