|  | 
|  | 1 | +cmake_minimum_required(VERSION 3.12) | 
|  | 2 | + | 
|  | 3 | +project(libsecp256k1 VERSION 0.1 LANGUAGES C) | 
|  | 4 | +set(CMAKE_C_STANDARD 90) | 
|  | 5 | +set(CMAKE_C_STANDARD_REQUIRED ON) | 
|  | 6 | + | 
|  | 7 | +if (CMAKE_C_FLAGS STREQUAL "") | 
|  | 8 | +    set(CMAKE_C_FLAGS "-g") | 
|  | 9 | +endif() | 
|  | 10 | + | 
|  | 11 | +if (CMAKE_CROSSCOMPILING) | 
|  | 12 | +    message(FATAL_ERROR "Currently Cmake makefile doesn't support cross compiling.") | 
|  | 13 | +endif() | 
|  | 14 | + | 
|  | 15 | +if (APPLE) | 
|  | 16 | +    find_program ("brew" brew) | 
|  | 17 | +    if (NOT brew STREQUAL "") | 
|  | 18 | +        # These Homebrew packages may be keg-only, meaning that they won't be found | 
|  | 19 | +        # in expected paths because they may conflict with system files. Ask | 
|  | 20 | +        # Homebrew where each one is located, then adjust paths accordingly. | 
|  | 21 | +        execute_process(COMMAND "${brew} --prefix openssl 2>/dev/null" OUTPUT_VARIABLE openssl_prefix) | 
|  | 22 | +        execute_process(COMMAND "${brew} --prefix gmp 2>/dev/null" OUTPUT_VARIABLE gmp_prefix) | 
|  | 23 | +        if (NOT openssl_prefix STREQUAL "") | 
|  | 24 | +            set(ENV{PKG_CONFIG_PATH} "${openssl_prefix}/lib/pkgconfig:$PKG_CONFIG_PATH") | 
|  | 25 | +        endif() | 
|  | 26 | +        if (NOT gmp_prefix STREQUAL "") | 
|  | 27 | +            set(ENV{PKG_CONFIG_PATH} "${gmp_prefix}/lib/pkgconfig:$PKG_CONFIG_PATH") | 
|  | 28 | +            set(GMP_C_FLAGS "-I${$gmp_prefix}/include") | 
|  | 29 | +            set(GMP_LIBS "-L${$gmp_prefix}/lib") | 
|  | 30 | +        endif() | 
|  | 31 | +    else() | 
|  | 32 | +        find_program ("port" port) | 
|  | 33 | +        # if homebrew isn't installed and macports is, add the macports default paths | 
|  | 34 | +        # as a last resort. | 
|  | 35 | +        if (NOT port STREQUAL "") | 
|  | 36 | +            set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -isystem /opt/local/include") | 
|  | 37 | +            set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -L/opt/local/lib") | 
|  | 38 | +        endif() | 
|  | 39 | +    endif() | 
|  | 40 | +endif() | 
|  | 41 | + | 
|  | 42 | +include(CheckCCompilerFlag) | 
|  | 43 | + | 
|  | 44 | +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -W") | 
|  | 45 | +set(WARN_C_FLAGS "-std=c89 -pedantic -Wall -Wextra -Wcast-align -Wnested-externs -Wshadow -Wstrict-prototypes -Wno-unused-function -Wno-long-long -Wno-overlength-strings") | 
|  | 46 | +set(SAVED_C_FLAGS "${CMAKE_C_FLAGS}") | 
|  | 47 | +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${WARN_C_FLAGS}") | 
|  | 48 | +check_c_compiler_flag(CMAKE_C_FLAGS USE_WARN_CFLAGS) | 
|  | 49 | +if (NOT USE_WARN_CFLAGS) | 
|  | 50 | +    set(CMAKE_C_FLAGS "${SAVED_C_FLAGS}") | 
|  | 51 | +endif() | 
|  | 52 | + | 
|  | 53 | +set(SAVED_C_FLAGS "${CMAKE_C_FLAGS}") | 
|  | 54 | +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden") | 
|  | 55 | +check_c_compiler_flag(CMAKE_C_FLAGS USE_OPAQUE_CFLAGS) | 
|  | 56 | +if (NOT USE_OPAQUE_CFLAGS) | 
|  | 57 | +    set(CMAKE_C_FLAGS "${SAVED_C_FLAGS}") | 
|  | 58 | +endif() | 
|  | 59 | + | 
|  | 60 | +option(BENCHMARK "compile benchmark (default is on)" ON) | 
|  | 61 | +option(COVERAGE "enable compiler flags to support kcov coverage analysis" OFF) | 
|  | 62 | +option(TESTS "compile tests (default is on)" ON) | 
|  | 63 | +option(OPENSSL_TESTS "enable OpenSSL tests, if OpenSSL is available (default is auto)" OFF) | 
|  | 64 | +option(EXPERIMENTAL "allow experimental configure options (default is off)" OFF) | 
|  | 65 | +option(EXHAUSTIVE_TESTS "compile exhaustive tests (default is on)" ON) | 
|  | 66 | +option(ENDOMORPHISM "enable endomorphism (default is off)" OFF) | 
|  | 67 | +option(ECMULT_STATIC_PRECOMPUTATION "enable precomputed ecmult table for signing (default is on)" ON) | 
|  | 68 | +option(MODULE_ECDH "enable ECDH shared secret computation (experimental)" OFF) | 
|  | 69 | +option(MODULE_RECOVERY "enable ECDSA pubkey recovery module (default is off)" OFF) | 
|  | 70 | +option(JNI "enable libsecp256k1_jni (default is off)" OFF) | 
|  | 71 | + | 
|  | 72 | +option(FIELD_64BIT "enable 64 bit field implementation (default is auto)" OFF) | 
|  | 73 | +option(FIELD_32BIT "enable 32 bit field implementation (default is auto)" OFF) | 
|  | 74 | + | 
|  | 75 | +option(BIGNUM_GMP "enable GMP bignum implementation (default is auto)" OFF) | 
|  | 76 | +option(BIGNUM_NO "don't use any bignum implementation (default is auto)" OFF) | 
|  | 77 | + | 
|  | 78 | +option(SCALAR_64BIT "enable 64 bit scalar implementation (default is auto)" OFF) | 
|  | 79 | +option(SCALAR_32BIT "enable 32 bit scalar implementation (default is auto)" OFF) | 
|  | 80 | + | 
|  | 81 | +option(ASM_x86_64 "enable x86_64 assembly optimization (default is auto)" OFF) | 
|  | 82 | +option(ASM_ARM "enable ARM assembly optimization (default is auto)" OFF) | 
|  | 83 | +option(ASM_NO "don't use any assembly optimization (default is auto)" OFF) | 
|  | 84 | + | 
|  | 85 | +include(CheckTypeSize) | 
|  | 86 | +check_type_size("__int128" INT128) | 
|  | 87 | +if (NOT INT128 STREQUAL "") | 
|  | 88 | +    set(INT128 ON) | 
|  | 89 | +else() | 
|  | 90 | +    set(INT128 OFF) | 
|  | 91 | +endif() | 
|  | 92 | + | 
|  | 93 | +include(CheckCSourceCompiles) | 
|  | 94 | +check_c_source_compiles("int main() {__builtin_expect(0,0);return 0;}" BUILTIN_EXPECT) | 
|  | 95 | + | 
|  | 96 | +if (COVERAGE) | 
|  | 97 | +    # Define this symbol to compile out all VERIFY code | 
|  | 98 | +    add_compile_definitions(COVERAGE=1) | 
|  | 99 | +    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 --coverage") | 
|  | 100 | +    set(CMAKE_MODULE_LINKER_FLAGS "${LDFLAGS} --coverage") | 
|  | 101 | +else() | 
|  | 102 | +    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3") | 
|  | 103 | +endif() | 
|  | 104 | + | 
|  | 105 | +# TODO: cross-compiler compatiblity | 
|  | 106 | +if (ECMULT_STATIC_PRECOMPUTATION) | 
|  | 107 | +    set(PRECOMP ON) | 
|  | 108 | +else() | 
|  | 109 | +    set(PRECOMP OFF) | 
|  | 110 | +endif() | 
|  | 111 | + | 
|  | 112 | +if (NOT ASM_NO) | 
|  | 113 | +    check_c_source_compiles("int main() {__builtin_expect(0,0);return 0;}" BUILTIN_EXPECT) | 
|  | 114 | +    if (NOT ASM_x86_64) | 
|  | 115 | +        if (NOT ASM_ARM) | 
|  | 116 | +            # TODO: this doesn't work | 
|  | 117 | +            check_c_source_compiles("\ | 
|  | 118 | +            #include <stdint.h> \n\ | 
|  | 119 | +            uint64_t a = 11, tmp; \n\ | 
|  | 120 | +            int main(void) {__asm__ __volatile__(\"movq \\@S|@0x100000000,%1; mulq %%rsi\" : \"+a\"(a) : \"S\"(tmp) : \"cc\", \"%rdx\");return 0;}" 64BIT_ASM_CHECK) | 
|  | 121 | +            if (64BIT_ASM_CHECK) | 
|  | 122 | +                set(ASM_x86_64 ON) | 
|  | 123 | +            else() | 
|  | 124 | +                set(ASM_NO ON) | 
|  | 125 | +            endif() | 
|  | 126 | +        endif() | 
|  | 127 | +    else() | 
|  | 128 | +        check_c_source_compiles("\ | 
|  | 129 | +        #include <stdint.h> \n\ | 
|  | 130 | +        uint64_t a = 11, tmp; \n\ | 
|  | 131 | +        int main(void) {__asm__ __volatile__(\"movq \\@S|@0x100000000,%1; mulq %%rsi\" : \"+a\"(a) : \"S\"(tmp) : \"cc\", \"%rdx\");return 0;}" 64BIT_ASM_CHECK) | 
|  | 132 | +        if (NOT 64BIT_ASM_CHECK) | 
|  | 133 | +            message(FATAL_ERROR "x86_64 assembly optimization requested but not available.") | 
|  | 134 | +        endif() | 
|  | 135 | +    endif() | 
|  | 136 | +endif() | 
|  | 137 | + | 
|  | 138 | +if (NOT FIELD_64BIT AND NOT FIELD_32BIT) | 
|  | 139 | +    if (ASM_x86_64) | 
|  | 140 | +        set(FIELD_64BIT ON) | 
|  | 141 | +    else() | 
|  | 142 | +        if (INT128) | 
|  | 143 | +            set(FIELD_64BIT ON) | 
|  | 144 | +        else() | 
|  | 145 | +            set(FIELD_32BIT ON) | 
|  | 146 | +        endif() | 
|  | 147 | +    endif() | 
|  | 148 | +else () | 
|  | 149 | +    if (FIELD_64BIT) | 
|  | 150 | +        if (NOT ASM_x86_64) | 
|  | 151 | +            if (NOT INT128) | 
|  | 152 | +                message(FATAL_ERROR "64bit field explicitly requested but neither __int128 support or x86_64 assembly available.") | 
|  | 153 | +            endif() | 
|  | 154 | +        endif() | 
|  | 155 | +    else() | 
|  | 156 | +        set(FIELD_32BIT ON) | 
|  | 157 | +    endif() | 
|  | 158 | +endif() | 
|  | 159 | + | 
|  | 160 | +if (NOT SCALAR_64BIT AND NOT SCALAR_32BIT) | 
|  | 161 | +    if (INT128) | 
|  | 162 | +        set(SCALAR_64BIT ON) | 
|  | 163 | +    else() | 
|  | 164 | +        set(SCALAR_32BIT ON) | 
|  | 165 | +    endif() | 
|  | 166 | +else() | 
|  | 167 | +    if (SCALAR_64BIT) | 
|  | 168 | +        if (NOT INT128) | 
|  | 169 | +            message(FATAL_ERROR "64bit scalar explicitly requested but __int128 support not available.") | 
|  | 170 | +        endif() | 
|  | 171 | +    else() | 
|  | 172 | +        set(SCALAR_32BIT ON) | 
|  | 173 | +    endif() | 
|  | 174 | +endif() | 
|  | 175 | + | 
|  | 176 | +set(CPPFLAGS_TEMP "${CMAKE_CXX_FLAGS}") | 
|  | 177 | +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${GMP_C_FLAGS}") | 
|  | 178 | +set(LIBS "${CMAKE_MODULE_LINKER_FLAGS}") | 
|  | 179 | +set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${GMP_LIBS}") | 
|  | 180 | +# No need to append CMAKE_C_FLAGS to CMAKE_REQUIRED_FLAGS | 
|  | 181 | +set(CMAKE_REQUIRED_FLAGS "${GMP_C_FLAGS} ${GMP_LIBS}") | 
|  | 182 | + | 
|  | 183 | + | 
|  | 184 | +if (NOT BIGNUM_NO AND NOT BIGNUM_GMP) | 
|  | 185 | +    check_c_source_compiles("\ | 
|  | 186 | +    #include <gmp.h>\ | 
|  | 187 | +    mpz_t integ; int main() {return 0;};\ | 
|  | 188 | +    " BIGNUM_GMP) | 
|  | 189 | +    if (NOT BIGNUM_GMP) | 
|  | 190 | +        set(BIGNUM_NO ON) | 
|  | 191 | +    else() | 
|  | 192 | +        set(BIGNUM_GMP ON) | 
|  | 193 | +        # Define this symbol if libgmp is installed | 
|  | 194 | +        add_compile_definitions(HAVE_LIBGMP=1) | 
|  | 195 | +    endif() | 
|  | 196 | +else() | 
|  | 197 | +    if (BIGNUM_GMP) | 
|  | 198 | +        check_c_source_compiles("\ | 
|  | 199 | +        #include <gmp.h>\ | 
|  | 200 | +        mpz_t integ; int main() {return 0;} \ | 
|  | 201 | +        " BIGNUM_GMP) | 
|  | 202 | +        if (NOT BIGNUM_GMP) | 
|  | 203 | +            message(FATAL_ERROR "gmp bignum explicitly requested but libgmp not available.") | 
|  | 204 | +        else() | 
|  | 205 | +            set(BIGNUM_GMP ON) | 
|  | 206 | +            # Define this symbol if libgmp is installed | 
|  | 207 | +            add_compile_definitions(HAVE_LIBGMP=1) | 
|  | 208 | +        endif() | 
|  | 209 | +    else() | 
|  | 210 | +        set(BIGNUM_NO ON) | 
|  | 211 | +    endif() | 
|  | 212 | +endif() | 
|  | 213 | + | 
|  | 214 | +set(CMAKE_CXX_FLAGS "${CPPFLAGS_TEMP}") | 
|  | 215 | +set(CMAKE_MODULE_LINKER_FLAGS "${LIBS}") | 
|  | 216 | +set(CMAKE_REQUIRED_FLAGS "") | 
|  | 217 | + | 
|  | 218 | +set(USE_EXTERNAL_ASM OFF) | 
|  | 219 | +if (ASM_x86_64) | 
|  | 220 | +    # Define this symbol to enable x86_64 assembly optimizations | 
|  | 221 | +    add_compile_definitions(USE_ASM_X86_64=1) | 
|  | 222 | +else() | 
|  | 223 | +    if(ASM_ARM) | 
|  | 224 | +        set(USE_EXTERNAL_ASM ON) | 
|  | 225 | +    endif() | 
|  | 226 | +    # no asm | 
|  | 227 | +endif() | 
|  | 228 | + | 
|  | 229 | +if (FIELD_64BIT) | 
|  | 230 | +    # Define this symbol to use the FIELD_5X52 implementation | 
|  | 231 | +    add_compile_definitions(USE_FIELD_5X52=1) | 
|  | 232 | +else() | 
|  | 233 | +    # Define this symbol to use the FIELD_10X26 implementation | 
|  | 234 | +    add_compile_definitions(USE_FIELD_10X26=1) | 
|  | 235 | +endif() | 
|  | 236 | + | 
|  | 237 | +if (BIGNUM_GMP) | 
|  | 238 | +    # Define this symbol if libgmp is installed | 
|  | 239 | +    add_compile_definitions(HAVE_LIBGMP=1) | 
|  | 240 | +    # Define this symbol to use the gmp implementation for num | 
|  | 241 | +    add_compile_definitions(USE_NUM_GMP=1) | 
|  | 242 | +    # Define this symbol to use the num-based field inverse implementation | 
|  | 243 | +    add_compile_definitions(USE_FIELD_INV_NUM=1) | 
|  | 244 | +    # Define this symbol to use the num-based scalar inverse implementation | 
|  | 245 | +    add_compile_definitions(USE_SCALAR_INV_NUM=1) | 
|  | 246 | +else() | 
|  | 247 | +    # Define this symbol to use no num implementation | 
|  | 248 | +    add_compile_definitions(USE_NUM_NONE=1) | 
|  | 249 | +    # Define this symbol to use the native field inverse implementation | 
|  | 250 | +    add_compile_definitions(USE_FIELD_INV_BUILTIN=1) | 
|  | 251 | +    # Define this symbol to use the native scalar inverse implementation | 
|  | 252 | +    add_compile_definitions(USE_SCALAR_INV_BUILTIN=1) | 
|  | 253 | +endif() | 
|  | 254 | + | 
|  | 255 | +if (SCALAR_64BIT) | 
|  | 256 | +    # Define this symbol to use the 4x64 scalar implementation | 
|  | 257 | +    add_compile_definitions(USE_SCALAR_4X64=1) | 
|  | 258 | +else() | 
|  | 259 | +    # Define this symbol to use the 8x32 scalar implementation | 
|  | 260 | +    add_compile_definitions(USE_SCALAR_8X32=1) | 
|  | 261 | +endif() | 
|  | 262 | + | 
|  | 263 | +if (TESTS) | 
|  | 264 | +    # TODO: here add OpenSSL tests | 
|  | 265 | +    if (MINGW) | 
|  | 266 | +    endif() | 
|  | 267 | +else() | 
|  | 268 | +    if (OPENSSL_TESTS) | 
|  | 269 | +        message(FATAL_ERROR "OpenSSL tests requested but tests are not enabled.") | 
|  | 270 | +    endif() | 
|  | 271 | +endif() | 
|  | 272 | + | 
|  | 273 | +if (JNI) | 
|  | 274 | +    # TODO: add jni | 
|  | 275 | +endif() | 
|  | 276 | + | 
|  | 277 | +if (BIGNUM_GMP) | 
|  | 278 | +    set(SECP_LIBS "${SECP_LIBS} ${GMP_LIBS}") | 
|  | 279 | +    set(SECP_INCLUDES "${SECP_INCLUDES} ${GMP_C_FLAGS}") | 
|  | 280 | +endif() | 
|  | 281 | + | 
|  | 282 | +if (ENDOMORPHISM) | 
|  | 283 | +    # Define this symbol to use endomorphism optimization | 
|  | 284 | +    add_compile_definitions(USE_ENDOMORPHISM=1) | 
|  | 285 | +endif() | 
|  | 286 | + | 
|  | 287 | +if (PRECOMP) | 
|  | 288 | +    # Define this symbol to use a statically generated ecmult table | 
|  | 289 | +    add_compile_definitions(USE_ECMULT_STATIC_PRECOMPUTATION=1) | 
|  | 290 | +endif() | 
|  | 291 | + | 
|  | 292 | +if (MODULE_ECDH) | 
|  | 293 | +    # Define this symbol to enable the ECDH module | 
|  | 294 | +    add_compile_definitions(ENABLE_MODULE_ECDH=1) | 
|  | 295 | +endif() | 
|  | 296 | + | 
|  | 297 | +if (MODULE_RECOVERY) | 
|  | 298 | +    # Define this symbol to enable the ECDSA pubkey recovery module | 
|  | 299 | +    add_compile_definitions(ENABLE_MODULE_RECOVERY=1) | 
|  | 300 | +endif() | 
|  | 301 | + | 
|  | 302 | +if (USE_EXTERNAL_ASM) | 
|  | 303 | +    # Define this symbol if an external (non-inline) assembly implementation is used | 
|  | 304 | +    add_compile_definitions(USE_EXTERNAL_ASM=1) | 
|  | 305 | +endif() | 
|  | 306 | + | 
|  | 307 | +if (INT128) | 
|  | 308 | +    add_compile_definitions(HAVE___INT128=1) | 
|  | 309 | +endif() | 
|  | 310 | + | 
|  | 311 | +message("Using static precomputation: ${PRECOMP}") | 
|  | 312 | +message("Using x86_64 ASM: ${ASM_x86_64}") | 
|  | 313 | +message("Using ARM ASM: ${ASM_ARM}") | 
|  | 314 | +message("Using external ASM: ${USE_EXTERNAL_ASM}") | 
|  | 315 | +message("Using 64 bit field implementation: ${FIELD_64BIT}") | 
|  | 316 | +message("Using GMP bignum implementation: ${BIGNUM_GMP}") | 
|  | 317 | +message("Using 64 bit scalar implementation: ${SCALAR_64BIT}") | 
|  | 318 | +message("Using endomorphism optimizations: ${ENDOMORPHISM}") | 
|  | 319 | +message("Building benchmarks: ${BENCHMARK}") | 
|  | 320 | +message("Building for coverage analysis: ${COVERAGE}") | 
|  | 321 | +message("Building ECDH module: ${MODULE_ECDH}") | 
|  | 322 | +message("Building ECDSA pubkey recovery module: ${MODULE_RECOVERY}") | 
|  | 323 | +message("Using JNI: ${JNI}") | 
|  | 324 | + | 
|  | 325 | +if (EXPERIMENTAL) | 
|  | 326 | +    message("******") | 
|  | 327 | +    message("WARNING: experimental build") | 
|  | 328 | +    message("Experimental features do not have stable APIs or properties, and may not be safe for production use.") | 
|  | 329 | +    message("Building ECDH module: ${MODULE_ECDH}.") | 
|  | 330 | +    message("******") | 
|  | 331 | +else() | 
|  | 332 | +    if (MODULE_ECDH) | 
|  | 333 | +        message(FATAL_ERROR "ECDH module is experimental. Use -DEXPERIMENTAL to allow.") | 
|  | 334 | +    endif() | 
|  | 335 | +    if (ASM_ARM) | 
|  | 336 | +        message(FATAL_ERROR "ARM assembly optimization is experimental. Use --enable-experimental to allow.") | 
|  | 337 | +    endif() | 
|  | 338 | +endif() | 
|  | 339 | + | 
|  | 340 | +add_subdirectory(src) | 
|  | 341 | + | 
|  | 342 | +# What should be here? | 
0 commit comments