diff --git a/.bazelrc b/.bazelrc index c00abb69..a8ab15a9 100644 --- a/.bazelrc +++ b/.bazelrc @@ -15,6 +15,10 @@ common --experimental_repo_remote_exec common --experimental_cc_shared_library +common --registry=https://raw.githubusercontent.com/secretflow/bazel-registry/main +common --registry=https://bcr.bazel.build +common --registry=https://baidu.github.io/babylon/registry + build --incompatible_new_actions_api=false build --copt=-fdiagnostics-color=always build --enable_platform_specific_config @@ -40,6 +44,7 @@ build:macos --action_env MACOSX_DEPLOYMENT_TARGET=13.0 build:asan --features=asan build:ubsan --features=ubsan + test --keep_going test --test_output=errors test --test_timeout=360 @@ -49,4 +54,4 @@ test --test_timeout=360 build:gm --define BRPC_WITH_TONGSUO=true # make sure yacl is built with tongsuo in gm mode -build:gm --define YACL_WITH_TONGSUO=true \ No newline at end of file +build:gm --define YACL_WITH_TONGSUO=true diff --git a/.bazelversion b/.bazelversion index ba7f754d..815da58b 100644 --- a/.bazelversion +++ b/.bazelversion @@ -1 +1 @@ -7.4.0 +7.4.1 diff --git a/.gitignore b/.gitignore index 839e33a5..8cf35551 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,8 @@ _build # bazel bazel-* +# bazel lock file +MODULE.bazel.lock # cmake related abseil-cpp diff --git a/.licenserc.yaml b/.licenserc.yaml index 84362c99..7504ef15 100644 --- a/.licenserc.yaml +++ b/.licenserc.yaml @@ -2,7 +2,7 @@ header: # <1> license: spdx-id: Apache-2.0 # <2> copyright-owner: Ant Group Co., Ltd. # <3> - copyright-year: '2024' # <25> + copyright-year: "2024" # <25> software-name: secretflow # <4> pattern: | # <6> @@ -19,33 +19,35 @@ header: # <1> limitations under the License. paths: # <7> - - '**' + - "**" paths-ignore: # <8> - - '.circleci' - - '.github' - - '**/*.md' - - '**/*.json' - - '**/*.yaml' - - '**/*.yml' - - '**/*.patch' - - '**/*.lds' - - '**/*.csv' - - '**/*.rst' - - 'LICENSE' - - 'NOTICE' - - '.bazelversion' - - '.bazelignore' - - '.clang-format' - - '.clang-tidy' - - '.gitattributes' - - '.gitignore' - - '.gitmodules' - - 'yacl/base/dynamic_bitset.h' # MIT - - 'yacl/crypto/aes/aes_intrinsics.h' - - 'yacl/crypto/aes/aes_opt.h' - - 'yacl/io/circuit/data/**.txt' - - 'docs/requirements.txt' + - ".circleci" + - ".github" + - "**/*.md" + - "**/*.json" + - "**/*.yaml" + - "**/*.yml" + - "**/*.patch" + - "**/*.lds" + - "**/*.csv" + - "**/*.rst" + - "LICENSE" + - "NOTICE" + - ".bazelversion" + - ".bazelignore" + - ".clang-format" + - ".clang-tidy" + - ".gitattributes" + - ".gitignore" + - ".gitmodules" + - "yacl/base/dynamic_bitset.h" # MIT + - "yacl/crypto/aes/aes_intrinsics.h" + - "yacl/crypto/aes/aes_opt.h" + - "yacl/io/circuit/data/**.txt" + - "docs/requirements.txt" + - "MODULE.bazel.lock" + - "bazel/modules/" comment: never # <9> diff --git a/MODULE.bazel b/MODULE.bazel index 495f8e61..4fe31336 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -18,3 +18,57 @@ # # For more details, please check https://github.com/bazelbuild/bazel/issues/18958 ############################################################################### + +module( + name = "yacl", + compatibility_level = 1, +) + +# --registry=https://bcr.bazel.build +bazel_dep(name = "rules_proto", version = "4.0.0") +bazel_dep(name = "bazel_skylib", version = "1.7.1") +bazel_dep(name = "rules_cc") +single_version_override( + module_name = "rules_cc", + version = "0.0.12", +) + +bazel_dep(name = "abseil-cpp", version = "20230802.1") +bazel_dep(name = "gflags", version = "2.2.2") +bazel_dep(name = "zlib", version = "1.3.1.bcr.3") +bazel_dep(name = "msgpack-c", version = "6.1.0") +bazel_dep(name = "apple_support", version = "1.17.1") +bazel_dep(name = "blake3", version = "1.5.4") +bazel_dep(name = "fmt", version = "11.0.2") +bazel_dep(name = "spdlog", version = "1.14.1") +bazel_dep(name = "rules_python", version = "0.16.2") +bazel_dep(name = "bazel_features", version = "1.20.0") +bazel_dep(name = "rules_foreign_cc", version = "0.12.0") +bazel_dep(name = "platforms", version = "0.0.8") + +# self-hosted registry +bazel_dep(name = "brpc", version = "1.11.0-20241119-72ce354") +bazel_dep(name = "cpu_features", version = "0.9.0") +bazel_dep(name = "simplest-ot", version = "0.0.1") +bazel_dep(name = "libtommath", version = "0.0.0-20240407-42b3fb0") +bazel_dep(name = "tongsuo", version = "8.4.0") +bazel_dep(name = "fourqlib", version = "0.0.0-20220901-1031567") +bazel_dep(name = "libsodium", version = "1.0.18") +bazel_dep(name = "sse2neon", version = "1.7.0-20240330-8df2f48") +bazel_dep(name = "hash_drbg", version = "0.0.0-20230516-2411fa9") +bazel_dep(name = "org_interconnection", version = "0.0.1") +bazel_dep(name = "lib25519", version = "20240321") +bazel_dep(name = "mcl", version = "1.99", repo_name = "mcl") +bazel_dep(name = "protobuf") +single_version_override( + module_name = "protobuf", + version = "25.5", +) + +# --registry=https://baidu.github.io/babylon/registry +bazel_dep(name = "leveldb", version = "1.23") +bazel_dep(name = "openssl", version = "3.3.2") + +# test +bazel_dep(name = "googletest", version = "1.15.2", dev_dependency = True) +bazel_dep(name = "google_benchmark", version = "1.8.5", dev_dependency = True) diff --git a/WORKSPACE b/WORKSPACE deleted file mode 100644 index f568448d..00000000 --- a/WORKSPACE +++ /dev/null @@ -1,51 +0,0 @@ -# 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. - -workspace(name = "yacl") - -# yacl's dependencies - -load("//bazel:repositories.bzl", "yacl_deps") - -yacl_deps() - -load("@rules_python//python:repositories.bzl", "py_repositories") - -py_repositories() - -load( - "@rules_foreign_cc//foreign_cc:repositories.bzl", - "rules_foreign_cc_dependencies", -) - -rules_foreign_cc_dependencies( - register_built_tools = False, - register_default_tools = False, - register_preinstalled_tools = True, -) - -load( - "@build_bazel_apple_support//lib:repositories.bzl", - "apple_support_dependencies", -) - -apple_support_dependencies() - -load("@bazel_features//:deps.bzl", "bazel_features_deps") - -bazel_features_deps() - -load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps") - -protobuf_deps() diff --git a/bazel/blake3.BUILD b/bazel/blake3.BUILD deleted file mode 100644 index 180ae84a..00000000 --- a/bazel/blake3.BUILD +++ /dev/null @@ -1,41 +0,0 @@ -# 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("@rules_cc//cc:defs.bzl", "cc_library") - -cc_library( - name = "blake3_c", - srcs = [ - "c/blake3.c", - "c/blake3_dispatch.c", - "c/blake3_portable.c", - ] + select({ - "@platforms//cpu:aarch64": [ - "c/blake3_neon.c", - ], - "//conditions:default": [ - "c/blake3_avx2_x86-64_unix.S", - "c/blake3_avx512_x86-64_unix.S", - "c/blake3_sse2_x86-64_unix.S", - "c/blake3_sse41_x86-64_unix.S", - ], - }), - hdrs = [ - "c/blake3.h", - "c/blake3_impl.h", - ], - defines = ["BLAKE3_C_EXTERNAL"], - includes = ["include"], - visibility = ["//visibility:public"], -) diff --git a/bazel/cpu_features.BUILD b/bazel/cpu_features.BUILD deleted file mode 100644 index c3802a06..00000000 --- a/bazel/cpu_features.BUILD +++ /dev/null @@ -1,35 +0,0 @@ -# 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("@yacl//bazel:yacl.bzl", "yacl_cmake_external") - -package(default_visibility = ["//visibility:public"]) - -filegroup( - name = "all_srcs", - srcs = glob(["**"]), -) - -yacl_cmake_external( - name = "cpu_features", - cache_entries = { - "CMAKE_INSTALL_LIBDIR": "lib", - "CMAKE_POSITION_INDEPENDENT_CODE": "ON", - "BUILD_TESTING": "OFF", - }, - lib_source = ":all_srcs", - out_data_dirs = ["lib/cmake"], - out_lib_dir = "lib", - out_static_libs = ["libcpu_features.a"], -) diff --git a/bazel/leveldb.BUILD b/bazel/leveldb.BUILD deleted file mode 100644 index f378bff4..00000000 --- a/bazel/leveldb.BUILD +++ /dev/null @@ -1,92 +0,0 @@ -# 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("@rules_cc//cc:defs.bzl", "cc_library") - -package(default_visibility = ["//visibility:public"]) - -SOURCES = [ - "db/builder.cc", - "db/c.cc", - "db/dbformat.cc", - "db/db_impl.cc", - "db/db_iter.cc", - "db/dumpfile.cc", - "db/filename.cc", - "db/log_reader.cc", - "db/log_writer.cc", - "db/memtable.cc", - "db/repair.cc", - "db/table_cache.cc", - "db/version_edit.cc", - "db/version_set.cc", - "db/write_batch.cc", - "table/block_builder.cc", - "table/block.cc", - "table/filter_block.cc", - "table/format.cc", - "table/iterator.cc", - "table/merger.cc", - "table/table_builder.cc", - "table/table.cc", - "table/two_level_iterator.cc", - "util/arena.cc", - "util/bloom.cc", - "util/cache.cc", - "util/coding.cc", - "util/comparator.cc", - "util/crc32c.cc", - "util/env.cc", - "util/env_posix.cc", - "util/filter_policy.cc", - "util/hash.cc", - "util/histogram.cc", - "util/logging.cc", - "util/options.cc", - "util/status.cc", - "helpers/memenv/memenv.cc", -] - -cc_library( - name = "leveldb", - srcs = SOURCES, - hdrs = glob( - [ - "helpers/memenv/*.h", - "util/*.h", - "port/*.h", - "port/win/*.h", - "table/*.h", - "db/*.h", - "include/leveldb/*.h", - ], - exclude = [ - "**/*test.*", - ], - ), - copts = [ - "-fno-builtin-memcmp", - "-DLEVELDB_PLATFORM_POSIX=1", - "-DLEVELDB_ATOMIC_PRESENT", - ], - defines = [ - "LEVELDB_PLATFORM_POSIX", - ] + select({ - "@bazel_tools//src/conditions:darwin": ["OS_MACOSX"], - "//conditions:default": [], - }), - includes = [ - "include/", - ], -) diff --git a/bazel/mcl.BUILD b/bazel/mcl.BUILD index ab7f3814..b89377b9 100644 --- a/bazel/mcl.BUILD +++ b/bazel/mcl.BUILD @@ -13,8 +13,7 @@ # limitations under the License. load("@bazel_skylib//lib:selects.bzl", "selects") -load("@rules_foreign_cc//foreign_cc:defs.bzl", "make") -load("@yacl//bazel:yacl.bzl", "yacl_cmake_external") +load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake", "make") package(default_visibility = ["//visibility:public"]) @@ -58,13 +57,14 @@ android_config = { # bad for mac intel # https://github.com/herumi/mcl/issues/174 -yacl_cmake_external( +cmake( name = "mcl-cmake", build_args = ["-j"], cache_entries = select({ ":cpu_arm64_v8a": android_config, "//conditions:default": default_config, }), + generate_args = ["-G Ninja"], # generate_crosstool_file = False, lib_source = ":source", out_static_libs = [ diff --git a/bazel/msgpack.BUILD b/bazel/msgpack.BUILD deleted file mode 100644 index 7d8ca687..00000000 --- a/bazel/msgpack.BUILD +++ /dev/null @@ -1,36 +0,0 @@ -# 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. - -load("@yacl//bazel:yacl.bzl", "yacl_cmake_external") - -package(default_visibility = ["//visibility:public"]) - -filegroup( - name = "all_srcs", - srcs = glob(["**"]), -) - -yacl_cmake_external( - name = "msgpack", - cache_entries = { - "MSGPACK_CXX17": "ON", - "MSGPACK_USE_BOOST": "OFF", - "MSGPACK_BUILD_EXAMPLES": "OFF", - "BUILD_SHARED_LIBS": "OFF", - "MSGPACK_BUILD_TESTS": "OFF", - }, - defines = ["MSGPACK_NO_BOOST"], - lib_source = ":all_srcs", - out_headers_only = True, -) diff --git a/bazel/openssl.BUILD b/bazel/openssl.BUILD deleted file mode 100644 index 15210084..00000000 --- a/bazel/openssl.BUILD +++ /dev/null @@ -1,71 +0,0 @@ -# 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("@yacl//bazel:yacl.bzl", "yacl_configure_make") - -# An openssl build file based on a snippet found in the github issue: -# https://github.com/bazelbuild/rules_foreign_cc/issues/337 - -# Read https://wiki.openssl.org/index.php/Compilation_and_Installation - -filegroup( - name = "all_srcs", - srcs = glob( - include = ["**"], - exclude = ["*.bazel"], - ), -) - -CONFIGURE_OPTIONS = [ - # fixed openssl work dir for deterministic build. - "--openssldir=/tmp/openssl", - "--libdir=lib", - "no-legacy", - "no-weak-ssl-ciphers", - "no-tests", - "no-shared", - "no-ui-console", -] - -MAKE_TARGETS = [ - "build_programs", - "install_sw", -] - -yacl_configure_make( - name = "openssl", - args = ["-j 4"], - configure_command = "Configure", - configure_in_place = True, - configure_options = CONFIGURE_OPTIONS, - env = select({ - "@platforms//os:macos": { - "AR": "", - }, - "//conditions:default": { - "MODULESDIR": "", - }, - }), - lib_name = "openssl", - lib_source = ":all_srcs", - linkopts = ["-ldl"], - # Note that for Linux builds, libssl must come before libcrypto on the linker command-line. - # As such, libssl must be listed before libcrypto - out_static_libs = [ - "libssl.a", - "libcrypto.a", - ], - targets = MAKE_TARGETS, - visibility = ["//visibility:public"], -) diff --git a/bazel/patches/brpc.patch b/bazel/patches/brpc.patch index cf08f1fa..b1dbb245 100644 --- a/bazel/patches/brpc.patch +++ b/bazel/patches/brpc.patch @@ -99,7 +99,7 @@ index 0bdaa4d3..b23881b3 100644 @@ -326,13 +326,13 @@ cc_library( visibility = ["//visibility:public"], deps = [ - "@com_github_gflags_gflags//:gflags", + "@gflags//:gflags", - "@com_github_madler_zlib//:zlib", + "@zlib//:zlib", "@com_google_protobuf//:protobuf", diff --git a/bazel/patches/brpc_crypto.patch b/bazel/patches/brpc_crypto.patch index 79d95fc7..f6d3ee71 100644 --- a/bazel/patches/brpc_crypto.patch +++ b/bazel/patches/brpc_crypto.patch @@ -7,8 +7,8 @@ index b2a453e0..10eeee74 100644 }) + select({ "//bazel/config:brpc_with_boringssl": ["@boringssl//:ssl", "@boringssl//:crypto"], - "//conditions:default": ["@openssl//:ssl", "@openssl//:crypto"], -+ "//bazel/config:brpc_with_tongsuo": ["@com_github_tongsuo_tongsuo//:tongsuo"], -+ "//conditions:default": ["@com_github_openssl_openssl//:openssl"], ++ "//bazel/config:brpc_with_tongsuo": ["@tongsuo//:tongsuo"], ++ "//conditions:default": ["@openssl//:openssl"], }), ) diff --git a/bazel/repositories.bzl b/bazel/repositories.bzl deleted file mode 100644 index 87f6949f..00000000 --- a/bazel/repositories.bzl +++ /dev/null @@ -1,428 +0,0 @@ -# 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. - -load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") -load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") - -def yacl_deps(): - _rule_proto() - _rule_python() - _rules_foreign_cc() - _build_bazel_apple_support() - _com_github_madler_zlib() - _com_google_protobuf() - _com_github_gflags_gflags() - _com_google_googletest() - _com_google_absl() - _com_github_google_leveldb() - _com_github_brpc_brpc() - _com_github_fmtlib_fmt() - _com_github_gabime_spdlog() - _com_github_google_benchmark() - _com_github_google_cpu_features() - _com_github_dltcollab_sse2neon() - _com_github_msgpack_msgpack() - _com_github_greendow_hash_drbg() - - # ssl - _com_github_tongsuo_tongsuo() - _com_github_openssl_openssl() - - # crypto related - _com_github_blake3team_blake3() - _com_github_libsodium() - _com_github_libtom_libtommath() - _com_github_herumi_mcl() - _com_github_microsoft_FourQlib() - _lib25519() - - _simplest_ot() - _org_interconnection() - -def _simplest_ot(): - maybe( - http_archive, - name = "simplest_ot", - urls = [ - "https://github.com/secretflow/simplest-ot/archive/60197bc7dad327bb55759e8e854885411e999167.tar.gz", - ], - strip_prefix = "simplest-ot-60197bc7dad327bb55759e8e854885411e999167", - sha256 = "c8816bf147e320f51c516f4c511f2d1a732ac0d0f171d29f442cbe2b5173ddba", - ) - -def _org_interconnection(): - maybe( - http_archive, - name = "org_interconnection", - urls = [ - "https://github.com/secretflow/interconnection/archive/16957915c16839503bd18f55aa536f475ab61856.tar.gz", - ], - strip_prefix = "interconnection-16957915c16839503bd18f55aa536f475ab61856", - sha256 = "fad6d278632361f3a79d05254feaec890da3d287e441feedbe7a4fe51521c439", - ) - - # Add homebrew openmp for macOS, somehow..homebrew installs to different location on Apple Silcon/Intel macs.. so we need two rules here - native.new_local_repository( - name = "macos_omp_x64", - build_file = "@yacl//bazel:local_openmp_macos.BUILD", - path = "/usr/local/opt/libomp", - ) - - native.new_local_repository( - name = "macos_omp_arm64", - build_file = "@yacl//bazel:local_openmp_macos.BUILD", - path = "/opt/homebrew/opt/libomp/", - ) - -def _com_github_brpc_brpc(): - maybe( - http_archive, - name = "com_github_brpc_brpc", - sha256 = "fe4eb10b4ca1a59e0f71086552b2d8897afd66df93b53c18ad83f6a93717cc2d", - strip_prefix = "brpc-1.10.0", - type = "tar.gz", - patch_args = ["-p1"], - patches = [ - "@yacl//bazel:patches/brpc.patch", - "@yacl//bazel:patches/brpc_m1.patch", - "@yacl//bazel:patches/brpc_crypto.patch", - ], - urls = [ - "https://github.com/apache/brpc/archive/refs/tags/1.10.0.tar.gz", - ], - ) - -def _com_github_gflags_gflags(): - maybe( - http_archive, - name = "com_github_gflags_gflags", - strip_prefix = "gflags-2.2.2", - sha256 = "34af2f15cf7367513b352bdcd2493ab14ce43692d2dcd9dfc499492966c64dcf", - type = "tar.gz", - urls = [ - "https://github.com/gflags/gflags/archive/v2.2.2.tar.gz", - ], - ) - -def _com_github_google_leveldb(): - maybe( - http_archive, - name = "com_github_google_leveldb", - strip_prefix = "leveldb-1.23", - sha256 = "9a37f8a6174f09bd622bc723b55881dc541cd50747cbd08831c2a82d620f6d76", - type = "tar.gz", - build_file = "@yacl//bazel:leveldb.BUILD", - patch_args = ["-p1"], - patches = ["@yacl//bazel:patches/leveldb.patch"], - urls = [ - "https://github.com/google/leveldb/archive/refs/tags/1.23.tar.gz", - ], - ) - -def _com_github_madler_zlib(): - maybe( - http_archive, - name = "zlib", - build_file = "@yacl//bazel:zlib.BUILD", - strip_prefix = "zlib-1.3.1", - sha256 = "17e88863f3600672ab49182f217281b6fc4d3c762bde361935e436a95214d05c", - type = ".tar.gz", - urls = [ - "https://github.com/madler/zlib/archive/refs/tags/v1.3.1.tar.gz", - ], - ) - -def _com_google_protobuf(): - maybe( - http_archive, - name = "com_google_protobuf", - sha256 = "2c6a36c7b5a55accae063667ef3c55f2642e67476d96d355ff0acb13dbb47f09", - strip_prefix = "protobuf-21.12", - type = "tar.gz", - patch_args = ["-p1"], - patches = ["@yacl//bazel:patches/protobuf.patch"], - urls = [ - "https://github.com/protocolbuffers/protobuf/releases/download/v21.12/protobuf-all-21.12.tar.gz", - ], - ) - -def _com_google_absl(): - maybe( - http_archive, - name = "com_google_absl", - sha256 = "f50e5ac311a81382da7fa75b97310e4b9006474f9560ac46f54a9967f07d4ae3", - type = "tar.gz", - strip_prefix = "abseil-cpp-20240722.0", - urls = [ - "https://github.com/abseil/abseil-cpp/archive/refs/tags/20240722.0.tar.gz", - ], - ) - -def _com_github_openssl_openssl(): - maybe( - http_archive, - name = "com_github_openssl_openssl", - sha256 = "bedbb16955555f99b1a7b1ba90fc97879eb41025081be359ecd6a9fcbdf1c8d2", - type = "tar.gz", - strip_prefix = "openssl-openssl-3.3.2", - urls = [ - "https://github.com/openssl/openssl/archive/refs/tags/openssl-3.3.2.tar.gz", - ], - build_file = "@yacl//bazel:openssl.BUILD", - ) - -def _com_github_tongsuo_tongsuo(): - maybe( - http_archive, - name = "com_github_tongsuo_tongsuo", - sha256 = "57c2741750a699bfbdaa1bbe44a5733e9c8fc65d086c210151cfbc2bbd6fc975", - type = "tar.gz", - strip_prefix = "Tongsuo-8.4.0", - urls = [ - "https://github.com/Tongsuo-Project/Tongsuo/archive/refs/tags/8.4.0.tar.gz", - ], - build_file = "@yacl//bazel:tongsuo.BUILD", - ) - -def _com_github_fmtlib_fmt(): - maybe( - http_archive, - name = "com_github_fmtlib_fmt", - strip_prefix = "fmt-11.0.2", - sha256 = "6cb1e6d37bdcb756dbbe59be438790db409cdb4868c66e888d5df9f13f7c027f", - build_file = "@yacl//bazel:fmtlib.BUILD", - urls = [ - "https://github.com/fmtlib/fmt/archive/refs/tags/11.0.2.tar.gz", - ], - ) - -def _com_github_gabime_spdlog(): - maybe( - http_archive, - name = "com_github_gabime_spdlog", - strip_prefix = "spdlog-1.14.1", - type = "tar.gz", - sha256 = "1586508029a7d0670dfcb2d97575dcdc242d3868a259742b69f100801ab4e16b", - build_file = "@yacl//bazel:spdlog.BUILD", - urls = [ - "https://github.com/gabime/spdlog/archive/refs/tags/v1.14.1.tar.gz", - ], - ) - -def _com_google_googletest(): - maybe( - http_archive, - name = "com_google_googletest", - sha256 = "7b42b4d6ed48810c5362c265a17faebe90dc2373c885e5216439d37927f02926", - type = "tar.gz", - strip_prefix = "googletest-1.15.2", - urls = [ - "https://github.com/google/googletest/archive/refs/tags/v1.15.2.tar.gz", - ], - ) - -def _com_github_google_benchmark(): - maybe( - http_archive, - name = "com_github_google_benchmark", - type = "tar.gz", - strip_prefix = "benchmark-1.9.0", - sha256 = "35a77f46cc782b16fac8d3b107fbfbb37dcd645f7c28eee19f3b8e0758b48994", - urls = [ - "https://github.com/google/benchmark/archive/refs/tags/v1.9.0.tar.gz", - ], - ) - -def _com_github_blake3team_blake3(): - maybe( - http_archive, - name = "com_github_blake3team_blake3", - strip_prefix = "BLAKE3-1.5.4", - sha256 = "ddd24f26a31d23373e63d9be2e723263ac46c8b6d49902ab08024b573fd2a416", - build_file = "@yacl//bazel:blake3.BUILD", - urls = [ - "https://github.com/BLAKE3-team/BLAKE3/archive/refs/tags/1.5.4.tar.gz", - ], - ) - -def _rule_proto(): - maybe( - http_archive, - name = "rules_proto", - sha256 = "dc3fb206a2cb3441b485eb1e423165b231235a1ea9b031b4433cf7bc1fa460dd", - strip_prefix = "rules_proto-5.3.0-21.7", - urls = [ - "https://github.com/bazelbuild/rules_proto/archive/refs/tags/5.3.0-21.7.tar.gz", - ], - ) - -# Required by protobuf -def _rule_python(): - maybe( - http_archive, - name = "rules_python", - sha256 = "ca77768989a7f311186a29747e3e95c936a41dffac779aff6b443db22290d913", - strip_prefix = "rules_python-0.36.0", - urls = [ - "https://github.com/bazelbuild/rules_python/archive/refs/tags/0.36.0.tar.gz", - ], - ) - -def _rules_foreign_cc(): - maybe( - http_archive, - name = "rules_foreign_cc", - sha256 = "a2e6fb56e649c1ee79703e99aa0c9d13c6cc53c8d7a0cbb8797ab2888bbc99a3", - strip_prefix = "rules_foreign_cc-0.12.0", - url = "https://github.com/bazelbuild/rules_foreign_cc/releases/download/0.12.0/rules_foreign_cc-0.12.0.tar.gz", - ) - -def _com_github_libsodium(): - maybe( - http_archive, - name = "com_github_libsodium", - type = "tar.gz", - strip_prefix = "libsodium-1.0.18", - sha256 = "6f504490b342a4f8a4c4a02fc9b866cbef8622d5df4e5452b46be121e46636c1", - build_file = "@yacl//bazel:libsodium.BUILD", - urls = [ - "https://github.com/jedisct1/libsodium/releases/download/1.0.18-RELEASE/libsodium-1.0.18.tar.gz", - ], - ) - -def _com_github_microsoft_FourQlib(): - maybe( - http_archive, - name = "com_github_microsoft_FourQlib", - type = "tar.gz", - strip_prefix = "FourQlib-1031567f23278e1135b35cc04e5d74c2ac88c029", - sha256 = "7417c829d7933facda568c7a08924dfefb0c83dd1dab411e597af4c0cc0417f0", - patch_args = ["-p1"], - patches = [ - "@yacl//bazel:patches/FourQlib.patch", - ], - build_file = "@yacl//bazel:FourQlib.BUILD", - urls = [ - "https://github.com/microsoft/FourQlib/archive/1031567f23278e1135b35cc04e5d74c2ac88c029.tar.gz", - ], - ) - -def _com_github_google_cpu_features(): - maybe( - http_archive, - name = "com_github_google_cpu_features", - strip_prefix = "cpu_features-0.9.0", - type = "tar.gz", - build_file = "@yacl//bazel:cpu_features.BUILD", - sha256 = "bdb3484de8297c49b59955c3b22dba834401bc2df984ef5cfc17acbe69c5018e", - urls = [ - "https://github.com/google/cpu_features/archive/refs/tags/v0.9.0.tar.gz", - ], - ) - -def _com_github_dltcollab_sse2neon(): - maybe( - http_archive, - name = "com_github_dltcollab_sse2neon", - sha256 = "787e0a7a64f1461b48232a7f9b9e9c14fa4a35a30875f2fb91aec6ddeaddfc0f", - strip_prefix = "sse2neon-8df2f48dbd0674ae5087f7a6281af6f55fa5a8e2", - type = "tar.gz", - urls = [ - "https://github.com/DLTcollab/sse2neon/archive/8df2f48dbd0674ae5087f7a6281af6f55fa5a8e2.tar.gz", - ], - build_file = "@yacl//bazel:sse2neon.BUILD", - ) - -def _com_github_libtom_libtommath(): - maybe( - http_archive, - name = "com_github_libtom_libtommath", - sha256 = "7cfbdb64431129de4257e7d3349200fdbd4f229b470ff3417b30d0f39beed41f", - type = "tar.gz", - strip_prefix = "libtommath-42b3fb07e7d504f61a04c7fca12e996d76a25251", - patch_args = ["-p1"], - patches = [ - "@yacl//bazel:patches/libtommath.patch", - ], - urls = [ - "https://github.com/libtom/libtommath/archive/42b3fb07e7d504f61a04c7fca12e996d76a25251.tar.gz", - ], - build_file = "@yacl//bazel:libtommath.BUILD", - ) - -def _com_github_msgpack_msgpack(): - maybe( - http_archive, - name = "com_github_msgpack_msgpack", - type = "tar.gz", - strip_prefix = "msgpack-c-cpp-6.1.0", - sha256 = "5e63e4d9b12ab528fccf197f7e6908031039b1fc89cd8da0e97fbcbf5a6c6d3a", - patches = [ - "@yacl//bazel:patches/msgpack.patch", - ], - patch_args = ["-p1"], - urls = [ - "https://github.com/msgpack/msgpack-c/archive/refs/tags/cpp-6.1.0.tar.gz", - ], - build_file = "@yacl//bazel:msgpack.BUILD", - ) - -def _com_github_greendow_hash_drbg(): - maybe( - http_archive, - name = "com_github_greendow_hash_drbg", - sha256 = "c03a3da5742d0f0c40232817d84f21d8eed4c4af498c4dff3a51b3bcadcb3787", - type = "tar.gz", - strip_prefix = "Hash-DRBG-2411fa9d0de81c69dce2a48555c30298253db15d", - urls = [ - "https://github.com/greendow/Hash-DRBG/archive/2411fa9d0de81c69dce2a48555c30298253db15d.tar.gz", - ], - build_file = "@yacl//bazel:hash_drbg.BUILD", - ) - -def _com_github_herumi_mcl(): - maybe( - http_archive, - name = "com_github_herumi_mcl", - strip_prefix = "mcl-1.99", - sha256 = "5ff9702c1f1b021925d1334ca0a03c87783174075aeaf87801842d3c08b3d39e", - type = "tar.gz", - build_file = "@yacl//bazel:mcl.BUILD", - patch_args = ["-p1"], - patches = [ - "@yacl//bazel:patches/mcl.patch", - ], - urls = ["https://github.com/herumi/mcl/archive/refs/tags/v1.99.tar.gz"], - ) - -def _lib25519(): - maybe( - http_archive, - name = "lib25519", - strip_prefix = "lib25519-20240321", - sha256 = "d010baea719153fe3f012789b5a1de27d91fbbcfc65559e7eee5d802bf91eadd", - type = "tar.gz", - build_file = "@yacl//bazel:lib25519.BUILD", - urls = [ - "https://lib25519.cr.yp.to/lib25519-20240321.tar.gz", - ], - ) - -def _build_bazel_apple_support(): - maybe( - http_archive, - name = "build_bazel_apple_support", - sha256 = "b53f6491e742549f13866628ddffcc75d1f3b2d6987dc4f14a16b242113c890b", - url = "https://github.com/bazelbuild/apple_support/releases/download/1.17.1/apple_support.1.17.1.tar.gz", - ) diff --git a/bazel/spdlog.BUILD b/bazel/spdlog.BUILD deleted file mode 100644 index 8504c299..00000000 --- a/bazel/spdlog.BUILD +++ /dev/null @@ -1,46 +0,0 @@ -# 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("@yacl//bazel:yacl.bzl", "yacl_cmake_external") - -package(default_visibility = ["//visibility:public"]) - -filegroup( - name = "all_srcs", - srcs = glob(["**"]), -) - -yacl_cmake_external( - name = "spdlog", - cache_entries = { - "SPDLOG_BUILD_EXAMPLE": "OFF", - "SPDLOG_FMT_EXTERNAL": "ON", - "SPDLOG_NO_TLS": "ON", - "CMAKE_INSTALL_LIBDIR": "lib", - "SPDLOG_BUILD_PIC": "ON", - }, - defines = [ - "SPDLOG_FMT_EXTERNAL", - "SPDLOG_NO_TLS", - ], - lib_source = ":all_srcs", - out_lib_dir = "lib", - out_static_libs = select({ - "@yacl//bazel:yacl_build_as_debug": ["libspdlogd.a"], - "//conditions:default": ["libspdlog.a"], - }), - deps = [ - "@com_github_fmtlib_fmt//:fmtlib", - ], -) diff --git a/bazel/yacl.bzl b/bazel/yacl.bzl index 26762eb1..eaca83f8 100644 --- a/bazel/yacl.bzl +++ b/bazel/yacl.bzl @@ -86,7 +86,7 @@ def yacl_cc_library( "//conditions:default": [], }), deps = deps + [ - "@com_github_gabime_spdlog//:spdlog", + "@spdlog//:spdlog", ], **kargs ) @@ -112,7 +112,7 @@ def yacl_cc_test( "//conditions:default": [], }), deps = deps + [ - "@com_google_googletest//:gtest_main", + "@googletest//:gtest_main", ], linkopts = linkopts + ["-ldl"], **kwargs diff --git a/bazel/zlib.BUILD b/bazel/zlib.BUILD deleted file mode 100644 index 504e7371..00000000 --- a/bazel/zlib.BUILD +++ /dev/null @@ -1,54 +0,0 @@ -# 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. - -package(default_visibility = ["//visibility:public"]) - -licenses(["notice"]) # BSD/MIT-like license (for zlib) - -cc_library( - name = "zlib", - srcs = [ - "adler32.c", - "compress.c", - "crc32.c", - "crc32.h", - "deflate.c", - "deflate.h", - "gzclose.c", - "gzguts.h", - "gzlib.c", - "gzread.c", - "gzwrite.c", - "infback.c", - "inffast.c", - "inffast.h", - "inffixed.h", - "inflate.c", - "inflate.h", - "inftrees.c", - "inftrees.h", - "trees.c", - "trees.h", - "uncompr.c", - "zconf.h", - "zutil.c", - "zutil.h", - ], - hdrs = ["zlib.h"], - copts = [ - "-Wno-shift-negative-value", - "-DZ_HAVE_UNISTD_H", - ], - includes = ["."], -) diff --git a/bazel/fmtlib.BUILD b/examples/MODULE.bazel similarity index 55% rename from bazel/fmtlib.BUILD rename to examples/MODULE.bazel index 4058f33e..495f8e61 100644 --- a/bazel/fmtlib.BUILD +++ b/examples/MODULE.bazel @@ -1,4 +1,4 @@ -# Copyright 2022 Ant Group Co., Ltd. +# 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. @@ -12,21 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -load("@yacl//bazel:yacl.bzl", "yacl_cmake_external") - -package(default_visibility = ["//visibility:public"]) - -filegroup( - name = "all_srcs", - srcs = glob(["**"]), -) - -yacl_cmake_external( - name = "fmtlib", - cache_entries = { - "FMT_TEST": "OFF", - }, - defines = ["FMT_HEADER_ONLY"], - lib_source = ":all_srcs", - out_headers_only = True, -) +############################################################################### +# Bazel now uses Bzlmod by default to manage external dependencies. +# Please consider migrating your external dependencies from WORKSPACE to MODULE.bazel. +# +# For more details, please check https://github.com/bazelbuild/bazel/issues/18958 +############################################################################### diff --git a/MODULE.bazel.lock b/examples/MODULE.bazel.lock similarity index 78% rename from MODULE.bazel.lock rename to examples/MODULE.bazel.lock index c75693b1..b9b80d4d 100644 --- a/MODULE.bazel.lock +++ b/examples/MODULE.bazel.lock @@ -1,16 +1,3 @@ -# 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. { "lockFileVersion": 11, "registryFileHashes": { @@ -73,51 +60,5 @@ "https://bcr.bazel.build/modules/zlib/1.3.1.bcr.3/source.json": "2be409ac3c7601245958cd4fcdff4288be79ed23bd690b4b951f500d54ee6e7d" }, "selectedYankedVersions": {}, - "moduleExtensions": { - "@@apple_support~//crosstool:setup.bzl%apple_cc_configure_extension": { - "general": { - "bzlTransitiveDigest": "PjIds3feoYE8SGbbIq2SFTZy3zmxeO2tQevJZNDo7iY=", - "usagesDigest": "+hz7IHWN6A1oVJJWNDB6yZRG+RYhF76wAYItpAeIUIg=", - "recordedFileInputs": {}, - "recordedDirentsInputs": {}, - "envVariables": {}, - "generatedRepoSpecs": { - "local_config_apple_cc_toolchains": { - "bzlFile": "@@apple_support~//crosstool:setup.bzl", - "ruleClassName": "_apple_cc_autoconf_toolchains", - "attributes": {} - }, - "local_config_apple_cc": { - "bzlFile": "@@apple_support~//crosstool:setup.bzl", - "ruleClassName": "_apple_cc_autoconf", - "attributes": {} - } - }, - "recordedRepoMappingEntries": [ - [ - "apple_support~", - "bazel_tools", - "bazel_tools" - ] - ] - } - }, - "@@platforms//host:extension.bzl%host_platform": { - "general": { - "bzlTransitiveDigest": "xelQcPZH8+tmuOHVjL9vDxMnnQNMlwj0SlvgoqBkm4U=", - "usagesDigest": "pCYpDQmqMbmiiPI1p2Kd3VLm5T48rRAht5WdW0X2GlA=", - "recordedFileInputs": {}, - "recordedDirentsInputs": {}, - "envVariables": {}, - "generatedRepoSpecs": { - "host_platform": { - "bzlFile": "@@platforms//host:extension.bzl", - "ruleClassName": "host_platform_repo", - "attributes": {} - } - }, - "recordedRepoMappingEntries": [] - } - } - } + "moduleExtensions": {} } diff --git a/examples/WORKSPACE b/examples/WORKSPACE index 7842bb04..bf88b52b 100644 --- a/examples/WORKSPACE +++ b/examples/WORKSPACE @@ -28,9 +28,9 @@ load("@yacl//bazel:repositories.bzl", "yacl_deps") yacl_deps() -load("@rules_python//python:repositories.bzl", "py_repositories") +# load("@rules_python//python:repositories.bzl", "py_repositories") -py_repositories() +# py_repositories() load("@pybind11_bazel//:python_configure.bzl", "python_configure") diff --git a/examples/psu/BUILD.bazel b/examples/psu/BUILD.bazel index 5f73b2c1..f28342ec 100644 --- a/examples/psu/BUILD.bazel +++ b/examples/psu/BUILD.bazel @@ -26,7 +26,7 @@ yacl_cc_library( ], copts = AES_COPT_FLAGS, deps = [ - "@com_google_absl//absl/types:span", + "@abseil-cpp//absl/types:span", "@yacl//yacl/base:exception", "@yacl//yacl/base:int128", "@yacl//yacl/crypto/hash:hash_utils", diff --git a/renovate.json b/renovate.json index add6bd25..6a5b51dd 100644 --- a/renovate.json +++ b/renovate.json @@ -1,8 +1,7 @@ { "$schema": "https://docs.renovatebot.com/renovate-schema.json", "extends": [ - "config:recommended", - ":semanticCommits" + "config:recommended" ], "labels": [ "dependencies" diff --git a/yacl/base/BUILD.bazel b/yacl/base/BUILD.bazel index a4afa048..4e6f67fd 100644 --- a/yacl/base/BUILD.bazel +++ b/yacl/base/BUILD.bazel @@ -21,10 +21,10 @@ yacl_cc_library( srcs = ["exception.cc"], hdrs = ["exception.h"], deps = [ - "@com_github_fmtlib_fmt//:fmtlib", - "@com_google_absl//absl/debugging:stacktrace", - "@com_google_absl//absl/debugging:symbolize", - "@com_google_absl//absl/types:span", + "@abseil-cpp//absl/debugging:stacktrace", + "@abseil-cpp//absl/debugging:symbolize", + "@abseil-cpp//absl/types:span", + "@fmt", ], ) @@ -41,8 +41,8 @@ yacl_cc_library( srcs = ["int128.cc"], hdrs = ["int128.h"], deps = [ - "@com_google_absl//absl/numeric:bits", - "@com_google_absl//absl/numeric:int128", + "@abseil-cpp//absl/numeric:bits", + "@abseil-cpp//absl/numeric:int128", ], ) @@ -60,7 +60,7 @@ yacl_cc_library( hdrs = ["byte_container_view.h"], deps = [ ":buffer", - "@com_google_absl//absl/types:span", + "@abseil-cpp//absl/types:span", ], ) @@ -87,7 +87,7 @@ yacl_cc_binary( linkopts = ["-lm"], deps = [ ":int128", - "@com_github_google_benchmark//:benchmark_main", + "@google_benchmark//:benchmark_main", ], ) @@ -114,10 +114,10 @@ yacl_cc_library( hdrs = ["block.h"], deps = [ "//yacl/base:int128", - "@com_google_absl//absl/types:span", + "@abseil-cpp//absl/types:span", ] + select({ "@platforms//cpu:aarch64": [ - "@com_github_dltcollab_sse2neon//:sse2neon", + "@sse2neon", ], "//conditions:default": [], }), @@ -130,7 +130,7 @@ yacl_cc_binary( ":block", "//yacl/crypto/tools:prg", "//yacl/utils:matrix_utils", - "@com_github_google_benchmark//:benchmark_main", + "@google_benchmark//:benchmark_main", ], ) diff --git a/yacl/crypto/BUILD.bazel b/yacl/crypto/BUILD.bazel index ae77c7c8..a8cee78f 100644 --- a/yacl/crypto/BUILD.bazel +++ b/yacl/crypto/BUILD.bazel @@ -26,8 +26,8 @@ yacl_cc_library( "//yacl/base:exception", "//yacl/utils:scope_guard", ] + select({ - "//bazel/config:gm": ["@com_github_tongsuo_tongsuo//:tongsuo"], - "//conditions:default": ["@com_github_openssl_openssl//:openssl"], + "//bazel/config:gm": ["@tongsuo"], + "//conditions:default": ["@openssl"], }), ) diff --git a/yacl/crypto/aead/BUILD.bazel b/yacl/crypto/aead/BUILD.bazel index 42c00955..f6faed99 100644 --- a/yacl/crypto/aead/BUILD.bazel +++ b/yacl/crypto/aead/BUILD.bazel @@ -16,6 +16,37 @@ load("//bazel:yacl.bzl", "yacl_cc_library", "yacl_cc_test") package(default_visibility = ["//visibility:public"]) +# ==================== +# High-level interface +# ==================== + +# In most of the cases, we recommend to use this target +yacl_cc_library( + name = "aead", + srcs = ["aead.cc"], + hdrs = ["aead.h"], + deps = [ + ":all_gcm", + ":sm4_mte", + "//yacl:secparam", + "//yacl/base:int128", + "//yacl/crypto:key_utils", + ], +) + +yacl_cc_test( + name = "aead_test", + srcs = ["aead_test.cc"], + deps = [ + ":aead", + "//yacl/crypto/rand", + ], +) + +# ======================== +# Algorithm implementaions +# ======================== + yacl_cc_library( name = "all_gcm", srcs = ["all_gcm.cc"], diff --git a/yacl/crypto/aead/aead.cc b/yacl/crypto/aead/aead.cc new file mode 100644 index 00000000..04fea64b --- /dev/null +++ b/yacl/crypto/aead/aead.cc @@ -0,0 +1,163 @@ +// 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 "yacl/crypto/aead/aead.h" + +#include "yacl/crypto/aead/all_gcm.h" +#include "yacl/crypto/aead/sm4_mte.h" + +namespace yacl::crypto { + +size_t AeadCtx::GetKeySize(AeadAlgorithm algorithm) { + switch (algorithm) { + case AeadAlgorithm::AES128_GCM: + return kAes128GcmKeySize; + case AeadAlgorithm::AES256_GCM: + return kAes256GcmKeySize; +#ifdef YACL_WITH_TONGSUO + case AeadAlgorithm::SM4_GCM: + return kSm4GcmKeySize; +#endif + case AeadAlgorithm::SM4_MTE_HMAC_SM3: + return kSm4MteKeySize; + case AeadAlgorithm::UNKNOWN: + YACL_THROW("It seems you have set AeadAlgorithm::UNKNOWN, aborting"); + default: + YACL_THROW("Unknown AeadAlgorithm"); + } +} + +size_t AeadCtx::GetMacSize(AeadAlgorithm algorithm) { + switch (algorithm) { + case AeadAlgorithm::AES128_GCM: + return kAes128GcmKeySize; + case AeadAlgorithm::AES256_GCM: + return kAes256GcmKeySize; +#ifdef YACL_WITH_TONGSUO + case AeadAlgorithm::SM4_GCM: + return kSm4GcmKeySize; +#endif + case AeadAlgorithm::SM4_MTE_HMAC_SM3: + return kSm4MteMacCipherSize; + case AeadAlgorithm::UNKNOWN: + YACL_THROW("It seems you have set AeadAlgorithm::UNKNOWN, aborting"); + default: + YACL_THROW("Unknown AeadAlgorithm"); + } +} + +// Encrypts plaintext into ciphertext and mac. The input arguments +// are the AEAD algorithm, the plaintext, and the optional +// additional-authenticated-data (aad). +void AeadCtx::Encrypt(ByteContainerView plaintext, ByteContainerView key, + ByteContainerView iv, absl::Span ciphertext, + absl::Span mac, ByteContainerView aad) const { + Encrypt(algorithm_, plaintext, key, iv, ciphertext, mac, aad); +} + +// Decrypts ciphertext and mac into ciphertext. The input +// arguments are the AEAD algorithm, the ciphertext, the mac,and the optional +// additional-authenticated-data (aad). +void AeadCtx::Decrypt(ByteContainerView ciphertext, ByteContainerView mac, + ByteContainerView key, ByteContainerView iv, + absl::Span plaintext, + ByteContainerView aad) const { + Decrypt(algorithm_, ciphertext, mac, key, iv, plaintext, aad); +} + +// Staticlly encrypts plaintext into ciphertext and mac. The input arguments +// are the AEAD algorithm, the plaintext, the symmetric encryption key, the +// initialization vector (iv), and the optional additional-authenticated-data +// (aad). +void AeadCtx::Encrypt(AeadAlgorithm algorithm, ByteContainerView plaintext, + ByteContainerView key, ByteContainerView iv, + absl::Span ciphertext, absl::Span mac, + ByteContainerView aad) { + YACL_ENFORCE(algorithm != AeadAlgorithm::UNKNOWN); + switch (algorithm) { + case AeadAlgorithm::AES128_GCM: { + GcmCrypto(GcmCryptoSchema::AES128_GCM, key, iv) + .Encrypt(plaintext, aad, ciphertext, mac); + break; + } + case AeadAlgorithm::AES256_GCM: { + GcmCrypto(GcmCryptoSchema::AES256_GCM, key, iv) + .Encrypt(plaintext, aad, ciphertext, mac); + break; + } +#ifdef YACL_WITH_TONGSUO + case AeadAlgorithm::SM4_GCM: { + GcmCrypto(GcmCryptoSchema::SM4_GCM, key, iv) + .Encrypt(plaintext, aad, ciphertext, mac); + break; + } +#endif + case AeadAlgorithm::SM4_MTE_HMAC_SM3: { + // cipher is in the form of Enc(hmac || plaintext) + auto cipher = Sm4MteEncrypt(key, iv, plaintext); + YACL_ENFORCE_EQ(cipher.size(), ciphertext.size()); + memcpy(ciphertext.data(), cipher.data(), cipher.size()); + break; + } + case AeadAlgorithm::UNKNOWN: { + YACL_THROW("It seems you have set AeadAlgorithm::UNKNOWN, aborting"); + break; + } + default: + YACL_THROW("Unknown AeadAlgorithm"); + } +} + +// Staticlly decrypts ciphertext and mac into ciphertext. The input +// arguments are the AEAD algorithm, the ciphertext, the mac, the symmetric +// encryption key, the initialization vector (iv), and the optional +// additional-authenticated-data (aad). +void AeadCtx::Decrypt(AeadAlgorithm algorithm, ByteContainerView ciphertext, + ByteContainerView mac, ByteContainerView key, + ByteContainerView iv, absl::Span plaintext, + ByteContainerView aad) { + YACL_ENFORCE(algorithm != AeadAlgorithm::UNKNOWN); + switch (algorithm) { + case AeadAlgorithm::AES128_GCM: + GcmCrypto(GcmCryptoSchema::AES128_GCM, key, iv) + .Decrypt(ciphertext, aad, mac, plaintext); + break; + case AeadAlgorithm::AES256_GCM: + GcmCrypto(GcmCryptoSchema::AES256_GCM, key, iv) + .Decrypt(ciphertext, aad, mac, plaintext); + break; +#ifdef YACL_WITH_TONGSUO + case AeadAlgorithm::SM4_GCM: { + GcmCrypto(GcmCryptoSchema::SM4_GCM, key, iv) + .Decrypt(ciphertext, aad, mac, plaintext); + break; + } +#endif + case AeadAlgorithm::SM4_MTE_HMAC_SM3: { + // cipher is in the form of Enc(hmac || plaintext) + auto plain = Sm4MteDecrypt(key, iv, ciphertext); + YACL_ENFORCE_EQ(plain.size(), plaintext.size()); + memcpy(plaintext.data(), plain.data(), plain.size()); + break; + } + case AeadAlgorithm::UNKNOWN: { + YACL_THROW("It seems you have set AeadAlgorithm::UNKNOWN, aborting"); + break; + } + default: + YACL_THROW("Unknown AeadAlgorithm"); + } +} + +} // namespace yacl::crypto diff --git a/yacl/crypto/aead/aead.h b/yacl/crypto/aead/aead.h new file mode 100644 index 00000000..324df07f --- /dev/null +++ b/yacl/crypto/aead/aead.h @@ -0,0 +1,138 @@ +// 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 "absl/types/span.h" + +#include "yacl/base/byte_container_view.h" + +namespace yacl::crypto { + +// ============================================= +// AEAD: Authenticated Encryption And Decryption +// ============================================= +// +// AEAD provides confidentiality by encrypting the data with a symmetric +// encryption algorithm, and provides authenticity by using a MAC tag over the +// encrypted data. +// +// Related standards: +// + https://datatracker.ietf.org/doc/html/rfc5116 +// +// NOTE Strictly, mac-then-encrypt algorithm is not aead, we add those +// algorithms only for backword compatiability. + +enum class AeadAlgorithm : int { + UNKNOWN = 0, + AES128_GCM = 1, // Galois-Counter Mode + AES256_GCM = 2, // Galois-Counter Mode +#ifdef YACL_WITH_TONGSUO + SM4_GCM = 3, // NOTE only Yacl built with gm mode supports this feature +#endif + SM4_MTE_HMAC_SM3 = 4, // Mac-Then-Encrypt with SM4 counter mode +}; + +// Pre-defined default Aead algorithm for AeadCtx only +constexpr AeadAlgorithm kDefaultAeadAlgorithm = AeadAlgorithm::AES128_GCM; + +// AEAD Context Class +class AeadCtx { + public: + // Constructors + AeadCtx(); + explicit AeadCtx(AeadAlgorithm algorithm) { SetAlgorithm(algorithm); } + + // Get a default AeadCtx with the AeadAlgorithm set. This function could be + // seen as a helper function if you do not know which algorithm to choose. + // Yacl recommend the use of GetDefault(). + static AeadCtx& GetDefault() { + static AeadCtx ctx(kDefaultAeadAlgorithm); + return ctx; + } + + // Get the key size of the AEAD algorithm that is stored insize AeadCtx + size_t GetKeySize() { + YACL_ENFORCE(algorithm_ != AeadAlgorithm::UNKNOWN); + return GetKeySize(algorithm_); + } + + // Staticlly get the key size of an AEAD algorithm + static size_t GetKeySize(AeadAlgorithm algorithm); + + // Get the mac size of the AEAD algorithm that is stored insize AeadCtx + // + // NOTE in case of mac-then-encrypt algorithm, this function fetches the + // encrypted mac size + size_t GetMacSize() { + YACL_ENFORCE(algorithm_ != AeadAlgorithm::UNKNOWN); + return GetMacSize(algorithm_); + } + + // Staticlly get the mac size of an AEAD algorithm + // + // NOTE in case of mac-then-encrypt algorithm, this function fetches the + // encrypted mac size + static size_t GetMacSize(AeadAlgorithm algorithm); + + AeadAlgorithm GetAlgorithm() { return algorithm_; } + void SetAlgorithm(AeadAlgorithm algorithm) { algorithm_ = algorithm; } + + // Encrypts plaintext into ciphertext and mac. The input arguments + // are the AEAD algorithm, the plaintext, and the optional + // additional-authenticated-data (aad). + // + // NOTE Since Mac-Then-Encrypt results in one ciphertext, the argument "mac" + // is ignored for Mte algorithms + void Encrypt(ByteContainerView plaintext, ByteContainerView key, + ByteContainerView iv, absl::Span ciphertext, + absl::Span mac, ByteContainerView aad = "") const; + + // Decrypts ciphertext and mac into ciphertext. The input arguments are the + // AEAD algorithm, the ciphertext, the mac,and the optional + // additional-authenticated-data (aad). + // + // NOTE Since Mac-Then-Encrypt results in one ciphertext, the argument "mac" + // is ignored for Mte algorithms + void Decrypt(ByteContainerView ciphertext, ByteContainerView mac, + ByteContainerView key, ByteContainerView iv, + absl::Span plaintext, ByteContainerView aad = "") const; + + // Staticlly encrypts plaintext into ciphertext and mac. The input arguments + // are the AEAD algorithm, the plaintext, the symmetric encryption key, the + // initialization vector (iv), and the optional additional-authenticated-data + // (aad). + // + // NOTE Since Mac-Then-Encrypt results in one ciphertext, the argument "mac" + // is ignored for Mte algorithms + static void Encrypt(AeadAlgorithm algorithm, ByteContainerView plaintext, + ByteContainerView key, ByteContainerView iv, + absl::Span ciphertext, absl::Span mac, + ByteContainerView aad = ""); + + // Staticlly decrypts ciphertext and mac into ciphertext. The input + // arguments are the AEAD algorithm, the ciphertext, the mac, the symmetric + // encryption key, the initialization vector (iv), and the optional + // additional-authenticated-data (aad). + // + // NOTE Since Mac-Then-Encrypt results in one ciphertext, the argument "mac" + // is ignored for Mte algorithms + static void Decrypt(AeadAlgorithm algorithm, ByteContainerView ciphertext, + ByteContainerView mac, ByteContainerView key, + ByteContainerView iv, absl::Span plaintext, + ByteContainerView aad = ""); + + private: + AeadAlgorithm algorithm_ = AeadAlgorithm::UNKNOWN; // GCM crypto schema +}; + +} // namespace yacl::crypto diff --git a/yacl/crypto/aead/aead_test.cc b/yacl/crypto/aead/aead_test.cc new file mode 100644 index 00000000..41826120 --- /dev/null +++ b/yacl/crypto/aead/aead_test.cc @@ -0,0 +1,94 @@ +// 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/crypto/aead/aead.h" + +#include + +#include "gtest/gtest.h" + +#include "yacl/crypto/aead/sm4_mte.h" +#include "yacl/crypto/rand/rand.h" + +namespace yacl::crypto { + +constexpr char iv_96[] = "000000000000"; + +class AeadAlgorithmsTest : public testing::TestWithParam {}; + +// This will create multiple tests. +TEST_P(AeadAlgorithmsTest, EncryptDecrypt_ShouldOk) { + AeadAlgorithm algorithm = GetParam(); + auto key = FastRandBytes(AeadCtx::GetKeySize(algorithm)); + auto iv = ByteContainerView(iv_96, sizeof(iv_96) - 1); + + auto aead_cxt = AeadCtx(algorithm); + std::string plaintext = "I am a plaintext."; + std::string aad = "This is additional authenticated data."; + size_t additional_cipher_size = + algorithm == AeadAlgorithm::SM4_MTE_HMAC_SM3 ? kSm4MteMacCipherSize : 0; + std::vector ciphertext(plaintext.size() + additional_cipher_size); + std::vector mac(aead_cxt.GetMacSize()); + + aead_cxt.Encrypt(plaintext, key, iv, absl::MakeSpan(ciphertext), + absl::MakeSpan(mac), aad); + + std::vector decrypted(plaintext.size()); + + aead_cxt.Decrypt(ciphertext, mac, key, iv, absl::MakeSpan(decrypted), aad); + + EXPECT_EQ(plaintext, std::string(decrypted.begin(), decrypted.end())); +} + +TEST_P(AeadAlgorithmsTest, EncryptDecrypt_withErrorGMAC_ShouldThrowException) { + AeadAlgorithm algorithm = GetParam(); + auto key = FastRandBytes(AeadCtx::GetKeySize(algorithm)); + auto iv = ByteContainerView(iv_96, sizeof(iv_96) - 1); + + auto aead_cxt = AeadCtx(algorithm); + std::string plaintext = "I am a plaintext."; + std::string aad = "This is additional authenticated data."; + size_t additional_cipher_size = + algorithm == AeadAlgorithm::SM4_MTE_HMAC_SM3 ? kSm4MteMacCipherSize : 0; + std::vector ciphertext(plaintext.size() + additional_cipher_size); + std::vector mac(aead_cxt.GetMacSize()); + + aead_cxt.Encrypt(plaintext, key, iv, absl::MakeSpan(ciphertext), + absl::MakeSpan(mac), aad); + + std::vector decrypted(plaintext.size()); + + if (algorithm == AeadAlgorithm::SM4_MTE_HMAC_SM3) { + // wrong cipher, SM4_MTE_HMAC_SM3 does not have plaintext mac + ciphertext[0] += 1; + } else { + // wrong mac + mac[0] += 1; + } + + // THEN + EXPECT_ANY_THROW({ + aead_cxt.Decrypt(ciphertext, mac, key, iv, absl::MakeSpan(decrypted), aad); + }); +} + +INSTANTIATE_TEST_SUITE_P(AeadTest, AeadAlgorithmsTest, + testing::Values(AeadAlgorithm::AES128_GCM, + AeadAlgorithm::AES256_GCM, +#ifdef YACL_WITH_TONGSUO + AeadAlgorithm::SM4_GCM, +#endif + AeadAlgorithm::SM4_MTE_HMAC_SM3)); + +} // namespace yacl::crypto diff --git a/yacl/crypto/aead/all_gcm.cc b/yacl/crypto/aead/all_gcm.cc index 390560e2..0ef495b8 100644 --- a/yacl/crypto/aead/all_gcm.cc +++ b/yacl/crypto/aead/all_gcm.cc @@ -20,19 +20,16 @@ namespace yacl::crypto { namespace { -constexpr size_t kAesMacSize = 16; -// constexpr size_t kSm4MacSize = 16; - size_t GetMacSize(GcmCryptoSchema schema) { switch (schema) { case GcmCryptoSchema::AES128_GCM: + return kAes128GcmMacSize; case GcmCryptoSchema::AES256_GCM: - return kAesMacSize; + return kAes256GcmMacSize; #ifdef YACL_WITH_TONGSUO case GcmCryptoSchema::SM4_GCM: - return kAesMacSize; + return kSm4GcmMacSize; #endif - default: YACL_THROW("Unknown crypto schema: {}", static_cast(schema)); } @@ -44,7 +41,7 @@ void GcmCrypto::Encrypt(ByteContainerView plaintext, ByteContainerView aad, absl::Span ciphertext, absl::Span mac) const { YACL_ENFORCE_EQ(ciphertext.size(), plaintext.size()); - YACL_ENFORCE_EQ(mac.size(), GetMacSize(schema_)); + // YACL_ENFORCE_EQ(mac.size(), GetMacSize(schema_)); // init openssl evp cipher context auto ctx = openssl::UniqueCipherCtx(EVP_CIPHER_CTX_new()); @@ -72,6 +69,9 @@ void GcmCrypto::Encrypt(ByteContainerView plaintext, ByteContainerView aad, // Note that get no output here as the data is always aligned for GCM. EVP_EncryptFinal_ex(ctx.get(), nullptr, &out_length); + + YACL_ENFORCE(EVP_CIPHER_CTX_get_tag_length(ctx.get()) == + (int)GetMacSize(schema_)); OSSL_RET_1(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_GET_TAG, GetMacSize(schema_), mac.data())); } @@ -80,7 +80,7 @@ void GcmCrypto::Decrypt(ByteContainerView ciphertext, ByteContainerView aad, ByteContainerView mac, absl::Span plaintext) const { YACL_ENFORCE_EQ(ciphertext.size(), plaintext.size()); - YACL_ENFORCE_EQ(mac.size(), GetMacSize(schema_)); + // YACL_ENFORCE_EQ(mac.size(), GetMacSize(schema_)); // init openssl evp cipher context auto ctx = openssl::UniqueCipherCtx(EVP_CIPHER_CTX_new()); @@ -105,6 +105,8 @@ void GcmCrypto::Decrypt(ByteContainerView ciphertext, ByteContainerView aad, ciphertext.data(), ciphertext.size())); YACL_ENFORCE(out_length == (int)plaintext.size(), "Unexpcted decryption out length."); + YACL_ENFORCE(EVP_CIPHER_CTX_get_tag_length(ctx.get()) == + (int)GetMacSize(schema_)); OSSL_RET_1(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_SET_TAG, GetMacSize(schema_), (void*)mac.data())); diff --git a/yacl/crypto/aead/all_gcm.h b/yacl/crypto/aead/all_gcm.h index 601cbc37..4532bb8b 100644 --- a/yacl/crypto/aead/all_gcm.h +++ b/yacl/crypto/aead/all_gcm.h @@ -26,6 +26,21 @@ YACL_MODULE_DECLARE("all_gcm", SecParam::C::k128, SecParam::S::INF); namespace yacl::crypto { +// =============================== +// GCM Supported Schema/Algorithms +// =============================== + +constexpr size_t kAes128GcmMacSize = 16; +constexpr size_t kAes128GcmKeySize = 16; + +constexpr size_t kAes256GcmMacSize = 16; +constexpr size_t kAes256GcmKeySize = 32; + +#ifdef YACL_WITH_TONGSUO +constexpr size_t kSm4GcmMacSize = 16; +constexpr size_t kSm4GcmKeySize = 16; +#endif + enum class GcmCryptoSchema : int { AES128_GCM, /* security level = 128 */ AES256_GCM, /* security level = 256 */ @@ -34,9 +49,26 @@ enum class GcmCryptoSchema : int { #endif }; -// ------------- -// GCM Interface -// ------------- +/* to a string which openssl recognizes */ +inline const char* ToString(GcmCryptoSchema scheme) { + switch (scheme) { + case GcmCryptoSchema::AES128_GCM: + return "aes-128-gcm"; + case GcmCryptoSchema::AES256_GCM: + return "aes-256-gcm"; +#ifdef YACL_WITH_TONGSUO + case GcmCryptoSchema::SM4_GCM: + return "sm4-gcm"; +#endif + default: + YACL_THROW("Unsupported gcm scheme: {}", static_cast(scheme)); + } +} + +// ================ +// GCM Geneic Class +// ================ + class GcmCrypto { public: GcmCrypto(GcmCryptoSchema schema, ByteContainerView key, ByteContainerView iv) @@ -59,9 +91,10 @@ class GcmCrypto { const std::vector iv_; // Initialize vector }; -// --------------- -// Implementations -// --------------- +// ======================== +// Alias for Specific Types +// ======================== + class Aes128GcmCrypto : public GcmCrypto { public: Aes128GcmCrypto(ByteContainerView key, ByteContainerView iv) @@ -82,20 +115,4 @@ class Sm4GcmCrypto : public GcmCrypto { }; #endif -/* to a string which openssl recognizes */ -inline const char* ToString(GcmCryptoSchema scheme) { - switch (scheme) { - case GcmCryptoSchema::AES128_GCM: - return "aes-128-gcm"; - case GcmCryptoSchema::AES256_GCM: - return "aes-256-gcm"; -#ifdef YACL_WITH_TONGSUO - case GcmCryptoSchema::SM4_GCM: - return "sm4-gcm"; -#endif - default: - YACL_THROW("Unsupported gcm scheme: {}", static_cast(scheme)); - } -} - } // namespace yacl::crypto diff --git a/yacl/crypto/aead/sm4_mte.cc b/yacl/crypto/aead/sm4_mte.cc index 63226aa0..98c1922b 100644 --- a/yacl/crypto/aead/sm4_mte.cc +++ b/yacl/crypto/aead/sm4_mte.cc @@ -18,13 +18,6 @@ namespace yacl::crypto { -namespace { - -constexpr size_t HMAC_SIZE = 32; -constexpr size_t HMAC_KEY_SIZE = 16; - -} // namespace - std::vector Sm4MteEncrypt(ByteContainerView key, ByteContainerView iv, ByteContainerView plaintext) { // Step 1. get hash of (iv || key) @@ -32,9 +25,9 @@ std::vector Sm4MteEncrypt(ByteContainerView key, ByteContainerView iv, Sm3Hash().Update(iv).Update(key).CumulativeHash(); // Step2. get hmac of plaintext - YACL_ENFORCE_GE(iv_key_hash.size(), HMAC_KEY_SIZE); + YACL_ENFORCE_GE(iv_key_hash.size(), kSm4MteKeySize); std::vector hmac_plaintext = - HmacSm3(ByteContainerView(iv_key_hash.data(), HMAC_KEY_SIZE)) + HmacSm3(ByteContainerView(iv_key_hash.data(), kSm4MteKeySize)) .Update(plaintext) .CumulativeMac(); hmac_plaintext.insert(hmac_plaintext.end(), plaintext.begin(), @@ -54,17 +47,19 @@ std::vector Sm4MteDecrypt(ByteContainerView key, ByteContainerView iv, std::vector hmac_plaintext(ciphertext.size()); SymmetricCrypto(SymmetricCrypto::CryptoType::SM4_CTR, key, iv) .Decrypt(ciphertext, absl::MakeSpan(hmac_plaintext)); - YACL_ENFORCE_GT(hmac_plaintext.size(), HMAC_SIZE); - ByteContainerView hmac_from_cipher(hmac_plaintext.data(), HMAC_SIZE); - ByteContainerView plaintext_from_cipher(hmac_plaintext.data() + HMAC_SIZE, - hmac_plaintext.size() - HMAC_SIZE); + YACL_ENFORCE_GT(hmac_plaintext.size(), kSm4MteMacCipherSize); + ByteContainerView hmac_from_cipher(hmac_plaintext.data(), + kSm4MteMacCipherSize); + ByteContainerView plaintext_from_cipher( + hmac_plaintext.data() + kSm4MteMacCipherSize, + hmac_plaintext.size() - kSm4MteMacCipherSize); // Step 2. cal hmac std::vector iv_key_hash = Sm3Hash().Update(iv).Update(key).CumulativeHash(); - YACL_ENFORCE_GE(iv_key_hash.size(), HMAC_KEY_SIZE); + YACL_ENFORCE_GE(iv_key_hash.size(), kSm4MteKeySize); std::vector hmac_actual = - HmacSm3(ByteContainerView(iv_key_hash.data(), HMAC_KEY_SIZE)) + HmacSm3(ByteContainerView(iv_key_hash.data(), kSm4MteKeySize)) .Update(plaintext_from_cipher) .CumulativeMac(); @@ -72,7 +67,7 @@ std::vector Sm4MteDecrypt(ByteContainerView key, ByteContainerView iv, YACL_ENFORCE_EQ(hmac_actual.size(), hmac_from_cipher.size()); YACL_ENFORCE(std::equal(hmac_actual.begin(), hmac_actual.end(), hmac_from_cipher.begin())); - return {hmac_plaintext.begin() + HMAC_SIZE, hmac_plaintext.end()}; + return {hmac_plaintext.begin() + kSm4MteMacCipherSize, hmac_plaintext.end()}; } } // namespace yacl::crypto diff --git a/yacl/crypto/aead/sm4_mte.h b/yacl/crypto/aead/sm4_mte.h index ff7b72d3..02006115 100644 --- a/yacl/crypto/aead/sm4_mte.h +++ b/yacl/crypto/aead/sm4_mte.h @@ -29,6 +29,9 @@ YACL_MODULE_DECLARE("sm4_mac", SecParam::C::k128, SecParam::S::INF); namespace yacl::crypto { +constexpr size_t kSm4MteMacCipherSize = 32; // the encrypted sm3 hmac size +constexpr size_t kSm4MteKeySize = 16; + // SM4-CTR based MAC then Encryption(Mte). Hmac-sm3 is used as MAC algorithm // here. diff --git a/yacl/crypto/aes/BUILD.bazel b/yacl/crypto/aes/BUILD.bazel index 537b06fa..a45d26f6 100644 --- a/yacl/crypto/aes/BUILD.bazel +++ b/yacl/crypto/aes/BUILD.bazel @@ -24,7 +24,7 @@ yacl_cc_binary( ":aes_opt", "//yacl/crypto/rand", "//yacl/crypto/tools:prg", - "@com_github_google_benchmark//:benchmark_main", + "@google_benchmark//:benchmark_main", ], ) @@ -39,7 +39,7 @@ yacl_cc_library( "//yacl/base:int128", ] + select({ "@platforms//cpu:aarch64": [ - "@com_github_dltcollab_sse2neon//:sse2neon", + "@sse2neon", ], "//conditions:default": [], }), diff --git a/yacl/crypto/ecc/BUILD.bazel b/yacl/crypto/ecc/BUILD.bazel index 9c29047d..c8b6a862 100644 --- a/yacl/crypto/ecc/BUILD.bazel +++ b/yacl/crypto/ecc/BUILD.bazel @@ -77,7 +77,7 @@ yacl_cc_library( ], deps = [ "//yacl/base:exception", - "@com_google_absl//absl/strings", + "@abseil-cpp//absl/strings", ], ) @@ -88,7 +88,7 @@ yacl_cc_test( ], deps = [ ":ecc", - "@com_github_fmtlib_fmt//:fmtlib", + "@fmt", "@yacl//yacl/utils:parallel", ], ) diff --git a/yacl/crypto/ecc/FourQlib/BUILD.bazel b/yacl/crypto/ecc/FourQlib/BUILD.bazel index d0138bfb..767dbf66 100644 --- a/yacl/crypto/ecc/FourQlib/BUILD.bazel +++ b/yacl/crypto/ecc/FourQlib/BUILD.bazel @@ -30,7 +30,7 @@ yacl_cc_library( deps = [ "//yacl/crypto/ecc:spi", "//yacl/crypto/hash:ssl_hash", - "@com_github_microsoft_FourQlib//:FourQlib", + "@fourqlib//:FourQlib", ], ) diff --git a/yacl/crypto/ecc/benchmark/BUILD.bazel b/yacl/crypto/ecc/benchmark/BUILD.bazel index 8c0c3685..69f24510 100644 --- a/yacl/crypto/ecc/benchmark/BUILD.bazel +++ b/yacl/crypto/ecc/benchmark/BUILD.bazel @@ -21,8 +21,8 @@ yacl_cc_binary( srcs = ["bench_ecc.cc"], deps = [ "//yacl/crypto/ecc", - "@com_github_gflags_gflags//:gflags", - "@com_github_google_benchmark//:benchmark", - "@com_google_absl//absl/strings", + "@abseil-cpp//absl/strings", + "@gflags", + "@google_benchmark//:benchmark", ], ) diff --git a/yacl/crypto/ecc/benchmark/bench_ecc.cc b/yacl/crypto/ecc/benchmark/bench_ecc.cc index 8b77e4a7..1d746717 100644 --- a/yacl/crypto/ecc/benchmark/bench_ecc.cc +++ b/yacl/crypto/ecc/benchmark/bench_ecc.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include + #include "absl/strings/str_split.h" #include "benchmark/benchmark.h" #include "gflags/gflags.h" @@ -181,10 +183,14 @@ void InitAndRunBenchmarks() { } // namespace yacl::crypto::bench int main(int argc, char** argv) { - google::ParseCommandLineFlags(&argc, &argv, true); - benchmark::Initialize(&argc, argv); - yacl::crypto::bench::InitAndRunBenchmarks(); - benchmark::RunSpecifiedBenchmarks(); - benchmark::Shutdown(); + try { + google::ParseCommandLineFlags(&argc, &argv, true); + benchmark::Initialize(&argc, argv); + yacl::crypto::bench::InitAndRunBenchmarks(); + benchmark::RunSpecifiedBenchmarks(); + benchmark::Shutdown(); + } catch (std::exception& e) { + YACL_THROW("Something is wrong!"); + } return 0; } diff --git a/yacl/crypto/ecc/libsodium/BUILD.bazel b/yacl/crypto/ecc/libsodium/BUILD.bazel index d2726a7c..1c7dcde7 100644 --- a/yacl/crypto/ecc/libsodium/BUILD.bazel +++ b/yacl/crypto/ecc/libsodium/BUILD.bazel @@ -40,7 +40,7 @@ yacl_cc_library( deps = [ "//yacl/crypto/ecc:spi", "//yacl/crypto/hash:ssl_hash", - "@com_github_libsodium//:libsodium", + "@libsodium", "@yacl//yacl/utils:parallel", ], ) diff --git a/yacl/crypto/ecc/mcl/BUILD.bazel b/yacl/crypto/ecc/mcl/BUILD.bazel index 132f872e..a609b585 100644 --- a/yacl/crypto/ecc/mcl/BUILD.bazel +++ b/yacl/crypto/ecc/mcl/BUILD.bazel @@ -49,7 +49,7 @@ yacl_cc_library( hdrs = ["mcl_util.h"], deps = [ "//yacl/math/mpint", - "@com_github_herumi_mcl//:mcl", + "@mcl", ], ) diff --git a/yacl/crypto/ecc/openssl/openssl_factory.cc b/yacl/crypto/ecc/openssl/openssl_factory.cc index 363d28b5..312a811b 100644 --- a/yacl/crypto/ecc/openssl/openssl_factory.cc +++ b/yacl/crypto/ecc/openssl/openssl_factory.cc @@ -118,20 +118,20 @@ std::map kName2Nid = { {"wap-wsg-idm-ecid-wtls12", NID_wap_wsg_idm_ecid_wtls12}, {"Oakley Group 3", NID_ipsec3}, {"Oakley Group 4", NID_ipsec4}, - {"brainpoolP160r1", NID_brainpoolP160r1}, - {"brainpoolP160t1", NID_brainpoolP160t1}, - {"brainpoolP192r1", NID_brainpoolP192r1}, - {"brainpoolP192t1", NID_brainpoolP192t1}, - {"brainpoolP224r1", NID_brainpoolP224r1}, - {"brainpoolP224t1", NID_brainpoolP224t1}, - {"brainpoolP256r1", NID_brainpoolP256r1}, - {"brainpoolP256t1", NID_brainpoolP256t1}, - {"brainpoolP320r1", NID_brainpoolP320r1}, - {"brainpoolP320t1", NID_brainpoolP320t1}, - {"brainpoolP384r1", NID_brainpoolP384r1}, - {"brainpoolP384t1", NID_brainpoolP384t1}, - {"brainpoolP512r1", NID_brainpoolP512r1}, - {"brainpoolP512t1", NID_brainpoolP512t1}, + {"brainpoolp160r1", NID_brainpoolP160r1}, + {"brainpoolp160t1", NID_brainpoolP160t1}, + {"brainpoolp192r1", NID_brainpoolP192r1}, + {"brainpoolp192t1", NID_brainpoolP192t1}, + {"brainpoolp224r1", NID_brainpoolP224r1}, + {"brainpoolp224t1", NID_brainpoolP224t1}, + {"brainpoolp256r1", NID_brainpoolP256r1}, + {"brainpoolp256t1", NID_brainpoolP256t1}, + {"brainpoolp320r1", NID_brainpoolP320r1}, + {"brainpoolp320t1", NID_brainpoolP320t1}, + {"brainpoolp384r1", NID_brainpoolP384r1}, + {"brainpoolp384t1", NID_brainpoolP384t1}, + {"brainpoolp512r1", NID_brainpoolP512r1}, + {"brainpoolp512t1", NID_brainpoolP512t1}, {"sm2", NID_sm2}, }; diff --git a/yacl/crypto/ecc/openssl/openssl_test.cc b/yacl/crypto/ecc/openssl/openssl_test.cc index 76c25dc0..ee16d4fd 100644 --- a/yacl/crypto/ecc/openssl/openssl_test.cc +++ b/yacl/crypto/ecc/openssl/openssl_test.cc @@ -102,7 +102,7 @@ TEST(OpensslTest, AddInplaceWorks) { TEST(OpensslMemLeakTest, MulBaseLeaks) { std::shared_ptr ec = - yacl::crypto::EcGroupFactory::Instance().Create("sm2", + yacl::crypto::EcGroupFactory::Instance().Create("brainpoolP256r1", ArgLib = "openssl"); yacl::parallel_for(0, 2, [&](int64_t, int64_t) { diff --git a/yacl/crypto/ecc/toy/montgomery.cc b/yacl/crypto/ecc/toy/montgomery.cc index 35251951..46894859 100644 --- a/yacl/crypto/ecc/toy/montgomery.cc +++ b/yacl/crypto/ecc/toy/montgomery.cc @@ -74,6 +74,7 @@ EcPoint ToyXGroup::Mul(const EcPoint &point, const MPInt &k) const { auto z_3 = x_2; int8_t swap = 0; + YACL_ENFORCE(params_.p.BitCount() > 0); for (int t = params_.p.BitCount() - 1; t >= 0; --t) { int8_t k_t = scalar[t]; swap ^= k_t; diff --git a/yacl/crypto/envelope/BUILD.bazel b/yacl/crypto/envelope/BUILD.bazel index 21426197..5341e627 100644 --- a/yacl/crypto/envelope/BUILD.bazel +++ b/yacl/crypto/envelope/BUILD.bazel @@ -29,8 +29,8 @@ yacl_cc_library( "//yacl/crypto/pke:rsa_enc", "//yacl/crypto/pke:sm2_enc", "//yacl/crypto/rand", - "@com_google_absl//absl/strings", - "@com_google_absl//absl/types:span", + "@abseil-cpp//absl/strings", + "@abseil-cpp//absl/types:span", ], ) diff --git a/yacl/crypto/experimental/dpf/dcf.h b/yacl/crypto/experimental/dpf/dcf.h index 447f362b..38b67e0e 100644 --- a/yacl/crypto/experimental/dpf/dcf.h +++ b/yacl/crypto/experimental/dpf/dcf.h @@ -78,8 +78,8 @@ class DcfKey { void SetV(uint128_t v) { this->v_ = v; } private: - uint128_t seed_ = 0; // this level's seed, default = 0 - uint128_t v_; + uint128_t seed_ = 0; // this level's seed, default = 0 + uint128_t v_{0}; // default to 0 uint8_t t_store_ = 0; // 1st bit=> t_left, 2nd bit=> t_right }; diff --git a/yacl/crypto/experimental/dpf/ge2n.h b/yacl/crypto/experimental/dpf/ge2n.h index 641a8796..f2bdc752 100644 --- a/yacl/crypto/experimental/dpf/ge2n.h +++ b/yacl/crypto/experimental/dpf/ge2n.h @@ -98,7 +98,7 @@ class GE2n { private: static constexpr StoreTy kMask_ = N == 128 ? Uint128Max() : (StoreTy(1) << N) - 1; - StoreTy store_; + StoreTy store_{0}; }; } // namespace yacl::crypto diff --git a/yacl/crypto/experimental/dpf/pprf.cc b/yacl/crypto/experimental/dpf/pprf.cc index bc42e9b7..95f410a0 100644 --- a/yacl/crypto/experimental/dpf/pprf.cc +++ b/yacl/crypto/experimental/dpf/pprf.cc @@ -73,8 +73,9 @@ void GgmExpandAndPunc(absl::Span working_span, GE2n punc_point, template void PprfPunc(uint128_t prf_key, GE2n punc_point, PprfPuncKey* out) { static_assert(M <= 64); - auto m = M; // m is a runtime var - auto num = (m == 64) ? std::numeric_limits::max() : 1 << m; + uint64_t m = M; // m is a runtime var + uint64_t num = + (m == 64) ? std::numeric_limits::max() : (uint64_t)1 << m; std::vector working_vec(num); working_vec[0] = prf_key; diff --git a/yacl/crypto/experimental/sync_drbg/BUILD.bazel b/yacl/crypto/experimental/sync_drbg/BUILD.bazel index f7026281..a247b736 100644 --- a/yacl/crypto/experimental/sync_drbg/BUILD.bazel +++ b/yacl/crypto/experimental/sync_drbg/BUILD.bazel @@ -28,7 +28,7 @@ yacl_cc_library( "//yacl/link:context", "//yacl/link:test_util", "//yacl/utils:serializer", - "@com_github_greendow_hash_drbg//:hash_drbg", + "@hash_drbg", ], ) diff --git a/yacl/crypto/experimental/sync_drbg/sync_drbg.h b/yacl/crypto/experimental/sync_drbg/sync_drbg.h index 7c9697e1..e69261de 100644 --- a/yacl/crypto/experimental/sync_drbg/sync_drbg.h +++ b/yacl/crypto/experimental/sync_drbg/sync_drbg.h @@ -21,8 +21,8 @@ #include #include -#include "hash_drbg.h" // from @com_github_greendow_hash_drbg//:hash_drbg -#include "hash_drbg_error_codes.h" // from @com_github_greendow_hash_drbg//:hash_drbg +#include "hash_drbg.h" // from @hash_drbg//:hash_drbg +#include "hash_drbg_error_codes.h" // from @hash_drbg//:hash_drbg #include "spdlog/spdlog.h" #include "yacl/base/byte_container_view.h" diff --git a/yacl/crypto/experimental/tpre/BUILD.bazel b/yacl/crypto/experimental/tpre/BUILD.bazel index 8ea8f1fe..6e748139 100644 --- a/yacl/crypto/experimental/tpre/BUILD.bazel +++ b/yacl/crypto/experimental/tpre/BUILD.bazel @@ -100,7 +100,7 @@ yacl_cc_test( "//yacl/crypto/ecc:spi", "//yacl/math/mpint", "//yacl/utils:scope_guard", - "@com_google_absl//absl/strings", + "@abseil-cpp//absl/strings", ], ) @@ -129,6 +129,6 @@ yacl_cc_test( ":tpre", "//yacl/crypto/ecc", "//yacl/crypto/ecc:spi", - "@com_google_absl//absl/strings", + "@abseil-cpp//absl/strings", ], ) diff --git a/yacl/crypto/hash/BUILD.bazel b/yacl/crypto/hash/BUILD.bazel index 292a9a6e..d3d2f26f 100644 --- a/yacl/crypto/hash/BUILD.bazel +++ b/yacl/crypto/hash/BUILD.bazel @@ -44,7 +44,7 @@ yacl_cc_library( ":hash_interface", "//yacl/base:exception", "//yacl/base:int128", - "@com_github_blake3team_blake3//:blake3_c", + "@blake3", ], ) @@ -61,7 +61,7 @@ yacl_cc_binary( srcs = ["blake3_bench.cc"], deps = [ ":blake3", - "@com_github_google_benchmark//:benchmark_main", + "@google_benchmark//:benchmark_main", ], ) @@ -82,6 +82,6 @@ yacl_cc_library( deps = [ ":ssl_hash", "//yacl/base:int128", - "@com_github_blake3team_blake3//:blake3_c", + "@blake3", ], ) diff --git a/yacl/crypto/hash/blake3_test.cc b/yacl/crypto/hash/blake3_test.cc index 542e9ea5..64f170ba 100644 --- a/yacl/crypto/hash/blake3_test.cc +++ b/yacl/crypto/hash/blake3_test.cc @@ -53,9 +53,7 @@ TestData test_data_blake3 = { TEST(Blake3HashTest, TestVector1) { Blake3Hash blake3; - std::string vector_bytes; - - ASSERT_TRUE(absl::HexStringToBytes(test_data_blake3.vector1, &vector_bytes)); + std::string vector_bytes = absl::HexStringToBytes(test_data_blake3.vector1); std::vector hash = blake3.Update(vector_bytes).CumulativeHash(); @@ -66,8 +64,7 @@ TEST(Blake3HashTest, TestVector1) { TEST(Blake3HashTest, TestVector2) { Blake3Hash blake3; - std::string vector_bytes; - ASSERT_TRUE(absl::HexStringToBytes(test_data_blake3.vector2, &vector_bytes)); + std::string vector_bytes = absl::HexStringToBytes(test_data_blake3.vector2); std::vector hash = blake3.Update(vector_bytes).CumulativeHash(); @@ -80,10 +77,8 @@ TEST(Blake3HashTest, TestVector2) { // a clean state, allowing a new hash operation to take place. TEST(Blake3HashTest, ResetBetweenUpdates) { Blake3Hash blake3; - std::string vector1_bytes; - ASSERT_TRUE(absl::HexStringToBytes(test_data_blake3.vector1, &vector1_bytes)); - std::string vector2_bytes; - ASSERT_TRUE(absl::HexStringToBytes(test_data_blake3.vector2, &vector2_bytes)); + std::string vector1_bytes = absl::HexStringToBytes(test_data_blake3.vector1); + std::string vector2_bytes = absl::HexStringToBytes(test_data_blake3.vector2); std::vector hash = blake3.Update(vector1_bytes) .Reset() .Update(vector2_bytes) @@ -97,10 +92,8 @@ TEST(Blake3HashTest, ResetBetweenUpdates) { // calls to Update. TEST(Blake3HashTest, MultipleUpdates) { Blake3Hash blake3; - std::string vector1_bytes; - ASSERT_TRUE(absl::HexStringToBytes(test_data_blake3.vector1, &vector1_bytes)); - std::string suffix_bytes; - ASSERT_TRUE(absl::HexStringToBytes(test_data_blake3.suffix, &suffix_bytes)); + std::string vector1_bytes = absl::HexStringToBytes(test_data_blake3.vector1); + std::string suffix_bytes = absl::HexStringToBytes(test_data_blake3.suffix); std::vector result = blake3.Update(vector1_bytes).CumulativeHash(); EXPECT_EQ(absl::BytesToHexString( @@ -122,9 +115,8 @@ TEST(Blake3HashTest, CustomOutLength) { for (size_t i = 0; i <= (8 * BLAKE3_OUT_LEN); i++) { Blake3Hash blake3(i); - std::string vector1_bytes; - ASSERT_TRUE( - absl::HexStringToBytes(test_data_blake3.vector1, &vector1_bytes)); + std::string vector1_bytes = + absl::HexStringToBytes(test_data_blake3.vector1); // Shorter outputs are prefixes of longer ones. // reference @@ -153,8 +145,7 @@ TEST(Blake3HashTest, MaximumLength) { Blake3Hash blake3(max_size); - std::string vector1_bytes; - ASSERT_TRUE(absl::HexStringToBytes(test_data_blake3.vector1, &vector1_bytes)); + std::string vector1_bytes = absl::HexStringToBytes(test_data_blake3.vector1); auto len = std::min(max_size, static_cast(BLAKE3_OUT_LEN)); diff --git a/yacl/crypto/hash/hash_interface.h b/yacl/crypto/hash/hash_interface.h index 322e29ec..2e173091 100644 --- a/yacl/crypto/hash/hash_interface.h +++ b/yacl/crypto/hash/hash_interface.h @@ -41,7 +41,9 @@ enum class HashAlgorithm : int { #ifndef YACL_WITH_TONGSUO BLAKE2B = 7, // blake2 is disabled by tongsuo #endif - BLAKE3 = 8 + BLAKE3 = 8, + + SHAKE512 = 9, }; // HashInterface defines an interface for hash functions. @@ -105,6 +107,8 @@ inline const char *ToString(HashAlgorithm hash_algo) { case HashAlgorithm::BLAKE2B: return "blake2b-512"; #endif + case HashAlgorithm::SHAKE512: + return "shake-512"; default: YACL_THROW("Unsupported hash algo: {}", static_cast(hash_algo)); } diff --git a/bazel/libtommath.BUILD b/yacl/crypto/oprf/BUILD.bazel similarity index 52% rename from bazel/libtommath.BUILD rename to yacl/crypto/oprf/BUILD.bazel index 3e427e9b..eca74f75 100644 --- a/bazel/libtommath.BUILD +++ b/yacl/crypto/oprf/BUILD.bazel @@ -1,4 +1,4 @@ -# Copyright 2023 Ant Group Co., Ltd. +# 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. @@ -12,22 +12,34 @@ # See the License for the specific language governing permissions and # limitations under the License. -load("@yacl//bazel:yacl.bzl", "yacl_cmake_external") +load("//bazel:yacl.bzl", "yacl_cc_library", "yacl_cc_test") package(default_visibility = ["//visibility:public"]) -filegroup( - name = "all_srcs", - srcs = glob(["**"]), +yacl_cc_library( + name = "oprf_ctx", + srcs = ["oprf_ctx.cc"], + hdrs = ["oprf_ctx.h"], + deps = [ + "//yacl/base:exception", + "//yacl/crypto/ecc", + "//yacl/crypto/hash:ssl_hash", + ], ) -yacl_cmake_external( - name = "libtommath", - cache_entries = { - "CMAKE_INSTALL_LIBDIR": "lib", - "CMAKE_POSITION_INDEPENDENT_CODE": "ON", - }, - copts = ["-Wno-error"], - lib_source = ":all_srcs", - out_static_libs = ["libtommath.a"], +yacl_cc_library( + name = "oprf", + # srcs = ["oprf.cc"], + hdrs = ["oprf.h"], + deps = [ + ":oprf_ctx", + ], +) + +yacl_cc_test( + name = "oprf_test", + srcs = ["oprf_test.cc"], + deps = [ + ":oprf", + ], ) diff --git a/yacl/crypto/oprf/oprf.h b/yacl/crypto/oprf/oprf.h new file mode 100644 index 00000000..d9a85c1a --- /dev/null +++ b/yacl/crypto/oprf/oprf.h @@ -0,0 +1,201 @@ +// 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 + +#include "yacl/base/byte_container_view.h" +#include "yacl/crypto/ecc/ec_point.h" +#include "yacl/crypto/hash/ssl_hash.h" +#include "yacl/crypto/oprf/oprf_ctx.h" +#include "yacl/math/mpint/mp_int.h" + +namespace yacl::crypto { + +// RFC 9497: oblivious pseudorandom function (OPRF) using prime-order groups +// +// Client(input) Server(skS) +// ------------------------------------------------------------------- +// blind, blindedElement = Blind(input) +// +// blindedElement +// ----------> +// +// evaluatedElement = BlindEvaluate(skS, blindedElement) +// +// evaluatedElement +// <---------- +// +// output = Finalize(input, blind, evaluatedElement) + +class OprfServer { + public: + // Default constructor (with null OprfCtx) + OprfServer() = default; + + // Construct OprfServer from OprfConfig + explicit OprfServer(const OprfConfig& config) + : ctx_(std::make_shared(config)) { + RefreshBlind(); + } + + // Construct OprfServer from Existing OprfCtx (implicitly copied) + explicit OprfServer(const std::shared_ptr& ctx) : ctx_(ctx) { + RefreshBlind(); + } + + // Construct OprfServer from Existing OprfCtx (explicitly moved) + explicit OprfServer(std::shared_ptr&& ctx) : ctx_(std::move(ctx)) { + RefreshBlind(); + } + + // Setup OprfServer context, the new OprfCtx would overwrite the previous + // OprfCtx, but previous OprfCtx may not be fully released from memory. + void SetupCtx(const OprfConfig& config) { + ctx_ = std::make_shared(config); + RefreshBlind(); + } + + void BlindEvaluate(const EcPoint& in, EcPoint* out) { + YACL_ENFORCE(ctx_ != nullptr); // make sure context is setup + YACL_ENFORCE(out != nullptr); // make sure out is not nullptr + + auto* const ec = ctx_->BorrowEcGroup(); + *out = ec->Mul(in, blind_); + } + + // Refresh the internally stored blind to a random value. You need to setup + // OprfCtx before calling RefreshBlind() + void RefreshBlind() { + YACL_ENFORCE(ctx_ != nullptr); // make sure context is setup + auto* const ec = ctx_->BorrowEcGroup(); + math::MPInt::RandomLtN(ec->GetOrder(), &blind_); + } + + // Clear the internally stored blind value to zero + void ClearBlind() { blind_ = 0_mp; } + + private: + // NOTE oprf ctx may be reused by different oprf instance + std::shared_ptr ctx_; + + // Security-related values + math::MPInt blind_; +}; + +class OprfClient { + public: + // Default constructor (with null OprfCtx) + OprfClient() = default; + + // Construct OprfClient from OprfConfig + explicit OprfClient(const OprfConfig& config) + : ctx_(std::make_shared(config)) { + RefreshBlind(); + } + + // Construct OprfClient from Existing OprfCtx (implicitly copied) + explicit OprfClient(const std::shared_ptr& ctx) : ctx_(ctx) { + RefreshBlind(); + } + + // Construct OprfClient from Existing OprfCtx (explicitly moved) + explicit OprfClient(std::shared_ptr&& ctx) : ctx_(std::move(ctx)) { + RefreshBlind(); + } + + // Setup OprfClient context, the new OprfCtx would overwrite the previous + // OprfCtx, but previous OprfCtx may not be fully released from memory. + void SetupCtx(const OprfConfig& config) { + ctx_ = std::make_shared(config); + RefreshBlind(); + }; + + // Hash to map and then blind the string + void Blind(const std::string& in, EcPoint* out) { + YACL_ENFORCE(ctx_ != nullptr); // make sure context is setup + YACL_ENFORCE(out != nullptr); // make sure out is not nullptr + + auto* const ec = ctx_->BorrowEcGroup(); + EcPoint in_point = ec->HashToCurve(in); + *out = ec->Mul(in_point, blind_); + } + + std::vector Finalize(const EcPoint& in, + const std::string& private_input = "") { + YACL_ENFORCE(ctx_ != nullptr); // make sure context is setup + + auto* const ec = ctx_->BorrowEcGroup(); + if (blind_inv_ != 0_mp) { + MPInt::InvertMod(blind_, ec->GetOrder(), &blind_inv_); + } + + // FIXME https://www.rfc-editor.org/rfc/rfc9496#section-4.3.2 + auto point_buf = ec->SerializePoint(ec->Mul(in, blind_inv_)); + + const std::string kPhaseStr = "Finalize"; + Buffer hash_buf(2 + private_input.size() + 2 + point_buf.size() + + kPhaseStr.size()); + char* p = hash_buf.data(); + + // copy len of private input + YACL_ENFORCE(private_input.size() <= (1 << 16)); + uint64_t len = private_input.size(); + std::memcpy(p, &len, 2); + p += 2; + + // copy private_input + snprintf(p, private_input.size(), "%s", private_input.data()); + p += private_input.size(); + + // copy len of point_buf + YACL_ENFORCE(point_buf.size() <= (1 << 16)); + len = point_buf.size(); + std::memcpy(p, &len, 2); + p += 2; + + // copy point_buf + std::memcpy(p, point_buf.data(), point_buf.size()); + p += point_buf.size(); + + // final step: copy phase string + snprintf(p, kPhaseStr.size(), "%s", kPhaseStr.data()); + + // hash every thing in hash_buf + return SslHash(ctx_->GetHashAlgorithm()).Update(hash_buf).CumulativeHash(); + } + + void RefreshBlind() { + YACL_ENFORCE(ctx_ != nullptr); // make sure context is setup + auto* const ec = ctx_->BorrowEcGroup(); + math::MPInt::RandomLtN(ec->GetOrder(), &blind_); + } + + // Clear the internally stored blind value to zero + void ClearBlind() { + blind_ = 0_mp; + blind_inv_ = 0_mp; + } + + private: + // NOTE oprf ctx may be reused by different oprf instance + std::shared_ptr ctx_; + + // Security-related values + math::MPInt blind_; + math::MPInt blind_inv_; +}; + +} // namespace yacl::crypto diff --git a/yacl/crypto/oprf/oprf_ctx.cc b/yacl/crypto/oprf/oprf_ctx.cc new file mode 100644 index 00000000..9747afd5 --- /dev/null +++ b/yacl/crypto/oprf/oprf_ctx.cc @@ -0,0 +1,75 @@ +// 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 "yacl/crypto/oprf/oprf_ctx.h" + +#include "yacl/crypto/hash/ssl_hash.h" + +namespace yacl::crypto { + +std::pair OprfCtx::GenKeyPair() { + SkTy sk; + math::MPInt::RandomLtN(ec_->GetOrder(), &sk); + PkTy pk = ec_->MulBase(sk); + return {sk, pk}; +} + +std::pair OprfCtx::DeriveKeyPair( + uint128_t seed, const std::string& info) { + constexpr std::string_view kDst = "DeriveKeyPair"; + + YACL_ENFORCE(info.size() < (1 << 16)); + uint16_t info_size = info.size(); + uint8_t counter = 0; + uint64_t n = sizeof(seed) + sizeof(info_size) + info_size + 1 + kDst.size() + + ctx_str_.size(); + Buffer derive_input(static_cast(n)); + char* p = derive_input.data(); + + // copy seed + std::memcpy(p, &seed, sizeof(uint128_t)); + p += sizeof(uint128_t); + + // copy info size + std::memcpy(p, &info_size, sizeof(info_size)); + p += sizeof(info_size); + + // copy info + snprintf(p, info.size(), "%s", info.data()); + p += info_size; + + // copy counter + *p = static_cast(counter); + p++; + + // copy dst + std::memcpy(p, kDst.data(), kDst.size()); + p += kDst.size(); + + // copy ctx_str + std::memcpy(p, ctx_str_.data(), ctx_str_.size()); + + SkTy sk; + PkTy pk; + while (sk == 0_mp) { + YACL_ENFORCE(counter <= 255); + auto hash_buf = SslHash(hash_).Update(derive_input).CumulativeHash(); + sk.FromMagBytes(hash_buf, Endian::little); + math::MPInt::Mod(sk, ec_->GetOrder(), &sk); + counter++; + pk = ec_->MulBase(sk); + } + return {sk, pk}; +} +} // namespace yacl::crypto diff --git a/yacl/crypto/oprf/oprf_ctx.h b/yacl/crypto/oprf/oprf_ctx.h new file mode 100644 index 00000000..66be4772 --- /dev/null +++ b/yacl/crypto/oprf/oprf_ctx.h @@ -0,0 +1,218 @@ +// 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 +#include +#include +#include + +#include "absl/strings/str_split.h" + +#include "yacl/crypto/ecc/ecc_spi.h" +#include "yacl/crypto/hash/hash_interface.h" + +namespace yacl::crypto { + +// ---------- // +// Oprf Enums // +// ---------- // +enum class OprfMode : uint8_t { OPRF = 0x00, VOPRF = 0x01, POPRF = 0x02 }; + +enum class OprfCipherSuite : int { + ristretto255_Sha512, // FIXME unsupported + decaf448_SHAKE256, // FIXME unsupported + P256_SHA256, + P384_SHA384, + P521_SHA512, +}; + +// ------------------ // +// Helper Oprf Config // +// ------------------ // +class OprfConfig { + public: + // Default config values + static constexpr OprfMode kDefaultMode = OprfMode::OPRF; + static constexpr OprfCipherSuite kDefaultCipherSuite = + OprfCipherSuite::P256_SHA256; + + // Static helper functions + static OprfCipherSuite CipherSuiteFromStr(const std::string& str) { + if (str == "ristretto255-SHA512") { + return OprfCipherSuite::ristretto255_Sha512; + } else if (str == "decaf448-SHAKE256") { + return OprfCipherSuite::decaf448_SHAKE256; + } else if (str == "P256-SHA256") { + return OprfCipherSuite::P256_SHA256; + } else if (str == "P384-SHA384") { + return OprfCipherSuite::P384_SHA384; + } else if (str == "P521-SHA512") { + return OprfCipherSuite::P521_SHA512; + } else { + YACL_THROW("Unrecognized Cipher Suite String: {}", str); + } + } + + static std::string CipherSuiteToStr(const OprfCipherSuite& cipher_suite) { + switch (cipher_suite) { + case OprfCipherSuite::ristretto255_Sha512: + return "ristretto255-SHA512"; + case OprfCipherSuite::decaf448_SHAKE256: + return "decaf448-SHAKE256"; + case OprfCipherSuite::P256_SHA256: + return "P256-SHA256"; + case OprfCipherSuite::P384_SHA384: + return "P384-SHA384"; + case OprfCipherSuite::P521_SHA512: + return "P521-SHA512"; + default: + YACL_THROW("Unrecognized Cipher Suite code: {}", (int)cipher_suite); + } + } + static OprfMode ModeFromU8(uint8_t x) { return static_cast(x); } + static uint8_t ModeToU8(OprfMode mode) { return static_cast(mode); } + + // Constructors + OprfConfig(const OprfMode& mode, const OprfCipherSuite& cipher_suite) + : mode_(mode), cipher_suite_(cipher_suite) {} + + OprfConfig(uint8_t mode, const std::string& cipher_suite) + : OprfConfig(ModeFromU8(mode), CipherSuiteFromStr(cipher_suite)) {} + + // Grab a default OprfConfig, it makes life easier + static OprfConfig& GetDefault() { + static OprfConfig config(kDefaultMode, kDefaultCipherSuite); + return config; + } + + // Get the defined oprf mode + OprfMode GetMode() const { return mode_; } + + // Get the defined oprf mode + OprfCipherSuite GetCipherSuite() const { return cipher_suite_; } + + // Convert the information to context string + std::string ToContextString() const { + return fmt::format("OPRFV1-{}-{}", ModeToU8(mode_), + CipherSuiteToStr(cipher_suite_)); + } + + // Get config from context string + static OprfConfig FromContextString(const std::string& str) { + std::vector split = absl::StrSplit(str, '-'); + YACL_ENFORCE(split.size() == 3); + YACL_ENFORCE_EQ(split[0], "OPRFV1"); + YACL_ENFORCE(split[1].size() == 1); + auto mode = ModeFromU8(static_cast(split[1][0])); + auto cipher_suite = CipherSuiteFromStr(split[2]); + return {mode, cipher_suite}; + } + + private: + OprfMode mode_; + OprfCipherSuite cipher_suite_; +}; + +// --------------------------- // +// Oprf Context +// --------------------------- // + +class OprfCtx { + public: + using SkTy = math::MPInt; + using PkTy = EcPoint; + + // Constructor from oprf config + explicit OprfCtx(const OprfConfig& config) : mode_(config.GetMode()) { + ctx_str_ = config.ToContextString(); + auto t = DecomposeCipherSuite(config.GetCipherSuite()); + ec_ = std::move(t.first); + hash_ = t.second; + } + + // Constructor from context string + explicit OprfCtx(const std::string& ctx_str) + : OprfCtx(OprfConfig::FromContextString(ctx_str)) {} + + // Grab a default OprfConfig, it makes life easier + static OprfCtx& GetDefault() { + static OprfCtx ctx(OprfConfig::GetDefault()); + return ctx; + } + + // Steal the extracted ec group from OprfConfig, the ec_ definied in + // OprfConfig will be reset. Therefore, this function should be called at + // most once for one OprfConfig instance. + // + // NOTE be careful when you steal things + // + std::unique_ptr StealEcGroup() { return std::move(ec_); } + + // Borrwo the ec group from context, ec group's lifetime stays with in + // OprfCtx + EcGroup* BorrowEcGroup() { return ec_.get(); } + + // Get the defined hash algorithm + HashAlgorithm GetHashAlgorithm() const { return hash_; } + + // Get the defined oprf mode + OprfMode GetMode() const { return mode_; } + + // Generate random key pair + std::pair GenKeyPair(); + + // Extract deterministic key pair from seed and info + std::pair DeriveKeyPair(uint128_t seed, + const std::string& info = ""); + + // Statistcally decompose the cipher suite object to valid EcGroup and + // HashAlgorithm objects + static std::pair, HashAlgorithm> + DecomposeCipherSuite(const OprfCipherSuite& cipher_suite) { + switch (cipher_suite) { + case OprfCipherSuite::ristretto255_Sha512: + YACL_THROW("Unsupported cipher suite: ristretto255_Sha512"); + // return {EcGroupFactory::Instance().Create("ristretto255"), + // HashAlgorithm::SHA512}; + case OprfCipherSuite::decaf448_SHAKE256: + // return {EcGroupFactory::Instance().Create("decaf448"), + // HashAlgorithm::SHAKE512}; + YACL_THROW("Unsupported cipher suite: decaf448_SHAKE256"); + case OprfCipherSuite::P256_SHA256: + return {EcGroupFactory::Instance().Create("brainpoolP256r1"), + HashAlgorithm::SHA256}; + case OprfCipherSuite::P384_SHA384: + return {EcGroupFactory::Instance().Create("brainpoolP384r1"), + HashAlgorithm::SHA384}; + case OprfCipherSuite::P521_SHA512: + return {EcGroupFactory::Instance().Create("brainpoolP512r1"), + HashAlgorithm::SHA512}; + default: + YACL_THROW( + "Decompose Oprf Cipher Suite failure, unknown CipherSuite " + "code: {}", + (int)cipher_suite); + } + } + + private: + std::string ctx_str_; + OprfMode mode_; + std::unique_ptr ec_; + HashAlgorithm hash_; +}; + +} // namespace yacl::crypto diff --git a/yacl/crypto/oprf/oprf_test.cc b/yacl/crypto/oprf/oprf_test.cc new file mode 100644 index 00000000..7a2cfba1 --- /dev/null +++ b/yacl/crypto/oprf/oprf_test.cc @@ -0,0 +1,40 @@ +// 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 "yacl/crypto/oprf/oprf.h" + +#include "absl/strings/escaping.h" +#include "gtest/gtest.h" + +namespace yacl::crypto { + +TEST(SimpleTest, Works) { + // get a default config + const auto config = OprfConfig::GetDefault(); + + auto server = OprfServer(config); + auto client = OprfClient(config); + + const std::string input = "test_element"; + + EcPoint c2s_tape; + client.Blind(input, &c2s_tape); + + EcPoint s2c_tape; + server.BlindEvaluate(c2s_tape, &s2c_tape); + + client.Finalize(s2c_tape); +} + +} // namespace yacl::crypto diff --git a/yacl/crypto/ossl_provider/BUILD.bazel b/yacl/crypto/ossl_provider/BUILD.bazel index f2118486..ade6f5a8 100644 --- a/yacl/crypto/ossl_provider/BUILD.bazel +++ b/yacl/crypto/ossl_provider/BUILD.bazel @@ -39,19 +39,6 @@ cc_shared_library( ], ) -# helper for loading provider -yacl_cc_library( - name = "helper", - hdrs = [ - "helper.h", - ], - visibility = ["//visibility:public"], # public - deps = [ - "//yacl/crypto:openssl_wrappers", # openssl here - "@com_google_absl//absl/strings", - ], -) - # private target yacl_cc_library( name = "provider", @@ -88,8 +75,8 @@ yacl_cc_test( ":prov_shared", ], deps = [ - ":helper", "//yacl/base:exception", + "//yacl/crypto:openssl_wrappers", "@bazel_tools//tools/cpp/runfiles", ], ) diff --git a/yacl/crypto/ossl_provider/helper.h b/yacl/crypto/ossl_provider/helper.h deleted file mode 100644 index 55af04b7..00000000 --- a/yacl/crypto/ossl_provider/helper.h +++ /dev/null @@ -1,83 +0,0 @@ -// 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 -#include - -#include "absl/strings/str_split.h" -#include "spdlog/spdlog.h" - -#include "yacl/base/exception.h" - -#ifdef __APPLE__ -#include -#include - -#define SO_EXT ".dylib" -#else -#define SO_EXT ".so" -#endif - -namespace yacl::crypto { - -inline std::string GetProviderPath() { - // first, get the exec path - std::filesystem::path exe_path; -#ifndef __APPLE__ - exe_path = std::filesystem::canonical("/proc/self/exe"); -#else - std::array buf; - uint32_t bufsize = PATH_MAX; - auto ret = _NSGetExecutablePath(buf.data(), &bufsize); - YACL_ENFORCE(ret == 0); - - exe_path = std::filesystem::path(buf.data()); -#endif - auto selfdir_str = exe_path.parent_path().generic_string(); - - // persumely, you are using bazel, so split the path in a bazel way - // HACK: bazel path - try { - std::string path1; - std::string path2; - std::string path3 = - fmt::format("/yacl/crypto/ossl_provider/libprov_shared{}", SO_EXT); - - // step 1: determine if target is "cc_test" or "cc_library" - if (selfdir_str.find("sandbox") != std::string::npos) { - std::vector tmp = absl::StrSplit(selfdir_str, "sandbox"); - path1 = tmp.at(0); - tmp = absl::StrSplit(selfdir_str, "execroot"); - tmp = absl::StrSplit(tmp.at(1), "bin"); - path2 = tmp.at(0); - } else { - std::vector tmp = absl::StrSplit(selfdir_str, "execroot"); - path1 = tmp.at(0); - tmp = absl::StrSplit(tmp.at(1), "bin"); - path2 = tmp.at(0); - } - - std::string filename = - fmt::format("{}execroot{}bin{}", path1, path2, path3); - return filename; - } catch (std::exception& e) { - return ""; - } -} - -} // namespace yacl::crypto diff --git a/yacl/crypto/ossl_provider/provider_test.cc b/yacl/crypto/ossl_provider/provider_test.cc index 12d76e08..7a4f45e1 100644 --- a/yacl/crypto/ossl_provider/provider_test.cc +++ b/yacl/crypto/ossl_provider/provider_test.cc @@ -27,7 +27,6 @@ #include "tools/cpp/runfiles/runfiles.h" #include "yacl/crypto/openssl_wrappers.h" -#include "yacl/crypto/ossl_provider/helper.h" using bazel::tools::cpp::runfiles::Runfiles; diff --git a/yacl/crypto/pairing/BUILD.bazel b/yacl/crypto/pairing/BUILD.bazel index 16e7ad8a..f18c33ce 100644 --- a/yacl/crypto/pairing/BUILD.bazel +++ b/yacl/crypto/pairing/BUILD.bazel @@ -31,7 +31,7 @@ yacl_cc_library( "//yacl/crypto/ecc:spi", "//yacl/math/galois_field:gf", "//yacl/utils/spi", - "@com_google_absl//absl/strings", + "@abseil-cpp//absl/strings", ], ) @@ -42,7 +42,7 @@ yacl_cc_library( # deps = [ # "//yacl/crypto/ecc:spi", # "//yacl/math/galois_field:gf", -# "@com_google_absl//absl/strings", +# "@abseil-cpp//absl/strings", # ], # ) diff --git a/yacl/crypto/pairing/factory/BUILD.bazel b/yacl/crypto/pairing/factory/BUILD.bazel index 10f63d5e..14bb8aab 100644 --- a/yacl/crypto/pairing/factory/BUILD.bazel +++ b/yacl/crypto/pairing/factory/BUILD.bazel @@ -40,7 +40,7 @@ yacl_cc_library( ], "//conditions:default": [], }), - deps = ["@com_github_herumi_mcl//:mcl"], + deps = ["@mcl"], ) yacl_cc_library( diff --git a/yacl/crypto/rand/BUILD.bazel b/yacl/crypto/rand/BUILD.bazel index 9bbfc211..e4b98f6f 100644 --- a/yacl/crypto/rand/BUILD.bazel +++ b/yacl/crypto/rand/BUILD.bazel @@ -44,6 +44,6 @@ yacl_cc_binary( srcs = ["rand_bench.cc"], deps = [ ":rand", - "@com_github_google_benchmark//:benchmark_main", + "@google_benchmark//:benchmark_main", ], ) diff --git a/yacl/crypto/rand/drbg/BUILD.bazel b/yacl/crypto/rand/drbg/BUILD.bazel index b4f8e85a..18ccd45b 100644 --- a/yacl/crypto/rand/drbg/BUILD.bazel +++ b/yacl/crypto/rand/drbg/BUILD.bazel @@ -56,7 +56,6 @@ yacl_cc_library( deps = [ ":spi", "//yacl/crypto:openssl_wrappers", - "//yacl/crypto/ossl_provider:helper", # helper "//yacl/crypto/rand/entropy_source", ], alwayslink = 1, @@ -80,7 +79,6 @@ yacl_cc_library( "//yacl/crypto:openssl_wrappers", "//yacl/crypto/block_cipher:symmetric_crypto", "//yacl/crypto/hash:hash_utils", - "//yacl/crypto/ossl_provider:helper", # helper "//yacl/crypto/rand/entropy_source", ], alwayslink = 1, diff --git a/yacl/crypto/rand/drbg/openssl_factory.cc b/yacl/crypto/rand/drbg/openssl_factory.cc index 910c84a5..abe38027 100644 --- a/yacl/crypto/rand/drbg/openssl_factory.cc +++ b/yacl/crypto/rand/drbg/openssl_factory.cc @@ -20,7 +20,6 @@ #include #include "yacl/base/exception.h" -#include "yacl/crypto/ossl_provider/helper.h" #include "yacl/secparam.h" namespace yacl::crypto { @@ -65,8 +64,10 @@ OpensslDrbg::OpensslDrbg(std::string type, // load openssl provider auto libctx = openssl::UniqueLib(OSSL_LIB_CTX_new()); - auto prov = openssl::UniqueProv( - OSSL_PROVIDER_load(libctx.get(), GetProviderPath().c_str())); + // NOTE: set ENV "OPENSSL_MODULES" to the directory where "libprov_shared.so" + // is located. + auto prov = + openssl::UniqueProv(OSSL_PROVIDER_load(libctx.get(), "libprov_shared")); if (prov != nullptr) { // fetch provider's entropy_source algorithm diff --git a/yacl/crypto/rand/entropy_source/BUILD.bazel b/yacl/crypto/rand/entropy_source/BUILD.bazel index 040bd021..ee46afbd 100644 --- a/yacl/crypto/rand/entropy_source/BUILD.bazel +++ b/yacl/crypto/rand/entropy_source/BUILD.bazel @@ -69,7 +69,7 @@ yacl_cc_library( visibility = ["//visibility:private"], deps = [ ":spi", - "@com_github_google_cpu_features//:cpu_features", + "@cpu_features//:cpuinfo", ], alwayslink = 1, ) diff --git a/yacl/crypto/rand/entropy_source/rdseed_factory.cc b/yacl/crypto/rand/entropy_source/rdseed_factory.cc index 539e210d..6f5310a6 100644 --- a/yacl/crypto/rand/entropy_source/rdseed_factory.cc +++ b/yacl/crypto/rand/entropy_source/rdseed_factory.cc @@ -37,7 +37,7 @@ int rdseed64_step(uint64_t *out) { } // namespace -Buffer RdSeedEntropySource::GetEntropy(uint32_t bits_of_entropy) noexcept { +Buffer RdSeedEntropySource::GetEntropy(uint32_t bits_of_entropy) { // required bits_of_entropy should > 0 if (bits_of_entropy == 0) { return {}; diff --git a/yacl/crypto/rand/entropy_source/rdseed_factory.h b/yacl/crypto/rand/entropy_source/rdseed_factory.h index 244be9cd..411c52b5 100644 --- a/yacl/crypto/rand/entropy_source/rdseed_factory.h +++ b/yacl/crypto/rand/entropy_source/rdseed_factory.h @@ -23,7 +23,7 @@ #ifdef __x86_64 #include -#include "cpu_features/cpuinfo_x86.h" +#include "cpuinfo_x86.h" namespace yacl::crypto { @@ -65,7 +65,7 @@ class RdSeedEntropySource : public EntropySource { absl::AsciiStrToLower(type) == "auto"); } - Buffer GetEntropy(uint32_t bits_of_entropy) noexcept override; + Buffer GetEntropy(uint32_t bits_of_entropy) override; std::string Name() override { return "rdseed entropy source"; } }; diff --git a/yacl/crypto/rand/rand.cc b/yacl/crypto/rand/rand.cc index ec93d1ea..5f6d8d72 100644 --- a/yacl/crypto/rand/rand.cc +++ b/yacl/crypto/rand/rand.cc @@ -19,7 +19,6 @@ #include "yacl/base/byte_container_view.h" #include "yacl/base/dynamic_bitset.h" -#include "yacl/crypto/ossl_provider/helper.h" #include "yacl/crypto/rand/entropy_source/entropy_source.h" #include "yacl/math/gadget.h" diff --git a/yacl/crypto/tools/BUILD.bazel b/yacl/crypto/tools/BUILD.bazel index 233a2e6d..99e73841 100644 --- a/yacl/crypto/tools/BUILD.bazel +++ b/yacl/crypto/tools/BUILD.bazel @@ -116,6 +116,6 @@ yacl_cc_binary( ":rp", "//yacl/base:aligned_vector", "//yacl/crypto/rand", - "@com_github_google_benchmark//:benchmark_main", + "@google_benchmark//:benchmark_main", ], ) diff --git a/yacl/io/circuit/BUILD.bazel b/yacl/io/circuit/BUILD.bazel index 707f93cd..8bcfc61d 100644 --- a/yacl/io/circuit/BUILD.bazel +++ b/yacl/io/circuit/BUILD.bazel @@ -25,16 +25,16 @@ yacl_cc_library( name = "bristol_fashion", srcs = ["bristol_fashion.cc"], hdrs = ["bristol_fashion.h"], + data = [":circuit_data"], deps = [ + "//yacl/base:byte_container_view", "//yacl/io/stream:file_io", - "//yacl/link:context", ], ) yacl_cc_test( name = "bristol_fashion_test", srcs = ["bristol_fashion_test.cc"], - data = [":circuit_data"], deps = [ ":bristol_fashion", ], diff --git a/yacl/io/circuit/bristol_fashion.cc b/yacl/io/circuit/bristol_fashion.cc index 1ae35715..d64baba6 100644 --- a/yacl/io/circuit/bristol_fashion.cc +++ b/yacl/io/circuit/bristol_fashion.cc @@ -16,11 +16,14 @@ #include "absl/strings/numbers.h" #include "absl/strings/str_split.h" +#include "spdlog/spdlog.h" #include "yacl/base/exception.h" namespace yacl::io { +const std::string BuiltinBFCircuit::CircDataDir = "yacl/io/circuit/data"; + void BFCircuit::PrintSummary() { SPDLOG_INFO("number of gates: {}", ng); SPDLOG_INFO("number of wires: {}", nw); @@ -148,4 +151,63 @@ void CircuitReader::ReadAllGates() { } } +namespace { +constexpr std::array GetSha256InitialHashValues() { + return {0x19, 0xcd, 0xe0, 0x5b, 0xab, 0xd9, 0x83, 0x1f, 0x8c, 0x68, 0x05, + 0x9b, 0x7f, 0x52, 0x0e, 0x51, 0x3a, 0xf5, 0x4f, 0xa5, 0x72, 0xf3, + 0x6e, 0x3c, 0x85, 0xae, 0x67, 0xbb, 0x67, 0xe6, 0x09, 0x6a}; +} +} // namespace + +std::vector BuiltinBFCircuit::PrepareSha256Input( + ByteContainerView input) { + constexpr size_t kFixPadSize = 1; // in bytes + constexpr size_t kMsgLenSize = sizeof(uint64_t); // in bytes + constexpr size_t kMsgBlockSize = 64; // in bytes + const auto kInitSha256Bytes = GetSha256InitialHashValues(); + + uint64_t input_size = input.size(); // in bytes + uint64_t zero_padding_size = + (input_size + kFixPadSize + kMsgLenSize) % kMsgBlockSize == 0 + ? 0 + : kMsgBlockSize - + (input_size + kFixPadSize + kMsgLenSize) % kMsgBlockSize; + uint64_t message_size = + input_size + kFixPadSize + zero_padding_size + kMsgLenSize; + uint64_t result_size = message_size + kInitSha256Bytes.size(); + + // TODO: support arbitrary large input + YACL_ENFORCE(message_size == kMsgBlockSize); + + // Declare the result byte-vector + size_t offset = 0; + std::vector result(result_size); + + // the next 64 bits should be the byte length of input message + uint64_t input_bitnum = input_size * 8; // in bits + std::memcpy(result.data() + offset, &input_bitnum, sizeof(input_bitnum)); + offset += sizeof(uint64_t); + + // zero padding (result vector has zero initialization) + // ... should doing nothing ... + offset += zero_padding_size; + + // additional padding bit-'1' (as a mark) + result[offset] = 0x80; + offset += kFixPadSize; + + // original input message + auto input_reverse = std::vector(input.begin(), input.end()); + std::reverse(input_reverse.begin(), input_reverse.end()); + std::memcpy(result.data() + offset, input_reverse.data(), input_size); + offset += input_size; + + // initial hash values + std::memcpy(result.data() + offset, kInitSha256Bytes.data(), + kInitSha256Bytes.size()); + // offset += kInitSha256Bytes.size(); + + return result; +} + } // namespace yacl::io diff --git a/yacl/io/circuit/bristol_fashion.h b/yacl/io/circuit/bristol_fashion.h index 3b669ab3..ed2964ea 100644 --- a/yacl/io/circuit/bristol_fashion.h +++ b/yacl/io/circuit/bristol_fashion.h @@ -14,15 +14,12 @@ #pragma once -#include #include -#include #include #include #include -#include "spdlog/spdlog.h" - +#include "yacl/base/byte_container_view.h" #include "yacl/base/exception.h" #include "yacl/io/stream/file_io.h" #include "yacl/io/stream/interface.h" @@ -33,12 +30,17 @@ namespace yacl::io { // see: https://nigelsmart.github.io/MPC-Circuits/ class BFCircuit { public: - uint32_t ng = 0; // number of gates - uint32_t nw = 0; // number of wires - uint32_t niv; // number of input values - std::vector niw; // number of wires per each input values - uint32_t nov; // number of output values - std::vector now; // number of wires per each output values + using GateNumType = uint32_t; + // now, assume small circuit only + using GateWireType = uint32_t; + + GateNumType ng = 0; // number of gates + GateWireType nw = 0; // number of wires + + uint32_t niv; // number of input values + std::vector niw; // number of wires per each input values + uint32_t nov; // number of output values + std::vector now; // number of wires per each output values // circuit oeprations enum class Op { XOR, AND, INV, EQ, EQW, MAND }; @@ -46,10 +48,10 @@ class BFCircuit { // Gate definition class Gate { public: - uint32_t niw = 0; // numer of input wires - uint32_t now = 0; // number of output wires - std::vector iw; // lists of input wires - std::vector ow; // lists of output wires + GateNumType niw = 0; // numer of input wires + GateNumType now = 0; // number of output wires + std::vector iw; // lists of input wires + std::vector ow; // lists of output wires Op op; }; @@ -101,39 +103,34 @@ class CircuitReader { class BuiltinBFCircuit { public: + static const std::string CircDataDir; + static std::string Add64Path() { - return fmt::format("{}/yacl/io/circuit/data/adder64.txt", - std::filesystem::current_path().string()); + return fmt::format("{}/adder64.txt", CircDataDir); } static std::string Sub64Path() { - return fmt::format("{}/yacl/io/circuit/data/sub64.txt", - std::filesystem::current_path().string()); + return fmt::format("{}/sub64.txt", CircDataDir); } static std::string Neg64Path() { - return fmt::format("{}/yacl/io/circuit/data/neg64.txt", - std::filesystem::current_path().string()); + return fmt::format("{}/neg64.txt", CircDataDir); } static std::string Mul64Path() { - return fmt::format("{}/yacl/io/circuit/data/mult64.txt", - std::filesystem::current_path().string()); + return fmt::format("{}/mult64.txt", CircDataDir); } static std::string Div64Path() { - return fmt::format("{}/yacl/io/circuit/data/divide64.txt", - std::filesystem::current_path().string()); + return fmt::format("{}/divide64.txt", CircDataDir); } static std::string UDiv64Path() { - return fmt::format("{}/yacl/io/circuit/data/udivide64.txt", - std::filesystem::current_path().string()); + return fmt::format("{}/udivide64.txt", CircDataDir); } static std::string EqzPath() { - return fmt::format("{}/yacl/io/circuit/data/zero_equal.txt", - std::filesystem::current_path().string()); + return fmt::format("{}/zero_equal.txt", CircDataDir); } // NOTE: For AES-128 the wire orders are in the reverse order as used in @@ -142,17 +139,26 @@ class BuiltinBFCircuit { // // see: https://nigelsmart.github.io/MPC-Circuits/ static std::string Aes128Path() { - return fmt::format("{}/yacl/io/circuit/data/aes_128.txt", - std::filesystem::current_path().string()); + return fmt::format("{}/aes_128.txt", CircDataDir); } // NOTE: sha256 needs two inputs, a 512 bit buffer, and a 256 bit previous // digest value // - // static std::string Sha256Path() { - // return fmt::format("{}/yacl/io/circuit/data/sha256.txt", - // std::filesystem::current_path().string()); - // } + static std::string Sha256Path() { + return fmt::format("{}/sha256.txt", CircDataDir); + } + + // Prepare (append & tweak) the input sha256 message before fed to the sha256 + // bristol circuit. + // + // For more details, please check: + // https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf + // + // NOTE since we are using dynamic_bitset for bristol format circuit + // representation, the actual bit operation here is slightly different from + // the standards. + static std::vector PrepareSha256Input(ByteContainerView input); }; } // namespace yacl::io diff --git a/yacl/io/circuit/bristol_fashion_test.cc b/yacl/io/circuit/bristol_fashion_test.cc index d077356c..168298e1 100644 --- a/yacl/io/circuit/bristol_fashion_test.cc +++ b/yacl/io/circuit/bristol_fashion_test.cc @@ -14,25 +14,15 @@ #include "yacl/io/circuit/bristol_fashion.h" -#include -#include -#include - -#include "fmt/format.h" #include "gtest/gtest.h" -#include "spdlog/spdlog.h" #include "yacl/base/exception.h" -#include "yacl/io/stream/file_io.h" namespace yacl::io { namespace { -std::string GetAesBfPath() { - return fmt::format("{}/yacl/io/circuit/data/aes_128.txt", - std::filesystem::current_path().string()); -} +std::string GetAesBfPath() { return BuiltinBFCircuit::Aes128Path(); } } // namespace diff --git a/yacl/io/kv/BUILD.bazel b/yacl/io/kv/BUILD.bazel index 554848fb..579774d2 100644 --- a/yacl/io/kv/BUILD.bazel +++ b/yacl/io/kv/BUILD.bazel @@ -41,8 +41,8 @@ yacl_cc_library( deps = [ ":kvstore", "//yacl/base:exception", - "@com_github_brpc_brpc//:brpc", - "@com_github_google_leveldb//:leveldb", + "@brpc", + "@leveldb", ], ) diff --git a/yacl/io/msgpack/BUILD.bazel b/yacl/io/msgpack/BUILD.bazel index a1587751..4924be77 100644 --- a/yacl/io/msgpack/BUILD.bazel +++ b/yacl/io/msgpack/BUILD.bazel @@ -35,6 +35,6 @@ yacl_cc_test( srcs = ["buffer_test.cc"], deps = [ ":buffer", - "@com_github_msgpack_msgpack//:msgpack", + "@msgpack-c//:msgpack", ], ) diff --git a/yacl/io/rw/BUILD.bazel b/yacl/io/rw/BUILD.bazel index 7b3a8f09..0db77322 100644 --- a/yacl/io/rw/BUILD.bazel +++ b/yacl/io/rw/BUILD.bazel @@ -38,7 +38,7 @@ yacl_cc_library( name = "float", hdrs = ["float.h"], visibility = ["//visibility:public"], - deps = ["@com_google_absl//absl/strings"], + deps = ["@abseil-cpp//absl/strings"], ) yacl_cc_library( @@ -52,8 +52,8 @@ yacl_cc_library( ":mmapped_file", "//yacl/base:exception", "//yacl/io/stream", - "@com_github_fmtlib_fmt//:fmtlib", - "@com_google_absl//absl/strings", + "@abseil-cpp//absl/strings", + "@fmt", ], ) @@ -67,7 +67,7 @@ yacl_cc_library( ":interface", "//yacl/base:exception", "//yacl/io/stream", - "@com_github_fmtlib_fmt//:fmtlib", + "@fmt", ], ) @@ -77,8 +77,8 @@ yacl_cc_library( hdrs = ["mmapped_file.h"], deps = [ "//yacl/base:exception", - "@com_google_absl//absl/base:malloc_internal", - "@com_google_absl//absl/cleanup", + "@abseil-cpp//absl/base:malloc_internal", + "@abseil-cpp//absl/cleanup", ], ) @@ -99,7 +99,7 @@ yacl_cc_test( deps = [ ":rw", "//yacl/utils:elapsed_timer", - "@com_github_gflags_gflags//:gflags", - "@com_google_absl//absl/strings", + "@abseil-cpp//absl/strings", + "@gflags", ], ) diff --git a/yacl/io/stream/BUILD.bazel b/yacl/io/stream/BUILD.bazel index fd8c168a..152b30a2 100644 --- a/yacl/io/stream/BUILD.bazel +++ b/yacl/io/stream/BUILD.bazel @@ -37,7 +37,7 @@ yacl_cc_library( deps = [ ":interface", "//yacl/base:exception", - "@com_github_fmtlib_fmt//:fmtlib", + "@fmt", ], ) diff --git a/yacl/kernel/BUILD.bazel b/yacl/kernel/BUILD.bazel index c61086f9..55cbaf8e 100644 --- a/yacl/kernel/BUILD.bazel +++ b/yacl/kernel/BUILD.bazel @@ -42,7 +42,7 @@ yacl_cc_binary( deps = [ ":svole_kernel", "//yacl/link:test_util", - "@com_github_google_benchmark//:benchmark", + "@google_benchmark//:benchmark", ], ) @@ -85,6 +85,6 @@ yacl_cc_binary( deps = [ ":ot_kernel", "//yacl/link:test_util", - "@com_github_google_benchmark//:benchmark", + "@google_benchmark//:benchmark", ], ) diff --git a/yacl/kernel/algorithms/BUILD.bazel b/yacl/kernel/algorithms/BUILD.bazel index 723a8d1f..2aa34d63 100644 --- a/yacl/kernel/algorithms/BUILD.bazel +++ b/yacl/kernel/algorithms/BUILD.bazel @@ -32,7 +32,7 @@ yacl_cc_library( "//yacl/base:exception", "//yacl/crypto/tools:ro", "//yacl/link", - "@simplest_ot//:simplest_ot_portable", + "@simplest-ot//:simplest_ot_portable", ], ) @@ -52,7 +52,7 @@ yacl_cc_library( "//yacl/link", "//yacl/math:gadget", "//yacl/math/galois_field:gf_intrinsic", - "@simplest_ot//:simplest_ot_x86_asm", + "@simplest-ot//:simplest_ot_x86_asm", ], ) @@ -65,7 +65,7 @@ yacl_cc_library( "//yacl/base:exception", "//yacl/kernel/type:ot_store_utils", "//yacl/link", - "@com_google_absl//absl/types:span", + "@abseil-cpp//absl/types:span", ] + select({ "@bazel_tools//src/conditions:linux_x86_64": [ ":x86_asm_ot_interface", @@ -158,7 +158,7 @@ yacl_cc_library( "//yacl/kernel/type:ot_store_utils", "//yacl/link", "//yacl/math:gadget", - "@com_google_absl//absl/numeric:bits", + "@abseil-cpp//absl/numeric:bits", ], ) @@ -188,7 +188,7 @@ yacl_cc_library( "//yacl/kernel/type:ot_store_utils", "//yacl/link", "//yacl/math:gadget", - "@com_google_absl//absl/numeric:bits", + "@abseil-cpp//absl/numeric:bits", ], ) @@ -294,7 +294,7 @@ yacl_cc_library( "//yacl/utils:matrix_utils", ] + select({ "@platforms//cpu:aarch64": [ - "@com_github_dltcollab_sse2neon//:sse2neon", + "@sse2neon", ], "//conditions:default": [], }), diff --git a/yacl/kernel/algorithms/ferret_ote_rn.h b/yacl/kernel/algorithms/ferret_ote_rn.h index 92f70544..2b097677 100644 --- a/yacl/kernel/algorithms/ferret_ote_rn.h +++ b/yacl/kernel/algorithms/ferret_ote_rn.h @@ -48,6 +48,7 @@ inline void MpCotRNSend(const std::shared_ptr& ctx, const uint64_t batch_size = spcot_size; const uint64_t batch_num = math::DivCeil(full_size, batch_size); YACL_ENFORCE(batch_num <= idx_num); + YACL_ENFORCE(batch_num >= 1); const uint64_t last_size = full_size - (batch_num - 1) * batch_size; const uint64_t batch_length = math::Log2Ceil(batch_size); @@ -106,6 +107,7 @@ inline void MpCotRNRecv(const std::shared_ptr& ctx, const uint64_t batch_size = spcot_size; const uint64_t batch_num = math::DivCeil(full_size, batch_size); YACL_ENFORCE(batch_num <= idx_num); + YACL_ENFORCE(batch_num >= 1); const uint64_t last_size = full_size - (batch_num - 1) * batch_size; const uint64_t batch_length = math::Log2Ceil(batch_size); diff --git a/yacl/kernel/algorithms/sgrr_ote.cc b/yacl/kernel/algorithms/sgrr_ote.cc index 14febe5f..c23024bd 100644 --- a/yacl/kernel/algorithms/sgrr_ote.cc +++ b/yacl/kernel/algorithms/sgrr_ote.cc @@ -304,6 +304,7 @@ void SgrrOtExtRecv_fixed_index(const OtRecvStore& base_ot, uint32_t n, YACL_ENFORCE_GE((uint32_t)128, base_ot.Size()); // base ot num < 128 YACL_ENFORCE_GE(base_ot.Size(), ot_num); // YACL_ENFORCE_EQ(static_cast(recv_buf.size()), buf_size); + YACL_ENFORCE(ot_num > 0); // we need log(n) 1-2 OTs from log(n) ROTs // most significant bit first @@ -366,6 +367,7 @@ void SgrrOtExtSend_fixed_index(const OtSendStore& base_ot, uint32_t n, YACL_ENFORCE_GE(base_ot.Size(), ot_num); YACL_ENFORCE_GT(n, (uint32_t)1); YACL_ENFORCE_EQ(static_cast(send_buf.size()), buf_size); + YACL_ENFORCE(ot_num > 0); output[0] = SecureRandSeed(); auto send_msgs = absl::MakeSpan( diff --git a/yacl/kernel/benchmark/BUILD.bazel b/yacl/kernel/benchmark/BUILD.bazel index 23ae5c74..32e392ba 100644 --- a/yacl/kernel/benchmark/BUILD.bazel +++ b/yacl/kernel/benchmark/BUILD.bazel @@ -33,8 +33,8 @@ yacl_cc_binary( "//yacl/kernel/algorithms:sgrr_ote", "//yacl/kernel/algorithms:softspoken_ote", "//yacl/link:test_util", - "@com_github_google_benchmark//:benchmark_main", - "@com_google_absl//absl/numeric:bits", + "@abseil-cpp//absl/numeric:bits", + "@google_benchmark//:benchmark_main", ], ) @@ -49,7 +49,7 @@ yacl_cc_binary( "//yacl/kernel/algorithms:base_vole", "//yacl/kernel/algorithms:silent_vole", "//yacl/link:test_util", - "@com_github_google_benchmark//:benchmark_main", - "@com_google_absl//absl/numeric:bits", + "@abseil-cpp//absl/numeric:bits", + "@google_benchmark//:benchmark_main", ], ) diff --git a/yacl/kernel/benchmark/vole_bench.cc b/yacl/kernel/benchmark/vole_bench.cc index 8c228c19..e18ee8f2 100644 --- a/yacl/kernel/benchmark/vole_bench.cc +++ b/yacl/kernel/benchmark/vole_bench.cc @@ -138,6 +138,7 @@ void GilboaVoleBench(benchmark::State& state, Args&&... args) { } state.ResumeTiming(); } + YACL_ENFORCE(state.iterations() > 0); state.counters["send"] /= state.iterations(); state.counters["recv"] /= state.iterations(); state.counters["send_byte"] /= state.iterations(); @@ -196,6 +197,7 @@ void SilentVoleBench(benchmark::State& state, Args&&... args) { } state.ResumeTiming(); } + YACL_ENFORCE(state.iterations() > 0); state.counters["send"] /= state.iterations(); state.counters["recv"] /= state.iterations(); state.counters["send_byte"] /= state.iterations(); diff --git a/yacl/kernel/code/BUILD.bazel b/yacl/kernel/code/BUILD.bazel index 72d64bc4..a07f27b8 100644 --- a/yacl/kernel/code/BUILD.bazel +++ b/yacl/kernel/code/BUILD.bazel @@ -31,7 +31,7 @@ yacl_cc_library( "//yacl/utils:thread_pool", ] + select({ "@platforms//cpu:aarch64": [ - "@com_github_dltcollab_sse2neon//:sse2neon", + "@sse2neon", ], "//conditions:default": [], }), @@ -57,7 +57,7 @@ yacl_cc_library( "//yacl/utils:thread_pool", ] + select({ "@platforms//cpu:aarch64": [ - "@com_github_dltcollab_sse2neon//:sse2neon", + "@sse2neon", ], "//conditions:default": [], }), @@ -83,7 +83,7 @@ yacl_cc_library( "//yacl/utils:thread_pool", ] + select({ "@platforms//cpu:aarch64": [ - "@com_github_dltcollab_sse2neon//:sse2neon", + "@sse2neon", ], "//conditions:default": [], }), @@ -110,6 +110,6 @@ yacl_cc_binary( ":silver_code", "//yacl/base:aligned_vector", "//yacl/crypto/rand", - "@com_github_google_benchmark//:benchmark_main", + "@google_benchmark//:benchmark_main", ], ) diff --git a/yacl/link/BUILD.bazel b/yacl/link/BUILD.bazel index 1460f259..361b0442 100644 --- a/yacl/link/BUILD.bazel +++ b/yacl/link/BUILD.bazel @@ -35,7 +35,7 @@ yacl_cc_library( }), deps = [ "//yacl/base:exception", - "@com_google_absl//absl/strings", + "@abseil-cpp//absl/strings", ], ) @@ -128,7 +128,7 @@ yacl_cc_library( "//yacl/base:buffer", "//yacl/link:context", "//yacl/link:factory", - "@com_github_fmtlib_fmt//:fmtlib", + "@fmt", ], ) diff --git a/yacl/link/algorithm/BUILD.bazel b/yacl/link/algorithm/BUILD.bazel index a8253fb2..c2503edf 100644 --- a/yacl/link/algorithm/BUILD.bazel +++ b/yacl/link/algorithm/BUILD.bazel @@ -24,7 +24,7 @@ yacl_cc_library( "//yacl/base:exception", "//yacl/link:context", "//yacl/link:trace", - "@com_github_fmtlib_fmt//:fmtlib", + "@fmt", ], ) @@ -45,8 +45,8 @@ yacl_cc_library( "//yacl/base:exception", "//yacl/link:context", "//yacl/link:trace", - "@com_github_fmtlib_fmt//:fmtlib", - "@com_google_absl//absl/numeric:bits", + "@abseil-cpp//absl/numeric:bits", + "@fmt", ], ) @@ -67,7 +67,7 @@ yacl_cc_library( "//yacl/base:exception", "//yacl/link:context", "//yacl/link:trace", - "@com_github_fmtlib_fmt//:fmtlib", + "@fmt", ], ) @@ -89,7 +89,7 @@ yacl_cc_library( "//yacl/link:context", "//yacl/link:trace", "//yacl/utils:serialize", - "@com_github_fmtlib_fmt//:fmtlib", + "@fmt", ], ) @@ -111,7 +111,7 @@ yacl_cc_library( "//yacl/link:context", "//yacl/link:trace", "//yacl/utils:serialize", - "@com_github_fmtlib_fmt//:fmtlib", + "@fmt", ], ) diff --git a/yacl/link/transport/BUILD.bazel b/yacl/link/transport/BUILD.bazel index 05efbe44..3397db0d 100644 --- a/yacl/link/transport/BUILD.bazel +++ b/yacl/link/transport/BUILD.bazel @@ -28,7 +28,7 @@ yacl_cc_library( "//yacl/link:retry_options", "//yacl/link:ssl_options", "//yacl/utils:segment_tree", - "@com_github_brpc_brpc//:brpc", + "@brpc", ], ) diff --git a/yacl/link/transport/blackbox_interconnect/BUILD.bazel b/yacl/link/transport/blackbox_interconnect/BUILD.bazel index d4940bc0..1c37cd1b 100644 --- a/yacl/link/transport/blackbox_interconnect/BUILD.bazel +++ b/yacl/link/transport/blackbox_interconnect/BUILD.bazel @@ -55,8 +55,8 @@ yacl_cc_library( ":blackbox_service_errorcode", ":blackbox_service_proto", "//yacl/base:exception", - "@com_github_brpc_brpc//:brpc", - "@com_google_absl//absl/strings", + "@abseil-cpp//absl/strings", + "@brpc", ], ) @@ -66,7 +66,7 @@ yacl_cc_library( deps = [ ":blackbox_dummy_service_impl", "//yacl/base:exception", - "@com_github_brpc_brpc//:brpc", + "@brpc", ], ) diff --git a/yacl/math/BUILD.bazel b/yacl/math/BUILD.bazel index 19522c7b..0c5af2d4 100644 --- a/yacl/math/BUILD.bazel +++ b/yacl/math/BUILD.bazel @@ -20,6 +20,6 @@ yacl_cc_library( name = "gadget", hdrs = ["gadget.h"], deps = [ - "@com_google_absl//absl/strings", + "@abseil-cpp//absl/strings", ], ) diff --git a/yacl/math/galois_field/BUILD.bazel b/yacl/math/galois_field/BUILD.bazel index 094a3cde..14a5d203 100644 --- a/yacl/math/galois_field/BUILD.bazel +++ b/yacl/math/galois_field/BUILD.bazel @@ -64,7 +64,7 @@ yacl_cc_library( "//yacl/base:exception", "//yacl/base:int128", "//yacl/math:gadget", - "@com_google_absl//absl/types:span", + "@abseil-cpp//absl/types:span", ], ) diff --git a/yacl/math/galois_field/benchmark/BUILD.bazel b/yacl/math/galois_field/benchmark/BUILD.bazel index 02e7a94e..a53db48a 100644 --- a/yacl/math/galois_field/benchmark/BUILD.bazel +++ b/yacl/math/galois_field/benchmark/BUILD.bazel @@ -19,6 +19,6 @@ yacl_cc_binary( srcs = ["operation_bench.cc"], deps = [ "//yacl/math/galois_field:gf", - "@com_github_google_benchmark//:benchmark_main", + "@google_benchmark//:benchmark_main", ], ) diff --git a/yacl/math/galois_field/factory/BUILD.bazel b/yacl/math/galois_field/factory/BUILD.bazel index 3c5551f1..b080efdc 100644 --- a/yacl/math/galois_field/factory/BUILD.bazel +++ b/yacl/math/galois_field/factory/BUILD.bazel @@ -31,7 +31,7 @@ yacl_cc_library( "//yacl/math/mpint", "//yacl/utils:parallel", "//yacl/utils/spi/sketch", - "@com_google_absl//absl/types:span", + "@abseil-cpp//absl/types:span", ], ) @@ -98,7 +98,7 @@ yacl_cc_library( "//yacl/base:exception", "//yacl/base:int128", "//yacl/crypto/rand", - "@com_google_absl//absl/types:span", + "@abseil-cpp//absl/types:span", ], alwayslink = 1, ) diff --git a/yacl/math/galois_field/factory/intel_factory.h b/yacl/math/galois_field/factory/intel_factory.h index c45d73b9..6f1839df 100644 --- a/yacl/math/galois_field/factory/intel_factory.h +++ b/yacl/math/galois_field/factory/intel_factory.h @@ -119,7 +119,7 @@ class IntrinsicField : public GFScalarSketch { // compile-time utilities for generating galois field basis for uint64_t and // uint128_t constexpr std::array GenGfBasisArray() { - std::array basis = {0}; + std::array basis = {0}; uint128_t one = yacl::MakeUint128(0, 1); for (size_t i = 0; i < sizeof(T) * 8; ++i) { basis[i] = one << i; diff --git a/yacl/math/galois_field/gf_intrinsic.h b/yacl/math/galois_field/gf_intrinsic.h index 5f533d6b..b4e57d69 100644 --- a/yacl/math/galois_field/gf_intrinsic.h +++ b/yacl/math/galois_field/gf_intrinsic.h @@ -180,7 +180,7 @@ std::vector ExtractHashCoef(T seed, if (mask & index_plus_one) { coef = GfMul(coef, buff[i]); } - mask <<= 1; + mask <<= (uint64_t)1; } ret.push_back(coef); } diff --git a/yacl/math/mpint/BUILD.bazel b/yacl/math/mpint/BUILD.bazel index 1ccfd42e..38d47bfa 100644 --- a/yacl/math/mpint/BUILD.bazel +++ b/yacl/math/mpint/BUILD.bazel @@ -25,8 +25,8 @@ yacl_cc_library( hdrs = ["mp_int_enforce.h"], deps = [ "//yacl/base:exception", - "@com_github_fmtlib_fmt//:fmtlib", - "@com_github_libtom_libtommath//:libtommath", + "@fmt", + "@libtommath", ], ) @@ -40,8 +40,8 @@ yacl_cc_library( ":tommath_ext_types", "//yacl/base:byte_container_view", "//yacl/base:int128", - "@com_github_fmtlib_fmt//:fmtlib", - "@com_github_msgpack_msgpack//:msgpack", + "@fmt", + "@msgpack-c//:msgpack", ], ) @@ -52,7 +52,7 @@ yacl_cc_library( deps = [ ":mp_int_enforce", "//yacl/base:int128", - "@com_github_libtom_libtommath//:libtommath", + "@libtommath", ], ) @@ -66,7 +66,7 @@ yacl_cc_library( "//yacl/base:exception", "//yacl/utils:scope_guard", "//yacl/utils/spi:type_traits", - "@com_github_libtom_libtommath//:libtommath", + "@libtommath", ], ) @@ -76,7 +76,7 @@ yacl_cc_library( hdrs = ["montgomery_math.h"], deps = [ ":mpint", - "@com_github_libtom_libtommath//:libtommath", + "@libtommath", ], ) @@ -85,7 +85,7 @@ yacl_cc_test( srcs = ["montgomery_math_test.cc"], deps = [ ":montgomery_math", - "@com_google_googletest//:gtest", + "@googletest//:gtest", ], ) @@ -94,7 +94,7 @@ yacl_cc_test( srcs = ["tommath_ext_test.cc"], deps = [ ":tommath_ext_features", - "@com_google_googletest//:gtest", + "@googletest//:gtest", ], ) @@ -103,6 +103,6 @@ yacl_cc_test( srcs = ["mp_int_test.cc"], deps = [ ":mpint", - "@com_google_googletest//:gtest", + "@googletest//:gtest", ], ) diff --git a/yacl/math/mpint/benchmark/BUILD.bazel b/yacl/math/mpint/benchmark/BUILD.bazel index 25f0842d..e0f0175c 100644 --- a/yacl/math/mpint/benchmark/BUILD.bazel +++ b/yacl/math/mpint/benchmark/BUILD.bazel @@ -19,7 +19,7 @@ yacl_cc_binary( srcs = ["serialize_bench.cc"], deps = [ "//yacl/math/mpint", - "@com_github_google_benchmark//:benchmark", + "@google_benchmark//:benchmark", ], ) @@ -28,7 +28,7 @@ yacl_cc_binary( srcs = ["mpint_bench.cc"], deps = [ "//yacl/math/mpint", - "@com_github_google_benchmark//:benchmark_main", + "@google_benchmark//:benchmark_main", ], ) @@ -37,7 +37,7 @@ yacl_cc_binary( srcs = ["prime_bench.cc"], deps = [ "//yacl/math/mpint", - "@com_github_google_benchmark//:benchmark_main", + "@google_benchmark//:benchmark_main", ], ) @@ -46,6 +46,6 @@ yacl_cc_binary( srcs = ["mod_bench.cc"], deps = [ "//yacl/math/mpint", - "@com_github_google_benchmark//:benchmark_main", + "@google_benchmark//:benchmark_main", ], ) diff --git a/yacl/math/mpint/tommath_ext_types.cc b/yacl/math/mpint/tommath_ext_types.cc index def7bb16..f16bd579 100644 --- a/yacl/math/mpint/tommath_ext_types.cc +++ b/yacl/math/mpint/tommath_ext_types.cc @@ -82,11 +82,13 @@ void mpx_reserve(mp_int *a, size_t n_digits) { (unsigned)a->used, \ (unsigned)((MP_SIZEOF_BITS(type) + MP_DIGIT_BIT - 1) / MP_DIGIT_BIT)); \ type res = 0u; \ - while (i-- > 0u) { \ - res <<= ((MP_SIZEOF_BITS(type) <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT); \ - res |= (type)a->dp[i]; \ - if (MP_SIZEOF_BITS(type) <= MP_DIGIT_BIT) { \ - break; \ + if (i) { \ + while (i-- > 0u) { \ + res <<= ((MP_SIZEOF_BITS(type) <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT); \ + res |= (type)a->dp[i]; \ + if (MP_SIZEOF_BITS(type) <= MP_DIGIT_BIT) { \ + break; \ + } \ } \ } \ return res; \ diff --git a/yacl/math/mpint/tommath_ext_types.h b/yacl/math/mpint/tommath_ext_types.h index 3e6f2b2c..d3a0fad4 100644 --- a/yacl/math/mpint/tommath_ext_types.h +++ b/yacl/math/mpint/tommath_ext_types.h @@ -19,7 +19,7 @@ #include "yacl/base/int128.h" #define MP_BITS_TO_DIGITS(bits) ((bits) + MP_DIGIT_BIT - 1) / MP_DIGIT_BIT -#define MP_BYTES_TO_DIGITS(bytes) MP_BITS_TO_DIGITS((bytes) * CHAR_BIT) +#define MP_BYTES_TO_DIGITS(bytes) MP_BITS_TO_DIGITS((bytes)*CHAR_BIT) void mpx_init(mp_int *a); void mpx_reserve(mp_int *a, size_t n_digits); diff --git a/yacl/utils/BUILD.bazel b/yacl/utils/BUILD.bazel index e0b3ab4f..e5ba0b44 100644 --- a/yacl/utils/BUILD.bazel +++ b/yacl/utils/BUILD.bazel @@ -22,7 +22,6 @@ yacl_cc_library( hdrs = ["circuit_executor.h"], deps = [ "//yacl/base:dynamic_bitset", - "//yacl/base:int128", "//yacl/io/circuit:bristol_fashion", ], ) @@ -32,7 +31,7 @@ yacl_cc_test( srcs = ["circuit_executor_test.cc"], data = ["//yacl/io/circuit:circuit_data"], deps = [ - "circuit_executor", + ":circuit_executor", "//yacl/crypto/block_cipher:symmetric_crypto", "//yacl/crypto/rand", ], @@ -44,8 +43,8 @@ yacl_cc_library( deps = [ "//yacl/base:exception", "//yacl/base:int128", - "@com_google_absl//absl/numeric:bits", - "@com_google_absl//absl/types:span", + "@abseil-cpp//absl/numeric:bits", + "@abseil-cpp//absl/types:span", ], ) @@ -73,7 +72,7 @@ yacl_cc_test( yacl_cc_library( name = "scope_guard", hdrs = ["scope_guard.h"], - deps = ["@com_google_absl//absl/cleanup"], + deps = ["@abseil-cpp//absl/cleanup"], ) yacl_cc_test( @@ -113,7 +112,7 @@ yacl_cc_library( "serializer_adapter.h", ], deps = [ - "@com_github_msgpack_msgpack//:msgpack", + "@msgpack-c//:msgpack", "@yacl//yacl/base:byte_container_view", "@yacl//yacl/base:int128", "@yacl//yacl/io/msgpack:buffer", @@ -172,11 +171,11 @@ yacl_cc_library( "//yacl/base:block", "//yacl/base:byte_container_view", "//yacl/base:int128", - "@com_github_google_cpu_features//:cpu_features", - "@com_google_absl//absl/types:span", + "@abseil-cpp//absl/types:span", + "@cpu_features//:cpuinfo", ] + select({ "@platforms//cpu:aarch64": [ - "@com_github_dltcollab_sse2neon//:sse2neon", + "@sse2neon", ], "//conditions:default": [], }), @@ -196,7 +195,7 @@ yacl_cc_binary( deps = [ ":matrix_utils", "//yacl/crypto/tools:prg", - "@com_github_google_benchmark//:benchmark_main", + "@google_benchmark//:benchmark_main", ], ) @@ -206,7 +205,7 @@ yacl_cc_binary( deps = [ ":matrix_utils", "//yacl/crypto/tools:prg", - "@com_github_google_benchmark//:benchmark_main", + "@google_benchmark//:benchmark_main", ], ) @@ -218,7 +217,7 @@ yacl_cc_library( deps = [ "//yacl/base:exception", "//yacl/base:int128", - "@com_google_absl//absl/types:span", + "@abseil-cpp//absl/types:span", ], ) @@ -243,7 +242,7 @@ yacl_cc_library( }), deps = select({ "@platforms//cpu:x86_64": [ - "@com_github_google_cpu_features//:cpu_features", + "@cpu_features//:cpuinfo", ], "//conditions:default": [], }), @@ -274,7 +273,7 @@ yacl_cc_binary( linkopts = OMP_LINKFLAGS, deps = [ ":parallel", - "@com_github_google_benchmark//:benchmark", + "@google_benchmark//:benchmark", ] + OMP_DEPS, ) diff --git a/yacl/utils/circuit_executor.cc b/yacl/utils/circuit_executor.cc index 856b8cb3..b93291be 100644 --- a/yacl/utils/circuit_executor.cc +++ b/yacl/utils/circuit_executor.cc @@ -14,6 +14,15 @@ #include "yacl/utils/circuit_executor.h" +#include +#include +#include +#include +#include + +#include "yacl/base/exception.h" +#include "yacl/io/circuit/bristol_fashion.h" + namespace yacl { namespace { @@ -25,24 +34,34 @@ class PlaintextCore { }; } // namespace -template -void PlainExecutor::LoadCircuitFile(const std::string& path) { +void PlainExecutor::LoadCircuitFile(const std::string& path) { io::CircuitReader reader(path); reader.ReadAll(); circ_ = reader.StealCirc(); } +void PlainExecutor::SetupInputs(ByteContainerView bytes) { + YACL_ENFORCE(std::accumulate(circ_->niw.cbegin(), circ_->niw.cend(), + static_cast(0)) == + bytes.size() * 8, + "mismatch input size and input wire size."); + wires_.resize(circ_->nw); + + std::memcpy(wires_.data(), bytes.data(), bytes.size()); +} + template -void PlainExecutor::SetupInputs(absl::Span inputs) { +void PlainExecutor::SetupInputs(absl::Span inputs) { YACL_ENFORCE(inputs.size() == circ_->niv); - for (auto input : inputs) { - wires_.append(input); - } + + dynamic_bitset input_wires; + input_wires.resize(sizeof(T) * 8 * inputs.size()); + std::memcpy(input_wires.data(), inputs.data(), inputs.size() * sizeof(T)); + wires_.append(input_wires); wires_.resize(circ_->nw); } -template -void PlainExecutor::Exec() { +void PlainExecutor::Exec() { // Evaluate all gates, sequentially for (const auto& gate : circ_->gates) { switch (gate.op) { @@ -64,7 +83,7 @@ void PlainExecutor::Exec() { break; } case io::BFCircuit::Op::EQ: { - wires_.set(gate.ow[0], gate.iw[0]); + wires_.set(gate.ow[0], (gate.iw[0] != 0U)); break; } case io::BFCircuit::Op::EQW: { @@ -82,9 +101,34 @@ void PlainExecutor::Exec() { } } +std::vector PlainExecutor::Finalize() { + // Count the total number of output wires (a.k.a. output bits) + size_t total_out_bitnum = 0; + for (size_t i = 0; i < circ_->nov; ++i) { + total_out_bitnum += circ_->now[i]; + } + + const auto out_size = (total_out_bitnum + 7) / 8; + std::vector out(out_size); + + size_t index = wires_.size(); + for (size_t i = 0; i < out_size; ++i) { + dynamic_bitset result(8); + for (size_t j = 0; j < 8; ++j) { + result[j] = wires_[index - 8 + j]; + } + out[out_size - i - 1] = *(static_cast(result.data())); + index -= 8; + } + std::reverse(out.begin(), out.end()); + return out; +} + template -void PlainExecutor::Finalize(absl::Span outputs) { +void PlainExecutor::Finalize(absl::Span outputs) { YACL_ENFORCE(outputs.size() >= circ_->nov); + YACL_ENFORCE(std::all_of(circ_->now.begin(), circ_->now.end(), + [](const auto n) { return n <= sizeof(T) * 8; })); size_t index = wires_.size(); for (size_t i = 0; i < circ_->nov; ++i) { @@ -92,12 +136,17 @@ void PlainExecutor::Finalize(absl::Span outputs) { for (size_t j = 0; j < circ_->now[i]; ++j) { result[j] = wires_[index - circ_->now[i] + j]; } - outputs[circ_->nov - i - 1] = *(T*)result.data(); + outputs[circ_->nov - i - 1] = *result.data(); index -= circ_->now[i]; } } -template class PlainExecutor; -template class PlainExecutor; +template void PlainExecutor::SetupInputs<>(absl::Span inputs); + +template void PlainExecutor::SetupInputs<>(absl::Span inputs); + +template void PlainExecutor::Finalize<>(absl::Span outputs); + +template void PlainExecutor::Finalize<>(absl::Span outputs); } // namespace yacl diff --git a/yacl/utils/circuit_executor.h b/yacl/utils/circuit_executor.h index fe824cf4..5fc825cc 100644 --- a/yacl/utils/circuit_executor.h +++ b/yacl/utils/circuit_executor.h @@ -14,7 +14,9 @@ #pragma once +#include #include +#include #include "yacl/base/dynamic_bitset.h" #include "yacl/io/circuit/bristol_fashion.h" @@ -23,27 +25,43 @@ namespace yacl { // plaintext protocol that executes everything without link -template class PlainExecutor { public: + using BlockType = uint8_t; + // Constructor explicit PlainExecutor() = default; // Load circuit from file (local operation) void LoadCircuitFile(const std::string &path); - // Setup the input wire (local operation) + /// + /// Load inputs functions: Setup the input wire (local operation) + /// + // general setup function, just copies the memory to internal wires_ + void SetupInputs(ByteContainerView bytes); + + // fast path for circuit with "small" bits (e.g. <= 128) + template void SetupInputs(absl::Span inputs); // Execute the circuit void Exec(); - // Finalize and get the result + /// + /// Get results functions: Finalize and get the result + /// + // + // general finalize, get result from wires_ + std::vector Finalize(); + + // fast path for circuit with "small" bits (e.g. <= 128) + template void Finalize(absl::Span outputs); private: // NOTE: please make sure you use the correct order of wires - dynamic_bitset wires_; // shares + dynamic_bitset wires_; // shares std::shared_ptr circ_; // bristol fashion circuit }; diff --git a/yacl/utils/circuit_executor_test.cc b/yacl/utils/circuit_executor_test.cc index 9df0ad4e..1eb4e790 100644 --- a/yacl/utils/circuit_executor_test.cc +++ b/yacl/utils/circuit_executor_test.cc @@ -16,13 +16,14 @@ #include "yacl/utils/circuit_executor.h" #include +#include -#include "absl/strings/escaping.h" #include "gtest/gtest.h" #include "yacl/base/byte_container_view.h" #include "yacl/base/dynamic_bitset.h" #include "yacl/crypto/block_cipher/symmetric_crypto.h" +#include "yacl/crypto/hash/ssl_hash.h" #include "yacl/crypto/rand/rand.h" #include "yacl/io/circuit/bristol_fashion.h" @@ -68,11 +69,11 @@ TEST(ArithmaticTest, Add64Test) { std::vector result(1); /* WHEN */ - PlainExecutor exec; + PlainExecutor exec; exec.LoadCircuitFile(io::BuiltinBFCircuit::Add64Path()); - exec.SetupInputs(absl::MakeSpan(inputs)); + exec.SetupInputs(absl::MakeSpan(inputs)); exec.Exec(); - exec.Finalize(absl::MakeSpan(result)); + exec.Finalize(absl::MakeSpan(result)); /* THEN */ EXPECT_EQ(result[0], Add64(inputs[0], inputs[1])); @@ -84,11 +85,11 @@ TEST(ArithmaticTest, Sub64Test) { std::vector result(1); /* WHEN */ - PlainExecutor exec; + PlainExecutor exec; exec.LoadCircuitFile(io::BuiltinBFCircuit::Sub64Path()); - exec.SetupInputs(absl::MakeSpan(inputs)); + exec.SetupInputs(absl::MakeSpan(inputs)); exec.Exec(); - exec.Finalize(absl::MakeSpan(result)); + exec.Finalize(absl::MakeSpan(result)); /* THEN */ EXPECT_EQ(result[0], Sub64(inputs[0], inputs[1])); @@ -100,11 +101,11 @@ TEST(ArithmaticTest, Neg64Test) { std::vector result(1); /* WHEN */ - PlainExecutor exec; + PlainExecutor exec; exec.LoadCircuitFile(io::BuiltinBFCircuit::Neg64Path()); - exec.SetupInputs(absl::MakeSpan(inputs)); + exec.SetupInputs(absl::MakeSpan(inputs)); exec.Exec(); - exec.Finalize(absl::MakeSpan(result)); + exec.Finalize(absl::MakeSpan(result)); /* THEN */ EXPECT_EQ(result[0], Neg64(inputs[0])); @@ -116,11 +117,11 @@ TEST(ArithmaticTest, Mul64Test) { std::vector result(1); /* WHEN */ - PlainExecutor exec; + PlainExecutor exec; exec.LoadCircuitFile(io::BuiltinBFCircuit::Mul64Path()); - exec.SetupInputs(absl::MakeSpan(inputs)); + exec.SetupInputs(absl::MakeSpan(inputs)); exec.Exec(); - exec.Finalize(absl::MakeSpan(result)); + exec.Finalize(absl::MakeSpan(result)); /* THEN */ EXPECT_EQ(result[0], Mul64(inputs[0], inputs[1])); @@ -132,11 +133,11 @@ TEST(ArithmaticTest, Div64Test) { std::vector result(1); /* WHEN */ - PlainExecutor exec; + PlainExecutor exec; exec.LoadCircuitFile(io::BuiltinBFCircuit::Div64Path()); - exec.SetupInputs(absl::MakeSpan(inputs)); + exec.SetupInputs(absl::MakeSpan(inputs)); exec.Exec(); - exec.Finalize(absl::MakeSpan(result)); + exec.Finalize(absl::MakeSpan(result)); /* THEN */ EXPECT_EQ(result[0], Div64(inputs[0], inputs[1])); @@ -149,11 +150,11 @@ TEST(ArithmaticTest, UDiv64Test) { /* WHEN */ - PlainExecutor exec; + PlainExecutor exec; exec.LoadCircuitFile(io::BuiltinBFCircuit::UDiv64Path()); - exec.SetupInputs(absl::MakeSpan(inputs)); + exec.SetupInputs(absl::MakeSpan(inputs)); exec.Exec(); - exec.Finalize(absl::MakeSpan(result)); + exec.Finalize(absl::MakeSpan(result)); /* THEN */ EXPECT_EQ(result[0], UDiv64(inputs[0], inputs[1])); @@ -165,11 +166,11 @@ TEST(ArithmaticTest, EqzTest) { std::vector result(1); /* WHEN */ - PlainExecutor exec; + PlainExecutor exec; exec.LoadCircuitFile(io::BuiltinBFCircuit::EqzPath()); - exec.SetupInputs(absl::MakeSpan(inputs)); + exec.SetupInputs(absl::MakeSpan(inputs)); exec.Exec(); - exec.Finalize(absl::MakeSpan(result)); + exec.Finalize(absl::MakeSpan(result)); /* THEN */ EXPECT_EQ(result[0], Eqz(inputs[0])); @@ -188,11 +189,11 @@ TEST(CryptoTest, Aes128Test) { std::vector result(1); /* WHEN */ - PlainExecutor exec; + PlainExecutor exec; exec.LoadCircuitFile(io::BuiltinBFCircuit::Aes128Path()); - exec.SetupInputs(absl::MakeSpan(inputs)); + exec.SetupInputs(absl::MakeSpan(inputs)); exec.Exec(); - exec.Finalize(absl::MakeSpan(result)); + exec.Finalize(absl::MakeSpan(result)); /* THEN */ // NOTE: For AES-128 the wire orders are in the reverse order as used in @@ -202,10 +203,28 @@ TEST(CryptoTest, Aes128Test) { // see: https://nigelsmart.github.io/MPC-Circuits/ // // NOTE: it simply means every byte is reversed, not every bit. - auto compare = Aes128(ReverseBytes(inputs[0]), ReverseBytes(inputs[1])); + auto aes = Aes128(ReverseBytes(inputs[0]), ReverseBytes(inputs[1])); // SPDLOG_INFO(ToBinaryString(ReverseBytes(result[0]))); // SPDLOG_INFO(ToBinaryString(compare)); - EXPECT_EQ(ReverseBytes(result[0]), compare); + EXPECT_EQ(ReverseBytes(result[0]), aes); +} + +TEST(CryptoTest, Sha256Test) { + /* GIVEN */ + auto message = crypto::FastRandBytes(crypto::RandLtN(32)); + auto in_buf = io::BuiltinBFCircuit::PrepareSha256Input(message); + + /* WHEN */ + PlainExecutor exec; + exec.LoadCircuitFile(io::BuiltinBFCircuit::Sha256Path()); + exec.SetupInputs(in_buf); + exec.Exec(); + auto result = exec.Finalize(); + + /* THEN */ + auto sha256 = crypto::Sha256Hash().Update(message).CumulativeHash(); + EXPECT_EQ(sha256.size(), result.size()); + EXPECT_TRUE(std::equal(result.begin(), result.end(), sha256.begin())); } } // namespace yacl diff --git a/yacl/utils/matrix_utils.cc b/yacl/utils/matrix_utils.cc index ce450652..44199798 100644 --- a/yacl/utils/matrix_utils.cc +++ b/yacl/utils/matrix_utils.cc @@ -26,7 +26,7 @@ #include "yacl/base/block.h" #ifdef __x86_64 -#include "cpu_features/cpuinfo_x86.h" +#include "cpuinfo_x86.h" #endif namespace yacl { diff --git a/yacl/utils/platform_utils.cc b/yacl/utils/platform_utils.cc index b0b1482b..cbf27f48 100644 --- a/yacl/utils/platform_utils.cc +++ b/yacl/utils/platform_utils.cc @@ -17,7 +17,7 @@ #ifdef __x86_64__ #include -#include "cpu_features/cpuinfo_x86.h" +#include "cpuinfo_x86.h" #endif namespace yacl { diff --git a/yacl/utils/spi/BUILD.bazel b/yacl/utils/spi/BUILD.bazel index 5a572356..ea4645d7 100644 --- a/yacl/utils/spi/BUILD.bazel +++ b/yacl/utils/spi/BUILD.bazel @@ -33,7 +33,7 @@ yacl_cc_library( "//yacl/base:exception", "//yacl/math/mpint", "//yacl/utils:parallel", - "@com_google_absl//absl/types:span", + "@abseil-cpp//absl/types:span", ], ) diff --git a/yacl/utils/spi/argument/BUILD.bazel b/yacl/utils/spi/argument/BUILD.bazel index 06b8e37f..3f3c22b0 100644 --- a/yacl/utils/spi/argument/BUILD.bazel +++ b/yacl/utils/spi/argument/BUILD.bazel @@ -56,7 +56,7 @@ yacl_cc_library( "util.h", ], deps = [ - "@com_google_absl//absl/strings", + "@abseil-cpp//absl/strings", ], ) diff --git a/yacl/utils/spi/sketch/BUILD.bazel b/yacl/utils/spi/sketch/BUILD.bazel index 5216d6e8..ef6a0035 100644 --- a/yacl/utils/spi/sketch/BUILD.bazel +++ b/yacl/utils/spi/sketch/BUILD.bazel @@ -36,6 +36,6 @@ yacl_cc_library( deps = [ "//yacl/io/msgpack:buffer", "//yacl/io/msgpack:spec_traits", - "@com_github_msgpack_msgpack//:msgpack", + "@msgpack-c//:msgpack", ], )