From 3462066e4aa00de6be5771055ac67bb196f7b222 Mon Sep 17 00:00:00 2001 From: Alec-xdu <1767162258@qq.com> Date: Fri, 16 Jan 2026 20:42:39 +0800 Subject: [PATCH 1/5] add ashe --- heu/library/algorithms/ashe/BUILD.bazel | 113 +++++++++++ heu/library/algorithms/ashe/ashe.h | 22 +++ heu/library/algorithms/ashe/ashe_tests.cc | 183 ++++++++++++++++++ heu/library/algorithms/ashe/ciphertext.h | 50 +++++ heu/library/algorithms/ashe/decryptor.cc | 26 +++ heu/library/algorithms/ashe/decryptor.h | 45 +++++ heu/library/algorithms/ashe/encryptor.cc | 54 ++++++ heu/library/algorithms/ashe/encryptor.h | 46 +++++ heu/library/algorithms/ashe/evaluator.cc | 115 +++++++++++ heu/library/algorithms/ashe/evaluator.h | 65 +++++++ heu/library/algorithms/ashe/key_generator.cc | 58 ++++++ heu/library/algorithms/ashe/key_generator.h | 32 +++ .../algorithms/ashe/public_parameters.cc | 35 ++++ .../algorithms/ashe/public_parameters.h | 81 ++++++++ heu/library/algorithms/ashe/secret_key.h | 49 +++++ 15 files changed, 974 insertions(+) create mode 100644 heu/library/algorithms/ashe/BUILD.bazel create mode 100644 heu/library/algorithms/ashe/ashe.h create mode 100644 heu/library/algorithms/ashe/ashe_tests.cc create mode 100644 heu/library/algorithms/ashe/ciphertext.h create mode 100644 heu/library/algorithms/ashe/decryptor.cc create mode 100644 heu/library/algorithms/ashe/decryptor.h create mode 100644 heu/library/algorithms/ashe/encryptor.cc create mode 100644 heu/library/algorithms/ashe/encryptor.h create mode 100644 heu/library/algorithms/ashe/evaluator.cc create mode 100644 heu/library/algorithms/ashe/evaluator.h create mode 100644 heu/library/algorithms/ashe/key_generator.cc create mode 100644 heu/library/algorithms/ashe/key_generator.h create mode 100644 heu/library/algorithms/ashe/public_parameters.cc create mode 100644 heu/library/algorithms/ashe/public_parameters.h create mode 100644 heu/library/algorithms/ashe/secret_key.h diff --git a/heu/library/algorithms/ashe/BUILD.bazel b/heu/library/algorithms/ashe/BUILD.bazel new file mode 100644 index 0000000..c2f39d4 --- /dev/null +++ b/heu/library/algorithms/ashe/BUILD.bazel @@ -0,0 +1,113 @@ +# Copyright 2024 Ant Group Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +load("@yacl//bazel:yacl.bzl", "yacl_cc_library", "yacl_cc_test") + +package(default_visibility = ["//visibility:public"]) + +test_suite( + name = "ashe_tests", +) + +yacl_cc_library( + name = "ashe", + hdrs = [ + "ashe.h", + ], + deps = [ + ":decryptor", + ":encryptor", + ":evaluator", + ":key_generator", + ], +) + +yacl_cc_library( + name = "ciphertext", + hdrs = ["ciphertext.h"], + deps = [ + "//heu/library/algorithms/util", + "@msgpack-c//:msgpack", + ], +) + +yacl_cc_library( + name = "secret_key", + hdrs = ["secret_key.h"], + deps = [ + "//heu/library/algorithms/util", + "@msgpack-c//:msgpack", + ], +) + +yacl_cc_library( + name = "public_parameters", + srcs = ["public_parameters.cc"], + hdrs = ["public_parameters.h"], + deps = [ + "//heu/library/algorithms/util", + "@msgpack-c//:msgpack", + ], +) + +yacl_cc_library( + name = "key_generator", + srcs = ["key_generator.cc"], + hdrs = ["key_generator.h"], + deps = [ + ":public_parameters", + ":secret_key", + ":encryptor" + ], +) + +yacl_cc_library( + name = "encryptor", + srcs = ["encryptor.cc"], + hdrs = ["encryptor.h"], + deps = [ + ":ciphertext", + ":public_parameters", + ":secret_key" + ], +) + +yacl_cc_library( + name = "decryptor", + srcs = ["decryptor.cc"], + hdrs = ["decryptor.h"], + deps = [ + ":ciphertext", + ":public_parameters", + ":secret_key", + ], +) + +yacl_cc_library( + name = "evaluator", + srcs = ["evaluator.cc"], + hdrs = ["evaluator.h"], + deps = [ + ":ciphertext", + ":public_parameters", + ], +) + +yacl_cc_test( + name = "ashe_test", + srcs = ["ashe_tests.cc"], + deps = [ + ":ashe" + ] +) \ No newline at end of file diff --git a/heu/library/algorithms/ashe/ashe.h b/heu/library/algorithms/ashe/ashe.h new file mode 100644 index 0000000..052b6b5 --- /dev/null +++ b/heu/library/algorithms/ashe/ashe.h @@ -0,0 +1,22 @@ +// Copyright 2022 Ant Group Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include "heu/library/algorithms/ashe/decryptor.h" +#include "heu/library/algorithms/ashe/encryptor.h" +#include "heu/library/algorithms/ashe/evaluator.h" +#include "heu/library/algorithms/ashe/key_generator.h" +#include "heu/library/algorithms/ashe/public_parameters.h" +#include "heu/library/algorithms/ashe/secret_key.h" diff --git a/heu/library/algorithms/ashe/ashe_tests.cc b/heu/library/algorithms/ashe/ashe_tests.cc new file mode 100644 index 0000000..577ebca --- /dev/null +++ b/heu/library/algorithms/ashe/ashe_tests.cc @@ -0,0 +1,183 @@ +// Copyright 2022 Ant Group Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "heu/library/algorithms/ashe/ashe.h" + +#include + +#include "gtest/gtest.h" + +namespace heu::lib::algorithms::ashe::test { + +class asheTest : public testing::Test { + protected: + static void SetUpTestSuite() { KeyGenerator::Generate(4096, &sk_, &pp_); } + + static SecretKey sk_; + static PublicParameters pp_; +}; +SecretKey asheTest::sk_; +PublicParameters asheTest::pp_; + +TEST_F(asheTest, SerializeTest) { + auto pp_buffer = pp_.Serialize(); + PublicParameters pp2; + pp2.Deserialize(pp_buffer); + ASSERT_EQ(pp_.k_r1, pp2.k_r1); + ASSERT_EQ(pp_.k_r2, pp2.k_r2); + ASSERT_EQ(pp_.k_q, pp2.k_q); + ASSERT_EQ(pp_.k_p, pp2.k_p); + ASSERT_EQ(pp_.randomZeros, pp2.randomZeros); + + auto sk_buffer = sk_.Serialize(); + SecretKey sk2; + sk2.Deserialize(sk_buffer); + ASSERT_EQ(sk_.p_, sk2.p_); + ASSERT_EQ(sk_.q_, sk2.q_); + + Encryptor encryptor(pp2, sk2); + + Evaluator evaluator(pp2); + + BigInt m0(-12345); + Ciphertext ct = encryptor.Encrypt(m0); + + BigInt dc; + Decryptor decryptor(pp_, sk_); + decryptor.Decrypt(ct, &dc); + EXPECT_EQ(dc, m0); +} + +TEST_F(asheTest, OperationEvaluate) { + Encryptor encryptor_(pp_, sk_); + Evaluator evaluator_(pp_); + Decryptor decryptor_(pp_, sk_); + + Plaintext m0 = Plaintext(12345); + Plaintext m1 = Plaintext(-20000); + Plaintext m3 = Plaintext(0); + Ciphertext c0 = encryptor_.Encrypt(m0); + Ciphertext c1 = encryptor_.Encrypt(m1); + Ciphertext c2 = encryptor_.Encrypt(-m0); + Ciphertext c3 = encryptor_.Encrypt(m3); + EXPECT_EQ(m0, Plaintext(12345)); + + Plaintext plain; + Ciphertext res; + + // evaluate add + res = evaluator_.Add(c0, c0); + decryptor_.Decrypt(res, &plain); + EXPECT_EQ(plain, Plaintext(12345 * 2)); + res = evaluator_.Add(c1, c1); + decryptor_.Decrypt(res, &plain); + EXPECT_EQ(plain, Plaintext(-20000 * 2)); + res = evaluator_.Add(c0, c1); + decryptor_.Decrypt(res, &plain); + EXPECT_EQ(plain, Plaintext(12345 - 20000)); + res = evaluator_.Add(c1, m3); + decryptor_.Decrypt(res, &plain); + EXPECT_EQ(plain, Plaintext(-20000)); + res = evaluator_.Add(c0, m1); + decryptor_.Decrypt(res, &plain); + EXPECT_EQ(plain, Plaintext(12345 - 20000)); + res = evaluator_.Add(c1, m0); + decryptor_.Decrypt(res, &plain); + EXPECT_EQ(plain, Plaintext(12345 - 20000)); + res = evaluator_.Add(c2, c0); + decryptor_.Decrypt(res, &plain); + EXPECT_EQ(plain, Plaintext(0)); + + res = evaluator_.Mul(c0, m0); + decryptor_.Decrypt(res, &plain); + EXPECT_EQ(plain, Plaintext(12345 * 12345)); + res = evaluator_.Mul(c1, m0); + decryptor_.Decrypt(res, &plain); + // EXPECT_EQ(plain, Plaintext(-20000 * 12345)); + // res = evaluator_.Mul(c1, m1); + // decryptor_.Decrypt(res, &plain); + // EXPECT_EQ(plain, Plaintext(20000 * 20000)); + + Ciphertext Zero = encryptor_.EncryptZero(); + decryptor_.Decrypt(Zero, &plain); + EXPECT_EQ(plain, BigInt(0)); + decryptor_.Decrypt(c1, &plain); + EXPECT_EQ(plain, BigInt(-20000)); + + // evaluator_.MulInplace(&c0, m1); + // decryptor_.Decrypt(c0, &plain); + // EXPECT_EQ(plain, BigInt(-20000 * 12345)); + + Plaintext pt0 = Plaintext(12345); + Plaintext pt1 = Plaintext(20000); + Ciphertext ct0 = encryptor_.Encrypt(pt0); + Ciphertext ct1 = encryptor_.Encrypt(pt1); + evaluator_.AddInplace(&ct0, pt1); // call add, test Inplace function + decryptor_.Decrypt(ct0, &plain); + EXPECT_EQ(plain, BigInt(20000 + 12345)); + evaluator_.AddInplace(&ct0, ct1); + decryptor_.Decrypt(ct0, &plain); + EXPECT_EQ(plain, BigInt(20000 + 12345 + 20000)); + evaluator_.Randomize(&ct0); + decryptor_.Decrypt(ct0, &plain); + EXPECT_EQ(plain, BigInt(20000 + 12345 + 20000)); + // m < pp_.MessageSpace()[1] && m >= pp_.MessageSpace()[0] + Plaintext pt_min = Plaintext(pp_.MessageSpace().first); + Plaintext pt_max = Plaintext(pp_.MessageSpace().second - BigInt(1)); + Ciphertext ct_max = encryptor_.Encrypt(pt_max); + Ciphertext ct_min = encryptor_.Encrypt(pt_min); + Plaintext tmp = decryptor_.Decrypt(ct_min); + EXPECT_EQ(tmp, pt_min); + tmp = decryptor_.Decrypt(ct_max); + EXPECT_EQ(tmp, pt_max); + +} + +TEST_F(asheTest, NegateEvalutate) { + Encryptor encryptor_(pp_, sk_); + Evaluator evaluator_(pp_); + Decryptor decryptor_(pp_, sk_); + Plaintext p = Plaintext(123456); + +} + +TEST_F(asheTest, RuntimeEfficientTest) { + Encryptor encryptor_(pp_, sk_); + Evaluator evaluator_(pp_); + Decryptor decryptor_(pp_, sk_); + Ciphertext c1, c2; + std::chrono::time_point t1, t2; + t1 = std::chrono::high_resolution_clock::now(); + for (int i = 0 ; i < 10000 ; i++) { + c1 = encryptor_.Encrypt(BigInt(123456)); + } + t2 = std::chrono::high_resolution_clock::now(); + auto duration = std::chrono::duration_cast(t2 - t1); + std:: cout << "encrypt 1w times used " << duration.count() << std::endl; + t1 = std::chrono::high_resolution_clock::now(); + for (int i = 0 ; i < 10000 ; i++) { + c2 = evaluator_.Add(c1, c1); + } + t2 = std::chrono::high_resolution_clock::now(); + duration = std::chrono::duration_cast(t2 - t1); + std:: cout << "add 1w times used " << duration.count() << std::endl; + t1 = std::chrono::high_resolution_clock::now(); + for (int i = 0 ; i < 10000 ; i++) { + Plaintext m = decryptor_.Decrypt(c2); + } + t2 = std::chrono::high_resolution_clock::now(); + duration = std::chrono::duration_cast(t2 - t1); + std:: cout << "decrypt 1w times used " << duration.count() << std::endl; +} +} // namespace heu::lib::algorithms::ou::test diff --git a/heu/library/algorithms/ashe/ciphertext.h b/heu/library/algorithms/ashe/ciphertext.h new file mode 100644 index 0000000..0bb55d2 --- /dev/null +++ b/heu/library/algorithms/ashe/ciphertext.h @@ -0,0 +1,50 @@ +// Copyright 2022 Ant Group Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include + +#include "heu/library/algorithms/util/big_int.h" +#include "heu/library/algorithms/util/he_object.h" + +namespace heu::lib::algorithms::ashe { + +using Plaintext = BigInt; + +class Ciphertext : public HeObject { + public: + + Ciphertext() = default; + + explicit Ciphertext(BigInt n) : n_(std::move(n)) {} + + [[nodiscard]] std::string ToString() const override { + return fmt::format("CT: {}", n_); + } + + bool operator==(const Ciphertext &other) const { + return n_ == other.n_; + } + + bool operator!=(const Ciphertext &other) const { + return !this->operator==(other); + } + + MSGPACK_DEFINE(n_); + + BigInt n_; +}; + +} // namespace heu::lib::algorithms::ashe diff --git a/heu/library/algorithms/ashe/decryptor.cc b/heu/library/algorithms/ashe/decryptor.cc new file mode 100644 index 0000000..63f360f --- /dev/null +++ b/heu/library/algorithms/ashe/decryptor.cc @@ -0,0 +1,26 @@ +// Copyright 2022 Ant Group Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "heu/library/algorithms/ashe/decryptor.h" + +namespace heu::lib::algorithms::ashe { +void Decryptor::Decrypt(const Ciphertext &ct, Plaintext *out) const { + *out = Decrypt(ct); +} + +Plaintext Decryptor::Decrypt(const Ciphertext &ct) const { + BigInt tmp = ct.n_.AddMod(ZERO, p).AddMod(ZERO, q).AddMod(ZERO, MAX); + return tmp <= half ? tmp : tmp - MAX; +} +} // namespace heu::lib::algorithms::ashe diff --git a/heu/library/algorithms/ashe/decryptor.h b/heu/library/algorithms/ashe/decryptor.h new file mode 100644 index 0000000..7de265a --- /dev/null +++ b/heu/library/algorithms/ashe/decryptor.h @@ -0,0 +1,45 @@ +// Copyright 2022 Ant Group Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include + +#include "heu/library/algorithms/ashe/ciphertext.h" +#include "heu/library/algorithms/ashe/public_parameters.h" +#include "heu/library/algorithms/ashe/secret_key.h" + +namespace heu::lib::algorithms::ashe { +class Decryptor { +public: + explicit Decryptor(PublicParameters pp, SecretKey sk) + : pp_(std::move(pp)), sk_(std::move(sk)) { + p = sk_.p_; + q = sk_.q_; + } + + void Decrypt(const Ciphertext &ct, Plaintext *out) const; + + [[nodiscard]] Plaintext Decrypt(const Ciphertext &ct) const; + +private: + PublicParameters pp_; + SecretKey sk_; + BigInt half = BigInt(UINT64_MAX) / BigInt(2); + BigInt MAX = BigInt(UINT64_MAX); + BigInt p; + BigInt q; + BigInt ZERO = BigInt(0); +}; +} // namespace heu::lib::algorithms::ashe diff --git a/heu/library/algorithms/ashe/encryptor.cc b/heu/library/algorithms/ashe/encryptor.cc new file mode 100644 index 0000000..05fc19f --- /dev/null +++ b/heu/library/algorithms/ashe/encryptor.cc @@ -0,0 +1,54 @@ +// Copyright 2022 Ant Group Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "heu/library/algorithms/ashe/encryptor.h" + +namespace heu::lib::algorithms::ashe { +Ciphertext Encryptor::EncryptZero() const { + return Encrypt(BigInt(0)); +} + +Ciphertext Encryptor::Encrypt(const Plaintext &m) const { + return EncryptImpl(m, nullptr); +} + +void Encryptor::Encrypt(const Plaintext &m, Ciphertext *out) const { + *out = Encrypt(m); +} + +std::pair Encryptor::EncryptWithAudit(const Plaintext &m) const { + std::string audit_out; + Ciphertext ct_out = EncryptImpl(m, &audit_out); + audit_out.append( + fmt::format("pt:{}\n ct:{}", m.ToString(), ct_out.n_.ToString())); + return std::make_pair(ct_out, audit_out); +} + +template +Ciphertext Encryptor::EncryptImpl(const Plaintext &m, std::string *audit_str) const { + YACL_ENFORCE(m <= pp_.MessageSpace().second && m >= pp_.MessageSpace().first, + "Plaintext {} is too large, cannot encrypt.", m); + BigInt r, r1; + BigInt::RandomExactBits(pp_.k_r1, &r); + BigInt::RandomExactBits(pp_.k_r2, &r1); + const BigInt m1 = r * sk_.p_ + r1 * sk_.q_ + m.AddMod(ZERO, MAX); + + if constexpr (audit) { + YACL_ENFORCE(audit_str != nullptr); + *audit_str = + fmt::format("r:{}\n r':{}\n", r.ToHexString(), r1.ToHexString()); + } + return Ciphertext(m1); +} +} // namespace heu::lib::algorithms::ashe diff --git a/heu/library/algorithms/ashe/encryptor.h b/heu/library/algorithms/ashe/encryptor.h new file mode 100644 index 0000000..5f3ac42 --- /dev/null +++ b/heu/library/algorithms/ashe/encryptor.h @@ -0,0 +1,46 @@ +// Copyright 2022 Ant Group Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include + +#include "heu/library/algorithms/ashe/ciphertext.h" +#include "heu/library/algorithms/ashe/public_parameters.h" +#include "heu/library/algorithms/ashe/secret_key.h" + +namespace heu::lib::algorithms::ashe { +class Encryptor { +public: + explicit Encryptor(PublicParameters pk, SecretKey sk) + : pp_(std::move(pk)), sk_(std::move(sk)) {} + + [[nodiscard]] Ciphertext EncryptZero() const; + [[nodiscard]] Ciphertext Encrypt(const Plaintext &m) const; + + void Encrypt(const Plaintext &m, Ciphertext *out) const; + + [[nodiscard]] std::pair EncryptWithAudit( + const Plaintext &m) const; + +private: + template + Ciphertext EncryptImpl(const Plaintext &m, + std::string *audit_str) const; + PublicParameters pp_; + SecretKey sk_; + BigInt ZERO = BigInt(0); + BigInt MAX = BigInt(UINT64_MAX); +}; +} // namespace heu::lib::algorithms::ashe diff --git a/heu/library/algorithms/ashe/evaluator.cc b/heu/library/algorithms/ashe/evaluator.cc new file mode 100644 index 0000000..ed91534 --- /dev/null +++ b/heu/library/algorithms/ashe/evaluator.cc @@ -0,0 +1,115 @@ +// Copyright 2022 Ant Group Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "heu/library/algorithms/ashe/evaluator.h" + +#include "fmt/ranges.h" + +namespace heu::lib::algorithms::ashe { +void Evaluator::Randomize(Ciphertext *ct) const { + BigInt r; + BigInt::RandomLtN(BigInt(pp_.randomZeros.size()), &r); + AddInplace(ct, Ciphertext(pp_.randomZeros[r.Get()])); +} + +Ciphertext Evaluator::Add(const Ciphertext &a, const Ciphertext &b) const { + return Ciphertext(a.n_ + b.n_); +} + +Ciphertext Evaluator::Add(const Ciphertext &a, const Plaintext &b) const { + return Ciphertext(a.n_ + b % MAX); +} + +Ciphertext Evaluator::Add(const Plaintext &a, const Ciphertext &b) const { + return Add(b, a); +} + +Plaintext Evaluator::Add(const Plaintext &a, const Plaintext &b) const { + return a + b; +} + +void Evaluator::AddInplace(Ciphertext *a, const Ciphertext &b) const { + *a = Add(*a, b); +} + +void Evaluator::AddInplace(Ciphertext *a, const Plaintext &b) const { + *a = Add(*a, b); +} + +void Evaluator::AddInplace(Plaintext *a, const Plaintext &b) const { + *a = Add(*a, b); +} + +Ciphertext Evaluator::Sub(const Ciphertext &a, const Ciphertext &b) const { + const Ciphertext b_ = Negate(b); + return Add(a, b_); +} + +Ciphertext Evaluator::Sub(const Ciphertext &a, const Plaintext &b) const { + return Add(a, -b); +} + +Ciphertext Evaluator::Sub(const Plaintext &a, const Ciphertext &b) const { + return Add(Negate(b), a); +} + +Plaintext Evaluator::Sub(const Plaintext &a, const Plaintext &b) const { + return a - b; +} + +void Evaluator::SubInplace(Ciphertext *a, const Ciphertext &b) const { + *a = Sub(*a, b); +} + +void Evaluator::SubInplace(Ciphertext *a, const Plaintext &p) const { + *a = Sub(*a, p); +} + +void Evaluator::SubInplace(Plaintext *a, const Plaintext &b) const { + *a = Sub(*a, b); +} + +Ciphertext Evaluator::Mul(const Ciphertext &a, const Plaintext &b) const { + YACL_ENFORCE(b % MAX <= BigInt(2).Pow(16), + "Plaintext {} is too large, cannot encrypt.", b); + Ciphertext res; + res.n_ = b.AddMod(ZERO, MAX) * a.n_; + return res; +} + +Ciphertext Evaluator::Mul(const Plaintext &a, const Ciphertext &b) const { + return Mul(b, a); +} + +Plaintext Evaluator::Mul(const Plaintext &a, const Plaintext &b) const { + return a * b; +} + +void Evaluator::MulInplace(Ciphertext *a, const Plaintext &b) const { + *a = Mul(*a, b); +} + +void Evaluator::MulInplace(Plaintext *a, const Plaintext &b) const { + *a = Mul(*a, b); +} + +Ciphertext Evaluator::Negate(const Ciphertext &a) const { + const BigInt neg = BigInt(-1) % MAX; + return Mul(a, neg); +} + +void Evaluator::NegateInplace(Ciphertext *a) const { + *a = Negate(*a); +} +} // namespace heu::lib::algorithms::ashe diff --git a/heu/library/algorithms/ashe/evaluator.h b/heu/library/algorithms/ashe/evaluator.h new file mode 100644 index 0000000..9670bb3 --- /dev/null +++ b/heu/library/algorithms/ashe/evaluator.h @@ -0,0 +1,65 @@ +// Copyright 2022 Ant Group Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include + +#include "heu/library/algorithms/ashe/ciphertext.h" +#include "heu/library/algorithms/ashe/public_parameters.h" + +namespace heu::lib::algorithms::ashe { +class Evaluator { +public: + explicit Evaluator(PublicParameters pp) : pp_(std::move(pp)) { + } + + void Randomize(Ciphertext *ct) const; + + [[nodiscard]] Ciphertext Add(const Ciphertext &a, const Ciphertext &b) const; + [[nodiscard]] Ciphertext Add(const Ciphertext &a, const Plaintext &b) const; + [[nodiscard]] Ciphertext Add(const Plaintext &a, const Ciphertext &b) const; + [[nodiscard]] Plaintext Add(const Plaintext &a, const Plaintext &b) const; + + void AddInplace(Ciphertext *a, const Ciphertext &b) const; + void AddInplace(Ciphertext *a, const Plaintext &b) const; + void AddInplace(Plaintext *a, const Plaintext &b) const; + + [[nodiscard]] Ciphertext Sub(const Ciphertext &a, const Ciphertext &b) const; + [[nodiscard]] Ciphertext Sub(const Ciphertext &a, const Plaintext &b) const; + [[nodiscard]] Ciphertext Sub(const Plaintext &a, const Ciphertext &b) const; + [[nodiscard]] Plaintext Sub(const Plaintext &a, const Plaintext &b) const; + + void SubInplace(Ciphertext *a, const Ciphertext &b) const; + void SubInplace(Ciphertext *a, const Plaintext &p) const; + void SubInplace(Plaintext *a, const Plaintext &b) const; + + [[nodiscard]] Ciphertext Mul(const Ciphertext &a, const Plaintext &b) const; + [[nodiscard]] Ciphertext Mul(const Plaintext &a, const Ciphertext &b) const; + [[nodiscard]] Plaintext Mul(const Plaintext &a, const Plaintext &b) const; + + void MulInplace(Ciphertext *a, const Plaintext &b) const; + void MulInplace(Plaintext *a, const Plaintext &b) const; + + [[nodiscard]] Ciphertext Negate(const Ciphertext &a) const; + void NegateInplace(Ciphertext *a) const; + +private: + PublicParameters pp_; + BigInt ONE = BigInt(1); + BigInt ZERO = BigInt(0); + BigInt MAX = BigInt(UINT64_MAX); + BigInt PlainSpace = BigInt(2).Pow(16); +}; +} // namespace heu::lib::algorithms::ashe diff --git a/heu/library/algorithms/ashe/key_generator.cc b/heu/library/algorithms/ashe/key_generator.cc new file mode 100644 index 0000000..cfacc9e --- /dev/null +++ b/heu/library/algorithms/ashe/key_generator.cc @@ -0,0 +1,58 @@ +// Copyright 2022 Ant Group Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "heu/library/algorithms/ashe/key_generator.h" + +#include + +namespace heu::lib::algorithms::ashe { +void KeyGenerator::Generate(int key_size, SecretKey *sk, PublicParameters *pk) { + int64_t k_r1, k_p, k_q, k_r2, k_m;; + if (key_size == 2048) { + k_r1 = 4384; + k_p = 1536; + k_q = 1008; + k_r2 = 512; + k_m = 64; + } else { + k_r1 = 8832; + k_p = 1536; + k_q = 992; + k_r2 = 512; + k_m = 64; + } + std::vector zeros; + BigInt p = BigInt::RandPrimeOver(k_p); + const BigInt q = BigInt::RandPrimeOver(k_q); + *sk = SecretKey(p, q); + + InitZeros(k_r1, k_p, k_q, k_r2, k_m, *sk, &zeros); + *pk = + PublicParameters(k_r1, k_p, k_q, k_r2, k_m, zeros); +} + +void KeyGenerator::Generate(SecretKey *sk, PublicParameters *pk) { + Generate(2048, sk, pk); +} + +void KeyGenerator::InitZeros(int64_t k_r1, int64_t k_p, int64_t k_q, + int64_t k_r2, int64_t k_m, SecretKey sk_, + std::vector *zeros) { + auto tmp = PublicParameters(k_r1, k_p, k_q, k_r2, k_m); + auto et = Encryptor(tmp, std::move(sk_)); + for (int i = 1; i <= 20; ++i) { + zeros->emplace_back(et.Encrypt(BigInt(0)).n_); + } +} +} // namespace heu::lib::algorithms::ashe diff --git a/heu/library/algorithms/ashe/key_generator.h b/heu/library/algorithms/ashe/key_generator.h new file mode 100644 index 0000000..54c8c5f --- /dev/null +++ b/heu/library/algorithms/ashe/key_generator.h @@ -0,0 +1,32 @@ +// Copyright 2022 Ant Group Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include "heu/library/algorithms/ashe/public_parameters.h" +#include "heu/library/algorithms/ashe/secret_key.h" +#include "heu/library/algorithms/ashe/encryptor.h" + +namespace heu::lib::algorithms::ashe { +class KeyGenerator { +public: + static void Generate(int key_size, SecretKey *sk, PublicParameters *pk); + static void Generate(SecretKey *sk, PublicParameters *pk); + +private: + static void InitZeros(int64_t k_r1, int64_t k_p, int64_t k_q, int64_t k_r2, + int64_t k_m, SecretKey sk_, + std::vector *zeros); +}; +} // namespace heu::lib::algorithms::ashe diff --git a/heu/library/algorithms/ashe/public_parameters.cc b/heu/library/algorithms/ashe/public_parameters.cc new file mode 100644 index 0000000..f81f85b --- /dev/null +++ b/heu/library/algorithms/ashe/public_parameters.cc @@ -0,0 +1,35 @@ +#include "heu/library/algorithms/ashe/public_parameters.h" + +namespace heu::lib::algorithms::ashe { +PublicParameters::PublicParameters(int64_t k_r1, int64_t k_p, int64_t k_q, + int64_t k_r2, int64_t k_m) { + this->k_r1 = k_r1; + this->k_p = k_p; + this->k_q = k_q; + this->k_r2 = k_r2; + this->k_m = k_m; + Init(); +} + +PublicParameters::PublicParameters(int64_t k_r1, int64_t k_p, int64_t k_q, + int64_t k_r2, int64_t k_m, + const std::vector &zeros) : + PublicParameters( + k_r1, k_p, k_q, k_r2, k_m) { + this->randomZeros = zeros; +} + +std::string PublicParameters::ToString() const { + return fmt::format( + "ashe PP: k_r1={}, k_p={}, k_q={}, " + "k_r2={}, k_m={}, randomZeros={}[size:{}]", + std::to_string(k_r1), std::to_string(k_p), std::to_string(k_q), + std::to_string(k_r2), std::to_string(k_m), ToHexString(randomZeros), + randomZeros.size()); +} + +void PublicParameters::Init() { + this->M[1] = BigInt(2).Pow(k_m - 1) - BigInt(1); + this->M[0] = -this->M[1]; +} +} // namespace heu::lib::algorithms::ashe diff --git a/heu/library/algorithms/ashe/public_parameters.h b/heu/library/algorithms/ashe/public_parameters.h new file mode 100644 index 0000000..b71f4ec --- /dev/null +++ b/heu/library/algorithms/ashe/public_parameters.h @@ -0,0 +1,81 @@ +// Copyright 2022 Ant Group Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include + +#include "heu/library/algorithms/util/big_int.h" +#include "heu/library/algorithms/util/he_object.h" + +namespace heu::lib::algorithms::ashe { +class PublicParameters : public HeObject { +private: + BigInt plaintextBound; + + static std::string ToHexString(const std::vector &vec) { + std::ostringstream oss; + oss << "["; + for (size_t i = 0; i < vec.size(); ++i) { + if (i != 0) { + oss << ", "; + } + oss << "0x" << vec[i].ToHexString(); + } + oss << "]"; + return oss.str(); + } + +public: + int64_t k_r1 = 4384; + int64_t k_p = 1536; + int64_t k_q = 1008; + int64_t k_r2 = 512; + int64_t k_m = 64; + std::vector randomZeros; + BigInt M[2]; + + + PublicParameters() = default; + + PublicParameters(int64_t k_r1, int64_t k_p, int64_t k_q, int64_t k_r2, + int64_t k_m); + + PublicParameters(int64_t k_r1, int64_t k_p, int64_t k_q, int64_t k_r2, + int64_t k_m, const std::vector &zeros); + + bool operator==(const PublicParameters &other) const { + return k_r1 == other.k_r1 && k_p == other.k_p && k_q == other.k_q && + k_r2 == other.k_r2 && k_m == other.k_m; + } + + bool operator!=(const PublicParameters &other) const { + return !this->operator==(other); + } + + [[nodiscard]] std::string ToString() const override; + + [[nodiscard]] const BigInt &PlaintextBound() const & { + return M[1]; + } + + void Init(); + + [[nodiscard]] std::pair MessageSpace() const { + return std::make_pair(M[0], M[1]); + } + + MSGPACK_DEFINE(k_r1, k_p, k_q, k_r2, k_m, M, randomZeros); +}; +} // namespace heu::lib::algorithms::ashe diff --git a/heu/library/algorithms/ashe/secret_key.h b/heu/library/algorithms/ashe/secret_key.h new file mode 100644 index 0000000..65dff8f --- /dev/null +++ b/heu/library/algorithms/ashe/secret_key.h @@ -0,0 +1,49 @@ +// Copyright 2022 Ant Group Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include + +#include "heu/library/algorithms/util/he_object.h" + +namespace heu::lib::algorithms::ashe { +class SecretKey : public HeObject { +public: + BigInt p_, q_; + + SecretKey(BigInt p, BigInt q) { + this->p_ = std::move(p); + this->q_ = std::move(q); + } + + SecretKey() = default; + + bool operator==(const SecretKey &other) const { + return p_ == other.p_ && q_ == other.q_; + } + + bool operator!=(const SecretKey &other) const { + return !this->operator==(other); + } + + [[nodiscard]] std::string ToString() const override { + return fmt::format("ashe SK, p={}[{}bits], q={}[{}bits]", + p_.ToHexString(), p_.BitCount(), + q_.ToHexString(), q_.BitCount()); + } + + MSGPACK_DEFINE(p_, q_); +}; +} // namespace heu::lib::algorithms::ashe From 4371c7c234746811ec036a309eec1d0b84dab4c0 Mon Sep 17 00:00:00 2001 From: Alec-xdu <1767162258@qq.com> Date: Fri, 16 Jan 2026 21:11:13 +0800 Subject: [PATCH 2/5] reformat --- heu/library/algorithms/ashe/BUILD.bazel | 2 +- heu/library/algorithms/ashe/ashe_tests.cc | 35 +++++++------------ heu/library/algorithms/ashe/ciphertext.h | 10 +++--- heu/library/algorithms/ashe/decryptor.cc | 2 +- heu/library/algorithms/ashe/decryptor.h | 8 ++--- heu/library/algorithms/ashe/encryptor.cc | 12 +++---- heu/library/algorithms/ashe/encryptor.h | 11 +++--- heu/library/algorithms/ashe/evaluator.cc | 6 ++-- heu/library/algorithms/ashe/evaluator.h | 9 +++-- heu/library/algorithms/ashe/key_generator.cc | 8 ++--- heu/library/algorithms/ashe/key_generator.h | 11 +++--- .../algorithms/ashe/public_parameters.cc | 7 ++-- .../algorithms/ashe/public_parameters.h | 11 +++--- heu/library/algorithms/ashe/secret_key.h | 9 +++-- 14 files changed, 60 insertions(+), 81 deletions(-) diff --git a/heu/library/algorithms/ashe/BUILD.bazel b/heu/library/algorithms/ashe/BUILD.bazel index c2f39d4..5eb85ee 100644 --- a/heu/library/algorithms/ashe/BUILD.bazel +++ b/heu/library/algorithms/ashe/BUILD.bazel @@ -110,4 +110,4 @@ yacl_cc_test( deps = [ ":ashe" ] -) \ No newline at end of file +) diff --git a/heu/library/algorithms/ashe/ashe_tests.cc b/heu/library/algorithms/ashe/ashe_tests.cc index 577ebca..5036569 100644 --- a/heu/library/algorithms/ashe/ashe_tests.cc +++ b/heu/library/algorithms/ashe/ashe_tests.cc @@ -12,12 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "heu/library/algorithms/ashe/ashe.h" - #include #include "gtest/gtest.h" +#include "heu/library/algorithms/ashe/ashe.h" + namespace heu::lib::algorithms::ashe::test { class asheTest : public testing::Test { @@ -27,6 +27,7 @@ class asheTest : public testing::Test { static SecretKey sk_; static PublicParameters pp_; }; + SecretKey asheTest::sk_; PublicParameters asheTest::pp_; @@ -104,10 +105,6 @@ TEST_F(asheTest, OperationEvaluate) { EXPECT_EQ(plain, Plaintext(12345 * 12345)); res = evaluator_.Mul(c1, m0); decryptor_.Decrypt(res, &plain); - // EXPECT_EQ(plain, Plaintext(-20000 * 12345)); - // res = evaluator_.Mul(c1, m1); - // decryptor_.Decrypt(res, &plain); - // EXPECT_EQ(plain, Plaintext(20000 * 20000)); Ciphertext Zero = encryptor_.EncryptZero(); decryptor_.Decrypt(Zero, &plain); @@ -115,15 +112,11 @@ TEST_F(asheTest, OperationEvaluate) { decryptor_.Decrypt(c1, &plain); EXPECT_EQ(plain, BigInt(-20000)); - // evaluator_.MulInplace(&c0, m1); - // decryptor_.Decrypt(c0, &plain); - // EXPECT_EQ(plain, BigInt(-20000 * 12345)); - Plaintext pt0 = Plaintext(12345); Plaintext pt1 = Plaintext(20000); Ciphertext ct0 = encryptor_.Encrypt(pt0); Ciphertext ct1 = encryptor_.Encrypt(pt1); - evaluator_.AddInplace(&ct0, pt1); // call add, test Inplace function + evaluator_.AddInplace(&ct0, pt1); decryptor_.Decrypt(ct0, &plain); EXPECT_EQ(plain, BigInt(20000 + 12345)); evaluator_.AddInplace(&ct0, ct1); @@ -132,7 +125,6 @@ TEST_F(asheTest, OperationEvaluate) { evaluator_.Randomize(&ct0); decryptor_.Decrypt(ct0, &plain); EXPECT_EQ(plain, BigInt(20000 + 12345 + 20000)); - // m < pp_.MessageSpace()[1] && m >= pp_.MessageSpace()[0] Plaintext pt_min = Plaintext(pp_.MessageSpace().first); Plaintext pt_max = Plaintext(pp_.MessageSpace().second - BigInt(1)); Ciphertext ct_max = encryptor_.Encrypt(pt_max); @@ -141,7 +133,6 @@ TEST_F(asheTest, OperationEvaluate) { EXPECT_EQ(tmp, pt_min); tmp = decryptor_.Decrypt(ct_max); EXPECT_EQ(tmp, pt_max); - } TEST_F(asheTest, NegateEvalutate) { @@ -149,7 +140,6 @@ TEST_F(asheTest, NegateEvalutate) { Evaluator evaluator_(pp_); Decryptor decryptor_(pp_, sk_); Plaintext p = Plaintext(123456); - } TEST_F(asheTest, RuntimeEfficientTest) { @@ -159,25 +149,26 @@ TEST_F(asheTest, RuntimeEfficientTest) { Ciphertext c1, c2; std::chrono::time_point t1, t2; t1 = std::chrono::high_resolution_clock::now(); - for (int i = 0 ; i < 10000 ; i++) { + for (int i = 0; i < 10000; i++) { c1 = encryptor_.Encrypt(BigInt(123456)); } t2 = std::chrono::high_resolution_clock::now(); - auto duration = std::chrono::duration_cast(t2 - t1); - std:: cout << "encrypt 1w times used " << duration.count() << std::endl; + auto duration = + std::chrono::duration_cast(t2 - t1); + std::cout << "encrypt 1w times used " << duration.count() << std::endl; t1 = std::chrono::high_resolution_clock::now(); - for (int i = 0 ; i < 10000 ; i++) { + for (int i = 0; i < 10000; i++) { c2 = evaluator_.Add(c1, c1); } t2 = std::chrono::high_resolution_clock::now(); duration = std::chrono::duration_cast(t2 - t1); - std:: cout << "add 1w times used " << duration.count() << std::endl; + std::cout << "add 1w times used " << duration.count() << std::endl; t1 = std::chrono::high_resolution_clock::now(); - for (int i = 0 ; i < 10000 ; i++) { + for (int i = 0; i < 10000; i++) { Plaintext m = decryptor_.Decrypt(c2); } t2 = std::chrono::high_resolution_clock::now(); duration = std::chrono::duration_cast(t2 - t1); - std:: cout << "decrypt 1w times used " << duration.count() << std::endl; + std::cout << "decrypt 1w times used " << duration.count() << std::endl; } -} // namespace heu::lib::algorithms::ou::test +} // namespace heu::lib::algorithms::ashe::test diff --git a/heu/library/algorithms/ashe/ciphertext.h b/heu/library/algorithms/ashe/ciphertext.h index 0bb55d2..356f789 100644 --- a/heu/library/algorithms/ashe/ciphertext.h +++ b/heu/library/algorithms/ashe/ciphertext.h @@ -20,15 +20,14 @@ #include "heu/library/algorithms/util/he_object.h" namespace heu::lib::algorithms::ashe { - using Plaintext = BigInt; class Ciphertext : public HeObject { - public: - +public: Ciphertext() = default; - explicit Ciphertext(BigInt n) : n_(std::move(n)) {} + explicit Ciphertext(BigInt n) : n_(std::move(n)) { + } [[nodiscard]] std::string ToString() const override { return fmt::format("CT: {}", n_); @@ -46,5 +45,4 @@ class Ciphertext : public HeObject { BigInt n_; }; - -} // namespace heu::lib::algorithms::ashe +} // namespace heu::lib::algorithms::ashe diff --git a/heu/library/algorithms/ashe/decryptor.cc b/heu/library/algorithms/ashe/decryptor.cc index 63f360f..4f17a64 100644 --- a/heu/library/algorithms/ashe/decryptor.cc +++ b/heu/library/algorithms/ashe/decryptor.cc @@ -23,4 +23,4 @@ Plaintext Decryptor::Decrypt(const Ciphertext &ct) const { BigInt tmp = ct.n_.AddMod(ZERO, p).AddMod(ZERO, q).AddMod(ZERO, MAX); return tmp <= half ? tmp : tmp - MAX; } -} // namespace heu::lib::algorithms::ashe +} // namespace heu::lib::algorithms::ashe diff --git a/heu/library/algorithms/ashe/decryptor.h b/heu/library/algorithms/ashe/decryptor.h index 7de265a..b3ae071 100644 --- a/heu/library/algorithms/ashe/decryptor.h +++ b/heu/library/algorithms/ashe/decryptor.h @@ -22,9 +22,9 @@ namespace heu::lib::algorithms::ashe { class Decryptor { -public: + public: explicit Decryptor(PublicParameters pp, SecretKey sk) - : pp_(std::move(pp)), sk_(std::move(sk)) { + : pp_(std::move(pp)), sk_(std::move(sk)) { p = sk_.p_; q = sk_.q_; } @@ -33,7 +33,7 @@ class Decryptor { [[nodiscard]] Plaintext Decrypt(const Ciphertext &ct) const; -private: + private: PublicParameters pp_; SecretKey sk_; BigInt half = BigInt(UINT64_MAX) / BigInt(2); @@ -42,4 +42,4 @@ class Decryptor { BigInt q; BigInt ZERO = BigInt(0); }; -} // namespace heu::lib::algorithms::ashe +} // namespace heu::lib::algorithms::ashe diff --git a/heu/library/algorithms/ashe/encryptor.cc b/heu/library/algorithms/ashe/encryptor.cc index 05fc19f..4dadc12 100644 --- a/heu/library/algorithms/ashe/encryptor.cc +++ b/heu/library/algorithms/ashe/encryptor.cc @@ -15,9 +15,7 @@ #include "heu/library/algorithms/ashe/encryptor.h" namespace heu::lib::algorithms::ashe { -Ciphertext Encryptor::EncryptZero() const { - return Encrypt(BigInt(0)); -} +Ciphertext Encryptor::EncryptZero() const { return Encrypt(BigInt(0)); } Ciphertext Encryptor::Encrypt(const Plaintext &m) const { return EncryptImpl(m, nullptr); @@ -27,7 +25,8 @@ void Encryptor::Encrypt(const Plaintext &m, Ciphertext *out) const { *out = Encrypt(m); } -std::pair Encryptor::EncryptWithAudit(const Plaintext &m) const { +std::pair Encryptor::EncryptWithAudit( + const Plaintext &m) const { std::string audit_out; Ciphertext ct_out = EncryptImpl(m, &audit_out); audit_out.append( @@ -36,7 +35,8 @@ std::pair Encryptor::EncryptWithAudit(const Plaintext & } template -Ciphertext Encryptor::EncryptImpl(const Plaintext &m, std::string *audit_str) const { +Ciphertext Encryptor::EncryptImpl(const Plaintext &m, + std::string *audit_str) const { YACL_ENFORCE(m <= pp_.MessageSpace().second && m >= pp_.MessageSpace().first, "Plaintext {} is too large, cannot encrypt.", m); BigInt r, r1; @@ -51,4 +51,4 @@ Ciphertext Encryptor::EncryptImpl(const Plaintext &m, std::string *audit_str) co } return Ciphertext(m1); } -} // namespace heu::lib::algorithms::ashe +} // namespace heu::lib::algorithms::ashe diff --git a/heu/library/algorithms/ashe/encryptor.h b/heu/library/algorithms/ashe/encryptor.h index 5f3ac42..d5bf2c4 100644 --- a/heu/library/algorithms/ashe/encryptor.h +++ b/heu/library/algorithms/ashe/encryptor.h @@ -22,9 +22,9 @@ namespace heu::lib::algorithms::ashe { class Encryptor { -public: + public: explicit Encryptor(PublicParameters pk, SecretKey sk) - : pp_(std::move(pk)), sk_(std::move(sk)) {} + : pp_(std::move(pk)), sk_(std::move(sk)) {} [[nodiscard]] Ciphertext EncryptZero() const; [[nodiscard]] Ciphertext Encrypt(const Plaintext &m) const; @@ -34,13 +34,12 @@ class Encryptor { [[nodiscard]] std::pair EncryptWithAudit( const Plaintext &m) const; -private: + private: template - Ciphertext EncryptImpl(const Plaintext &m, - std::string *audit_str) const; + Ciphertext EncryptImpl(const Plaintext &m, std::string *audit_str) const; PublicParameters pp_; SecretKey sk_; BigInt ZERO = BigInt(0); BigInt MAX = BigInt(UINT64_MAX); }; -} // namespace heu::lib::algorithms::ashe +} // namespace heu::lib::algorithms::ashe diff --git a/heu/library/algorithms/ashe/evaluator.cc b/heu/library/algorithms/ashe/evaluator.cc index ed91534..38c4712 100644 --- a/heu/library/algorithms/ashe/evaluator.cc +++ b/heu/library/algorithms/ashe/evaluator.cc @@ -109,7 +109,5 @@ Ciphertext Evaluator::Negate(const Ciphertext &a) const { return Mul(a, neg); } -void Evaluator::NegateInplace(Ciphertext *a) const { - *a = Negate(*a); -} -} // namespace heu::lib::algorithms::ashe +void Evaluator::NegateInplace(Ciphertext *a) const { *a = Negate(*a); } +} // namespace heu::lib::algorithms::ashe diff --git a/heu/library/algorithms/ashe/evaluator.h b/heu/library/algorithms/ashe/evaluator.h index 9670bb3..8e85b5b 100644 --- a/heu/library/algorithms/ashe/evaluator.h +++ b/heu/library/algorithms/ashe/evaluator.h @@ -21,9 +21,8 @@ namespace heu::lib::algorithms::ashe { class Evaluator { -public: - explicit Evaluator(PublicParameters pp) : pp_(std::move(pp)) { - } + public: + explicit Evaluator(PublicParameters pp) : pp_(std::move(pp)) {} void Randomize(Ciphertext *ct) const; @@ -55,11 +54,11 @@ class Evaluator { [[nodiscard]] Ciphertext Negate(const Ciphertext &a) const; void NegateInplace(Ciphertext *a) const; -private: + private: PublicParameters pp_; BigInt ONE = BigInt(1); BigInt ZERO = BigInt(0); BigInt MAX = BigInt(UINT64_MAX); BigInt PlainSpace = BigInt(2).Pow(16); }; -} // namespace heu::lib::algorithms::ashe +} // namespace heu::lib::algorithms::ashe diff --git a/heu/library/algorithms/ashe/key_generator.cc b/heu/library/algorithms/ashe/key_generator.cc index cfacc9e..9509494 100644 --- a/heu/library/algorithms/ashe/key_generator.cc +++ b/heu/library/algorithms/ashe/key_generator.cc @@ -18,7 +18,8 @@ namespace heu::lib::algorithms::ashe { void KeyGenerator::Generate(int key_size, SecretKey *sk, PublicParameters *pk) { - int64_t k_r1, k_p, k_q, k_r2, k_m;; + int64_t k_r1, k_p, k_q, k_r2, k_m; + ; if (key_size == 2048) { k_r1 = 4384; k_p = 1536; @@ -38,8 +39,7 @@ void KeyGenerator::Generate(int key_size, SecretKey *sk, PublicParameters *pk) { *sk = SecretKey(p, q); InitZeros(k_r1, k_p, k_q, k_r2, k_m, *sk, &zeros); - *pk = - PublicParameters(k_r1, k_p, k_q, k_r2, k_m, zeros); + *pk = PublicParameters(k_r1, k_p, k_q, k_r2, k_m, zeros); } void KeyGenerator::Generate(SecretKey *sk, PublicParameters *pk) { @@ -55,4 +55,4 @@ void KeyGenerator::InitZeros(int64_t k_r1, int64_t k_p, int64_t k_q, zeros->emplace_back(et.Encrypt(BigInt(0)).n_); } } -} // namespace heu::lib::algorithms::ashe +} // namespace heu::lib::algorithms::ashe diff --git a/heu/library/algorithms/ashe/key_generator.h b/heu/library/algorithms/ashe/key_generator.h index 54c8c5f..4646fdf 100644 --- a/heu/library/algorithms/ashe/key_generator.h +++ b/heu/library/algorithms/ashe/key_generator.h @@ -14,19 +14,18 @@ #pragma once +#include "heu/library/algorithms/ashe/encryptor.h" #include "heu/library/algorithms/ashe/public_parameters.h" #include "heu/library/algorithms/ashe/secret_key.h" -#include "heu/library/algorithms/ashe/encryptor.h" namespace heu::lib::algorithms::ashe { class KeyGenerator { -public: + public: static void Generate(int key_size, SecretKey *sk, PublicParameters *pk); static void Generate(SecretKey *sk, PublicParameters *pk); -private: + private: static void InitZeros(int64_t k_r1, int64_t k_p, int64_t k_q, int64_t k_r2, - int64_t k_m, SecretKey sk_, - std::vector *zeros); + int64_t k_m, SecretKey sk_, std::vector *zeros); }; -} // namespace heu::lib::algorithms::ashe +} // namespace heu::lib::algorithms::ashe diff --git a/heu/library/algorithms/ashe/public_parameters.cc b/heu/library/algorithms/ashe/public_parameters.cc index f81f85b..2b9a345 100644 --- a/heu/library/algorithms/ashe/public_parameters.cc +++ b/heu/library/algorithms/ashe/public_parameters.cc @@ -13,9 +13,8 @@ PublicParameters::PublicParameters(int64_t k_r1, int64_t k_p, int64_t k_q, PublicParameters::PublicParameters(int64_t k_r1, int64_t k_p, int64_t k_q, int64_t k_r2, int64_t k_m, - const std::vector &zeros) : - PublicParameters( - k_r1, k_p, k_q, k_r2, k_m) { + const std::vector &zeros) + : PublicParameters(k_r1, k_p, k_q, k_r2, k_m) { this->randomZeros = zeros; } @@ -32,4 +31,4 @@ void PublicParameters::Init() { this->M[1] = BigInt(2).Pow(k_m - 1) - BigInt(1); this->M[0] = -this->M[1]; } -} // namespace heu::lib::algorithms::ashe +} // namespace heu::lib::algorithms::ashe diff --git a/heu/library/algorithms/ashe/public_parameters.h b/heu/library/algorithms/ashe/public_parameters.h index b71f4ec..b5cc12e 100644 --- a/heu/library/algorithms/ashe/public_parameters.h +++ b/heu/library/algorithms/ashe/public_parameters.h @@ -21,7 +21,7 @@ namespace heu::lib::algorithms::ashe { class PublicParameters : public HeObject { -private: + private: BigInt plaintextBound; static std::string ToHexString(const std::vector &vec) { @@ -37,7 +37,7 @@ class PublicParameters : public HeObject { return oss.str(); } -public: + public: int64_t k_r1 = 4384; int64_t k_p = 1536; int64_t k_q = 1008; @@ -46,7 +46,6 @@ class PublicParameters : public HeObject { std::vector randomZeros; BigInt M[2]; - PublicParameters() = default; PublicParameters(int64_t k_r1, int64_t k_p, int64_t k_q, int64_t k_r2, @@ -66,9 +65,7 @@ class PublicParameters : public HeObject { [[nodiscard]] std::string ToString() const override; - [[nodiscard]] const BigInt &PlaintextBound() const & { - return M[1]; - } + [[nodiscard]] const BigInt &PlaintextBound() const & { return M[1]; } void Init(); @@ -78,4 +75,4 @@ class PublicParameters : public HeObject { MSGPACK_DEFINE(k_r1, k_p, k_q, k_r2, k_m, M, randomZeros); }; -} // namespace heu::lib::algorithms::ashe +} // namespace heu::lib::algorithms::ashe diff --git a/heu/library/algorithms/ashe/secret_key.h b/heu/library/algorithms/ashe/secret_key.h index 65dff8f..117fe5c 100644 --- a/heu/library/algorithms/ashe/secret_key.h +++ b/heu/library/algorithms/ashe/secret_key.h @@ -20,7 +20,7 @@ namespace heu::lib::algorithms::ashe { class SecretKey : public HeObject { -public: + public: BigInt p_, q_; SecretKey(BigInt p, BigInt q) { @@ -39,11 +39,10 @@ class SecretKey : public HeObject { } [[nodiscard]] std::string ToString() const override { - return fmt::format("ashe SK, p={}[{}bits], q={}[{}bits]", - p_.ToHexString(), p_.BitCount(), - q_.ToHexString(), q_.BitCount()); + return fmt::format("ashe SK, p={}[{}bits], q={}[{}bits]", p_.ToHexString(), + p_.BitCount(), q_.ToHexString(), q_.BitCount()); } MSGPACK_DEFINE(p_, q_); }; -} // namespace heu::lib::algorithms::ashe +} // namespace heu::lib::algorithms::ashe From 9db2c2f145b49529810066204df4a062694d495d Mon Sep 17 00:00:00 2001 From: Alec-xdu <1767162258@qq.com> Date: Mon, 19 Jan 2026 14:02:15 +0800 Subject: [PATCH 3/5] reformat --- heu/library/algorithms/ashe/BUILD.bazel | 8 ++++---- heu/library/algorithms/ashe/ciphertext.h | 11 ++++------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/heu/library/algorithms/ashe/BUILD.bazel b/heu/library/algorithms/ashe/BUILD.bazel index 5eb85ee..a647baf 100644 --- a/heu/library/algorithms/ashe/BUILD.bazel +++ b/heu/library/algorithms/ashe/BUILD.bazel @@ -68,7 +68,7 @@ yacl_cc_library( deps = [ ":public_parameters", ":secret_key", - ":encryptor" + ":encryptor", ], ) @@ -79,7 +79,7 @@ yacl_cc_library( deps = [ ":ciphertext", ":public_parameters", - ":secret_key" + ":secret_key", ], ) @@ -108,6 +108,6 @@ yacl_cc_test( name = "ashe_test", srcs = ["ashe_tests.cc"], deps = [ - ":ashe" - ] + ":ashe", + ], ) diff --git a/heu/library/algorithms/ashe/ciphertext.h b/heu/library/algorithms/ashe/ciphertext.h index 356f789..9668403 100644 --- a/heu/library/algorithms/ashe/ciphertext.h +++ b/heu/library/algorithms/ashe/ciphertext.h @@ -23,19 +23,16 @@ namespace heu::lib::algorithms::ashe { using Plaintext = BigInt; class Ciphertext : public HeObject { -public: + public: Ciphertext() = default; - explicit Ciphertext(BigInt n) : n_(std::move(n)) { - } + explicit Ciphertext(BigInt n) : n_(std::move(n)) {} [[nodiscard]] std::string ToString() const override { return fmt::format("CT: {}", n_); } - bool operator==(const Ciphertext &other) const { - return n_ == other.n_; - } + bool operator==(const Ciphertext &other) const { return n_ == other.n_; } bool operator!=(const Ciphertext &other) const { return !this->operator==(other); @@ -45,4 +42,4 @@ class Ciphertext : public HeObject { BigInt n_; }; -} // namespace heu::lib::algorithms::ashe +} // namespace heu::lib::algorithms::ashe From 2e209845b6abd63a90273509c3bb8dbe99f784af Mon Sep 17 00:00:00 2001 From: Alec-xdu <1767162258@qq.com> Date: Mon, 19 Jan 2026 15:54:43 +0800 Subject: [PATCH 4/5] reformat BUILD.bazel --- heu/library/algorithms/ashe/BUILD.bazel | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/heu/library/algorithms/ashe/BUILD.bazel b/heu/library/algorithms/ashe/BUILD.bazel index a647baf..43bd176 100644 --- a/heu/library/algorithms/ashe/BUILD.bazel +++ b/heu/library/algorithms/ashe/BUILD.bazel @@ -66,9 +66,9 @@ yacl_cc_library( srcs = ["key_generator.cc"], hdrs = ["key_generator.h"], deps = [ + ":encryptor", ":public_parameters", ":secret_key", - ":encryptor", ], ) From 7ecc4a385f0e9be9b8d63a6bad57809f7ab80ede Mon Sep 17 00:00:00 2001 From: Alec-xdu <1767162258@qq.com> Date: Mon, 19 Jan 2026 16:00:48 +0800 Subject: [PATCH 5/5] add license information for public_parameters.cc --- heu/library/algorithms/ashe/public_parameters.cc | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/heu/library/algorithms/ashe/public_parameters.cc b/heu/library/algorithms/ashe/public_parameters.cc index 2b9a345..0610562 100644 --- a/heu/library/algorithms/ashe/public_parameters.cc +++ b/heu/library/algorithms/ashe/public_parameters.cc @@ -1,3 +1,17 @@ +// Copyright 2022 Ant Group Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + #include "heu/library/algorithms/ashe/public_parameters.h" namespace heu::lib::algorithms::ashe {