Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ build-aux/
*.lo
*.o
*~
*.gcov
*.gcda
*.gcno
*.log
*.trs
src/libsecp256k1-config.h
src/libsecp256k1-config.h.in
m4/libtool.m4
Expand Down
9 changes: 9 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ tests_CPPFLAGS = -DVERIFY $(SECP_INCLUDES) $(SECP_TEST_INCLUDES)
tests_LDADD = $(SECP_LIBS) $(SECP_TEST_LIBS)
tests_LDFLAGS = -static
TESTS = tests
if USE_GCOV
tests_CPPFLAGS += $(COVERAGE_CPPFLAGS) -DDETERMINISTIC_TESTS_ONLY
tests_LDFLAGS += $(COVERAGE_LDFLAGS)
TESTS += gcov
gcov.log:
@gcov -b src/tests-tests.gcda -p > gcov.log
@touch gcov.trs
MOSTLYCLEANFILES = src/*.gcda src/*.gcno *.gcov
endif
endif

EXTRA_DIST = autogen.sh
22 changes: 20 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ if test x"$ac_cv_prog_cc_c89" = x"no"; then
fi

case $host in
*mingw*)
*mingw* | *darwin*)
use_pkgconfig=no
;;
*)
Expand Down Expand Up @@ -91,6 +91,11 @@ AC_ARG_ENABLE(tests,
[use_tests=$enableval],
[use_tests=yes])

AC_ARG_ENABLE(gcov,
AS_HELP_STRING([--enable-gcov],[enable coverage testing (default is no)]),
[use_gcov=$enableval],
[use_gcov=no])

AC_ARG_ENABLE(endomorphism,
AS_HELP_STRING([--enable-endomorphism],[enable endomorphism (default is no)]),
[use_endomorphism=$enableval],
Expand Down Expand Up @@ -280,6 +285,19 @@ case $set_scalar in
;;
esac

if test x"$use_gcov" != x"no"; then
use_tests=yes
changequote({,})
CFLAGS=`echo "$CFLAGS" | $SED -e 's/-O[0-9]*//g'`
CPPFLAGS=`echo "$CPPFLAGS" | $SED -e 's/-O[0-9]*//g'`
changequote([,])
COVERAGE_CFLAGS="-O0 --coverage"
COVERAGE_CPPFLAGS="-O0 --coverage"
COVERAGE_LDFLAGS="--coverage"
AC_SUBST(COVERAGE_CPPFLAGS)
AC_SUBST(COVERAGE_LDFLAGS)
fi

if test x"$use_tests" = x"yes"; then
SECP_OPENSSL_CHECK
if test x"$has_openssl_ec" = x"yes"; then
Expand All @@ -292,7 +310,6 @@ if test x"$use_tests" = x"yes"; then
SECP_TEST_LIBS="$SECP_TEST_LIBS -lgdi32"
;;
esac

fi
fi

Expand All @@ -318,6 +335,7 @@ AC_SUBST(SECP_LIBS)
AC_SUBST(SECP_TEST_LIBS)
AC_SUBST(SECP_TEST_INCLUDES)
AM_CONDITIONAL([USE_TESTS], [test x"$use_tests" != x"no"])
AM_CONDITIONAL([USE_GCOV], [test x"$use_gcov" != x"no"])
AM_CONDITIONAL([USE_BENCHMARK], [test x"$use_benchmark" = x"yes"])

dnl make sure nothing new is exported so that we don't break the cache
Expand Down
12 changes: 8 additions & 4 deletions src/testrand.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@

/* A non-cryptographic RNG used only for test infrastructure. */

typedef struct {
uint32_t Rz, Rw;
} secp256k1_rand_t;

/** Seed the pseudorandom number generator for testing. */
SECP256K1_INLINE static void secp256k1_rand_seed(uint64_t v);
SECP256K1_INLINE static void secp256k1_rand_seed(secp256k1_rand_t *state, uint64_t v);

