Skip to content

Commit

Permalink
fix: cmake build and urandom entropy source
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie-Cui committed Dec 30, 2024
1 parent f52d93d commit 46a7489
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 15 deletions.
20 changes: 19 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})
enable_testing()

add_library(yacl_no_spi STATIC)
add_library(yacl_spi STATIC)

target_include_directories(yacl_no_spi PUBLIC
${CMAKE_THIRDPARTY_INCLUDEDIR}
# these dirs are only used when building inside the build tree
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>"
# these dirs are only used when linking against a prebuilt package
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")

target_include_directories(yacl_no_spi PUBLIC
${CMAKE_THIRDPARTY_INCLUDEDIR}
Expand All @@ -147,6 +155,7 @@ target_include_directories(yacl_no_spi PUBLIC
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")

set(YACL_SOURCE_FILES "")
set(YACL_SPI_SOURCE_FILES "")

add_subdirectory(yacl/base)
add_subdirectory(yacl/crypto)
Expand All @@ -158,7 +167,8 @@ add_subdirectory(yacl/utils)

target_sources(yacl_no_spi PRIVATE ${YACL_SOURCE_FILES})

target_link_libraries(yacl_no_spi PUBLIC
target_link_libraries(yacl_no_spi
PUBLIC
Thirdparty::absl
Thirdparty::blake3
Thirdparty::cpu_features
Expand All @@ -172,3 +182,11 @@ target_link_libraries(yacl_no_spi PUBLIC
Thirdparty::openssl
Thirdparty::spdlog
Thirdparty::sse2neon)

# add_custom_command(libyacl
# COMMAND
# bash ${PROJECT_SOURCE_DIR}/cmake/scripts/unify-static-libs.sh libyacl${CMAKE_STATIC_LIBRARY_SUFFIX} ${CMAKE_INSTALL_LIBDIR} libyacl_*.a
# WORKING_DIRECTORY ${CMAKE_INSTALL_LIBDIR}
# DEPENDS
# yacl_no_spi
# )
4 changes: 2 additions & 2 deletions yacl/crypto/ecc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ add_yacl_test(ecc_test)
# For libspi_ecc
# ------------------------------------------------------------------------------

add_library(spi_ecc STATIC
add_library(yacl_spi_ecc STATIC
${CMAKE_CURRENT_LIST_DIR}/curve_meta.cc
${CMAKE_CURRENT_LIST_DIR}/ec_point.cc
${CMAKE_CURRENT_LIST_DIR}/ecc_spi.cc
Expand All @@ -48,7 +48,7 @@ add_library(spi_ecc STATIC
${CMAKE_CURRENT_LIST_DIR}/openssl/openssl_factory.cc
${CMAKE_CURRENT_LIST_DIR}/openssl/openssl_group.cc)

target_include_directories(spi_ecc PUBLIC
target_include_directories(yacl_spi_ecc PUBLIC
${CMAKE_THIRDPARTY_INCLUDEDIR}
# these dirs are only used when building inside the build tree
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>")
Expand Down
4 changes: 2 additions & 2 deletions yacl/crypto/rand/drbg/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ add_yacl_test(drbg_test)
# For libspi_drbg
# ------------------------------------------------------------------------------

add_library(spi_drbg STATIC
add_library(yacl_spi_drbg STATIC
${CMAKE_CURRENT_LIST_DIR}/native_factory.cc
${CMAKE_CURRENT_LIST_DIR}/openssl_factory.cc)

target_include_directories(spi_drbg PUBLIC
target_include_directories(yacl_spi_drbg PUBLIC
${CMAKE_THIRDPARTY_INCLUDEDIR}
# these dirs are only used when building inside the build tree
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>")
Expand Down
4 changes: 2 additions & 2 deletions yacl/crypto/rand/entropy_source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ add_yacl_test(entropy_source_test)
# For libspi_drbg
# ------------------------------------------------------------------------------

add_library(spi_entropy_source STATIC
add_library(yacl_spi_entropy_source STATIC
${CMAKE_CURRENT_LIST_DIR}/urandom_factory.cc
${CMAKE_CURRENT_LIST_DIR}/rdseed_factory.cc)

target_include_directories(spi_entropy_source PUBLIC
target_include_directories(yacl_spi_entropy_source PUBLIC
${CMAKE_THIRDPARTY_INCLUDEDIR}
# these dirs are only used when building inside the build tree
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>")
Expand Down
4 changes: 3 additions & 1 deletion yacl/crypto/rand/entropy_source/urandom_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@

namespace yacl::crypto {

Buffer UrandomEntropySource::GetEntropy(uint32_t num_bytes) {
Buffer UrandomEntropySource::GetEntropy(uint32_t bits_of_entropy) {
YACL_ENFORCE(num_bytes != 0);

Buffer out(num_bytes);
std::random_device rd("/dev/urandom");

auto num_bytes = (bits_of_entropy + 7) / 8;

// Batched Random Entropy Generation
size_t batch_size = sizeof(uint32_t);
size_t batch_num = (num_bytes + batch_size - 1) / batch_size;
Expand Down
2 changes: 1 addition & 1 deletion yacl/crypto/rand/entropy_source/urandom_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class UrandomEntropySource : public EntropySource {
absl::AsciiStrToLower(type) == "auto";
}

Buffer GetEntropy(uint32_t num_bytes) override;
Buffer GetEntropy(uint32_t bits_of_entropy) override;

std::string Name() override { return "urandom entropy source"; }
};
Expand Down
6 changes: 3 additions & 3 deletions yacl/math/galois_field/factory/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ add_yacl_test(mpint_factory_test)
# For libspi_galois_field
# ------------------------------------------------------------------------------

add_library(spi_galois_field STATIC
add_library(yacl_spi_galois_field STATIC
${CMAKE_CURRENT_LIST_DIR}/gf_spi.cc
${CMAKE_CURRENT_LIST_DIR}/mcl_factory.cc
# ${CMAKE_CURRENT_LIST_DIR}/intel_factory.cc
${CMAKE_CURRENT_LIST_DIR}/mpint_factory.cc)

target_include_directories(spi_galois_field PUBLIC
target_include_directories(yacl_spi_galois_field PUBLIC
${CMAKE_THIRDPARTY_INCLUDEDIR}
# these dirs are only used when building inside the build tree
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>")

target_link_libraries(spi_galois_field PUBLIC
target_link_libraries(yacl_spi_galois_field PUBLIC
$<LINK_LIBRARY:WHOLE_ARCHIVE,spi_ecc>)

target_link_libraries(yacl PUBLIC
Expand Down
4 changes: 2 additions & 2 deletions yacl/math/mpint/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ add_yacl_test(tommath_ext_test)
# For libspi_mpint
# ------------------------------------------------------------------------------

add_library(spi_mpint STATIC
add_library(yacl_spi_mpint STATIC
${CMAKE_CURRENT_LIST_DIR}/montgomery_math.cc
${CMAKE_CURRENT_LIST_DIR}/mp_int.cc
${CMAKE_CURRENT_LIST_DIR}/tommath_ext_types.cc
${CMAKE_CURRENT_LIST_DIR}/tommath_ext_features.cc)

target_include_directories(spi_mpint PUBLIC
target_include_directories(yacl_spi_mpint PUBLIC
${GMP_INCLUDE_DIR}
${CMAKE_THIRDPARTY_INCLUDEDIR}
# these dirs are only used when building inside the build tree
Expand Down
2 changes: 1 addition & 1 deletion yacl/utils/spi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# License for the specific language governing permissions and limitations under
# the License.

add_yacl_test(spi_factory_test)
add_yacl_test(yacl_spi_factory_test)

# Add header files for installation
install(
Expand Down

0 comments on commit 46a7489

Please sign in to comment.