ed25519-donna-portable.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "ed25519-donna-portable-identify.h"
  2. #define mul32x32_64(a,b) (((uint64_t)(a))*(b))
  3. /* platform */
  4. #if defined(COMPILER_MSVC)
  5. #include <intrin.h>
  6. #if !defined(_DEBUG)
  7. #undef mul32x32_64
  8. #define mul32x32_64(a,b) __emulu(a,b)
  9. #endif
  10. #undef inline
  11. #define inline __forceinline
  12. #define DONNA_INLINE __forceinline
  13. #define DONNA_NOINLINE __declspec(noinline)
  14. #define ALIGN(x) __declspec(align(x))
  15. #define ROTL32(a,b) _rotl(a,b)
  16. #define ROTR32(a,b) _rotr(a,b)
  17. #else
  18. #include <sys/param.h>
  19. #define DONNA_INLINE inline __attribute__((always_inline))
  20. #define DONNA_NOINLINE __attribute__((noinline))
  21. #define ALIGN(x) __attribute__((aligned(x)))
  22. #define ROTL32(a,b) (((a) << (b)) | ((a) >> (32 - b)))
  23. #define ROTR32(a,b) (((a) >> (b)) | ((a) << (32 - b)))
  24. #endif
  25. /* uint128_t */
  26. #if defined(CPU_64BITS) && !defined(ED25519_FORCE_32BIT)
  27. #if defined(COMPILER_CLANG) && (COMPILER_CLANG >= 30100)
  28. #define HAVE_NATIVE_UINT128
  29. typedef unsigned __int128 uint128_t;
  30. #elif defined(COMPILER_MSVC)
  31. #define HAVE_UINT128
  32. typedef struct uint128_t {
  33. uint64_t lo, hi;
  34. } uint128_t;
  35. #define mul64x64_128(out,a,b) out.lo = _umul128(a,b,&out.hi);
  36. #define shr128_pair(out,hi,lo,shift) out = __shiftright128(lo, hi, shift);
  37. #define shl128_pair(out,hi,lo,shift) out = __shiftleft128(lo, hi, shift);
  38. #define shr128(out,in,shift) shr128_pair(out, in.hi, in.lo, shift)
  39. #define shl128(out,in,shift) shl128_pair(out, in.hi, in.lo, shift)
  40. #define add128(a,b) { uint64_t p = a.lo; a.lo += b.lo; a.hi += b.hi + (a.lo < p); }
  41. #define add128_64(a,b) { uint64_t p = a.lo; a.lo += b; a.hi += (a.lo < p); }
  42. #define lo128(a) (a.lo)
  43. #define hi128(a) (a.hi)
  44. #elif defined(COMPILER_GCC) && !defined(HAVE_NATIVE_UINT128)
  45. #if defined(__SIZEOF_INT128__)
  46. #define HAVE_NATIVE_UINT128
  47. typedef unsigned __int128 uint128_t;
  48. #elif (COMPILER_GCC >= 40400)
  49. #define HAVE_NATIVE_UINT128
  50. typedef unsigned uint128_t __attribute__((mode(TI)));
  51. #elif defined(CPU_X86_64)
  52. #define HAVE_UINT128
  53. typedef struct uint128_t {
  54. uint64_t lo, hi;
  55. } uint128_t;
  56. #define mul64x64_128(out,a,b) __asm__ ("mulq %3" : "=a" (out.lo), "=d" (out.hi) : "a" (a), "rm" (b));
  57. #define shr128_pair(out,hi,lo,shift) __asm__ ("shrdq %2,%1,%0" : "+r" (lo) : "r" (hi), "J" (shift)); out = lo;
  58. #define shl128_pair(out,hi,lo,shift) __asm__ ("shldq %2,%1,%0" : "+r" (hi) : "r" (lo), "J" (shift)); out = hi;
  59. #define shr128(out,in,shift) shr128_pair(out,in.hi, in.lo, shift)
  60. #define shl128(out,in,shift) shl128_pair(out,in.hi, in.lo, shift)
  61. #define add128(a,b) __asm__ ("addq %4,%2; adcq %5,%3" : "=r" (a.hi), "=r" (a.lo) : "1" (a.lo), "0" (a.hi), "rm" (b.lo), "rm" (b.hi) : "cc");
  62. #define add128_64(a,b) __asm__ ("addq %4,%2; adcq $0,%3" : "=r" (a.hi), "=r" (a.lo) : "1" (a.lo), "0" (a.hi), "rm" (b) : "cc");
  63. #define lo128(a) (a.lo)
  64. #define hi128(a) (a.hi)
  65. #endif
  66. #endif
  67. #if defined(HAVE_NATIVE_UINT128)
  68. #define HAVE_UINT128
  69. #define mul64x64_128(out,a,b) out = (uint128_t)a * b;
  70. #define shr128_pair(out,hi,lo,shift) out = (uint64_t)((((uint128_t)hi << 64) | lo) >> (shift));
  71. #define shl128_pair(out,hi,lo,shift) out = (uint64_t)(((((uint128_t)hi << 64) | lo) << (shift)) >> 64);
  72. #define shr128(out,in,shift) out = (uint64_t)(in >> (shift));
  73. #define shl128(out,in,shift) out = (uint64_t)((in << shift) >> 64);
  74. #define add128(a,b) a += b;
  75. #define add128_64(a,b) a += (uint64_t)b;
  76. #define lo128(a) ((uint64_t)a)
  77. #define hi128(a) ((uint64_t)(a >> 64))
  78. #endif
  79. #if !defined(HAVE_UINT128)
  80. #error Need a uint128_t implementation!
  81. #endif
  82. #endif
  83. /* endian */
  84. #if !defined(ED25519_OPENSSLRNG)
  85. static inline void U32TO8_LE(unsigned char *p, const uint32_t v) {
  86. p[0] = (unsigned char)(v );
  87. p[1] = (unsigned char)(v >> 8);
  88. p[2] = (unsigned char)(v >> 16);
  89. p[3] = (unsigned char)(v >> 24);
  90. }
  91. #endif
  92. #if !defined(HAVE_UINT128)
  93. static inline uint32_t U8TO32_LE(const unsigned char *p) {
  94. return
  95. (((uint32_t)(p[0]) ) |
  96. ((uint32_t)(p[1]) << 8) |
  97. ((uint32_t)(p[2]) << 16) |
  98. ((uint32_t)(p[3]) << 24));
  99. }
  100. #else
  101. static inline uint64_t U8TO64_LE(const unsigned char *p) {
  102. return
  103. (((uint64_t)(p[0]) ) |
  104. ((uint64_t)(p[1]) << 8) |
  105. ((uint64_t)(p[2]) << 16) |
  106. ((uint64_t)(p[3]) << 24) |
  107. ((uint64_t)(p[4]) << 32) |
  108. ((uint64_t)(p[5]) << 40) |
  109. ((uint64_t)(p[6]) << 48) |
  110. ((uint64_t)(p[7]) << 56));
  111. }
  112. static inline void U64TO8_LE(unsigned char *p, const uint64_t v) {
  113. p[0] = (unsigned char)(v );
  114. p[1] = (unsigned char)(v >> 8);
  115. p[2] = (unsigned char)(v >> 16);
  116. p[3] = (unsigned char)(v >> 24);
  117. p[4] = (unsigned char)(v >> 32);
  118. p[5] = (unsigned char)(v >> 40);
  119. p[6] = (unsigned char)(v >> 48);
  120. p[7] = (unsigned char)(v >> 56);
  121. }
  122. #endif
  123. #include <stdlib.h>
  124. #include <string.h>