From 9d01a0ce307491fc8218ad3f004d88e11164d54a Mon Sep 17 00:00:00 2001 From: tobtoht Date: Thu, 2 Jan 2025 19:43:29 +0100 Subject: [PATCH] build: make libsodium an optional build dependency --- CMakeLists.txt | 5 -- README.md | 2 +- cmake/CheckTrezor.cmake | 5 ++ src/crypto/CMakeLists.txt | 4 +- src/crypto/generic-ops.h | 2 +- src/crypto/verify.c | 69 +++++++++++++++++++++++++++ src/crypto/verify.h | 20 ++++++++ src/device_trezor/trezor/protocol.cpp | 3 +- src/ringct/rctTypes.h | 1 - tests/performance_tests/equality.h | 2 +- 10 files changed, 100 insertions(+), 13 deletions(-) create mode 100644 src/crypto/verify.c create mode 100644 src/crypto/verify.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 7f19b3b60..b6e03f902 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1193,11 +1193,6 @@ if(CMAKE_C_COMPILER_ID MATCHES "Clang" AND ARCH_WIDTH EQUAL "32" AND NOT IOS AND endif() endif() -if(STATIC) - set(sodium_USE_STATIC_LIBS ON) -endif() -find_package(Sodium REQUIRED) - find_package(PkgConfig REQUIRED) pkg_check_modules(libzmq REQUIRED IMPORTED_TARGET libzmq) diff --git a/README.md b/README.md index 0c5fefaac..a5fbbc9ac 100644 --- a/README.md +++ b/README.md @@ -171,7 +171,7 @@ library archives (`.a`). | OpenSSL | basically any | NO | `libssl-dev` | `openssl` | `openssl-devel` | `openssl-devel` | NO | sha256 sum | | libzmq | 4.2.0 | NO | `libzmq3-dev` | `zeromq` | `zeromq-devel` | `zeromq-devel` | NO | ZeroMQ library | | libunbound | 1.4.16 | NO | `libunbound-dev` | `unbound` | `unbound-devel` | `unbound-devel` | NO | DNS resolver | -| libsodium | ? | NO | `libsodium-dev` | `libsodium` | `libsodium-devel` | `libsodium-devel` | NO | cryptography | +| libsodium | ? | NO | `libsodium-dev` | `libsodium` | `libsodium-devel` | `libsodium-devel` | YES | cryptography | | libunwind | any | NO | `libunwind8-dev` | `libunwind` | `libunwind-devel` | `libunwind-devel` | YES | Stack traces | | liblzma | any | NO | `liblzma-dev` | `xz` | `liblzma-devel` | `xz-devel` | YES | For libunwind | | libreadline | 6.3.0 | NO | `libreadline6-dev` | `readline` | `readline-devel` | `readline-devel` | YES | Input editing | diff --git a/cmake/CheckTrezor.cmake b/cmake/CheckTrezor.cmake index a6b0605dc..0855a49b0 100644 --- a/cmake/CheckTrezor.cmake +++ b/cmake/CheckTrezor.cmake @@ -63,6 +63,11 @@ if (USE_DEVICE_TREZOR) trezor_fatal_msg("Trezor: protobuf library not found") endif() + if(STATIC) + set(sodium_USE_STATIC_LIBS ON) + endif() + find_package(Sodium REQUIRED) + if(TREZOR_DEBUG) set(USE_DEVICE_TREZOR_DEBUG 1) message(STATUS "Trezor: debug build enabled") diff --git a/src/crypto/CMakeLists.txt b/src/crypto/CMakeLists.txt index b0006faba..e8955b51f 100644 --- a/src/crypto/CMakeLists.txt +++ b/src/crypto/CMakeLists.txt @@ -50,7 +50,8 @@ set(crypto_sources slow-hash.c rx-slow-hash.c CryptonightR_JIT.c - tree-hash.c) + tree-hash.c + verify.c) if(ARCH_ID STREQUAL "i386" OR ARCH_ID STREQUAL "x86_64" OR ARCH_ID STREQUAL "x86-64" OR ARCH_ID STREQUAL "amd64") list(APPEND crypto_sources CryptonightR_template.S) @@ -73,7 +74,6 @@ target_link_libraries(cncrypto epee randomx ${Boost_SYSTEM_LIBRARY} - ${sodium_LIBRARIES} PRIVATE ${EXTRA_LIBRARIES}) diff --git a/src/crypto/generic-ops.h b/src/crypto/generic-ops.h index 107f4cfdc..cdb1d01c9 100644 --- a/src/crypto/generic-ops.h +++ b/src/crypto/generic-ops.h @@ -33,7 +33,7 @@ #include #include #include -#include +#include "verify.h" #define CRYPTO_MAKE_COMPARABLE(type) \ namespace crypto { \ diff --git a/src/crypto/verify.c b/src/crypto/verify.c new file mode 100644 index 000000000..8d56abaf1 --- /dev/null +++ b/src/crypto/verify.c @@ -0,0 +1,69 @@ +// SPDX-License-Identifier: ISC +// SPDX-FileCopyrightText: 2013-2024 Frank Denis + +#include + +#include "verify.h" + +#define crypto_verify_32_BYTES 32U + +#if defined(__x86_64__) && defined(__SSE2__) + +# ifdef __GNUC__ +# pragma GCC target("sse2") +# endif +# include + +static inline int +crypto_verify_n(const unsigned char *x_, const unsigned char *y_, + const int n) +{ + const __m128i zero = _mm_setzero_si128(); + volatile __m128i v1, v2, z; + volatile int m; + int i; + + const volatile __m128i *volatile x = + (const volatile __m128i *volatile) (const void *) x_; + const volatile __m128i *volatile y = + (const volatile __m128i *volatile) (const void *) y_; + v1 = _mm_loadu_si128((const __m128i *) &x[0]); + v2 = _mm_loadu_si128((const __m128i *) &y[0]); + z = _mm_xor_si128(v1, v2); + for (i = 1; i < n / 16; i++) { + v1 = _mm_loadu_si128((const __m128i *) &x[i]); + v2 = _mm_loadu_si128((const __m128i *) &y[i]); + z = _mm_or_si128(z, _mm_xor_si128(v1, v2)); + } + m = _mm_movemask_epi8(_mm_cmpeq_epi32(z, zero)); + v1 = zero; v2 = zero; z = zero; + + return (int) (((uint32_t) m + 1U) >> 16) - 1; +} + +#else + +static inline int +crypto_verify_n(const unsigned char *x_, const unsigned char *y_, + const int n) +{ + const volatile unsigned char *volatile x = + (const volatile unsigned char *volatile) x_; + const volatile unsigned char *volatile y = + (const volatile unsigned char *volatile) y_; + volatile uint_fast16_t d = 0U; + int i; + + for (i = 0; i < n; i++) { + d |= x[i] ^ y[i]; + } + return (1 & ((d - 1) >> 8)) - 1; +} + +#endif + +int +crypto_verify_32(const unsigned char *x, const unsigned char *y) +{ + return crypto_verify_n(x, y, crypto_verify_32_BYTES); +} diff --git a/src/crypto/verify.h b/src/crypto/verify.h new file mode 100644 index 000000000..0f561996c --- /dev/null +++ b/src/crypto/verify.h @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: ISC +// SPDX-FileCopyrightText: 2013-2024 Frank Denis + +#pragma once + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +// fix naming collision with libsodium +#define crypto_verify_32 monero_crypto_verify_32 + +int crypto_verify_32(const unsigned char *x, const unsigned char *y) + __attribute__ ((warn_unused_result)) __attribute__ ((nonnull)); + +#ifdef __cplusplus +} +#endif diff --git a/src/device_trezor/trezor/protocol.cpp b/src/device_trezor/trezor/protocol.cpp index 98dde7d92..fce3f4995 100644 --- a/src/device_trezor/trezor/protocol.cpp +++ b/src/device_trezor/trezor/protocol.cpp @@ -36,12 +36,11 @@ #include #include #include +#include #include #include #include #include "cryptonote_config.h" -#include -#include #include #define GET_FIELD_STRING(name, type, jtype) field_##name = std::string(json[#name].GetString(), json[#name].GetStringLength()) diff --git a/src/ringct/rctTypes.h b/src/ringct/rctTypes.h index 20b952c5e..1e8143585 100644 --- a/src/ringct/rctTypes.h +++ b/src/ringct/rctTypes.h @@ -36,7 +36,6 @@ #include #include #include -#include extern "C" { #include "crypto/crypto-ops.h" diff --git a/tests/performance_tests/equality.h b/tests/performance_tests/equality.h index 0c1378b1f..39acd64db 100644 --- a/tests/performance_tests/equality.h +++ b/tests/performance_tests/equality.h @@ -31,7 +31,7 @@ #pragma once #include -#include +#include "crypto/verify.h" struct memcmp32 {