ed25519.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* Edwards curve operations
  2. * Daniel Beer <dlbeer@gmail.com>, 9 Jan 2014
  3. *
  4. * This file is in the public domain.
  5. */
  6. #ifndef ED25519_H_
  7. #define ED25519_H_
  8. #include "f25519.h"
  9. /* This is not the Ed25519 signature system. Rather, we're implementing
  10. * basic operations on the twisted Edwards curve over (Z mod 2^255-19):
  11. *
  12. * -x^2 + y^2 = 1 - (121665/121666)x^2y^2
  13. *
  14. * With the positive-x base point y = 4/5.
  15. *
  16. * These functions will not leak secret data through timing.
  17. *
  18. * For more information, see:
  19. *
  20. * Bernstein, D.J. & Lange, T. (2007) "Faster addition and doubling on
  21. * elliptic curves". Document ID: 95616567a6ba20f575c5f25e7cebaf83.
  22. *
  23. * Hisil, H. & Wong, K K. & Carter, G. & Dawson, E. (2008) "Twisted
  24. * Edwards curves revisited". Advances in Cryptology, ASIACRYPT 2008,
  25. * Vol. 5350, pp. 326-343.
  26. */
  27. /* Projective coordinates */
  28. struct ed25519_pt {
  29. uint8_t x[F25519_SIZE];
  30. uint8_t y[F25519_SIZE];
  31. uint8_t t[F25519_SIZE];
  32. uint8_t z[F25519_SIZE];
  33. };
  34. extern const struct ed25519_pt ed25519_base;
  35. /* Convert between projective and affine coordinates (x/y in F25519) */
  36. void ed25519_project(struct ed25519_pt *p,
  37. const uint8_t *x, const uint8_t *y);
  38. void ed25519_unproject(uint8_t *x, uint8_t *y,
  39. const struct ed25519_pt *p);
  40. /* Compress/uncompress points. try_unpack() will check that the
  41. * compressed point is on the curve, returning 1 if the unpacked point
  42. * is valid, and 0 otherwise.
  43. */
  44. #define ED25519_PACK_SIZE F25519_SIZE
  45. void ed25519_pack(uint8_t *c, const uint8_t *x, const uint8_t *y);
  46. uint8_t ed25519_try_unpack(uint8_t *x, uint8_t *y, const uint8_t *c);
  47. /* Add, double and scalar multiply */
  48. #define ED25519_EXPONENT_SIZE 32
  49. /* Prepare an exponent by clamping appropriate bits */
  50. static inline void ed25519_prepare(uint8_t *e)
  51. {
  52. e[0] &= 0xf8;
  53. e[31] &= 0x7f;
  54. e[31] |= 0x40;
  55. }
  56. /* Order of the group generated by the base point */
  57. static inline void ed25519_copy(struct ed25519_pt *dst,
  58. const struct ed25519_pt *src)
  59. {
  60. memcpy(dst, src, sizeof(*dst));
  61. }
  62. void ed25519_add(struct ed25519_pt *r,
  63. const struct ed25519_pt *a, const struct ed25519_pt *b);
  64. void ed25519_smult(struct ed25519_pt *r, const struct ed25519_pt *a,
  65. const uint8_t *e);
  66. #endif