Skip to content

Commit 411aef6

Browse files
fanquakejanus
authored andcommittedJul 26, 2024
Squashed 'src/secp256k1/' changes from 06bff6dec8..4af241b320
4af241b320 Merge bitcoin-core/secp256k1#1535: build: Replace hardcoded "auto" value with default one f473c959f0 Merge bitcoin-core/secp256k1#1543: cmake: Do not modify build types when integrating by downstream project d403eea484 Merge bitcoin-core/secp256k1#1546: cmake: Rename `SECP256K1_LATE_CFLAGS` and switch to Bitcoin Core's approach d7ae25ce6f Merge bitcoin-core/secp256k1#1550: fix: typos in secp256k1.c 0e2fadb20c fix: typos in secp256k1.c 69b2192ad4 Merge bitcoin-core/secp256k1#1545: cmake: Do not set `CTEST_TEST_TARGET_ALIAS` 5dd637f3cf Merge bitcoin-core/secp256k1#1548: README: mention ellswift module 7454a53736 README: mention ellswift module 4706be2cd0 cmake: Reimplement `SECP256K1_APPEND_CFLAGS` using Bitcoin Core approach c2764dbb99 cmake: Rename `SECP256K1_LATE_CFLAGS` to `SECP256K1_APPEND_CFLAGS` f87a3589f4 cmake: Do not set `CTEST_TEST_TARGET_ALIAS` 158f9e5eae cmake: Do not modify build types when integrating by downstream project 35c0fdc86b Merge bitcoin-core/secp256k1#1529: cmake: Fix cache issue when integrating by downstream project 4392f0f717 Merge bitcoin-core/secp256k1#1533: tests: refactor: tidy up util functions (#1491) bedffd53d8 Merge bitcoin-core/secp256k1#1488: ci: Add native macOS arm64 job 4b8d5eeacf Merge bitcoin-core/secp256k1#1532: cmake: Disable eager MSan in ctime_tests f55703ba49 autotools: Delete unneeded compiler test 396e885886 autotools: Align MSan checking code with CMake's implementation abde59f52d cmake: Report more compiler details in summary 7abf979a43 cmake: Disable `ctime_tests` if build with `-fsanitize=memory` 4d9645bee0 cmake: Remove "AUTO" value of `SECP256K1_ECMULT_GEN_KB` option a06805ee74 cmake: Remove "AUTO" value of `SECP256K1_ECMULT_WINDOW_SIZE` option 1791f6fce4 Merge bitcoin-core/secp256k1#1517: autotools: Disable eager MSan in ctime_tests 26b94ee92a autotools: Remove "auto" value of `--with-ecmult-gen-kb` option 122dbaeb37 autotools: Remove "auto" value of `--with-ecmult-window` option e73f6f8fd9 tests: refactor: drop `secp256k1_` prefix from testrand.h functions 0ee7453a99 tests: refactor: add `testutil_` prefix to testutil.h functions 0c6bc76dcd tests: refactor: move `random_` helpers from tests.c to testutil.h 0fef8479be tests: refactor: rename `random_field_element_magnitude` -> `random_fe_magnitude` 59db007f0f tests: refactor: rename `random_group_element_...` -> `random_ge_...` ebfb82ee2f ci: Add job with -fsanitize-memory-param-retval e1bef0961c configure: Move "experimental" warning to bottom 55e5d975db autotools: Disable eager MSan in ctime_tests ec4c002faa cmake: Simplify `PROJECT_IS_TOP_LEVEL` emulation cae9a7ad14 cmake: Do not set emulated PROJECT_IS_TOP_LEVEL as cache variable 218f0cc93b ci: Add native macOS arm64 job git-subtree-dir: src/secp256k1 git-subtree-split: 4af241b32099067464e015fa66daac5096206dea
1 parent cafb5f6 commit 411aef6

20 files changed

+761
-641
lines changed
 

‎.cirrus.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ env:
1010
MAKEFLAGS: -j4
1111
BUILD: check
1212
### secp256k1 config
13-
ECMULTWINDOW: auto
14-
ECMULTGENKB: auto
13+
ECMULTWINDOW: 15
14+
ECMULTGENKB: 22
1515
ASM: no
1616
WIDEMUL: auto
1717
WITH_VALGRIND: yes

‎.github/workflows/ci.yml

+111-8
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,34 @@ concurrency:
1212
cancel-in-progress: true
1313

1414
env:
15-
CI_FAILFAST_TEST_LEAVE_DANGLING: 1 # GHA does not care about dangling processes and setting this variable avoids killing the CI script itself on error
16-
MAKEJOBS: '-j10'
15+
### compiler options
16+
HOST:
17+
WRAPPER_CMD:
18+
# Specific warnings can be disabled with -Wno-error=foo.
19+
# -pedantic-errors is not equivalent to -Werror=pedantic and thus not implied by -Werror according to the GCC manual.
20+
WERROR_CFLAGS: '-Werror -pedantic-errors'
21+
MAKEFLAGS: '-j4'
22+
BUILD: 'check'
23+
### secp256k1 config
24+
ECMULTWINDOW: 15
25+
ECMULTGENKB: 22
26+
ASM: 'no'
27+
WIDEMUL: 'auto'
28+
WITH_VALGRIND: 'yes'
29+
EXTRAFLAGS:
30+
### secp256k1 modules
31+
EXPERIMENTAL: 'no'
32+
ECDH: 'no'
33+
RECOVERY: 'no'
34+
SCHNORRSIG: 'no'
35+
ELLSWIFT: 'no'
36+
### test options
37+
SECP256K1_TEST_ITERS:
38+
BENCH: 'yes'
39+
SECP256K1_BENCH_ITERS: 2
40+
CTIMETESTS: 'yes'
41+
# Compile and run the examples.
42+
EXAMPLES: 'yes'
1743

1844
jobs:
1945
test-each-commit:
@@ -472,15 +498,36 @@ jobs:
472498
matrix:
473499
configuration:
474500
- env_vars:
501+
CTIMETESTS: 'yes'
475502
CFLAGS: '-fsanitize=memory -fsanitize-recover=memory -g'
476503
- env_vars:
477504
ECMULTGENKB: 2
478505
ECMULTWINDOW: 2
506+
CTIMETESTS: 'yes'
479507
CFLAGS: '-fsanitize=memory -fsanitize-recover=memory -g -O3'
508+
- env_vars:
509+
# -fsanitize-memory-param-retval is clang's default, but our build system disables it
510+
# when ctime_tests when enabled.
511+
CFLAGS: '-fsanitize=memory -fsanitize-recover=memory -fsanitize-memory-param-retval -g'
512+
CTIMETESTS: 'no'
480513

481-
- name: Ccache installation cache
482-
id: ccache-installation-cache
483-
uses: actions/cache@v4
514+
env:
515+
ECDH: 'yes'
516+
RECOVERY: 'yes'
517+
SCHNORRSIG: 'yes'
518+
ELLSWIFT: 'yes'
519+
CC: 'clang'
520+
SECP256K1_TEST_ITERS: 32
521+
ASM: 'no'
522+
WITH_VALGRIND: 'no'
523+
524+
steps:
525+
- name: Checkout
526+
uses: actions/checkout@v4
527+
528+
- name: CI script
529+
env: ${{ matrix.configuration.env_vars }}
530+
uses: ./.github/actions/run-in-docker-action
484531
with:
485532
dockerfile: ./ci/linux-debian.Dockerfile
486533
tag: linux-debian-image
@@ -533,10 +580,10 @@ jobs:
533580
run: env
534581
if: ${{ always() }}
535582

536-
macos-native:
537-
name: "x86_64: macOS Monterey"
583+
x86_64-macos-native:
584+
name: "x86_64: macOS Monterey, Valgrind"
538585
# See: https://github.com/actions/runner-images#available-images.
539-
runs-on: macos-12 # Use M1 once available https://github.com/github/roadmap/issues/528
586+
runs-on: macos-12
540587

541588
env:
542589
CC: 'clang'
@@ -603,6 +650,62 @@ jobs:
603650
run: env
604651
if: ${{ always() }}
605652

