Skip to content

Commit

Permalink
Add leichi algorithm to HEU
Browse files Browse the repository at this point in the history
  • Loading branch information
tomithy001 committed Aug 8, 2023
1 parent dae9e57 commit 1456de6
Show file tree
Hide file tree
Showing 10 changed files with 348 additions and 178 deletions.
33 changes: 31 additions & 2 deletions heu/library/algorithms/leichi_paillier/ciphertext.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,47 @@ namespace heu::lib::algorithms::leichi_paillier {
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;
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;
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
43 changes: 7 additions & 36 deletions heu/library/algorithms/leichi_paillier/ciphertext.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,48 +25,19 @@ namespace heu::lib::algorithms::leichi_paillier {
public:
BIGNUM* bn_;
public:
Ciphertext() {
bn_ = BN_new();
}
~Ciphertext(){
BN_free(bn_);
}

Ciphertext(const Ciphertext& other) {
bn_ = BN_dup(other.bn_);
}

Ciphertext& operator=(const Ciphertext& other) {
if (this != &other) {
BN_copy(bn_, other.bn_);
}
return *this;
}

explicit Ciphertext(BIGNUM *bn){ bn_ = bn;}//BN_dup(bn);}
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{
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]);
}
yacl::Buffer buf(vec_tmp.data(), std::ceil(BN_num_bits(bn_)/8.0));
return buf;
}
void Deserialize(yacl::ByteContainerView in){
std::istringstream is((std::string)in);
BN_bin2bn((uint8_t *)(is.str().data()), is.str().size(),bn_);
}
yacl::Buffer Serialize() const;
void Deserialize(yacl::ByteContainerView in);
};
}// namespace heu::lib::algorithms::leichi_paillier
6 changes: 5 additions & 1 deletion heu/library/algorithms/leichi_paillier/key_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ namespace heu::lib::algorithms::leichi_paillier {
uint32_t a =1;
one.Set(a);
pk->g_ = pk->n_ + one;
pk->max_plaintext_ = pk->n_;

Plaintext two;
uint32_t b =2;
two.Set(b);
pk->max_plaintext_ = pk->n_/two;
}

