ed25519-donna-portable.h 4.7 KB

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