From a91c4f2f7e43f7de367076c9705010e605e7865d Mon Sep 17 00:00:00 2001 From: Jamie-Cui Date: Tue, 25 Jun 2024 17:26:41 +0800 Subject: [PATCH] repo-sync-2024-06-25T13:11:42+0800 (#345) --- GETTING_STARTED.md | 4 +- examples/psi/BUILD.bazel | 37 +++ examples/psi/ecdh_psi.cc | 44 +++ examples/psi/ecdh_psi.h | 58 ++++ examples/psi/ecdh_psi_test.cc | 104 +++++++ yacl/kernel/algorithms/BUILD.bazel | 47 +-- yacl/kernel/algorithms/base_ot.h | 2 +- yacl/kernel/algorithms/base_vole.h | 2 +- yacl/kernel/algorithms/ferret_ote.h | 2 +- yacl/kernel/algorithms/ferret_ote_test.cc | 2 +- yacl/kernel/algorithms/gywz_ote.h | 2 +- yacl/kernel/algorithms/gywz_ote_test.cc | 2 +- yacl/kernel/algorithms/iknp_ote.h | 2 +- yacl/kernel/algorithms/kkrt_ote.h | 2 +- yacl/kernel/algorithms/kkrt_ote_test.cc | 2 +- yacl/kernel/algorithms/kos_ote.h | 2 +- yacl/kernel/algorithms/mp_vole.h | 2 +- yacl/kernel/algorithms/mpfss.h | 2 +- yacl/kernel/algorithms/sgrr_ote.h | 2 +- yacl/kernel/algorithms/sgrr_ote_test.cc | 2 +- yacl/kernel/algorithms/softspoken_ote.cc | 2 +- yacl/kernel/algorithms/softspoken_ote.h | 2 +- yacl/kernel/algorithms/softspoken_ote_test.cc | 2 +- yacl/kernel/benchmark/ot_bench.h | 2 +- yacl/kernel/ot_kernel.cc | 2 +- yacl/kernel/ot_kernel.h | 4 +- yacl/kernel/ot_kernel_bench.cc | 14 +- yacl/kernel/ot_kernel_test.cc | 2 +- yacl/kernel/type/BUILD.bazel | 65 ++++ yacl/kernel/{algorithms => type}/ot_store.cc | 291 +----------------- yacl/kernel/{algorithms => type}/ot_store.h | 119 +------ .../{algorithms => type}/ot_store_test.cc | 3 +- yacl/kernel/type/ot_store_utils.cc | 257 ++++++++++++++++ yacl/kernel/type/ot_store_utils.h | 80 +++++ yacl/kernel/type/slice_base.cc | 71 +++++ yacl/kernel/type/slice_base.h | 81 +++++ 36 files changed, 848 insertions(+), 471 deletions(-) create mode 100644 examples/psi/BUILD.bazel create mode 100644 examples/psi/ecdh_psi.cc create mode 100644 examples/psi/ecdh_psi.h create mode 100644 examples/psi/ecdh_psi_test.cc create mode 100644 yacl/kernel/type/BUILD.bazel rename yacl/kernel/{algorithms => type}/ot_store.cc (56%) rename yacl/kernel/{algorithms => type}/ot_store.h (58%) rename yacl/kernel/{algorithms => type}/ot_store_test.cc (99%) create mode 100644 yacl/kernel/type/ot_store_utils.cc create mode 100644 yacl/kernel/type/ot_store_utils.h create mode 100644 yacl/kernel/type/slice_base.cc create mode 100644 yacl/kernel/type/slice_base.h diff --git a/GETTING_STARTED.md b/GETTING_STARTED.md index 8262efbf..8b57abe9 100644 --- a/GETTING_STARTED.md +++ b/GETTING_STARTED.md @@ -4,7 +4,7 @@ This document includes guidelines. ## Table of Contents -- [Prerequisites]((#prerequisites)) +- [Prerequisites](#prerequisites) - [Download and Build](#download-and-build) - [Ubuntu](#ubuntu) - [MacOS](#macos) @@ -15,7 +15,7 @@ This document includes guidelines. To build Yacl from source, you will need the following tools: -- **bazel**: [.bazelversion](.bazelversion) file describes the recommended version of bazel. We recommend to use the official [bazelisk](https://github.com/bazelbuild/bazelisk?tab=readme-ov-file#installation) to manage bazel version. +- **bazel**: We recommend to use the official [bazelisk](https://github.com/bazelbuild/bazelisk?tab=readme-ov-file#installation) to manage bazel version. - **gcc >= 10.3** - **[cmake](https://cmake.org/getting-started/)** - **[ninja/ninja-build](https://ninja-build.org/)** diff --git a/examples/psi/BUILD.bazel b/examples/psi/BUILD.bazel new file mode 100644 index 00000000..b1474ce4 --- /dev/null +++ b/examples/psi/BUILD.bazel @@ -0,0 +1,37 @@ +# Copyright 2024 zhangwfjh +# +# 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("//bazel:yacl.bzl", "AES_COPT_FLAGS", "yacl_cc_library", "yacl_cc_test") + +package(default_visibility = ["//visibility:public"]) + +yacl_cc_library( + name = "ecdh_psi", + srcs = [ + "ecdh_psi.cc", + ], + hdrs = [ + "ecdh_psi.h", + ], + deps = [ + "//yacl/crypto/ecc", + "//yacl/link", + ], +) + +yacl_cc_test( + name = "ecdh_psi_test", + srcs = ["ecdh_psi_test.cc"], + deps = [":ecdh_psi"], +) diff --git a/examples/psi/ecdh_psi.cc b/examples/psi/ecdh_psi.cc new file mode 100644 index 00000000..ee21926d --- /dev/null +++ b/examples/psi/ecdh_psi.cc @@ -0,0 +1,44 @@ +// Copyright 2023 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 "examples/psi/ecdh_psi.h" + +#include +#include + +#include "yacl/crypto/ecc/ec_point.h" +#include "yacl/crypto/ecc/ecc_spi.h" +#include "yacl/link/link.h" +#include "yacl/secparam.h" + +namespace examples::psi { + +void EcdhPsi::MaskStrings(absl::Span in, + absl::Span out) { + YACL_ENFORCE(in.size() == out.size()); + for (size_t i = 0; i < in.size(); ++i) { + out[i] = ec_->HashToCurve(yc::HashToCurveStrategy::Autonomous, in[i]); + ec_->MulInplace(&out[i], sk_); + } +} + +void EcdhPsi::MaskEcPoints(absl::Span in, + absl::Span out) { + YACL_ENFORCE(in.size() == out.size()); + for (size_t i = 0; i < in.size(); ++i) { + out[i] = ec_->SerializePoint(ec_->Mul(in[i], sk_)); + } +} + +} // namespace examples::psi diff --git a/examples/psi/ecdh_psi.h b/examples/psi/ecdh_psi.h new file mode 100644 index 00000000..d2bb0ab3 --- /dev/null +++ b/examples/psi/ecdh_psi.h @@ -0,0 +1,58 @@ +// Copyright 2023 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 + +#include "yacl/crypto/ecc/ec_point.h" +#include "yacl/crypto/ecc/ecc_spi.h" +#include "yacl/link/link.h" +#include "yacl/secparam.h" + +namespace examples::psi { + +namespace yc = yacl::crypto; + +// An example of PSI protocol +// +// NOTE: this PSI protocol is designed solely for demonstation and is not ready, +// or designed for production use, please do not use this in production. +// +// NOTE: we recommend user to use https://github.com/secretflow/psi +// +class EcdhPsi { + public: + EcdhPsi() { + // Use FourQ curve + ec_ = yc::EcGroupFactory::Instance().Create(/* curve name */ "FourQ"); + + // Generate random key + yc::MPInt::RandomLtN(ec_->GetOrder(), &sk_); + } + + // Mask input strings with secret key, and outputs the EcPoint results + void MaskStrings(absl::Span in, absl::Span out); + + // Mask input EcPoints with secret key, and outputs the serialized + // EcPoint strings + void MaskEcPoints(absl::Span in, absl::Span out); + + private: + yc::MPInt sk_; // secret key + std::shared_ptr ec_; // ec group +}; + +} // namespace examples::psi diff --git a/examples/psi/ecdh_psi_test.cc b/examples/psi/ecdh_psi_test.cc new file mode 100644 index 00000000..5f9a78df --- /dev/null +++ b/examples/psi/ecdh_psi_test.cc @@ -0,0 +1,104 @@ +// Copyright 2023 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 "examples/psi/ecdh_psi.h" + +#include + +#include "gtest/gtest.h" + +#include "yacl/crypto/hash/hash_utils.h" + +namespace examples::psi { + +namespace { +std::vector CreateRangeItems(size_t begin, size_t size) { + std::vector ret; + for (size_t i = 0; i < size; i++) { + ret.push_back(std::to_string(begin + i)); + } + return ret; +} +inline std::vector GetIntersectionIdx( + const std::vector &x, const std::vector &y) { + std::set set(x.begin(), x.end()); + std::vector ret; + for (size_t i = 0; i < y.size(); ++i) { + if (set.count(y[i]) != 0) { + ret.push_back(i); + } + } + return ret; +} +} // namespace + +TEST(PsiTest, Works) { + size_t n = 4; + auto x = CreateRangeItems(0, n); + auto y = CreateRangeItems(3, n); + + EcdhPsi alice; + EcdhPsi bob; + + // ------------------- + // Step 1 + // ------------------- + std::vector x_points(n); + // x_points = H(x) ^ {alice_sk} + alice.MaskStrings(absl::MakeSpan(x), absl::MakeSpan(x_points)); + + std::vector y_points(n); + // y_points = H(y) ^ {bob_sk} + bob.MaskStrings(absl::MakeSpan(y), absl::MakeSpan(y_points)); + + // ------------------- + // Step 2 + // ------------------- + // + // Alice send x_points to bob, and bob send y_points to alice + // + // ... code here (omitted) ... + // + // You may mannually send the EcPoints through yacl::link::Context, which + // handles an RPC channel, see: yacl/link/context.h. You may also use any + // method that you like to let Alice talk to Bob. Remember the communication + // channel needs to be a secure P2P channel. + // + // Since most of communication methods only accept strings or bytes, you may + // serialize EcPoints by calling ec_->SerializePoint(/* ec points here */). + // see: yacl/ecc/ecc_spi.h for more details. + + // ------------------- + // Step 3 + // ------------------- + std::vector y_str(n); + // y_str = y_points ^ {alice_sk} + alice.MaskEcPoints(absl::MakeSpan(y_points), absl::MakeSpan(y_str)); + + std::vector x_str(n); + // x_str = x_points ^ {bob_sk} + bob.MaskEcPoints(absl::MakeSpan(x_points), absl::MakeSpan(x_str)); + + /* check results */ + auto compare = GetIntersectionIdx(x, y); // result + auto z = GetIntersectionIdx(x_str, y_str); + + EXPECT_EQ(compare.size(), z.size()); + + for (size_t i = 0; i < z.size(); ++i) { + EXPECT_EQ(compare[i], z[i]); + } +} + +} // namespace examples::psi diff --git a/yacl/kernel/algorithms/BUILD.bazel b/yacl/kernel/algorithms/BUILD.bazel index b8dd5f49..766386d5 100644 --- a/yacl/kernel/algorithms/BUILD.bazel +++ b/yacl/kernel/algorithms/BUILD.bazel @@ -16,29 +16,6 @@ load("//bazel:yacl.bzl", "AES_COPT_FLAGS", "yacl_cc_library", "yacl_cc_test") package(default_visibility = ["//visibility:public"]) -yacl_cc_library( - name = "ot_store", - srcs = ["ot_store.cc"], - hdrs = ["ot_store.h"], - deps = [ - "//yacl/base:aligned_vector", - "//yacl/base:dynamic_bitset", - "//yacl/base:int128", - "//yacl/crypto/rand", - "//yacl/crypto/tools:prg", - "//yacl/link:context", - ], -) - -yacl_cc_test( - name = "ot_store_test", - srcs = ["ot_store_test.cc"], - deps = [ - ":ot_store", - "//yacl/link:test_util", - ], -) - yacl_cc_library( name = "base_ot_interface", hdrs = ["base_ot_interface.h"], @@ -86,7 +63,7 @@ yacl_cc_library( deps = [ "//yacl:secparam", "//yacl/base:exception", - "//yacl/kernel/algorithms:ot_store", + "//yacl/kernel/type:ot_store_utils", "//yacl/link", "@com_google_absl//absl/types:span", ] + select({ @@ -114,11 +91,11 @@ yacl_cc_library( srcs = ["iknp_ote.cc"], hdrs = ["iknp_ote.h"], deps = [ - ":ot_store", "//yacl:secparam", "//yacl/crypto/tools:crhash", "//yacl/crypto/tools:prg", "//yacl/crypto/tools:rp", + "//yacl/kernel/type:ot_store_utils", "//yacl/link", "//yacl/utils:matrix_utils", ], @@ -140,7 +117,6 @@ yacl_cc_library( hdrs = ["kkrt_ote.h"], copts = AES_COPT_FLAGS, deps = [ - ":ot_store", "//yacl:secparam", "//yacl/base:exception", "//yacl/base:int128", @@ -150,6 +126,7 @@ yacl_cc_library( "//yacl/crypto/tools:prg", "//yacl/crypto/tools:ro", "//yacl/crypto/tools:rp", + "//yacl/kernel/type:ot_store_utils", "//yacl/link", "//yacl/utils:matrix_utils", ], @@ -171,7 +148,6 @@ yacl_cc_library( hdrs = ["sgrr_ote.h"], copts = AES_COPT_FLAGS, deps = [ - ":ot_store", "//yacl:secparam", "//yacl/crypto/aes:aes_opt", "//yacl/crypto/rand", @@ -179,6 +155,7 @@ yacl_cc_library( "//yacl/crypto/tools:prg", "//yacl/crypto/tools:ro", "//yacl/crypto/tools:rp", + "//yacl/kernel/type:ot_store_utils", "//yacl/link", "//yacl/math:gadget", "@com_google_absl//absl/numeric:bits", @@ -201,7 +178,6 @@ yacl_cc_library( hdrs = ["gywz_ote.h"], copts = AES_COPT_FLAGS, deps = [ - ":ot_store", "//yacl:secparam", "//yacl/base:aligned_vector", "//yacl/crypto/aes:aes_opt", @@ -209,6 +185,7 @@ yacl_cc_library( "//yacl/crypto/tools:crhash", "//yacl/crypto/tools:prg", "//yacl/crypto/tools:rp", + "//yacl/kernel/type:ot_store_utils", "//yacl/link", "//yacl/math:gadget", "@com_google_absl//absl/numeric:bits", @@ -237,7 +214,6 @@ yacl_cc_library( ], copts = AES_COPT_FLAGS, deps = [ - ":ot_store", "//yacl:secparam", "//yacl/base:exception", "//yacl/crypto/hash:hash_utils", @@ -248,6 +224,7 @@ yacl_cc_library( "//yacl/kernel/algorithms:gywz_ote", "//yacl/kernel/algorithms:sgrr_ote", "//yacl/kernel/code:linear_code", + "//yacl/kernel/type:ot_store_utils", "//yacl/link", "//yacl/math:gadget", "//yacl/math/f2k", @@ -271,7 +248,6 @@ yacl_cc_library( hdrs = ["kos_ote.h"], copts = AES_COPT_FLAGS, deps = [ - ":ot_store", "//yacl:secparam", "//yacl/base:dynamic_bitset", "//yacl/base:exception", @@ -281,6 +257,7 @@ yacl_cc_library( "//yacl/crypto/tools:crhash", "//yacl/crypto/tools:prg", "//yacl/crypto/tools:rp", + "//yacl/kernel/type:ot_store_utils", "//yacl/link", "//yacl/math/f2k", "//yacl/utils:matrix_utils", @@ -304,7 +281,6 @@ yacl_cc_library( copts = AES_COPT_FLAGS, deps = [ ":base_ot", - ":ot_store", ":sgrr_ote", "//yacl/base:exception", "//yacl/base:int128", @@ -312,6 +288,7 @@ yacl_cc_library( "//yacl/crypto/tools:crhash", "//yacl/crypto/tools:prg", "//yacl/crypto/tools:rp", + "//yacl/kernel/type:ot_store_utils", "//yacl/link", "//yacl/math/f2k", "//yacl/utils:matrix_utils", @@ -346,8 +323,8 @@ yacl_cc_library( "//yacl/crypto/rand", "//yacl/crypto/tools:crhash", "//yacl/kernel/algorithms:gywz_ote", - "//yacl/kernel/algorithms:ot_store", "//yacl/kernel/algorithms:sgrr_ote", + "//yacl/kernel/type:ot_store_utils", "//yacl/math:gadget", "//yacl/math/f2k", ], @@ -376,8 +353,8 @@ yacl_cc_library( "//yacl/base:dynamic_bitset", "//yacl/base:int128", "//yacl/crypto/rand", - "//yacl/kernel/algorithms:ot_store", "//yacl/kernel/algorithms:softspoken_ote", + "//yacl/kernel/type:ot_store_utils", "//yacl/math:gadget", "//yacl/math/f2k", "//yacl/utils:serialize", @@ -410,7 +387,7 @@ yacl_cc_library( "//yacl/crypto/rand", "//yacl/crypto/tools:common", "//yacl/kernel/algorithms:mpfss", - "//yacl/kernel/algorithms:ot_store", + "//yacl/kernel/type:ot_store_utils", "//yacl/math:gadget", "//yacl/math/f2k", "//yacl/utils:serialize", @@ -442,11 +419,11 @@ yacl_cc_library( "//yacl/base:aligned_vector", "//yacl/base:dynamic_bitset", "//yacl/base:int128", - "//yacl/kernel/algorithms:ot_store", "//yacl/kernel/algorithms:softspoken_ote", "//yacl/kernel/code:code_interface", "//yacl/kernel/code:ea_code", "//yacl/kernel/code:silver_code", + "//yacl/kernel/type:ot_store_utils", "//yacl/link:context", ], ) diff --git a/yacl/kernel/algorithms/base_ot.h b/yacl/kernel/algorithms/base_ot.h index 001ba918..b8e761af 100644 --- a/yacl/kernel/algorithms/base_ot.h +++ b/yacl/kernel/algorithms/base_ot.h @@ -21,7 +21,7 @@ #include "yacl/base/dynamic_bitset.h" #include "yacl/base/int128.h" -#include "yacl/kernel/algorithms/ot_store.h" +#include "yacl/kernel/type/ot_store_utils.h" #include "yacl/link/link.h" #include "yacl/secparam.h" diff --git a/yacl/kernel/algorithms/base_vole.h b/yacl/kernel/algorithms/base_vole.h index 29b79cd0..7b54dab3 100644 --- a/yacl/kernel/algorithms/base_vole.h +++ b/yacl/kernel/algorithms/base_vole.h @@ -20,8 +20,8 @@ #include "yacl/math/gadget.h" /* submodules */ -#include "yacl/kernel/algorithms/ot_store.h" #include "yacl/kernel/algorithms/softspoken_ote.h" +#include "yacl/kernel/type/ot_store_utils.h" #include "yacl/secparam.h" YACL_MODULE_DECLARE("base_vole", SecParam::C::INF, SecParam::S::INF); diff --git a/yacl/kernel/algorithms/ferret_ote.h b/yacl/kernel/algorithms/ferret_ote.h index c33452b1..c7249350 100644 --- a/yacl/kernel/algorithms/ferret_ote.h +++ b/yacl/kernel/algorithms/ferret_ote.h @@ -19,7 +19,7 @@ #include #include -#include "yacl/kernel/algorithms/ot_store.h" +#include "yacl/kernel/type/ot_store_utils.h" #include "yacl/math/gadget.h" #include "yacl/secparam.h" #include "yacl/utils/cuckoo_index.h" diff --git a/yacl/kernel/algorithms/ferret_ote_test.cc b/yacl/kernel/algorithms/ferret_ote_test.cc index ea1c3c81..e0fac237 100644 --- a/yacl/kernel/algorithms/ferret_ote_test.cc +++ b/yacl/kernel/algorithms/ferret_ote_test.cc @@ -27,7 +27,7 @@ #include "yacl/base/int128.h" #include "yacl/crypto/rand/rand.h" #include "yacl/crypto/tools/prg.h" -#include "yacl/kernel/algorithms/ot_store.h" +#include "yacl/kernel/type/ot_store_utils.h" #include "yacl/link/test_util.h" namespace yacl::crypto { diff --git a/yacl/kernel/algorithms/gywz_ote.h b/yacl/kernel/algorithms/gywz_ote.h index 49dad696..9aadea43 100644 --- a/yacl/kernel/algorithms/gywz_ote.h +++ b/yacl/kernel/algorithms/gywz_ote.h @@ -27,7 +27,7 @@ #include "yacl/crypto/rand/rand.h" #include "yacl/crypto/tools/crhash.h" #include "yacl/crypto/tools/prg.h" -#include "yacl/kernel/algorithms/ot_store.h" +#include "yacl/kernel/type/ot_store_utils.h" /* security parameter declaration */ YACL_MODULE_DECLARE("gywz_ote", SecParam::C::INF, SecParam::S::INF); diff --git a/yacl/kernel/algorithms/gywz_ote_test.cc b/yacl/kernel/algorithms/gywz_ote_test.cc index 0e19947d..923f7d2c 100644 --- a/yacl/kernel/algorithms/gywz_ote_test.cc +++ b/yacl/kernel/algorithms/gywz_ote_test.cc @@ -22,7 +22,7 @@ #include "yacl/base/exception.h" #include "yacl/crypto/rand/rand.h" -#include "yacl/kernel/algorithms/ot_store.h" +#include "yacl/kernel/type/ot_store_utils.h" #include "yacl/link/test_util.h" #include "yacl/math/gadget.h" diff --git a/yacl/kernel/algorithms/iknp_ote.h b/yacl/kernel/algorithms/iknp_ote.h index c53cdb98..023cd219 100644 --- a/yacl/kernel/algorithms/iknp_ote.h +++ b/yacl/kernel/algorithms/iknp_ote.h @@ -20,7 +20,7 @@ #include "absl/types/span.h" #include "yacl/base/dynamic_bitset.h" -#include "yacl/kernel/algorithms/ot_store.h" +#include "yacl/kernel/type/ot_store_utils.h" #include "yacl/link/link.h" #include "yacl/secparam.h" #include "yacl/utils/matrix_utils.h" diff --git a/yacl/kernel/algorithms/kkrt_ote.h b/yacl/kernel/algorithms/kkrt_ote.h index a03421b0..21d108b1 100644 --- a/yacl/kernel/algorithms/kkrt_ote.h +++ b/yacl/kernel/algorithms/kkrt_ote.h @@ -19,7 +19,7 @@ #include "absl/types/span.h" -#include "yacl/kernel/algorithms/ot_store.h" +#include "yacl/kernel/type/ot_store_utils.h" #include "yacl/link/link.h" #include "yacl/secparam.h" diff --git a/yacl/kernel/algorithms/kkrt_ote_test.cc b/yacl/kernel/algorithms/kkrt_ote_test.cc index d302aa67..05a2a98f 100644 --- a/yacl/kernel/algorithms/kkrt_ote_test.cc +++ b/yacl/kernel/algorithms/kkrt_ote_test.cc @@ -24,7 +24,7 @@ #include "yacl/base/exception.h" #include "yacl/crypto/rand/rand.h" #include "yacl/crypto/tools/prg.h" -#include "yacl/kernel/algorithms/ot_store.h" +#include "yacl/kernel/type/ot_store_utils.h" #include "yacl/link/test_util.h" namespace yacl::crypto { diff --git a/yacl/kernel/algorithms/kos_ote.h b/yacl/kernel/algorithms/kos_ote.h index 750ff854..2de34c6a 100644 --- a/yacl/kernel/algorithms/kos_ote.h +++ b/yacl/kernel/algorithms/kos_ote.h @@ -19,7 +19,7 @@ #include "absl/types/span.h" #include "yacl/base/dynamic_bitset.h" -#include "yacl/kernel/algorithms/ot_store.h" +#include "yacl/kernel/type/ot_store_utils.h" #include "yacl/link/link.h" #include "yacl/secparam.h" diff --git a/yacl/kernel/algorithms/mp_vole.h b/yacl/kernel/algorithms/mp_vole.h index 4de79ba3..dfa5b039 100644 --- a/yacl/kernel/algorithms/mp_vole.h +++ b/yacl/kernel/algorithms/mp_vole.h @@ -21,7 +21,7 @@ #include "yacl/crypto/rand/rand.h" #include "yacl/crypto/tools/common.h" #include "yacl/kernel/algorithms/mpfss.h" -#include "yacl/kernel/algorithms/ot_store.h" +#include "yacl/kernel/type/ot_store_utils.h" #include "yacl/math/f2k/f2k_utils.h" #include "yacl/math/gadget.h" #include "yacl/secparam.h" diff --git a/yacl/kernel/algorithms/mpfss.h b/yacl/kernel/algorithms/mpfss.h index 228b89d9..bd04ac7b 100644 --- a/yacl/kernel/algorithms/mpfss.h +++ b/yacl/kernel/algorithms/mpfss.h @@ -19,7 +19,7 @@ /* submodules */ #include "yacl/crypto/rand/rand.h" -#include "yacl/kernel/algorithms/ot_store.h" +#include "yacl/kernel/type/ot_store_utils.h" #include "yacl/secparam.h" /* security parameter declaration */ diff --git a/yacl/kernel/algorithms/sgrr_ote.h b/yacl/kernel/algorithms/sgrr_ote.h index 729f003b..fd468683 100644 --- a/yacl/kernel/algorithms/sgrr_ote.h +++ b/yacl/kernel/algorithms/sgrr_ote.h @@ -18,7 +18,7 @@ #include "absl/types/span.h" -#include "yacl/kernel/algorithms/ot_store.h" +#include "yacl/kernel/type/ot_store_utils.h" #include "yacl/link/link.h" #include "yacl/secparam.h" diff --git a/yacl/kernel/algorithms/sgrr_ote_test.cc b/yacl/kernel/algorithms/sgrr_ote_test.cc index 95c58f8d..20309948 100644 --- a/yacl/kernel/algorithms/sgrr_ote_test.cc +++ b/yacl/kernel/algorithms/sgrr_ote_test.cc @@ -23,7 +23,7 @@ #include "gtest/gtest.h" #include "yacl/base/exception.h" -#include "yacl/kernel/algorithms/ot_store.h" +#include "yacl/kernel/type/ot_store_utils.h" #include "yacl/link/test_util.h" #include "yacl/math/gadget.h" diff --git a/yacl/kernel/algorithms/softspoken_ote.cc b/yacl/kernel/algorithms/softspoken_ote.cc index 12ff4379..f861ff6e 100644 --- a/yacl/kernel/algorithms/softspoken_ote.cc +++ b/yacl/kernel/algorithms/softspoken_ote.cc @@ -24,7 +24,7 @@ #include "yacl/base/byte_container_view.h" #include "yacl/base/exception.h" #include "yacl/crypto/tools/common.h" -#include "yacl/kernel/algorithms/ot_store.h" +#include "yacl/kernel/type/ot_store_utils.h" #include "yacl/math/f2k/f2k.h" #include "yacl/utils/matrix_utils.h" #include "yacl/utils/serialize.h" diff --git a/yacl/kernel/algorithms/softspoken_ote.h b/yacl/kernel/algorithms/softspoken_ote.h index 42d18f06..b977b844 100644 --- a/yacl/kernel/algorithms/softspoken_ote.h +++ b/yacl/kernel/algorithms/softspoken_ote.h @@ -32,8 +32,8 @@ #include "yacl/crypto/tools/prg.h" #include "yacl/crypto/tools/rp.h" #include "yacl/kernel/algorithms/base_ot.h" -#include "yacl/kernel/algorithms/ot_store.h" #include "yacl/kernel/algorithms/sgrr_ote.h" +#include "yacl/kernel/type/ot_store_utils.h" /* security parameter declaration */ YACL_MODULE_DECLARE("softspoken_ote", SecParam::C::k128, SecParam::S::INF); diff --git a/yacl/kernel/algorithms/softspoken_ote_test.cc b/yacl/kernel/algorithms/softspoken_ote_test.cc index 24c461fd..52d86b06 100644 --- a/yacl/kernel/algorithms/softspoken_ote_test.cc +++ b/yacl/kernel/algorithms/softspoken_ote_test.cc @@ -23,7 +23,7 @@ #include "yacl/base/exception.h" #include "yacl/crypto/rand/rand.h" -#include "yacl/kernel/algorithms/ot_store.h" +#include "yacl/kernel/type/ot_store_utils.h" #include "yacl/link/test_util.h" namespace yacl::crypto { diff --git a/yacl/kernel/benchmark/ot_bench.h b/yacl/kernel/benchmark/ot_bench.h index 26b88cef..ade0b74f 100644 --- a/yacl/kernel/benchmark/ot_bench.h +++ b/yacl/kernel/benchmark/ot_bench.h @@ -31,9 +31,9 @@ #include "yacl/kernel/algorithms/iknp_ote.h" #include "yacl/kernel/algorithms/kkrt_ote.h" #include "yacl/kernel/algorithms/kos_ote.h" -#include "yacl/kernel/algorithms/ot_store.h" #include "yacl/kernel/algorithms/sgrr_ote.h" #include "yacl/kernel/algorithms/softspoken_ote.h" +#include "yacl/kernel/type/ot_store_utils.h" #include "yacl/link/test_util.h" #include "yacl/utils/matrix_utils.h" diff --git a/yacl/kernel/ot_kernel.cc b/yacl/kernel/ot_kernel.cc index 923dbd9c..4c8d8456 100644 --- a/yacl/kernel/ot_kernel.cc +++ b/yacl/kernel/ot_kernel.cc @@ -25,8 +25,8 @@ #include "yacl/crypto/tools/ro.h" #include "yacl/kernel/algorithms/base_ot.h" #include "yacl/kernel/algorithms/ferret_ote.h" -#include "yacl/kernel/algorithms/ot_store.h" #include "yacl/kernel/algorithms/softspoken_ote.h" +#include "yacl/kernel/type/ot_store_utils.h" #include "yacl/link/context.h" #include "yacl/secparam.h" #include "yacl/utils/parallel.h" diff --git a/yacl/kernel/ot_kernel.h b/yacl/kernel/ot_kernel.h index 697b8fb0..2199eec3 100644 --- a/yacl/kernel/ot_kernel.h +++ b/yacl/kernel/ot_kernel.h @@ -18,8 +18,8 @@ #include #include "yacl/base/dynamic_bitset.h" -#include "yacl/kernel/algorithms/ot_store.h" #include "yacl/kernel/kernel.h" +#include "yacl/kernel/type/ot_store_utils.h" /* submodules */ #include "yacl/kernel/algorithms/base_ot.h" @@ -77,7 +77,7 @@ class OtKernel : SingleThreadKernel { // delta-correlated, which means m0 xor m1 = delta // // Note: all correlated ot instances are stored in *compact mode* - // see: yacl/kernel/algorithms/ot_store.h + // see: yacl/kernel/type/ot_store_utils.h void eval_cot_random_choice(const std::shared_ptr& lctx, uint64_t ot_num, /* compact mode */ OtSendStore* out); diff --git a/yacl/kernel/ot_kernel_bench.cc b/yacl/kernel/ot_kernel_bench.cc index 383f9680..6ea0ee37 100644 --- a/yacl/kernel/ot_kernel_bench.cc +++ b/yacl/kernel/ot_kernel_bench.cc @@ -24,9 +24,10 @@ static void BM_Ferret_OT_single_thread(benchmark::State& state) { for (auto _ : state) { state.PauseTiming(); { - const size_t num_ot = 1 << 24; + const size_t num_ot = state.range(0); OtSendStore ot_send(num_ot, OtStoreType::Compact); // placeholder OtRecvStore ot_recv(num_ot, OtStoreType::Compact); // placeholder + OtKernel kernel0(OtKernel::Role::Sender, OtKernel::ExtAlgorithm::Ferret); OtKernel kernel1(OtKernel::Role::Receiver, OtKernel::ExtAlgorithm::Ferret); @@ -54,7 +55,7 @@ static void BM_SoftSpoken_OT_single_thread(benchmark::State& state) { for (auto _ : state) { state.PauseTiming(); { - const size_t num_ot = 1 << 24; + const size_t num_ot = state.range(0); OtSendStore ot_send(num_ot, OtStoreType::Compact); // placeholder OtRecvStore ot_recv(num_ot, OtStoreType::Compact); // placeholder OtKernel kernel0(OtKernel::Role::Sender, @@ -83,9 +84,18 @@ static void BM_SoftSpoken_OT_single_thread(benchmark::State& state) { BENCHMARK(yacl::crypto::BM_Ferret_OT_single_thread) ->Iterations(1) + ->Arg(1 << 20) + ->Arg(1 << 22) + ->Arg(1 << 24) + ->Arg(1 << 26) ->Unit(benchmark::kMillisecond); BENCHMARK(yacl::crypto::BM_SoftSpoken_OT_single_thread) ->Iterations(1) + ->Arg(1 << 20) + ->Arg(1 << 22) + ->Arg(1 << 24) + ->Arg(1 << 26) ->Unit(benchmark::kMillisecond); + BENCHMARK_MAIN(); diff --git a/yacl/kernel/ot_kernel_test.cc b/yacl/kernel/ot_kernel_test.cc index 5086804a..a65fbeff 100644 --- a/yacl/kernel/ot_kernel_test.cc +++ b/yacl/kernel/ot_kernel_test.cc @@ -21,7 +21,7 @@ #include "gtest/gtest.h" -#include "yacl/kernel/algorithms/ot_store.h" +#include "yacl/kernel/type/ot_store_utils.h" #include "yacl/link/test_util.h" namespace yacl::crypto { diff --git a/yacl/kernel/type/BUILD.bazel b/yacl/kernel/type/BUILD.bazel new file mode 100644 index 00000000..355ef9c9 --- /dev/null +++ b/yacl/kernel/type/BUILD.bazel @@ -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. + +load("//bazel:yacl.bzl", "yacl_cc_library", "yacl_cc_test") + +package(default_visibility = ["//visibility:public"]) + +yacl_cc_library( + name = "slice_base", + srcs = ["slice_base.cc"], + hdrs = [ + "slice_base.h", + ], + deps = [ + "//yacl/base:exception", + ], +) + +yacl_cc_library( + name = "ot_store", + srcs = ["ot_store.cc"], + hdrs = [ + "ot_store.h", + ], + deps = [ + ":slice_base", + "//yacl/base:aligned_vector", + "//yacl/base:dynamic_bitset", + "//yacl/base:int128", + "//yacl/crypto/rand", + "//yacl/crypto/tools:prg", + "//yacl/link:context", + ], +) + +yacl_cc_library( + name = "ot_store_utils", + srcs = ["ot_store_utils.cc"], + hdrs = [ + "ot_store_utils.h", + ], + deps = [ + ":ot_store", + ], +) + +yacl_cc_test( + name = "ot_store_test", + srcs = ["ot_store_test.cc"], + deps = [ + ":ot_store_utils", + "//yacl/link:test_util", + ], +) diff --git a/yacl/kernel/algorithms/ot_store.cc b/yacl/kernel/type/ot_store.cc similarity index 56% rename from yacl/kernel/algorithms/ot_store.cc rename to yacl/kernel/type/ot_store.cc index 72c43d10..21bc9da9 100644 --- a/yacl/kernel/algorithms/ot_store.cc +++ b/yacl/kernel/type/ot_store.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "yacl/kernel/algorithms/ot_store.h" +#include "yacl/kernel/type/ot_store.h" #include #include @@ -24,58 +24,6 @@ namespace yacl::crypto { -//---------------------------------- -// Slice Base -//---------------------------------- - -void SliceBase::ConsistencyCheck() const { - YACL_ENFORCE( - internal_use_size_ > 0, - "Internal slice size shoud be greater than 0, but got slice size: {}", - internal_use_size_); - YACL_ENFORCE( - internal_buf_size_ > 0, - "Internal buffer size shoud be greater than 0, but got buffer size: {}", - internal_buf_size_); - YACL_ENFORCE(internal_buf_size_ >= internal_use_size_, - "Buffer size should great or equal to slice size, got {} >= {}", - internal_buf_size_, internal_use_size_); - YACL_ENFORCE(internal_buf_size_ >= internal_buf_ctr_, "Slice out of range!"); -} - -void SliceBase::InitCtrs(uint64_t use_ctr, uint64_t use_size, uint64_t buf_ctr, - uint64_t buf_size) { - internal_use_ctr_ = use_ctr; - internal_use_size_ = use_size; - internal_buf_ctr_ = buf_ctr; - internal_buf_size_ = buf_size; - ConsistencyCheck(); -} - -uint64_t SliceBase::GetBufIdx(uint64_t slice_idx) const { - YACL_ENFORCE(internal_use_size_ > slice_idx, - "Slice index out of range, slice size: {}, but got index: {}", - internal_use_size_, slice_idx); - return internal_use_ctr_ + slice_idx; -} - -void SliceBase::IncreaseBufCtr(uint64_t size) { - YACL_ENFORCE( - internal_buf_size_ - internal_buf_ctr_ >= size, - "Increase buffer counter failed, not enough space, buffer left space: " - "{}, but tried increase with size: {}", - internal_buf_size_ - internal_buf_ctr_, size); - internal_buf_ctr_ += size; -} - -void SliceBase::Reset() { - internal_use_ctr_ = 0; - internal_use_size_ = 0; - internal_buf_ctr_ = 0; - internal_buf_size_ = 0; - ConsistencyCheck(); -} - //---------------------------------- // OtRecvStore //---------------------------------- @@ -244,86 +192,6 @@ void OtRecvStore::ConsistencyCheck() const { } } -OtRecvStore MakeOtRecvStore(const dynamic_bitset& choices, - const std::vector& blocks) { - auto tmp1_ptr = std::make_shared>(choices); // copy - auto tmp2_ptr = - std::make_shared>(blocks.size()); - std::memcpy(tmp2_ptr->data(), blocks.data(), - blocks.size() * sizeof(uint128_t)); // copy - - return {tmp1_ptr, tmp2_ptr, 0, tmp1_ptr->size(), 0, - tmp1_ptr->size(), OtStoreType::Normal}; -} - -OtRecvStore MakeOtRecvStore(const dynamic_bitset& choices, - const UninitAlignedVector& blocks) { - auto tmp1_ptr = std::make_shared>(choices); // copy - auto tmp2_ptr = - std::make_shared>(blocks); // copy - - return {tmp1_ptr, tmp2_ptr, 0, tmp1_ptr->size(), 0, - tmp1_ptr->size(), OtStoreType::Normal}; -} - -OtRecvStore MakeCompactOtRecvStore(const std::vector& blocks) { - auto tmp_ptr = - std::make_shared>(blocks.size()); - std::memcpy(tmp_ptr->data(), blocks.data(), - blocks.size() * sizeof(uint128_t)); // copy - - return {nullptr, - tmp_ptr, - 0, - tmp_ptr->size(), - 0, - tmp_ptr->size(), - OtStoreType::Compact}; -} - -OtRecvStore MakeCompactOtRecvStore( - const UninitAlignedVector& blocks) { - auto tmp_ptr = - std::make_shared>(blocks); // copy - - return {nullptr, - tmp_ptr, - 0, - tmp_ptr->size(), - 0, - tmp_ptr->size(), - OtStoreType::Compact}; -} - -OtRecvStore MakeCompactOtRecvStore(std::vector&& blocks) { - auto tmp_ptr = - std::make_shared>(blocks.size()); - - std::memcpy(tmp_ptr->data(), blocks.data(), - blocks.size() * sizeof(uint128_t)); // copy - - return {nullptr, - tmp_ptr, - 0, - tmp_ptr->size(), - 0, - tmp_ptr->size(), - OtStoreType::Compact}; -} - -OtRecvStore MakeCompactOtRecvStore(UninitAlignedVector&& blocks) { - auto tmp_ptr = std::make_shared>( - std::move(blocks)); // move - - return {nullptr, - tmp_ptr, - 0, - tmp_ptr->size(), - 0, - tmp_ptr->size(), - OtStoreType::Compact}; -} - //---------------------------------- // OtSendStore //---------------------------------- @@ -483,161 +351,4 @@ UninitAlignedVector OtSendStore::CopyCotBlkBuf() const { blk_buf_->begin() + internal_buf_ctr_ + internal_use_size_}; } -OtSendStore MakeOtSendStore( - const std::vector>& blocks) { - // warning: copy - auto buf_ptr = - std::make_shared>(blocks.size() * 2); - memcpy(buf_ptr->data(), blocks.data(), buf_ptr->size() * sizeof(uint128_t)); - - return {buf_ptr, - 0, - 0, - blocks.size() * 2, - 0, - blocks.size() * 2, - OtStoreType::Normal}; -} - -OtSendStore MakeOtSendStore( - const UninitAlignedVector>& blocks) { - // warning: copy - auto buf_ptr = - std::make_shared>(blocks.size() * 2); - memcpy(buf_ptr->data(), blocks.data(), buf_ptr->size() * sizeof(uint128_t)); - - return {buf_ptr, - 0, - 0, - blocks.size() * 2, - 0, - blocks.size() * 2, - OtStoreType::Normal}; -} - -OtSendStore MakeCompactOtSendStore(const std::vector& blocks, - uint128_t delta) { - // warning: copy - auto buf_ptr = - std::make_shared>(blocks.size()); - std::memcpy(buf_ptr->data(), blocks.data(), - blocks.size() * sizeof(uint128_t)); // copy - - return {buf_ptr, - delta, - 0, - buf_ptr->size(), - 0, - buf_ptr->size(), - OtStoreType::Compact}; -} - -OtSendStore MakeCompactOtSendStore(const UninitAlignedVector& blocks, - uint128_t delta) { - // warning: copy - auto buf_ptr = std::make_shared>(blocks); - - return {buf_ptr, - delta, - 0, - buf_ptr->size(), - 0, - buf_ptr->size(), - OtStoreType::Compact}; -} - -OtSendStore MakeCompactOtSendStore(std::vector&& blocks, - uint128_t delta) { - auto buf_ptr = - std::make_shared>(blocks.size()); - std::memcpy(buf_ptr->data(), blocks.data(), - blocks.size() * sizeof(uint128_t)); // copy - - return {buf_ptr, - delta, - 0, - buf_ptr->size(), - 0, - buf_ptr->size(), - OtStoreType::Compact}; -} - -OtSendStore MakeCompactOtSendStore(UninitAlignedVector&& blocks, - uint128_t delta) { - auto buf_ptr = std::make_shared>( - std::move(blocks)); // move - - return {buf_ptr, - delta, - 0, - buf_ptr->size(), - 0, - buf_ptr->size(), - OtStoreType::Compact}; -} - -MockOtStore MockRots(uint64_t num) { - auto recv_choices = RandBits>(num); - return MockRots(num, recv_choices); -} - -MockOtStore MockRots(uint64_t num, dynamic_bitset choices) { - YACL_ENFORCE(choices.size() == num); - UninitAlignedVector recv_blocks; - UninitAlignedVector> send_blocks; - - Prg gen(FastRandSeed()); - for (uint64_t i = 0; i < num; ++i) { - send_blocks.push_back({gen(), gen()}); - recv_blocks.push_back(send_blocks[i][choices[i]]); - } - - return {MakeOtSendStore(send_blocks), // sender is normal - MakeOtRecvStore(choices, recv_blocks)}; // receiver is normal -} - -MockOtStore MockCots(uint64_t num, uint128_t delta) { - auto recv_choices = RandBits>(num); - return MockCots(num, delta, recv_choices); -} - -MockOtStore MockCots(uint64_t num, uint128_t delta, - dynamic_bitset choices) { - YACL_ENFORCE(choices.size() == num); - UninitAlignedVector recv_blocks; - UninitAlignedVector send_blocks; - - Prg gen(FastRandSeed()); - for (uint64_t i = 0; i < num; ++i) { - auto msg = gen(); - send_blocks.push_back(msg); - if (!choices[i]) { - recv_blocks.push_back(send_blocks[i]); - } else { - recv_blocks.push_back(send_blocks[i] ^ delta); - } - } - - return {MakeCompactOtSendStore(send_blocks, delta), // sender is compact - MakeOtRecvStore(choices, recv_blocks)}; // receiver is normal -} - -MockOtStore MockCompactOts(uint64_t num) { - uint128_t delta = FastRandU128(); - delta |= 0x1; // make sure its last bits = 1; - UninitAlignedVector recv_blocks; - UninitAlignedVector send_blocks; - - Prg gen(FastRandSeed()); - for (uint64_t i = 0; i < num; ++i) { - auto recv_msg = gen(); - auto choice = recv_msg & 0x1; - send_blocks.push_back(recv_msg ^ (choice * delta)); - recv_blocks.push_back(recv_msg); - } - - return {MakeCompactOtSendStore(send_blocks, delta), // sender is compact - MakeCompactOtRecvStore(recv_blocks)}; // receiver is compact -} - } // namespace yacl::crypto diff --git a/yacl/kernel/algorithms/ot_store.h b/yacl/kernel/type/ot_store.h similarity index 58% rename from yacl/kernel/algorithms/ot_store.h rename to yacl/kernel/type/ot_store.h index c898cb41..93a83965 100644 --- a/yacl/kernel/algorithms/ot_store.h +++ b/yacl/kernel/type/ot_store.h @@ -21,72 +21,13 @@ #include "yacl/base/dynamic_bitset.h" #include "yacl/base/exception.h" #include "yacl/base/int128.h" +#include "yacl/kernel/type/slice_base.h" #include "yacl/link/context.h" namespace yacl::crypto { enum class OtStoreType { Normal, Compact }; -class SliceBase { - public: - // setters and getters - bool IsSliced() const { return internal_buf_ctr_ != 0; } - virtual ~SliceBase() = default; - - void ResetSlice() { - internal_use_ctr_ = 0; - internal_use_size_ = internal_buf_size_; - internal_buf_ctr_ = 0; - } - - protected: - uint64_t GetUseCtr() const { return internal_use_ctr_; } - uint64_t GetUseSize() const { return internal_use_size_; } - uint64_t GetBufCtr() const { return internal_buf_ctr_; } - uint64_t GetBufSize() const { return internal_buf_size_; } - virtual void ConsistencyCheck() const; - - // init all interal values - void InitCtrs(uint64_t use_ctr, uint64_t use_size, uint64_t buf_ctr, - uint64_t buf_size); - - // get the internal buffer index from a slice index - uint64_t GetBufIdx(uint64_t slice_idx) const; - - // manually increase the buffer counter by "size" - void IncreaseBufCtr(uint64_t size); - - // reset all pointers - void Reset(); - - // An unused slice looks like the follwoing: - // - // |---------------|-----slice-----|----------------| internal buffer - // a b c d - // - // internal_use_ctr_ = b - // internal_use_size_ = c - b - // internal_buf_ctr_ = b - // internal_buf_size_ = c - // - // where who slice this buffer looks like the following: - // - // - // |---------------|-----slice-----|----------------| internal buffer - // a b c d - // - // internal_use_ctr_ = a - // internal_use_size_ = d - a - // internal_buf_ctr_ = c (since the underlying buffer is already sliced to c) - // internal_buf_size_ = d - a - - uint64_t internal_use_ctr_ = 0; // slice begin position in buffer - uint64_t internal_use_size_ = 0; // allowed slice size (read & wrtie) - uint64_t internal_buf_ctr_ = 0; // buffer use counter (next slice position) - uint64_t internal_buf_size_ = 0; // underlying buf max size - // (will not be affected by slice op) -}; - // OT Receiver (for 1-out-of-2 OT) // // Data structure that stores multiple ot receier's data (a.k.a. the choice and @@ -184,25 +125,6 @@ class OtRecvStore : public SliceBase { // in compact mode }; -// Easier way of generate a ot_store pointer from a given choice buffer and -// a block buffer -OtRecvStore MakeOtRecvStore(const dynamic_bitset& choices, - const UninitAlignedVector& blocks); - -OtRecvStore MakeOtRecvStore(const dynamic_bitset& choices, - const std::vector& blocks); - -// Easier way of generate a compact cot_store pointer from a given block buffer -// Note: Compact ot is correlated-ot (or called delta-ot) -OtRecvStore MakeCompactOtRecvStore( - const UninitAlignedVector& blocks); - -OtRecvStore MakeCompactOtRecvStore(const std::vector& blocks); - -OtRecvStore MakeCompactOtRecvStore(UninitAlignedVector&& blocks); - -OtRecvStore MakeCompactOtRecvStore(std::vector&& blocks); - // OT Sender (for 1-out-of-2 OT) // // Data structure that stores multiple ot sender's data (a.k.a. the ot messages) @@ -272,43 +194,4 @@ class OtSendStore : public SliceBase { BlkBufPtr blk_buf_; // store blocks }; -// Easier way of generate a ot_store pointer from a given blocks buffer -OtSendStore MakeOtSendStore( - const UninitAlignedVector>& blocks); - -OtSendStore MakeOtSendStore( - const std::vector>& blocks); - -// Easier way of generate a compact cot_store pointer from a given blocks -// buffer and cot delta -// Note: Compact ot is correlated-ot (or called delta-ot) -OtSendStore MakeCompactOtSendStore(const std::vector& blocks, - uint128_t delta); - -OtSendStore MakeCompactOtSendStore(const UninitAlignedVector& blocks, - uint128_t delta); - -OtSendStore MakeCompactOtSendStore(std::vector&& blocks, - uint128_t delta); - -OtSendStore MakeCompactOtSendStore(UninitAlignedVector&& blocks, - uint128_t delta); - -// OT Store (for mocking only) -class MockOtStore { - public: - OtSendStore send; - OtRecvStore recv; -}; - -// Locally mock ots -MockOtStore MockRots(uint64_t num); -MockOtStore MockRots(uint64_t num, dynamic_bitset choices); -MockOtStore MockCots(uint64_t num, uint128_t delta); -MockOtStore MockCots(uint64_t num, uint128_t delta, - dynamic_bitset choices); - -// Note: Compact ot is correlated-ot (or called delta-ot) -MockOtStore MockCompactOts(uint64_t num); - } // namespace yacl::crypto diff --git a/yacl/kernel/algorithms/ot_store_test.cc b/yacl/kernel/type/ot_store_test.cc similarity index 99% rename from yacl/kernel/algorithms/ot_store_test.cc rename to yacl/kernel/type/ot_store_test.cc index d1cd8be4..fe5fca21 100644 --- a/yacl/kernel/algorithms/ot_store_test.cc +++ b/yacl/kernel/type/ot_store_test.cc @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "yacl/kernel/algorithms/ot_store.h" - #include #include #include @@ -27,6 +25,7 @@ #include "yacl/base/exception.h" #include "yacl/crypto/rand/rand.h" #include "yacl/crypto/tools/prg.h" +#include "yacl/kernel/type/ot_store_utils.h" #include "yacl/link/test_util.h" namespace yacl::crypto { diff --git a/yacl/kernel/type/ot_store_utils.cc b/yacl/kernel/type/ot_store_utils.cc new file mode 100644 index 00000000..7e49c91d --- /dev/null +++ b/yacl/kernel/type/ot_store_utils.cc @@ -0,0 +1,257 @@ +// 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 "yacl/kernel/type/ot_store_utils.h" + +#include "yacl/crypto/rand/rand.h" +#include "yacl/crypto/tools/prg.h" + +namespace yacl::crypto { + +MockOtStore MockRots(uint64_t num) { + auto recv_choices = RandBits>(num); + return MockRots(num, recv_choices); +} + +MockOtStore MockRots(uint64_t num, dynamic_bitset choices) { + YACL_ENFORCE(choices.size() == num); + UninitAlignedVector recv_blocks; + UninitAlignedVector> send_blocks; + + Prg gen(FastRandSeed()); + for (uint64_t i = 0; i < num; ++i) { + send_blocks.push_back({gen(), gen()}); + recv_blocks.push_back(send_blocks[i][choices[i]]); + } + + return {MakeOtSendStore(send_blocks), // sender is normal + MakeOtRecvStore(choices, recv_blocks)}; // receiver is normal +} + +MockOtStore MockCots(uint64_t num, uint128_t delta) { + auto recv_choices = RandBits>(num); + return MockCots(num, delta, recv_choices); +} + +MockOtStore MockCots(uint64_t num, uint128_t delta, + dynamic_bitset choices) { + YACL_ENFORCE(choices.size() == num); + UninitAlignedVector recv_blocks; + UninitAlignedVector send_blocks; + + Prg gen(FastRandSeed()); + for (uint64_t i = 0; i < num; ++i) { + auto msg = gen(); + send_blocks.push_back(msg); + if (!choices[i]) { + recv_blocks.push_back(send_blocks[i]); + } else { + recv_blocks.push_back(send_blocks[i] ^ delta); + } + } + + return {MakeCompactOtSendStore(send_blocks, delta), // sender is compact + MakeOtRecvStore(choices, recv_blocks)}; // receiver is normal +} + +MockOtStore MockCompactOts(uint64_t num) { + uint128_t delta = FastRandU128(); + delta |= 0x1; // make sure its last bits = 1; + UninitAlignedVector recv_blocks; + UninitAlignedVector send_blocks; + + Prg gen(FastRandSeed()); + for (uint64_t i = 0; i < num; ++i) { + auto recv_msg = gen(); + auto choice = recv_msg & 0x1; + send_blocks.push_back(recv_msg ^ (choice * delta)); + recv_blocks.push_back(recv_msg); + } + + return {MakeCompactOtSendStore(send_blocks, delta), // sender is compact + MakeCompactOtRecvStore(recv_blocks)}; // receiver is compact +} + +OtSendStore MakeOtSendStore( + const std::vector>& blocks) { + // warning: copy + auto buf_ptr = + std::make_shared>(blocks.size() * 2); + memcpy(buf_ptr->data(), blocks.data(), buf_ptr->size() * sizeof(uint128_t)); + + return {buf_ptr, + 0, + 0, + blocks.size() * 2, + 0, + blocks.size() * 2, + OtStoreType::Normal}; +} + +OtSendStore MakeOtSendStore( + const UninitAlignedVector>& blocks) { + // warning: copy + auto buf_ptr = + std::make_shared>(blocks.size() * 2); + memcpy(buf_ptr->data(), blocks.data(), buf_ptr->size() * sizeof(uint128_t)); + + return {buf_ptr, + 0, + 0, + blocks.size() * 2, + 0, + blocks.size() * 2, + OtStoreType::Normal}; +} + +OtSendStore MakeCompactOtSendStore(const std::vector& blocks, + uint128_t delta) { + // warning: copy + auto buf_ptr = + std::make_shared>(blocks.size()); + std::memcpy(buf_ptr->data(), blocks.data(), + blocks.size() * sizeof(uint128_t)); // copy + + return {buf_ptr, + delta, + 0, + buf_ptr->size(), + 0, + buf_ptr->size(), + OtStoreType::Compact}; +} + +OtSendStore MakeCompactOtSendStore(const UninitAlignedVector& blocks, + uint128_t delta) { + // warning: copy + auto buf_ptr = std::make_shared>(blocks); + + return {buf_ptr, + delta, + 0, + buf_ptr->size(), + 0, + buf_ptr->size(), + OtStoreType::Compact}; +} + +OtSendStore MakeCompactOtSendStore(std::vector&& blocks, + uint128_t delta) { + auto buf_ptr = + std::make_shared>(blocks.size()); + std::memcpy(buf_ptr->data(), blocks.data(), + blocks.size() * sizeof(uint128_t)); // copy + + return {buf_ptr, + delta, + 0, + buf_ptr->size(), + 0, + buf_ptr->size(), + OtStoreType::Compact}; +} + +OtSendStore MakeCompactOtSendStore(UninitAlignedVector&& blocks, + uint128_t delta) { + auto buf_ptr = std::make_shared>( + std::move(blocks)); // move + + return {buf_ptr, + delta, + 0, + buf_ptr->size(), + 0, + buf_ptr->size(), + OtStoreType::Compact}; +} +OtRecvStore MakeOtRecvStore(const dynamic_bitset& choices, + const std::vector& blocks) { + auto tmp1_ptr = std::make_shared>(choices); // copy + auto tmp2_ptr = + std::make_shared>(blocks.size()); + std::memcpy(tmp2_ptr->data(), blocks.data(), + blocks.size() * sizeof(uint128_t)); // copy + + return {tmp1_ptr, tmp2_ptr, 0, tmp1_ptr->size(), 0, + tmp1_ptr->size(), OtStoreType::Normal}; +} + +OtRecvStore MakeOtRecvStore(const dynamic_bitset& choices, + const UninitAlignedVector& blocks) { + auto tmp1_ptr = std::make_shared>(choices); // copy + auto tmp2_ptr = + std::make_shared>(blocks); // copy + + return {tmp1_ptr, tmp2_ptr, 0, tmp1_ptr->size(), 0, + tmp1_ptr->size(), OtStoreType::Normal}; +} + +OtRecvStore MakeCompactOtRecvStore(const std::vector& blocks) { + auto tmp_ptr = + std::make_shared>(blocks.size()); + std::memcpy(tmp_ptr->data(), blocks.data(), + blocks.size() * sizeof(uint128_t)); // copy + + return {nullptr, + tmp_ptr, + 0, + tmp_ptr->size(), + 0, + tmp_ptr->size(), + OtStoreType::Compact}; +} + +OtRecvStore MakeCompactOtRecvStore( + const UninitAlignedVector& blocks) { + auto tmp_ptr = + std::make_shared>(blocks); // copy + + return {nullptr, + tmp_ptr, + 0, + tmp_ptr->size(), + 0, + tmp_ptr->size(), + OtStoreType::Compact}; +} + +OtRecvStore MakeCompactOtRecvStore(std::vector&& blocks) { + auto tmp_ptr = + std::make_shared>(blocks.size()); + + std::memcpy(tmp_ptr->data(), blocks.data(), + blocks.size() * sizeof(uint128_t)); // copy + + return {nullptr, + tmp_ptr, + 0, + tmp_ptr->size(), + 0, + tmp_ptr->size(), + OtStoreType::Compact}; +} + +OtRecvStore MakeCompactOtRecvStore(UninitAlignedVector&& blocks) { + auto tmp_ptr = std::make_shared>( + std::move(blocks)); // move + + return {nullptr, + tmp_ptr, + 0, + tmp_ptr->size(), + 0, + tmp_ptr->size(), + OtStoreType::Compact}; +} +} // namespace yacl::crypto diff --git a/yacl/kernel/type/ot_store_utils.h b/yacl/kernel/type/ot_store_utils.h new file mode 100644 index 00000000..1bf2853b --- /dev/null +++ b/yacl/kernel/type/ot_store_utils.h @@ -0,0 +1,80 @@ + +// 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 "yacl/kernel/type/ot_store.h" + +namespace yacl::crypto { + +// Easier way of generate a ot_store pointer from a given choice buffer and +// a block buffer +OtRecvStore MakeOtRecvStore(const dynamic_bitset& choices, + const UninitAlignedVector& blocks); + +OtRecvStore MakeOtRecvStore(const dynamic_bitset& choices, + const std::vector& blocks); + +// Easier way of generate a compact cot_store pointer from a given block buffer +// Note: Compact ot is correlated-ot (or called delta-ot) +OtRecvStore MakeCompactOtRecvStore( + const UninitAlignedVector& blocks); + +OtRecvStore MakeCompactOtRecvStore(const std::vector& blocks); + +OtRecvStore MakeCompactOtRecvStore(UninitAlignedVector&& blocks); + +OtRecvStore MakeCompactOtRecvStore(std::vector&& blocks); + +// Easier way of generate a ot_store pointer from a given blocks buffer +OtSendStore MakeOtSendStore( + const UninitAlignedVector>& blocks); + +OtSendStore MakeOtSendStore( + const std::vector>& blocks); + +// Easier way of generate a compact cot_store pointer from a given blocks +// buffer and cot delta +// Note: Compact ot is correlated-ot (or called delta-ot) +OtSendStore MakeCompactOtSendStore(const std::vector& blocks, + uint128_t delta); + +OtSendStore MakeCompactOtSendStore(const UninitAlignedVector& blocks, + uint128_t delta); + +OtSendStore MakeCompactOtSendStore(std::vector&& blocks, + uint128_t delta); + +OtSendStore MakeCompactOtSendStore(UninitAlignedVector&& blocks, + uint128_t delta); + +// OT Store (for mocking only) +class MockOtStore { + public: + OtSendStore send; + OtRecvStore recv; +}; + +// Locally mock ots +MockOtStore MockRots(uint64_t num); +MockOtStore MockRots(uint64_t num, dynamic_bitset choices); +MockOtStore MockCots(uint64_t num, uint128_t delta); +MockOtStore MockCots(uint64_t num, uint128_t delta, + dynamic_bitset choices); + +// Note: Compact ot is correlated-ot (or called delta-ot) +MockOtStore MockCompactOts(uint64_t num); + +} // namespace yacl::crypto diff --git a/yacl/kernel/type/slice_base.cc b/yacl/kernel/type/slice_base.cc new file mode 100644 index 00000000..86502a55 --- /dev/null +++ b/yacl/kernel/type/slice_base.cc @@ -0,0 +1,71 @@ +// 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 "yacl/kernel/type/slice_base.h" + +#include "yacl/base/exception.h" + +namespace yacl::crypto { +//---------------------------------- +// Slice Base +//---------------------------------- + +void SliceBase::ConsistencyCheck() const { + YACL_ENFORCE( + internal_use_size_ > 0, + "Internal slice size shoud be greater than 0, but got slice size: {}", + internal_use_size_); + YACL_ENFORCE( + internal_buf_size_ > 0, + "Internal buffer size shoud be greater than 0, but got buffer size: {}", + internal_buf_size_); + YACL_ENFORCE(internal_buf_size_ >= internal_use_size_, + "Buffer size should great or equal to slice size, got {} >= {}", + internal_buf_size_, internal_use_size_); + YACL_ENFORCE(internal_buf_size_ >= internal_buf_ctr_, "Slice out of range!"); +} + +void SliceBase::InitCtrs(uint64_t use_ctr, uint64_t use_size, uint64_t buf_ctr, + uint64_t buf_size) { + internal_use_ctr_ = use_ctr; + internal_use_size_ = use_size; + internal_buf_ctr_ = buf_ctr; + internal_buf_size_ = buf_size; + ConsistencyCheck(); +} + +uint64_t SliceBase::GetBufIdx(uint64_t slice_idx) const { + YACL_ENFORCE(internal_use_size_ > slice_idx, + "Slice index out of range, slice size: {}, but got index: {}", + internal_use_size_, slice_idx); + return internal_use_ctr_ + slice_idx; +} + +void SliceBase::IncreaseBufCtr(uint64_t size) { + YACL_ENFORCE( + internal_buf_size_ - internal_buf_ctr_ >= size, + "Increase buffer counter failed, not enough space, buffer left space: " + "{}, but tried increase with size: {}", + internal_buf_size_ - internal_buf_ctr_, size); + internal_buf_ctr_ += size; +} + +void SliceBase::Reset() { + internal_use_ctr_ = 0; + internal_use_size_ = 0; + internal_buf_ctr_ = 0; + internal_buf_size_ = 0; + ConsistencyCheck(); +} +} // namespace yacl::crypto diff --git a/yacl/kernel/type/slice_base.h b/yacl/kernel/type/slice_base.h new file mode 100644 index 00000000..91823515 --- /dev/null +++ b/yacl/kernel/type/slice_base.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 + +namespace yacl::crypto { + +class SliceBase { + public: + // setters and getters + bool IsSliced() const { return internal_buf_ctr_ != 0; } + virtual ~SliceBase() = default; + + void ResetSlice() { + internal_use_ctr_ = 0; + internal_use_size_ = internal_buf_size_; + internal_buf_ctr_ = 0; + } + + protected: + uint64_t GetUseCtr() const { return internal_use_ctr_; } + uint64_t GetUseSize() const { return internal_use_size_; } + uint64_t GetBufCtr() const { return internal_buf_ctr_; } + uint64_t GetBufSize() const { return internal_buf_size_; } + virtual void ConsistencyCheck() const; + + // init all interal values + void InitCtrs(uint64_t use_ctr, uint64_t use_size, uint64_t buf_ctr, + uint64_t buf_size); + + // get the internal buffer index from a slice index + uint64_t GetBufIdx(uint64_t slice_idx) const; + + // manually increase the buffer counter by "size" + void IncreaseBufCtr(uint64_t size); + + // reset all pointers + void Reset(); + + // An unused slice looks like the follwoing: + // + // |---------------|-----slice-----|----------------| internal buffer + // a b c d + // + // internal_use_ctr_ = b + // internal_use_size_ = c - b + // internal_buf_ctr_ = b + // internal_buf_size_ = c + // + // where who slice this buffer looks like the following: + // + // + // |---------------|-----slice-----|----------------| internal buffer + // a b c d + // + // internal_use_ctr_ = a + // internal_use_size_ = d - a + // internal_buf_ctr_ = c (since the underlying buffer is already sliced to c) + // internal_buf_size_ = d - a + + uint64_t internal_use_ctr_ = 0; // slice begin position in buffer + uint64_t internal_use_size_ = 0; // allowed slice size (read & wrtie) + uint64_t internal_buf_ctr_ = 0; // buffer use counter (next slice position) + uint64_t internal_buf_size_ = 0; // underlying buf max size + // (will not be affected by slice op) +}; +} // namespace yacl::crypto