/** Generate a pseudorandom 32-bit number. */
static uint32_t secp256k1_rand32(void);
static uint32_t secp256k1_rand32(secp256k1_rand_t *state);

/** Generate a pseudorandom 32-byte array. */
static void secp256k1_rand256(unsigned char *b32);
static void secp256k1_rand256(secp256k1_rand_t *state, unsigned char *b32);

/** Generate a pseudorandom 32-byte array with long sequences of zero and one bits. */
static void secp256k1_rand256_test(unsigned char *b32);
static void secp256k1_rand256_test(secp256k1_rand_t *state, unsigned char *b32);

#endif
34 changes: 16 additions & 18 deletions src/testrand_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,44 @@

#include "testrand.h"

static uint32_t secp256k1_Rz = 11, secp256k1_Rw = 11;

SECP256K1_INLINE static void secp256k1_rand_seed(uint64_t v) {
secp256k1_Rz = v >> 32;
secp256k1_Rw = v;
SECP256K1_INLINE static void secp256k1_rand_seed(secp256k1_rand_t *state, uint64_t v) {
state->Rz = v >> 32;
state->Rw = v;

/* There are two seeds with short (length 1) cycles for the Rz PRNG. */
if (secp256k1_Rz == 0 || secp256k1_Rz == 0x9068ffffU) {
secp256k1_Rz = 111;
if (state->Rz == 0 || state->Rz == 0x9068ffffU) {
state->Rz = 111;
}
/* There are four seeds with short (length 1) cycles for the Rw PRNG. */
if (secp256k1_Rw == 0 || secp256k1_Rw == 0x464fffffU ||
secp256k1_Rw == 0x8c9ffffeU || secp256k1_Rw == 0xd2effffdU) {
secp256k1_Rw = 111;
if (state->Rw == 0 || state->Rw == 0x464fffffU ||
state->Rw == 0x8c9ffffeU || state->Rw == 0xd2effffdU) {
state->Rw = 111;
}
}

SECP256K1_INLINE static uint32_t secp256k1_rand32(void) {
SECP256K1_INLINE static uint32_t secp256k1_rand32(secp256k1_rand_t *state) {
/* MWC PRNG for tests. */
secp256k1_Rz = 36969 * (secp256k1_Rz & 0xFFFF) + (secp256k1_Rz >> 16);
secp256k1_Rw = 18000 * (secp256k1_Rw & 0xFFFF) + (secp256k1_Rw >> 16);
return (secp256k1_Rw << 16) + (secp256k1_Rw >> 16) + secp256k1_Rz;
state->Rz = 36969 * (state->Rz & 0xFFFF) + (state->Rz >> 16);
state->Rw = 18000 * (state->Rw & 0xFFFF) + (state->Rw >> 16);
return (state->Rw << 16) + (state->Rw >> 16) + state->Rz;
}

static void secp256k1_rand256(unsigned char *b32) {
static void secp256k1_rand256(secp256k1_rand_t *state, unsigned char *b32) {
int i;
for (i = 0; i < 8; i++) {
uint32_t r = secp256k1_rand32();
uint32_t r = secp256k1_rand32(state);
b32[i*4 + 0] = (r >> 0) & 0xFF;
b32[i*4 + 1] = (r >> 8) & 0xFF;
b32[i*4 + 2] = (r >> 16) & 0xFF;
b32[i*4 + 3] = (r >> 24) & 0xFF;
}
}

static void secp256k1_rand256_test(unsigned char *b32) {
static void secp256k1_rand256_test(secp256k1_rand_t *state, unsigned char *b32) {
int bits=0;
memset(b32, 0, 32);
while (bits < 256) {
uint32_t ent = secp256k1_rand32();
uint32_t ent = secp256k1_rand32(state);
int now = 1 + ((ent % 64)*((ent >> 6) % 32)+16)/31;
uint32_t val = 1 & (ent >> 11);
while (now > 0 && bits < 256) {
Expand Down
Loading