Skip to content
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

Add leichi algorithm to HEU #80

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

tomithy001
Copy link

[Feature] Add algorithm(leichi_paillier) which uses leichi to speed up Paillier

@github-actions
Copy link

github-actions bot commented Jul 30, 2023

CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅

@tomithy001
Copy link
Author

I have read the CLA Document and I hereby sign the CLA

Copy link
Member

@usafchn usafchn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

所有的代码需要 format 一下

# c++
echo 'Formatting c++ code'
find . -type f -name '*.h' -o -name '*.cc' | xargs clang-format -i 

# bazel
echo 'Formatting bazel code'
buildifier -r .

do{
get_prime(key_size/2,sk->p_);
get_prime(key_size/2,sk->q_);
}while(sk->p_ == sk->q_);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 判断 gcd(p-1 ,q-1) == 2,从而确保 n - 1 没有小因子
  2. 防止 square-root attacks, 需确保 p-q 是一个大整数

请参考一下这里的写法:
https://github.com/secretflow/heu/blob/main/heu/library/algorithms/paillier_zahlen/key_generator.cc#L41C1-L41C48

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的

namespace heu::lib::algorithms::leichi_paillier::test {
class KeyGenTest : public ::testing::TestWithParam<size_t> {};
INSTANTIATE_TEST_SUITE_P(SubTest, KeyGenTest,
::testing::Values(1024));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

根据安全要求,即使 UT 代码也不再允许出现 1024 keysize,以避免对用户产生误导

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的

TEST_P(KeyGenTest, SubTest) {
SecretKey sk;
PublicKey pk;
KeyGenerator::Generate(GetParam(), &sk, &pk);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个单测啥都没测吗

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我把这个单测加上


namespace heu::lib::algorithms::leichi_paillier::test {

class LEICHITest : public testing::Test {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

命名风格: LeichiTest

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的

return BN_cmp(bn1, bn2);
}

TEST_F(LEICHITest, DISABLE_EncDec) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的单测为什么都 Disable 了?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR提交测试时需要硬件支持,否则会failed,把单测都Disable了

};
void Deserialize(yacl::ByteContainerView in){
std::istringstream is((std::string)in);
BN_bin2bn((uint8_t *)(is.str().data()), is.str().size(),n_.bn_);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么要先转换成 istringstream ?

std::vector<uint8_t> Tobin(T &op)
{
uint32_t n_bits_len = op.numBits();
uint8_t* n_arr = new uint8_t[n_bits_len];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上,不允许直接操作裸指针

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的


Runtime _runtime;
std::vector<Plaintext> _pts(in_cts.size());
uint8_t *ct_bytes = new uint8_t[in_cts.size()*BYTECOUNT(pk_.n_.numBits()*2)];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

请去掉 new

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@@ -56,6 +56,9 @@ TEST_P(EncryptorTest, EncryptZero) {
}

TEST_P(EncryptorTest, MinMaxEnc) {
// if (GetParam() == SchemaType::Leichi) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

没用的注释去掉吧

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的

@@ -87,6 +89,8 @@ std::string format_as(SchemaType i);
INVOKE(true, func_or_macro, ::heu::lib::algorithms::ou, ##__VA_ARGS__) \
INVOKE(ENABLE_IPCL, func_or_macro, ::heu::lib::algorithms::paillier_ipcl, ##__VA_ARGS__) \
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_LEICHI, func_or_macro, ::heu::lib::algorithms::leichi_paillier, ##__VA_ARGS__) \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leichi_paillier 加到 elgamal 之后,顺序与SchemaType enum相同

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

}
}

OPERATION_TYPE Runtime::operation_trans(std::string operation)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

入参类型改成 const std::string & or std::string_view

break;
case 512:
count = 1;
break;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

增加 default 语句并报错

};

std::map<std::string, int> CHIP_TABLE = {
{"CHIP_0", (int) std::pow(2, 15)},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以用移位代替

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants