Skip to content

Commit

Permalink
Merge remote-tracking branch 'main/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
dterazhao committed Nov 15, 2023
2 parents 6f43572 + b8e2f0d commit 4adb390
Show file tree
Hide file tree
Showing 52 changed files with 271 additions and 299 deletions.
2 changes: 2 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ build:macos --host_copt=-Wa,--noexecstack
# Bazel will automatic pick platform config since we have enable_platform_specific_config set
build:macos --features=-supports_dynamic_linker
build:macos --cxxopt -Wno-error=unused-const-variable
build:macos --cxxopt -Wno-error=sign-compare # for eigen 3.4
build:macos --cxxopt -Wno-sign-compare # for eigen 3.4
build:macos --macos_minimum_os=11.0

# static link libstdc++ & libgcc
Expand Down
64 changes: 30 additions & 34 deletions .circleci/continue-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,51 +46,47 @@ jobs:
path: test_logs.tar.gz
macOS_ut:
macos:
xcode: 14.2
xcode: 15.0.0
environment:
HOMEBREW_NO_AUTO_UPDATE: 1
resource_class: macos.m1.large.gen1
steps:
- checkout
- when:
condition:
equal: [ main, << pipeline.git.branch >> ]
steps:
- run:
name: "Install homebrew dependencies"
command: brew install bazelisk cmake ninja libomp wget
- run:
name: "Install Miniconda"
command: |
wget https://repo.anaconda.com/miniconda/Miniconda3-py38_23.1.0-1-MacOSX-arm64.sh -O ~/miniconda.sh
bash ~/miniconda.sh -b -p $HOME/miniconda
source $HOME/miniconda/bin/activate
conda init bash zsh
pip install numpy
- run:
name: Checkout devtools
command: git clone https://github.com/secretflow/devtools.git
- run:
name: "bazel test"
command: |
bazel test --ui_event_filters=-info,-debug,-warning --test_timeout=1800 --test_output=errors --verbose_failures -- //heu/... -//heu/experimental/tfhe/...
- run:
name: "Install homebrew dependencies"
command: brew install bazelisk cmake ninja libomp wget
- run:
name: "Install Miniconda"
command: |
wget https://repo.anaconda.com/miniconda/Miniconda3-py38_23.1.0-1-MacOSX-arm64.sh -O ~/miniconda.sh
bash ~/miniconda.sh -b -p $HOME/miniconda
source $HOME/miniconda/bin/activate
conda init bash zsh
pip install numpy
- run:
name: Checkout devtools
command: git clone https://github.com/secretflow/devtools.git
- run:
name: "bazel test"
command: |
bazel test --ui_event_filters=-info,-debug,-warning --test_timeout=1800 --test_output=errors --verbose_failures -- //heu/... -//heu/experimental/tfhe/...
sh devtools/rename-junit-xml.sh
find bazel-bin/ -perm +111 -type f -name "*_test" -print0 | xargs -0 tar -cvzf test_binary.tar.gz
find bazel-testlogs/ -type f -name "test.log" -print0 | xargs -0 tar -cvzf test_logs.tar.gz
sh devtools/rename-junit-xml.sh
find bazel-bin/ -perm +111 -type f -name "*_test" -print0 | xargs -0 tar -cvzf test_binary.tar.gz
find bazel-testlogs/ -type f -name "test.log" -print0 | xargs -0 tar -cvzf test_logs.tar.gz
exit ${test_status}
- store_test_results:
path: test-results
- store_artifacts:
path: test_binary.tar.gz
- store_artifacts:
path: test_logs.tar.gz
exit ${test_status}
- store_test_results:
path: test-results
- store_artifacts:
path: test_binary.tar.gz
- store_artifacts:
path: test_logs.tar.gz
# Invoke jobs via workflows
# See: https://circleci.com/docs/2.0/configuration-reference/#workflows
workflows:
ut:
when: << pipeline.parameters.build-and-run >>
jobs:
- linux_ut
- macOS_ut
- macOS_ut
2 changes: 1 addition & 1 deletion CHANGELOGS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
## [Unreleased]

