ed25519-donna.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. Public domain by Andrew M. <liquidsun@gmail.com>
  3. Modified from the amd64-51-30k implementation by
  4. Daniel J. Bernstein
  5. Niels Duif
  6. Tanja Lange
  7. Peter Schwabe
  8. Bo-Yin Yang
  9. */
  10. #include "ed25519-donna-portable.h"
  11. #if defined(ED25519_SSE2)
  12. #else
  13. #if defined(HAVE_UINT128) && !defined(ED25519_FORCE_32BIT)
  14. #define ED25519_64BIT
  15. #else
  16. #define ED25519_32BIT
  17. #endif
  18. #endif
  19. #if !defined(ED25519_NO_INLINE_ASM)
  20. /* detect extra features first so un-needed functions can be disabled throughout */
  21. #if defined(ED25519_SSE2)
  22. #if defined(COMPILER_GCC) && defined(CPU_X86)
  23. #define ED25519_GCC_32BIT_SSE_CHOOSE
  24. #elif defined(COMPILER_GCC) && defined(CPU_X86_64)
  25. #define ED25519_GCC_64BIT_SSE_CHOOSE
  26. #endif
  27. #else
  28. #if defined(CPU_X86_64)
  29. #if defined(COMPILER_GCC)
  30. #if defined(ED25519_64BIT)
  31. #define ED25519_GCC_64BIT_X86_CHOOSE
  32. #else
  33. #define ED25519_GCC_64BIT_32BIT_CHOOSE
  34. #endif
  35. #endif
  36. #endif
  37. #endif
  38. #endif
  39. #if defined(ED25519_SSE2)
  40. #include "curve25519-donna-sse2.h"
  41. #elif defined(ED25519_64BIT)
  42. #include "curve25519-donna-64bit.h"
  43. #else
  44. #include "curve25519-donna-32bit.h"
  45. #endif
  46. #include "curve25519-donna-helpers.h"
  47. /* separate uint128 check for 64 bit sse2 */
  48. #if defined(HAVE_UINT128) && !defined(ED25519_FORCE_32BIT)
  49. #include "modm-donna-64bit.h"
  50. #else
  51. #include "modm-donna-32bit.h"
  52. #endif
  53. typedef unsigned char hash_512bits[64];
  54. /*
  55. Timing safe memory compare
  56. */
  57. static int
  58. ed25519_verify(const unsigned char *x, const unsigned char *y, size_t len) {
  59. size_t differentbits = 0;
  60. while (len--)
  61. differentbits |= (*x++ ^ *y++);
  62. /*coverity[overflow]*/
  63. return (int) (1 & ((differentbits - 1) >> 8));
  64. }
  65. /*
  66. * Arithmetic on the twisted Edwards curve -x^2 + y^2 = 1 + dx^2y^2
  67. * with d = -(121665/121666) = 37095705934669439343138083508754565189542113879843219016388785533085940283555
  68. * Base point: (15112221349535400772501151409588531511454012693041857206046113283949847762202,46316835694926478169428394003475163141307993866256225615783033603165251855960);
  69. */
  70. typedef struct ge25519_t {
  71. bignum25519 x, y, z, t;
  72. } ge25519;
  73. typedef struct ge25519_p1p1_t {
  74. bignum25519 x, y, z, t;
  75. } ge25519_p1p1;
  76. typedef struct ge25519_niels_t {
  77. bignum25519 ysubx, xaddy, t2d;
  78. } ge25519_niels;
  79. typedef struct ge25519_pniels_t {
  80. bignum25519 ysubx, xaddy, z, t2d;
  81. } ge25519_pniels;
  82. #include "ed25519-donna-basepoint-table.h"
  83. #if defined(ED25519_64BIT)
  84. #include "ed25519-donna-64bit-tables.h"
  85. #include "ed25519-donna-64bit-x86.h"
  86. #else
  87. #include "ed25519-donna-32bit-tables.h"
  88. #include "ed25519-donna-64bit-x86-32bit.h"
  89. #endif
  90. #if defined(ED25519_SSE2)
  91. #include "ed25519-donna-32bit-sse2.h"
  92. #include "ed25519-donna-64bit-sse2.h"
  93. #include "ed25519-donna-impl-sse2.h"
  94. #else
  95. #include "ed25519-donna-impl-base.h"
  96. #endif