ge25519.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. #include "fe25519.h"
  2. #include "sc25519.h"
  3. #include "ge25519.h"
  4. /*
  5. * Arithmetic on the twisted Edwards curve -x^2 + y^2 = 1 + dx^2y^2
  6. * with d = -(121665/121666) = 37095705934669439343138083508754565189542113879843219016388785533085940283555
  7. * Base point: (15112221349535400772501151409588531511454012693041857206046113283949847762202,46316835694926478169428394003475163141307993866256225615783033603165251855960);
  8. */
  9. /* d */
  10. static const fe25519 ge25519_ecd = {{0xA3, 0x78, 0x59, 0x13, 0xCA, 0x4D, 0xEB, 0x75, 0xAB, 0xD8, 0x41, 0x41, 0x4D, 0x0A, 0x70, 0x00,
  11. 0x98, 0xE8, 0x79, 0x77, 0x79, 0x40, 0xC7, 0x8C, 0x73, 0xFE, 0x6F, 0x2B, 0xEE, 0x6C, 0x03, 0x52}};
  12. /* 2*d */
  13. static const fe25519 ge25519_ec2d = {{0x59, 0xF1, 0xB2, 0x26, 0x94, 0x9B, 0xD6, 0xEB, 0x56, 0xB1, 0x83, 0x82, 0x9A, 0x14, 0xE0, 0x00,
  14. 0x30, 0xD1, 0xF3, 0xEE, 0xF2, 0x80, 0x8E, 0x19, 0xE7, 0xFC, 0xDF, 0x56, 0xDC, 0xD9, 0x06, 0x24}};
  15. /* sqrt(-1) */
  16. static const fe25519 ge25519_sqrtm1 = {{0xB0, 0xA0, 0x0E, 0x4A, 0x27, 0x1B, 0xEE, 0xC4, 0x78, 0xE4, 0x2F, 0xAD, 0x06, 0x18, 0x43, 0x2F,
  17. 0xA7, 0xD7, 0xFB, 0x3D, 0x99, 0x00, 0x4D, 0x2B, 0x0B, 0xDF, 0xC1, 0x4F, 0x80, 0x24, 0x83, 0x2B}};
  18. #define ge25519_p3 ge25519
  19. typedef struct
  20. {
  21. fe25519 x;
  22. fe25519 z;
  23. fe25519 y;
  24. fe25519 t;
  25. } ge25519_p1p1;
  26. typedef struct
  27. {
  28. fe25519 x;
  29. fe25519 y;
  30. fe25519 z;
  31. } ge25519_p2;
  32. typedef struct
  33. {
  34. fe25519 x;
  35. fe25519 y;
  36. } ge25519_aff;
  37. /* Packed coordinates of the base point */
  38. const ge25519 ge25519_base = {{{0x1A, 0xD5, 0x25, 0x8F, 0x60, 0x2D, 0x56, 0xC9, 0xB2, 0xA7, 0x25, 0x95, 0x60, 0xC7, 0x2C, 0x69,
  39. 0x5C, 0xDC, 0xD6, 0xFD, 0x31, 0xE2, 0xA4, 0xC0, 0xFE, 0x53, 0x6E, 0xCD, 0xD3, 0x36, 0x69, 0x21}},
  40. {{0x58, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  41. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66}},
  42. {{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  43. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
  44. {{0xA3, 0xDD, 0xB7, 0xA5, 0xB3, 0x8A, 0xDE, 0x6D, 0xF5, 0x52, 0x51, 0x77, 0x80, 0x9F, 0xF0, 0x20,
  45. 0x7D, 0xE3, 0xAB, 0x64, 0x8E, 0x4E, 0xEA, 0x66, 0x65, 0x76, 0x8B, 0xD7, 0x0F, 0x5F, 0x87, 0x67}}};
  46. /* Multiples of the base point in affine representation */
  47. static const ge25519_aff ge25519_base_multiples_affine[425] = {
  48. #include "ge25519_base.data"
  49. };
  50. static void p1p1_to_p2(ge25519_p2 *r, const ge25519_p1p1 *p)
  51. {
  52. fe25519_mul(&r->x, &p->x, &p->t);
  53. fe25519_mul(&r->y, &p->y, &p->z);
  54. fe25519_mul(&r->z, &p->z, &p->t);
  55. }
  56. static void p1p1_to_p3(ge25519_p3 *r, const ge25519_p1p1 *p)
  57. {
  58. p1p1_to_p2((ge25519_p2 *)r, p);
  59. fe25519_mul(&r->t, &p->x, &p->y);
  60. }
  61. static void ge25519_mixadd2(ge25519_p3 *r, const ge25519_aff *q)
  62. {
  63. fe25519 a,b,t1,t2,c,d,e,f,g,h,qt;
  64. fe25519_mul(&qt, &q->x, &q->y);
  65. fe25519_sub(&a, &r->y, &r->x); /* A = (Y1-X1)*(Y2-X2) */
  66. fe25519_add(&b, &r->y, &r->x); /* B = (Y1+X1)*(Y2+X2) */
  67. fe25519_sub(&t1, &q->y, &q->x);
  68. fe25519_add(&t2, &q->y, &q->x);
  69. fe25519_mul(&a, &a, &t1);
  70. fe25519_mul(&b, &b, &t2);
  71. fe25519_sub(&e, &b, &a); /* E = B-A */
  72. fe25519_add(&h, &b, &a); /* H = B+A */
  73. fe25519_mul(&c, &r->t, &qt); /* C = T1*k*T2 */
  74. fe25519_mul(&c, &c, &ge25519_ec2d);
  75. fe25519_add(&d, &r->z, &r->z); /* D = Z1*2 */
  76. fe25519_sub(&f, &d, &c); /* F = D-C */
  77. fe25519_add(&g, &d, &c); /* G = D+C */
  78. fe25519_mul(&r->x, &e, &f);
  79. fe25519_mul(&r->y, &h, &g);
  80. fe25519_mul(&r->z, &g, &f);
  81. fe25519_mul(&r->t, &e, &h);
  82. }
  83. static void add_p1p1(ge25519_p1p1 *r, const ge25519_p3 *p, const ge25519_p3 *q)
  84. {
  85. fe25519 a, b, c, d, t;
  86. fe25519_sub(&a, &p->y, &p->x); /* A = (Y1-X1)*(Y2-X2) */
  87. fe25519_sub(&t, &q->y, &q->x);
  88. fe25519_mul(&a, &a, &t);
  89. fe25519_add(&b, &p->x, &p->y); /* B = (Y1+X1)*(Y2+X2) */
  90. fe25519_add(&t, &q->x, &q->y);
  91. fe25519_mul(&b, &b, &t);
  92. fe25519_mul(&c, &p->t, &q->t); /* C = T1*k*T2 */
  93. fe25519_mul(&c, &c, &ge25519_ec2d);
  94. fe25519_mul(&d, &p->z, &q->z); /* D = Z1*2*Z2 */
  95. fe25519_add(&d, &d, &d);
  96. fe25519_sub(&r->x, &b, &a); /* E = B-A */
  97. fe25519_sub(&r->t, &d, &c); /* F = D-C */
  98. fe25519_add(&r->z, &d, &c); /* G = D+C */
  99. fe25519_add(&r->y, &b, &a); /* H = B+A */
  100. }
  101. /* See http://www.hyperelliptic.org/EFD/g1p/auto-twisted-extended-1.html#doubling-dbl-2008-hwcd */
  102. static void dbl_p1p1(ge25519_p1p1 *r, const ge25519_p2 *p)
  103. {
  104. fe25519 a,b,c,d;
  105. fe25519_square(&a, &p->x);
  106. fe25519_square(&b, &p->y);
  107. fe25519_square(&c, &p->z);
  108. fe25519_add(&c, &c, &c);
  109. fe25519_neg(&d, &a);
  110. fe25519_add(&r->x, &p->x, &p->y);
  111. fe25519_square(&r->x, &r->x);
  112. fe25519_sub(&r->x, &r->x, &a);
  113. fe25519_sub(&r->x, &r->x, &b);
  114. fe25519_add(&r->z, &d, &b);
  115. fe25519_sub(&r->t, &r->z, &c);
  116. fe25519_sub(&r->y, &d, &b);
  117. }
  118. /* Constant-time version of: if(b) r = p */
  119. static void cmov_aff(ge25519_aff *r, const ge25519_aff *p, unsigned char b)
  120. {
  121. fe25519_cmov(&r->x, &p->x, b);
  122. fe25519_cmov(&r->y, &p->y, b);
  123. }
  124. static unsigned char equal(signed char b,signed char c)
  125. {
  126. unsigned char ub = b;
  127. unsigned char uc = c;
  128. unsigned char x = ub ^ uc; /* 0: yes; 1..255: no */
  129. crypto_uint32 y = x; /* 0: yes; 1..255: no */
  130. y -= 1; /* 4294967295: yes; 0..254: no */
  131. y >>= 31; /* 1: yes; 0: no */
  132. return y;
  133. }
  134. static unsigned char negative(signed char b)
  135. {
  136. unsigned long long x = b; /* 18446744073709551361..18446744073709551615: yes; 0..255: no */
  137. x >>= 63; /* 1: yes; 0: no */
  138. return x;
  139. }
  140. static void choose_t(ge25519_aff *t, unsigned long long pos, signed char b)
  141. {
  142. /* constant time */
  143. fe25519 v;
  144. *t = ge25519_base_multiples_affine[5*pos+0];
  145. cmov_aff(t, &ge25519_base_multiples_affine[5*pos+1],equal(b,1) | equal(b,-1));
  146. cmov_aff(t, &ge25519_base_multiples_affine[5*pos+2],equal(b,2) | equal(b,-2));
  147. cmov_aff(t, &ge25519_base_multiples_affine[5*pos+3],equal(b,3) | equal(b,-3));
  148. cmov_aff(t, &ge25519_base_multiples_affine[5*pos+4],equal(b,-4));
  149. fe25519_neg(&v, &t->x);
  150. fe25519_cmov(&t->x, &v, negative(b));
  151. }
  152. static void setneutral(ge25519 *r)
  153. {
  154. fe25519_setzero(&r->x);
  155. fe25519_setone(&r->y);
  156. fe25519_setone(&r->z);
  157. fe25519_setzero(&r->t);
  158. }
  159. /* ********************************************************************
  160. * EXPORTED FUNCTIONS
  161. ******************************************************************** */
  162. /* return 0 on success, -1 otherwise */
  163. int ge25519_unpackneg_vartime(ge25519_p3 *r, const unsigned char p[32])
  164. {
  165. unsigned char par;
  166. fe25519 t, chk, num, den, den2, den4, den6;
  167. fe25519_setone(&r->z);
  168. par = p[31] >> 7;
  169. fe25519_unpack(&r->y, p);
  170. fe25519_square(&num, &r->y); /* x = y^2 */
  171. fe25519_mul(&den, &num, &ge25519_ecd); /* den = dy^2 */
  172. fe25519_sub(&num, &num, &r->z); /* x = y^2-1 */
  173. fe25519_add(&den, &r->z, &den); /* den = dy^2+1 */
  174. /* Computation of sqrt(num/den) */
  175. /* 1.: computation of num^((p-5)/8)*den^((7p-35)/8) = (num*den^7)^((p-5)/8) */
  176. fe25519_square(&den2, &den);
  177. fe25519_square(&den4, &den2);
  178. fe25519_mul(&den6, &den4, &den2);
  179. fe25519_mul(&t, &den6, &num);
  180. fe25519_mul(&t, &t, &den);
  181. fe25519_pow2523(&t, &t);
  182. /* 2. computation of r->x = t * num * den^3 */
  183. fe25519_mul(&t, &t, &num);
  184. fe25519_mul(&t, &t, &den);
  185. fe25519_mul(&t, &t, &den);
  186. fe25519_mul(&r->x, &t, &den);
  187. /* 3. Check whether sqrt computation gave correct result, multiply by sqrt(-1) if not: */
  188. fe25519_square(&chk, &r->x);
  189. fe25519_mul(&chk, &chk, &den);
  190. if (!fe25519_iseq_vartime(&chk, &num))
  191. fe25519_mul(&r->x, &r->x, &ge25519_sqrtm1);
  192. /* 4. Now we have one of the two square roots, except if input was not a square */
  193. fe25519_square(&chk, &r->x);
  194. fe25519_mul(&chk, &chk, &den);
  195. if (!fe25519_iseq_vartime(&chk, &num))
  196. return -1;
  197. /* 5. Choose the desired square root according to parity: */
  198. if(fe25519_getparity(&r->x) != (1-par))
  199. fe25519_neg(&r->x, &r->x);
  200. fe25519_mul(&r->t, &r->x, &r->y);
  201. return 0;
  202. }
  203. void ge25519_pack(unsigned char r[32], const ge25519_p3 *p)
  204. {
  205. fe25519 tx, ty, zi;
  206. fe25519_invert(&zi, &p->z);
  207. fe25519_mul(&tx, &p->x, &zi);
  208. fe25519_mul(&ty, &p->y, &zi);
  209. fe25519_pack(r, &ty);
  210. r[31] ^= fe25519_getparity(&tx) << 7;
  211. }
  212. int ge25519_isneutral_vartime(const ge25519_p3 *p)
  213. {
  214. int ret = 1;
  215. if(!fe25519_iszero(&p->x)) ret = 0;
  216. if(!fe25519_iseq_vartime(&p->y, &p->z)) ret = 0;
  217. return ret;
  218. }
  219. /* computes [s1]p1 + [s2]p2 */
  220. void ge25519_double_scalarmult_vartime(ge25519_p3 *r, const ge25519_p3 *p1, const sc25519 *s1, const ge25519_p3 *p2, const sc25519 *s2)
  221. {
  222. ge25519_p1p1 tp1p1;
  223. ge25519_p3 pre[16];
  224. unsigned char b[127];
  225. int i;
  226. /* precomputation s2 s1 */
  227. setneutral(pre); /* 00 00 */
  228. pre[1] = *p1; /* 00 01 */
  229. dbl_p1p1(&tp1p1,(ge25519_p2 *)p1); p1p1_to_p3( &pre[2], &tp1p1); /* 00 10 */
  230. add_p1p1(&tp1p1,&pre[1], &pre[2]); p1p1_to_p3( &pre[3], &tp1p1); /* 00 11 */
  231. pre[4] = *p2; /* 01 00 */
  232. add_p1p1(&tp1p1,&pre[1], &pre[4]); p1p1_to_p3( &pre[5], &tp1p1); /* 01 01 */
  233. add_p1p1(&tp1p1,&pre[2], &pre[4]); p1p1_to_p3( &pre[6], &tp1p1); /* 01 10 */
  234. add_p1p1(&tp1p1,&pre[3], &pre[4]); p1p1_to_p3( &pre[7], &tp1p1); /* 01 11 */
  235. dbl_p1p1(&tp1p1,(ge25519_p2 *)p2); p1p1_to_p3( &pre[8], &tp1p1); /* 10 00 */
  236. add_p1p1(&tp1p1,&pre[1], &pre[8]); p1p1_to_p3( &pre[9], &tp1p1); /* 10 01 */
  237. dbl_p1p1(&tp1p1,(ge25519_p2 *)&pre[5]); p1p1_to_p3(&pre[10], &tp1p1); /* 10 10 */
  238. add_p1p1(&tp1p1,&pre[3], &pre[8]); p1p1_to_p3(&pre[11], &tp1p1); /* 10 11 */
  239. add_p1p1(&tp1p1,&pre[4], &pre[8]); p1p1_to_p3(&pre[12], &tp1p1); /* 11 00 */
  240. add_p1p1(&tp1p1,&pre[1],&pre[12]); p1p1_to_p3(&pre[13], &tp1p1); /* 11 01 */
  241. add_p1p1(&tp1p1,&pre[2],&pre[12]); p1p1_to_p3(&pre[14], &tp1p1); /* 11 10 */
  242. add_p1p1(&tp1p1,&pre[3],&pre[12]); p1p1_to_p3(&pre[15], &tp1p1); /* 11 11 */
  243. sc25519_2interleave2(b,s1,s2);
  244. /* scalar multiplication */
  245. *r = pre[b[126]];
  246. for(i=125;i>=0;i--)
  247. {
  248. dbl_p1p1(&tp1p1, (ge25519_p2 *)r);
  249. p1p1_to_p2((ge25519_p2 *) r, &tp1p1);
  250. dbl_p1p1(&tp1p1, (ge25519_p2 *)r);
  251. if(b[i]!=0)
  252. {
  253. p1p1_to_p3(r, &tp1p1);
  254. add_p1p1(&tp1p1, r, &pre[b[i]]);
  255. }
  256. if(i != 0) p1p1_to_p2((ge25519_p2 *)r, &tp1p1);
  257. else p1p1_to_p3(r, &tp1p1);
  258. }
  259. }
  260. void ge25519_scalarmult_base(ge25519_p3 *r, const sc25519 *s)
  261. {
  262. signed char b[85];
  263. int i;
  264. ge25519_aff t;
  265. sc25519_window3(b,s);
  266. choose_t((ge25519_aff *)r, 0, b[0]);
  267. fe25519_setone(&r->z);
  268. fe25519_mul(&r->t, &r->x, &r->y);
  269. for(i=1;i<85;i++)
  270. {
  271. choose_t(&t, (unsigned long long) i, b[i]);
  272. ge25519_mixadd2(r, &t);
  273. }
  274. }