- [Feature] Add DGK cryptosystem
- [Feature] Add Damgard-Juric cryptosystem
- [Feature] Add Damgard-Jurik cryptosystem
- [Feature] Add a new paillier implementation based on ClustarFPGA hardware from Clustar Technology
- [Feature] Add an experimental implementation of GPU-based paillier cryptosystem
- [Optimize] Optimize vectorized spi in mat mul
Expand Down
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

SECRETFLOW_GIT = "https://github.com/secretflow"

YACL_COMMIT_ID = "f933d7ff4caf0d9f7ea84cc3e9f51a9a6ee9eeca"
YACL_COMMIT_ID = "3b82829f7c25f581bf6b5d7ac7e7296ea4746cd3"

git_repository(
name = "yacl",
Expand Down
2 changes: 1 addition & 1 deletion heu/library/algorithms/clean_template/public_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class PublicKey {

std::string ToString() const;

// Valid plaintext range: (max_int_, -max_int_)
// Valid plaintext range: [max_int_, -max_int_]
const Plaintext &PlaintextBound() const &;

yacl::Buffer Serialize() const;
Expand Down
2 changes: 1 addition & 1 deletion heu/library/algorithms/dgk/dgk_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ TEST_F(DGKTest, MinMaxDecrypt) {
Plaintext plain = pk_.PlainModule();
EXPECT_THROW(encryptor_->Encrypt(plain), std::exception); // too many bits

plain = pk_.PlaintextBound();
plain = pk_.PlaintextBound() + 1_mp;
EXPECT_THROW(encryptor_->Encrypt(plain), std::exception); // too many bits

Plaintext plain2;
Expand Down
2 changes: 1 addition & 1 deletion heu/library/algorithms/dgk/encryptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace heu::lib::algorithms::dgk {
Ciphertext Encryptor::EncryptZero() const { return Ciphertext{pk_.RandomHr()}; }

Ciphertext Encryptor::Encrypt(const Plaintext &m) const {
YACL_ENFORCE(m.CompareAbs(pk_.PlaintextBound()) < 0,
YACL_ENFORCE(m.CompareAbs(pk_.PlaintextBound()) <= 0,
"message number out of range, message={}, max (abs)={}", m,
pk_.PlaintextBound());
Ciphertext ctR;
Expand Down
2 changes: 1 addition & 1 deletion heu/library/algorithms/dj/decryptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Plaintext Decryptor::Decrypt(const Ciphertext& ct) const {
HE_ASSERT(!ct.c_.IsNegative() && ct.c_ < pk_.CipherModule(),
"Decryptor: Invalid ciphertext");
Plaintext m{sk_.Decrypt(pk_.MapBackToZSpace(ct.c_))};
return m >= pk_.PlaintextBound() ? m - pk_.PlainModule() : m;
return m > pk_.PlaintextBound() ? m - pk_.PlainModule() : m;
}

} // namespace heu::lib::algorithms::dj
2 changes: 1 addition & 1 deletion heu/library/algorithms/dj/dj_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ TEST_F(DJTest, MinMaxDecrypt) {
Plaintext plain = pk_.PlainModule();
EXPECT_THROW(encryptor_->Encrypt(plain), std::exception); // too many bits

plain = pk_.PlaintextBound();
plain = pk_.PlaintextBound() + 1_mp;
EXPECT_THROW(encryptor_->Encrypt(plain), std::exception); // too many bits

Plaintext plain2;
Expand Down
2 changes: 1 addition & 1 deletion heu/library/algorithms/dj/encryptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Ciphertext Encryptor::EncryptZero() const {
}

Ciphertext Encryptor::Encrypt(const Plaintext &m) const {
YACL_ENFORCE(m.CompareAbs(pk_.PlaintextBound()) < 0,
YACL_ENFORCE(m.CompareAbs(pk_.PlaintextBound()) <= 0,
"message number out of range, message={}, max (abs)={}", m,
pk_.PlaintextBound());
Ciphertext ctR;
Expand Down
2 changes: 1 addition & 1 deletion heu/library/algorithms/elgamal/elgamal_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ TEST_F(ElGamalTest, EncDecWorks) {
});

// too big to decrypt
EXPECT_ANY_THROW(encryptor.Encrypt(pk_.PlaintextBound()));
EXPECT_ANY_THROW(encryptor.Encrypt(pk_.PlaintextBound() + 1_mp));
}

TEST_F(ElGamalTest, CiphertextEvaluate) {
Expand Down
2 changes: 1 addition & 1 deletion heu/library/algorithms/elgamal/public_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class PublicKey {

std::string ToString() const;

// Valid plaintext range: (max_int_, -max_int_)
// Valid plaintext range: [max_int_, -max_int_]
const Plaintext &PlaintextBound() const &;

yacl::Buffer Serialize() const;
Expand Down
4 changes: 2 additions & 2 deletions heu/library/algorithms/elgamal/scalar_encryptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Ciphertext Encryptor::EncryptZero() const {
}

Ciphertext Encryptor::Encrypt(const Plaintext& m) const {
YACL_ENFORCE(m.CompareAbs(pk_.PlaintextBound()) < 0,
YACL_ENFORCE(m.CompareAbs(pk_.PlaintextBound()) <= 0,
"message number out of range, message={}, max (abs)={}", m,
pk_.PlaintextBound());

Expand All @@ -42,7 +42,7 @@ Ciphertext Encryptor::Encrypt(const Plaintext& m) const {

std::pair<Ciphertext, std::string> Encryptor::EncryptWithAudit(
const Plaintext& m) const {
YACL_ENFORCE(m.CompareAbs(pk_.PlaintextBound()) < 0,
YACL_ENFORCE(m.CompareAbs(pk_.PlaintextBound()) <= 0,
"message number out of range, message={}, max (abs)={}", m,
pk_.PlaintextBound());

Expand Down
2 changes: 1 addition & 1 deletion heu/library/algorithms/elgamal/utils/lookup_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ constexpr static int64_t kTableMaxValue = 1LL << kLookupTableBits;
constexpr static int64_t kSearchMaxValue = 1LL << kExtraSearchBits;

const MPInt &LookupTable::MaxSupportedValue() {
const static MPInt max(kTableMaxValue * kSearchMaxValue);
const static MPInt max(kTableMaxValue * kSearchMaxValue - 1);
return max;
}

Expand Down
2 changes: 1 addition & 1 deletion heu/library/algorithms/elgamal/utils/lookup_table_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ TEST_F(LookupTableTest, MinMaxSearch) {
LookupTable table;
table.Init(ec_);

auto max_v = table.MaxSupportedValue() - 1_mp;
auto max_v = table.MaxSupportedValue();
auto point = ec_->MulBase(max_v);
EXPECT_EQ(table.Search(point), max_v.Get<int64_t>());

Expand Down
8 changes: 4 additions & 4 deletions heu/library/algorithms/mock/encryptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Ciphertext Encryptor::EncryptZero() const {
}

Ciphertext Encryptor::Encrypt(const Plaintext& m) const {
YACL_ENFORCE(m.bn_.CompareAbs(pk_.PlaintextBound().bn_) < 0,
YACL_ENFORCE(m.bn_.CompareAbs(pk_.PlaintextBound().bn_) <= 0,
"message number out of range, message={}, max (abs)={}", m.bn_,
pk_.PlaintextBound());

Expand All @@ -35,7 +35,7 @@ Ciphertext Encryptor::Encrypt(const Plaintext& m) const {

std::pair<Ciphertext, std::string> Encryptor::EncryptWithAudit(
const Plaintext& m) const {
YACL_ENFORCE(m.bn_.CompareAbs(pk_.PlaintextBound().bn_) < 0,
YACL_ENFORCE(m.bn_.CompareAbs(pk_.PlaintextBound().bn_) <= 0,
"message number out of range, message={}, max (abs)={}", m.bn_,
pk_.PlaintextBound());

Expand All @@ -54,7 +54,7 @@ std::vector<Ciphertext> Encryptor::Encrypt(ConstSpan<Plaintext> pts) const {
std::vector<Ciphertext> res;
res.reserve(pts.size());
for (size_t i = 0; i < pts.size(); ++i) {
YACL_ENFORCE(pts[i]->bn_.CompareAbs(pk_.PlaintextBound().bn_) < 0,
YACL_ENFORCE(pts[i]->bn_.CompareAbs(pk_.PlaintextBound().bn_) <= 0,
"message number out of range, pts={}, max (abs)={}",
pts[i]->bn_.ToHexString(), pk_.PlaintextBound());

Expand All @@ -70,7 +70,7 @@ Encryptor::EncryptWithAudit(ConstSpan<Plaintext> pts) const {
std::vector<std::string> res_s(pts.size());

for (size_t i = 0; i < pts.size(); ++i) {
YACL_ENFORCE(pts[i]->bn_.CompareAbs(pk_.PlaintextBound().bn_) < 0,
YACL_ENFORCE(pts[i]->bn_.CompareAbs(pk_.PlaintextBound().bn_) <= 0,
"message number out of range, pts={}, max (abs)={}",
pts[i]->bn_.ToHexString(), pk_.PlaintextBound());

Expand Down
4 changes: 2 additions & 2 deletions heu/library/algorithms/mock/evaluator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ namespace heu::lib::algorithms::mock {
void Evaluator::Randomize(Ciphertext *ct) const { (void)ct; }

void CheckRange(const PublicKey &pk, const Ciphertext &, const Plaintext &p) {
YACL_ENFORCE(p.bn_.CompareAbs(pk.PlaintextBound().bn_) < 0,
YACL_ENFORCE(p.bn_.CompareAbs(pk.PlaintextBound().bn_) <= 0,
"plaintext number out of range, message={}, max (abs)={}",
p.ToHexString(), pk.PlaintextBound());
}

void CheckRange(const PublicKey &pk, const Plaintext &p, const Ciphertext &) {
YACL_ENFORCE(p.bn_.CompareAbs(pk.PlaintextBound().bn_) < 0,
YACL_ENFORCE(p.bn_.CompareAbs(pk.PlaintextBound().bn_) <= 0,
"plaintext number out of range, message={}, max (abs)={}",
p.ToHexString(), pk.PlaintextBound());
}
Expand Down
2 changes: 1 addition & 1 deletion heu/library/algorithms/mock/public_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class PublicKey : public HeObject<PublicKey> {
return fmt::format("Mock phe public key with {} bit length", key_size_);
}

// Valid plaintext range: (max_int_, -max_int_)
// Valid plaintext range: [max_int_, -max_int_]
// [SPI: Critical]
[[nodiscard]] const Plaintext &PlaintextBound() const & { return max_int_; }

Expand Down
2 changes: 1 addition & 1 deletion heu/library/algorithms/ou/encryptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Ciphertext Encryptor::EncryptZero() const { return Ciphertext(GetHr()); }
template <bool audit>
Ciphertext Encryptor::EncryptImpl(const MPInt &m,
std::string *audit_str) const {
YACL_ENFORCE(m.CompareAbs(pk_.PlaintextBound()) < 0,
YACL_ENFORCE(m.CompareAbs(pk_.PlaintextBound()) <= 0,
"message number out of range, message={}, max (abs)={}", m,
pk_.PlaintextBound());

Expand Down
2 changes: 1 addition & 1 deletion heu/library/algorithms/ou/evaluator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void Evaluator::AddInplace(Ciphertext* a, const Ciphertext& b) const {

Ciphertext Evaluator::Add(const Ciphertext& a, const MPInt& p) const {
VALIDATE(a);
YACL_ENFORCE(p.CompareAbs(pk_.PlaintextBound()) < 0,
YACL_ENFORCE(p.CompareAbs(pk_.PlaintextBound()) <= 0,
"plaintext number out of range, message={}, max (abs)={}",
p.ToHexString(), pk_.PlaintextBound());

Expand Down
7 changes: 3 additions & 4 deletions heu/library/algorithms/ou/ou_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ TEST_F(OUTest, MinMaxDecrypt) {
MPInt plain = sk_.p_ / MPInt::_2_;
EXPECT_THROW(encryptor.Encrypt(plain), std::exception); // too many bits

plain = pk_.PlaintextBound();
plain = pk_.PlaintextBound() + 1_mp;
EXPECT_THROW(encryptor.Encrypt(plain), std::exception); // too many bits

MPInt plain2;
Expand All @@ -148,9 +148,8 @@ TEST_F(OUTest, MinMaxDecrypt) {
decryptor.Decrypt(ct0, &plain2);
EXPECT_EQ(plain, plain2);

plain.DecrOne();
EXPECT_THROW(encryptor.Encrypt(plain),
std::exception); // too many bits
plain.DecrOne(); // too many bits
EXPECT_THROW(encryptor.Encrypt(plain), std::exception);
}

TEST_F(OUTest, PlaintextEvaluate1) {
Expand Down
2 changes: 1 addition & 1 deletion heu/library/algorithms/ou/public_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class PublicKey : public HeObject<PublicKey> {
return !this->operator==(other);
}

// Valid plaintext range: (max_plaintext_, -max_plaintext_)
// Valid plaintext range: [max_plaintext_, -max_plaintext_]
[[nodiscard]] const MPInt &PlaintextBound() const & { return max_plaintext_; }
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/file.h>
Expand Down
2 changes: 1 addition & 1 deletion heu/library/algorithms/paillier_clustar_fpga/public_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class PublicKey : public HeObject<PublicKey> {

std::string ToString() const override;

// Valid plaintext range: (-max_int_, max_int_)
// Valid plaintext range: [max_int_, -max_int_]
const Plaintext& PlaintextBound() const&;

// Serialize and Deserialize
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ std::vector<Ciphertext> Encryptor::EncryptImpl(
int idx = 0;
for (auto item : pts) {
YACL_ENFORCE(
item->CompareAbs(pub_key_.PlaintextBound()) < 0,
item->CompareAbs(pub_key_.PlaintextBound()) <= 0,
"{} th msg number out of range, msg in hex={}, max in dec(abs)={}", idx,
item->ToHexString(), pub_key_.PlaintextBound());
idx++;
Expand Down
2 changes: 1 addition & 1 deletion heu/library/algorithms/paillier_float/internal/codec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace heu::lib::algorithms::paillier_f::internal {
const MPInt Codec::kBaseCache = MPInt(Codec::kBase);

EncodedNumber Codec::Encode(const MPInt& scalar, int exponent) const {
YACL_ENFORCE(scalar.CompareAbs(pk_.PlaintextBound()) < 0,
YACL_ENFORCE(scalar.CompareAbs(pk_.PlaintextBound()) <= 0,
"integer scalar should in +/- {}, but get {}",
pk_.PlaintextBound().ToHexString(), scalar.ToHexString());

Expand Down
4 changes: 2 additions & 2 deletions heu/library/algorithms/paillier_float/public_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class PublicKey : public HeObject<PublicKey> {
public:
PublicKey() = default;

// Valid plaintext range: (max_int_, -max_int_)
// Valid plaintext range: [max_int_, -max_int_]
[[nodiscard]] const MPInt& PlaintextBound() const& { return max_int_; }

[[nodiscard]] std::string ToString() const override;
Expand Down Expand Up @@ -75,7 +75,7 @@ class PublicKey : public HeObject<PublicKey> {
// Maximum int that may safely be stored. This can be increased, if you are
// happy to redefine "safely" and lower the chance of detecting an integer
// overflow.
// Bound: (max_int_, -max_int_)
// Bound: [max_int_, -max_int_]
MPInt max_int_; // n_ / 3
};

Expand Down
Loading

0 comments on commit 4adb390

Please sign in to comment.