Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call Keccak_(X4_)Dispatch with pthread_once #1549

Merged
merged 2 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ else()
target_compile_definitions(common PRIVATE OQS_HAVE_GETENTROPY)
endif()
endif()
if(CMAKE_USE_PTHREADS_INIT)
target_link_libraries(common PRIVATE Threads::Threads)
endif()

# check available functions to perform aligned mallocs
check_symbol_exists(aligned_alloc stdlib.h CMAKE_HAVE_ALIGNED_ALLOC)
Expand Down
24 changes: 16 additions & 8 deletions src/common/sha3/xkcp_sha3.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

#include <oqs/common.h>

#if CMAKE_USE_PTHREADS_INIT
#include <pthread.h>
#endif
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
Expand All @@ -23,18 +26,18 @@
#define KECCAK_CTX_BYTES (KECCAK_CTX_ALIGNMENT * \
((_KECCAK_CTX_BYTES + KECCAK_CTX_ALIGNMENT - 1)/KECCAK_CTX_ALIGNMENT))

/* The first call to Keccak_Initialize will be routed through dispatch, which
* updates all of the function pointers used below.
*/
static KeccakInitFn Keccak_Dispatch;
static KeccakInitFn *Keccak_Initialize_ptr = &Keccak_Dispatch;
#if CMAKE_USE_PTHREADS_INIT
static pthread_once_t dispatch_once_control = PTHREAD_ONCE_INIT;
#endif

static KeccakInitFn *Keccak_Initialize_ptr = NULL;
static KeccakAddByteFn *Keccak_AddByte_ptr = NULL;
static KeccakAddBytesFn *Keccak_AddBytes_ptr = NULL;
static KeccakPermuteFn *Keccak_Permute_ptr = NULL;
static KeccakExtractBytesFn *Keccak_ExtractBytes_ptr = NULL;
static KeccakFastLoopAbsorbFn *Keccak_FastLoopAbsorb_ptr = NULL;

