1
0

ed25519_impl_pre.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #ifndef ED25519_donna
  2. # if defined(_MSC_VER)
  3. # define ALIGN(x) __declspec(align(x))
  4. # elif defined(__GNUC__)
  5. # undef ALIGN
  6. # define ALIGN(x) __attribute__((aligned(x)))
  7. # else
  8. # ifndef ALIGN
  9. # define ALIGN(x)
  10. # endif
  11. # endif
  12. #endif
  13. #define ed25519_seckey CRYPTO_NAMESPACE(seckey)
  14. #define ed25519_seckey_expand CRYPTO_NAMESPACE(seckey_expand)
  15. #define ed25519_pubkey CRYPTO_NAMESPACE(pubkey)
  16. #define ed25519_keygen CRYPTO_NAMESPACE(keygen)
  17. #define ge_eightpoint CRYPTO_NAMESPACE(ge_eightpoint)
  18. #define ge_initeightpoint CRYPTO_NAMESPACE(ge_initeightpoint)
  19. #ifdef ED25519_ref10
  20. #include "ref10/crypto_sign.h"
  21. #include "ref10/ge.h"
  22. /* The basepoint multiplied by 8. */
  23. static const ge_cached ge_eightpoint = {
  24. /* YplusX */
  25. {
  26. 48496028, -16430416, 15164263, 11885335, 60784617, -4866353, 46481863,
  27. -2771805, 9708580, 2387263
  28. },
  29. /* YmunusX */
  30. {
  31. -10173472, -5540046, 21277639, 4080693, 1932823, -14916249, -9515873,
  32. -21787995, -36575460, 29827857
  33. },
  34. /* Z */
  35. {
  36. 25143927, -10256223, -3515585, 5715072, 19432778, -14905909, 22462083,
  37. -8862871, 13226552, 743677
  38. },
  39. /* T2d */
  40. {
  41. -784818, -8208065, -28479270, 5551579, 15746872, 4911053, 19117091,
  42. 11267669, -24569594, 14624995
  43. }
  44. };
  45. inline static void ge_initeightpoint(void) {}
  46. #endif
  47. #ifdef ED25519_amd64_51_30k
  48. #include "amd64-51-30k/crypto_sign.h"
  49. #include "amd64-51-30k/ge25519.h"
  50. #endif
  51. #ifdef ED25519_amd64_64_24k
  52. #include "amd64-64-24k/crypto_sign.h"
  53. #include "amd64-64-24k/ge25519.h"
  54. #endif
  55. // common
  56. #if defined(ED25519_amd64_51_30k) || defined(ED25519_amd64_64_24k)
  57. #define fe fe25519
  58. #define ge_p1p1 ge25519_p1p1
  59. #define ge_p3 ge25519_p3
  60. #define ge_p1p1_to_p3 ge25519_p1p1_to_p3
  61. #define ge_p3_tobytes ge25519_pack
  62. #define ge_add ge25519_pnielsadd_p1p1
  63. #define ge_p3_batchtobytes_destructive_1 ge25519_batchpack_destructive_1
  64. #define ge_p3_batchtobytes_destructive_finish ge25519_batchpack_destructive_finish
  65. #define ge_scalarmult_base CRYPTO_NAMESPACE(ge_scalarmult_base)
  66. #endif
  67. #ifdef ED25519_amd64_51_30k
  68. static inline void ge_scalarmult_base(ge_p3 *gepk,const unsigned char *sk)
  69. {
  70. sc25519 scsk;
  71. sc25519_from32bytes(&scsk,sk);
  72. ge25519_scalarmult_base(gepk,&scsk);
  73. }
  74. /* The basepoint multiplied by 8. */
  75. static const ge25519_pniels ge_eightpoint = {
  76. // ysubx
  77. {{ 1880013609944032, 273850692840390, 1250787290086935, 789632210881694, 2001713562248987 }},
  78. // xaddy
  79. {{ 1149173309373852, 797611345273702, 1925224452816873, 2065787175387590, 160206517707811 }},
  80. // z
  81. {{ 1563516364368503, 383531986082622, 1251481213240650, 1657022631558786, 49907331879479 }},
  82. // t2d
  83. {{ 1700965895112270, 372560131616985, 329575203620664, 756160485635107, 981466775886086 }},
  84. };
  85. inline static void ge_initeightpoint(void) {}
  86. #endif
  87. #ifdef ED25519_amd64_64_24k
  88. static inline void ge_scalarmult_base(ge_p3 *gepk,const unsigned char *sk)
  89. {
  90. sc25519 scsk;
  91. sc25519_from32bytes(&scsk,sk);
  92. ge25519_scalarmult_base(gepk,&scsk);
  93. }
  94. /* The basepoint multiplied by 8. */
  95. static const ge25519_pniels ge_eightpoint = {
  96. // ysubx
  97. {{ 6788804652057281504U, 531290374162262565U, 6135835192563885415U, 8199018750971852188U }},
  98. // xaddy
  99. {{ 1960215011215539612U, 16708348392717346619U, 11897818088205565647U, 656205896531197613U }},
  100. // z
  101. {{ 15705615417005288055U, 5341641389565279826U, 1966574939768917451U, 204420431378348998U }},
  102. // t2d
  103. {{ 9713713562319586894U, 4328467261753610859U, 8262494979546083277U, 4020087914029409631U }},
  104. };
  105. inline static void ge_initeightpoint(void) {}
  106. #endif
  107. #ifdef ED25519_donna
  108. #define ED25519_CUSTOMRANDOM
  109. #define ED25519_CUSTOMHASH
  110. #include <sodium/crypto_hash_sha512.h>
  111. #include <sodium/randombytes.h>
  112. #include "ed25519-donna/ed25519-donna.h"
  113. static int ed25519_seckey_expand(unsigned char *sk,const unsigned char *seed)
  114. {
  115. crypto_hash_sha512(sk,seed,32);
  116. sk[0] &= 248;
  117. sk[31] &= 127;
  118. sk[31] |= 64;
  119. return 0;
  120. }
  121. static int ed25519_seckey(unsigned char *sk)
  122. {
  123. unsigned char seed[32];
  124. randombytes(seed,32);
  125. return ed25519_seckey_expand(sk,seed);
  126. }
  127. static int ed25519_pubkey(unsigned char *pk,const unsigned char *sk)
  128. {
  129. bignum256modm a;
  130. ge25519 ALIGN(16) A;
  131. expand256_modm(a,sk,32);
  132. ge25519_scalarmult_base_niels(&A,ge25519_niels_base_multiples,a);
  133. ge25519_pack(pk,&A);
  134. return 0;
  135. }
  136. static int ed25519_keypair(unsigned char *pk,unsigned char *sk)
  137. {
  138. ed25519_seckey(sk);
  139. ed25519_pubkey(pk,sk);
  140. return 0;
  141. }
  142. #define fe bignum25519
  143. #define ge_p1p1 ge25519_p1p1
  144. #define ge_p3 ge25519
  145. #define ge_p1p1_to_p3 ge25519_p1p1_to_full
  146. #define ge_p3_tobytes ge25519_pack
  147. #define ge_p3_batchtobytes_destructive_1 ge25519_batchpack_destructive_1
  148. #define ge_p3_batchtobytes_destructive_finish ge25519_batchpack_destructive_finish
  149. #define ge_add CRYPTO_NAMESPACE(ge_add)
  150. #define ge_scalarmult_base CRYPTO_NAMESPACE(ge_scalarmult_base)
  151. DONNA_INLINE static void ge_add(ge25519_p1p1 *r,const ge25519 *p,const ge25519_pniels *q)
  152. {
  153. ge25519_pnielsadd_p1p1(r,p,q,0);
  154. }
  155. DONNA_INLINE static void ge_scalarmult_base(ge25519 *A,const unsigned char *sk)
  156. {
  157. bignum256modm ALIGN(16) a;
  158. expand256_modm(a,sk,32);
  159. ge25519_scalarmult_base_niels(A,ge25519_niels_base_multiples,a);
  160. }
  161. #define fe_ysubx CRYPTO_NAMESPACE(fe_ysubx)
  162. #define fe_xaddy CRYPTO_NAMESPACE(fe_xaddy)
  163. #define fe_z CRYPTO_NAMESPACE(fe_z)
  164. #define fe_t2d CRYPTO_NAMESPACE(fe_t2d)
  165. static ge25519_pniels ALIGN(16) ge_eightpoint;
  166. // portable representation of (basepoint * 8)
  167. static u8 fe_ysubx[32] = {
  168. 0xE0,0xC3,0x64,0xC7,0xDC,0xAD,0x36,0x5E,
  169. 0x25,0xAA,0x86,0xC8,0xC7,0x85,0x5F,0x07,
  170. 0x67,0x65,0x1C,0x3D,0x99,0xDD,0x26,0x55,
  171. 0x9C,0xB5,0x71,0x1E,0x1D,0xC4,0xC8,0x71,
  172. };
  173. static u8 fe_xaddy[32] = {
  174. 0x9C,0xFD,0xE3,0xC2,0x2A,0x15,0x34,0x1B,
  175. 0x3B,0xE7,0x62,0xAB,0x56,0xFA,0xDF,0xE7,
  176. 0xCF,0xBE,0xB5,0x8D,0x83,0x8A,0x1D,0xA5,
  177. 0xAD,0x3E,0x42,0x42,0xC9,0x4F,0x1B,0x09,
  178. };
  179. static u8 fe_z[32] = {
  180. 0x77,0xAA,0x7F,0x85,0x02,0x8E,0xF5,0xD9,
  181. 0x52,0xFE,0x8F,0xE6,0x8A,0x52,0x21,0x4A,
  182. 0xCB,0x8D,0x1C,0x05,0x7D,0xAD,0x4A,0x1B,
  183. 0xC6,0x7B,0x23,0x9D,0x4C,0x3F,0xD6,0x02,
  184. };
  185. static u8 fe_t2d[32] = {
  186. 0x4E,0x06,0xF4,0xFB,0x04,0x0B,0xCE,0x86,
  187. 0x6B,0x52,0xBB,0x96,0x0A,0xCE,0x11,0x3C,
  188. 0xCD,0xEF,0x4A,0x46,0x68,0x47,0xAA,0x72,
  189. 0x5F,0x65,0x90,0x91,0xA8,0x38,0xCA,0x37,
  190. };
  191. // initialize from packed representation
  192. static void ge_initeightpoint(void)
  193. {
  194. memset(&ge_eightpoint,0,sizeof(ge_eightpoint));
  195. curve25519_expand(ge_eightpoint.ysubx,fe_ysubx);
  196. curve25519_expand(ge_eightpoint.xaddy,fe_xaddy);
  197. curve25519_expand(ge_eightpoint.z,fe_z);
  198. curve25519_expand(ge_eightpoint.t2d,fe_t2d);
  199. }
  200. #endif