7#ifndef SECP256K1_UTIL_H
8#define SECP256K1_UTIL_H
10#include "../include/secp256k1.h"
25#define DEBUG_CONFIG_MSG(x) "DEBUG_CONFIG: " x
26#define DEBUG_CONFIG_DEF(x) DEBUG_CONFIG_MSG(#x "=" STR(x))
29#define PRINT_BUF(buf, len) do { \
30 printf("%s[%lu] = ", #buf, (unsigned long)len); \
31 print_buf_plain(buf, len); \
37 for (i = 0; i < len; i++) {
48# if (!defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L) )
49# if SECP256K1_GNUC_PREREQ(2,7)
50# define SECP256K1_INLINE __inline__
51# elif (defined(_MSC_VER))
52# define SECP256K1_INLINE __inline
54# define SECP256K1_INLINE
57# define SECP256K1_INLINE inline
60# if !defined(_DEBUG) && !defined(__NO_INLINE__) && !defined(__OPTIMIZE_SIZE__)
61# if defined(__OPTIMIZE__) && (SECP256K1_GNUC_PREREQ(3, 0) || defined(__clang__))
62# define SECP256K1_FORCE_INLINE SECP256K1_INLINE __attribute__((always_inline))
63# elif defined(_MSC_VER)
64# define SECP256K1_FORCE_INLINE __forceinline
67# ifndef SECP256K1_FORCE_INLINE
68# define SECP256K1_FORCE_INLINE SECP256K1_INLINE
75#define STATIC_ASSERT(expr) do { \
88#define ASSERT_INT_CONST_AND_DO(expr, stmt) do { \
99 void (*fn)(
const char *text,
void*
data);
104 cb->
fn(text, (
void*)cb->
data);
107#ifndef USE_EXTERNAL_DEFAULT_CALLBACKS
110 fprintf(stderr,
"[libsecp256k1] illegal argument: %s\n", str);
115 fprintf(stderr,
"[libsecp256k1] internal consistency check failed: %s\n", str);
135#define TEST_FAILURE(msg) do { \
136 fprintf(stderr, "%s\n", msg); \
140#define TEST_FAILURE(msg) do { \
141 fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, msg); \
146#if SECP256K1_GNUC_PREREQ(3, 0)
147#define EXPECT(x,c) __builtin_expect((x),(c))
149#define EXPECT(x,c) (x)
153#define CHECK(cond) do { \
154 if (EXPECT(!(cond), 0)) { \
155 TEST_FAILURE("test condition failed"); \
159#define CHECK(cond) do { \
160 if (EXPECT(!(cond), 0)) { \
161 TEST_FAILURE("test condition failed: " #cond); \
168#define VERIFY_CHECK CHECK
170#define VERIFY_CHECK(cond)
174 void *
ret = malloc(size);
181#if defined(__BIGGEST_ALIGNMENT__)
182#define ALIGNMENT __BIGGEST_ALIGNMENT__
191#define CEIL_DIV(x, y) (1 + ((x) - 1) / (y))
193#define ROUND_TO_ALIGN(size) (CEIL_DIV(size, ALIGNMENT) * ALIGNMENT)
195#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
198#if defined(SECP256K1_BUILD) && defined(VERIFY)
199# define SECP256K1_RESTRICT
201# if (!defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L) )
202# if SECP256K1_GNUC_PREREQ(3,0)
203# define SECP256K1_RESTRICT __restrict__
204# elif (defined(_MSC_VER) && _MSC_VER >= 1400)
205# define SECP256K1_RESTRICT __restrict
207# define SECP256K1_RESTRICT
210# define SECP256K1_RESTRICT restrict
215# define SECP256K1_GNUC_EXT __extension__
217# define SECP256K1_GNUC_EXT
222 unsigned char *p = (
unsigned char *)
s;
226 volatile int vflag = flag;
227 unsigned char mask = -(
unsigned char) vflag;
240 SecureZeroMemory(ptr, len);
241#elif defined(__GNUC__)
254 __asm__ __volatile__(
"" : :
"r"(ptr) :
"memory");
256 void *(*
volatile const volatile_memset)(
void *,
int,
size_t) = memset;
257 volatile_memset(ptr, 0, len);
283 const unsigned char *p1 = s1, *p2 = s2;
286 for (i = 0; i < n; i++) {
287 int diff = p1[i] - p2[i];
298 unsigned char acc = 0;
302 for (i = 0; i < len; i++) {
314 unsigned int mask0, mask1, r_masked, a_masked;
318 volatile int vflag = flag;
324 mask0 = (
unsigned int)vflag + ~0u;
326 r_masked = ((
unsigned int)*r & mask0);
327 a_masked = ((
unsigned int)*a & mask1);
329 *r = (int)(r_masked | a_masked);
332#if defined(USE_FORCE_WIDEMUL_INT128_STRUCT)
334# define SECP256K1_WIDEMUL_INT128 1
335# define SECP256K1_INT128_STRUCT 1
336#elif defined(USE_FORCE_WIDEMUL_INT128)
338# define SECP256K1_WIDEMUL_INT128 1
339# define SECP256K1_INT128_NATIVE 1
340#elif defined(USE_FORCE_WIDEMUL_INT64)
342# define SECP256K1_WIDEMUL_INT64 1
343#elif defined(UINT128_MAX) || defined(__SIZEOF_INT128__)
345# define SECP256K1_WIDEMUL_INT128 1
346# define SECP256K1_INT128_NATIVE 1
347#elif defined(_MSC_VER) && (defined(_M_X64) || defined(_M_ARM64))
350# define SECP256K1_WIDEMUL_INT128 1
351# define SECP256K1_INT128_STRUCT 1
352#elif SIZE_MAX > 0xffffffff
356# define SECP256K1_WIDEMUL_INT128 1
357# define SECP256K1_INT128_STRUCT 1
360# define SECP256K1_WIDEMUL_INT64 1
364#define __has_builtin(x) 0
371 static const uint8_t debruijn[32] = {
372 0x00, 0x01, 0x02, 0x18, 0x03, 0x13, 0x06, 0x19, 0x16, 0x04, 0x14, 0x0A,
373 0x10, 0x07, 0x0C, 0x1A, 0x1F, 0x17, 0x12, 0x05, 0x15, 0x09, 0x0F, 0x0B,
374 0x1E, 0x11, 0x08, 0x0E, 0x1D, 0x0D, 0x1C, 0x1B
376 return debruijn[(uint32_t)((x & -x) * 0x04D7651FU) >> 27];
383 static const uint8_t debruijn[64] = {
384 0, 1, 2, 53, 3, 7, 54, 27, 4, 38, 41, 8, 34, 55, 48, 28,
385 62, 5, 39, 46, 44, 42, 22, 9, 24, 35, 59, 56, 49, 18, 29, 11,
386 63, 52, 6, 26, 37, 40, 33, 47, 61, 45, 43, 21, 23, 58, 17, 10,
387 51, 25, 36, 32, 60, 20, 57, 16, 50, 31, 19, 15, 30, 14, 13, 12
389 return debruijn[(uint64_t)((x & -x) * 0x022FDD63CC95386DU) >> 58];
395#if (__has_builtin(__builtin_ctz) || SECP256K1_GNUC_PREREQ(3,4))
397 if (((
unsigned)UINT32_MAX) == UINT32_MAX) {
398 return __builtin_ctz(x);
401#if (__has_builtin(__builtin_ctzl) || SECP256K1_GNUC_PREREQ(3,4))
403 return __builtin_ctzl(x);
413#if (__has_builtin(__builtin_ctzl) || SECP256K1_GNUC_PREREQ(3,4))
415 if (((
unsigned long)UINT64_MAX) == UINT64_MAX) {
416 return __builtin_ctzl(x);
419#if (__has_builtin(__builtin_ctzll) || SECP256K1_GNUC_PREREQ(3,4))
421 return __builtin_ctzll(x);
430 return (uint32_t)p[0] << 24 |
431 (uint32_t)p[1] << 16 |
432 (uint32_t)p[2] << 8 |
446 return (uint64_t)p[0] << 56 |
447 (uint64_t)p[1] << 48 |
448 (uint64_t)p[2] << 40 |
449 (uint64_t)p[3] << 32 |
450 (uint64_t)p[4] << 24 |
451 (uint64_t)p[5] << 16 |
452 (uint64_t)p[6] << 8 |
474 const unsigned int mask = CHAR_BIT *
sizeof(x) - 1;
476 return (x >> (by & mask)) | (x << ((-by) & mask));
#define SECP256K1_CHECKMEM_UNDEFINE(p, len)
static SECP256K1_INLINE int secp256k1_ctz64_var(uint64_t x)
static SECP256K1_INLINE void secp256k1_memclear_explicit(void *ptr, size_t len)
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 1, set *r equal to *a; if flag is 0, leave it.
static SECP256K1_INLINE void secp256k1_memzero_explicit(void *ptr, size_t len)
static void secp256k1_default_error_callback_fn(const char *str, void *data)
static SECP256K1_INLINE int secp256k1_is_zero_array(const unsigned char *s, size_t len)
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 SECP256K1_INLINE void secp256k1_write_be64(unsigned char *p, uint64_t x)
static SECP256K1_INLINE uint32_t secp256k1_rotr32(const uint32_t x, const unsigned int by)
static void secp256k1_default_illegal_callback_fn(const char *str, void *data)
static SECP256K1_INLINE int secp256k1_ctz64_var_debruijn(uint64_t x)
static void print_buf_plain(const unsigned char *buf, size_t len)
#define VERIFY_CHECK(cond)
static SECP256K1_INLINE int secp256k1_ctz32_var_debruijn(uint32_t x)
static SECP256K1_INLINE uint64_t secp256k1_read_be64(const unsigned char *p)
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)