653+
arm64-macos-native:
654+
name: "ARM64: macOS Sonoma"
655+
# See: https://github.com/actions/runner-images#available-images.
656+
runs-on: macos-14
657+
658+
env:
659+
CC: 'clang'
660+
HOMEBREW_NO_AUTO_UPDATE: 1
661+
HOMEBREW_NO_INSTALL_CLEANUP: 1
662+
WITH_VALGRIND: 'no'
663+
CTIMETESTS: 'no'
664+
665+
strategy:
666+
fail-fast: false
667+
matrix:
668+
env_vars:
669+
- { WIDEMUL: 'int64', RECOVERY: 'yes', ECDH: 'yes', SCHNORRSIG: 'yes', ELLSWIFT: 'yes' }
670+
- { WIDEMUL: 'int128_struct', ECMULTGENPRECISION: 2, ECMULTWINDOW: 4 }
671+
- { WIDEMUL: 'int128', ECDH: 'yes', SCHNORRSIG: 'yes', ELLSWIFT: 'yes' }
672+
- { WIDEMUL: 'int128', RECOVERY: 'yes' }
673+
- { WIDEMUL: 'int128', RECOVERY: 'yes', ECDH: 'yes', SCHNORRSIG: 'yes', ELLSWIFT: 'yes' }
674+
- { WIDEMUL: 'int128', RECOVERY: 'yes', ECDH: 'yes', SCHNORRSIG: 'yes', ELLSWIFT: 'yes', CC: 'gcc' }
675+
- { WIDEMUL: 'int128', RECOVERY: 'yes', ECDH: 'yes', SCHNORRSIG: 'yes', ELLSWIFT: 'yes', CPPFLAGS: '-DVERIFY' }
676+
- BUILD: 'distcheck'
677+
678+
steps:
679+
- name: Checkout
680+
uses: actions/checkout@v4
681+
682+
- name: Install Homebrew packages
683+
run: |
684+
brew install automake libtool gcc
685+
ln -s $(brew --prefix gcc)/bin/gcc-?? /usr/local/bin/gcc
686+
687+
- name: CI script
688+
env: ${{ matrix.env_vars }}
689+
run: ./ci/ci.sh
690+
691+
- run: cat tests.log || true
692+
if: ${{ always() }}
693+
- run: cat noverify_tests.log || true
694+
if: ${{ always() }}
695+
- run: cat exhaustive_tests.log || true
696+
if: ${{ always() }}
697+
- run: cat ctime_tests.log || true
698+
if: ${{ always() }}
699+
- run: cat bench.log || true
700+
if: ${{ always() }}
701+
- run: cat config.log || true
702+
if: ${{ always() }}
703+
- run: cat test_env.log || true
704+
if: ${{ always() }}
705+
- name: CI env
706+
run: env
707+
if: ${{ always() }}
708+
606709
win64-native:
607710
name: ${{ matrix.configuration.job_name }}
608711
# See: https://github.com/actions/runner-images#available-images.

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Features:
1919
* Optional module for public key recovery.
2020
* Optional module for ECDH key exchange.
2121
* Optional module for Schnorr signatures according to [BIP-340](https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki).
22+
* Optional module for ElligatorSwift key exchange according to [BIP-324](https://github.com/bitcoin/bips/blob/master/bip-0324.mediawiki).
2223

2324
<b>Bitgesell is a fork of Bitcoin with the following changes:</b> <br>
2425
* Block Reward [Burn rate is 90% of tx fees]

‎configure.ac

+158-172
Original file line numberDiff line numberDiff line change
@@ -813,69 +813,98 @@ fi
813813
dnl Check for endianness
814814
AC_C_BIGENDIAN
815815

816-
dnl Check for pthread compile/link requirements
817-
AX_PTHREAD
818-
819-
dnl Check if -latomic is required for <std::atomic>
820-
CHECK_ATOMIC
821-
822-
dnl The following macro will add the necessary defines to bitcoin-config.h, but
823-
dnl they also need to be passed down to any subprojects. Pull the results out of
824-
dnl the cache and add them to CPPFLAGS.
825-
AC_SYS_LARGEFILE
826-
dnl detect POSIX or GNU variant of strerror_r
827-
AC_FUNC_STRERROR_R
828-
829-
if test "$ac_cv_sys_file_offset_bits" != "" &&
830-
test "$ac_cv_sys_file_offset_bits" != "no" &&
831-
test "$ac_cv_sys_file_offset_bits" != "unknown"; then
832-
CORE_CPPFLAGS="$CORE_CPPFLAGS -D_FILE_OFFSET_BITS=$ac_cv_sys_file_offset_bits"
833-
fi
834-
835-
if test "$ac_cv_sys_large_files" != "" &&
836-
test "$ac_cv_sys_large_files" != "no" &&
837-
test "$ac_cv_sys_large_files" != "unknown"; then
838-
CORE_CPPFLAGS="$CORE_CPPFLAGS -D_LARGE_FILES=$ac_cv_sys_large_files"
839-
fi
840-
841-
if test "$TARGET_OS" != "windows"; then
842-
dnl All windows code is PIC, forcing it on just adds useless compile warnings
843-
AX_CHECK_COMPILE_FLAG([-fPIC], [PIC_FLAGS="-fPIC"])
844-
fi
845-
846-
if test "$use_hardening" != "no"; then
847-
AX_CHECK_COMPILE_FLAG([-Wstack-protector], [HARDENED_CXXFLAGS="$HARDENED_CXXFLAGS -Wstack-protector"])
848-
AX_CHECK_COMPILE_FLAG([-fstack-protector-all], [HARDENED_CXXFLAGS="$HARDENED_CXXFLAGS -fstack-protector-all"])
849-
850-
AX_CHECK_COMPILE_FLAG([-fcf-protection=full], [HARDENED_CXXFLAGS="$HARDENED_CXXFLAGS -fcf-protection=full"])
851-
852-
case $host in
853-
*mingw*)
854-
dnl stack-clash-protection doesn't compile with GCC 10 and earlier.
855-
dnl In any case, it is a no-op for Windows.
856-
dnl See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90458 for more details.
857-
;;
858-
*)
859-
AX_CHECK_COMPILE_FLAG([-fstack-clash-protection], [HARDENED_CXXFLAGS="$HARDENED_CXXFLAGS -fstack-clash-protection"], [], [$CXXFLAG_WERROR])
860-
;;
861-
esac
862-
863-
case $host in
864-
*aarch64*)
865-
AX_CHECK_COMPILE_FLAG([-mbranch-protection=bti], [HARDENED_CXXFLAGS="$HARDENED_CXXFLAGS -mbranch-protection=bti"])
866-
;;
867-
esac
868-
869-
dnl When enable_debug is yes, all optimizations are disabled.
870-
dnl However, FORTIFY_SOURCE requires that there is some level of optimization, otherwise it does nothing and just creates a compiler warning.
871-
dnl Since FORTIFY_SOURCE is a no-op without optimizations, do not enable it when enable_debug is yes.
872-
if test "$enable_debug" != "yes"; then
873-
AX_CHECK_PREPROC_FLAG([-D_FORTIFY_SOURCE=3],[
874-
AX_CHECK_PREPROC_FLAG([-U_FORTIFY_SOURCE],[
875-
HARDENED_CPPFLAGS="$HARDENED_CPPFLAGS -U_FORTIFY_SOURCE"
876-
])
877-
HARDENED_CPPFLAGS="$HARDENED_CPPFLAGS -D_FORTIFY_SOURCE=3"
878-
])
816+
AC_ARG_ENABLE(tests,
817+
AS_HELP_STRING([--enable-tests],[compile tests [default=yes]]), [],
818+
[SECP_SET_DEFAULT([enable_tests], [yes], [yes])])
819+
820+
AC_ARG_ENABLE(ctime_tests,
821+
AS_HELP_STRING([--enable-ctime-tests],[compile constant-time tests [default=yes if valgrind enabled]]), [],
822+
[SECP_SET_DEFAULT([enable_ctime_tests], [auto], [auto])])
823+
824+
AC_ARG_ENABLE(experimental,
825+
AS_HELP_STRING([--enable-experimental],[allow experimental configure options [default=no]]), [],
826+
[SECP_SET_DEFAULT([enable_experimental], [no], [yes])])
827+
828+
AC_ARG_ENABLE(exhaustive_tests,
829+
AS_HELP_STRING([--enable-exhaustive-tests],[compile exhaustive tests [default=yes]]), [],
830+
[SECP_SET_DEFAULT([enable_exhaustive_tests], [yes], [yes])])
831+
832+
AC_ARG_ENABLE(examples,
833+
AS_HELP_STRING([--enable-examples],[compile the examples [default=no]]), [],
834+
[SECP_SET_DEFAULT([enable_examples], [no], [yes])])
835+
836+
AC_ARG_ENABLE(module_ecdh,
837+
AS_HELP_STRING([--enable-module-ecdh],[enable ECDH module [default=yes]]), [],
838+
[SECP_SET_DEFAULT([enable_module_ecdh], [yes], [yes])])
839+
840+
AC_ARG_ENABLE(module_recovery,
841+
AS_HELP_STRING([--enable-module-recovery],[enable ECDSA pubkey recovery module [default=no]]), [],
842+
[SECP_SET_DEFAULT([enable_module_recovery], [no], [yes])])
843+
844+
AC_ARG_ENABLE(module_extrakeys,
845+
AS_HELP_STRING([--enable-module-extrakeys],[enable extrakeys module [default=yes]]), [],
846+
[SECP_SET_DEFAULT([enable_module_extrakeys], [yes], [yes])])
847+
848+
AC_ARG_ENABLE(module_schnorrsig,
849+
AS_HELP_STRING([--enable-module-schnorrsig],[enable schnorrsig module [default=yes]]), [],
850+
[SECP_SET_DEFAULT([enable_module_schnorrsig], [yes], [yes])])
851+
852+
AC_ARG_ENABLE(module_ellswift,
853+
AS_HELP_STRING([--enable-module-ellswift],[enable ElligatorSwift module [default=yes]]), [],
854+
[SECP_SET_DEFAULT([enable_module_ellswift], [yes], [yes])])
855+
856+
AC_ARG_ENABLE(external_default_callbacks,
857+
AS_HELP_STRING([--enable-external-default-callbacks],[enable external default callback functions [default=no]]), [],
858+
[SECP_SET_DEFAULT([enable_external_default_callbacks], [no], [no])])
859+
860+
# Test-only override of the (autodetected by the C code) "widemul" setting.
861+
# Legal values are:
862+
# * int64 (for [u]int64_t),
863+
# * int128 (for [unsigned] __int128),
864+
# * int128_struct (for int128 implemented as a structure),
865+
# * and auto (the default).
866+
AC_ARG_WITH([test-override-wide-multiply], [] ,[set_widemul=$withval], [set_widemul=auto])
867+
868+
AC_ARG_WITH([asm], [AS_HELP_STRING([--with-asm=x86_64|arm32|no|auto],
869+
[assembly to use (experimental: arm32) [default=auto]])],[req_asm=$withval], [req_asm=auto])
870+
871+
AC_ARG_WITH([ecmult-window], [AS_HELP_STRING([--with-ecmult-window=SIZE],
872+
[window size for ecmult precomputation for verification, specified as integer in range [2..24].]
873+
[Larger values result in possibly better performance at the cost of an exponentially larger precomputed table.]
874+
[The table will store 2^(SIZE-1) * 64 bytes of data but can be larger in memory due to platform-specific padding and alignment.]
875+
[A window size larger than 15 will require you delete the prebuilt precomputed_ecmult.c file so that it can be rebuilt.]
876+
[For very large window sizes, use "make -j 1" to reduce memory use during compilation.]
877+
[The default value is a reasonable setting for desktop machines (currently 15). [default=15]]
878+
)],
879+
[set_ecmult_window=$withval], [set_ecmult_window=15])
880+
881+
AC_ARG_WITH([ecmult-gen-kb], [AS_HELP_STRING([--with-ecmult-gen-kb=2|22|86],
882+
[The size of the precomputed table for signing in multiples of 1024 bytes (on typical platforms).]
883+
[Larger values result in possibly better signing/keygeneration performance at the cost of a larger table.]
884+
[The default value is a reasonable setting for desktop machines (currently 22). [default=22]]
885+
)],
886+
[set_ecmult_gen_kb=$withval], [set_ecmult_gen_kb=22])
887+
888+
AC_ARG_WITH([valgrind], [AS_HELP_STRING([--with-valgrind=yes|no|auto],
889+
[Build with extra checks for running inside Valgrind [default=auto]]
890+
)],
891+
[req_valgrind=$withval], [req_valgrind=auto])
892+
893+
###
894+
### Handle config options (except for modules)
895+
###
896+
897+
if test x"$req_valgrind" = x"no"; then
898+
enable_valgrind=no
899+
else
900+
SECP_VALGRIND_CHECK
901+
if test x"$has_valgrind" != x"yes"; then
902+
if test x"$req_valgrind" = x"yes"; then
903+
AC_MSG_ERROR([Valgrind support explicitly requested but valgrind/memcheck.h header not available])
904+
fi
905+
enable_valgrind=no
906+
else
907+
enable_valgrind=yes
879908
fi
880909

881910
AX_CHECK_LINK_FLAG([-Wl,--enable-reloc-section], [HARDENED_LDFLAGS="$HARDENED_LDFLAGS -Wl,--enable-reloc-section"], [], [$LDFLAG_WERROR])
@@ -897,64 +926,28 @@ if test "$TARGET_OS" = "darwin"; then
897926
AX_CHECK_LINK_FLAG([-Wl,-fixup_chains], [HARDENED_LDFLAGS="$HARDENED_LDFLAGS -Wl,-fixup_chains"], [], [$LDFLAG_WERROR])
898927
fi
899928