void KeyGenerator::Generate(SecretKey* sk, PublicKey* pk) {
Expand Down
102 changes: 93 additions & 9 deletions heu/library/algorithms/leichi_paillier/leichi_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,21 @@ namespace heu::lib::algorithms::leichi_paillier::test {
Encryptor encryptor(pk_);
Decryptor decryptor(pk_, sk_);
std::vector<uint32_t> a_vec{25, 13};
std::vector<uint32_t> expect_res{25, 13};
std::vector<Plaintext> a_pt_vec;
std::vector<Plaintext> expect_res_pt_vec;
auto vec_size = a_vec.size();
for (size_t i = 0; i < vec_size; i++) {
Plaintext a;
a.Set(a_vec[i]);
a_pt_vec.push_back(a);
}

for (size_t i = 0; i < vec_size; i++) {
Plaintext res;
res.Set(expect_res[i]);
expect_res_pt_vec.push_back(res);
}

std::vector<Plaintext *> a_pt_pts;
ValueVecToPtsVec(a_pt_vec, a_pt_pts);
Expand All @@ -64,12 +72,16 @@ namespace heu::lib::algorithms::leichi_paillier::test {
ValueVecToPtsVec(a_ct_vec, a_ct_pts);
auto a_ct_span = absl::MakeConstSpan(a_ct_pts.data(), vec_size);
std::vector<Plaintext> res_pt_vec = decryptor_->Decrypt(a_ct_span);
for (size_t i = 0; i < vec_size; i++) {
EXPECT_EQ(res_pt_vec[i], expect_res_pt_vec[i]);
}
}

TEST_F(LEICHITest, DISABLE_CTPlusCT) {
std::vector<int32_t> a_vec{25, 13};
std::vector<int32_t> b_vec{-25, 13};

std::vector<int32_t> expect_res{0, 26};
std::vector<Plaintext> expect_res_pt_vec;
std::vector<Plaintext> a_pt_vec;
std::vector<Plaintext> b_pt_vec;
auto vec_size = a_vec.size();
Expand All @@ -84,6 +96,12 @@ namespace heu::lib::algorithms::leichi_paillier::test {
b_pt_vec.push_back(b);
}

for (size_t i = 0; i < vec_size; i++) {
Plaintext res;
res.Set(expect_res[i]);
expect_res_pt_vec.push_back(res);
}

std::vector<Plaintext *> a_pt_pts;
std::vector<Plaintext *> b_pt_pts;
ValueVecToPtsVec(a_pt_vec, a_pt_pts);
Expand All @@ -104,12 +122,18 @@ namespace heu::lib::algorithms::leichi_paillier::test {
ValueVecToPtsVec(res_ct_vec, res_ct_pts);
auto res_ct_span = absl::MakeConstSpan(res_ct_pts.data(), vec_size);
std::vector<Plaintext> res_pt_vec = decryptor_->Decrypt(res_ct_span);

for (size_t i = 0; i < vec_size; i++) {
std::cout<<res_pt_vec[i]<<std::endl;
EXPECT_EQ(res_pt_vec[i], expect_res_pt_vec[i]);
}
}

TEST_F(LEICHITest, DISABLE_CTPlusPT) {
std::vector<int32_t> a_vec{25, 13, 15};
std::vector<int32_t> b_vec{-25, 13, 15};

std::vector<int32_t> expect_res{0, 26,30};
std::vector<Plaintext> expect_res_pt_vec;
std::vector<Plaintext> a_pt_vec;
std::vector<Plaintext> b_pt_vec;
auto vec_size = a_vec.size();
Expand All @@ -124,6 +148,12 @@ namespace heu::lib::algorithms::leichi_paillier::test {
b_pt_vec.push_back(b);
}

for (size_t i = 0; i < vec_size; i++) {
Plaintext res;
res.Set(expect_res[i]);
expect_res_pt_vec.push_back(res);
}

std::vector<Plaintext *> a_pt_pts;
std::vector<Plaintext *> b_pt_pts;
ValueVecToPtsVec(a_pt_vec, a_pt_pts);
Expand All @@ -142,12 +172,16 @@ namespace heu::lib::algorithms::leichi_paillier::test {
ValueVecToPtsVec(res_ct_vec, res_ct_pts);
auto res_ct_span = absl::MakeConstSpan(res_ct_pts.data(), vec_size);
std::vector<Plaintext> res_pt_vec = decryptor_->Decrypt(res_ct_span);
for (size_t i = 0; i < vec_size; i++) {
EXPECT_EQ(res_pt_vec[i], expect_res_pt_vec[i]);
}
}

TEST_F(LEICHITest, DISABLE_PTPlusCT) {
std::vector<int32_t> a_vec{25, 13, 15};
std::vector<int32_t> b_vec{25, 13, 15};

std::vector<int32_t> expect_res{50, 26,30};
std::vector<Plaintext> expect_res_pt_vec;
std::vector<Plaintext> a_pt_vec;
std::vector<Plaintext> b_pt_vec;
auto vec_size = a_vec.size();
Expand All @@ -161,6 +195,13 @@ namespace heu::lib::algorithms::leichi_paillier::test {
b.Set(b_vec[i]);
b_pt_vec.push_back(b);
}

for (size_t i = 0; i < vec_size; i++) {
Plaintext res;
res.Set(expect_res[i]);
expect_res_pt_vec.push_back(res);
}

std::vector<Plaintext *> a_pt_pts;
std::vector<Plaintext *> b_pt_pts;
ValueVecToPtsVec(a_pt_vec, a_pt_pts);
Expand All @@ -179,14 +220,17 @@ namespace heu::lib::algorithms::leichi_paillier::test {
ValueVecToPtsVec(res_ct_vec, res_ct_pts);
auto res_ct_span = absl::MakeConstSpan(res_ct_pts.data(), vec_size);
std::vector<Plaintext> res_pt_vec = decryptor_->Decrypt(res_ct_span);
for (size_t i = 0; i < vec_size; i++) {
EXPECT_EQ(res_pt_vec[i], expect_res_pt_vec[i]);
}
}


TEST_F(LEICHITest, DISABLE_CTSubCT) {
std::vector<int32_t> a_vec{50, 30};
std::vector<int32_t> b_vec{-20, 10};
std::vector<int32_t> expect_res{30, 20};

std::vector<int32_t> expect_res{70, 20};
std::vector<Plaintext> expect_res_pt_vec;
std::vector<Plaintext> a_pt_vec;
std::vector<Plaintext> b_pt_vec;
auto vec_size = a_vec.size();
Expand All @@ -199,6 +243,12 @@ namespace heu::lib::algorithms::leichi_paillier::test {
b.Set(b_vec[i]);
b_pt_vec.push_back(b);
}

for (size_t i = 0; i < vec_size; i++) {
Plaintext res;
res.Set(expect_res[i]);
expect_res_pt_vec.push_back(res);
}
std::vector<Plaintext *> a_pt_pts;
std::vector<Plaintext *> b_pt_pts;
ValueVecToPtsVec(a_pt_vec, a_pt_pts);
Expand All @@ -220,12 +270,16 @@ namespace heu::lib::algorithms::leichi_paillier::test {
ValueVecToPtsVec(res_ct_vec, res_ct_pts);
auto res_ct_span = absl::MakeConstSpan(res_ct_pts.data(), vec_size);
std::vector<Plaintext> res_pt_vec = decryptor_->Decrypt(res_ct_span);
for (size_t i = 0; i < vec_size; i++) {
EXPECT_EQ(res_pt_vec[i], expect_res_pt_vec[i]);
}
}

TEST_F(LEICHITest, DISABLE_CTSubPT) {
std::vector<int32_t> a_vec{25, 13, 15};
std::vector<int32_t> b_vec{20, 10, 10};

std::vector<int32_t> expect_res{5, 3, 5};
std::vector<Plaintext> expect_res_pt_vec;
std::vector<Plaintext> a_pt_vec;
std::vector<Plaintext> b_pt_vec;
auto vec_size = a_vec.size();
Expand All @@ -240,6 +294,12 @@ namespace heu::lib::algorithms::leichi_paillier::test {
b_pt_vec.push_back(b);
}

for (size_t i = 0; i < vec_size; i++) {
Plaintext res;
res.Set(expect_res[i]);
expect_res_pt_vec.push_back(res);
}

std::vector<Plaintext *> a_pt_pts;
std::vector<Plaintext *> b_pt_pts;
ValueVecToPtsVec(a_pt_vec, a_pt_pts);
Expand All @@ -256,12 +316,16 @@ namespace heu::lib::algorithms::leichi_paillier::test {
ValueVecToPtsVec(res_ct_vec, res_ct_pts);
auto res_ct_span = absl::MakeConstSpan(res_ct_pts.data(), vec_size);
std::vector<Plaintext> res_pt_vec = decryptor_->Decrypt(res_ct_span);
for (size_t i = 0; i < vec_size; i++) {
EXPECT_EQ(res_pt_vec[i], expect_res_pt_vec[i]);
}
}

TEST_F(LEICHITest, DISABLE_PTSubCT) {
std::vector<int32_t> a_vec{25, 13, 15};
std::vector<int32_t> b_vec{20, 10, 10};

std::vector<int32_t> expect_res{5, 3, 5};
std::vector<Plaintext> expect_res_pt_vec;
std::vector<Plaintext> a_pt_vec;
std::vector<Plaintext> b_pt_vec;
auto vec_size = a_vec.size();
Expand All @@ -274,6 +338,12 @@ namespace heu::lib::algorithms::leichi_paillier::test {
b.Set(b_vec[i]);
b_pt_vec.push_back(b);
}

for (size_t i = 0; i < vec_size; i++) {
Plaintext res;
res.Set(expect_res[i]);
expect_res_pt_vec.push_back(res);
}
std::vector<Plaintext *> a_pt_pts;
std::vector<Plaintext *> b_pt_pts;
ValueVecToPtsVec(a_pt_vec, a_pt_pts);
Expand All @@ -290,13 +360,17 @@ namespace heu::lib::algorithms::leichi_paillier::test {
ValueVecToPtsVec(res_ct_vec, res_ct_pts);
auto res_ct_span = absl::MakeConstSpan(res_ct_pts.data(), vec_size);
std::vector<Plaintext> res_pt_vec = decryptor_->Decrypt(res_ct_span);

for (size_t i = 0; i < vec_size; i++) {
EXPECT_EQ(res_pt_vec[i], expect_res_pt_vec[i]);
}
}

TEST_F(LEICHITest, DISABLE_CTMultiplyPT) {
std::vector<int32_t> a_vec{-5, 3};
std::vector<int32_t> b_vec{2, 1};
std::vector<int32_t> expect_res{10, 3};

std::vector<int32_t> expect_res{-10, 3};
std::vector<Plaintext> expect_res_pt_vec;
std::vector<Plaintext> a_pt_vec;
std::vector<Plaintext> b_pt_vec;

Expand All @@ -310,6 +384,13 @@ namespace heu::lib::algorithms::leichi_paillier::test {
b.Set(b_vec[i]);
b_pt_vec.push_back(b);
}

for (size_t i = 0; i < a_vec.size(); i++) {
Plaintext res;
res.Set(expect_res[i]);
expect_res_pt_vec.push_back(res);
}

std::vector<Plaintext *> a_pt_pts;
std::vector<Plaintext *> b_pt_pts;
ValueVecToPtsVec(a_pt_vec, a_pt_pts);
Expand All @@ -326,5 +407,8 @@ namespace heu::lib::algorithms::leichi_paillier::test {
ValueVecToPtsVec(res_ct_vec, res_ct_pts);
auto res_ct_span = absl::MakeConstSpan(res_ct_pts.data(), res_ct_pts.size());
std::vector<Plaintext> res_pt_vec = decryptor_->Decrypt(res_ct_span);
for (size_t i = 0; i < a_vec.size(); i++) {
EXPECT_EQ(res_pt_vec[i], expect_res_pt_vec[i]);
}
}
} // namespace heu::lib::algorithms::leichi_paillier::test
Loading

0 comments on commit 1456de6

Please sign in to comment.