2020-12-17 08:33:49 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* Copyright (c) 2013, 2014 Pieter Wuille *
|
|
|
|
* Distributed under the MIT software license, see the accompanying *
|
|
|
|
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
|
|
|
|
***********************************************************************/
|
2013-05-09 15:24:32 +02:00
|
|
|
|
2017-08-26 18:44:21 +03:00
|
|
|
#ifndef SECP256K1_UTIL_H
|
|
|
|
#define SECP256K1_UTIL_H
|
2013-04-20 23:34:41 +02:00
|
|
|
|
2014-08-18 23:07:46 +02:00
|
|
|
#if defined HAVE_CONFIG_H
|
|
|
|
#include "libsecp256k1-config.h"
|
|
|
|
#endif
|
|
|
|
|
2014-10-31 03:15:25 -07:00
|
|
|
#include <stdlib.h>
|
2014-08-18 23:07:46 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
2020-01-08 14:58:28 +00:00
|
|
|
#include <limits.h>
|
2014-08-18 23:07:46 +02:00
|
|
|
|
2022-07-06 15:10:34 +02:00
|
|
|
#define STR_(x) #x
|
|
|
|
#define STR(x) STR_(x)
|
|
|
|
#define DEBUG_CONFIG_MSG(x) "DEBUG_CONFIG: " x
|
|
|
|
#define DEBUG_CONFIG_DEF(x) DEBUG_CONFIG_MSG(#x "=" STR(x))
|
|
|
|
|
2015-07-18 16:29:10 -04:00
|
|
|
typedef struct {
|
|
|
|
void (*fn)(const char *text, void* data);
|
2015-09-01 05:48:58 +00:00
|
|
|
const void* data;
|
2015-09-21 20:57:54 +02:00
|
|
|
} secp256k1_callback;
|
2015-07-18 16:29:10 -04:00
|
|
|
|
2015-09-21 20:57:54 +02:00
|
|
|
static SECP256K1_INLINE void secp256k1_callback_call(const secp256k1_callback * const cb, const char * const text) {
|
2015-09-01 05:48:58 +00:00
|
|
|
cb->fn(text, (void*)cb->data);
|
|
|
|
}
|
|
|
|
|
2021-11-08 20:16:55 +01:00
|
|
|
#ifndef USE_EXTERNAL_DEFAULT_CALLBACKS
|
|
|
|
static void secp256k1_default_illegal_callback_fn(const char* str, void* data) {
|
|
|
|
(void)data;
|
|
|
|
fprintf(stderr, "[libsecp256k1] illegal argument: %s\n", str);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
static void secp256k1_default_error_callback_fn(const char* str, void* data) {
|
|
|
|
(void)data;
|
|
|
|
fprintf(stderr, "[libsecp256k1] internal consistency check failed: %s\n", str);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
void secp256k1_default_illegal_callback_fn(const char* str, void* data);
|
|
|
|
void secp256k1_default_error_callback_fn(const char* str, void* data);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static const secp256k1_callback default_illegal_callback = {
|
|
|
|
secp256k1_default_illegal_callback_fn,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static const secp256k1_callback default_error_callback = {
|
|
|
|
secp256k1_default_error_callback_fn,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-11-17 13:16:47 +01:00
|
|
|
#ifdef DETERMINISTIC
|
|
|
|
#define TEST_FAILURE(msg) do { \
|
|
|
|
fprintf(stderr, "%s\n", msg); \
|
|
|
|
abort(); \
|
|
|
|
} while(0);
|
|
|
|
#else
|
2014-08-18 23:07:46 +02:00
|
|
|
#define TEST_FAILURE(msg) do { \
|
|
|
|
fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, msg); \
|
|
|
|
abort(); \
|
|
|
|
} while(0)
|
2014-11-17 13:16:47 +01:00
|
|
|
#endif
|
2014-08-18 23:07:46 +02:00
|
|
|
|
2018-12-17 11:46:54 +01:00
|
|
|
#if SECP256K1_GNUC_PREREQ(3, 0)
|
2014-08-18 23:07:46 +02:00
|
|
|
#define EXPECT(x,c) __builtin_expect((x),(c))
|
|
|
|
#else
|
|
|
|
#define EXPECT(x,c) (x)
|
|
|
|
#endif
|
|
|
|
|
2014-11-17 13:16:47 +01:00
|
|
|
#ifdef DETERMINISTIC
|
|
|
|
#define CHECK(cond) do { \
|
|
|
|
if (EXPECT(!(cond), 0)) { \
|
|
|
|
TEST_FAILURE("test condition failed"); \
|
|
|
|
} \
|
|
|
|
} while(0)
|
|
|
|
#else
|
2014-08-18 23:07:46 +02:00
|
|
|
#define CHECK(cond) do { \
|
|
|
|
if (EXPECT(!(cond), 0)) { \
|
|
|
|
TEST_FAILURE("test condition failed: " #cond); \
|
|
|
|
} \
|
|
|
|
} while(0)
|
2014-11-17 13:16:47 +01:00
|
|
|
#endif
|
2014-08-18 23:07:46 +02:00
|
|
|
|
2015-07-18 16:29:10 -04:00
|
|
|
/* Like assert(), but when VERIFY is defined, and side-effect safe. */
|
2016-11-26 20:34:15 +00:00
|
|
|
#if defined(COVERAGE)
|
|
|
|
#define VERIFY_CHECK(check)
|
|
|
|
#define VERIFY_SETUP(stmt)
|
|
|
|
#elif defined(VERIFY)
|
2014-08-18 23:07:46 +02:00
|
|
|
#define VERIFY_CHECK CHECK
|
2015-07-15 20:33:35 +07:00
|
|
|
#define VERIFY_SETUP(stmt) do { stmt; } while(0)
|
2014-08-18 23:07:46 +02:00
|
|
|
#else
|
2014-11-04 13:14:47 -08:00
|
|
|
#define VERIFY_CHECK(cond) do { (void)(cond); } while(0)
|
2015-07-15 20:33:35 +07:00
|
|
|
#define VERIFY_SETUP(stmt)
|
2014-08-18 23:07:46 +02:00
|
|
|
#endif
|
|
|
|
|
2020-05-20 15:12:09 +03:00
|
|
|
/* Define `VG_UNDEF` and `VG_CHECK` when VALGRIND is defined */
|
|
|
|
#if !defined(VG_CHECK)
|
|
|
|
# if defined(VALGRIND)
|
|
|
|
# include <valgrind/memcheck.h>
|
|
|
|
# define VG_UNDEF(x,y) VALGRIND_MAKE_MEM_UNDEFINED((x),(y))
|
|
|
|
# define VG_CHECK(x,y) VALGRIND_CHECK_MEM_IS_DEFINED((x),(y))
|
|
|
|
# else
|
|
|
|
# define VG_UNDEF(x,y)
|
|
|
|
# define VG_CHECK(x,y)
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Like `VG_CHECK` but on VERIFY only */
|
|
|
|
#if defined(VERIFY)
|
|
|
|
#define VG_CHECK_VERIFY(x,y) VG_CHECK((x), (y))
|
|
|
|
#else
|
|
|
|
#define VG_CHECK_VERIFY(x,y)
|
|
|
|
#endif
|
|
|
|
|
2015-09-21 20:57:54 +02:00
|
|
|
static SECP256K1_INLINE void *checked_malloc(const secp256k1_callback* cb, size_t size) {
|
2014-12-07 02:58:24 +01:00
|
|
|
void *ret = malloc(size);
|
2015-07-18 16:29:10 -04:00
|
|
|
if (ret == NULL) {
|
2015-09-21 20:57:54 +02:00
|
|
|
secp256k1_callback_call(cb, "Out of memory");
|
2015-07-18 16:29:10 -04:00
|
|
|
}
|
2014-12-07 02:58:24 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-07-22 18:03:17 +00:00
|
|
|
static SECP256K1_INLINE void *checked_realloc(const secp256k1_callback* cb, void *ptr, size_t size) {
|
|
|
|
void *ret = realloc(ptr, size);
|
|
|
|
if (ret == NULL) {
|
|
|
|
secp256k1_callback_call(cb, "Out of memory");
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-10-18 19:09:51 +02:00
|
|
|
#if defined(__BIGGEST_ALIGNMENT__)
|
|
|
|
#define ALIGNMENT __BIGGEST_ALIGNMENT__
|
|
|
|
#else
|
|
|
|
/* Using 16 bytes alignment because common architectures never have alignment
|
|
|
|
* requirements above 8 for any of the types we care about. In addition we
|
|
|
|
* leave some room because currently we don't care about a few bytes. */
|
|
|
|
#define ALIGNMENT 16
|
|
|
|
#endif
|
|
|
|
|
2021-01-25 11:43:45 -05:00
|
|
|
#define ROUND_TO_ALIGN(size) ((((size) + ALIGNMENT - 1) / ALIGNMENT) * ALIGNMENT)
|
2018-10-18 19:09:51 +02:00
|
|
|
|
2014-11-30 19:27:24 +01:00
|
|
|
/* Macro for restrict, when available and not in a VERIFY build. */
|
|
|
|
#if defined(SECP256K1_BUILD) && defined(VERIFY)
|
|
|
|
# define SECP256K1_RESTRICT
|
|
|
|
#else
|
|
|
|
# if (!defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L) )
|
|
|
|
# if SECP256K1_GNUC_PREREQ(3,0)
|
|
|
|
# define SECP256K1_RESTRICT __restrict__
|
|
|
|
# elif (defined(_MSC_VER) && _MSC_VER >= 1400)
|
|
|
|
# define SECP256K1_RESTRICT __restrict
|
|
|
|
# else
|
|
|
|
# define SECP256K1_RESTRICT
|
|
|
|
# endif
|
|
|
|
# else
|
|
|
|
# define SECP256K1_RESTRICT restrict
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2015-01-25 17:32:08 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
# define I64FORMAT "I64d"
|
|
|
|
# define I64uFORMAT "I64u"
|
|
|
|
#else
|
|
|
|
# define I64FORMAT "lld"
|
|
|
|
# define I64uFORMAT "llu"
|
|
|
|
#endif
|
|
|
|
|
2020-08-09 10:58:40 -07:00
|
|
|
#if defined(__GNUC__)
|
|
|
|
# define SECP256K1_GNUC_EXT __extension__
|
|
|
|
#else
|
|
|
|
# define SECP256K1_GNUC_EXT
|
2015-01-25 03:41:59 +00:00
|
|
|
#endif
|
|
|
|
|
2020-03-27 10:54:09 +01:00
|
|
|
/* Zero memory if flag == 1. Flag must be 0 or 1. Constant time. */
|
2020-10-20 14:53:50 +02:00
|
|
|
static SECP256K1_INLINE void secp256k1_memczero(void *s, size_t len, int flag) {
|
2020-03-25 16:04:49 +01:00
|
|
|
unsigned char *p = (unsigned char *)s;
|
|
|
|
/* Access flag with a volatile-qualified lvalue.
|
|
|
|
This prevents clang from figuring out (after inlining) that flag can
|
|
|
|
take only be 0 or 1, which leads to variable time code. */
|
|
|
|
volatile int vflag = flag;
|
|
|
|
unsigned char mask = -(unsigned char) vflag;
|
2020-01-11 01:01:05 +00:00
|
|
|
while (len) {
|
2020-03-25 16:04:49 +01:00
|
|
|
*p &= ~mask;
|
2020-01-11 01:01:05 +00:00
|
|
|
p++;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 12:12:48 +02:00
|
|
|
/** Semantics like memcmp. Variable-time.
|
|
|
|
*
|
|
|
|
* We use this to avoid possible compiler bugs with memcmp, e.g.
|
|
|
|
* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95189
|
|
|
|
*/
|
|
|
|
static SECP256K1_INLINE int secp256k1_memcmp_var(const void *s1, const void *s2, size_t n) {
|
|
|
|
const unsigned char *p1 = s1, *p2 = s2;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
int diff = p1[i] - p2[i];
|
|
|
|
if (diff != 0) {
|
|
|
|
return diff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-27 00:37:59 +03:00
|
|
|
/** If flag is true, set *r equal to *a; otherwise leave it. Constant-time. Both *r and *a must be initialized and non-negative.*/
|
|
|
|
static SECP256K1_INLINE void secp256k1_int_cmov(int *r, const int *a, int flag) {
|
|
|
|
unsigned int mask0, mask1, r_masked, a_masked;
|
2020-07-27 14:35:05 +02:00
|
|
|
/* Access flag with a volatile-qualified lvalue.
|
|
|
|
This prevents clang from figuring out (after inlining) that flag can
|
|
|
|
take only be 0 or 1, which leads to variable time code. */
|
|
|
|
volatile int vflag = flag;
|
|
|
|
|
2020-05-27 00:37:59 +03:00
|
|
|
/* Casting a negative int to unsigned and back to int is implementation defined behavior */
|
|
|
|
VERIFY_CHECK(*r >= 0 && *a >= 0);
|
|
|
|
|
2020-07-27 14:35:05 +02:00
|
|
|
mask0 = (unsigned int)vflag + ~0u;
|
2020-05-27 00:37:59 +03:00
|
|
|
mask1 = ~mask0;
|
|
|
|
r_masked = ((unsigned int)*r & mask0);
|
|
|
|
a_masked = ((unsigned int)*a & mask1);
|
|
|
|
|
|
|
|
*r = (int)(r_masked | a_masked);
|
|
|
|
}
|
|
|
|
|
2022-07-27 11:09:51 -04:00
|
|
|
/* If USE_FORCE_WIDEMUL_{INT128, INT128_STRUCT, INT64} is set, use that wide multiplication implementation.
|
2020-08-09 10:58:40 -07:00
|
|
|
* Otherwise use the presence of __SIZEOF_INT128__ to decide.
|
|
|
|
*/
|
2022-07-27 11:09:51 -04:00
|
|
|
#if defined(USE_FORCE_WIDEMUL_INT128_STRUCT)
|
2020-08-09 10:58:40 -07:00
|
|
|
# define SECP256K1_WIDEMUL_INT128 1
|
2022-07-27 11:09:51 -04:00
|
|
|
# define SECP256K1_INT128_STRUCT 1
|
|
|
|
#elif defined(USE_FORCE_WIDEMUL_INT128)
|
|
|
|
# define SECP256K1_WIDEMUL_INT128 1
|
|
|
|
# define SECP256K1_INT128_NATIVE 1
|
2020-08-09 10:58:40 -07:00
|
|
|
#elif defined(USE_FORCE_WIDEMUL_INT64)
|
|
|
|
# define SECP256K1_WIDEMUL_INT64 1
|
2020-10-27 15:58:04 +01:00
|
|
|
#elif defined(UINT128_MAX) || defined(__SIZEOF_INT128__)
|
2020-08-09 10:58:40 -07:00
|
|
|
# define SECP256K1_WIDEMUL_INT128 1
|
2022-07-27 11:09:51 -04:00
|
|
|
# define SECP256K1_INT128_NATIVE 1
|
2020-08-09 10:58:40 -07:00
|
|
|
#else
|
|
|
|
# define SECP256K1_WIDEMUL_INT64 1
|
|
|
|
#endif
|
|
|
|
|
2020-10-11 19:10:58 -07:00
|
|
|
#ifndef __has_builtin
|
|
|
|
#define __has_builtin(x) 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Determine the number of trailing zero bits in a (non-zero) 32-bit x.
|
|
|
|
* This function is only intended to be used as fallback for
|
|
|
|
* secp256k1_ctz32_var, but permits it to be tested separately. */
|
|
|
|
static SECP256K1_INLINE int secp256k1_ctz32_var_debruijn(uint32_t x) {
|
|
|
|
static const uint8_t debruijn[32] = {
|
|
|
|
0x00, 0x01, 0x02, 0x18, 0x03, 0x13, 0x06, 0x19, 0x16, 0x04, 0x14, 0x0A,
|
|
|
|
0x10, 0x07, 0x0C, 0x1A, 0x1F, 0x17, 0x12, 0x05, 0x15, 0x09, 0x0F, 0x0B,
|
|
|
|
0x1E, 0x11, 0x08, 0x0E, 0x1D, 0x0D, 0x1C, 0x1B
|
|
|
|
};
|
|
|
|
return debruijn[((x & -x) * 0x04D7651F) >> 27];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine the number of trailing zero bits in a (non-zero) 64-bit x.
|
|
|
|
* This function is only intended to be used as fallback for
|
|
|
|
* secp256k1_ctz64_var, but permits it to be tested separately. */
|
|
|
|
static SECP256K1_INLINE int secp256k1_ctz64_var_debruijn(uint64_t x) {
|
|
|
|
static const uint8_t debruijn[64] = {
|
|
|
|
0, 1, 2, 53, 3, 7, 54, 27, 4, 38, 41, 8, 34, 55, 48, 28,
|
|
|
|
62, 5, 39, 46, 44, 42, 22, 9, 24, 35, 59, 56, 49, 18, 29, 11,
|
|
|
|
63, 52, 6, 26, 37, 40, 33, 47, 61, 45, 43, 21, 23, 58, 17, 10,
|
|
|
|
51, 25, 36, 32, 60, 20, 57, 16, 50, 31, 19, 15, 30, 14, 13, 12
|
|
|
|
};
|
|
|
|
return debruijn[((x & -x) * 0x022FDD63CC95386D) >> 58];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine the number of trailing zero bits in a (non-zero) 32-bit x. */
|
|
|
|
static SECP256K1_INLINE int secp256k1_ctz32_var(uint32_t x) {
|
|
|
|
VERIFY_CHECK(x != 0);
|
|
|
|
#if (__has_builtin(__builtin_ctz) || SECP256K1_GNUC_PREREQ(3,4))
|
|
|
|
/* If the unsigned type is sufficient to represent the largest uint32_t, consider __builtin_ctz. */
|
|
|
|
if (((unsigned)UINT32_MAX) == UINT32_MAX) {
|
|
|
|
return __builtin_ctz(x);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if (__has_builtin(__builtin_ctzl) || SECP256K1_GNUC_PREREQ(3,4))
|
|
|
|
/* Otherwise consider __builtin_ctzl (the unsigned long type is always at least 32 bits). */
|
|
|
|
return __builtin_ctzl(x);
|
|
|
|
#else
|
|
|
|
/* If no suitable CTZ builtin is available, use a (variable time) software emulation. */
|
|
|
|
return secp256k1_ctz32_var_debruijn(x);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine the number of trailing zero bits in a (non-zero) 64-bit x. */
|
|
|
|
static SECP256K1_INLINE int secp256k1_ctz64_var(uint64_t x) {
|
|
|
|
VERIFY_CHECK(x != 0);
|
|
|
|
#if (__has_builtin(__builtin_ctzl) || SECP256K1_GNUC_PREREQ(3,4))
|
|
|
|
/* If the unsigned long type is sufficient to represent the largest uint64_t, consider __builtin_ctzl. */
|
|
|
|
if (((unsigned long)UINT64_MAX) == UINT64_MAX) {
|
|
|
|
return __builtin_ctzl(x);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if (__has_builtin(__builtin_ctzll) || SECP256K1_GNUC_PREREQ(3,4))
|
|
|
|
/* Otherwise consider __builtin_ctzll (the unsigned long long type is always at least 64 bits). */
|
|
|
|
return __builtin_ctzll(x);
|
|
|
|
#else
|
|
|
|
/* If no suitable CTZ builtin is available, use a (variable time) software emulation. */
|
|
|
|
return secp256k1_ctz64_var_debruijn(x);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-03-25 10:30:24 +01:00
|
|
|
/* Read a uint32_t in big endian */
|
|
|
|
SECP256K1_INLINE static uint32_t secp256k1_read_be32(const unsigned char* p) {
|
|
|
|
return (uint32_t)p[0] << 24 |
|
|
|
|
(uint32_t)p[1] << 16 |
|
|
|
|
(uint32_t)p[2] << 8 |
|
|
|
|
(uint32_t)p[3];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write a uint32_t in big endian */
|
|
|
|
SECP256K1_INLINE static void secp256k1_write_be32(unsigned char* p, uint32_t x) {
|
|
|
|
p[3] = x;
|
|
|
|
p[2] = x >> 8;
|
|
|
|
p[1] = x >> 16;
|
|
|
|
p[0] = x >> 24;
|
|
|
|
}
|
|
|
|
|
2017-08-26 18:44:21 +03:00
|
|
|
#endif /* SECP256K1_UTIL_H */
|