900-
AC_CHECK_HEADERS([sys/select.h sys/prctl.h vm/vm_param.h sys/vmmeter.h sys/resources.h])
901-
902-
AC_CHECK_DECLS([getifaddrs, freeifaddrs],[CHECK_SOCKET],,
903-
[#include <sys/types.h>
904-
#include <ifaddrs.h>]
905-
)
906-
907-
dnl These are used for daemonization in BGLd
908-
AC_CHECK_DECLS([fork])
909-
AC_CHECK_DECLS([setsid])
910-
911-
AC_CHECK_DECLS([pipe2])
912-
913-
dnl Check for malloc_info (for memory statistics information in getmemoryinfo)
914-
AC_MSG_CHECKING([for getmemoryinfo])
915-
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <malloc.h>]],
916-
[[ int f = malloc_info(0, NULL); ]])],
917-
[ AC_MSG_RESULT([yes]); AC_DEFINE([HAVE_MALLOC_INFO], [1], [Define this symbol if you have malloc_info]) ],
918-
[ AC_MSG_RESULT([no])]
919-
)
920-
921-
dnl Check for mallopt(M_ARENA_MAX) (to set glibc arenas)
922-
AC_MSG_CHECKING([for mallopt M_ARENA_MAX])
923-
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <malloc.h>]],
924-
[[ mallopt(M_ARENA_MAX, 1); ]])],
925-
[ AC_MSG_RESULT([yes]); AC_DEFINE([HAVE_MALLOPT_ARENA_MAX], [1], [Define this symbol if you have mallopt with M_ARENA_MAX]) ],
926-
[ AC_MSG_RESULT([no])]
927-
)
928-
929-
dnl Check for posix_fallocate
930-
AC_MSG_CHECKING([for posix_fallocate])
931-
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
932-
// same as in src/util/fs_helpers.cpp
933-
#ifdef __linux__
934-
#ifdef _POSIX_C_SOURCE
935-
#undef _POSIX_C_SOURCE
936-
#endif
937-
#define _POSIX_C_SOURCE 200112L
938-
#endif // __linux__
939-
#include <fcntl.h>]],
940-
[[ int f = posix_fallocate(0, 0, 0); ]])],
941-
[ AC_MSG_RESULT([yes]); AC_DEFINE([HAVE_POSIX_FALLOCATE], [1], [Define this symbol if you have posix_fallocate]) ],
942-
[ AC_MSG_RESULT([no])]
943-
)
929+
print_msan_notice=no
930+
if test x"$enable_ctime_tests" = x"yes"; then
931+
SECP_MSAN_CHECK
932+
# MSan on Clang >=16 reports unitialized memory in function parameters and return values, even if
933+
# the uninitalized variable is never actually "used". This is called "eager" checking, and it's
934+
# sounds like good idea for normal use of MSan. However, it yields many false positives in the
935+
# ctime_tests because many return values depend on secret (i.e., "uninitialized") values, and
936+
# we're only interested in detecting branches (which count as "uses") on secret data.
937+
if test x"$msan_enabled" = x"yes"; then
938+
SECP_TRY_APPEND_CFLAGS([-fno-sanitize-memory-param-retval], SECP_CFLAGS)
939+
print_msan_notice=yes
940+
fi
941+
fi
944942

945-
AC_MSG_CHECKING([for default visibility attribute])
946-
AC_COMPILE_IFELSE([AC_LANG_SOURCE([
947-
int foo(void) __attribute__((visibility("default")));
948-
int main(){}
949-
])],
950-
[
951-
AC_DEFINE([HAVE_DEFAULT_VISIBILITY_ATTRIBUTE], [1], [Define if the visibility attribute is supported.])
952-
AC_MSG_RESULT([yes])
953-
],
954-
[
955-
AC_MSG_RESULT([no])
956-
if test "$use_reduce_exports" = "yes"; then
957-
AC_MSG_ERROR([Cannot find a working visibility attribute. Use --disable-reduce-exports.])
943+
if test x"$enable_coverage" = x"yes"; then
944+
SECP_CONFIG_DEFINES="$SECP_CONFIG_DEFINES -DCOVERAGE=1"
945+
SECP_CFLAGS="-O0 --coverage $SECP_CFLAGS"
946+
# If coverage is enabled, and the user has not overridden CFLAGS,
947+
# override Autoconf's value "-g -O2" with "-g". Otherwise we'd end up
948+
# with "-O0 --coverage -g -O2".
949+
if test "$CFLAGS_overridden" = "no"; then
950+
CFLAGS="-g"
958951
fi
959952
]
960953
)
@@ -1269,42 +1262,30 @@ if test "$use_natpmp" != "no"; then
12691262
AC_CHECK_LIB([natpmp], [initnatpmp], [NATPMP_LIBS="$NATPMP_LIBS -lnatpmp"], [have_natpmp=no], [$NATPMP_LIBS])
12701263
fi
12711264

1272-
CPPFLAGS="$TEMP_CPPFLAGS"
1273-
fi
1274-
1275-
if test "$build_BGL_wallet$build_BGL_cli$build_BGL_tx$build_BGL_util$build_BGLd$BGL_enable_qt$use_tests$use_bench$enable_fuzz_binary" = "nonononononononono"; then
1276-
use_boost=no
1277-
else
1278-
use_boost=yes
1279-
fi
1280-
1281-
if test "$use_boost" = "yes"; then
1282-
1283-
dnl Check for Boost headers
1284-
AX_BOOST_BASE([1.73.0],[],[AC_MSG_ERROR([Boost is not available!])])
1285-
if test "$want_boost" = "no"; then
1286-
AC_MSG_ERROR([Boost is required])
1287-
fi
1288-
1289-
dnl we don't use multi_index serialization
1290-
BOOST_CPPFLAGS="$BOOST_CPPFLAGS -DBOOST_MULTI_INDEX_DISABLE_SERIALIZATION"
1291-
1292-
dnl Prevent use of std::unary_function, which was removed in C++17,
1293-
dnl and will generate warnings with newer compilers for Boost
1294-
dnl older than 1.80.
1295-
dnl See: https://github.com/boostorg/config/pull/430.
1296-
AX_CHECK_PREPROC_FLAG([-DBOOST_NO_CXX98_FUNCTION_BASE], [BOOST_CPPFLAGS="$BOOST_CPPFLAGS -DBOOST_NO_CXX98_FUNCTION_BASE"], [], [$CXXFLAG_WERROR],
1297-
[AC_LANG_PROGRAM([[#include <boost/config.hpp>]])])
1298-
1299-
if test "$suppress_external_warnings" != "no"; then
1300-
BOOST_CPPFLAGS=SUPPRESS_WARNINGS($BOOST_CPPFLAGS)
1265+
error_window_size=['window size for ecmult precomputation not an integer in range [2..24]']
1266+
case $set_ecmult_window in
1267+
''|*[[!0-9]]*)
1268+
# no valid integer
1269+
AC_MSG_ERROR($error_window_size)
1270+
;;
1271+
*)
1272+
if test "$set_ecmult_window" -lt 2 -o "$set_ecmult_window" -gt 24 ; then
1273+
# not in range
1274+
AC_MSG_ERROR($error_window_size)
13011275
fi
1302-
fi
13031276

1304-
case $host in
1305-
dnl Re-enable it after enabling Windows support in cpp-subprocess.
1306-
*mingw*)
1307-
use_external_signer="no"
1277+
case $set_ecmult_gen_kb in
1278+
2)
1279+
SECP_CONFIG_DEFINES="$SECP_CONFIG_DEFINES -DCOMB_BLOCKS=2 -DCOMB_TEETH=5"
1280+
;;
1281+
22)
1282+
SECP_CONFIG_DEFINES="$SECP_CONFIG_DEFINES -DCOMB_BLOCKS=11 -DCOMB_TEETH=6"
1283+
;;
1284+
86)
1285+
SECP_CONFIG_DEFINES="$SECP_CONFIG_DEFINES -DCOMB_BLOCKS=43 -DCOMB_TEETH=6"
1286+
;;
1287+
*)
1288+
AC_MSG_ERROR(['ecmult gen table size not 2, 22 or 86'])
13081289
;;
13091290
esac
13101291
if test "$use_external_signer" = "yes"; then
@@ -1514,24 +1495,9 @@ else
15141495
fi
15151496
fi
15161497

1517-
dnl Enable NAT-PMP support.
1518-
AC_MSG_CHECKING([whether to build with support for NAT-PMP])
1519-
if test "$have_natpmp" = "no"; then
1520-
if test "$use_natpmp" = "yes"; then
1521-
AC_MSG_ERROR([NAT-PMP requested but cannot be built. Use --without-natpmp])
1522-
fi
1523-
AC_MSG_RESULT([no])
1524-
use_natpmp=no
1525-
else
1526-
if test "$use_natpmp" != "no"; then
1527-
AC_MSG_RESULT([yes])
1528-
use_natpmp=yes
1529-
AC_DEFINE([USE_NATPMP], [1], [Define to 1 if UPnP support should be compiled in.])
1530-
if test "$TARGET_OS" = "windows"; then
1531-
NATPMP_CPPFLAGS="$NATPMP_CPPFLAGS -DSTATICLIB -DNATPMP_STATICLIB"
1532-
fi
1533-
else
1534-
AC_MSG_RESULT([no])
1498+
if test x"$enable_experimental" = x"no"; then
1499+
if test x"$set_asm" = x"arm32"; then
1500+
AC_MSG_ERROR([ARM32 assembly is experimental. Use --enable-experimental to allow.])
15351501
fi
15361502
fi
15371503

@@ -1784,3 +1750,23 @@ echo " LDFLAGS = $PTHREAD_LIBS $HARDENED_LDFLAGS $SANITIZER_LDFLAGS $CO
17841750
echo " AR = $AR"
17851751
echo " ARFLAGS = $ARFLAGS"
17861752
echo
1753+
echo " valgrind = $enable_valgrind"
1754+
echo " CC = $CC"
1755+
echo " CPPFLAGS = $CPPFLAGS"
1756+
echo " SECP_CFLAGS = $SECP_CFLAGS"
1757+
echo " CFLAGS = $CFLAGS"
1758+
echo " LDFLAGS = $LDFLAGS"
1759+
1760+
if test x"$print_msan_notice" = x"yes"; then
1761+
echo
1762+
echo "Note:"
1763+
echo " MemorySanitizer detected, tried to add -fno-sanitize-memory-param-retval to SECP_CFLAGS"
1764+
echo " to avoid false positives in ctime_tests. Pass --disable-ctime-tests to avoid this."
1765+
fi
1766+
1767+
if test x"$enable_experimental" = x"yes"; then
1768+
echo
1769+
echo "WARNING: Experimental build"
1770+
echo " Experimental features do not have stable APIs or properties, and may not be safe for"
1771+
echo " production use."
1772+
fi

‎src/secp256k1/CMakeLists.txt

+55-44
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@ project(libsecp256k1
1818
)
1919

2020
if(CMAKE_VERSION VERSION_LESS 3.21)
21-
get_directory_property(parent_directory PARENT_DIRECTORY)
22-
if(parent_directory)
23-
set(PROJECT_IS_TOP_LEVEL OFF CACHE INTERNAL "Emulates CMake 3.21+ behavior.")
24-
set(${PROJECT_NAME}_IS_TOP_LEVEL OFF CACHE INTERNAL "Emulates CMake 3.21+ behavior.")
21+
# Emulates CMake 3.21+ behavior.
22+
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
23+
set(PROJECT_IS_TOP_LEVEL ON)
24+
set(${PROJECT_NAME}_IS_TOP_LEVEL ON)
2525
else()
26-
set(PROJECT_IS_TOP_LEVEL ON CACHE INTERNAL "Emulates CMake 3.21+ behavior.")
27-
set(${PROJECT_NAME}_IS_TOP_LEVEL ON CACHE INTERNAL "Emulates CMake 3.21+ behavior.")
26+
set(PROJECT_IS_TOP_LEVEL OFF)
27+
set(${PROJECT_NAME}_IS_TOP_LEVEL OFF)
2828
endif()
29-
unset(parent_directory)
3029
endif()
3130

3231
# The library version is based on libtool versioning of the ABI. The set of
@@ -92,21 +91,15 @@ if(SECP256K1_USE_EXTERNAL_DEFAULT_CALLBACKS)
9291
add_compile_definitions(USE_EXTERNAL_DEFAULT_CALLBACKS=1)
9392
endif()
9493

