int128.h (4168B)
1 #ifndef SECP256K1_INT128_H 2 #define SECP256K1_INT128_H 3 4 #include "util.h" 5 6 #if defined(SECP256K1_WIDEMUL_INT128) 7 # if defined(SECP256K1_INT128_NATIVE) 8 # include "int128_native.h" 9 # elif defined(SECP256K1_INT128_STRUCT) 10 # include "int128_struct.h" 11 # else 12 # error "Please select int128 implementation" 13 # endif 14 15 /* Construct an unsigned 128-bit value from a high and a low 64-bit value. */ 16 static SECP256K1_INLINE void haskellsecp256k1_v0_1_0_u128_load(haskellsecp256k1_v0_1_0_uint128 *r, uint64_t hi, uint64_t lo); 17 18 /* Multiply two unsigned 64-bit values a and b and write the result to r. */ 19 static SECP256K1_INLINE void haskellsecp256k1_v0_1_0_u128_mul(haskellsecp256k1_v0_1_0_uint128 *r, uint64_t a, uint64_t b); 20 21 /* Multiply two unsigned 64-bit values a and b and add the result to r. 22 * The final result is taken modulo 2^128. 23 */ 24 static SECP256K1_INLINE void haskellsecp256k1_v0_1_0_u128_accum_mul(haskellsecp256k1_v0_1_0_uint128 *r, uint64_t a, uint64_t b); 25 26 /* Add an unsigned 64-bit value a to r. 27 * The final result is taken modulo 2^128. 28 */ 29 static SECP256K1_INLINE void haskellsecp256k1_v0_1_0_u128_accum_u64(haskellsecp256k1_v0_1_0_uint128 *r, uint64_t a); 30 31 /* Unsigned (logical) right shift. 32 * Non-constant time in n. 33 */ 34 static SECP256K1_INLINE void haskellsecp256k1_v0_1_0_u128_rshift(haskellsecp256k1_v0_1_0_uint128 *r, unsigned int n); 35 36 /* Return the low 64-bits of a 128-bit value as an unsigned 64-bit value. */ 37 static SECP256K1_INLINE uint64_t haskellsecp256k1_v0_1_0_u128_to_u64(const haskellsecp256k1_v0_1_0_uint128 *a); 38 39 /* Return the high 64-bits of a 128-bit value as an unsigned 64-bit value. */ 40 static SECP256K1_INLINE uint64_t haskellsecp256k1_v0_1_0_u128_hi_u64(const haskellsecp256k1_v0_1_0_uint128 *a); 41 42 /* Write an unsigned 64-bit value to r. */ 43 static SECP256K1_INLINE void haskellsecp256k1_v0_1_0_u128_from_u64(haskellsecp256k1_v0_1_0_uint128 *r, uint64_t a); 44 45 /* Tests if r is strictly less than to 2^n. 46 * n must be strictly less than 128. 47 */ 48 static SECP256K1_INLINE int haskellsecp256k1_v0_1_0_u128_check_bits(const haskellsecp256k1_v0_1_0_uint128 *r, unsigned int n); 49 50 /* Construct an signed 128-bit value from a high and a low 64-bit value. */ 51 static SECP256K1_INLINE void haskellsecp256k1_v0_1_0_i128_load(haskellsecp256k1_v0_1_0_int128 *r, int64_t hi, uint64_t lo); 52 53 /* Multiply two signed 64-bit values a and b and write the result to r. */ 54 static SECP256K1_INLINE void haskellsecp256k1_v0_1_0_i128_mul(haskellsecp256k1_v0_1_0_int128 *r, int64_t a, int64_t b); 55 56 /* Multiply two signed 64-bit values a and b and add the result to r. 57 * Overflow or underflow from the addition is undefined behaviour. 58 */ 59 static SECP256K1_INLINE void haskellsecp256k1_v0_1_0_i128_accum_mul(haskellsecp256k1_v0_1_0_int128 *r, int64_t a, int64_t b); 60 61 /* Compute a*d - b*c from signed 64-bit values and write the result to r. */ 62 static SECP256K1_INLINE void haskellsecp256k1_v0_1_0_i128_det(haskellsecp256k1_v0_1_0_int128 *r, int64_t a, int64_t b, int64_t c, int64_t d); 63 64 /* Signed (arithmetic) right shift. 65 * Non-constant time in b. 66 */ 67 static SECP256K1_INLINE void haskellsecp256k1_v0_1_0_i128_rshift(haskellsecp256k1_v0_1_0_int128 *r, unsigned int b); 68 69 /* Return the input value modulo 2^64. */ 70 static SECP256K1_INLINE uint64_t haskellsecp256k1_v0_1_0_i128_to_u64(const haskellsecp256k1_v0_1_0_int128 *a); 71 72 /* Return the value as a signed 64-bit value. 73 * Requires the input to be between INT64_MIN and INT64_MAX. 74 */ 75 static SECP256K1_INLINE int64_t haskellsecp256k1_v0_1_0_i128_to_i64(const haskellsecp256k1_v0_1_0_int128 *a); 76 77 /* Write a signed 64-bit value to r. */ 78 static SECP256K1_INLINE void haskellsecp256k1_v0_1_0_i128_from_i64(haskellsecp256k1_v0_1_0_int128 *r, int64_t a); 79 80 /* Compare two 128-bit values for equality. */ 81 static SECP256K1_INLINE int haskellsecp256k1_v0_1_0_i128_eq_var(const haskellsecp256k1_v0_1_0_int128 *a, const haskellsecp256k1_v0_1_0_int128 *b); 82 83 /* Tests if r is equal to sign*2^n (sign must be 1 or -1). 84 * n must be strictly less than 127. 85 */ 86 static SECP256K1_INLINE int haskellsecp256k1_v0_1_0_i128_check_pow2(const haskellsecp256k1_v0_1_0_int128 *r, unsigned int n, int sign); 87 88 #endif 89 90 #endif