You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
2.7 KiB

  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. return (int) (1 & ((differentbits - 1) >> 8));
  63. }
  64. /*
  65. * Arithmetic on the twisted Edwards curve -x^2 + y^2 = 1 + dx^2y^2
  66. * with d = -(121665/121666) = 37095705934669439343138083508754565189542113879843219016388785533085940283555
  67. * Base point: (15112221349535400772501151409588531511454012693041857206046113283949847762202,46316835694926478169428394003475163141307993866256225615783033603165251855960);
  68. */
  69. typedef struct ge25519_t {
  70. bignum25519 x, y, z, t;
  71. } ge25519;
  72. typedef struct ge25519_p1p1_t {
  73. bignum25519 x, y, z, t;
  74. } ge25519_p1p1;
  75. typedef struct ge25519_niels_t {
  76. bignum25519 ysubx, xaddy, t2d;
  77. } ge25519_niels;
  78. typedef struct ge25519_pniels_t {
  79. bignum25519 ysubx, xaddy, z, t2d;
  80. } ge25519_pniels;
  81. #include "ed25519-donna-basepoint-table.h"
  82. #if defined(ED25519_64BIT)
  83. #include "ed25519-donna-64bit-tables.h"
  84. #include "ed25519-donna-64bit-x86.h"
  85. #else
  86. #include "ed25519-donna-32bit-tables.h"
  87. #include "ed25519-donna-64bit-x86-32bit.h"
  88. #endif
  89. #if defined(ED25519_SSE2)
  90. #include "ed25519-donna-32bit-sse2.h"
  91. #include "ed25519-donna-64bit-sse2.h"
  92. #include "ed25519-donna-impl-sse2.h"
  93. #else
  94. #include "ed25519-donna-impl-base.h"
  95. #endif