Skip to content

Commit

Permalink
repo-sync-2024-05-29T11:18:59+0800 (#136)
Browse files Browse the repository at this point in the history
* repo-sync-2024-05-29T11:18:59+0800

* Update WORKSPACE

* Reformat bazel file

* Reformat code
  • Loading branch information
shaojian-ant authored May 29, 2024
1 parent c23fdf9 commit c6886af
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 12 deletions.
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 = "a1037e4740fbb0f49f8e2b0a87dcbf7ed344c2c6"
YACL_COMMIT_ID = "05b879b8133e280d16ee03d24d2f2bba1c9db7ea"

git_repository(
name = "yacl",
Expand Down
7 changes: 4 additions & 3 deletions heu/algorithms/mock_fhe/he_kit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ std::unique_ptr<spi::HeKit> HeKit::Create(spi::Schema schema,
YACL_ENFORCE(
schema == spi::Schema::MockBfv || schema == spi::Schema::MockCkks,
"Schema {} not supported by {}", schema, kLibName);
YACL_ENFORCE(
args.Exist(spi::ArgGenNewPkSk) || args.Exist(spi::ArgPkFrom),
"Neither ArgGenNewPkSk nor ArgPkFrom is set, you must set one of them");
YACL_ENFORCE(args.Exist(spi::ArgGenNewPkSk) || args.Exist(spi::ArgPkFrom),
"Neither ArgGenNewPkSk nor ArgPkFrom is set, you must set one "
"of them, args={}",
args);

// process context
auto kit = std::make_unique<HeKit>();
Expand Down
2 changes: 1 addition & 1 deletion heu/library/algorithms/elgamal/key_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void KeyGenerator::Generate(const yacl::crypto::CurveName &curve_name,
::yacl::crypto::EcGroupFactory::Instance().Create(curve_name);
MPInt x;
do {
MPInt::RandomLtN(curve->GetField(), &x);
MPInt::RandomLtN(curve->GetOrder(), &x);
// The following operations may not be necessary, but there is no harm even
// if they are done. It is a kind of psychological comfort and makes people
// feel safer.
Expand Down
7 changes: 7 additions & 0 deletions heu/library/algorithms/paillier_gpu/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ paillier_gpu 算法默认关闭,请用以下方式启用:
bazel test --config=gpu //heu/...
```

运行性能测试:
```
bazel run -c opt heu/library/benchmark:phe --config=gpu -- --schema=gpaillier
# 或者
bazel run -c opt heu/library/benchmark:np --config=gpu -- --schema=gpaillier
```

编译带 paillier_gpu 的 pip 包:

```
Expand Down
9 changes: 6 additions & 3 deletions heu/library/numpy/matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,13 +265,16 @@ class DenseMatrix {

static DenseMatrix<T> LoadFrom(
yacl::ByteContainerView in,
MatrixSerializeFormat format = MatrixSerializeFormat::Best) {
MatrixSerializeFormat format = MatrixSerializeFormat::Best,
size_t *offset = nullptr) {
if (format == MatrixSerializeFormat::Interconnection) {
return LoadFromIc(in);
}

auto msg =
msgpack::unpack(reinterpret_cast<const char *>(in.data()), in.size());
size_t zero = 0;
size_t *off = (offset == nullptr ? &zero : offset);
auto msg = msgpack::unpack(reinterpret_cast<const char *>(in.data()),
in.size(), *off);
msgpack::object o = msg.get();

YACL_ENFORCE(o.type == msgpack::type::ARRAY && o.via.array.size == 4,
Expand Down
36 changes: 33 additions & 3 deletions heu/pylib/phe_binding/bind_phe.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,22 @@ void PyBindPhe(pybind11::module &m) {

py::class_<phe::Ciphertext>(m, "Ciphertext")
.def("__str__", [](const phe::Ciphertext &ct) { return ct.ToString(); })
.def(PyUtils::PickleSupport<phe::Ciphertext>());
.def(PyUtils::PickleSupport<phe::Ciphertext>())
.def(
"serialize",
[](const phe::Ciphertext &ct) {
auto buffer = ct.Serialize();
return pybind11::bytes(buffer.template data<char>(), buffer.size());
},
"serialize ciphertext to bytes")
.def_static(
"load_from",
[](const pybind11::bytes &buffer) {
phe::Ciphertext ct;
ct.Deserialize(static_cast<std::string_view>(buffer));
return ct;
},
py::arg("bytes_buffer"), "deserialize ciphertext from bytes");

/****** key management ******/
py::class_<phe::PublicKey, std::shared_ptr<phe::PublicKey>>(m, "PublicKey")
Expand All @@ -133,10 +148,25 @@ void PyBindPhe(pybind11::module &m) {
pk.Deserialize(static_cast<std::string_view>(buffer));
return pk;
},
py::arg("bytes_buffer"), "deserialize matrix from bytes");
py::arg("bytes_buffer"), "deserialize public key from bytes");
py::class_<phe::SecretKey, std::shared_ptr<phe::SecretKey>>(m, "SecretKey")
.def("__str__", [](const phe::SecretKey &sk) { return sk.ToString(); })
.def(PyUtils::PickleSupport<phe::SecretKey>());
.def(PyUtils::PickleSupport<phe::SecretKey>())
.def(
"serialize",
[](const phe::SecretKey &sk) {
auto buffer = sk.Serialize();
return pybind11::bytes(buffer.template data<char>(), buffer.size());
},
"serialize secret key to bytes")
.def_static(
"load_from",
[](const pybind11::bytes &buffer) {
phe::SecretKey sk;
sk.Deserialize(static_cast<std::string_view>(buffer));
return sk;
},
py::arg("bytes_buffer"), "deserialize secret key from bytes");

/****** He kit ******/
py::class_<phe::HeKitPublicBase>(m, "HeKitPublicBase")
Expand Down
25 changes: 25 additions & 0 deletions heu/pylib/phe_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,14 @@ def test_batch_encoding(self):
(123 - 789, 456 - 101112),
)

def test_ciphertext_serialize(self):
# client
ct1 = self.encryptor.encrypt_raw(123)
ct1_bytes = ct1.serialize()

ct2 = phe.Ciphertext.load_from(ct1_bytes)
self.assertEqual(self.decryptor.decrypt_raw(ct2), 123)


class ServerClientCase(unittest.TestCase):
def test_pickle(self):
Expand All @@ -269,6 +277,23 @@ def test_pickle(self):
ct_x = pickle.loads(ct3_buffer)
self.assertEqual(client_he2.decryptor().decrypt_raw(ct_x), 123 - 456)

def test_sk_serialize(self):
# client
client_he1 = phe.setup(phe.SchemaType.ZPaillier, 2048)
sk1 = client_he1.secret_key()
pk1 = client_he1.public_key()
sk1_bytes = sk1.serialize()
pk1_bytes = pk1.serialize()

sk2 = phe.SecretKey.load_from(sk1_bytes)
pk2 = phe.PublicKey.load_from(pk1_bytes)

client_he2 = phe.setup(pk2, sk2)

ct1 = client_he1.encryptor().encrypt_raw(123)

self.assertEqual(client_he2.decryptor().decrypt_raw(ct1), 123)


if __name__ == "__main__":
unittest.main()
2 changes: 1 addition & 1 deletion heu/pylib/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# limitations under the License.


__version__ = "0.6.0.dev0"
__version__ = "0.6.0.dev20240529"
3 changes: 3 additions & 0 deletions heu/spi/he/sketches/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
如果您是 SPI 的使用者,则无需关心 `sketches/` 目录,该目录存放 SPI 接口的实现


# Sketch 说明

`sketches/` 目录存放 HE SPI 接口的预实现,根据预实现的方式不同 Sketch 分为两大类:
Expand Down

0 comments on commit c6886af

Please sign in to comment.