Skip to content

Commit 52fc59f

Browse files
committed
Libsnark get_evaluation_domain works on macOS
Before this change on macOS systems, libsnark would crash on get_root_of_unity and exception handlers would not work. The hypothesis is that using exceptions for control flow in get_evaluation_domain triggers some sort of UB (probably because this runs statically before the main function). On Linux, the UB happens to work properly, but on OSX it doesn't. As such, I rewrote all the control-flow related exception handling in get_evaluation_domain to use an error bool pointer. I'll open a PR against libsnark directly when the review for this PR finishes.
1 parent d1f3990 commit 52fc59f

24 files changed

+197
-67
lines changed

src/lib/snarky/src/camlsnark_c/libsnark-caml/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ if(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
105105
# Common compilation flags and warning configuration
106106
set(
107107
CMAKE_CXX_FLAGS
108-
"${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wextra -Wfatal-errors"
108+
"${CMAKE_CXX_FLAGS} -std=c++14 -Wall -Wextra -Wfatal-errors"
109109
)
110110
if("${MULTICORE}")
111111
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp")

src/lib/snarky/src/camlsnark_c/libsnark-caml/depends/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
add_subdirectory(gtest EXCLUDE_FROM_ALL)
22

3+
find_package(OpenSSL REQUIRED)
4+
35
if(${CURVE} STREQUAL "BN128")
46
include_directories(ate-pairing/include)
57
include_directories(xbyak)
8+
include_directories(${OPENSSL_INCLUDE_DIR})
69
add_library(
710
zm
811
STATIC
912

1013
ate-pairing/src/zm.cpp
1114
ate-pairing/src/zm2.cpp
1215
)
16+
target_link_libraries(zm ${OPENSSL_LIBRARIES})
1317
endif()
1418

1519
if("${WITH_SUPERCOP}")

src/lib/snarky/src/camlsnark_c/libsnark-caml/depends/gtest/.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ env:
3838
matrix:
3939
- GTEST_TARGET=googletest SHARED_LIB=OFF STATIC_LIB=ON CMAKE_PKG=OFF BUILD_TYPE=debug VERBOSE_MAKE=true VERBOSE
4040
- GTEST_TARGET=googlemock SHARED_LIB=OFF STATIC_LIB=ON CMAKE_PKG=OFF BUILD_TYPE=debug VERBOSE_MAKE=true VERBOSE
41-
- GTEST_TARGET=googlemock SHARED_LIB=OFF STATIC_LIB=ON CMAKE_PKG=OFF BUILD_TYPE=debug CXX_FLAGS=-std=c++11 VERBOSE_MAKE=true VERBOSE
41+
- GTEST_TARGET=googlemock SHARED_LIB=OFF STATIC_LIB=ON CMAKE_PKG=OFF BUILD_TYPE=debug CXX_FLAGS=-std=c++14 VERBOSE_MAKE=true VERBOSE
4242
# - GTEST_TARGET=googletest SHARED_LIB=ON STATIC_LIB=ON CMAKE_PKG=ON BUILD_TYPE=release VERBOSE_MAKE=false
4343
# - GTEST_TARGET=googlemock SHARED_LIB=ON STATIC_LIB=ON CMAKE_PKG=ON BUILD_TYPE=release VERBOSE_MAKE=false
4444
notifications:

src/lib/snarky/src/camlsnark_c/libsnark-caml/depends/libff/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ if(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
9393
# Common compilation flags and warning configuration
9494
set(
9595
CMAKE_CXX_FLAGS
96-
"${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wextra -Wfatal-errors"
96+
"${CMAKE_CXX_FLAGS} -std=c++14 -Wall -Wextra -Wfatal-errors"
9797
)
9898
if("${MULTICORE}")
9999
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp")

src/lib/snarky/src/camlsnark_c/libsnark-caml/depends/libff/libff/algebra/fields/field_utils.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ namespace libff {
1818
// returns root of unity of order n (for n a power of 2), if one exists
1919
template<typename FieldT>
2020
typename std::enable_if<std::is_same<FieldT, Double>::value, FieldT>::type
21-
get_root_of_unity(const size_t n);
21+
get_root_of_unity(const size_t n, bool* err);
2222

2323
template<typename FieldT>
2424
typename std::enable_if<!std::is_same<FieldT, Double>::value, FieldT>::type
25-
get_root_of_unity(const size_t n);
25+
get_root_of_unity(const size_t n, bool* err);
2626

2727
template<typename FieldT>
2828
std::vector<FieldT> pack_int_vector_into_field_element_vector(const std::vector<size_t> &v, const size_t w);

src/lib/snarky/src/camlsnark_c/libsnark-caml/depends/libff/libff/algebra/fields/field_utils.tcc

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
#include <libff/common/double.hpp>
1717
#include <libff/common/utils.hpp>
1818

19+
#include <execinfo.h>
20+
#include <stdio.h>
21+
#include <stdlib.h>
22+
#include <unistd.h>
23+
1924
namespace libff {
2025

2126
template<typename FieldT>
@@ -26,7 +31,7 @@ FieldT coset_shift()
2631

2732
template<typename FieldT>
2833
typename std::enable_if<std::is_same<FieldT, Double>::value, FieldT>::type
29-
get_root_of_unity(const size_t n)
34+
get_root_of_unity(const size_t n, bool* err)
3035
{
3136
const double PI = 3.141592653589793238460264338328L;
3237

@@ -39,11 +44,18 @@ get_root_of_unity(const size_t n)
3944

4045
template<typename FieldT>
4146
typename std::enable_if<!std::is_same<FieldT, Double>::value, FieldT>::type
42-
get_root_of_unity(const size_t n)
47+
get_root_of_unity(const size_t n, bool* err)
4348
{
4449
const size_t logn = log2(n);
45-
if (n != (1u << logn)) throw std::invalid_argument("libff::get_root_of_unity: expected n == (1u << logn)");
46-
if (logn > FieldT::s) throw std::invalid_argument("libff::get_root_of_unity: expected logn <= FieldT::s");
50+
if (n != (1u << logn)) {
51+
*err = true;
52+
return FieldT(1,1);
53+
}
54+
55+
if (logn > FieldT::s) {
56+
*err = true;
57+
return FieldT(1,1);
58+
}
4759

4860
FieldT omega = FieldT::root_of_unity;
4961
for (size_t i = FieldT::s; i > logn; --i)

src/lib/snarky/src/camlsnark_c/libsnark-caml/depends/libfqfft/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ if(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
6262
set(
6363
CMAKE_CXX_FLAGS
6464

65-
"${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wextra -Wfatal-errors -pthread"
65+
"${CMAKE_CXX_FLAGS} -std=c++14 -Wall -Wextra -Wfatal-errors -pthread"
6666
)
6767

6868
if("${MULTICORE}")

src/lib/snarky/src/camlsnark_c/libsnark-caml/depends/libfqfft/depends/gtest/.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ env:
3838
matrix:
3939
- GTEST_TARGET=googletest SHARED_LIB=OFF STATIC_LIB=ON CMAKE_PKG=OFF BUILD_TYPE=debug VERBOSE_MAKE=true VERBOSE
4040
- GTEST_TARGET=googlemock SHARED_LIB=OFF STATIC_LIB=ON CMAKE_PKG=OFF BUILD_TYPE=debug VERBOSE_MAKE=true VERBOSE
41-
- GTEST_TARGET=googlemock SHARED_LIB=OFF STATIC_LIB=ON CMAKE_PKG=OFF BUILD_TYPE=debug CXX_FLAGS=-std=c++11 VERBOSE_MAKE=true VERBOSE
41+
- GTEST_TARGET=googlemock SHARED_LIB=OFF STATIC_LIB=ON CMAKE_PKG=OFF BUILD_TYPE=debug CXX_FLAGS=-std=c++14 VERBOSE_MAKE=true VERBOSE
4242
# - GTEST_TARGET=googletest SHARED_LIB=ON STATIC_LIB=ON CMAKE_PKG=ON BUILD_TYPE=release VERBOSE_MAKE=false
4343
# - GTEST_TARGET=googlemock SHARED_LIB=ON STATIC_LIB=ON CMAKE_PKG=ON BUILD_TYPE=release VERBOSE_MAKE=false
4444
notifications:

src/lib/snarky/src/camlsnark_c/libsnark-caml/depends/libfqfft/depends/libff/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ if(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
9393
# Common compilation flags and warning configuration
9494
set(
9595
CMAKE_CXX_FLAGS
96-
"${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wextra -Wfatal-errors"
96+
"${CMAKE_CXX_FLAGS} -std=c++14 -Wall -Wextra -Wfatal-errors"
9797
)
9898
if("${MULTICORE}")
9999
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp")

src/lib/snarky/src/camlsnark_c/libsnark-caml/depends/libfqfft/libfqfft/evaluation_domain/domains/arithmetic_sequence_domain.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace libfqfft {
2828
FieldT arithmetic_generator;
2929
void do_precomputation();
3030

31-
arithmetic_sequence_domain(const size_t m);
31+
arithmetic_sequence_domain(const size_t m, bool* err);
3232

3333
void FFT(std::vector<FieldT> &a);
3434
void iFFT(std::vector<FieldT> &a);
@@ -46,4 +46,4 @@ namespace libfqfft {
4646

4747
#include <libfqfft/evaluation_domain/domains/arithmetic_sequence_domain.tcc>
4848

49-
#endif // ARITHMETIC_SEQUENCE_DOMAIN_HPP
49+
#endif // ARITHMETIC_SEQUENCE_DOMAIN_HPP

0 commit comments

Comments
 (0)