src/include/barrett_field.h: - Requires review: corrected failure to cast to (mask_t) prior to negation. (Or, if this is wrong; should cast to needed bitwidth explicitly.) - Changed type of nwords_out to uint32_t to agree with header. src/include/intrinsics.h: - Fixed up various preprocessor statements to check for definition rather than value of built-ins. - Added macro to use Clang’s __builtin_readcyclecounter on platforms on which it’s available. (Which is most platforms these days.) src/include/magic.h: Preprocessor “if” versus “if defined”. src/include/word.h: Fixed ifdefs; enabled support for memset_s on Darwin. Added explicit cast to mask_t. Added void to function definitions and declarations in the following files (not including void is okay in modern C++, but not modern C, IIRC): include/goldilocks.h, src/crandom.c, src/goldilocks.c, src/include/api.h, src/include/intrinsics.h, test/bench.c, test/test.c, test/test.h, test/test_arithmetic.c, test/test_goldilocks.c, test/test_pointops.c, test/test_scalarmul.c, test/test_sha512.cmaster
| @@ -100,7 +100,7 @@ static const int GOLDI_EALREADYINIT = 44805; | |||||
| * @retval Nonzero An error occurred. | * @retval Nonzero An error occurred. | ||||
| */ | */ | ||||
| int | int | ||||
| goldilocks_init () | |||||
| goldilocks_init (void) | |||||
| __attribute__((warn_unused_result,visibility ("default"))); | __attribute__((warn_unused_result,visibility ("default"))); | ||||
| @@ -14,7 +14,7 @@ | |||||
| volatile unsigned int crandom_features = 0; | volatile unsigned int crandom_features = 0; | ||||
| unsigned int crandom_detect_features() { | |||||
| unsigned int crandom_detect_features(void) { | |||||
| unsigned int out = GEN; | unsigned int out = GEN; | ||||
| # if (defined(__i386__) || defined(__x86_64__)) | # if (defined(__i386__) || defined(__x86_64__)) | ||||
| @@ -57,7 +57,7 @@ static struct { | |||||
| } goldilocks_global; | } goldilocks_global; | ||||
| static inline mask_t | static inline mask_t | ||||
| goldilocks_check_init() { | |||||
| goldilocks_check_init(void) { | |||||
| if (likely(goldilocks_global.state == G_INITED)) { | if (likely(goldilocks_global.state == G_INITED)) { | ||||
| return MASK_SUCCESS; | return MASK_SUCCESS; | ||||
| } else { | } else { | ||||
| @@ -66,7 +66,7 @@ goldilocks_check_init() { | |||||
| } | } | ||||
| int | int | ||||
| goldilocks_init () { | |||||
| goldilocks_init (void) { | |||||
| const char *res = compare_and_swap(&goldilocks_global.state, NULL, G_INITING); | const char *res = compare_and_swap(&goldilocks_global.state, NULL, G_INITING); | ||||
| if (res == G_INITED) return GOLDI_EALREADYINIT; | if (res == G_INITED) return GOLDI_EALREADYINIT; | ||||
| else if (res) { | else if (res) { | ||||
| @@ -43,9 +43,9 @@ | |||||
| #endif | #endif | ||||
| */ | */ | ||||
| static inline int timingattacks() { return 0; } | |||||
| static inline int copyrightclaims() { return 0; } | |||||
| static inline int patentclaims() { | |||||
| static inline int timingattacks(void) { return 0; } | |||||
| static inline int copyrightclaims(void) { return 0; } | |||||
| static inline int patentclaims(void) { | |||||
| /* Until the end of July 2014, point compression | /* Until the end of July 2014, point compression | ||||
| * is patented. */ | * is patented. */ | ||||
| return 20; | return 20; | ||||
| @@ -37,7 +37,7 @@ extern const struct barrett_prime_t curve_prime_order; | |||||
| /** | /** | ||||
| * Reduce a number (with optional high carry word) mod p. | * Reduce a number (with optional high carry word) mod p. | ||||
| * | * | ||||
| * @param [inout] a The value to be reduced. | |||||
| * @param [in,out] a The value to be reduced. | |||||
| * @param [in] nwords_a The number of words in a. | * @param [in] nwords_a The number of words in a. | ||||
| * @param [in] a_carry A high word to be carried into the computation. | * @param [in] a_carry A high word to be carried into the computation. | ||||
| * @param [in] prime The Barrett prime. | * @param [in] prime The Barrett prime. | ||||
| @@ -132,7 +132,7 @@ barrett_mul_or_mac( | |||||
| static inline void | static inline void | ||||
| barrett_mul( | barrett_mul( | ||||
| word_t *out, | word_t *out, | ||||
| int nwords_out, | |||||
| uint32_t nwords_out, | |||||
| const word_t *a, | const word_t *a, | ||||
| uint32_t nwords_a, | uint32_t nwords_a, | ||||
| @@ -158,7 +158,7 @@ barrett_mac( | |||||
| const struct barrett_prime_t *prime | const struct barrett_prime_t *prime | ||||
| ) { | ) { | ||||
| barrett_mul_or_mac(out,nwords_out,a,nwords_a,b,nwords_b,prime,-1); | |||||
| barrett_mul_or_mac(out,nwords_out,a,nwords_a,b,nwords_b,prime,-(mask_t)1); | |||||
| } | } | ||||
| mask_t | mask_t | ||||
| @@ -13,13 +13,13 @@ | |||||
| #include <sys/types.h> | #include <sys/types.h> | ||||
| #include "config.h" | #include "config.h" | ||||
| #if __i386__ || __x86_64__ | |||||
| #if defined(__i386__) || defined(__x86_64__) | |||||
| #include <immintrin.h> | #include <immintrin.h> | ||||
| #endif | #endif | ||||
| /** @brief Macro to make a function static, forcibly inlined and possibly unused. */ | /** @brief Macro to make a function static, forcibly inlined and possibly unused. */ | ||||
| #define INTRINSIC \ | #define INTRINSIC \ | ||||
| static __inline__ __attribute__((__gnu_inline__, __always_inline__, unused)) | |||||
| static inline __attribute__((__gnu_inline__, __always_inline__)) | |||||
| #define GEN 1 /**< @brief Intrinsics field has been generated. */ | #define GEN 1 /**< @brief Intrinsics field has been generated. */ | ||||
| #define SSE2 2 /**< @brief Machine supports SSE2 */ | #define SSE2 2 /**< @brief Machine supports SSE2 */ | ||||
| @@ -33,13 +33,20 @@ | |||||
| /** | /** | ||||
| * @brief If on x86, read the timestamp counter. Otherwise, return 0. | * @brief If on x86, read the timestamp counter. Otherwise, return 0. | ||||
| */ | */ | ||||
| INTRINSIC u_int64_t rdtsc() { | |||||
| #ifndef __has_builtin | |||||
| #define __has_builtin(X) 0 | |||||
| #endif | |||||
| #if defined(__clang__) && __has_builtin(__builtin_readcyclecounter) | |||||
| #define rdtsc __builtin_readcyclecounter | |||||
| #else | |||||
| INTRINSIC u_int64_t rdtsc(void) { | |||||
| u_int64_t out = 0; | u_int64_t out = 0; | ||||
| # if (defined(__i386__) || defined(__x86_64__)) | # if (defined(__i386__) || defined(__x86_64__)) | ||||
| __asm__ __volatile__ ("rdtsc" : "=A"(out)); | __asm__ __volatile__ ("rdtsc" : "=A"(out)); | ||||
| # endif | # endif | ||||
| return out; | return out; | ||||
| } | } | ||||
| #endif | |||||
| /** | /** | ||||
| * Return x unchanged, but confuse the compiler. | * Return x unchanged, but confuse the compiler. | ||||
| @@ -76,7 +76,7 @@ extern const word_t SCALARMUL_FIXED_WINDOW_ADJUSTMENT[2*SCALAR_WORDS]; | |||||
| * @brief If true, use wider tables for the precomputed combs. | * @brief If true, use wider tables for the precomputed combs. | ||||
| */ | */ | ||||
| #ifndef USE_BIG_COMBS | #ifndef USE_BIG_COMBS | ||||
| #if __ARM_NEON__ | |||||
| #if defined(__ARM_NEON__) | |||||
| #define USE_BIG_COMBS 1 | #define USE_BIG_COMBS 1 | ||||
| #else | #else | ||||
| #define USE_BIG_COMBS (WORD_BITS==64) | #define USE_BIG_COMBS (WORD_BITS==64) | ||||
| @@ -20,13 +20,16 @@ | |||||
| #include <sys/types.h> | #include <sys/types.h> | ||||
| #include <inttypes.h> | #include <inttypes.h> | ||||
| #if __ARM_NEON__ | |||||
| #if defined(__ARM_NEON__) | |||||
| #include <arm_neon.h> | #include <arm_neon.h> | ||||
| #elif __SSE2__ | |||||
| #elif defined(__SSE2__) | |||||
| #include <immintrin.h> | #include <immintrin.h> | ||||
| #endif | #endif | ||||
| #if (__SIZEOF_INT128__ == 16 && __SIZEOF_SIZE_T__ == 8 && (__SIZEOF_LONG__==8 || __POINTER_WIDTH__==64) && !GOLDI_FORCE_32_BIT) | |||||
| #if (__SIZEOF_INT128__ == 16 \ | |||||
| && __SIZEOF_SIZE_T__ == 8 \ | |||||
| && (__SIZEOF_LONG__==8 || __POINTER_WIDTH__==64) \ | |||||
| && !defined(GOLDI_FORCE_32_BIT)) | |||||
| /* It's a 64-bit machine if: | /* It's a 64-bit machine if: | ||||
| * __uint128_t exists | * __uint128_t exists | ||||
| * size_t is 64 bits | * size_t is 64 bits | ||||
| @@ -67,7 +70,7 @@ typedef int64_t dsword_t; | |||||
| #define WORDS_FOR_BITS(_x) (DIV_CEIL((_x),WORD_BITS)) | #define WORDS_FOR_BITS(_x) (DIV_CEIL((_x),WORD_BITS)) | ||||
| typedef word_t mask_t; | typedef word_t mask_t; | ||||
| static const mask_t MASK_FAILURE = 0, MASK_SUCCESS = -1; | |||||
| static const mask_t MASK_FAILURE = 0, MASK_SUCCESS = -(mask_t)1; | |||||
| @@ -106,7 +109,7 @@ typedef word_t vecmask_t __attribute__((vector_size(32))); | |||||
| static __inline__ big_register_t | static __inline__ big_register_t | ||||
| br_set_to_mask(mask_t x) { | br_set_to_mask(mask_t x) { | ||||
| uint32_t y = x; | |||||
| uint32_t y = (uint32_t)x; | |||||
| big_register_t ret = {y,y,y,y,y,y,y,y}; | big_register_t ret = {y,y,y,y,y,y,y,y}; | ||||
| return ret; | return ret; | ||||
| } | } | ||||
| @@ -193,10 +196,22 @@ letoh64 (uint64_t x) { return x; } | |||||
| * @param c The char to set it to (probably zero). | * @param c The char to set it to (probably zero). | ||||
| * @param s The size of the object. | * @param s The size of the object. | ||||
| */ | */ | ||||
| #ifdef __STDC_LIB_EXT1__ /* which it won't be, because we're -std=c99 */ | |||||
| #if (defined(__DARWIN_C_LEVEL) \ | |||||
| || (defined(__STDC_WANT_LIB_EXT1__) && __STDC_WANT_LIB_EXT1__ == 1)) | |||||
| #define HAS_MEMSET_S | |||||
| #endif | |||||
| #if !defined(__STDC_WANT_LIB_EXT1__) || __STDC_WANT_LIB_EXT1__ != 1 | |||||
| #define NEED_MEMSET_S_EXTERN | |||||
| #endif | |||||
| #ifdef HAS_MEMSET_S | |||||
| #ifdef NEED_MEMSET_S_EXTERN | |||||
| extern int memset_s(void *, size_t, int, size_t); | |||||
| #endif | |||||
| static __inline__ void | static __inline__ void | ||||
| really_memset(void *p, char c, size_t s) { | really_memset(void *p, char c, size_t s) { | ||||
| memset_s(p,s,c,s); | |||||
| memset_s(p, s, c, s); | |||||
| } | } | ||||
| #else | #else | ||||
| static __inline__ void __attribute__((always_inline,unused)) | static __inline__ void __attribute__((always_inline,unused)) | ||||
| @@ -22,7 +22,7 @@ ignore_result ( int result ) { | |||||
| (void)result; | (void)result; | ||||
| } | } | ||||
| static double now() { | |||||
| static double now(void) { | |||||
| struct timeval tv; | struct timeval tv; | ||||
| gettimeofday(&tv, NULL); | gettimeofday(&tv, NULL); | ||||
| @@ -9,7 +9,7 @@ | |||||
| int failed_tests, n_tests, failed_this_test, running_a_test; | int failed_tests, n_tests, failed_this_test, running_a_test; | ||||
| static void end_test() { | |||||
| static void end_test(void) { | |||||
| if (!failed_this_test) { | if (!failed_this_test) { | ||||
| printf("[PASS]\n"); | printf("[PASS]\n"); | ||||
| } | } | ||||
| @@ -25,7 +25,7 @@ static void begin_test(const char *name) { | |||||
| running_a_test = 1; | running_a_test = 1; | ||||
| } | } | ||||
| void youfail() { | |||||
| void youfail(void) { | |||||
| if (failed_this_test) return; | if (failed_this_test) return; | ||||
| failed_this_test = 1; | failed_this_test = 1; | ||||
| failed_tests ++; | failed_tests ++; | ||||
| @@ -29,20 +29,20 @@ void scalar_print ( | |||||
| int nwords | int nwords | ||||
| ); | ); | ||||
| void youfail(); | |||||
| void youfail(void); | |||||
| int test_sha512_monte_carlo(); | |||||
| int test_sha512_monte_carlo(void); | |||||
| int test_linear_combo (); | |||||
| int test_linear_combo (void); | |||||
| int test_scalarmul_compatibility (); | |||||
| int test_scalarmul_compatibility (void); | |||||
| int test_scalarmul_commutativity (); | |||||
| int test_scalarmul_commutativity (void); | |||||
| int test_arithmetic (); | |||||
| int test_arithmetic (void); | |||||
| int test_goldilocks (); | |||||
| int test_goldilocks (void); | |||||
| int test_pointops (); | |||||
| int test_pointops (void); | |||||
| #endif // __GOLDILOCKS_TEST_H__ | #endif // __GOLDILOCKS_TEST_H__ | ||||
| @@ -148,7 +148,7 @@ static mask_t test_mul_sqr ( | |||||
| return succ; | return succ; | ||||
| } | } | ||||
| int test_arithmetic () { | |||||
| int test_arithmetic (void) { | |||||
| int j, ntests = 100000; | int j, ntests = 100000; | ||||
| gmp_randstate_t state; | gmp_randstate_t state; | ||||
| @@ -4,7 +4,7 @@ | |||||
| #include <stdlib.h> | #include <stdlib.h> | ||||
| #include <string.h> | #include <string.h> | ||||
| int test_goldilocks () { | |||||
| int test_goldilocks (void) { | |||||
| const char *message1 = "hello world"; | const char *message1 = "hello world"; | ||||
| const char *message2 = "Jello world"; | const char *message2 = "Jello world"; | ||||
| @@ -249,7 +249,7 @@ single_twisting_test ( | |||||
| return succ ? 0 : -1; | return succ ? 0 : -1; | ||||
| } | } | ||||
| int test_pointops () { | |||||
| int test_pointops (void) { | |||||
| struct affine_t base, pbase; | struct affine_t base, pbase; | ||||
| struct p448_t ser448; | struct p448_t ser448; | ||||
| @@ -274,7 +274,7 @@ single_scalarmul_commutativity_test ( | |||||
| } | } | ||||
| } | } | ||||
| int test_scalarmul_commutativity () { | |||||
| int test_scalarmul_commutativity (void) { | |||||
| int i,j,k,got; | int i,j,k,got; | ||||
| struct crandom_state_t crand; | struct crandom_state_t crand; | ||||
| @@ -312,7 +312,7 @@ int test_scalarmul_commutativity () { | |||||
| return 0; | return 0; | ||||
| } | } | ||||
| int test_linear_combo () { | |||||
| int test_linear_combo (void) { | |||||
| int i,j,k,got; | int i,j,k,got; | ||||
| struct crandom_state_t crand; | struct crandom_state_t crand; | ||||
| @@ -355,7 +355,7 @@ int test_linear_combo () { | |||||
| return 0; | return 0; | ||||
| } | } | ||||
| int test_scalarmul_compatibility () { | |||||
| int test_scalarmul_compatibility (void) { | |||||
| int i,j,k,got; | int i,j,k,got; | ||||
| struct crandom_state_t crand; | struct crandom_state_t crand; | ||||
| @@ -59,7 +59,7 @@ static int sha512_monte_carlo_core ( | |||||
| return 0; | return 0; | ||||
| } | } | ||||
| int test_sha512_monte_carlo() { | |||||
| int test_sha512_monte_carlo(void) { | |||||
| const char *seed = | const char *seed = | ||||
| "5c337de5caf35d18ed90b5cddfce001ca1b8ee8602f367e7c24ccca6f893802f" | "5c337de5caf35d18ed90b5cddfce001ca1b8ee8602f367e7c24ccca6f893802f" | ||||
| "b1aca7a3dae32dcd60800a59959bc540d63237876b799229ae71a2526fbc52cd"; | "b1aca7a3dae32dcd60800a59959bc540d63237876b799229ae71a2526fbc52cd"; | ||||