ed25519.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /* Edwards curve operations
  2. * Daniel Beer <dlbeer@gmail.com>, 9 Jan 2014
  3. *
  4. * This file is in the public domain.
  5. */
  6. #include "ed25519.h"
  7. /* Base point is (numbers wrapped):
  8. *
  9. * x = 151122213495354007725011514095885315114
  10. * 54012693041857206046113283949847762202
  11. * y = 463168356949264781694283940034751631413
  12. * 07993866256225615783033603165251855960
  13. *
  14. * y is derived by transforming the original Montgomery base (u=9). x
  15. * is the corresponding positive coordinate for the new curve equation.
  16. * t is x*y.
  17. */
  18. const struct ed25519_pt ed25519_base = {
  19. .x = {
  20. 0x1a, 0xd5, 0x25, 0x8f, 0x60, 0x2d, 0x56, 0xc9,
  21. 0xb2, 0xa7, 0x25, 0x95, 0x60, 0xc7, 0x2c, 0x69,
  22. 0x5c, 0xdc, 0xd6, 0xfd, 0x31, 0xe2, 0xa4, 0xc0,
  23. 0xfe, 0x53, 0x6e, 0xcd, 0xd3, 0x36, 0x69, 0x21
  24. },
  25. .y = {
  26. 0x58, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  27. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  28. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  29. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66
  30. },
  31. .t = {
  32. 0xa3, 0xdd, 0xb7, 0xa5, 0xb3, 0x8a, 0xde, 0x6d,
  33. 0xf5, 0x52, 0x51, 0x77, 0x80, 0x9f, 0xf0, 0x20,
  34. 0x7d, 0xe3, 0xab, 0x64, 0x8e, 0x4e, 0xea, 0x66,
  35. 0x65, 0x76, 0x8b, 0xd7, 0x0f, 0x5f, 0x87, 0x67
  36. },
  37. .z = {1, 0}
  38. };
  39. static const struct ed25519_pt ed25519_neutral = {
  40. .x = {0},
  41. .y = {1, 0},
  42. .t = {0},
  43. .z = {1, 0}
  44. };
  45. /* Conversion to and from projective coordinates */
  46. void ed25519_project(struct ed25519_pt *p,
  47. const uint8_t *x, const uint8_t *y)
  48. {
  49. f25519_copy(p->x, x);
  50. f25519_copy(p->y, y);
  51. f25519_load(p->z, 1);
  52. f25519_mul__distinct(p->t, x, y);
  53. }
  54. void ed25519_unproject(uint8_t *x, uint8_t *y,
  55. const struct ed25519_pt *p)
  56. {
  57. uint8_t z1[F25519_SIZE];
  58. f25519_inv__distinct(z1, p->z);
  59. f25519_mul__distinct(x, p->x, z1);
  60. f25519_mul__distinct(y, p->y, z1);
  61. f25519_normalize(x);
  62. f25519_normalize(y);
  63. }
  64. /* Compress/uncompress points. We compress points by storing the x
  65. * coordinate and the parity of the y coordinate.
  66. *
  67. * Rearranging the curve equation, we obtain explicit formulae for the
  68. * coordinates:
  69. *
  70. * x = sqrt((y^2-1) / (1+dy^2))
  71. * y = sqrt((x^2+1) / (1-dx^2))
  72. *
  73. * Where d = (-121665/121666), or:
  74. *
  75. * d = 370957059346694393431380835087545651895
  76. * 42113879843219016388785533085940283555
  77. */
  78. static const uint8_t ed25519_d[F25519_SIZE] = {
  79. 0xa3, 0x78, 0x59, 0x13, 0xca, 0x4d, 0xeb, 0x75,
  80. 0xab, 0xd8, 0x41, 0x41, 0x4d, 0x0a, 0x70, 0x00,
  81. 0x98, 0xe8, 0x79, 0x77, 0x79, 0x40, 0xc7, 0x8c,
  82. 0x73, 0xfe, 0x6f, 0x2b, 0xee, 0x6c, 0x03, 0x52
  83. };
  84. void ed25519_pack(uint8_t *c, const uint8_t *x, const uint8_t *y)
  85. {
  86. uint8_t tmp[F25519_SIZE];
  87. uint8_t parity;
  88. f25519_copy(tmp, x);
  89. f25519_normalize(tmp);
  90. parity = (tmp[0] & 1) << 7;
  91. f25519_copy(c, y);
  92. f25519_normalize(c);
  93. c[31] |= parity;
  94. }
  95. uint8_t ed25519_try_unpack(uint8_t *x, uint8_t *y, const uint8_t *comp)
  96. {
  97. const int parity = comp[31] >> 7;
  98. uint8_t a[F25519_SIZE];
  99. uint8_t b[F25519_SIZE];
  100. uint8_t c[F25519_SIZE];
  101. /* Unpack y */
  102. f25519_copy(y, comp);
  103. y[31] &= 127;
  104. /* Compute c = y^2 */
  105. f25519_mul__distinct(c, y, y);
  106. /* Compute b = (1+dy^2)^-1 */
  107. f25519_mul__distinct(b, c, ed25519_d);
  108. f25519_add(a, b, f25519_one);
  109. f25519_inv__distinct(b, a);
  110. /* Compute a = y^2-1 */
  111. f25519_sub(a, c, f25519_one);
  112. /* Compute c = a*b = (y^2-1)/(1-dy^2) */
  113. f25519_mul__distinct(c, a, b);
  114. /* Compute a, b = +/-sqrt(c), if c is square */
  115. f25519_sqrt(a, c);
  116. f25519_neg(b, a);
  117. /* Select one of them, based on the compressed parity bit */
  118. f25519_select(x, a, b, (a[0] ^ parity) & 1);
  119. /* Verify that x^2 = c */
  120. f25519_mul__distinct(a, x, x);
  121. f25519_normalize(a);
  122. f25519_normalize(c);
  123. return f25519_eq(a, c);
  124. }
  125. /* k = 2d */
  126. static const uint8_t ed25519_k[F25519_SIZE] = {
  127. 0x59, 0xf1, 0xb2, 0x26, 0x94, 0x9b, 0xd6, 0xeb,
  128. 0x56, 0xb1, 0x83, 0x82, 0x9a, 0x14, 0xe0, 0x00,
  129. 0x30, 0xd1, 0xf3, 0xee, 0xf2, 0x80, 0x8e, 0x19,
  130. 0xe7, 0xfc, 0xdf, 0x56, 0xdc, 0xd9, 0x06, 0x24
  131. };
  132. void ed25519_add(struct ed25519_pt *r,
  133. const struct ed25519_pt *p1, const struct ed25519_pt *p2)
  134. {
  135. /* Explicit formulas database: add-2008-hwcd-3
  136. *
  137. * source 2008 Hisil--Wong--Carter--Dawson,
  138. * http://eprint.iacr.org/2008/522, Section 3.1
  139. * appliesto extended-1
  140. * parameter k
  141. * assume k = 2 d
  142. * compute A = (Y1-X1)(Y2-X2)
  143. * compute B = (Y1+X1)(Y2+X2)
  144. * compute C = T1 k T2
  145. * compute D = Z1 2 Z2
  146. * compute E = B - A
  147. * compute F = D - C
  148. * compute G = D + C
  149. * compute H = B + A
  150. * compute X3 = E F
  151. * compute Y3 = G H
  152. * compute T3 = E H
  153. * compute Z3 = F G
  154. */
  155. uint8_t a[F25519_SIZE];
  156. uint8_t b[F25519_SIZE];
  157. uint8_t c[F25519_SIZE];
  158. uint8_t d[F25519_SIZE];
  159. uint8_t e[F25519_SIZE];
  160. uint8_t f[F25519_SIZE];
  161. uint8_t g[F25519_SIZE];
  162. uint8_t h[F25519_SIZE];
  163. /* A = (Y1-X1)(Y2-X2) */
  164. f25519_sub(c, p1->y, p1->x);
  165. f25519_sub(d, p2->y, p2->x);
  166. f25519_mul__distinct(a, c, d);
  167. /* B = (Y1+X1)(Y2+X2) */
  168. f25519_add(c, p1->y, p1->x);
  169. f25519_add(d, p2->y, p2->x);
  170. f25519_mul__distinct(b, c, d);
  171. /* C = T1 k T2 */
  172. f25519_mul__distinct(d, p1->t, p2->t);
  173. f25519_mul__distinct(c, d, ed25519_k);
  174. /* D = Z1 2 Z2 */
  175. f25519_mul__distinct(d, p1->z, p2->z);
  176. f25519_add(d, d, d);
  177. /* E = B - A */
  178. f25519_sub(e, b, a);
  179. /* F = D - C */
  180. f25519_sub(f, d, c);
  181. /* G = D + C */
  182. f25519_add(g, d, c);
  183. /* H = B + A */
  184. f25519_add(h, b, a);
  185. /* X3 = E F */
  186. f25519_mul__distinct(r->x, e, f);
  187. /* Y3 = G H */
  188. f25519_mul__distinct(r->y, g, h);
  189. /* T3 = E H */
  190. f25519_mul__distinct(r->t, e, h);
  191. /* Z3 = F G */
  192. f25519_mul__distinct(r->z, f, g);
  193. }
  194. static void ed25519_double(struct ed25519_pt *r, const struct ed25519_pt *p)
  195. {
  196. /* Explicit formulas database: dbl-2008-hwcd
  197. *
  198. * source 2008 Hisil--Wong--Carter--Dawson,
  199. * http://eprint.iacr.org/2008/522, Section 3.3
  200. * compute A = X1^2
  201. * compute B = Y1^2
  202. * compute C = 2 Z1^2
  203. * compute D = a A
  204. * compute E = (X1+Y1)^2-A-B
  205. * compute G = D + B
  206. * compute F = G - C
  207. * compute H = D - B
  208. * compute X3 = E F
  209. * compute Y3 = G H
  210. * compute T3 = E H
  211. * compute Z3 = F G
  212. */
  213. uint8_t a[F25519_SIZE];
  214. uint8_t b[F25519_SIZE];
  215. uint8_t c[F25519_SIZE];
  216. uint8_t e[F25519_SIZE];
  217. uint8_t f[F25519_SIZE];
  218. uint8_t g[F25519_SIZE];
  219. uint8_t h[F25519_SIZE];
  220. /* A = X1^2 */
  221. f25519_mul__distinct(a, p->x, p->x);
  222. /* B = Y1^2 */
  223. f25519_mul__distinct(b, p->y, p->y);
  224. /* C = 2 Z1^2 */
  225. f25519_mul__distinct(c, p->z, p->z);
  226. f25519_add(c, c, c);
  227. /* D = a A (alter sign) */
  228. /* E = (X1+Y1)^2-A-B */
  229. f25519_add(f, p->x, p->y);
  230. f25519_mul__distinct(e, f, f);
  231. f25519_sub(e, e, a);
  232. f25519_sub(e, e, b);
  233. /* G = D + B */
  234. f25519_sub(g, b, a);
  235. /* F = G - C */
  236. f25519_sub(f, g, c);
  237. /* H = D - B */
  238. f25519_neg(h, b);
  239. f25519_sub(h, h, a);
  240. /* X3 = E F */
  241. f25519_mul__distinct(r->x, e, f);
  242. /* Y3 = G H */
  243. f25519_mul__distinct(r->y, g, h);
  244. /* T3 = E H */
  245. f25519_mul__distinct(r->t, e, h);
  246. /* Z3 = F G */
  247. f25519_mul__distinct(r->z, f, g);
  248. }
  249. void ed25519_smult(struct ed25519_pt *r_out, const struct ed25519_pt *p,
  250. const uint8_t *e)
  251. {
  252. struct ed25519_pt r;
  253. int i;
  254. ed25519_copy(&r, &ed25519_neutral);
  255. for (i = 255; i >= 0; i--) {
  256. const uint8_t bit = (e[i >> 3] >> (i & 7)) & 1;
  257. struct ed25519_pt s;
  258. ed25519_double(&r, &r);
  259. ed25519_add(&s, &r, p);
  260. f25519_select(r.x, r.x, s.x, bit);
  261. f25519_select(r.y, r.y, s.y, bit);
  262. f25519_select(r.z, r.z, s.z, bit);
  263. f25519_select(r.t, r.t, s.t, bit);
  264. }
  265. ed25519_copy(r_out, &r);
  266. }