95-
set(SECP256K1_ECMULT_WINDOW_SIZE "AUTO" CACHE STRING "Window size for ecmult precomputation for verification, specified as integer in range [2..24]. \"AUTO\" is a reasonable setting for desktop machines (currently 15). [default=AUTO]")
96-
set_property(CACHE SECP256K1_ECMULT_WINDOW_SIZE PROPERTY STRINGS "AUTO" 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24)
94+
set(SECP256K1_ECMULT_WINDOW_SIZE 15 CACHE STRING "Window size for ecmult precomputation for verification, specified as integer in range [2..24]. The default value is a reasonable setting for desktop machines (currently 15). [default=15]")
95+
set_property(CACHE SECP256K1_ECMULT_WINDOW_SIZE PROPERTY STRINGS 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24)
9796
include(CheckStringOptionValue)
9897
check_string_option_value(SECP256K1_ECMULT_WINDOW_SIZE)
99-
if(SECP256K1_ECMULT_WINDOW_SIZE STREQUAL "AUTO")
100-
set(SECP256K1_ECMULT_WINDOW_SIZE 15)
101-
endif()
10298
add_compile_definitions(ECMULT_WINDOW_SIZE=${SECP256K1_ECMULT_WINDOW_SIZE})
10399

104-
set(SECP256K1_ECMULT_GEN_KB "AUTO" CACHE STRING "The size of the precomputed table for signing in multiples of 1024 bytes (on typical platforms). Larger values result in possibly better signing or key generation performance at the cost of a larger table. Valid choices are 2, 22, 86. \"AUTO\" is a reasonable setting for desktop machines (currently 22). [default=AUTO]")
105-
set_property(CACHE SECP256K1_ECMULT_GEN_KB PROPERTY STRINGS "AUTO" 2 22 86)
100+
set(SECP256K1_ECMULT_GEN_KB 22 CACHE STRING "The size of the precomputed table for signing in multiples of 1024 bytes (on typical platforms). Larger values result in possibly better signing or key generation performance at the cost of a larger table. Valid choices are 2, 22, 86. The default value is a reasonable setting for desktop machines (currently 22). [default=22]")
101+
set_property(CACHE SECP256K1_ECMULT_GEN_KB PROPERTY STRINGS 2 22 86)
106102
check_string_option_value(SECP256K1_ECMULT_GEN_KB)
107-
if(SECP256K1_ECMULT_GEN_KB STREQUAL "AUTO")
108-
set(SECP256K1_ECMULT_GEN_KB 22)
109-
endif()
110103
if(SECP256K1_ECMULT_GEN_KB EQUAL 2)
111104
add_compile_definitions(COMB_BLOCKS=2)
112105
add_compile_definitions(COMB_TEETH=5)
@@ -214,23 +207,25 @@ mark_as_advanced(
214207
CMAKE_SHARED_LINKER_FLAGS_COVERAGE
215208
)
216209

217-
get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
218-
set(default_build_type "RelWithDebInfo")
219-
if(is_multi_config)
220-
set(CMAKE_CONFIGURATION_TYPES "${default_build_type}" "Release" "Debug" "MinSizeRel" "Coverage" CACHE STRING
221-
"Supported configuration types."
222-
FORCE
223-
)
224-
else()
225-
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY
226-
STRINGS "${default_build_type}" "Release" "Debug" "MinSizeRel" "Coverage"
227-
)
228-
if(NOT CMAKE_BUILD_TYPE)
229-
message(STATUS "Setting build type to \"${default_build_type}\" as none was specified")
230-
set(CMAKE_BUILD_TYPE "${default_build_type}" CACHE STRING
231-
"Choose the type of build."
210+
if(PROJECT_IS_TOP_LEVEL)
211+
get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
212+
set(default_build_type "RelWithDebInfo")
213+
if(is_multi_config)
214+
set(CMAKE_CONFIGURATION_TYPES "${default_build_type}" "Release" "Debug" "MinSizeRel" "Coverage" CACHE STRING
215+
"Supported configuration types."
232216
FORCE
233217
)
218+
else()
219+
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY
220+
STRINGS "${default_build_type}" "Release" "Debug" "MinSizeRel" "Coverage"
221+
)
222+
if(NOT CMAKE_BUILD_TYPE)
223+
message(STATUS "Setting build type to \"${default_build_type}\" as none was specified")
224+
set(CMAKE_BUILD_TYPE "${default_build_type}" CACHE STRING
225+
"Choose the type of build."
226+
FORCE
227+
)
228+
endif()
234229
endif()
235230
endif()
236231

@@ -263,25 +258,34 @@ endif()
263258

264259
set(CMAKE_C_VISIBILITY_PRESET hidden)
265260

266-
# Ask CTest to create a "check" target (e.g., make check) as alias for the "test" target.
267-
# CTEST_TEST_TARGET_ALIAS is not documented but supposed to be user-facing.
268-
# See: https://gitlab.kitware.com/cmake/cmake/-/commit/816c9d1aa1f2b42d40c81a991b68c96eb12b6d2
269-
set(CTEST_TEST_TARGET_ALIAS check)
261+
set(print_msan_notice)
262+
if(SECP256K1_BUILD_CTIME_TESTS)
263+
include(CheckMemorySanitizer)
264+
check_memory_sanitizer(msan_enabled)
265+
if(msan_enabled)
266+
try_append_c_flags(-fno-sanitize-memory-param-retval)
267+
set(print_msan_notice YES)
268+
endif()
269+
unset(msan_enabled)
270+
endif()
271+
270272
include(CTest)
271273
# We do not use CTest's BUILD_TESTING because a single toggle for all tests is too coarse for our needs.
272274
mark_as_advanced(BUILD_TESTING)
273275
if(SECP256K1_BUILD_BENCHMARK OR SECP256K1_BUILD_TESTS OR SECP256K1_BUILD_EXHAUSTIVE_TESTS OR SECP256K1_BUILD_CTIME_TESTS OR SECP256K1_BUILD_EXAMPLES)
274276
enable_testing()
275277
endif()
276278

277-
set(SECP256K1_LATE_CFLAGS "" CACHE STRING "Compiler flags that are added to the command line after all other flags added by the build system.")
278-
include(AllTargetsCompileOptions)
279+
set(SECP256K1_APPEND_CFLAGS "" CACHE STRING "Compiler flags that are appended to the command line after all other flags added by the build system. This variable is intended for debugging and special builds.")
280+
if(SECP256K1_APPEND_CFLAGS)
281+
# Appending to this low-level rule variable is the only way to
282+
# guarantee that the flags appear at the end of the command line.
283+
string(APPEND CMAKE_C_COMPILE_OBJECT " ${SECP256K1_APPEND_CFLAGS}")
284+
endif()
279285

280286
add_subdirectory(src)
281-
all_targets_compile_options(src "${SECP256K1_LATE_CFLAGS}")
282287
if(SECP256K1_BUILD_EXAMPLES)
283288
add_subdirectory(examples)
284-
all_targets_compile_options(examples "${SECP256K1_LATE_CFLAGS}")
285289
endif()
286290

287291
message("\n")
@@ -332,7 +336,7 @@ message("Valgrind .............................. ${SECP256K1_VALGRIND}")
332336
get_directory_property(definitions COMPILE_DEFINITIONS)
333337
string(REPLACE ";" " " definitions "${definitions}")
334338
message("Preprocessor defined macros ........... ${definitions}")
335-
message("C compiler ............................ ${CMAKE_C_COMPILER}")
339+
message("C compiler ............................ ${CMAKE_C_COMPILER_ID} ${CMAKE_C_COMPILER_VERSION}, ${CMAKE_C_COMPILER}")
336340
message("CFLAGS ................................ ${CMAKE_C_FLAGS}")
337341
get_directory_property(compile_options COMPILE_OPTIONS)
338342
string(REPLACE ";" " " compile_options "${compile_options}")
@@ -355,10 +359,17 @@ else()
355359
message(" - LDFLAGS for executables ............ ${CMAKE_EXE_LINKER_FLAGS_DEBUG}")
356360
message(" - LDFLAGS for shared libraries ....... ${CMAKE_SHARED_LINKER_FLAGS_DEBUG}")
357361
endif()
358-
if(SECP256K1_LATE_CFLAGS)
359-
message("SECP256K1_LATE_CFLAGS ................. ${SECP256K1_LATE_CFLAGS}")
362+
if(SECP256K1_APPEND_CFLAGS)
363+
message("SECP256K1_APPEND_CFLAGS ............... ${SECP256K1_APPEND_CFLAGS}")
364+
endif()
365+
message("")
366+
if(print_msan_notice)
367+
message(
368+
"Note:\n"
369+
" MemorySanitizer detected, tried to add -fno-sanitize-memory-param-retval to compile options\n"
370+
" to avoid false positives in ctime_tests. Pass -DSECP256K1_BUILD_CTIME_TESTS=OFF to avoid this.\n"
371+
)
360372
endif()
361-
message("\n")
362373
if(SECP256K1_EXPERIMENTAL)
363374
message(
364375
" ******\n"

‎src/secp256k1/build-aux/m4/BGL_secp.m4

+16
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ if test x"$has_valgrind" != x"yes"; then
4343
fi
4444
])
4545

