From 354c6ba9454834c33efdfcc3c00bc3f37d729bd4 Mon Sep 17 00:00:00 2001 From: Wei Dai Date: Tue, 15 Mar 2022 23:33:42 -0700 Subject: [PATCH] Added SEAL_AVOID_BRANCHING option. --- CMakeLists.txt | 7 +++++++ native/src/seal/util/config.h.in | 1 + native/src/seal/util/defines.h | 5 +++++ 3 files changed, 13 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5d85e8e6e..22122d8b5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -265,6 +265,13 @@ set_property(CACHE SEAL_DEFAULT_PRNG PROPERTY STRINGS "Blake2xb" "Shake256") mark_as_advanced(FORCE SEAL_DEFAULT_PRNG) +# [option] SEAL_AVOID_BRANCHING (default: OFF) +# Avoid branching in conditional move operations if set to ON, use faster method if set to OFF. +set(SEAL_AVOID_BRANCHING_STR "Use an always branching-free conditional move") +option(SEAL_AVOID_BRANCHING ${SEAL_AVOID_BRANCHING_STR} OFF) +message(STATUS "SEAL_AVOID_BRANCHING: ${SEAL_AVOID_BRANCHING}") +mark_as_advanced(FORCE SEAL_AVOID_BRANCHING) + # [option] SEAL_USE_INTRIN (default: ON) set(SEAL_USE_INTRIN_OPTION_STR "Use intrinsics") option(SEAL_USE_INTRIN ${SEAL_USE_INTRIN_OPTION_STR} ON) diff --git a/native/src/seal/util/config.h.in b/native/src/seal/util/config.h.in index a28adc2af..1b0993a5f 100644 --- a/native/src/seal/util/config.h.in +++ b/native/src/seal/util/config.h.in @@ -24,6 +24,7 @@ #cmakedefine SEAL_THROW_ON_TRANSPARENT_CIPHERTEXT #cmakedefine SEAL_USE_GAUSSIAN_NOISE #cmakedefine SEAL_DEFAULT_PRNG @SEAL_DEFAULT_PRNG@ +#cmakedefine SEAL_AVOID_BRANCHING // Intrinsics #cmakedefine SEAL_USE_INTRIN diff --git a/native/src/seal/util/defines.h b/native/src/seal/util/defines.h index 8de72bd6c..1f3770536 100644 --- a/native/src/seal/util/defines.h +++ b/native/src/seal/util/defines.h @@ -285,4 +285,9 @@ namespace seal // Conditionally select the former if true and the latter if false // This is a temporary solution that generates constant-time code with all compilers on all platforms. +#ifndef SEAL_AVOID_BRANCHING #define SEAL_COND_SELECT(cond, if_true, if_false) (cond ? if_true : if_false) +#else +#define SEAL_COND_SELECT(cond, if_true, if_false) \ + ((if_false) ^ ((~static_cast(cond) + 1) & ((if_true) ^ (if_false)))) +#endif