-
Notifications
You must be signed in to change notification settings - Fork 44
Add leichi algorithm to HEU #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tomithy001
wants to merge
3
commits into
secretflow:main
Choose a base branch
from
tomithy001:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| load("@yacl//bazel:yacl.bzl", "yacl_cc_library", "yacl_cc_test") | ||
|
|
||
| package(default_visibility = ["//visibility:public"]) | ||
|
|
||
| test_suite( | ||
| name = "leichi_paillier_tests", | ||
| ) | ||
|
|
||
| config_setting( | ||
| name = "use_leichi", | ||
| values = {"define": "enable_leichi=true"}, | ||
| ) | ||
|
|
||
| yacl_cc_library( | ||
| name = "leichi_paillier_defs", | ||
| hdrs = ["leichi.h"], | ||
| deps = [":leichi_paillier"], | ||
| ) | ||
|
|
||
| yacl_cc_library( | ||
| name = "leichi_paillier", | ||
| srcs = select({ | ||
| ":use_leichi":[ | ||
| "vector_decryptor.cc", | ||
| "vector_encryptor.cc", | ||
| "vector_evaluator.cc", | ||
| "key_generator.cc", | ||
| "public_key.cc", | ||
| "secret_key.cc", | ||
| "plaintext.cc", | ||
| "ciphertext.cc", | ||
| "utils.cc", | ||
| "runtime.cc", | ||
| ], | ||
| "//conditions:default":[], | ||
| }), | ||
|
|
||
| hdrs = select({ | ||
| ":use_leichi":[ | ||
| "plaintext.h", | ||
| "ciphertext.h", | ||
| "vector_decryptor.h", | ||
| "vector_encryptor.h", | ||
| "vector_evaluator.h", | ||
| "key_generator.h", | ||
| "public_key.h", | ||
| "secret_key.h", | ||
| "leichi.h", | ||
| "utils.h", | ||
| "runtime.h", | ||
| ], | ||
| "//conditions:default":[], | ||
| }), | ||
|
|
||
| visibility = ["//visibility:public"], | ||
|
|
||
| deps = select({ | ||
| ":use_leichi":[ | ||
| "//heu/library/algorithms/util", | ||
| "@com_github_msgpack_msgpack//:msgpack", | ||
| "@com_github_openssl_openssl//:openssl", | ||
| "@com_github_uscilab_cereal//:cereal", | ||
| ":pcie", | ||
| "compiler", | ||
| ], | ||
| "//conditions:default":[], | ||
| }), | ||
|
|
||
| defines = select({ | ||
| "use_leichi":["APPLY_LEICHI"], | ||
| "//conditions:default":[], | ||
| }) | ||
| ) | ||
|
|
||
|
|
||
| yacl_cc_library( | ||
| name = "pcie", | ||
| srcs = ["pcie/pcie.cc"], | ||
| hdrs = ["pcie/pcie.h"], | ||
| deps = [ | ||
|
|
||
| ], | ||
| ) | ||
|
|
||
| yacl_cc_library( | ||
| name = "compiler", | ||
| srcs = ["compiler/compiler.cc"], | ||
| hdrs = ["compiler/compiler.h"], | ||
| deps = [ | ||
|
|
||
| ], | ||
| ) | ||
|
|
||
| yacl_cc_test( | ||
| name = "key_generator_test", | ||
| srcs = select({ | ||
| ":use_leichi":[ | ||
| "key_generator_test.cc"], | ||
| "//conditions:default":[], | ||
| }), | ||
| deps = select({ | ||
| ":use_leichi":[ | ||
| ":leichi_paillier", | ||
| "@com_github_openssl_openssl//:openssl"], | ||
| "//conditions:default":[], | ||
| }), | ||
|
|
||
| defines = select({ | ||
| "use_leichi":["APPLY_LEICHI"], | ||
| "//conditions:default":[], | ||
| }) | ||
| ) | ||
|
|
||
| yacl_cc_test( | ||
| name = "leichi_test", | ||
| srcs = select({ | ||
| ":use_leichi":[ | ||
| "leichi_test.cc"], | ||
| "//conditions:default":[], | ||
| }), | ||
|
|
||
| deps = select({ | ||
| ":use_leichi":[ | ||
| ":leichi_paillier"], | ||
| "//conditions:default":[], | ||
| }), | ||
| defines = select({ | ||
| "use_leichi":["APPLY_LEICHI"], | ||
| "//conditions:default":[], | ||
| }) | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| # 使用方法 | ||
|
|
||
| ## 简介 | ||
| ``` | ||
| leichi_paillier 默认关闭; | ||
| ``` | ||
| ## HEU编译 | ||
|
|
||
| bazel build heu/... | ||
| ``` | ||
| bazel test heu/... --test_output=all --cache_test_results=no | ||
| 使用 leichi_paillier | ||
| ``` | ||
| bazel test heu/... --test_output=all --cache_test_results=no --define enable_leichi=true | ||
|
|
||
|
|
||
| ## leichi_paillier相关单元测试 | ||
|
|
||
| 使用 leichi_paillier | ||
| ``` | ||
| bazel test --test_output=all --cache_test_results=no heu/library/algorithms/leichi_paillier:encryptor_test --define enable_leichi=true | ||
| bazel test --test_output=all --cache_test_results=no heu/library/algorithms/leichi_paillier:key_generator_test --define enable_leichi=true | ||
| ``` | ||
| ## Benchmark测试 | ||
|
|
||
| 使用 leichi_paillier | ||
| ``` | ||
| scalar 场景性能测试 | ||
| ``` | ||
| bazel run -c opt heu/library/benchmark:phe -- --schema=Leichi --define enable_leichi=true | ||
|
|
||
| vector 场景性能测试 | ||
| ``` | ||
| bazel run -c opt heu/library/benchmark:np -- --schema=Leichi --define enable_leichi=true | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| // Copyright 2023 Ant Group Co., Ltd. | ||
usafchn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // 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. | ||
|
|
||
| #include "heu/library/algorithms/leichi_paillier/ciphertext.h" | ||
| #include <string_view> | ||
|
|
||
| namespace heu::lib::algorithms::leichi_paillier { | ||
|
|
||
| std::string Ciphertext::ToString() const { | ||
| char* str = BN_bn2dec(bn_); | ||
| std::string result(str); | ||
| return result; | ||
| } | ||
|
|
||
| Ciphertext& Ciphertext::operator=(const Ciphertext& other) { | ||
| if (this != &other) { | ||
| BN_copy(bn_, other.bn_); | ||
| } | ||
| return *this; | ||
| } | ||
|
|
||
| std::ostream &operator<<(std::ostream &os, const Ciphertext &ct) { | ||
| char* str = BN_bn2dec(ct.bn_); | ||
| os << str; | ||
| return os; | ||
| } | ||
|
|
||
| bool Ciphertext::operator==(const Ciphertext &other) const { | ||
| return (BN_cmp(bn_, other.bn_) == 0)?true:false; | ||
| } | ||
|
|
||
| bool Ciphertext::operator!=(const Ciphertext &other) const { | ||
| return (BN_cmp(bn_, other.bn_) == 0)?true:false; | ||
| } | ||
|
|
||
| yacl::Buffer Ciphertext::Serialize() const{ | ||
| uint32_t n_bits_len = BN_num_bits(bn_); | ||
| uint8_t* n_arr = new uint8_t[n_bits_len]; | ||
| std::vector<uint8_t> vec_tmp; | ||
| BN_bn2bin(bn_, n_arr); | ||
| uint32_t bytes_len = std::ceil(n_bits_len/8.0); | ||
| for(uint32_t i=0;i<bytes_len;i++) | ||
| { | ||
| vec_tmp.push_back(n_arr[i]); | ||
| } | ||
| uint8_t sign = BN_is_negative(bn_) ? 1 : 0; | ||
| vec_tmp.push_back(sign); | ||
| yacl::Buffer buf(vec_tmp.data(), std::ceil(BN_num_bits(bn_)/8.0)+1); | ||
| return buf; | ||
| } | ||
| void Ciphertext::Deserialize(yacl::ByteContainerView in){ | ||
| std::istringstream is((std::string)in); | ||
| BN_bin2bn((uint8_t *)(is.str().data()), is.str().size()-1,bn_); | ||
| int sign = in[is.str().size()-1]& 0x01 ? 1 : 0; | ||
| BN_set_negative(bn_,sign); | ||
| } | ||
|
|
||
| } // namespace heu::lib::algorithms::leichi_paillier | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // Copyright 2023 Ant Group Co., Ltd. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // 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. | ||
|
|
||
| #include "openssl/bn.h" | ||
| #include <ostream> | ||
| #include <string> | ||
| #include <utility> | ||
| #include "yacl/base/byte_container_view.h" | ||
| #include "cereal/archives/portable_binary.hpp" | ||
| #include <vector> | ||
| #pragma once | ||
| namespace heu::lib::algorithms::leichi_paillier { | ||
| class Ciphertext { | ||
| public: | ||
| BIGNUM* bn_; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 直接使用裸指针是非常危险的,更何况还是 public 字段,可以考虑用只能指针包一下,下面的 Plaintext class 同理。 |
||
| public: | ||
| Ciphertext() {bn_ = BN_new();} | ||
| ~Ciphertext(){BN_free(bn_);} | ||
| Ciphertext(const Ciphertext& other) {bn_ = BN_dup(other.bn_);} | ||
|
|
||
| explicit Ciphertext(BIGNUM *bn){ bn_ = bn;} | ||
| std::string ToString() const; | ||
| friend std::ostream &operator<<(std::ostream &os, const Ciphertext &ct); | ||
|
|
||
| Ciphertext& operator=(const Ciphertext& other); | ||
| bool operator==(const Ciphertext &other) const; | ||
| bool operator!=(const Ciphertext &other) const; | ||
|
|
||
| yacl::Buffer Serialize() const; | ||
| void Deserialize(yacl::ByteContainerView in); | ||
| }; | ||
| }// namespace heu::lib::algorithms::leichi_paillier | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.