Skip to content

Commit

Permalink
Libsodium bringup. (#268)
Browse files Browse the repository at this point in the history
* Libsodium bringup.

* Attempt to use the most recent CMake version for this particular flow.
  • Loading branch information
AmineKhaldi committed Aug 9, 2021
1 parent ecf77cc commit e94282f
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 94 deletions.
11 changes: 5 additions & 6 deletions .github/workflows/build-aarch64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,15 @@ jobs:
python -m pip install --upgrade pip
CIBW_BEFORE_ALL_LINUX: >
yum -y install epel-release
&& yum -y install cmake3 lzip
&& rm -rf /usr/local/bin/cmake
&& ln -s /usr/bin/cmake3 /usr/local/bin/cmake
&& yum -y install lzip
&& curl -L https://github.com/Kitware/CMake/releases/download/v3.21.1/cmake-3.21.1-linux-aarch64.sh > cmake.sh
&& yes | sh cmake.sh | cat
&& rm -f /usr/bin/cmake && hash -r
&& which cmake
&& cmake --version
&& curl -L https://gmplib.org/download/gmp/gmp-6.2.1.tar.lz | lzip -dc | tar x
&& cd gmp-6.2.1 && ./configure --enable-fat
&& make && make install && cd .. && rm -rf gmp-6.2.1
&& curl -L https://download.libsodium.org/libsodium/releases/libsodium-1.0.18-stable.tar.gz | tar xz
&& cd libsodium-stable && ./configure --with-pic="yes"
&& make && make install && cd .. && rm -rf libsodium-stable
CIBW_TEST_REQUIRES: pytest
CIBW_TEST_COMMAND: pytest -v {project}/python-bindings/test.py

Expand Down
11 changes: 0 additions & 11 deletions .github/workflows/build-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,6 @@ jobs:
sudo snap install cmake --classic
hash -r
cmake --version
echo "attempting to curl"
curl -L https://download.libsodium.org/libsodium/releases/libsodium-1.0.18-stable.tar.gz | tar xz
echo "curl complete."
cd libsodium-stable
ls -l
./configure --with-pic="yes"
make
sudo make install
cd ..
echo "Setting libsodium to static compile."
export CIBUILDWHEEL=1
mkdir -p build
cd build
cmake ../
Expand Down
5 changes: 1 addition & 4 deletions .github/workflows/build-wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,10 @@ jobs:
&& echo "Building GMP"
&& cd gmp-6.2.1 && ./configure --enable-fat
&& make && make install && cd .. && rm -rf gmp-6.2.1
&& curl -L https://download.libsodium.org/libsodium/releases/libsodium-1.0.18-stable.tar.gz | tar xz
&& cd libsodium-stable && ./configure --with-pic="yes"
&& make && make install && cd .. && rm -rf libsodium-stable
CIBW_BEFORE_BUID_LINUX: >
python -m pip install --upgrade pip
CIBW_ENVIRONMENT_MACOS: "MACOSX_DEPLOYMENT_TARGET=10.14"
# GH hosted MacOS runner has gmp and libsodium natively
# GH hosted MacOS runner has gmp natively
# CIBW_ARCHS_MACOS: x86_64 arm64 universal2
CIBW_ARCHS_MACOS: x86_64
CIBW_TEST_SKIP: '*_arm64 *_universal2:arm64'
Expand Down
12 changes: 0 additions & 12 deletions .github/workflows/relic-nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,6 @@ jobs:
sudo snap install cmake --classic
hash -r
cmake --version
echo "attempting to curl"
curl -L https://download.libsodium.org/libsodium/releases/libsodium-1.0.18-stable.tar.gz | tar xz
echo "curl complete."
cd libsodium-stable
ls -l
./configure --with-pic="yes"
make
sudo make install
cd ..
echo "Setting libsodium to static compile."
export CIBUILDWHEEL=1
export RELIC_MAIN=1
mkdir -p build
cd build
Expand All @@ -61,7 +50,6 @@ jobs:
run: |
ls -l
export MACOSX_DEPLOYMENT_TARGET=10.14
export CIBUILDWHEEL=1
export RELIC_MAIN=1
mkdir -p build
ls -l build
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ contrib/relic/bin
contrib/relic/include/relic_conf.h
contrib/relic/test/CTestTestfile.cmake
contrib/gmp-6.1.2/
contrib/libsodium-1.0.16/

.idea
.vscode
Expand Down
23 changes: 11 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,18 @@ set(CMAKE_MODULE_PATH
${CMAKE_CURRENT_SOURCE_DIR}/cmake_modules
)

# Make sure to statically link libsodium when building wheels on ci
if(DEFINED ENV{CIBUILDWHEEL})
set(sodium_USE_STATIC_LIBS TRUE)
message("libsodium will be statically linked because $CIBUILDWHEEL is defined.")
endif()
include(FetchContent)

find_package(sodium)
if (SODIUM_FOUND)
message(STATUS "Found libsodium")
message(STATUS "Sodium include dir = ${sodium_INCLUDE_DIR}")
set(BLSALLOC_SODIUM "1" CACHE STRING "")
include_directories(${sodium_INCLUDE_DIR})
endif()
FetchContent_Declare(Sodium
GIT_REPOSITORY https://github.com/AmineKhaldi/libsodium-cmake.git
# Latest commit at the moment this was added here
# Anchored to libsodium v1.0.18
GIT_TAG f73a3fe1afdc4e37ac5fe0ddd401bf521f6bba65
)
set(SODIUM_PCH "on" CACHE STRING "")
set(SODIUM_DISABLE_TESTS "on" CACHE STRING "")
set(SODIUM_CHIA_MINIMAL "on" CACHE STRING "")
FetchContent_MakeAvailable(Sodium)

set(STBIN TRUE)
find_package(gmp)
Expand Down
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,12 @@ g++ -Wl,-no_pie -std=c++11 -Ibls-signatures/build/_deps/relic-src/include -Ibls

## Notes on dependencies

Libsodium and GMP are optional dependencies: libsodium gives secure memory
We use Libsodium and have GMP as an optional dependency: libsodium gives secure memory
allocation, and GMP speeds up the library by ~ 3x. MPIR is used on Windows via
GitHub Actions instead. To install them, either download them from github and
follow the instructions for each repo, or use a package manager like APT or
brew. You can follow the recipe used to build python wheels for multiple
platforms in `.github/workflows/`. libsodium is dynamically linked unless
the environment variable $CIBUILDWHEEL is set which will then cause
libsodium to statically link.
platforms in `.github/workflows/`.

## Discussion

Expand Down
7 changes: 1 addition & 6 deletions js-bindings/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ add_executable(blsjs ${CMAKE_CURRENT_SOURCE_DIR}/jsbindings.cpp
)
add_custom_target(install_npm_dependencies npm ci)
add_dependencies(blsjs install_npm_dependencies)

if (SODIUM_FOUND)
target_link_libraries(blsjs PRIVATE blstmp relic_s sodium)
else ()
target_link_libraries(blsjs PRIVATE blstmp relic_s)
endif ()
target_link_libraries(blsjs PRIVATE blstmp relic_s)

# Copy necessary files for the npm package
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/package.json package.json COPYONLY)
Expand Down
7 changes: 0 additions & 7 deletions lgtm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ extraction:
\ | tar --strip-components=1 -xz -C custom_cmake"
- "export PATH=$(pwd)/custom_cmake/bin:${PATH}"
- "cd $LGTM_SRC/"
- "mkdir custom_libsodium"
- "wget --quiet -O - \"https://download.libsodium.org/libsodium/releases/libsodium-1.0.18-stable.tar.gz\"\
\ | tar --strip-components=1 -xz -C custom_libsodium"
- "cd custom_libsodium"
- "./configure --with-pic=\"yes\" --prefix=$LGTM_SRC/"
- "make"
- "make install"
- "export CMAKE_INCLUDE_PATH=$LGTM_SRC/include:${CMAKE_INCLUDE_PATH}"
- "export CMAKE_LIBRARY_PATH=$LGTM_SRC/lib:${CMAKE_LIBRARY_PATH}"
- "mkdir $LGTM_SRC/_lgtm_build_dir"
Expand Down
9 changes: 1 addition & 8 deletions python-bindings/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,4 @@ if (GMP_FOUND)
endif(GMP_FOUND)

