-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0eb70fa
commit f2097fc
Showing
37 changed files
with
7,527 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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 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. | ||
// | ||
// 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 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_; | ||
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.