Skip to content

Commit

Permalink
repo-sync-2024-03-21T19:06:22+0800 (#134)
Browse files Browse the repository at this point in the history
* 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
usafchn authored Mar 25, 2024
1 parent bbfb547 commit 8f679eb
Show file tree
Hide file tree
Showing 25 changed files with 1,470 additions and 25 deletions.
3 changes: 0 additions & 3 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,3 @@ test --test_output=errors
test --test_timeout=600

coverage --test_timeout=1800

# delete this line when YACL repo is synced
build:linux --cxxopt -Wno-error=mismatched-new-delete
8 changes: 6 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# Install hooks to run automatically on git commit:
# >> pre-commit install
# Install hooks to run automatically on git commit: (Only needs to be run once)
# >> pre-commit install
#
# If the pre-commit command does not exist, please run
# >> pip install pre-commit
# to install it.
repos:
# buildifier
- repo: local
Expand Down
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

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

YACL_COMMIT_ID = "9a74ab6f4bb0c9c21171e945a379e27fde482cea"
YACL_COMMIT_ID = "a1037e4740fbb0f49f8e2b0a87dcbf7ed344c2c6"

git_repository(
name = "yacl",
Expand Down
79 changes: 79 additions & 0 deletions heu/algorithms/mock_fhe/BUILD.bazel
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",
],
)
61 changes: 61 additions & 0 deletions heu/algorithms/mock_fhe/base.cc
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
86 changes: 86 additions & 0 deletions heu/algorithms/mock_fhe/base.h
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
32 changes: 32 additions & 0 deletions heu/algorithms/mock_fhe/decryptor.cc
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
37 changes: 37 additions & 0 deletions heu/algorithms/mock_fhe/decryptor.h
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
Loading

0 comments on commit 8f679eb

Please sign in to comment.