Skip to content

Commit

Permalink
repo-sync-2023-10-20T15:35:58+0800 (#151)
Browse files Browse the repository at this point in the history
* repo-sync-2023-10-20T15:35:58+0800

* Update mcl.patch
  • Loading branch information
Jamie-Cui authored Oct 21, 2023
1 parent dc041ce commit 3982ecb
Show file tree
Hide file tree
Showing 88 changed files with 6,444 additions and 339 deletions.
9 changes: 5 additions & 4 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# 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.
Expand All @@ -14,12 +14,13 @@

common --experimental_repo_remote_exec

build --incompatible_new_actions_api=false
build --incompatible_new_actions_api=false
build --copt=-fdiagnostics-color=always
build --enable_platform_specific_config

build --cxxopt=-std=c++17
build --host_cxxopt=-std=c++17
build --linkopt -lm

# Binary safety flags
build --host_copt=-fPIE
Expand Down
3 changes: 2 additions & 1 deletion .circleci/continue-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ jobs:
- run:
name: "Install homebrew dependencies"
command: |
brew install bazelisk cmake ninja libomp wget md5sha1sum
brew install bazel cmake ninja libomp wget md5sha1sum
(cd /opt/homebrew/Cellar/bazel/*.*.*/libexec/bin && curl -fLO https://github.com/bazelbuild/bazel/releases/download/6.2.1/bazel-6.2.1-darwin-arm64 && chmod +x bazel-6.2.1-darwin-arm64)
- run:
name: "test"
command: |
Expand Down
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ All notable changes to this project will be documented in this file.
> - Add `[Bugfix]` prefix for bug fixes
> - Add `[API]` prefix for API changes
## Staging
> please add your unreleased change here.
## 2023-10-20
- [YACL] 0.4.1 release
- [Feature] Add Sigma-type ZKP Protocols (An unified implementation)
- [Feature] Add ECC Pairing SPI and support to libmcl(ecc, pairing)
- [Feature] Add Multiplication for GF(2^64) and GF(2^128)
- [Bugfix] fix KOS OTe security flaws
- [Feature] Add AVX2 Matrix Transpose

## 2023-05-25
- [YACL] 0.3.3 release
Expand Down
31 changes: 0 additions & 31 deletions SECURITY.md

This file was deleted.

48 changes: 48 additions & 0 deletions bazel/mcl.BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
load("@yacl//bazel:yacl.bzl", "yacl_cmake_external")

package(default_visibility = ["//visibility:public"])

filegroup(
name = "source",
srcs = glob(["**"]),
)

# This is the value could be defined by --config=android_arm64
config_setting(
name = "cpu_arm64_v8a",
values = {"cpu": "arm64-v8a"},
visibility = ["//visibility:private"],
)

default_config = {
"MCL_BUILD_TESTING": "OFF",
# remove dependency on libgmp
"MCL_TEST_WITH_GMP": "OFF",
"MCL_STATIC_LIB": "ON",
}

android_config = {
"MCL_BUILD_TESTING": "OFF",
# remove dependency on libgmp
"MCL_TEST_WITH_GMP": "OFF",
"MCL_STATIC_LIB": "ON",
"MCL_USE_LLVM": "OFF",
"CMAKE_SYSTEM_NAME": "Android",
"CMAKE_ANDROID_NDK": "$ANDROID_NDK_HOME",
"CMAKE_ANDROID_ARCH_ABI": "arm64-v8a",
}

yacl_cmake_external(
name = "mcl",
build_args = ["-j"],
cache_entries = select({
":cpu_arm64_v8a": android_config,
"//conditions:default": default_config,
}),
# generate_crosstool_file = False,
lib_source = ":source",
out_static_libs = [
"libmcl.a",
],
alwayslink = True,
)
34 changes: 34 additions & 0 deletions bazel/patches/mcl.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2b54b261..4c8eb32b 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -146,6 +146,11 @@ target_compile_options(mcl_st PRIVATE ${MCL_COMPILE_OPTIONS})
# target_compile_features(mcl_st PUBLIC cxx_std_11)


+set(TARGET_FLAG "")
+if(DEFINED CMAKE_ANDROID_ARCH_LLVM_TRIPLE)
+ set(TARGET_FLAG "--target=${CMAKE_ANDROID_ARCH_LLVM_TRIPLE}")
+endif()
+
# use bint-x64 on x64, bint${BIT}.ll on the other CPU
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "AMD64") # Win64
find_program(ML64 ml64.exe HINTS "${cl_path}" DOC "path to assembler")
@@ -172,7 +177,7 @@ else()
target_compile_definitions(mcl_st PUBLIC MCL_BINT_ASM_X64=0)

add_custom_command(OUTPUT ${BINT_OBJ}
- COMMAND ${CMAKE_CXX_COMPILER} -c -o ${BINT_OBJ} ${CMAKE_CURRENT_SOURCE_DIR}/src/bint${BIT}.ll -O3 -fPIC
+ COMMAND ${CMAKE_CXX_COMPILER} -c -o ${BINT_OBJ} ${CMAKE_CURRENT_SOURCE_DIR}/src/bint${BIT}.ll -O3 -fPIC ${TARGET_FLAG}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
add_custom_target(gen_bint.o
SOURCES ${BINT_OBJ})
@@ -199,7 +204,7 @@ elseif(${MCL_USE_LLVM})
target_compile_definitions(mcl_st PUBLIC MCL_USE_LLVM=1)

add_custom_command(OUTPUT ${BASE_OBJ}
- COMMAND ${CMAKE_CXX_COMPILER} -c -o ${BASE_OBJ} ${CMAKE_CURRENT_SOURCE_DIR}/src/base${BIT}.ll -O3 -fPIC
+ COMMAND ${CMAKE_CXX_COMPILER} -c -o ${BASE_OBJ} ${CMAKE_CURRENT_SOURCE_DIR}/src/base${BIT}.ll -O3 -fPIC ${TARGET_FLAG}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
add_custom_target(gen_base.o
SOURCES ${BASE_OBJ})
16 changes: 16 additions & 0 deletions bazel/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def yacl_deps():
_com_github_intel_ipp()
_com_github_libsodium()
_com_github_libtom_libtommath()
_com_github_herumi_mcl()

maybe(
git_repository,
Expand Down Expand Up @@ -339,3 +340,18 @@ def _com_github_msgpack_msgpack():
],
build_file = "@yacl//bazel:msgpack.BUILD",
)

def _com_github_herumi_mcl():
maybe(
http_archive,
name = "com_github_herumi_mcl",
strip_prefix = "mcl-1.84.0",
sha256 = "dc655c2eb5b2426736d8ab92ed501de0ac78472f1ee7083919a98a8aca3e76a3",
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.84.0.tar.gz"],
)
10 changes: 2 additions & 8 deletions bazel/yacl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

"""
warpper bazel cc_xx to modify flags.
wrapper bazel cc_xx to modify flags.
"""

load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test")
Expand All @@ -35,6 +35,7 @@ AES_COPT_FLAGS = select({
"//conditions:default": [
"-mavx",
"-maes",
"-mpclmul",
],
})

Expand All @@ -47,22 +48,18 @@ def _yacl_copts():
}) + WARNING_FLAGS

