7#ifndef SECP256K1_UTIL_H
8#define SECP256K1_UTIL_H
17#define DEBUG_CONFIG_MSG(x) "DEBUG_CONFIG: " x
18#define DEBUG_CONFIG_DEF(x) DEBUG_CONFIG_MSG(#x "=" STR(x))
21 void (*fn)(
const char *text,
void* data);
26 cb->
fn(text, (
void*)cb->
data);
29#ifndef USE_EXTERNAL_DEFAULT_CALLBACKS
32 fprintf(stderr,
"[libsecp256k1] illegal argument: %s\n", str);
37 fprintf(stderr,
"[libsecp256k1] internal consistency check failed: %s\n", str);
57#define TEST_FAILURE(msg) do { \
58 fprintf(stderr, "%s\n", msg); \
62#define TEST_FAILURE(msg) do { \
63 fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, msg); \
68#if SECP256K1_GNUC_PREREQ(3, 0)
69#define EXPECT(x,c) __builtin_expect((x),(c))
71#define EXPECT(x,c) (x)
75#define CHECK(cond) do { \
76 if (EXPECT(!(cond), 0)) { \
77 TEST_FAILURE("test condition failed"); \
81#define CHECK(cond) do { \
82 if (EXPECT(!(cond), 0)) { \
83 TEST_FAILURE("test condition failed: " #cond); \
90#define VERIFY_CHECK(check)
91#define VERIFY_SETUP(stmt)
93#define VERIFY_CHECK CHECK
94#define VERIFY_SETUP(stmt) do { stmt; } while(0)
96#define VERIFY_CHECK(cond) do { (void)(cond); } while(0)
97#define VERIFY_SETUP(stmt)
101 void *ret = malloc(size);
109 void *ret = realloc(ptr, size);
116#if defined(__BIGGEST_ALIGNMENT__)
117#define ALIGNMENT __BIGGEST_ALIGNMENT__
125#define ROUND_TO_ALIGN(size) ((((size) + ALIGNMENT - 1) / ALIGNMENT) * ALIGNMENT)
128#if defined(SECP256K1_BUILD) && defined(VERIFY)
129# define SECP256K1_RESTRICT
131# if (!defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L) )
132# if SECP256K1_GNUC_PREREQ(3,0)
133# define SECP256K1_RESTRICT __restrict__
134# elif (defined(_MSC_VER) && _MSC_VER >= 1400)
135# define SECP256K1_RESTRICT __restrict
137# define SECP256K1_RESTRICT
140# define SECP256K1_RESTRICT restrict
145# define I64FORMAT "I64d"
146# define I64uFORMAT "I64u"
148# define I64FORMAT "lld"
149# define I64uFORMAT "llu"
153# define SECP256K1_GNUC_EXT __extension__
155# define SECP256K1_GNUC_EXT
160 unsigned char *p = (
unsigned char *)s;
164 volatile int vflag = flag;
165 unsigned char mask = -(
unsigned char) vflag;
179 const unsigned char *p1 = s1, *p2 = s2;
182 for (i = 0; i < n; i++) {
183 int diff = p1[i] - p2[i];
193 unsigned int mask0, mask1, r_masked, a_masked;
197 volatile int vflag = flag;
202 mask0 = (
unsigned int)vflag + ~0u;
204 r_masked = ((
unsigned int)*r & mask0);
205 a_masked = ((
unsigned int)*a & mask1);
207 *r = (int)(r_masked | a_masked);
210#if defined(USE_FORCE_WIDEMUL_INT128_STRUCT)
212# define SECP256K1_WIDEMUL_INT128 1
213# define SECP256K1_INT128_STRUCT 1
214#elif defined(USE_FORCE_WIDEMUL_INT128)
216# define SECP256K1_WIDEMUL_INT128 1
217# define SECP256K1_INT128_NATIVE 1
218#elif defined(USE_FORCE_WIDEMUL_INT64)
220# define SECP256K1_WIDEMUL_INT64 1
221#elif defined(UINT128_MAX) || defined(__SIZEOF_INT128__)
223# define SECP256K1_WIDEMUL_INT128 1
224# define SECP256K1_INT128_NATIVE 1
225#elif defined(_MSC_VER) && (defined(_M_X64) || defined(_M_ARM64))
228# define SECP256K1_WIDEMUL_INT128 1
229# define SECP256K1_INT128_STRUCT 1
230#elif SIZE_MAX > 0xffffffff
234# define SECP256K1_WIDEMUL_INT128 1
235# define SECP256K1_INT128_STRUCT 1
238# define SECP256K1_WIDEMUL_INT64 1
242#define __has_builtin(x) 0
249 static const uint8_t debruijn[32] = {
250 0x00, 0x01, 0x02, 0x18, 0x03, 0x13, 0x06, 0x19, 0x16, 0x04, 0x14, 0x0A,
251 0x10, 0x07, 0x0C, 0x1A, 0x1F, 0x17, 0x12, 0x05, 0x15, 0x09, 0x0F, 0x0B,
252 0x1E, 0x11, 0x08, 0x0E, 0x1D, 0x0D, 0x1C, 0x1B
254 return debruijn[(uint32_t)((x & -x) * 0x04D7651FU) >> 27];
261 static const uint8_t debruijn[64] = {
262 0, 1, 2, 53, 3, 7, 54, 27, 4, 38, 41, 8, 34, 55, 48, 28,
263 62, 5, 39, 46, 44, 42, 22, 9, 24, 35, 59, 56, 49, 18, 29, 11,
264 63, 52, 6, 26, 37, 40, 33, 47, 61, 45, 43, 21, 23, 58, 17, 10,
265 51, 25, 36, 32, 60, 20, 57, 16, 50, 31, 19, 15, 30, 14, 13, 12
267 return debruijn[(uint64_t)((x & -x) * 0x022FDD63CC95386DU) >> 58];
273#if (__has_builtin(__builtin_ctz) || SECP256K1_GNUC_PREREQ(3,4))
275 if (((
unsigned)UINT32_MAX) == UINT32_MAX) {
276 return __builtin_ctz(x);
279#if (__has_builtin(__builtin_ctzl) || SECP256K1_GNUC_PREREQ(3,4))
281 return __builtin_ctzl(x);
291#if (__has_builtin(__builtin_ctzl) || SECP256K1_GNUC_PREREQ(3,4))
293 if (((
unsigned long)UINT64_MAX) == UINT64_MAX) {
294 return __builtin_ctzl(x);
297#if (__has_builtin(__builtin_ctzll) || SECP256K1_GNUC_PREREQ(3,4))
299 return __builtin_ctzll(x);
308 return (uint32_t)p[0] << 24 |
309 (uint32_t)p[1] << 16 |
310 (uint32_t)p[2] << 8 |
static SECP256K1_INLINE int secp256k1_ctz64_var(uint64_t x)
static SECP256K1_INLINE int secp256k1_memcmp_var(const void *s1, const void *s2, size_t n)
Semantics like memcmp.
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 void secp256k1_default_error_callback_fn(const char *str, void *data)
static const secp256k1_callback default_error_callback
static SECP256K1_INLINE uint32_t secp256k1_read_be32(const unsigned char *p)
static SECP256K1_INLINE int secp256k1_ctz32_var(uint32_t x)
static SECP256K1_INLINE void secp256k1_write_be32(unsigned char *p, uint32_t x)
static void secp256k1_default_illegal_callback_fn(const char *str, void *data)
static SECP256K1_INLINE int secp256k1_ctz64_var_debruijn(uint64_t x)
static SECP256K1_INLINE void * checked_realloc(const secp256k1_callback *cb, void *ptr, size_t size)
#define VERIFY_CHECK(cond)
static SECP256K1_INLINE int secp256k1_ctz32_var_debruijn(uint32_t x)
static SECP256K1_INLINE void * checked_malloc(const secp256k1_callback *cb, size_t size)
static SECP256K1_INLINE void secp256k1_memczero(void *s, size_t len, int flag)
static SECP256K1_INLINE void secp256k1_callback_call(const secp256k1_callback *const cb, const char *const text)
static const secp256k1_callback default_illegal_callback
void(* fn)(const char *text, void *data)