static void Keccak_Dispatch(void *state) {
static void Keccak_Dispatch(void) {
// TODO: Simplify this when we have a Windows-compatible AVX2 implementation of SHA3
#if defined(OQS_DIST_X86_64_BUILD)
#if defined(OQS_ENABLE_SHA3_xkcp_low_avx2)
Expand Down Expand Up @@ -69,8 +72,6 @@ static void Keccak_Dispatch(void *state) {
Keccak_ExtractBytes_ptr = &KeccakP1600_ExtractBytes;
Keccak_FastLoopAbsorb_ptr = &KeccakF1600_FastLoop_Absorb;
#endif

(*Keccak_Initialize_ptr)(state);
}

/*************************************************
Expand All @@ -84,6 +85,13 @@ static void Keccak_Dispatch(void *state) {
* that have not been permuted, or not-yet-squeezed bytes.
**************************************************/
static void keccak_inc_reset(uint64_t *s) {
#if CMAKE_USE_PTHREADS_INIT
pthread_once(&dispatch_once_control, Keccak_Dispatch);
#else
if (Keccak_Initialize_ptr == NULL) {
Keccak_Dispatch();
}
#endif
(*Keccak_Initialize_ptr)(s);
s[25] = 0;
}
Expand Down
25 changes: 16 additions & 9 deletions src/common/sha3/xkcp_sha3x4.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#include <oqs/common.h>
#include <oqs/oqsconfig.h>

#if CMAKE_USE_PTHREADS_INIT
#include <pthread.h>
#endif
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
Expand All @@ -18,17 +21,17 @@
#define KECCAK_X4_CTX_BYTES (KECCAK_X4_CTX_ALIGNMENT * \
((_KECCAK_X4_CTX_BYTES + KECCAK_X4_CTX_ALIGNMENT - 1)/KECCAK_X4_CTX_ALIGNMENT))

/* The first call to Keccak_Initialize will be routed through dispatch, which
* updates all of the function pointers used below.
*/
static KeccakX4InitFn Keccak_X4_Dispatch;
static KeccakX4InitFn *Keccak_X4_Initialize_ptr = &Keccak_X4_Dispatch;
#if CMAKE_USE_PTHREADS_INIT
static pthread_once_t dispatch_once_control = PTHREAD_ONCE_INIT;
#endif

static KeccakX4InitFn *Keccak_X4_Initialize_ptr = NULL;
static KeccakX4AddByteFn *Keccak_X4_AddByte_ptr = NULL;
static KeccakX4AddBytesFn *Keccak_X4_AddBytes_ptr = NULL;
static KeccakX4PermuteFn *Keccak_X4_Permute_ptr = NULL;
static KeccakX4ExtractBytesFn *Keccak_X4_ExtractBytes_ptr = NULL;

static void Keccak_X4_Dispatch(void *state) {
static void Keccak_X4_Dispatch(void) {
// TODO: Simplify this when we have a Windows-compatible AVX2 implementation of SHA3
#if defined(OQS_DIST_X86_64_BUILD)
#if defined(OQS_ENABLE_SHA3_xkcp_low_avx2)
Expand Down Expand Up @@ -59,11 +62,16 @@ static void Keccak_X4_Dispatch(void *state) {
Keccak_X4_Permute_ptr = &KeccakP1600times4_PermuteAll_24rounds;
Keccak_X4_ExtractBytes_ptr = &KeccakP1600times4_ExtractBytes;
#endif

(*Keccak_X4_Initialize_ptr)(state);
}

static void keccak_x4_inc_reset(uint64_t *s) {
#if CMAKE_USE_PTHREADS_INIT
pthread_once(&dispatch_once_control, Keccak_X4_Dispatch);
#else
if (Keccak_X4_Initialize_ptr == NULL) {
Keccak_X4_Dispatch();
}
#endif
(*Keccak_X4_Initialize_ptr)(s);
s[100] = 0;
}
Expand Down Expand Up @@ -234,4 +242,3 @@ void OQS_SHA3_shake256_x4_inc_ctx_release(OQS_SHA3_shake256_x4_inc_ctx *state) {
void OQS_SHA3_shake256_x4_inc_ctx_reset(OQS_SHA3_shake256_x4_inc_ctx *state) {
keccak_x4_inc_reset((uint64_t *)state->ctx);
}

22 changes: 10 additions & 12 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ if(NOT WIN32)
else()
set(INTERNAL_TEST_DEPS ${LIBM})
endif()
if(CMAKE_USE_PTHREADS_INIT)
set(INTERNAL_TEST_DEPS ${INTERNAL_TEST_DEPS} Threads::Threads)
endif()
if(DEFINED SANITIZER_LD_FLAGS)
set(INTERNAL_TEST_DEPS "${INTERNAL_TEST_DEPS} ${SANITIZER_LD_FLAGS}")
set(INTERNAL_TEST_DEPS ${INTERNAL_TEST_DEPS} ${SANITIZER_LD_FLAGS})
endif()
execute_process(COMMAND ${PROJECT_SOURCE_DIR}/scripts/git_commit.sh OUTPUT_VARIABLE GIT_COMMIT)
add_definitions(-DOQS_COMPILE_GIT_COMMIT="${GIT_COMMIT}")
Expand Down Expand Up @@ -59,6 +62,9 @@ else()
endif()

set(API_TEST_DEPS oqs ${LIBM})
if(CMAKE_USE_PTHREADS_INIT)
set(API_TEST_DEPS ${API_TEST_DEPS} Threads::Threads)
endif()

# KEM API tests
add_executable(example_kem example_kem.c)
Expand All @@ -68,11 +74,7 @@ add_executable(kat_kem kat_kem.c)
target_link_libraries(kat_kem PRIVATE ${API_TEST_DEPS})

add_executable(test_kem test_kem.c)
if((CMAKE_C_COMPILER_ID MATCHES "Clang") OR (CMAKE_C_COMPILER_ID STREQUAL "GNU"))
target_link_libraries(test_kem PRIVATE ${API_TEST_DEPS} Threads::Threads)
else ()
target_link_libraries(test_kem PRIVATE ${API_TEST_DEPS})
endif()
target_link_libraries(test_kem PRIVATE ${API_TEST_DEPS})

add_executable(test_kem_mem test_kem_mem.c)
target_link_libraries(test_kem_mem PRIVATE ${API_TEST_DEPS})
Expand All @@ -88,11 +90,7 @@ add_executable(kat_sig kat_sig.c)
target_link_libraries(kat_sig PRIVATE ${API_TEST_DEPS})

add_executable(test_sig test_sig.c)
if((CMAKE_C_COMPILER_ID MATCHES "Clang") OR (CMAKE_C_COMPILER_ID STREQUAL "GNU"))
target_link_libraries(test_sig PRIVATE ${API_TEST_DEPS} Threads::Threads)
else ()
target_link_libraries(test_sig PRIVATE ${API_TEST_DEPS})
endif()
target_link_libraries(test_sig PRIVATE ${API_TEST_DEPS})
baentsch marked this conversation as resolved.
Show resolved Hide resolved

add_executable(test_sig_mem test_sig_mem.c)
target_link_libraries(test_sig_mem PRIVATE ${API_TEST_DEPS})
Expand All @@ -118,7 +116,7 @@ if (CMAKE_GENERATOR MATCHES "Visual Studio")
# generating with Ninja
set_target_properties(
dump_alg_info example_kem kat_kem test_kem example_sig kat_sig test_sig test_sig_mem test_kem_mem speed_kem speed_sig
PROPERTIES
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_BINARY_DIR}/tests"
RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_BINARY_DIR}/tests"
RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO "${CMAKE_BINARY_DIR}/tests"
Expand Down
Loading