From 2e551781a17fe09a8244f85d1776ace03bd1d9b3 Mon Sep 17 00:00:00 2001 From: anakinxc Date: Wed, 31 Jul 2024 21:26:02 +0800 Subject: [PATCH 1/4] buildable --- bazel/microsoft_apsi.BUILD | 1 + bazel/patches/apsi-fourq.patch | 143 ++++++++++++++++ bazel/patches/apsi.patch | 305 --------------------------------- bazel/repositories.bzl | 10 +- 4 files changed, 151 insertions(+), 308 deletions(-) create mode 100644 bazel/patches/apsi-fourq.patch diff --git a/bazel/microsoft_apsi.BUILD b/bazel/microsoft_apsi.BUILD index 3476341..18d373d 100644 --- a/bazel/microsoft_apsi.BUILD +++ b/bazel/microsoft_apsi.BUILD @@ -35,6 +35,7 @@ psi_cmake_external( deps = [ "@com_github_facebook_zstd//:zstd", "@com_github_log4cplus_log4cplus//:log4cplus", + "@com_github_microsoft_FourQlib//:FourQlib", "@com_github_microsoft_gsl//:Microsoft.GSL", "@com_github_microsoft_kuku//:kuku", "@com_github_microsoft_seal//:seal", diff --git a/bazel/patches/apsi-fourq.patch b/bazel/patches/apsi-fourq.patch new file mode 100644 index 0000000..b0d4520 --- /dev/null +++ b/bazel/patches/apsi-fourq.patch @@ -0,0 +1,143 @@ +diff --git a/CMakeLists.txt b/CMakeLists.txt +index 78d54a6..471ab00 100644 +--- a/CMakeLists.txt ++++ b/CMakeLists.txt +@@ -257,6 +257,7 @@ target_include_directories(apsi PUBLIC + target_include_directories(apsi PUBLIC + $ + $) ++ + apsi_set_version(apsi) + apsi_link_threads(apsi) + apsi_install_target(apsi APSITargets) +@@ -273,10 +274,9 @@ if(APSI_USE_ZMQ) + target_link_libraries(apsi PUBLIC libzmq-static cppzmq-static) + endif() + +-# Configurations for FourQlib: system, arch, SIMD, and assembler +-target_compile_options(apsi PUBLIC -DHAVE_CONFIG) +-target_compile_options(apsi PUBLIC -DUSE_SECURE_SEED) +-target_compile_options(apsi PUBLIC -DUSE_ENDO=true) ++# Add FourQlib ++target_include_directories(apsi PUBLIC ${EXT_BUILD_DEPS}/FourQlib/include) ++target_link_libraries(apsi PUBLIC FourQ) + + # Set system + if(MSVC) +@@ -285,59 +285,6 @@ elseif (UNIX) + target_compile_options(apsi PUBLIC -D__LINUX__) + endif() + +-# Detect architecture +-include(DetectArch) +-if(APSI_FOURQ_ARM64) +- # _ARM64_ needs to be set if the ARM64 optimizations are used +- # (in UNIX) or the generic implementation is used (Windows) +- target_compile_options(apsi PUBLIC -D_ARM64_) +-endif() +-if(CMAKE_SYSTEM_PROCESSOR STREQUAL x86) +- target_compile_options(apsi PUBLIC -D_X86_) +-endif() +- +-if(APSI_FOURQ_AMD64) +- target_compile_options(apsi PUBLIC -D_AMD64_) +- message(STATUS "FourQlib optimization: arch=AMD64") +-elseif(APSI_FOURQ_ARM64 AND UNIX) +- message(STATUS "FourQlib optimization: arch=ARM64") +-else() +- target_compile_options(apsi PUBLIC -D_GENERIC_) +- message(STATUS "FourQlib optimization: arch=GENERIC") +-endif() +- +-# Detect AVX instructions +-if(APSI_FOURQ_AMD64 AND (APSI_USE_AVX OR APSI_USE_AVX2)) +- include(FindAVX) +- check_for_avx(apsi) +- if(HAVE_AVX2_EXTENSIONS AND APSI_USE_AVX2) +- target_compile_options(apsi PUBLIC -D_AVX2_) +- set(APSI_USE_AVX OFF CACHE BOOL ${APSI_USE_AVX_OPTION_STR} FORCE) +- message(STATUS "FourQlib optimization: simd=AVX2") +- elseif(HAVE_AVX_EXTENSIONS AND APSI_USE_AVX) +- target_compile_options(apsi PUBLIC -D_AVX_) +- set(APSI_USE_AVX2 OFF CACHE BOOL ${APSI_USE_AVX2_OPTION_STR} FORCE) +- message(STATUS "FourQlib optimization: simd=AVX") +- endif() +-else() +- set(APSI_USE_AVX OFF CACHE BOOL ${APSI_USE_AVX_OPTION_STR} FORCE) +- set(APSI_USE_AVX2 OFF CACHE BOOL ${APSI_USE_AVX2_OPTION_STR} FORCE) +- message(STATUS "FourQlib optimization: simd=OFF") +-endif() +- +-# Use optimized assembly on UNIX +-if(APSI_USE_ASM AND UNIX AND NOT APPLE AND NOT CYGWIN AND NOT MINGW) +- check_language(ASM) +- if(CMAKE_ASM_COMPILER) +- enable_language(ASM) +- target_compile_options(apsi PUBLIC -D_ASM_) +- message(STATUS "FourQlib optimization: asm=ON") +- endif() +-else() +- set(APSI_USE_ASM OFF CACHE BOOL ${APSI_USE_ASM_OPTION_STR} FORCE) +- message(STATUS "FourQlib optimization: asm=OFF") +-endif() +- + # Add source files to library and header files to install + # Must follow configurations for FourQlib + set(APSI_SOURCE_FILES "") +diff --git a/common/apsi/CMakeLists.txt b/common/apsi/CMakeLists.txt +index a65bbfe..60e246e 100644 +--- a/common/apsi/CMakeLists.txt ++++ b/common/apsi/CMakeLists.txt +@@ -28,7 +28,6 @@ install( + ${APSI_INCLUDES_INSTALL_DIR}/apsi + ) + +-add_subdirectory(fourq) + add_subdirectory(network) + add_subdirectory(oprf) + add_subdirectory(util) +diff --git a/common/apsi/network/zmq/zmq_channel.cpp b/common/apsi/network/zmq/zmq_channel.cpp +index bcaa013..93e2b4a 100644 +--- a/common/apsi/network/zmq/zmq_channel.cpp ++++ b/common/apsi/network/zmq/zmq_channel.cpp +@@ -8,7 +8,7 @@ + #include + + // APSI +-#include "apsi/fourq/random.h" ++#include "random.h" + #include "apsi/log.h" + #include "apsi/network/result_package_generated.h" + #include "apsi/network/sop_generated.h" +diff --git a/common/apsi/oprf/ecpoint.cpp b/common/apsi/oprf/ecpoint.cpp +index d12313f..5fd9be1 100644 +--- a/common/apsi/oprf/ecpoint.cpp ++++ b/common/apsi/oprf/ecpoint.cpp +@@ -10,10 +10,10 @@ + #include "apsi/util/utils.h" + + // FourQ +-#include "apsi/fourq/FourQ.h" +-#include "apsi/fourq/FourQ_api.h" +-#include "apsi/fourq/FourQ_internal.h" +-#include "apsi/fourq/random.h" ++#include "FourQ.h" ++#include "FourQ_api.h" ++#include "FourQ_internal.h" ++#include "random.h" + + // SEAL + #include "seal/randomgen.h" +diff --git a/common/apsi/util/label_encryptor.cpp b/common/apsi/util/label_encryptor.cpp +index 3e00b5e..57a5b40 100644 +--- a/common/apsi/util/label_encryptor.cpp ++++ b/common/apsi/util/label_encryptor.cpp +@@ -9,7 +9,7 @@ + #include + + // APSI +-#include "apsi/fourq/random.h" ++#include "random.h" + #include "apsi/util/label_encryptor.h" + #include "apsi/util/utils.h" + diff --git a/bazel/patches/apsi.patch b/bazel/patches/apsi.patch index abd35ac..f3b0566 100644 --- a/bazel/patches/apsi.patch +++ b/bazel/patches/apsi.patch @@ -1,295 +1,3 @@ -diff --git a/common/apsi/fourq/CMakeLists.txt b/common/apsi/fourq/CMakeLists.txt -index 3b15780..5085038 100644 ---- a/common/apsi/fourq/CMakeLists.txt -+++ b/common/apsi/fourq/CMakeLists.txt -@@ -8,8 +8,21 @@ set(APSI_SOURCE_FILES ${APSI_SOURCE_FILES} - ${CMAKE_CURRENT_LIST_DIR}/eccp2_no_endo.c - ${CMAKE_CURRENT_LIST_DIR}/eccp2.c - ${CMAKE_CURRENT_LIST_DIR}/hash_to_curve.c -+ ${CMAKE_CURRENT_LIST_DIR}/kex.c - ${CMAKE_CURRENT_LIST_DIR}/random.c - ) -+# Add header files for installation -+install( -+ FILES -+ ${CMAKE_CURRENT_LIST_DIR}/FourQ_api.h -+ ${CMAKE_CURRENT_LIST_DIR}/FourQ_internal.h -+ ${CMAKE_CURRENT_LIST_DIR}/FourQ_params.h -+ ${CMAKE_CURRENT_LIST_DIR}/FourQ_tables.h -+ ${CMAKE_CURRENT_LIST_DIR}/FourQ.h -+ ${CMAKE_CURRENT_LIST_DIR}/table_lookup.h -+ DESTINATION -+ ${APSI_INCLUDES_INSTALL_DIR}/apsi/fourq -+) - - if(APSI_FOURQ_AMD64) - add_subdirectory(amd64) -diff --git a/common/apsi/fourq/FourQ_internal.h b/common/apsi/fourq/FourQ_internal.h -index 009bb1d..5aa9886 100644 ---- a/common/apsi/fourq/FourQ_internal.h -+++ b/common/apsi/fourq/FourQ_internal.h -@@ -143,7 +143,7 @@ static __inline unsigned int is_digit_lessthan_ct(digit_t x, digit_t y) - - // 64x64-bit multiplication - #define MUL128(multiplier, multiplicand, product) \ -- mp_mul( \ -+ fq_mp_mul( \ - (digit_t *)&(multiplier), \ - (digit_t *)&(multiplicand), \ - (digit_t *)&(product), \ -@@ -151,12 +151,12 @@ static __inline unsigned int is_digit_lessthan_ct(digit_t x, digit_t y) - - // 128-bit addition, inputs < 2^127 - #define ADD128(addend1, addend2, addition) \ -- mp_add((digit_t *)(addend1), (digit_t *)(addend2), (digit_t *)(addition), NWORDS_FIELD); -+ fq_mp_add((digit_t *)(addend1), (digit_t *)(addend2), (digit_t *)(addition), NWORDS_FIELD); - - // 128-bit addition with output carry - #define ADC128(addend1, addend2, carry, addition) \ - (carry) = \ -- mp_add((digit_t *)(addend1), (digit_t *)(addend2), (digit_t *)(addition), NWORDS_FIELD); -+ fq_mp_add((digit_t *)(addend1), (digit_t *)(addend2), (digit_t *)(addition), NWORDS_FIELD); - - #elif (TARGET == TARGET_AMD64 && OS_TARGET == OS_WIN) - -@@ -257,10 +257,10 @@ static __inline unsigned int is_digit_lessthan_ct(digit_t x, digit_t y) - bool is_zero_ct(digit_t *a, unsigned int nwords); - - // Multiprecision addition, c = a+b. Returns the carry bit --unsigned int mp_add(digit_t *a, digit_t *b, digit_t *c, unsigned int nwords); -+unsigned int fq_mp_add(digit_t *a, digit_t *b, digit_t *c, unsigned int nwords); - - // Schoolbook multiprecision multiply, c = a*b --void mp_mul(const digit_t *a, const digit_t *b, digit_t *c, const unsigned int nwords); -+void fq_mp_mul(const digit_t *a, const digit_t *b, digit_t *c, const unsigned int nwords); - - // Multiprecision subtraction, c = a-b. Returns the borrow bit - #if defined(GENERIC_IMPLEMENTATION) -diff --git a/common/apsi/fourq/generic/fp.h b/common/apsi/fourq/generic/fp.h -index f475de1..e24a26a 100644 ---- a/common/apsi/fourq/generic/fp.h -+++ b/common/apsi/fourq/generic/fp.h -@@ -172,7 +172,7 @@ void mod1271(felm_t a) - ADDC(borrow, a[NWORDS_FIELD - 1], (mask >> 1), borrow, a[NWORDS_FIELD - 1]); - } - --void mp_mul(const digit_t *a, const digit_t *b, digit_t *c, const unsigned int nwords) -+void fq_mp_mul(const digit_t *a, const digit_t *b, digit_t *c, const unsigned int nwords) - { // Schoolbook multiprecision multiply, c = a*b - unsigned int i, j; - digit_t u, v, UV[2]; -@@ -195,7 +195,7 @@ void mp_mul(const digit_t *a, const digit_t *b, digit_t *c, const unsigned int n - } - } - --unsigned int mp_add(digit_t *a, digit_t *b, digit_t *c, unsigned int nwords) -+unsigned int fq_mp_add(digit_t *a, digit_t *b, digit_t *c, unsigned int nwords) - { // Multiprecision addition, c = a+b, where lng(a) = lng(b) = nwords. Returns the carry bit - unsigned int i, carry = 0; - -@@ -263,13 +263,13 @@ void fpinv1271(felm_t a) - static void multiply(const digit_t *a, const digit_t *b, digit_t *c) - { // Schoolbook multiprecision multiply, c = a*b - -- mp_mul(a, b, c, NWORDS_ORDER); -+ fq_mp_mul(a, b, c, NWORDS_ORDER); - } - - static unsigned int add(const digit_t *a, const digit_t *b, digit_t *c, const unsigned int nwords) - { // Multiprecision addition, c = a+b, where lng(a) = lng(b) = nwords. Returns the carry bit - -- return mp_add((digit_t *)a, (digit_t *)b, c, (unsigned int)nwords); -+ return fq_mp_add((digit_t *)a, (digit_t *)b, c, (unsigned int)nwords); - } - - unsigned int subtract(const digit_t *a, const digit_t *b, digit_t *c, const unsigned int nwords) -diff --git a/common/apsi/fourq/kex.c b/common/apsi/fourq/kex.c -new file mode 100644 -index 0000000..d59af6d ---- /dev/null -+++ b/common/apsi/fourq/kex.c -@@ -0,0 +1,181 @@ -+/******************************************************************************** -+* FourQlib: a high-performance crypto library based on the elliptic curve FourQ -+* -+* Copyright (c) Microsoft Corporation. All rights reserved. -+* -+* Abstract: Diffie-Hellman key exchange based on FourQ -+* option 1: co-factor ecdh using compressed 32-byte public keys, -+* (see https://datatracker.ietf.org/doc/draft-ladd-cfrg-4q/). -+* option 2: co-factor ecdh using uncompressed, 64-byte public keys. -+*********************************************************************************/ -+ -+#include "apsi/fourq/FourQ_internal.h" -+#include "apsi/fourq/FourQ_params.h" -+#include "apsi/fourq/random.h" -+#include -+ -+ -+static __inline bool is_neutral_point(point_t P) -+{ // Is P the neutral point (0,1)? -+ // SECURITY NOTE: this function does not run in constant time (input point P is assumed to be public). -+ -+ if (is_zero_ct((digit_t*)P->x, 2*NWORDS_FIELD) && is_zero_ct(&((digit_t*)P->y)[1], 2*NWORDS_FIELD-1) && is_digit_zero_ct(P->y[0][0] - 1)) { -+ return true; -+ } -+ return false; -+} -+ -+ -+/*************** ECDH USING COMPRESSED, 32-BYTE PUBLIC KEYS ***************/ -+ -+ECCRYPTO_STATUS CompressedPublicKeyGeneration(const unsigned char* SecretKey, unsigned char* PublicKey) -+{ // Compressed public key generation for key exchange -+ // It produces a public key PublicKey, which is the encoding of P = SecretKey*G (G is the generator). -+ // Input: 32-byte SecretKey -+ // Output: 32-byte PublicKey -+ point_t P; -+ -+ ecc_mul_fixed((digit_t*)SecretKey, P); // Compute public key -+ encode(P, PublicKey); // Encode public key -+ -+ return ECCRYPTO_SUCCESS; -+} -+ -+ -+ECCRYPTO_STATUS CompressedKeyGeneration(unsigned char* SecretKey, unsigned char* PublicKey) -+{ // Keypair generation for key exchange. Public key is compressed to 32 bytes -+ // It produces a private key SecretKey and a public key PublicKey, which is the encoding of P = SecretKey*G (G is the generator). -+ // Outputs: 32-byte SecretKey and 32-byte PublicKey -+ ECCRYPTO_STATUS Status = ECCRYPTO_ERROR_UNKNOWN; -+ -+ Status = RandomBytesFunction(SecretKey, 32); -+ if (Status != ECCRYPTO_SUCCESS) { -+ goto cleanup; -+ } -+ -+ Status = CompressedPublicKeyGeneration(SecretKey, PublicKey); -+ if (Status != ECCRYPTO_SUCCESS) { -+ goto cleanup; -+ } -+ -+ return ECCRYPTO_SUCCESS; -+ -+cleanup: -+ clear_words((unsigned int*)SecretKey, 256/(sizeof(unsigned int)*8)); -+ clear_words((unsigned int*)PublicKey, 256/(sizeof(unsigned int)*8)); -+ -+ return Status; -+} -+ -+ -+ECCRYPTO_STATUS CompressedSecretAgreement(const unsigned char* SecretKey, const unsigned char* PublicKey, unsigned char* SharedSecret) -+{ // Secret agreement computation for key exchange using a compressed, 32-byte public key -+ // The output is the y-coordinate of SecretKey*A, where A is the decoding of the public key PublicKey. -+ // Inputs: 32-byte SecretKey and 32-byte PublicKey -+ // Output: 32-byte SharedSecret -+ point_t A; -+ ECCRYPTO_STATUS Status = ECCRYPTO_ERROR_UNKNOWN; -+ -+ if ((PublicKey[15] & 0x80) != 0) { // Is bit128(PublicKey) = 0? -+ Status = ECCRYPTO_ERROR_INVALID_PARAMETER; -+ goto cleanup; -+ } -+ -+ Status = decode(PublicKey, A); // Also verifies that A is on the curve. If it is not, it fails -+ if (Status != ECCRYPTO_SUCCESS) { -+ goto cleanup; -+ } -+ -+ Status = ecc_mul(A, (digit_t*)SecretKey, A, true); -+ if (Status != ECCRYPTO_SUCCESS) { -+ goto cleanup; -+ } -+ -+ if (is_neutral_point(A)) { // Is output = neutral point (0,1)? -+ Status = ECCRYPTO_ERROR_SHARED_KEY; -+ goto cleanup; -+ } -+ -+ memmove(SharedSecret, (unsigned char*)A->y, 32); -+ -+ return ECCRYPTO_SUCCESS; -+ -+cleanup: -+ clear_words((unsigned int*)SharedSecret, 256/(sizeof(unsigned int)*8)); -+ -+ return Status; -+} -+ -+ -+/*************** ECDH USING UNCOMPRESSED PUBLIC KEYS ***************/ -+ -+ECCRYPTO_STATUS PublicKeyGeneration(const unsigned char* SecretKey, unsigned char* PublicKey) -+{ // Public key generation for key exchange -+ // It produces the public key PublicKey = SecretKey*G, where G is the generator. -+ // Input: 32-byte SecretKey -+ // Output: 64-byte PublicKey -+ -+ ecc_mul_fixed((digit_t*)SecretKey, (point_affine*)PublicKey); // Compute public key -+ -+ return ECCRYPTO_SUCCESS; -+} -+ -+ -+ECCRYPTO_STATUS KeyGeneration(unsigned char* SecretKey, unsigned char* PublicKey) -+{ // Keypair generation for key exchange -+ // It produces a private key SecretKey and computes the public key PublicKey = SecretKey*G, where G is the generator. -+ // Outputs: 32-byte SecretKey and 64-byte PublicKey -+ ECCRYPTO_STATUS Status = ECCRYPTO_ERROR_UNKNOWN; -+ -+ Status = RandomBytesFunction(SecretKey, 32); -+ if (Status != ECCRYPTO_SUCCESS) { -+ goto cleanup; -+ } -+ -+ Status = PublicKeyGeneration(SecretKey, PublicKey); -+ if (Status != ECCRYPTO_SUCCESS) { -+ goto cleanup; -+ } -+ -+ return ECCRYPTO_SUCCESS; -+ -+cleanup: -+ clear_words((unsigned int*)SecretKey, 256/(sizeof(unsigned int)*8)); -+ clear_words((unsigned int*)PublicKey, 512/(sizeof(unsigned int)*8)); -+ -+ return Status; -+} -+ -+ -+ECCRYPTO_STATUS SecretAgreement(const unsigned char* SecretKey, const unsigned char* PublicKey, unsigned char* SharedSecret) -+{ // Secret agreement computation for key exchange -+ // The output is the y-coordinate of SecretKey*PublicKey. -+ // Inputs: 32-byte SecretKey and 64-byte PublicKey -+ // Output: 32-byte SharedSecret -+ point_t A; -+ ECCRYPTO_STATUS Status = ECCRYPTO_ERROR_UNKNOWN; -+ -+ if (((PublicKey[15] & 0x80) != 0) || ((PublicKey[31] & 0x80) != 0) || ((PublicKey[47] & 0x80) != 0) || ((PublicKey[63] & 0x80) != 0)) { // Are PublicKey_x[i] and PublicKey_y[i] < 2^127? -+ Status = ECCRYPTO_ERROR_INVALID_PARAMETER; -+ goto cleanup; -+ } -+ -+ Status = ecc_mul((point_affine*)PublicKey, (digit_t*)SecretKey, A, true); // Also verifies that PublicKey is a point on the curve. If it is not, it fails -+ if (Status != ECCRYPTO_SUCCESS) { -+ goto cleanup; -+ } -+ -+ if (is_neutral_point(A)) { // Is output = neutral point (0,1)? -+ Status = ECCRYPTO_ERROR_SHARED_KEY; -+ goto cleanup; -+ } -+ -+ memmove(SharedSecret, (unsigned char*)A->y, 32); -+ -+ return ECCRYPTO_SUCCESS; -+ -+cleanup: -+ clear_words((unsigned int*)SharedSecret, 256/(sizeof(unsigned int)*8)); -+ -+ return Status; -+} diff --git a/receiver/apsi/itt.h b/receiver/apsi/itt.h index e683045..067d244 100644 --- a/receiver/apsi/itt.h @@ -398,16 +106,3 @@ index 4c4e116..8eb34fc 100644 } union Request { ParmsRequest, OPRFRequest, QueryRequest } -diff --git a/CMakeLists.txt b/CMakeLists.txt -index 78d54a6..46a452e 100644 ---- a/CMakeLists.txt -+++ b/CMakeLists.txt -@@ -326,7 +326,7 @@ else() - endif() - - # Use optimized assembly on UNIX --if(APSI_USE_ASM AND UNIX AND NOT APPLE AND NOT CYGWIN AND NOT MINGW) -+if(APSI_USE_ASM AND UNIX AND APSI_FOURQ_AMD64 AND NOT APPLE AND NOT CYGWIN AND NOT MINGW) - check_language(ASM) - if(CMAKE_ASM_COMPILER) - enable_language(ASM) diff --git a/bazel/repositories.bzl b/bazel/repositories.bzl index d706168..dc22eb3 100644 --- a/bazel/repositories.bzl +++ b/bazel/repositories.bzl @@ -54,10 +54,10 @@ def _yacl(): http_archive, name = "yacl", urls = [ - "https://github.com/secretflow/yacl/archive/refs/tags/0.4.5b4_nightly_20240731.tar.gz", + "https://github.com/secretflow/yacl/archive/refs/tags/0.4.5b4_fourq_test.tar.gz", ], - strip_prefix = "yacl-0.4.5b4_nightly_20240731", - sha256 = "e92484a9a60aaf86130157d9568b2bf7812bac4096cb108d565538268d74ea7e", + strip_prefix = "yacl-0.4.5b4_fourq_test", + sha256 = "5e490cba30bcb03e751937795a14fb5c9fd0a24d61095c8ec937a86ce7f96ec9", ) def _bazel_platform(): @@ -186,6 +186,10 @@ def _com_github_microsoft_apsi(): patch_args = ["-p1"], patches = [ "@psi//bazel:patches/apsi.patch", + "@psi//bazel:patches/apsi-fourq.patch", + ], + patch_cmds = [ + "rm -rf common/apsi/fourq", ], ) From a7eaa73598a3708d770ce92da603ce150951ce50 Mon Sep 17 00:00:00 2001 From: anakinxc Date: Wed, 31 Jul 2024 21:40:24 +0800 Subject: [PATCH 2/4] wip --- bazel/patches/apsi-fourq.patch | 72 +--------------------------------- psi/ecdh/BUILD.bazel | 2 +- psi/ecdh/basic_ecdh_oprf.cc | 4 +- 3 files changed, 5 insertions(+), 73 deletions(-) diff --git a/bazel/patches/apsi-fourq.patch b/bazel/patches/apsi-fourq.patch index b0d4520..060d866 100644 --- a/bazel/patches/apsi-fourq.patch +++ b/bazel/patches/apsi-fourq.patch @@ -1,16 +1,8 @@ diff --git a/CMakeLists.txt b/CMakeLists.txt -index 78d54a6..471ab00 100644 +index 78d54a6..166047c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt -@@ -257,6 +257,7 @@ target_include_directories(apsi PUBLIC - target_include_directories(apsi PUBLIC - $ - $) -+ - apsi_set_version(apsi) - apsi_link_threads(apsi) - apsi_install_target(apsi APSITargets) -@@ -273,10 +274,9 @@ if(APSI_USE_ZMQ) +@@ -273,10 +273,9 @@ if(APSI_USE_ZMQ) target_link_libraries(apsi PUBLIC libzmq-static cppzmq-static) endif() @@ -24,66 +16,6 @@ index 78d54a6..471ab00 100644 # Set system if(MSVC) -@@ -285,59 +285,6 @@ elseif (UNIX) - target_compile_options(apsi PUBLIC -D__LINUX__) - endif() - --# Detect architecture --include(DetectArch) --if(APSI_FOURQ_ARM64) -- # _ARM64_ needs to be set if the ARM64 optimizations are used -- # (in UNIX) or the generic implementation is used (Windows) -- target_compile_options(apsi PUBLIC -D_ARM64_) --endif() --if(CMAKE_SYSTEM_PROCESSOR STREQUAL x86) -- target_compile_options(apsi PUBLIC -D_X86_) --endif() -- --if(APSI_FOURQ_AMD64) -- target_compile_options(apsi PUBLIC -D_AMD64_) -- message(STATUS "FourQlib optimization: arch=AMD64") --elseif(APSI_FOURQ_ARM64 AND UNIX) -- message(STATUS "FourQlib optimization: arch=ARM64") --else() -- target_compile_options(apsi PUBLIC -D_GENERIC_) -- message(STATUS "FourQlib optimization: arch=GENERIC") --endif() -- --# Detect AVX instructions --if(APSI_FOURQ_AMD64 AND (APSI_USE_AVX OR APSI_USE_AVX2)) -- include(FindAVX) -- check_for_avx(apsi) -- if(HAVE_AVX2_EXTENSIONS AND APSI_USE_AVX2) -- target_compile_options(apsi PUBLIC -D_AVX2_) -- set(APSI_USE_AVX OFF CACHE BOOL ${APSI_USE_AVX_OPTION_STR} FORCE) -- message(STATUS "FourQlib optimization: simd=AVX2") -- elseif(HAVE_AVX_EXTENSIONS AND APSI_USE_AVX) -- target_compile_options(apsi PUBLIC -D_AVX_) -- set(APSI_USE_AVX2 OFF CACHE BOOL ${APSI_USE_AVX2_OPTION_STR} FORCE) -- message(STATUS "FourQlib optimization: simd=AVX") -- endif() --else() -- set(APSI_USE_AVX OFF CACHE BOOL ${APSI_USE_AVX_OPTION_STR} FORCE) -- set(APSI_USE_AVX2 OFF CACHE BOOL ${APSI_USE_AVX2_OPTION_STR} FORCE) -- message(STATUS "FourQlib optimization: simd=OFF") --endif() -- --# Use optimized assembly on UNIX --if(APSI_USE_ASM AND UNIX AND NOT APPLE AND NOT CYGWIN AND NOT MINGW) -- check_language(ASM) -- if(CMAKE_ASM_COMPILER) -- enable_language(ASM) -- target_compile_options(apsi PUBLIC -D_ASM_) -- message(STATUS "FourQlib optimization: asm=ON") -- endif() --else() -- set(APSI_USE_ASM OFF CACHE BOOL ${APSI_USE_ASM_OPTION_STR} FORCE) -- message(STATUS "FourQlib optimization: asm=OFF") --endif() -- - # Add source files to library and header files to install - # Must follow configurations for FourQlib - set(APSI_SOURCE_FILES "") diff --git a/common/apsi/CMakeLists.txt b/common/apsi/CMakeLists.txt index a65bbfe..60e246e 100644 --- a/common/apsi/CMakeLists.txt diff --git a/psi/ecdh/BUILD.bazel b/psi/ecdh/BUILD.bazel index 29afb88..c2b0b89 100644 --- a/psi/ecdh/BUILD.bazel +++ b/psi/ecdh/BUILD.bazel @@ -64,7 +64,7 @@ psi_cc_library( ":ecdh_oprf", "//psi/cryptor:ecc_utils", "//psi/cryptor:sm2_cryptor", - "@com_github_microsoft_apsi//:apsi", + "@com_github_microsoft_FourQlib//:FourQlib", "@com_google_absl//absl/types:span", "@yacl//yacl/base:exception", "@yacl//yacl/crypto/hash:blake3", diff --git a/psi/ecdh/basic_ecdh_oprf.cc b/psi/ecdh/basic_ecdh_oprf.cc index b2850bd..3ac22a0 100644 --- a/psi/ecdh/basic_ecdh_oprf.cc +++ b/psi/ecdh/basic_ecdh_oprf.cc @@ -18,8 +18,8 @@ #include #include "absl/strings/escaping.h" -#include "apsi/fourq/FourQ_api.h" -#include "apsi/fourq/FourQ_internal.h" +#include "FourQ_api.h" +#include "FourQ_internal.h" #include "yacl/crypto/hash/blake3.h" #include "yacl/crypto/hash/hash_utils.h" From 6f5a1feb32132e0b1aeeeabc311e0bef0d95fb51 Mon Sep 17 00:00:00 2001 From: anakinxc Date: Wed, 31 Jul 2024 21:50:41 +0800 Subject: [PATCH 3/4] Update yacl --- bazel/repositories.bzl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bazel/repositories.bzl b/bazel/repositories.bzl index dc22eb3..f157a78 100644 --- a/bazel/repositories.bzl +++ b/bazel/repositories.bzl @@ -54,10 +54,10 @@ def _yacl(): http_archive, name = "yacl", urls = [ - "https://github.com/secretflow/yacl/archive/refs/tags/0.4.5b4_fourq_test.tar.gz", + "https://github.com/secretflow/yacl/archive/refs/tags/0.4.5b4_nightly_20240731.tar.gz", ], - strip_prefix = "yacl-0.4.5b4_fourq_test", - sha256 = "5e490cba30bcb03e751937795a14fb5c9fd0a24d61095c8ec937a86ce7f96ec9", + strip_prefix = "yacl-0.4.5b4_nightly_20240731", + sha256 = "952715bd56f6d9386984e9963426a1399bd2bd3702cf3efede9c82591cfab99b", ) def _bazel_platform(): From 89c44cf2a3555c1c3ee599dded083009d7a662e9 Mon Sep 17 00:00:00 2001 From: anakinxc Date: Wed, 31 Jul 2024 22:00:25 +0800 Subject: [PATCH 4/4] format --- psi/ecdh/basic_ecdh_oprf.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/psi/ecdh/basic_ecdh_oprf.cc b/psi/ecdh/basic_ecdh_oprf.cc index 3ac22a0..26c9da1 100644 --- a/psi/ecdh/basic_ecdh_oprf.cc +++ b/psi/ecdh/basic_ecdh_oprf.cc @@ -17,9 +17,9 @@ #include #include -#include "absl/strings/escaping.h" #include "FourQ_api.h" #include "FourQ_internal.h" +#include "absl/strings/escaping.h" #include "yacl/crypto/hash/blake3.h" #include "yacl/crypto/hash/hash_utils.h"