-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
repo-sync-2024-03-21T19:06:22+0800 (#134)
* repo-sync-2024-03-21T19:06:22+0800 * repo-sync-2024-03-21T19:13:13+0800 * Update base.h * Update base.h * Update tool.cc
- Loading branch information
Showing
25 changed files
with
1,470 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# 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"]) | ||
|
||
yacl_cc_library( | ||
name = "mock_fhe", | ||
srcs = ["he_kit.cc"], | ||
hdrs = ["he_kit.h"], | ||
deps = [ | ||
":decryptor", | ||
":encoders", | ||
":encryptor", | ||
":evaluator", | ||
], | ||
alwayslink = 1, | ||
) | ||
|
||
yacl_cc_library( | ||
name = "base", | ||
srcs = ["base.cc"], | ||
hdrs = ["base.h"], | ||
deps = [ | ||
"//heu/spi/he/sketches/scalar", | ||
"//heu/spi/utils:formater", | ||
"@yacl//yacl/utils:serializer", | ||
], | ||
) | ||
|
||
yacl_cc_library( | ||
name = "encoders", | ||
srcs = ["encoders.cc"], | ||
hdrs = ["encoders.h"], | ||
deps = [ | ||
":base", | ||
], | ||
) | ||
|
||
yacl_cc_library( | ||
name = "encryptor", | ||
srcs = ["encryptor.cc"], | ||
hdrs = ["encryptor.h"], | ||
deps = [ | ||
":base", | ||
], | ||
) | ||
|
||
yacl_cc_library( | ||
name = "decryptor", | ||
srcs = ["decryptor.cc"], | ||
hdrs = ["decryptor.h"], | ||
deps = [ | ||
":base", | ||
], | ||
) | ||
|
||
yacl_cc_library( | ||
name = "evaluator", | ||
srcs = ["evaluator.cc"], | ||
hdrs = ["evaluator.h"], | ||
deps = [ | ||
":base", | ||
"//heu/algorithms/common:type_alias", | ||
"//heu/spi/utils:math_tool", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// 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. | ||
|
||
#include "heu/algorithms/mock_fhe/base.h" | ||
|
||
#include <vector> | ||
|
||
#include "yacl/utils/serializer.h" | ||
|
||
#include "heu/spi/utils/formater.h" | ||
|
||
namespace heu::algos::mock_fhe { | ||
|
||
std::string Plaintext::ToString() const { | ||
return fmt::format("PT({}, scale={})", | ||
spi::utils::ArrayToStringCompact<int64_t>(array_), scale_); | ||
} | ||
|
||
std::string Ciphertext::ToString() const { | ||
return fmt::format("CT({}, scale={})", | ||
spi::utils::ArrayToStringCompact<int64_t>(array_), scale_); | ||
} | ||
|
||
Plaintext ItemTool::Clone(const Plaintext &pt) const { return pt; } | ||
|
||
Ciphertext ItemTool::Clone(const Ciphertext &ct) const { return ct; } | ||
|
||
size_t ItemTool::Serialize(const Plaintext &pt, uint8_t *buf, | ||
size_t buf_len) const { | ||
return yacl::SerializeVarsTo(buf, buf_len, pt.array_, pt.scale_); | ||
} | ||
|
||
size_t ItemTool::Serialize(const Ciphertext &ct, uint8_t *buf, | ||
size_t buf_len) const { | ||
return yacl::SerializeVarsTo(buf, buf_len, ct.array_, ct.scale_); | ||
} | ||
|
||
Plaintext ItemTool::DeserializePT(yacl::ByteContainerView buffer) const { | ||
Plaintext res; | ||
yacl::DeserializeVarsTo(buffer, &res.array_, &res.scale_); | ||
return res; | ||
} | ||
|
||
Ciphertext ItemTool::DeserializeCT(yacl::ByteContainerView buffer) const { | ||
Ciphertext res; | ||
yacl::DeserializeVarsTo(buffer, &res.array_, &res.scale_); | ||
return res; | ||
} | ||
|
||
} // namespace heu::algos::mock_fhe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// 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. | ||
|
||
#pragma once | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
#include "heu/spi/he/sketches/common/keys.h" | ||
#include "heu/spi/he/sketches/scalar/item_tool.h" | ||
|
||
namespace heu::algos::mock_fhe { | ||
|
||
class MockObj { | ||
public: | ||
MockObj() = default; | ||
|
||
explicit MockObj(const std::vector<int64_t> &array, double scale = 1) | ||
: array_(array), scale_(scale) {} | ||
|
||
virtual ~MockObj() = default; | ||
|
||
auto operator->() { return &array_; } | ||
|
||
auto operator->() const { return &array_; } | ||
|
||
bool operator==(const MockObj &rhs) const { return array_ == rhs.array_; } | ||
|
||
virtual std::string ToString() const = 0; | ||
|
||
std::vector<int64_t> array_; | ||
double scale_ = 1; // only mock_ckks need scale | ||
}; | ||
|
||
class Plaintext : public MockObj { | ||
public: | ||
using MockObj::MockObj; | ||
|
||
std::string ToString() const; | ||
}; | ||
|
||
class Ciphertext : public MockObj { | ||
public: | ||
using MockObj::MockObj; | ||
|
||
std::string ToString() const; | ||
}; | ||
|
||
class SecretKey : public spi::EmptyKeySketch<spi::HeKeyType::SecretKey> {}; | ||
|
||
class PublicKey : public spi::EmptyKeySketch<spi::HeKeyType::PublicKey> {}; | ||
|
||
class RelinKeys : public spi::EmptyKeySketch<spi::HeKeyType::RelinKeys> {}; | ||
|
||
class GaloisKeys : public spi::EmptyKeySketch<spi::HeKeyType::GaloisKeys> {}; | ||
|
||
class BootstrapKey : public spi::EmptyKeySketch<spi::HeKeyType::BootstrapKey> { | ||
}; | ||
|
||
class ItemTool : public spi::ItemToolScalarSketch<Plaintext, Ciphertext, | ||
SecretKey, PublicKey> { | ||
public: | ||
Plaintext Clone(const Plaintext &pt) const override; | ||
Ciphertext Clone(const Ciphertext &ct) const override; | ||
|
||
size_t Serialize(const Plaintext &pt, uint8_t *buf, | ||
size_t buf_len) const override; | ||
size_t Serialize(const Ciphertext &ct, uint8_t *buf, | ||
size_t buf_len) const override; | ||
|
||
Plaintext DeserializePT(yacl::ByteContainerView buffer) const override; | ||
Ciphertext DeserializeCT(yacl::ByteContainerView buffer) const override; | ||
}; | ||
|
||
} // namespace heu::algos::mock_fhe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// 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. | ||
|
||
#include "heu/algorithms/mock_fhe/decryptor.h" | ||
|
||
namespace heu::algos::mock_fhe { | ||
|
||
Decryptor::Decryptor(const std::shared_ptr<PublicKey> &pk, | ||
const std::shared_ptr<SecretKey> &sk) | ||
: pk_(pk), sk_(sk) {} | ||
|
||
void Decryptor::Decrypt(const Ciphertext &ct, Plaintext *out) const { | ||
out->array_ = ct.array_; | ||
out->scale_ = ct.scale_; | ||
} | ||
|
||
Plaintext Decryptor::Decrypt(const Ciphertext &ct) const { | ||
return Plaintext(ct.array_, ct.scale_); | ||
} | ||
|
||
} // namespace heu::algos::mock_fhe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// 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. | ||
|
||
#pragma once | ||
|
||
#include <utility> | ||
|
||
#include "heu/algorithms/mock_fhe/base.h" | ||
#include "heu/spi/he/sketches/scalar/decryptor.h" | ||
|
||
namespace heu::algos::mock_fhe { | ||
|
||
class Decryptor : public spi::DecryptorScalarSketch<Plaintext, Ciphertext> { | ||
public: | ||
Decryptor(const std::shared_ptr<PublicKey> &pk, | ||
const std::shared_ptr<SecretKey> &sk); | ||
|
||
void Decrypt(const Ciphertext &ct, Plaintext *out) const override; | ||
Plaintext Decrypt(const Ciphertext &ct) const override; | ||
|
||
private: | ||
std::shared_ptr<PublicKey> pk_; | ||
std::shared_ptr<SecretKey> sk_; | ||
}; | ||
|
||
} // namespace heu::algos::mock_fhe |
Oops, something went wrong.