curve25519_sandy2x.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. #define x1 var[0]
  16. #define x2 var[1]
  17. #define z2 var[2]
  18. static int
  19. crypto_scalarmult_curve25519_sandy2x(unsigned char *q, const unsigned char *n,
  20. const unsigned char *p)
  21. {
  22. unsigned char *t = q;
  23. fe var[3];
  24. fe51 x_51;
  25. fe51 z_51;
  26. unsigned int i;
  27. for (i = 0; i < 32; i++) {
  28. t[i] = n[i];
  29. }
  30. t[0] &= 248;
  31. t[31] &= 127;
  32. t[31] |= 64;
  33. fe_frombytes(x1, p);
  34. ladder(var, t);
  35. z_51.v[0] = (z2[1] << 26) + z2[0];
  36. z_51.v[1] = (z2[3] << 26) + z2[2];
  37. z_51.v[2] = (z2[5] << 26) + z2[4];
  38. z_51.v[3] = (z2[7] << 26) + z2[6];
  39. z_51.v[4] = (z2[9] << 26) + z2[8];
  40. x_51.v[0] = (x2[1] << 26) + x2[0];
  41. x_51.v[1] = (x2[3] << 26) + x2[2];
  42. x_51.v[2] = (x2[5] << 26) + x2[4];
  43. x_51.v[3] = (x2[7] << 26) + x2[6];
  44. x_51.v[4] = (x2[9] << 26) + x2[8];
  45. fe51_invert(&z_51, &z_51);
  46. fe51_mul(&x_51, &x_51, &z_51);
  47. fe51_pack(q, &x_51);
  48. return 0;
  49. }
  50. struct crypto_scalarmult_curve25519_implementation
  51. crypto_scalarmult_curve25519_sandy2x_implementation = {
  52. SODIUM_C99(.mult = ) crypto_scalarmult_curve25519_sandy2x,
  53. SODIUM_C99(.mult_base = ) NULL
  54. };
  55. #endif