curve25519_sandy2x.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. This file is adapted from ref10/scalarmult.c:
  3. The code for Mongomery ladder is replace by the ladder assembly function;
  4. Inversion is done in the same way as amd64-51/.
  5. (fe is first converted into fe51 after Mongomery ladder)
  6. */
  7. #include <stddef.h>
  8. #ifdef HAVE_AVX_ASM
  9. #include "utils.h"
  10. #include "curve25519_sandy2x.h"
  11. #include "../scalarmult_curve25519.h"
  12. #include "fe.h"
  13. #include "fe51.h"
  14. #include "ladder.h"
  15. #include "ladder_base.h"
  16. #define x1 var[0]
  17. #define x2 var[1]
  18. #define z2 var[2]
  19. static int
  20. crypto_scalarmult_curve25519_sandy2x(unsigned char *q, const unsigned char *n,
  21. const unsigned char *p)
  22. {
  23. unsigned char *t = q;
  24. fe var[3];
  25. fe51 x_51;
  26. fe51 z_51;
  27. unsigned int i;
  28. for (i = 0; i < 32; i++) {
  29. t[i] = n[i];
  30. }
  31. t[0] &= 248;
  32. t[31] &= 127;
  33. t[31] |= 64;
  34. fe_frombytes(x1, p);
  35. ladder(var, t);
  36. z_51.v[0] = (z2[1] << 26) + z2[0];
  37. z_51.v[1] = (z2[3] << 26) + z2[2];
  38. z_51.v[2] = (z2[5] << 26) + z2[4];
  39. z_51.v[3] = (z2[7] << 26) + z2[6];
  40. z_51.v[4] = (z2[9] << 26) + z2[8];
  41. x_51.v[0] = (x2[1] << 26) + x2[0];
  42. x_51.v[1] = (x2[3] << 26) + x2[2];
  43. x_51.v[2] = (x2[5] << 26) + x2[4];
  44. x_51.v[3] = (x2[7] << 26) + x2[6];
  45. x_51.v[4] = (x2[9] << 26) + x2[8];
  46. fe51_invert(&z_51, &z_51);
  47. fe51_mul(&x_51, &x_51, &z_51);
  48. fe51_pack(q, &x_51);
  49. return 0;
  50. }
  51. #undef x2
  52. #undef z2
  53. #define x2 var[0]
  54. #define z2 var[1]
  55. static int
  56. crypto_scalarmult_curve25519_sandy2x_base(unsigned char *q,
  57. const unsigned char *n)
  58. {
  59. unsigned char *t = q;
  60. fe var[3];
  61. fe51 x_51;
  62. fe51 z_51;
  63. unsigned int i;
  64. for (i = 0;i < 32; i++) {
  65. t[i] = n[i];
  66. }
  67. t[0] &= 248;
  68. t[31] &= 127;
  69. t[31] |= 64;
  70. ladder_base(var, t);
  71. z_51.v[0] = (z2[1] << 26) + z2[0];
  72. z_51.v[1] = (z2[3] << 26) + z2[2];
  73. z_51.v[2] = (z2[5] << 26) + z2[4];
  74. z_51.v[3] = (z2[7] << 26) + z2[6];
  75. z_51.v[4] = (z2[9] << 26) + z2[8];
  76. x_51.v[0] = (x2[1] << 26) + x2[0];
  77. x_51.v[1] = (x2[3] << 26) + x2[2];
  78. x_51.v[2] = (x2[5] << 26) + x2[4];
  79. x_51.v[3] = (x2[7] << 26) + x2[6];
  80. x_51.v[4] = (x2[9] << 26) + x2[8];
  81. fe51_invert(&z_51, &z_51);
  82. fe51_mul(&x_51, &x_51, &z_51);
  83. fe51_pack(q, &x_51);
  84. return 0;
  85. }
  86. struct crypto_scalarmult_curve25519_implementation
  87. crypto_scalarmult_curve25519_sandy2x_implementation = {
  88. SODIUM_C99(.mult = ) crypto_scalarmult_curve25519_sandy2x,
  89. SODIUM_C99(.mult_base = ) crypto_scalarmult_curve25519_sandy2x_base
  90. };
  91. #endif