25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /********************************************************************************************
  2. * Supersingular Isogeny Key Encapsulation Library
  3. *
  4. * Abstract: configuration file and platform-dependent macros
  5. *********************************************************************************************/
  6. #ifndef __CONFIG_H__
  7. #define __CONFIG_H__
  8. #include <stdint.h>
  9. #include <stdbool.h>
  10. #include <stddef.h>
  11. // Definition of operating system
  12. #define OS_LINUX 1
  13. #if defined(__LINUX__) // Linux OS
  14. #define OS_TARGET OS_LINUX
  15. #else
  16. #error -- "Unsupported OS"
  17. #endif
  18. // Definition of compiler
  19. #define COMPILER_GCC 1
  20. #define COMPILER_CLANG 2
  21. #if defined(__GNUC__) // GNU GCC compiler
  22. #define COMPILER COMPILER_GCC
  23. #elif defined(__clang__) // Clang compiler
  24. #define COMPILER COMPILER_CLANG
  25. #else
  26. #error -- "Unsupported COMPILER"
  27. #endif
  28. // Definition of the targeted architecture and basic data types
  29. #define TARGET_AMD64 1
  30. #if defined(_AMD64_)
  31. #define TARGET TARGET_AMD64
  32. #define RADIX 64
  33. #define LOG2RADIX 6
  34. typedef uint64_t digit_t; // Unsigned 64-bit digit
  35. #else
  36. #error -- "Unsupported ARCHITECTURE"
  37. #endif
  38. #define RADIX64 64
  39. // Selection of implementation: optimized_fast with x64 assembly
  40. #if defined(_OPTIMIZED_FAST_)
  41. #define OPTIMIZED_FAST_IMPLEMENTATION
  42. #endif
  43. // Extended datatype support
  44. #define UINT128_SUPPORT
  45. typedef unsigned uint128_t __attribute__((mode(TI)));
  46. // Macro definitions
  47. #define NBITS_TO_NBYTES(nbits) (((nbits)+7)/8) // Conversion macro from number of bits to number of bytes
  48. #define NBITS_TO_NWORDS(nbits) (((nbits)+(sizeof(digit_t)*8)-1)/(sizeof(digit_t)*8)) // Conversion macro from number of bits to number of computer words
  49. #define NBYTES_TO_NWORDS(nbytes) (((nbytes)+sizeof(digit_t)-1)/sizeof(digit_t)) // Conversion macro from number of bytes to number of computer words
  50. // Macro to avoid compiler warnings when detecting unreferenced parameters
  51. #define UNREFERENCED_PARAMETER(PAR) ((void)(PAR))
  52. /********************** Constant-time unsigned comparisons ***********************/
  53. // The following functions return 1 (TRUE) if condition is true, 0 (FALSE) otherwise
  54. static __inline unsigned int is_digit_nonzero_ct(digit_t x)
  55. { // Is x != 0?
  56. return (unsigned int)((x | (0-x)) >> (RADIX-1));
  57. }
  58. static __inline unsigned int is_digit_zero_ct(digit_t x)
  59. { // Is x = 0?
  60. return (unsigned int)(1 ^ is_digit_nonzero_ct(x));
  61. }
  62. static __inline unsigned int is_digit_lessthan_ct(digit_t x, digit_t y)
  63. { // Is x < y?
  64. return (unsigned int)((x ^ ((x ^ y) | ((x - y) ^ y))) >> (RADIX-1));
  65. }
  66. /********************** Macros for platform-dependent operations **********************/
  67. // Digit multiplication
  68. #define MUL(multiplier, multiplicand, hi, lo) \
  69. { uint128_t tempReg = (uint128_t)(multiplier) * (uint128_t)(multiplicand); \
  70. *(hi) = (digit_t)(tempReg >> RADIX); \
  71. (lo) = (digit_t)tempReg; }
  72. // Digit addition with carry
  73. #define ADDC(carryIn, addend1, addend2, carryOut, sumOut) \
  74. { uint128_t tempReg = (uint128_t)(addend1) + (uint128_t)(addend2) + (uint128_t)(carryIn); \
  75. (carryOut) = (digit_t)(tempReg >> RADIX); \
  76. (sumOut) = (digit_t)tempReg; }
  77. // Digit subtraction with borrow
  78. #define SUBC(borrowIn, minuend, subtrahend, borrowOut, differenceOut) \
  79. { uint128_t tempReg = (uint128_t)(minuend) - (uint128_t)(subtrahend) - (uint128_t)(borrowIn); \
  80. (borrowOut) = (digit_t)(tempReg >> (sizeof(uint128_t)*8 - 1)); \
  81. (differenceOut) = (digit_t)tempReg; }
  82. // Digit shift right
  83. #define SHIFTR(highIn, lowIn, shift, shiftOut, DigitSize) \
  84. (shiftOut) = ((lowIn) >> (shift)) ^ ((highIn) << (RADIX - (shift)));
  85. // Digit shift left
  86. #define SHIFTL(highIn, lowIn, shift, shiftOut, DigitSize) \
  87. (shiftOut) = ((highIn) << (shift)) ^ ((lowIn) >> (RADIX - (shift)));
  88. #endif