From a42023a2ed710dbf462435ae35ff92ec0346d23a Mon Sep 17 00:00:00 2001 From: shaojian-ant <121606618+shaojian-ant@users.noreply.github.com> Date: Tue, 15 Oct 2024 11:23:05 +0800 Subject: [PATCH] repo-sync-2024-10-14T19:21:55+0800 (#157) * repo-sync-2024-10-14T17:48:53+0800 * repo-sync-2024-10-14T19:21:55+0800 * Update BUILD.bazel * Update BUILD.bazel * Update repositories.bzl * Update base.cc * Update public_key.cc * Update public_key.h * Update public_key.cc * Update public_key.h * Update secret_key.cc * Update base.cc * Update schema.h * Update base.cc * Update secret_key.cc * Update version.py * Update repositories.bzl --- CHANGELOGS.md | 3 +++ WORKSPACE | 4 ++++ .../incubator/mock_fhe/encryptor.cc | 2 +- heu/algorithms/paillier_zahlen/base.cc | 21 ++++++++++++++++--- heu/algorithms/paillier_zahlen/base.h | 5 +++++ heu/algorithms/paillier_zahlen/decryptor.cc | 19 ++++++++++++++--- .../algorithms/paillier_ic/decryptor.cc | 19 ++++++++++++++--- .../algorithms/paillier_zahlen/decryptor.cc | 19 ++++++++++++++--- .../algorithms/paillier_zahlen/secret_key.cc | 21 ++++++++++++++++--- .../algorithms/paillier_zahlen/secret_key.h | 5 +++++ heu/library/numpy/shape.h | 1 + heu/library/phe/base/schema.h | 7 +++---- heu/pylib/version.py | 2 +- heu/spi/he/sketches/common/item_tool.h | 1 + heu/spi/he/sketches/common/keys.cc | 2 +- heu/spi/utils/formater.h | 1 + third_party/bazel_cpp/repositories.bzl | 11 +++++----- 17 files changed, 115 insertions(+), 28 deletions(-) diff --git a/CHANGELOGS.md b/CHANGELOGS.md index 115528af..8f08dc18 100644 --- a/CHANGELOGS.md +++ b/CHANGELOGS.md @@ -8,6 +8,9 @@ ## [Unreleased] +## [0.5.1] + +- [other] Update yacl version ## [0.5.0] diff --git a/WORKSPACE b/WORKSPACE index 789503bb..fc3ce2b1 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -39,6 +39,10 @@ rules_foreign_cc_dependencies( register_preinstalled_tools = True, ) +load("@bazel_features//:deps.bzl", "bazel_features_deps") + +bazel_features_deps() + #### for python #### load("@rules_python//python:repositories.bzl", "py_repositories") diff --git a/heu/algorithms/incubator/mock_fhe/encryptor.cc b/heu/algorithms/incubator/mock_fhe/encryptor.cc index 6a9d45d4..526e3d6d 100644 --- a/heu/algorithms/incubator/mock_fhe/encryptor.cc +++ b/heu/algorithms/incubator/mock_fhe/encryptor.cc @@ -50,7 +50,7 @@ void Encryptor::EncryptWithAudit(const Plaintext &m, Ciphertext *ct_out, poly_degree_); ct_out->array_ = m.array_; ct_out->scale_ = m.scale_; - audit_out->assign(fmt::format("mock_fhe:{}", m.array_)); + audit_out->assign(fmt::format("mock_fhe:{}", fmt::join(m.array_, ","))); } } // namespace heu::algos::mock_fhe diff --git a/heu/algorithms/paillier_zahlen/base.cc b/heu/algorithms/paillier_zahlen/base.cc index 6ca59449..e48e2b17 100644 --- a/heu/algorithms/paillier_zahlen/base.cc +++ b/heu/algorithms/paillier_zahlen/base.cc @@ -41,9 +41,24 @@ void SecretKey::Init() { MPInt q_square_inv; MPInt::InvertMod(q_square_, p_square_, &q_square_inv); q_square_inv_mul_q_square_ = - q_square_inv * q_square_; // [(q^2)^{-1} mod p^2] * q^2 - phi_p_square_ = p_ * (p_ - MPInt::_1_); // p(p-1) - phi_q_square_ = q_ * (q_ - MPInt::_1_); // q(q-1) + q_square_inv * q_square_; // [(q^2)^{-1} mod p^2] * q^2 + MPInt::InvertMod(p_, q_, &p_inv_mod_q_); // p^{-1} mod q + phi_p_square_ = p_ * (p_ - MPInt::_1_); // p(p-1) + phi_q_square_ = q_ * (q_ - MPInt::_1_); // q(q-1) + phi_p_ = p_ - 1_mp; // p-1 + phi_q_ = q_ - 1_mp; // q-1 + + // Precompute hp + MPInt n = p_ * q_; + MPInt g = n + 1_mp; + MPInt::PowMod(g, phi_p_, p_square_, &hp_); + hp_ = hp_.DecrOne() / p_; + MPInt::InvertMod(hp_, p_, &hp_); + + // Precompute hq + MPInt::PowMod(g, phi_q_, q_square_, &hq_); + hq_ = hq_.DecrOne() / q_; + MPInt::InvertMod(hq_, q_, &hq_); } MPInt SecretKey::PowModNSquareCrt(const MPInt &base, const MPInt &exp) const { diff --git a/heu/algorithms/paillier_zahlen/base.h b/heu/algorithms/paillier_zahlen/base.h index 38ed4148..522b5ba8 100644 --- a/heu/algorithms/paillier_zahlen/base.h +++ b/heu/algorithms/paillier_zahlen/base.h @@ -59,8 +59,13 @@ class SecretKey : public spi::KeySketch { MPInt q_square_; // q^2 MPInt n_square_; // n_ * n_ MPInt q_square_inv_mul_q_square_; // (q^2)^{-1} mod p^2 * q^2 + MPInt p_inv_mod_q_; // p^{-1} mod q MPInt phi_p_square_; // p(p-1) MPInt phi_q_square_; // q(q-1) + MPInt phi_p_; // p-1 + MPInt phi_q_; // q-1 + MPInt hp_; + MPInt hq_; void Init(); // base^exp mod n^2, n = p * q diff --git a/heu/algorithms/paillier_zahlen/decryptor.cc b/heu/algorithms/paillier_zahlen/decryptor.cc index 3f033510..ee577c00 100644 --- a/heu/algorithms/paillier_zahlen/decryptor.cc +++ b/heu/algorithms/paillier_zahlen/decryptor.cc @@ -36,9 +36,22 @@ void Decryptor::Decrypt(const Ciphertext &ct, Plaintext *out) const { MPInt c(ct.c_); pk_->m_space_->MapBackToZSpace(&c); - *out = sk_->PowModNSquareCrt(c, sk_->lambda_); - MPInt::MulMod(out->DecrOne() / pk_->n_, sk_->mu_, pk_->n_, out); - // handle negative numbers + MPInt mp; + MPInt::PowMod(c, sk_->phi_p_, sk_->p_square_, &mp); + mp = mp.DecrOne() / sk_->p_; + MPInt::MulMod(mp, sk_->hp_, sk_->p_, &mp); + + MPInt mq; + MPInt::PowMod(c, sk_->phi_q_, sk_->q_square_, &mq); + mq = mq.DecrOne() / sk_->q_; + MPInt::MulMod(mq, sk_->hq_, sk_->q_, &mq); + + // Apply the CRT + MPInt::MulMod(mq - mp, sk_->p_inv_mod_q_, sk_->q_, out); + MPInt::Mul(*out, sk_->p_, out); + MPInt::Add(*out, mp, out); + + // Handle negative numbers if (*out > pk_->n_half_) { *out -= pk_->n_; } diff --git a/heu/library/algorithms/paillier_ic/decryptor.cc b/heu/library/algorithms/paillier_ic/decryptor.cc index 637478be..751b81af 100644 --- a/heu/library/algorithms/paillier_ic/decryptor.cc +++ b/heu/library/algorithms/paillier_ic/decryptor.cc @@ -25,9 +25,22 @@ namespace heu::lib::algorithms::paillier_ic { void Decryptor::Decrypt(const Ciphertext &ct, MPInt *out) const { VALIDATE(ct); - auto cl = sk_.PowModNSquareCrt(ct.c_, sk_.lambda_); - MPInt::MulMod(cl.DecrOne() / pk_.n_, sk_.mu_, pk_.n_, out); - // handle negative numbers + MPInt mp; + MPInt::PowMod(ct.c_, sk_.phi_p_, sk_.p_square_, &mp); + mp = mp.DecrOne() / sk_.p_; + MPInt::MulMod(mp, sk_.hp_, sk_.p_, &mp); + + MPInt mq; + MPInt::PowMod(ct.c_, sk_.phi_q_, sk_.q_square_, &mq); + mq = mq.DecrOne() / sk_.q_; + MPInt::MulMod(mq, sk_.hq_, sk_.q_, &mq); + + // Apply the CRT + MPInt::MulMod(mq - mp, sk_.p_inv_mod_q_, sk_.q_, out); + MPInt::Mul(*out, sk_.p_, out); + MPInt::Add(*out, mp, out); + + // Handle negative numbers if (*out > pk_.n_half_) { *out -= pk_.n_; } diff --git a/heu/library/algorithms/paillier_zahlen/decryptor.cc b/heu/library/algorithms/paillier_zahlen/decryptor.cc index d6f20190..a57fee97 100644 --- a/heu/library/algorithms/paillier_zahlen/decryptor.cc +++ b/heu/library/algorithms/paillier_zahlen/decryptor.cc @@ -35,9 +35,22 @@ void Decryptor::Decrypt(const Ciphertext &ct, MPInt *out) const { MPInt c(ct.c_); pk_.m_space_->MapBackToZSpace(&c); - *out = sk_.PowModNSquareCrt(c, sk_.lambda_); - MPInt::MulMod(out->DecrOne() / pk_.n_, sk_.mu_, pk_.n_, out); - // handle negative numbers + MPInt mp; + MPInt::PowMod(c, sk_.phi_p_, sk_.p_square_, &mp); + mp = mp.DecrOne() / sk_.p_; + MPInt::MulMod(mp, sk_.hp_, sk_.p_, &mp); + + MPInt mq; + MPInt::PowMod(c, sk_.phi_q_, sk_.q_square_, &mq); + mq = mq.DecrOne() / sk_.q_; + MPInt::MulMod(mq, sk_.hq_, sk_.q_, &mq); + + // Apply the CRT + MPInt::MulMod(mq - mp, sk_.p_inv_mod_q_, sk_.q_, out); + MPInt::Mul(*out, sk_.p_, out); + MPInt::Add(*out, mp, out); + + // Handle negative numbers if (*out > pk_.n_half_) { *out -= pk_.n_; } diff --git a/heu/library/algorithms/paillier_zahlen/secret_key.cc b/heu/library/algorithms/paillier_zahlen/secret_key.cc index ddd81822..75a105a5 100644 --- a/heu/library/algorithms/paillier_zahlen/secret_key.cc +++ b/heu/library/algorithms/paillier_zahlen/secret_key.cc @@ -23,9 +23,24 @@ void SecretKey::Init() { MPInt q_square_inv; MPInt::InvertMod(q_square_, p_square_, &q_square_inv); q_square_inv_mul_q_square_ = - q_square_inv * q_square_; // [(q^2)^{-1} mod p^2] * q^2 - phi_p_square_ = p_ * (p_ - MPInt::_1_); // p(p-1) - phi_q_square_ = q_ * (q_ - MPInt::_1_); // q(q-1) + q_square_inv * q_square_; // [(q^2)^{-1} mod p^2] * q^2 + MPInt::InvertMod(p_, q_, &p_inv_mod_q_); // p^{-1} mod q + phi_p_square_ = p_ * (p_ - MPInt::_1_); // p(p-1) + phi_q_square_ = q_ * (q_ - MPInt::_1_); // q(q-1) + phi_p_ = p_ - 1_mp; // p-1 + phi_q_ = q_ - 1_mp; // q-1 + + // Precompute hp + MPInt n = p_ * q_; + MPInt g = n + 1_mp; + MPInt::PowMod(g, phi_p_, p_square_, &hp_); + hp_ = hp_.DecrOne() / p_; + MPInt::InvertMod(hp_, p_, &hp_); + + // Precompute hq + MPInt::PowMod(g, phi_q_, q_square_, &hq_); + hq_ = hq_.DecrOne() / q_; + MPInt::InvertMod(hq_, q_, &hq_); } MPInt SecretKey::PowModNSquareCrt(const MPInt &base, const MPInt &exp) const { diff --git a/heu/library/algorithms/paillier_zahlen/secret_key.h b/heu/library/algorithms/paillier_zahlen/secret_key.h index cae1d1da..9888fd65 100644 --- a/heu/library/algorithms/paillier_zahlen/secret_key.h +++ b/heu/library/algorithms/paillier_zahlen/secret_key.h @@ -30,8 +30,13 @@ class SecretKey : public HeObject { MPInt q_square_; // q^2 MPInt n_square_; // n_ * n_ MPInt q_square_inv_mul_q_square_; // (q^2)^{-1} mod p^2 * q^2 + MPInt p_inv_mod_q_; // p^{-1} mod q MPInt phi_p_square_; // p(p-1) MPInt phi_q_square_; // q(q-1) + MPInt phi_p_; // p-1 + MPInt phi_q_; // q-1 + MPInt hp_; + MPInt hq_; void Init(); // base^exp mod n^2, n = p * q diff --git a/heu/library/numpy/shape.h b/heu/library/numpy/shape.h index 0ad15542..30341ffb 100644 --- a/heu/library/numpy/shape.h +++ b/heu/library/numpy/shape.h @@ -16,6 +16,7 @@ #include "fmt/format.h" #include "fmt/ostream.h" +#include "fmt/ranges.h" #include "msgpack.hpp" namespace heu::lib::numpy { diff --git a/heu/library/phe/base/schema.h b/heu/library/phe/base/schema.h index b23b2d55..3b6b4f81 100644 --- a/heu/library/phe/base/schema.h +++ b/heu/library/phe/base/schema.h @@ -39,14 +39,13 @@ namespace heu::lib::phe { #define ENUM_ELEMENT_HELPER(idx, enable, name) ECHO_##enable(name, idx) #define ENUM_ELEMENT(idx, enable, name) ENUM_ELEMENT_HELPER(idx, enable, name) - // [SPI: Please register your algorithm here] || progress: (2 of 5) // If you add a new schema, change this !! // clang-format off enum class SchemaType : uint8_t { - ENUM_ELEMENT(0,true, Mock) // Mock He - ENUM_ELEMENT(1,true, OU) - ENUM_ELEMENT(2,ENABLE_IPCL, IPCL) + ENUM_ELEMENT(0, true, Mock) // Mock He + ENUM_ELEMENT(1, true, OU) + ENUM_ELEMENT(2, ENABLE_IPCL, IPCL) ENUM_ELEMENT(3, ENABLE_GPAILLIER, GPaillier) ENUM_ELEMENT(4, true, ZPaillier) // Preferred ENUM_ELEMENT(5, true, FPaillier) diff --git a/heu/pylib/version.py b/heu/pylib/version.py index ef6eafa1..d9956c19 100644 --- a/heu/pylib/version.py +++ b/heu/pylib/version.py @@ -13,4 +13,4 @@ # limitations under the License. -__version__ = "0.6.0.dev0" +__version__ = "0.6.0.dev20241015" diff --git a/heu/spi/he/sketches/common/item_tool.h b/heu/spi/he/sketches/common/item_tool.h index e3f784e4..1714d2d8 100644 --- a/heu/spi/he/sketches/common/item_tool.h +++ b/heu/spi/he/sketches/common/item_tool.h @@ -20,6 +20,7 @@ #include #include +#include "fmt/ranges.h" #include "yacl/base/buffer.h" #include "yacl/base/byte_container_view.h" #include "yacl/utils/spi/sketch/scalar_tools.h" diff --git a/heu/spi/he/sketches/common/keys.cc b/heu/spi/he/sketches/common/keys.cc index 72e539fb..8bf50ee5 100644 --- a/heu/spi/he/sketches/common/keys.cc +++ b/heu/spi/he/sketches/common/keys.cc @@ -14,7 +14,7 @@ #include "heu/spi/he/sketches/common/keys.h" -#include "fmt/format.h" +#include "fmt/ranges.h" template <> struct fmt::formatter::value_type> { diff --git a/heu/spi/utils/formater.h b/heu/spi/utils/formater.h index 174b06f0..6f8e5585 100644 --- a/heu/spi/utils/formater.h +++ b/heu/spi/utils/formater.h @@ -19,6 +19,7 @@ #include "absl/types/span.h" #include "fmt/format.h" +#include "fmt/ranges.h" // for fmt lib namespace std { diff --git a/third_party/bazel_cpp/repositories.bzl b/third_party/bazel_cpp/repositories.bzl index 1990f07f..0776bac4 100644 --- a/third_party/bazel_cpp/repositories.bzl +++ b/third_party/bazel_cpp/repositories.bzl @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository") load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") @@ -120,8 +119,8 @@ def _com_github_intel_ipp(): maybe( http_archive, name = "com_github_intel_ipp", - sha256 = "1ecfa70328221748ceb694debffa0106b92e0f9bf6a484f8e8512c2730c7d730", - strip_prefix = "ipp-crypto-ippcp_2021.8", + sha256 = "d70f42832337775edb022ca8ac1ac418f272e791ec147778ef7942aede414cdc", + strip_prefix = "cryptography-primitives-ippcp_2021.8", build_file = "@com_alipay_sf_heu//third_party/bazel_cpp:ipp.BUILD", patch_args = ["-p1"], patches = [ @@ -137,10 +136,10 @@ def _yacl(): http_archive, name = "yacl", urls = [ - "https://github.com/secretflow/yacl/archive/refs/tags/0.4.5b2.tar.gz", + "https://github.com/secretflow/yacl/archive/refs/tags/0.4.5b8_nightly_20241014.tar.gz", ], - strip_prefix = "yacl-0.4.5b2", - sha256 = "b3fb75d41a32b80145a3bb9d36b8c039a262191f1a2f037292c649344289b01b", + strip_prefix = "yacl-0.4.5b8_nightly_20241014", + sha256 = "9141792f07eba507ffd21c57ec3df2ad5fdf90ce605ffb7bc1b7b4e84a9c34fa", ) def _rules_cuda():