46+
AC_DEFUN([SECP_MSAN_CHECK], [
47+
AC_MSG_CHECKING(whether MemorySanitizer is enabled)
48+
AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
49+
#if defined(__has_feature)
50+
# if __has_feature(memory_sanitizer)
51+
/* MemorySanitizer is enabled. */
52+
# elif
53+
# error "MemorySanitizer is disabled."
54+
# endif
55+
#else
56+
# error "__has_feature is not defined."
57+
#endif
58+
]])], [msan_enabled=yes], [msan_enabled=no])
59+
AC_MSG_RESULT([$msan_enabled])
60+
])
61+
4662
dnl SECP_TRY_APPEND_CFLAGS(flags, VAR)
4763
dnl Append flags to VAR if CC accepts them.
4864
AC_DEFUN([SECP_TRY_APPEND_CFLAGS], [

‎src/secp256k1/cmake/AllTargetsCompileOptions.cmake

-12
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
include_guard(GLOBAL)
2+
include(CheckCSourceCompiles)
3+
4+
function(check_memory_sanitizer output)
5+
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
6+
check_c_source_compiles("
7+
#if defined(__has_feature)
8+
# if __has_feature(memory_sanitizer)
9+
/* MemorySanitizer is enabled. */
10+
# elif
11+
# error \"MemorySanitizer is disabled.\"
12+
# endif
13+
#else
14+
# error \"__has_feature is not defined.\"
15+
#endif
16+
" HAVE_MSAN)
17+
set(${output} ${HAVE_MSAN} PARENT_SCOPE)
18+
endfunction()

‎src/secp256k1/src/modules/ecdh/tests_impl.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static void test_ecdh_generator_basepoint(void) {
5656
size_t point_ser_len = sizeof(point_ser);
5757
secp256k1_scalar s;
5858

59-
random_scalar_order(&s);
59+
testutil_random_scalar_order(&s);
6060
secp256k1_scalar_get_b32(s_b32, &s);
6161

6262
CHECK(secp256k1_ec_pubkey_create(CTX, &point[0], s_one) == 1);
@@ -95,7 +95,7 @@ static void test_bad_scalar(void) {
9595
secp256k1_pubkey point;
9696

9797
/* Create random point */
98-
random_scalar_order(&rand);
98+
testutil_random_scalar_order(&rand);
9999
secp256k1_scalar_get_b32(s_rand, &rand);
100100
CHECK(secp256k1_ec_pubkey_create(CTX, &point, s_rand) == 1);
101101

@@ -127,7 +127,7 @@ static void test_result_basepoint(void) {
127127
CHECK(secp256k1_ecdh(CTX, out_base, &point, s_one, NULL, NULL) == 1);
128128

129129
for (i = 0; i < 2 * COUNT; i++) {
130-
random_scalar_order(&rand);
130+
testutil_random_scalar_order(&rand);
131131
secp256k1_scalar_get_b32(s, &rand);
132132
secp256k1_scalar_inverse(&rand, &rand);
133133
secp256k1_scalar_get_b32(s_inv, &rand);

‎src/secp256k1/src/modules/ellswift/tests_impl.h

+19-19
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,9 @@ void run_ellswift_tests(void) {
229229
secp256k1_ge g, g2;
230230
secp256k1_pubkey pubkey, pubkey2;
231231
/* Generate random public key and random randomizer. */
232-
random_group_element_test(&g);
232+
testutil_random_ge_test(&g);
233233
secp256k1_pubkey_save(&pubkey, &g);
234-
secp256k1_testrand256(rnd32);
234+
testrand256(rnd32);
235235
/* Convert the public key to ElligatorSwift and back. */
236236
secp256k1_ellswift_encode(CTX, ell64, &pubkey, rnd32);
237237
secp256k1_ellswift_decode(CTX, &pubkey2, ell64);
@@ -249,8 +249,8 @@ void run_ellswift_tests(void) {
249249
unsigned char ell64[64];
250250
int ret;
251251
/* Generate random secret key and random randomizer. */
252-
if (i & 1) secp256k1_testrand256_test(auxrnd32);
253-
random_scalar_order_test(&sec);
252+
if (i & 1) testrand256_test(auxrnd32);
253+
testutil_random_scalar_order_test(&sec);
254254
secp256k1_scalar_get_b32(sec32, &sec);
255255
/* Construct ElligatorSwift-encoded public keys for that key. */
256256
ret = secp256k1_ellswift_create(CTX, ell64, sec32, (i & 1) ? auxrnd32 : NULL);
@@ -271,11 +271,11 @@ void run_ellswift_tests(void) {
271271
secp256k1_pubkey pub;
272272
int ret;
273273
/* Generate random secret key. */
274-
random_scalar_order_test(&sec);
274+
testutil_random_scalar_order_test(&sec);
275275
secp256k1_scalar_get_b32(sec32, &sec);
276276
/* Generate random ElligatorSwift encoding for the remote key and decode it. */
277-
secp256k1_testrand256_test(ell64);
278-
secp256k1_testrand256_test(ell64 + 32);
277+
testrand256_test(ell64);
278+
testrand256_test(ell64 + 32);
279279
secp256k1_ellswift_decode(CTX, &pub, ell64);
280280
secp256k1_pubkey_load(CTX, &dec, &pub);
281281
secp256k1_gej_set_ge(&decj, &dec);
@@ -313,18 +313,18 @@ void run_ellswift_tests(void) {
313313
data = NULL;
314314
} else {
315315
hash_function = secp256k1_ellswift_xdh_hash_function_prefix;
316-
secp256k1_testrand256_test(prefix64);
317-
secp256k1_testrand256_test(prefix64 + 32);
316+
testrand256_test(prefix64);
317+
testrand256_test(prefix64 + 32);
318318
data = prefix64;
319319
}
320320

321321
/* Generate random secret keys and random randomizers. */
322-
secp256k1_testrand256_test(auxrnd32a);
323-
secp256k1_testrand256_test(auxrnd32b);
324-
random_scalar_order_test(&seca);
322+
testrand256_test(auxrnd32a);
323+
testrand256_test(auxrnd32b);
324+
testutil_random_scalar_order_test(&seca);
325325
/* Draw secb uniformly at random to make sure that the secret keys
326326
* differ */
327-
random_scalar_order(&secb);
327+
testutil_random_scalar_order(&secb);
328328
secp256k1_scalar_get_b32(sec32a, &seca);
329329
secp256k1_scalar_get_b32(sec32b, &secb);
330330

@@ -349,42 +349,42 @@ void run_ellswift_tests(void) {
349349
/* Verify that the shared secret doesn't match if other side's public key is incorrect. */
350350
/* For A (using a bad public key for B): */
351351
memcpy(ell64b_bad, ell64b, sizeof(ell64a_bad));
352-
secp256k1_testrand_flip(ell64b_bad, sizeof(ell64b_bad));
352+
testrand_flip(ell64b_bad, sizeof(ell64b_bad));
353353
ret = secp256k1_ellswift_xdh(CTX, share32_bad, ell64a, ell64b_bad, sec32a, 0, hash_function, data);
354354
CHECK(ret); /* Mismatching encodings don't get detected by secp256k1_ellswift_xdh. */
355355
CHECK(secp256k1_memcmp_var(share32_bad, share32a, 32) != 0);
356356
/* For B (using a bad public key for A): */
357357
memcpy(ell64a_bad, ell64a, sizeof(ell64a_bad));
358-
secp256k1_testrand_flip(ell64a_bad, sizeof(ell64a_bad));
358+
testrand_flip(ell64a_bad, sizeof(ell64a_bad));
359359
ret = secp256k1_ellswift_xdh(CTX, share32_bad, ell64a_bad, ell64b, sec32b, 1, hash_function, data);
360360
CHECK(ret);
361361
CHECK(secp256k1_memcmp_var(share32_bad, share32b, 32) != 0);
362362

363363
/* Verify that the shared secret doesn't match if the private key is incorrect. */
364364
/* For A: */
365365
memcpy(sec32a_bad, sec32a, sizeof(sec32a_bad));
366-
secp256k1_testrand_flip(sec32a_bad, sizeof(sec32a_bad));
366+
testrand_flip(sec32a_bad, sizeof(sec32a_bad));
367367
ret = secp256k1_ellswift_xdh(CTX, share32_bad, ell64a, ell64b, sec32a_bad, 0, hash_function, data);
368368
CHECK(!ret || secp256k1_memcmp_var(share32_bad, share32a, 32) != 0);
369369
/* For B: */
370370
memcpy(sec32b_bad, sec32b, sizeof(sec32b_bad));
371-
secp256k1_testrand_flip(sec32b_bad, sizeof(sec32b_bad));
371+
testrand_flip(sec32b_bad, sizeof(sec32b_bad));
372372
ret = secp256k1_ellswift_xdh(CTX, share32_bad, ell64a, ell64b, sec32b_bad, 1, hash_function, data);
373373
CHECK(!ret || secp256k1_memcmp_var(share32_bad, share32b, 32) != 0);
374374

375375
if (hash_function != ellswift_xdh_hash_x32) {
376376
/* Verify that the shared secret doesn't match when a different encoding of the same public key is used. */
377377
/* For A (changing B's public key): */
378378
memcpy(auxrnd32b_bad, auxrnd32b, sizeof(auxrnd32b_bad));
379-
secp256k1_testrand_flip(auxrnd32b_bad, sizeof(auxrnd32b_bad));
379+
testrand_flip(auxrnd32b_bad, sizeof(auxrnd32b_bad));
380380
ret = secp256k1_ellswift_create(CTX, ell64b_bad, sec32b, auxrnd32b_bad);
381381
CHECK(ret);
382382
ret = secp256k1_ellswift_xdh(CTX, share32_bad, ell64a, ell64b_bad, sec32a, 0, hash_function, data);
383383
CHECK(ret);
384384
CHECK(secp256k1_memcmp_var(share32_bad, share32a, 32) != 0);
385385
/* For B (changing A's public key): */
386386
memcpy(auxrnd32a_bad, auxrnd32a, sizeof(auxrnd32a_bad));
387-
secp256k1_testrand_flip(auxrnd32a_bad, sizeof(auxrnd32a_bad));
387+
testrand_flip(auxrnd32a_bad, sizeof(auxrnd32a_bad));
388388
ret = secp256k1_ellswift_create(CTX, ell64a_bad, sec32a, auxrnd32a_bad);
389389
CHECK(ret);
390390
ret = secp256k1_ellswift_xdh(CTX, share32_bad, ell64a_bad, ell64b, sec32b, 1, hash_function, data);

‎src/secp256k1/src/modules/extrakeys/tests_impl.h

+18-18
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ static void test_xonly_pubkey(void) {
2323
int pk_parity;
2424
int i;
2525

26-
secp256k1_testrand256(sk);
26+
testrand256(sk);
2727
memset(ones32, 0xFF, 32);
28-
secp256k1_testrand256(xy_sk);
28+
testrand256(xy_sk);
2929
CHECK(secp256k1_ec_pubkey_create(CTX, &pk, sk) == 1);
3030
CHECK(secp256k1_xonly_pubkey_from_pubkey(CTX, &xonly_pk, &pk_parity, &pk) == 1);
3131

@@ -94,7 +94,7 @@ static void test_xonly_pubkey(void) {
9494
* the curve) then xonly_pubkey_parse should fail as well. */
9595
for (i = 0; i < COUNT; i++) {
9696
unsigned char rand33[33];
97-
secp256k1_testrand256(&rand33[1]);
97+
testrand256(&rand33[1]);
9898
rand33[0] = SECP256K1_TAG_PUBKEY_EVEN;
9999
if (!secp256k1_ec_pubkey_parse(CTX, &pk, rand33, 33)) {
100100
memset(&xonly_pk, 1, sizeof(xonly_pk));
@@ -151,8 +151,8 @@ static void test_xonly_pubkey_tweak(void) {
151151
int i;
152152

153153
memset(overflows, 0xff, sizeof(overflows));
154-
secp256k1_testrand256(tweak);
155-
secp256k1_testrand256(sk);
154+
testrand256(tweak);
155+
testrand256(sk);
156156
CHECK(secp256k1_ec_pubkey_create(CTX, &internal_pk, sk) == 1);
157157
CHECK(secp256k1_xonly_pubkey_from_pubkey(CTX, &internal_xonly_pk, &pk_parity, &internal_pk) == 1);
158158

@@ -189,7 +189,7 @@ static void test_xonly_pubkey_tweak(void) {
189189

190190
/* Invalid pk with a valid tweak */
191191
memset(&internal_xonly_pk, 0, sizeof(internal_xonly_pk));
192-
secp256k1_testrand256(tweak);
192+
testrand256(tweak);
193193
CHECK_ILLEGAL(CTX, secp256k1_xonly_pubkey_tweak_add(CTX, &output_pk, &internal_xonly_pk, tweak));
194194
CHECK(secp256k1_memcmp_var(&output_pk, zeros64, sizeof(output_pk)) == 0);
195195

@@ -212,8 +212,8 @@ static void test_xonly_pubkey_tweak_check(void) {
212212
unsigned char tweak[32];
213213

214214
memset(overflows, 0xff, sizeof(overflows));
215-
secp256k1_testrand256(tweak);
216-
secp256k1_testrand256(sk);
215+
testrand256(tweak);
216+
testrand256(sk);
217217
CHECK(secp256k1_ec_pubkey_create(CTX, &internal_pk, sk) == 1);
218218
CHECK(secp256k1_xonly_pubkey_from_pubkey(CTX, &internal_xonly_pk, &pk_parity, &internal_pk) == 1);
219219

@@ -259,7 +259,7 @@ static void test_xonly_pubkey_tweak_recursive(void) {
259259
unsigned char tweak[N_PUBKEYS - 1][32];
260260
int i;
261261

262-
secp256k1_testrand256(sk);
262+
testrand256(sk);
263263
CHECK(secp256k1_ec_pubkey_create(CTX, &pk[0], sk) == 1);
264264
/* Add tweaks */
265265
for (i = 0; i < N_PUBKEYS - 1; i++) {
@@ -295,7 +295,7 @@ static void test_keypair(void) {
295295
memset(overflows, 0xFF, sizeof(overflows));
296296

297297
/* Test keypair_create */
298-
secp256k1_testrand256(sk);
298+
testrand256(sk);
299299
CHECK(secp256k1_keypair_create(CTX, &keypair, sk) == 1);
300300
CHECK(secp256k1_memcmp_var(zeros96, &keypair, sizeof(keypair)) != 0);
301301
CHECK(secp256k1_keypair_create(CTX, &keypair, sk) == 1);
@@ -313,7 +313,7 @@ static void test_keypair(void) {
313313
CHECK(secp256k1_memcmp_var(zeros96, &keypair, sizeof(keypair)) == 0);
314314

315315
/* Test keypair_pub */
316-
secp256k1_testrand256(sk);
316+
testrand256(sk);
317317
CHECK(secp256k1_keypair_create(CTX, &keypair, sk) == 1);
318318
CHECK(secp256k1_keypair_pub(CTX, &pk, &keypair) == 1);
319319
CHECK_ILLEGAL(CTX, secp256k1_keypair_pub(CTX, NULL, &keypair));
@@ -332,7 +332,7 @@ static void test_keypair(void) {
332332
CHECK(secp256k1_memcmp_var(&pk, &pk_tmp, sizeof(pk)) == 0);
333333

334334
/** Test keypair_xonly_pub **/
335-
secp256k1_testrand256(sk);
335+
testrand256(sk);
336336
CHECK(secp256k1_keypair_create(CTX, &keypair, sk) == 1);
337337
CHECK(secp256k1_keypair_xonly_pub(CTX, &xonly_pk, &pk_parity, &keypair) == 1);
338338
CHECK_ILLEGAL(CTX, secp256k1_keypair_xonly_pub(CTX, NULL, &pk_parity, &keypair));
@@ -355,7 +355,7 @@ static void test_keypair(void) {
355355
CHECK(pk_parity == pk_parity_tmp);
356356

357357
/* Test keypair_seckey */
358-
secp256k1_testrand256(sk);
358+
testrand256(sk);
359359
CHECK(secp256k1_keypair_create(CTX, &keypair, sk) == 1);
360360
CHECK(secp256k1_keypair_sec(CTX, sk_tmp, &keypair) == 1);
361361
CHECK_ILLEGAL(CTX, secp256k1_keypair_sec(CTX, NULL, &keypair));
@@ -382,8 +382,8 @@ static void test_keypair_add(void) {
382382
int i;
383383

384384
CHECK(sizeof(zeros96) == sizeof(keypair));
385-
secp256k1_testrand256(sk);
386-
secp256k1_testrand256(tweak);
385+
testrand256(sk);
386+
testrand256(tweak);
387387
memset(overflows, 0xFF, 32);
388388
CHECK(secp256k1_keypair_create(CTX, &keypair, sk) == 1);
389389

@@ -408,7 +408,7 @@ static void test_keypair_add(void) {
408408
for (i = 0; i < COUNT; i++) {
409409
secp256k1_scalar scalar_tweak;
410410
secp256k1_keypair keypair_tmp;
411-
secp256k1_testrand256(sk);
411+
testrand256(sk);
412412
CHECK(secp256k1_keypair_create(CTX, &keypair, sk) == 1);
413413
memcpy(&keypair_tmp, &keypair, sizeof(keypair));
414414
/* Because sk may be negated before adding, we need to try with tweak =
@@ -424,7 +424,7 @@ static void test_keypair_add(void) {
424424

425425
/* Invalid keypair with a valid tweak */
426426
memset(&keypair, 0, sizeof(keypair));
427-
secp256k1_testrand256(tweak);
427+
testrand256(tweak);
428428
CHECK_ILLEGAL(CTX, secp256k1_keypair_xonly_tweak_add(CTX, &keypair, tweak));
429429
CHECK(secp256k1_memcmp_var(&keypair, zeros96, sizeof(keypair)) == 0);
430430
/* Only seckey part of keypair invalid */
@@ -447,7 +447,7 @@ static void test_keypair_add(void) {
447447
unsigned char sk32[32];
448448
int pk_parity;
449449

450-
secp256k1_testrand256(tweak);
450+
testrand256(tweak);
451451
CHECK(secp256k1_keypair_xonly_pub(CTX, &internal_pk, NULL, &keypair) == 1);
452452
CHECK(secp256k1_keypair_xonly_tweak_add(CTX, &keypair, tweak) == 1);
453453
CHECK(secp256k1_keypair_xonly_pub(CTX, &output_pk, &pk_parity, &keypair) == 1);

‎src/secp256k1/src/modules/recovery/tests_impl.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ static int recovery_test_nonce_function(unsigned char *nonce32, const unsigned c
2525
}
2626
/* On the next run, return a valid nonce, but flip a coin as to whether or not to fail signing. */
2727
memset(nonce32, 1, 32);
28-
return secp256k1_testrand_bits(1);
28+
return testrand_bits(1);
2929
}
3030

3131
static void test_ecdsa_recovery_api(void) {
@@ -106,8 +106,8 @@ static void test_ecdsa_recovery_end_to_end(void) {
106106
/* Generate a random key and message. */
107107
{
108108
secp256k1_scalar msg, key;
109-
random_scalar_order_test(&msg);
110-
random_scalar_order_test(&key);
109+
testutil_random_scalar_order_test(&msg);
110+
testutil_random_scalar_order_test(&key);
111111
secp256k1_scalar_get_b32(privkey, &key);
112112
secp256k1_scalar_get_b32(message, &msg);
113113
}
@@ -141,7 +141,7 @@ static void test_ecdsa_recovery_end_to_end(void) {
141141
CHECK(secp256k1_memcmp_var(&pubkey, &recpubkey, sizeof(pubkey)) == 0);
142142
/* Serialize/destroy/parse signature and verify again. */
143143
CHECK(secp256k1_ecdsa_recoverable_signature_serialize_compact(CTX, sig, &recid, &rsignature[4]) == 1);
144-
sig[secp256k1_testrand_bits(6)] += 1 + secp256k1_testrand_int(255);
144+
sig[testrand_bits(6)] += 1 + testrand_int(255);
145145
CHECK(secp256k1_ecdsa_recoverable_signature_parse_compact(CTX, &rsignature[4], sig, recid) == 1);
146146
CHECK(secp256k1_ecdsa_recoverable_signature_convert(CTX, &signature[4], &rsignature[4]) == 1);
147147
CHECK(secp256k1_ecdsa_verify(CTX, &signature[4], message, &pubkey) == 0);

‎src/secp256k1/src/modules/schnorrsig/tests_exhaustive_impl.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ static void test_exhaustive_schnorrsig_verify(const secp256k1_context *ctx, cons
104104
while (e_count_done < EXHAUSTIVE_TEST_ORDER) {
105105
secp256k1_scalar e;
106106
unsigned char msg32[32];
107-
secp256k1_testrand256(msg32);
107+
testrand256(msg32);
108108
secp256k1_schnorrsig_challenge(&e, sig64, msg32, sizeof(msg32), pk32);
109109
/* Only do work if we hit a challenge we haven't tried before. */
110110
if (!e_done[e]) {
@@ -120,7 +120,7 @@ static void test_exhaustive_schnorrsig_verify(const secp256k1_context *ctx, cons
120120
expect_valid = actual_k != -1 && s != EXHAUSTIVE_TEST_ORDER &&
121121
(s == (actual_k + actual_d * e) % EXHAUSTIVE_TEST_ORDER);
122122
} else {
123-
secp256k1_testrand256(sig64 + 32);
123+
testrand256(sig64 + 32);
124124
expect_valid = 0;
125125
}
126126
valid = secp256k1_schnorrsig_verify(ctx, sig64, msg32, sizeof(msg32), &pubkeys[d - 1]);
@@ -161,7 +161,7 @@ static void test_exhaustive_schnorrsig_sign(const secp256k1_context *ctx, unsign
161161
/* Generate random messages until all challenges have been tried. */
162162
while (e_count_done < EXHAUSTIVE_TEST_ORDER) {
163163
secp256k1_scalar e;
164-
secp256k1_testrand256(msg32);
164+
testrand256(msg32);
165165
secp256k1_schnorrsig_challenge(&e, xonly_pubkey_bytes[k - 1], msg32, sizeof(msg32), xonly_pubkey_bytes[d - 1]);
166166
/* Only do work if we hit a challenge we haven't tried before. */
167167
if (!e_done[e]) {

‎src/secp256k1/src/modules/schnorrsig/tests_impl.h

+25-25
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
static void nonce_function_bip340_bitflip(unsigned char **args, size_t n_flip, size_t n_bytes, size_t msglen, size_t algolen) {
1616
unsigned char nonces[2][32];
1717
CHECK(nonce_function_bip340(nonces[0], args[0], msglen, args[1], args[2], args[3], algolen, args[4]) == 1);
18-
secp256k1_testrand_flip(args[n_flip], n_bytes);
18+
testrand_flip(args[n_flip], n_bytes);
1919
CHECK(nonce_function_bip340(nonces[1], args[0], msglen, args[1], args[2], args[3], algolen, args[4]) == 1);
2020
CHECK(secp256k1_memcmp_var(nonces[0], nonces[1], 32) != 0);
2121
}
@@ -50,10 +50,10 @@ static void run_nonce_function_bip340_tests(void) {
5050
secp256k1_nonce_function_bip340_sha256_tagged_aux(&sha_optimized);
5151
test_sha256_eq(&sha, &sha_optimized);
5252

53-
secp256k1_testrand256(msg);
54-
secp256k1_testrand256(key);
55-
secp256k1_testrand256(pk);
56-
secp256k1_testrand256(aux_rand);
53+
testrand256(msg);
54+
testrand256(key);
55+
testrand256(pk);
56+
testrand256(aux_rand);
5757

5858
/* Check that a bitflip in an argument results in different nonces. */
5959
args[0] = msg;
@@ -76,12 +76,12 @@ static void run_nonce_function_bip340_tests(void) {
7676
CHECK(nonce_function_bip340(nonce, msg, msglen, key, pk, NULL, 0, NULL) == 0);
7777
CHECK(nonce_function_bip340(nonce, msg, msglen, key, pk, algo, algolen, NULL) == 1);
7878
/* Other algo is fine */
79-
secp256k1_testrand_bytes_test(algo, algolen);
79+
testrand_bytes_test(algo, algolen);
8080
CHECK(nonce_function_bip340(nonce, msg, msglen, key, pk, algo, algolen, NULL) == 1);
8181

8282
for (i = 0; i < COUNT; i++) {
8383
unsigned char nonce2[32];
84-
uint32_t offset = secp256k1_testrand_int(msglen - 1);
84+
uint32_t offset = testrand_int(msglen - 1);
8585
size_t msglen_tmp = (msglen + offset) % msglen;
8686
size_t algolen_tmp;
8787

@@ -90,7 +90,7 @@ static void run_nonce_function_bip340_tests(void) {
9090
CHECK(secp256k1_memcmp_var(nonce, nonce2, 32) != 0);
9191

9292
/* Different algolen gives different nonce */
93-
offset = secp256k1_testrand_int(algolen - 1);
93+
offset = testrand_int(algolen - 1);
9494
algolen_tmp = (algolen + offset) % algolen;
9595
CHECK(nonce_function_bip340(nonce2, msg, msglen, key, pk, algo, algolen_tmp, NULL) == 1);
9696
CHECK(secp256k1_memcmp_var(nonce, nonce2, 32) != 0);
@@ -116,10 +116,10 @@ static void test_schnorrsig_api(void) {
116116
secp256k1_schnorrsig_extraparams extraparams = SECP256K1_SCHNORRSIG_EXTRAPARAMS_INIT;
117117
secp256k1_schnorrsig_extraparams invalid_extraparams = {{ 0 }, NULL, NULL};
118118

119-
secp256k1_testrand256(sk1);
120-
secp256k1_testrand256(sk2);
121-
secp256k1_testrand256(sk3);
122-
secp256k1_testrand256(msg);
119+
testrand256(sk1);
120+
testrand256(sk2);
121+
testrand256(sk3);
122+
testrand256(msg);
123123
CHECK(secp256k1_keypair_create(CTX, &keypairs[0], sk1) == 1);
124124
CHECK(secp256k1_keypair_create(CTX, &keypairs[1], sk2) == 1);
125125
CHECK(secp256k1_keypair_create(CTX, &keypairs[2], sk3) == 1);
@@ -813,8 +813,8 @@ static void test_schnorrsig_sign(void) {
813813
secp256k1_schnorrsig_extraparams extraparams = SECP256K1_SCHNORRSIG_EXTRAPARAMS_INIT;
814814
unsigned char aux_rand[32];
815815

816-
secp256k1_testrand256(sk);
817-
secp256k1_testrand256(aux_rand);
816+
testrand256(sk);
817+
testrand256(aux_rand);
818818
CHECK(secp256k1_keypair_create(CTX, &keypair, sk));
819819
CHECK(secp256k1_keypair_xonly_pub(CTX, &pk, NULL, &keypair));
820820
CHECK(secp256k1_schnorrsig_sign32(CTX, sig, msg, &keypair, NULL) == 1);
@@ -861,32 +861,32 @@ static void test_schnorrsig_sign_verify(void) {
861861
secp256k1_xonly_pubkey pk;
862862
secp256k1_scalar s;
863863

864-
secp256k1_testrand256(sk);
864+
testrand256(sk);
865865
CHECK(secp256k1_keypair_create(CTX, &keypair, sk));
866866
CHECK(secp256k1_keypair_xonly_pub(CTX, &pk, NULL, &keypair));
867867

868868
for (i = 0; i < N_SIGS; i++) {
869-
secp256k1_testrand256(msg[i]);
869+
testrand256(msg[i]);
870870
CHECK(secp256k1_schnorrsig_sign32(CTX, sig[i], msg[i], &keypair, NULL));
871871
CHECK(secp256k1_schnorrsig_verify(CTX, sig[i], msg[i], sizeof(msg[i]), &pk));
872872
}
873873

874874
{
875875
/* Flip a few bits in the signature and in the message and check that
876876
* verify and verify_batch (TODO) fail */
877-
size_t sig_idx = secp256k1_testrand_int(N_SIGS);
878-
size_t byte_idx = secp256k1_testrand_bits(5);
879-
unsigned char xorbyte = secp256k1_testrand_int(254)+1;
877+
size_t sig_idx = testrand_int(N_SIGS);
878+
size_t byte_idx = testrand_bits(5);
879+
unsigned char xorbyte = testrand_int(254)+1;
880880
sig[sig_idx][byte_idx] ^= xorbyte;
881881
CHECK(!secp256k1_schnorrsig_verify(CTX, sig[sig_idx], msg[sig_idx], sizeof(msg[sig_idx]), &pk));
882882
sig[sig_idx][byte_idx] ^= xorbyte;
883883

884-
byte_idx = secp256k1_testrand_bits(5);
884+
byte_idx = testrand_bits(5);
885885
sig[sig_idx][32+byte_idx] ^= xorbyte;
886886
CHECK(!secp256k1_schnorrsig_verify(CTX, sig[sig_idx], msg[sig_idx], sizeof(msg[sig_idx]), &pk));
887887
sig[sig_idx][32+byte_idx] ^= xorbyte;
888888

889-
byte_idx = secp256k1_testrand_bits(5);
889+
byte_idx = testrand_bits(5);
890890
msg[sig_idx][byte_idx] ^= xorbyte;
891891
CHECK(!secp256k1_schnorrsig_verify(CTX, sig[sig_idx], msg[sig_idx], sizeof(msg[sig_idx]), &pk));
892892
msg[sig_idx][byte_idx] ^= xorbyte;
@@ -916,9 +916,9 @@ static void test_schnorrsig_sign_verify(void) {
916916
{
917917
/* Test varying message lengths */
918918
unsigned char msg_large[32 * 8];
919-
uint32_t msglen = secp256k1_testrand_int(sizeof(msg_large));
919+
uint32_t msglen = testrand_int(sizeof(msg_large));
920920
for (i = 0; i < sizeof(msg_large); i += 32) {
921-
secp256k1_testrand256(&msg_large[i]);
921+
testrand256(&msg_large[i]);
922922
}
923923
CHECK(secp256k1_schnorrsig_sign_custom(CTX, sig[0], msg_large, msglen, &keypair, NULL) == 1);
924924
CHECK(secp256k1_schnorrsig_verify(CTX, sig[0], msg_large, msglen, &pk) == 1);
@@ -942,7 +942,7 @@ static void test_schnorrsig_taproot(void) {
942942
unsigned char sig[64];
943943

944944
/* Create output key */
945-
secp256k1_testrand256(sk);
945+
testrand256(sk);
946946
CHECK(secp256k1_keypair_create(CTX, &keypair, sk) == 1);
947947
CHECK(secp256k1_keypair_xonly_pub(CTX, &internal_pk, NULL, &keypair) == 1);
948948
/* In actual taproot the tweak would be hash of internal_pk */
@@ -952,7 +952,7 @@ static void test_schnorrsig_taproot(void) {
952952
CHECK(secp256k1_xonly_pubkey_serialize(CTX, output_pk_bytes, &output_pk) == 1);
953953

954954
/* Key spend */
955-
secp256k1_testrand256(msg);
955+
testrand256(msg);
956956
CHECK(secp256k1_schnorrsig_sign32(CTX, sig, msg, &keypair, NULL) == 1);
957957
/* Verify key spend */
958958
CHECK(secp256k1_xonly_pubkey_parse(CTX, &output_pk, output_pk_bytes) == 1);

‎src/secp256k1/src/secp256k1.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ const secp256k1_context *secp256k1_context_no_precomp = &secp256k1_context_stati
7676

7777
/* Helper function that determines if a context is proper, i.e., is not the static context or a copy thereof.
7878
*
79-
* This is intended for "context" functions such as secp256k1_context_clone. Function which need specific
79+
* This is intended for "context" functions such as secp256k1_context_clone. Functions that need specific
8080
* features of a context should still check for these features directly. For example, a function that needs
8181
* ecmult_gen should directly check for the existence of the ecmult_gen context. */
8282
static int secp256k1_context_is_proper(const secp256k1_context* ctx) {
@@ -544,7 +544,7 @@ static int secp256k1_ecdsa_sign_inner(const secp256k1_context* ctx, secp256k1_sc
544544
break;
545545
}
546546
is_nonce_valid = secp256k1_scalar_set_b32_seckey(&non, nonce32);
547-
/* The nonce is still secret here, but it being invalid is is less likely than 1:2^255. */
547+
/* The nonce is still secret here, but it being invalid is less likely than 1:2^255. */
548548
secp256k1_declassify(ctx, &is_nonce_valid, sizeof(is_nonce_valid));
549549
if (is_nonce_valid) {
550550
ret = secp256k1_ecdsa_sig_sign(&ctx->ecmult_gen_ctx, r, s, &sec, &msg, &non, recid);

‎src/secp256k1/src/testrand.h

+11-11
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,37 @@
1212
/* A non-cryptographic RNG used only for test infrastructure. */
1313

1414
/** Seed the pseudorandom number generator for testing. */
15-
SECP256K1_INLINE static void secp256k1_testrand_seed(const unsigned char *seed16);
15+
SECP256K1_INLINE static void testrand_seed(const unsigned char *seed16);
1616

1717
/** Generate a pseudorandom number in the range [0..2**32-1]. */
18-
SECP256K1_INLINE static uint32_t secp256k1_testrand32(void);
18+
SECP256K1_INLINE static uint32_t testrand32(void);
1919

2020
/** Generate a pseudorandom number in the range [0..2**64-1]. */
21-
SECP256K1_INLINE static uint64_t secp256k1_testrand64(void);
21+
SECP256K1_INLINE static uint64_t testrand64(void);
2222

2323
/** Generate a pseudorandom number in the range [0..2**bits-1]. Bits must be 1 or
2424
* more. */
25-
SECP256K1_INLINE static uint64_t secp256k1_testrand_bits(int bits);
25+
SECP256K1_INLINE static uint64_t testrand_bits(int bits);
2626

2727
/** Generate a pseudorandom number in the range [0..range-1]. */
28-
static uint32_t secp256k1_testrand_int(uint32_t range);
28+
static uint32_t testrand_int(uint32_t range);
2929

3030
/** Generate a pseudorandom 32-byte array. */
31-
static void secp256k1_testrand256(unsigned char *b32);
31+
static void testrand256(unsigned char *b32);
3232

3333
/** Generate a pseudorandom 32-byte array with long sequences of zero and one bits. */
34-
static void secp256k1_testrand256_test(unsigned char *b32);
34+
static void testrand256_test(unsigned char *b32);
3535

3636
/** Generate pseudorandom bytes with long sequences of zero and one bits. */
37-
static void secp256k1_testrand_bytes_test(unsigned char *bytes, size_t len);
37+
static void testrand_bytes_test(unsigned char *bytes, size_t len);
3838

3939
/** Flip a single random bit in a byte array */
40-
static void secp256k1_testrand_flip(unsigned char *b, size_t len);
40+
static void testrand_flip(unsigned char *b, size_t len);
4141

4242
/** Initialize the test RNG using (hex encoded) array up to 16 bytes, or randomly if hexseed is NULL. */
43-
static void secp256k1_testrand_init(const char* hexseed);
43+
static void testrand_init(const char* hexseed);
4444

4545
/** Print final test information. */
46-
static void secp256k1_testrand_finish(void);
46+
static void testrand_finish(void);
4747

4848
#endif /* SECP256K1_TESTRAND_H */

‎src/secp256k1/src/testrand_impl.h

+21-21
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
static uint64_t secp256k1_test_state[4];
1919

20-
SECP256K1_INLINE static void secp256k1_testrand_seed(const unsigned char *seed16) {
20+
SECP256K1_INLINE static void testrand_seed(const unsigned char *seed16) {
2121
static const unsigned char PREFIX[19] = "secp256k1 test init";
2222
unsigned char out32[32];
2323
secp256k1_sha256 hash;
@@ -40,7 +40,7 @@ SECP256K1_INLINE static uint64_t rotl(const uint64_t x, int k) {
4040
return (x << k) | (x >> (64 - k));
4141
}
4242

43-
SECP256K1_INLINE static uint64_t secp256k1_testrand64(void) {
43+
SECP256K1_INLINE static uint64_t testrand64(void) {
4444
/* Test-only Xoshiro256++ RNG. See https://prng.di.unimi.it/ */
4545
const uint64_t result = rotl(secp256k1_test_state[0] + secp256k1_test_state[3], 23) + secp256k1_test_state[0];
4646
const uint64_t t = secp256k1_test_state[1] << 17;
@@ -53,16 +53,16 @@ SECP256K1_INLINE static uint64_t secp256k1_testrand64(void) {
5353
return result;
5454
}
5555

56-
SECP256K1_INLINE static uint64_t secp256k1_testrand_bits(int bits) {
56+
SECP256K1_INLINE static uint64_t testrand_bits(int bits) {
5757
if (bits == 0) return 0;
58-
return secp256k1_testrand64() >> (64 - bits);
58+
return testrand64() >> (64 - bits);
5959
}
6060

61-
SECP256K1_INLINE static uint32_t secp256k1_testrand32(void) {
62-
return secp256k1_testrand64() >> 32;
61+
SECP256K1_INLINE static uint32_t testrand32(void) {
62+
return testrand64() >> 32;
6363
}
6464

65-
static uint32_t secp256k1_testrand_int(uint32_t range) {
65+
static uint32_t testrand_int(uint32_t range) {
6666
uint32_t mask = 0;
6767
uint32_t range_copy;
6868
/* Reduce range by 1, changing its meaning to "maximum value". */
@@ -76,15 +76,15 @@ static uint32_t secp256k1_testrand_int(uint32_t range) {
7676
}
7777
/* Generation loop. */
7878
while (1) {
79-
uint32_t val = secp256k1_testrand64() & mask;
79+
uint32_t val = testrand64() & mask;
8080
if (val <= range) return val;
8181
}
8282
}
8383

84-
static void secp256k1_testrand256(unsigned char *b32) {
84+
static void testrand256(unsigned char *b32) {
8585
int i;
8686
for (i = 0; i < 4; ++i) {
87-
uint64_t val = secp256k1_testrand64();
87+
uint64_t val = testrand64();
8888
b32[0] = val;
8989
b32[1] = val >> 8;
9090
b32[2] = val >> 16;
@@ -97,14 +97,14 @@ static void secp256k1_testrand256(unsigned char *b32) {
9797
}
9898
}
9999

100-
static void secp256k1_testrand_bytes_test(unsigned char *bytes, size_t len) {
100+
static void testrand_bytes_test(unsigned char *bytes, size_t len) {
101101
size_t bits = 0;
102102
memset(bytes, 0, len);
103103
while (bits < len * 8) {
104104
int now;
105105
uint32_t val;
106-
now = 1 + (secp256k1_testrand_bits(6) * secp256k1_testrand_bits(5) + 16) / 31;
107-
val = secp256k1_testrand_bits(1);
106+
now = 1 + (testrand_bits(6) * testrand_bits(5) + 16) / 31;
107+
val = testrand_bits(1);
108108
while (now > 0 && bits < len * 8) {
109109
bytes[bits / 8] |= val << (bits % 8);
110110
now--;
@@ -113,15 +113,15 @@ static void secp256k1_testrand_bytes_test(unsigned char *bytes, size_t len) {
113113
}
114114
}
115115

116-
static void secp256k1_testrand256_test(unsigned char *b32) {
117-
secp256k1_testrand_bytes_test(b32, 32);
116+
static void testrand256_test(unsigned char *b32) {
117+
testrand_bytes_test(b32, 32);
118118
}
119119

120-
static void secp256k1_testrand_flip(unsigned char *b, size_t len) {
121-
b[secp256k1_testrand_int(len)] ^= (1 << secp256k1_testrand_bits(3));
120+
static void testrand_flip(unsigned char *b, size_t len) {
121+
b[testrand_int(len)] ^= (1 << testrand_bits(3));
122122
}
123123

124-
static void secp256k1_testrand_init(const char* hexseed) {
124+
static void testrand_init(const char* hexseed) {
125125
unsigned char seed16[16] = {0};
126126
if (hexseed && strlen(hexseed) != 0) {
127127
int pos = 0;
@@ -155,12 +155,12 @@ static void secp256k1_testrand_init(const char* hexseed) {
155155
}
156156

157157
printf("random seed = %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n", seed16[0], seed16[1], seed16[2], seed16[3], seed16[4], seed16[5], seed16[6], seed16[7], seed16[8], seed16[9], seed16[10], seed16[11], seed16[12], seed16[13], seed16[14], seed16[15]);
158-
secp256k1_testrand_seed(seed16);
158+
testrand_seed(seed16);
159159
}
160160

161-
static void secp256k1_testrand_finish(void) {
161+
static void testrand_finish(void) {
162162
unsigned char run32[32];
163-
secp256k1_testrand256(run32);
163+
testrand256(run32);
164164
printf("random run = %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n", run32[0], run32[1], run32[2], run32[3], run32[4], run32[5], run32[6], run32[7], run32[8], run32[9], run32[10], run32[11], run32[12], run32[13], run32[14], run32[15]);
165165
}
166166

‎src/secp256k1/src/tests.c

+171-287
Large diffs are not rendered by default.

‎src/secp256k1/src/tests_exhaustive.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ static void test_exhaustive_ecmult(const secp256k1_ge *group, const secp256k1_ge
171171
CHECK(secp256k1_fe_equal(&tmpf, &group[(i * j) % EXHAUSTIVE_TEST_ORDER].x));
172172

173173
/* Test secp256k1_ecmult_const_xonly with all curve X coordinates, with random xd. */
174-
random_fe_non_zero(&xd);
174+
testutil_random_fe_non_zero(&xd);
175175
secp256k1_fe_mul(&xn, &xd, &group[i].x);
176176
ret = secp256k1_ecmult_const_xonly(&tmpf, &xn, &xd, &ng, 0);
177177
CHECK(ret);
@@ -375,7 +375,7 @@ int main(int argc, char** argv) {
375375
printf("test count = %i\n", count);
376376

377377
/* find random seed */
378-
secp256k1_testrand_init(argc > 2 ? argv[2] : NULL);
378+
testrand_init(argc > 2 ? argv[2] : NULL);
379379

380380
/* set up split processing */
381381
if (argc > 4) {
@@ -394,8 +394,8 @@ int main(int argc, char** argv) {
394394

395395
while (count--) {
396396
/* Build context */
397-
ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
398-
secp256k1_testrand256(rand32);
397+
ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
398+
testrand256(rand32);
399399
CHECK(secp256k1_context_randomize(ctx, rand32));
400400

401401
/* Generate the entire group */
@@ -408,7 +408,7 @@ int main(int argc, char** argv) {
408408
/* Set a different random z-value for each Jacobian point, except z=1
409409
is used in the last iteration. */
410410
secp256k1_fe z;
411-
random_fe(&z);
411+
testutil_random_fe(&z);
412412
secp256k1_gej_rescale(&groupj[i], &z);
413413
}
414414

@@ -459,7 +459,7 @@ int main(int argc, char** argv) {
459459
secp256k1_context_destroy(ctx);
460460
}
461461

462-
secp256k1_testrand_finish();
462+
testrand_finish();
463463

464464
printf("no problems found\n");
465465
return 0;

‎src/testutil.h

+117-4
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,136 @@
77
#define SECP256K1_TESTUTIL_H
88

99
#include "field.h"
10+
#include "group.h"
1011
#include "testrand.h"
1112
#include "util.h"
1213

13-
static void random_fe(secp256k1_fe *x) {
14+
static void testutil_random_fe(secp256k1_fe *x) {
1415
unsigned char bin[32];
1516
do {
16-
secp256k1_testrand256(bin);
17+
testrand256(bin);
1718
if (secp256k1_fe_set_b32_limit(x, bin)) {
1819
return;
1920
}
2021
} while(1);
2122
}
2223

23-
static void random_fe_non_zero(secp256k1_fe *nz) {
24+
static void testutil_random_fe_non_zero(secp256k1_fe *nz) {
2425
do {
25-
random_fe(nz);
26+
testutil_random_fe(nz);
2627
} while (secp256k1_fe_is_zero(nz));
2728
}
2829

30+
static void testutil_random_fe_magnitude(secp256k1_fe *fe, int m) {
31+
secp256k1_fe zero;
32+
int n = testrand_int(m + 1);
33+
secp256k1_fe_normalize(fe);
34+
if (n == 0) {
35+
return;
36+
}
37+
secp256k1_fe_clear(&zero);
38+
secp256k1_fe_negate(&zero, &zero, 0);
39+
secp256k1_fe_mul_int_unchecked(&zero, n - 1);
40+
secp256k1_fe_add(fe, &zero);
41+
#ifdef VERIFY
42+
CHECK(fe->magnitude == n);
43+
#endif
44+
}
45+
46+
static void testutil_random_fe_test(secp256k1_fe *x) {
47+
unsigned char bin[32];
48+
do {
49+
testrand256_test(bin);
50+
if (secp256k1_fe_set_b32_limit(x, bin)) {
51+
return;
52+
}
53+
} while(1);
54+
}
55+
56+
static void testutil_random_fe_non_zero_test(secp256k1_fe *fe) {
57+
do {
58+
testutil_random_fe_test(fe);
59+
} while(secp256k1_fe_is_zero(fe));
60+
}
61+
62+
static void testutil_random_ge_x_magnitude(secp256k1_ge *ge) {
63+
testutil_random_fe_magnitude(&ge->x, SECP256K1_GE_X_MAGNITUDE_MAX);
64+
}
65+
66+
static void testutil_random_ge_y_magnitude(secp256k1_ge *ge) {
67+
testutil_random_fe_magnitude(&ge->y, SECP256K1_GE_Y_MAGNITUDE_MAX);
68+
}
69+
70+
static void testutil_random_gej_x_magnitude(secp256k1_gej *gej) {
71+
testutil_random_fe_magnitude(&gej->x, SECP256K1_GEJ_X_MAGNITUDE_MAX);
72+
}
73+
74+
static void testutil_random_gej_y_magnitude(secp256k1_gej *gej) {
75+
testutil_random_fe_magnitude(&gej->y, SECP256K1_GEJ_Y_MAGNITUDE_MAX);
76+
}
77+
78+
static void testutil_random_gej_z_magnitude(secp256k1_gej *gej) {
79+
testutil_random_fe_magnitude(&gej->z, SECP256K1_GEJ_Z_MAGNITUDE_MAX);
80+
}
81+
82+
static void testutil_random_ge_test(secp256k1_ge *ge) {
83+
secp256k1_fe fe;
84+
do {
85+
testutil_random_fe_test(&fe);
86+
if (secp256k1_ge_set_xo_var(ge, &fe, testrand_bits(1))) {
87+
secp256k1_fe_normalize(&ge->y);
88+
break;
89+
}
90+
} while(1);
91+
ge->infinity = 0;
92+
}
93+
94+
static void testutil_random_ge_jacobian_test(secp256k1_gej *gej, const secp256k1_ge *ge) {
95+
secp256k1_fe z2, z3;
96+
testutil_random_fe_non_zero_test(&gej->z);
97+
secp256k1_fe_sqr(&z2, &gej->z);
98+
secp256k1_fe_mul(&z3, &z2, &gej->z);
99+
secp256k1_fe_mul(&gej->x, &ge->x, &z2);
100+
secp256k1_fe_mul(&gej->y, &ge->y, &z3);
101+
gej->infinity = ge->infinity;
102+
}
103+
104+
static void testutil_random_gej_test(secp256k1_gej *gej) {
105+
secp256k1_ge ge;
106+
testutil_random_ge_test(&ge);
107+
testutil_random_ge_jacobian_test(gej, &ge);
108+
}
109+
110+
static void testutil_random_scalar_order_test(secp256k1_scalar *num) {
111+
do {
112+
unsigned char b32[32];
113+
int overflow = 0;
114+
testrand256_test(b32);
115+
secp256k1_scalar_set_b32(num, b32, &overflow);
116+
if (overflow || secp256k1_scalar_is_zero(num)) {
117+
continue;
118+
}
119+
break;
120+
} while(1);
121+
}
122+
123+
static void testutil_random_scalar_order(secp256k1_scalar *num) {
124+
do {
125+
unsigned char b32[32];
126+
int overflow = 0;
127+
testrand256(b32);
128+
secp256k1_scalar_set_b32(num, b32, &overflow);
129+
if (overflow || secp256k1_scalar_is_zero(num)) {
130+
continue;
131+
}
132+
break;
133+
} while(1);
134+
}
135+
136+
static void testutil_random_scalar_order_b32(unsigned char *b32) {
137+
secp256k1_scalar num;
138+
testutil_random_scalar_order(&num);
139+
secp256k1_scalar_get_b32(b32, &num);
140+
}
141+
29142
#endif /* SECP256K1_TESTUTIL_H */

0 commit comments

Comments
 (0)
Please sign in to comment.