pybind11_add_module(blspy ${CMAKE_CURRENT_SOURCE_DIR}/pythonbindings.cpp)

if (SODIUM_FOUND)
set(BLSALLOC_SODIUM "1" CACHE STRING "")
set(SODIUM_STATIC "1" CACHE STRING "")
target_link_libraries(blspy PRIVATE blstmp relic_s sodium)
else()
target_link_libraries(blspy PRIVATE blstmp relic_s)
endif()
target_link_libraries(blspy PRIVATE blstmp relic_s)
2 changes: 1 addition & 1 deletion python-bindings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pip3 install .
```

Cmake, a c++ compiler, and a recent version of pip3 (v18) are required for source install.
GMP(speed) and libsodium(secure memory alloc) are optional dependencies.
GMP(speed) is an optional dependency.
Public keys are G1Elements, and signatures are G2Elements.

Then, to use:
Expand Down
27 changes: 5 additions & 22 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
CMAKE_MINIMUM_REQUIRED(VERSION 3.14 FATAL_ERROR)
set (CMAKE_CXX_STANDARD 17)

# CMake 3.14+
include(FetchContent)

if (DEFINED ENV{RELIC_MAIN})
set(RELIC_GIT_TAG "origin/main")
else ()
Expand Down Expand Up @@ -43,6 +40,9 @@ add_library(blstmp ${HEADERS}
${CMAKE_CURRENT_SOURCE_DIR}/schemes.cpp
)

target_compile_definitions(blstmp PRIVATE BLSALLOC_SODIUM=1)
target_link_libraries(blstmp PUBLIC sodium)

if(MSVC)
add_custom_command(
OUTPUT ${C_LIB}
Expand All @@ -64,15 +64,6 @@ else()
list(APPEND LIBRARIES_TO_COMBINE COMMAND ${CMAKE_COMMAND} -E make_directory ${OPREFIX}gmp && cd ${OPREFIX}gmp && ${CMAKE_AR} -x ${GMP_NAME})
endif()

find_library(SODIUM_NAME NAMES libsodium.a)
if(NOT ${SODIUM_NAME} STREQUAL "SODIUM_NAME-NOTFOUND")
message("SODIUM_FOUND in src/CMakeLists.txt")
list(APPEND LIBRARIES_TO_COMBINE COMMAND ${CMAKE_COMMAND} -E make_directory ${OPREFIX}sodium && cd ${OPREFIX}sodium && ${CMAKE_AR} -x ${SODIUM_NAME})
target_compile_definitions(blstmp PRIVATE BLSALLOC_SODIUM=1)
else()
target_compile_definitions(blstmp PRIVATE)
endif()

add_custom_target(combined_custom
${LIBRARIES_TO_COMBINE}
COMMAND ${CMAKE_AR} -rs ${C_LIB} ${OPREFIX}*/*${CMAKE_C_OUTPUT_EXTENSION}
Expand All @@ -99,18 +90,10 @@ install(FILES ${C_LIB} DESTINATION lib)
if(BUILD_BLS_TESTS)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../contrib/catch)
add_executable(runtest test.cpp)
if(SODIUM_FOUND)
target_link_libraries(runtest blstmp relic_s sodium)
else()
target_link_libraries(runtest blstmp relic_s)
endif()
target_link_libraries(runtest blstmp relic_s)
endif()

if(BUILD_BLS_BENCHMARKS)
add_executable(runbench test-bench.cpp)
if(SODIUM_FOUND)
target_link_libraries(runbench blstmp relic_s sodium)
else()
target_link_libraries(runbench blstmp relic_s)
endif()
target_link_libraries(runbench blstmp relic_s)
endif()

0 comments on commit e94282f

Please sign in to comment.