Skip to content

Commit

Permalink
Add linters (#82)
Browse files Browse the repository at this point in the history
* Add linter

* format python code

* format c++ code
  • Loading branch information
anakinxc authored Aug 7, 2023
1 parent 0eb70fa commit e8f8424
Show file tree
Hide file tree
Showing 45 changed files with 294 additions and 58 deletions.
17 changes: 17 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ parameters:
type: boolean
default: false

jobs:
lint:
# Use machine executor for convenient data sharing between the host and docker-compose cluster
docker:
- image: secretflow/lint-ci:latest
resource_class: medium
shell: /bin/bash --login -eo pipefail
steps:
- checkout
- run:
name: Checkout devtools
command: git clone https://github.com/secretflow/devtools.git ../devtools
- run:
name: Whitespace check
command: bash ../devtools/lint-whitespace.sh

workflows:
unittest-workflow:
when:
Expand All @@ -52,6 +68,7 @@ workflows:
third_party/.* build-and-run true
.bazelrc build-and-run true
requirements.txt build-and-run true
- lint
publish-workflow:
when:
equal: [ "release", << pipeline.parameters.GHA_Event >> ]
Expand Down
13 changes: 13 additions & 0 deletions .github/workflows/black.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Python Lint

on: [push, pull_request]

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: psf/black@stable
with:
jupyter: true
version: "23.1"
13 changes: 13 additions & 0 deletions .github/workflows/clang-format-linter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Run clang-format Linter

on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: DoozyX/[email protected]
with:
extensions: 'h,cc'
clangFormatVersion: 16.0.3
1 change: 1 addition & 0 deletions heu/experimental/gemini-rlwe/a2h_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ bool transform_to_ntt_inplace(RLWEPt& pt, const seal::SEALContext& context) {
class A2HTest : public ::testing::TestWithParam<size_t> {
public:
static constexpr size_t poly_deg = 4096;

void SetUp() override {
rdv_.seed(std::time(0));
auto scheme_type = seal::scheme_type::ckks;
Expand Down
3 changes: 3 additions & 0 deletions heu/experimental/gemini-rlwe/lwe_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class LWECt {
*/
size_t save_size(seal::compr_mode_type compr_mode =
seal::Serialization::compr_mode_default) const;

/**
Saves the LWECt to a given memory location. The output is in binary
format and not human-readable.
Expand All @@ -114,12 +115,14 @@ class LWECt {
save_size(seal::compr_mode_type::none), buffer,
size, compr_mode, false);
}

/**
Loads an LWECt from an input stream overwriting the current LWECt.
The loaded ciphertext is verified to be valid for the given SEALContext.
*/
void load(const seal::SEALContext &context, const seal::seal_byte *buffer,
size_t size);

/**
Loads an LWECt from a given memory location overwriting the current
plaintext. No checking of the validity of the ciphertext data against
Expand Down
1 change: 1 addition & 0 deletions heu/experimental/gemini-rlwe/matvec_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class MatVecTest : public ::testing::TestWithParam<
std::tuple<size_t, std::tuple<size_t, size_t>>> {
protected:
static constexpr size_t poly_deg = 4096;

void SetUp() override {
rdv_.seed(std::time(0));
auto scheme_type = seal::scheme_type::ckks;
Expand Down
1 change: 1 addition & 0 deletions heu/experimental/gemini-rlwe/rlwe_2_lwe_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace heu::expt::rlwe::test {
class RLWE2LWETest : public testing::Test {
protected:
static constexpr size_t poly_deg = 4096;

void SetUp() override {
rdv_.seed(std::time(0));
auto scheme_type = seal::scheme_type::ckks;
Expand Down
1 change: 1 addition & 0 deletions heu/library/algorithms/clean_template/mix_spi_demo.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Encryptor {
Ciphertext Encrypt(const Plaintext& m) const {
YACL_THROW("To be implemented");
}

std::vector<Ciphertext> Encrypt(ConstSpan<Plaintext> pts) const {
YACL_THROW("To be implemented");
}
Expand Down
1 change: 1 addition & 0 deletions heu/library/algorithms/elgamal/public_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace heu::lib::algorithms::elgamal {
class PublicKey {
public:
PublicKey() {}

PublicKey(const std::shared_ptr<yacl::crypto::EcGroup> &curve,
const yacl::crypto::EcPoint &h)
: curve_(curve), h_(h) {}
Expand Down
2 changes: 1 addition & 1 deletion heu/library/algorithms/elgamal/secret_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ class SecretKey {
std::shared_ptr<LookupTable> table_;
};

} // namespace heu::lib::algorithms::elgamal
} // namespace heu::lib::algorithms::elgamal
3 changes: 3 additions & 0 deletions heu/library/algorithms/mock/ciphertext.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,20 @@ class Ciphertext : public HeObject<Ciphertext> {
public:
// [SPI: Critical]
Ciphertext() = default;

explicit Ciphertext(const MPInt &c) : bn_(c) {}

// [SPI: Critical]
[[nodiscard]] std::string ToString() const override { return bn_.ToString(); }

// [SPI: Important]
friend std::ostream &operator<<(std::ostream &os, const Ciphertext &c) {
return os << c.ToString();
}

// [SPI: Important]
bool operator==(const Ciphertext &other) const { return bn_ == other.bn_; }

// [SPI: Important]
bool operator!=(const Ciphertext &other) const {
return !this->operator==(other);
Expand Down
1 change: 1 addition & 0 deletions heu/library/algorithms/mock/evaluator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ void CheckRange(const PublicKey& pk, const Plaintext& p, const Ciphertext&) {
}

void CheckRange(const PublicKey& pk, const Ciphertext&, const Ciphertext&) {}

void CheckRange(const PublicKey& pk, const Plaintext&, const Plaintext&) {}

#define SCALAR_FUNCTION_IMPL(NAME, RET, T1, OP, T2) \
Expand Down
21 changes: 19 additions & 2 deletions heu/library/algorithms/mock/plaintext.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class Plaintext {

// [SPI: Critical]
[[nodiscard]] std::string ToString() const { return bn_.ToString(); }

// [SPI: Important]
friend std::ostream &operator<<(std::ostream &os, const Plaintext &pt) {
return os << pt.ToString();
Expand All @@ -84,43 +85,52 @@ class Plaintext {
size_t BitCount() const { return bn_.BitCount(); } // [SPI: Critical]

Plaintext operator-() const { return Wrap(-bn_); } // [SPI: Critical]
void NegateInplace() { bn_.NegateInplace(); } // [SPI: Critical]

void NegateInplace() { bn_.NegateInplace(); } // [SPI: Critical]

// [SPI: Critical]
Plaintext operator+(const Plaintext &op2) const {
return Wrap(bn_ + op2.bn_);
}

// [SPI: Critical]
Plaintext operator-(const Plaintext &op2) const {
return Wrap(bn_ - op2.bn_);
}

// [SPI: Critical]
Plaintext operator*(const Plaintext &op2) const {
return Wrap(bn_ * op2.bn_);
}

// [SPI: Important]
Plaintext operator/(const Plaintext &op2) const {
return Wrap(bn_ / op2.bn_);
}

// [SPI: Important]
Plaintext operator%(const Plaintext &op2) const {
return Wrap(bn_ % op2.bn_);
}

// [SPI: Important]
Plaintext operator&(const Plaintext &op2) const {
return Wrap(bn_ & op2.bn_);
}

// [SPI: Important]
Plaintext operator|(const Plaintext &op2) const {
return Wrap(bn_ | op2.bn_);
}

// [SPI: Important]
Plaintext operator^(const Plaintext &op2) const {
return Wrap(bn_ ^ op2.bn_);
}

// [SPI: Important]
Plaintext operator<<(size_t op2) const { return Wrap(bn_ << op2); }

// [SPI: Important]
Plaintext operator>>(size_t op2) const { return Wrap(bn_ >> op2); }

Expand Down Expand Up @@ -186,19 +196,26 @@ class Plaintext {

// [SPI: Important]
bool operator>(const Plaintext &other) const { return bn_ > other.bn_; }

// [SPI: Important]
bool operator<(const Plaintext &other) const { return bn_ < other.bn_; }

// [SPI: Important]
bool operator>=(const Plaintext &other) const { return bn_ >= other.bn_; }

// [SPI: Important]
bool operator<=(const Plaintext &other) const { return bn_ <= other.bn_; }

// [SPI: Important]
bool operator==(const Plaintext &other) const { return bn_ == other.bn_; }

// [SPI: Important]
bool operator!=(const Plaintext &other) const { return bn_ != other.bn_; }

bool IsZero() const { return bn_.IsZero(); } // [SPI: Critical]
bool IsZero() const { return bn_.IsZero(); } // [SPI: Critical]

bool IsPositive() const { return bn_.IsPositive(); } // [SPI: Important]

bool IsNegative() const { return bn_.IsNegative(); } // [SPI: Important]

MSGPACK_DEFINE(bn_);
Expand Down
2 changes: 2 additions & 0 deletions heu/library/algorithms/ou/ciphertext.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ using Plaintext = MPInt;
class Ciphertext : public HeObject<Ciphertext> {
public:
Ciphertext() = default;

explicit Ciphertext(MPInt c) : c_(std::move(c)) {}

[[nodiscard]] std::string ToString() const override { return c_.ToString(); }

bool operator==(const Ciphertext& other) const { return c_ == other.c_; }

bool operator!=(const Ciphertext& other) const {
return !this->operator==(other);
}
Expand Down
1 change: 1 addition & 0 deletions heu/library/algorithms/ou/encryptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Encryptor {
std::pair<Ciphertext, std::string> EncryptWithAudit(const MPInt& m) const;

MPInt GetHr() const;

void SetEnableCache(bool enable_cache) { enable_cache_ = enable_cache; }

private:
Expand Down
8 changes: 8 additions & 0 deletions heu/library/algorithms/ou/evaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,29 @@ class Evaluator {
// encoding form
Ciphertext Add(const Ciphertext& a, const Ciphertext& b) const;
Ciphertext Add(const Ciphertext& a, const Plaintext& b) const;

Plaintext Add(const Plaintext& a, const Plaintext& b) const { return a + b; };

Ciphertext Add(const Plaintext& a, const Ciphertext& b) const {
return Add(b, a);
}

void AddInplace(Ciphertext* a, const Ciphertext& b) const;
void AddInplace(Ciphertext* a, const Plaintext& p) const;

void AddInplace(Plaintext* a, const Plaintext& b) const { *a += b; }

// out = a - b
// Warning: Subtraction is not supported if a, b are in batch encoding
Ciphertext Sub(const Ciphertext& a, const Ciphertext& b) const;
Ciphertext Sub(const Ciphertext& a, const Plaintext& b) const;
Ciphertext Sub(const Plaintext& a, const Ciphertext& b) const;

Plaintext Sub(const Plaintext& a, const Plaintext& b) const { return a - b; };

void SubInplace(Ciphertext* a, const Ciphertext& b) const;
void SubInplace(Ciphertext* a, const Plaintext& p) const;

void SubInplace(Plaintext* a, const Plaintext& b) const { *a -= b; }

// out = a * p
Expand All @@ -61,12 +66,15 @@ class Evaluator {
// Warning 2:
// Multiplication is not supported if a is in batch encoding form
Ciphertext Mul(const Ciphertext& a, const MPInt& p) const;

Plaintext Mul(const Plaintext& a, const Plaintext& b) const { return a * b; };

Ciphertext Mul(const Plaintext& a, const Ciphertext& b) const {
return Mul(b, a);
}

void MulInplace(Ciphertext* a, const MPInt& p) const;

void MulInplace(Plaintext* a, const Plaintext& b) const { *a *= b; };

// out = -a
Expand Down
1 change: 1 addition & 0 deletions heu/library/algorithms/paillier_float/ciphertext.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Ciphertext : public HeObject<Ciphertext> {
: c_(std::move(c)), exponent_(exponent) {}

bool operator==(const Ciphertext& other) const { return c_ == other.c_; }

bool operator!=(const Ciphertext& other) const {
return !this->operator==(other);
}
Expand Down
4 changes: 4 additions & 0 deletions heu/library/algorithms/paillier_float/evaluator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Ciphertext Evaluator::Add(const Ciphertext& a, const MPInt& b) const {
Ciphertext cipher_b = encryptor_.EncryptEncoded(encoded_b, 1);
return Add(a, cipher_b);
}

Ciphertext Evaluator::Add(const Ciphertext& a, double b) const {
internal::EncodedNumber encoded_b = internal::Codec(pk_).Encode(b);

Expand All @@ -58,9 +59,11 @@ Ciphertext Evaluator::Add(const Ciphertext& a, double b) const {
void Evaluator::AddInplace(Ciphertext* a, const Ciphertext& b) const {
*a = Add(*a, b);
}

void Evaluator::AddInplace(Ciphertext* a, const MPInt& b) const {
*a = Add(*a, b);
}

void Evaluator::AddInplace(Ciphertext* a, double b) const { *a = Add(*a, b); }

MPInt Evaluator::AddRaw(const MPInt& a, const MPInt& b) const {
Expand Down Expand Up @@ -112,6 +115,7 @@ Ciphertext Evaluator::Mul(const Ciphertext& a, double b) const {
void Evaluator::MulInplace(Ciphertext* a, const MPInt& b) const {
*a = Mul(*a, b);
}

void Evaluator::MulInplace(Ciphertext* a, double b) const { *a = Mul(*a, b); }

Ciphertext Evaluator::Negate(const Ciphertext& a) const {
Expand Down
9 changes: 9 additions & 0 deletions heu/library/algorithms/paillier_float/evaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ class Evaluator {
Ciphertext Add(const Ciphertext& a, const Ciphertext& b) const;
Ciphertext Add(const Ciphertext& a, const MPInt& b) const;
Ciphertext Add(const Ciphertext& a, double b) const;

Plaintext Add(const Plaintext& a, const Plaintext& b) const { return a + b; };

Ciphertext Add(const Plaintext& a, const Ciphertext& b) const {
return Add(b, a);
}
Expand All @@ -39,29 +41,36 @@ class Evaluator {
void AddInplace(Ciphertext* a, const Ciphertext& b) const;
void AddInplace(Ciphertext* a, const MPInt& b) const;
void AddInplace(Ciphertext* a, double b) const;

void AddInplace(Plaintext* a, const Plaintext& b) const { *a += b; }

// out = a - b
Ciphertext Sub(const Ciphertext& a, const Ciphertext& b) const;
Ciphertext Sub(const Ciphertext& a, const MPInt& b) const;
Ciphertext Sub(const MPInt& a, const Ciphertext& b) const;

Plaintext Sub(const Plaintext& a, const Plaintext& b) const { return a - b; };

// a -= b
void SubInplace(Ciphertext* a, const Ciphertext& b) const;
void SubInplace(Ciphertext* a, const MPInt& b) const;

void SubInplace(Plaintext* a, const Plaintext& b) const { *a -= b; }

// c = a * b
Ciphertext Mul(const Ciphertext& a, const MPInt& b) const;
Ciphertext Mul(const Ciphertext& a, double b) const;

Plaintext Mul(const Plaintext& a, const Plaintext& b) const { return a * b; };

Ciphertext Mul(const Plaintext& a, const Ciphertext& b) const {
return Mul(b, a);
}

// a = a * b
void MulInplace(Ciphertext* a, const MPInt& b) const;
void MulInplace(Ciphertext* a, double b) const;

void MulInplace(Plaintext* a, const Plaintext& b) const { *a *= b; };

// b = -a
Expand Down
Loading

0 comments on commit e8f8424

Please sign in to comment.