7 #ifndef SECP256K1_UTIL_H 8 #define SECP256K1_UTIL_H 10 #if defined HAVE_CONFIG_H 20 void (*fn)(
const char *text,
void* data);
25 cb->
fn(text, (
void*)cb->
data);
28 #ifndef USE_EXTERNAL_DEFAULT_CALLBACKS 31 fprintf(stderr,
"[libsecp256k1] illegal argument: %s\n", str);
36 fprintf(stderr,
"[libsecp256k1] internal consistency check failed: %s\n", str);
56 #define TEST_FAILURE(msg) do { \ 57 fprintf(stderr, "%s\n", msg); \ 61 #define TEST_FAILURE(msg) do { \ 62 fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, msg); \ 67 #if SECP256K1_GNUC_PREREQ(3, 0) 68 #define EXPECT(x,c) __builtin_expect((x),(c)) 70 #define EXPECT(x,c) (x) 74 #define CHECK(cond) do { \ 75 if (EXPECT(!(cond), 0)) { \ 76 TEST_FAILURE("test condition failed"); \ 80 #define CHECK(cond) do { \ 81 if (EXPECT(!(cond), 0)) { \ 82 TEST_FAILURE("test condition failed: " #cond); \ 89 #define VERIFY_CHECK(check) 90 #define VERIFY_SETUP(stmt) 92 #define VERIFY_CHECK CHECK 93 #define VERIFY_SETUP(stmt) do { stmt; } while(0) 95 #define VERIFY_CHECK(cond) do { (void)(cond); } while(0) 96 #define VERIFY_SETUP(stmt) 100 #if !defined(VG_CHECK) 101 # if defined(VALGRIND) 102 # include <valgrind/memcheck.h> 103 # define VG_UNDEF(x,y) VALGRIND_MAKE_MEM_UNDEFINED((x),(y)) 104 # define VG_CHECK(x,y) VALGRIND_CHECK_MEM_IS_DEFINED((x),(y)) 106 # define VG_UNDEF(x,y) 107 # define VG_CHECK(x,y) 113 #define VG_CHECK_VERIFY(x,y) VG_CHECK((x), (y)) 115 #define VG_CHECK_VERIFY(x,y) 119 void *
ret = malloc(size);
127 void *
ret = realloc(ptr, size);
134 #if defined(__BIGGEST_ALIGNMENT__) 135 #define ALIGNMENT __BIGGEST_ALIGNMENT__ 143 #define ROUND_TO_ALIGN(size) ((((size) + ALIGNMENT - 1) / ALIGNMENT) * ALIGNMENT) 146 #if defined(SECP256K1_BUILD) && defined(VERIFY) 147 # define SECP256K1_RESTRICT 149 # if (!defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L) ) 150 # if SECP256K1_GNUC_PREREQ(3,0) 151 # define SECP256K1_RESTRICT __restrict__ 152 # elif (defined(_MSC_VER) && _MSC_VER >= 1400) 153 # define SECP256K1_RESTRICT __restrict 155 # define SECP256K1_RESTRICT 158 # define SECP256K1_RESTRICT restrict 163 # define I64FORMAT "I64d" 164 # define I64uFORMAT "I64u" 166 # define I64FORMAT "lld" 167 # define I64uFORMAT "llu" 170 #if defined(__GNUC__) 171 # define SECP256K1_GNUC_EXT __extension__ 173 # define SECP256K1_GNUC_EXT 178 unsigned char *p = (
unsigned char *)s;
182 volatile int vflag = flag;
183 unsigned char mask = -(
unsigned char) vflag;
197 const unsigned char *p1 = s1, *p2 = s2;
200 for (i = 0; i < n; i++) {
201 int diff = p1[i] - p2[i];
211 unsigned int mask0, mask1, r_masked, a_masked;
215 volatile int vflag = flag;
220 mask0 = (
unsigned int)vflag + ~0u;
222 r_masked = ((
unsigned int)*r & mask0);
223 a_masked = ((
unsigned int)*a & mask1);
225 *r = (int)(r_masked | a_masked);
231 #if defined(USE_FORCE_WIDEMUL_INT128) 232 # define SECP256K1_WIDEMUL_INT128 1 233 #elif defined(USE_FORCE_WIDEMUL_INT64) 234 # define SECP256K1_WIDEMUL_INT64 1 235 #elif defined(UINT128_MAX) || defined(__SIZEOF_INT128__) 236 # define SECP256K1_WIDEMUL_INT128 1 238 # define SECP256K1_WIDEMUL_INT64 1 240 #if defined(SECP256K1_WIDEMUL_INT128) 241 # if !defined(UINT128_MAX) && defined(__SIZEOF_INT128__) 244 #define UINT128_MAX ((uint128_t)(-1)) 245 #define INT128_MAX ((int128_t)(UINT128_MAX >> 1)) 246 #define INT128_MIN (-INT128_MAX - 1) 251 #ifndef __has_builtin 252 #define __has_builtin(x) 0 259 static const uint8_t debruijn[32] = {
260 0x00, 0x01, 0x02, 0x18, 0x03, 0x13, 0x06, 0x19, 0x16, 0x04, 0x14, 0x0A,
261 0x10, 0x07, 0x0C, 0x1A, 0x1F, 0x17, 0x12, 0x05, 0x15, 0x09, 0x0F, 0x0B,
262 0x1E, 0x11, 0x08, 0x0E, 0x1D, 0x0D, 0x1C, 0x1B
264 return debruijn[((x & -x) * 0x04D7651F) >> 27];
271 static const uint8_t debruijn[64] = {
272 0, 1, 2, 53, 3, 7, 54, 27, 4, 38, 41, 8, 34, 55, 48, 28,
273 62, 5, 39, 46, 44, 42, 22, 9, 24, 35, 59, 56, 49, 18, 29, 11,
274 63, 52, 6, 26, 37, 40, 33, 47, 61, 45, 43, 21, 23, 58, 17, 10,
275 51, 25, 36, 32, 60, 20, 57, 16, 50, 31, 19, 15, 30, 14, 13, 12
277 return debruijn[((x & -x) * 0x022FDD63CC95386D) >> 58];
283 #if (__has_builtin(__builtin_ctz) || SECP256K1_GNUC_PREREQ(3,4)) 285 if (((
unsigned)UINT32_MAX) == UINT32_MAX) {
286 return __builtin_ctz(x);
289 #if (__has_builtin(__builtin_ctzl) || SECP256K1_GNUC_PREREQ(3,4)) 291 return __builtin_ctzl(x);
301 #if (__has_builtin(__builtin_ctzl) || SECP256K1_GNUC_PREREQ(3,4)) 303 if (((
unsigned long)UINT64_MAX) == UINT64_MAX) {
304 return __builtin_ctzl(x);
307 #if (__has_builtin(__builtin_ctzll) || SECP256K1_GNUC_PREREQ(3,4)) 309 return __builtin_ctzll(x);
318 return (uint32_t)p[0] << 24 |
319 (uint32_t)p[1] << 16 |
320 (uint32_t)p[2] << 8 |
#define VERIFY_CHECK(cond)
static SECP256K1_INLINE int secp256k1_ctz32_var(uint32_t x)
static SECP256K1_INLINE uint32_t secp256k1_read_be32(const unsigned char *p)
void(* fn)(const char *text, void *data)
static void secp256k1_default_illegal_callback_fn(const char *str, void *data)
#define SECP256K1_GNUC_EXT
static SECP256K1_INLINE void secp256k1_write_be32(unsigned char *p, uint32_t x)
static SECP256K1_INLINE int secp256k1_ctz64_var(uint64_t x)
static SECP256K1_INLINE int secp256k1_ctz64_var_debruijn(uint64_t x)
static SECP256K1_INLINE void secp256k1_memczero(void *s, size_t len, int flag)
static SECP256K1_INLINE void * checked_realloc(const secp256k1_callback *cb, void *ptr, size_t size)
static SECP256K1_INLINE int secp256k1_memcmp_var(const void *s1, const void *s2, size_t n)
Semantics like memcmp.
static void secp256k1_default_error_callback_fn(const char *str, void *data)
static const secp256k1_callback default_illegal_callback
static SECP256K1_INLINE void secp256k1_callback_call(const secp256k1_callback *const cb, const char *const text)
static SECP256K1_INLINE int secp256k1_ctz32_var_debruijn(uint32_t x)
static SECP256K1_INLINE void secp256k1_int_cmov(int *r, const int *a, int flag)
If flag is true, set *r equal to *a; otherwise leave it.
static SECP256K1_INLINE void * checked_malloc(const secp256k1_callback *cb, size_t size)
static const secp256k1_callback default_error_callback