Skip to content

Commit

Permalink
repo-sync-2024-10-14T19:21:55+0800 (#157)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
shaojian-ant authored Oct 15, 2024
1 parent 4ae7a20 commit a42023a
Show file tree
Hide file tree
Showing 17 changed files with 115 additions and 28 deletions.
3 changes: 3 additions & 0 deletions CHANGELOGS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
## [Unreleased]

## [0.5.1]

- [other] Update yacl version

## [0.5.0]

Expand Down
4 changes: 4 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion heu/algorithms/incubator/mock_fhe/encryptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 18 additions & 3 deletions heu/algorithms/paillier_zahlen/base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions heu/algorithms/paillier_zahlen/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,13 @@ class SecretKey : public spi::KeySketch<spi::HeKeyType::SecretKey> {
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
Expand Down
19 changes: 16 additions & 3 deletions heu/algorithms/paillier_zahlen/decryptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
}
Expand Down
19 changes: 16 additions & 3 deletions heu/library/algorithms/paillier_ic/decryptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
}
Expand Down
19 changes: 16 additions & 3 deletions heu/library/algorithms/paillier_zahlen/decryptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
}
Expand Down
21 changes: 18 additions & 3 deletions heu/library/algorithms/paillier_zahlen/secret_key.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions heu/library/algorithms/paillier_zahlen/secret_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ class SecretKey : public HeObject<SecretKey> {
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
Expand Down
1 change: 1 addition & 0 deletions heu/library/numpy/shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "fmt/format.h"
#include "fmt/ostream.h"
#include "fmt/ranges.h"
#include "msgpack.hpp"

namespace heu::lib::numpy {
Expand Down
7 changes: 3 additions & 4 deletions heu/library/phe/base/schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion heu/pylib/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# limitations under the License.


__version__ = "0.6.0.dev0"
__version__ = "0.6.0.dev20241015"
1 change: 1 addition & 0 deletions heu/spi/he/sketches/common/item_tool.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <string>
#include <vector>

#include "fmt/ranges.h"
#include "yacl/base/buffer.h"
#include "yacl/base/byte_container_view.h"
#include "yacl/utils/spi/sketch/scalar_tools.h"
Expand Down
2 changes: 1 addition & 1 deletion heu/spi/he/sketches/common/keys.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#include "heu/spi/he/sketches/common/keys.h"

#include "fmt/format.h"
#include "fmt/ranges.h"

template <>
struct fmt::formatter<std::map<std::string, std::string>::value_type> {
Expand Down
1 change: 1 addition & 0 deletions heu/spi/utils/formater.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "absl/types/span.h"
#include "fmt/format.h"
#include "fmt/ranges.h"

// for fmt lib
namespace std {
Expand Down
11 changes: 5 additions & 6 deletions third_party/bazel_cpp/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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 = [
Expand All @@ -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():
Expand Down

0 comments on commit a42023a

Please sign in to comment.