def yacl_cc_binary(
linkopts = [],
copts = [],
**kargs):
cc_binary(
linkopts = linkopts + ["-lm"],
copts = copts + _yacl_copts(),
**kargs
)

def yacl_cc_library(
linkopts = [],
copts = [],
deps = [],
**kargs):
cc_library(
linkopts = linkopts,
copts = _yacl_copts() + copts,
deps = deps + [
"@com_github_gabime_spdlog//:spdlog",
Expand All @@ -81,14 +78,11 @@ def yacl_configure_make(**attrs):
return configure_make(**attrs)

def yacl_cc_test(
linkopts = [],
copts = [],
deps = [],
linkstatic = True,
**kwargs):
cc_test(
# -lm for tcmalloc
linkopts = linkopts + ["-lm"],
copts = _yacl_copts() + copts,
deps = deps + [
"@com_google_googletest//:gtest_main",
Expand Down
11 changes: 11 additions & 0 deletions yacl/base/aligned_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,15 @@ class AlignedAllocator {
template <typename T, std::size_t ALIGNMENT_IN_BYTES = 16>
using AlignedVector = std::vector<T, AlignedAllocator<T, ALIGNMENT_IN_BYTES> >;

template <typename T, std::size_t A>
bool operator==(AlignedAllocator<T, A> const& a0,
AlignedAllocator<T, A> const& a1) {
return a0.ALIGNMENT == a1.ALIGNMENT;
}

template <typename T, size_t A>
bool operator!=(AlignedAllocator<T, A> const& a0,
AlignedAllocator<T, A> const& a1) {
return !(a0 == a1);
}
} // namespace yacl
13 changes: 13 additions & 0 deletions yacl/base/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ struct alignas(16) block {
return _mm_srli_epi64(*this, rhs);
}

inline bool operator==(const block& rhs) const {
auto& x = as<std::uint64_t>();
auto& y = rhs.as<std::uint64_t>();
return x[0] == y[0] && x[1] == y[1];
}
inline bool operator!=(const block& rhs) const {
auto& x = as<std::uint64_t>();
auto& y = rhs.as<std::uint64_t>();
return x[0] != y[0] || x[1] != y[1];
}

inline int movemask_epi8() const { return _mm_movemask_epi8(*this); }

template <typename T>
Expand Down Expand Up @@ -125,4 +136,6 @@ inline block toBlock(const std::uint8_t* data) {
return toBlock(((std::uint64_t*)data)[1], ((std::uint64_t*)data)[0]);
}

inline uint128_t toU128(block data) { return data.as<uint128_t>()[0]; }

} // namespace yacl
28 changes: 28 additions & 0 deletions yacl/crypto/base/ecc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ yacl_cc_library(
name = "ecc",
deps = [
"//yacl/crypto/base/ecc/libsodium",
"//yacl/crypto/base/ecc/mcl:ecc",
"//yacl/crypto/base/ecc/openssl",
"//yacl/crypto/base/ecc/toy",
],
Expand Down Expand Up @@ -89,3 +90,30 @@ yacl_cc_test(
"@yacl//yacl/utils:parallel",
],
)

yacl_cc_library(
name = "pairing_spi",
srcs = ["pairing_spi.cc"],
hdrs = ["pairing_spi.h"],
deps = [
":spi",
"//yacl/crypto/base/field:field_spi",
"@com_google_absl//absl/strings",
],
)

yacl_cc_library(
name = "pairing",
deps = [
"//yacl/crypto/base/ecc/mcl:pairing",
],
)

yacl_cc_test(
name = "pairing_test",
srcs = ["pairing_test.cc"],
deps = [
":pairing",
"@yacl//yacl/utils:parallel",
],
)
18 changes: 11 additions & 7 deletions yacl/crypto/base/ecc/benchmark/bench_ecc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,22 @@ class EccBencher {

// for small func
benchmark::IterationCount n = 1000;
benchmark::RegisterBenchmark(
fmt::format("{}/BM_HashPoint", prefix).c_str(),
[this](benchmark::State& st) { BenchHashPoint(st); })
->Iterations(n);
// mcl not support hash point now
if (absl::AsciiStrToLower(ec_->GetLibraryName()) != "libmcl") {
benchmark::RegisterBenchmark(
fmt::format("{}/BM_HashPoint", prefix).c_str(),
[this](benchmark::State& st) { BenchHashPoint(st); })
->Iterations(n);
} else {
fmt::print("\t{} not supports BM_HashPoint\n", ec_->GetLibraryName());
}
benchmark::RegisterBenchmark(
fmt::format("{}/BM_PointEqual", prefix).c_str(),
[this](benchmark::State& st) { BenchPointEqual(st); })
->Iterations(n);
benchmark::RegisterBenchmark(fmt::format("{}/BM_Add", prefix).c_str(),
[this](benchmark::State& st) { BenchAdd(st); })
->Iterations(n);
;
}

void BenchMulBase(benchmark::State& state) {
Expand Down Expand Up @@ -159,13 +163,13 @@ void InitAndRunBenchmarks() {
for (const std::string& curve : curves) {
if (!FLAGS_lib.empty()) {
benchers.emplace_back(
EcGroupFactory::Instance().Create(curve, Lib = FLAGS_lib));
EcGroupFactory::Instance().Create(curve, ArgLib = FLAGS_lib));
continue;
}

for (const auto& lib : EcGroupFactory::Instance().ListLibraries(curve)) {
benchers.emplace_back(
EcGroupFactory::Instance().Create(curve, Lib = lib));
EcGroupFactory::Instance().Create(curve, ArgLib = lib));
}
}

Expand Down
Loading

0 comments on commit 3982ecb

Please sign in to comment.