scalar_impl.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /**********************************************************************
  2. * Copyright (c) 2014 Pieter Wuille *
  3. * Distributed under the MIT software license, see the accompanying *
  4. * file COPYING or http://www.opensource.org/licenses/mit-license.php.*
  5. **********************************************************************/
  6. #ifndef _SECP256K1_SCALAR_IMPL_H_
  7. #define _SECP256K1_SCALAR_IMPL_H_
  8. #include <string.h>
  9. #include "group.h"
  10. #include "scalar.h"
  11. #if defined HAVE_CONFIG_H
  12. #include "libsecp256k1-config.h"
  13. #endif
  14. #if defined(USE_SCALAR_4X64)
  15. #include "scalar_4x64_impl.h"
  16. #elif defined(USE_SCALAR_8X32)
  17. #include "scalar_8x32_impl.h"
  18. #else
  19. #error "Please select scalar implementation"
  20. #endif
  21. #ifndef USE_NUM_NONE
  22. static void secp256k1_scalar_get_num(secp256k1_num_t *r, const secp256k1_scalar_t *a) {
  23. unsigned char c[32];
  24. secp256k1_scalar_get_b32(c, a);
  25. secp256k1_num_set_bin(r, c, 32);
  26. }
  27. /** secp256k1 curve order, see secp256k1_ecdsa_const_order_as_fe in ecdsa_impl.h */
  28. static void secp256k1_scalar_order_get_num(secp256k1_num_t *r) {
  29. static const unsigned char order[32] = {
  30. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  31. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,
  32. 0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,
  33. 0xBF,0xD2,0x5E,0x8C,0xD0,0x36,0x41,0x41
  34. };
  35. secp256k1_num_set_bin(r, order, 32);
  36. }
  37. #endif
  38. static void secp256k1_scalar_inverse(secp256k1_scalar_t *r, const secp256k1_scalar_t *x) {
  39. secp256k1_scalar_t *t;
  40. int i;
  41. /* First compute x ^ (2^N - 1) for some values of N. */
  42. secp256k1_scalar_t x2, x3, x4, x6, x7, x8, x15, x30, x60, x120, x127;
  43. secp256k1_scalar_sqr(&x2, x);
  44. secp256k1_scalar_mul(&x2, &x2, x);
  45. secp256k1_scalar_sqr(&x3, &x2);
  46. secp256k1_scalar_mul(&x3, &x3, x);
  47. secp256k1_scalar_sqr(&x4, &x3);
  48. secp256k1_scalar_mul(&x4, &x4, x);
  49. secp256k1_scalar_sqr(&x6, &x4);
  50. secp256k1_scalar_sqr(&x6, &x6);
  51. secp256k1_scalar_mul(&x6, &x6, &x2);
  52. secp256k1_scalar_sqr(&x7, &x6);
  53. secp256k1_scalar_mul(&x7, &x7, x);
  54. secp256k1_scalar_sqr(&x8, &x7);
  55. secp256k1_scalar_mul(&x8, &x8, x);
  56. secp256k1_scalar_sqr(&x15, &x8);
  57. for (i = 0; i < 6; i++) {
  58. secp256k1_scalar_sqr(&x15, &x15);
  59. }
  60. secp256k1_scalar_mul(&x15, &x15, &x7);
  61. secp256k1_scalar_sqr(&x30, &x15);
  62. for (i = 0; i < 14; i++) {
  63. secp256k1_scalar_sqr(&x30, &x30);
  64. }
  65. secp256k1_scalar_mul(&x30, &x30, &x15);
  66. secp256k1_scalar_sqr(&x60, &x30);
  67. for (i = 0; i < 29; i++) {
  68. secp256k1_scalar_sqr(&x60, &x60);
  69. }
  70. secp256k1_scalar_mul(&x60, &x60, &x30);
  71. secp256k1_scalar_sqr(&x120, &x60);
  72. for (i = 0; i < 59; i++) {
  73. secp256k1_scalar_sqr(&x120, &x120);
  74. }
  75. secp256k1_scalar_mul(&x120, &x120, &x60);
  76. secp256k1_scalar_sqr(&x127, &x120);
  77. for (i = 0; i < 6; i++) {
  78. secp256k1_scalar_sqr(&x127, &x127);
  79. }
  80. secp256k1_scalar_mul(&x127, &x127, &x7);
  81. /* Then accumulate the final result (t starts at x127). */
  82. t = &x127;
  83. for (i = 0; i < 2; i++) { /* 0 */
  84. secp256k1_scalar_sqr(t, t);
  85. }
  86. secp256k1_scalar_mul(t, t, x); /* 1 */
  87. for (i = 0; i < 4; i++) { /* 0 */
  88. secp256k1_scalar_sqr(t, t);
  89. }
  90. secp256k1_scalar_mul(t, t, &x3); /* 111 */
  91. for (i = 0; i < 2; i++) { /* 0 */
  92. secp256k1_scalar_sqr(t, t);
  93. }
  94. secp256k1_scalar_mul(t, t, x); /* 1 */
  95. for (i = 0; i < 2; i++) { /* 0 */
  96. secp256k1_scalar_sqr(t, t);
  97. }
  98. secp256k1_scalar_mul(t, t, x); /* 1 */
  99. for (i = 0; i < 2; i++) { /* 0 */
  100. secp256k1_scalar_sqr(t, t);
  101. }
  102. secp256k1_scalar_mul(t, t, x); /* 1 */
  103. for (i = 0; i < 4; i++) { /* 0 */
  104. secp256k1_scalar_sqr(t, t);
  105. }
  106. secp256k1_scalar_mul(t, t, &x3); /* 111 */
  107. for (i = 0; i < 3; i++) { /* 0 */
  108. secp256k1_scalar_sqr(t, t);
  109. }
  110. secp256k1_scalar_mul(t, t, &x2); /* 11 */
  111. for (i = 0; i < 4; i++) { /* 0 */
  112. secp256k1_scalar_sqr(t, t);
  113. }
  114. secp256k1_scalar_mul(t, t, &x3); /* 111 */
  115. for (i = 0; i < 5; i++) { /* 00 */
  116. secp256k1_scalar_sqr(t, t);
  117. }
  118. secp256k1_scalar_mul(t, t, &x3); /* 111 */
  119. for (i = 0; i < 4; i++) { /* 00 */
  120. secp256k1_scalar_sqr(t, t);
  121. }
  122. secp256k1_scalar_mul(t, t, &x2); /* 11 */
  123. for (i = 0; i < 2; i++) { /* 0 */
  124. secp256k1_scalar_sqr(t, t);
  125. }
  126. secp256k1_scalar_mul(t, t, x); /* 1 */
  127. for (i = 0; i < 2; i++) { /* 0 */
  128. secp256k1_scalar_sqr(t, t);
  129. }
  130. secp256k1_scalar_mul(t, t, x); /* 1 */
  131. for (i = 0; i < 5; i++) { /* 0 */
  132. secp256k1_scalar_sqr(t, t);
  133. }
  134. secp256k1_scalar_mul(t, t, &x4); /* 1111 */
  135. for (i = 0; i < 2; i++) { /* 0 */
  136. secp256k1_scalar_sqr(t, t);
  137. }
  138. secp256k1_scalar_mul(t, t, x); /* 1 */
  139. for (i = 0; i < 3; i++) { /* 00 */
  140. secp256k1_scalar_sqr(t, t);
  141. }
  142. secp256k1_scalar_mul(t, t, x); /* 1 */
  143. for (i = 0; i < 4; i++) { /* 000 */
  144. secp256k1_scalar_sqr(t, t);
  145. }
  146. secp256k1_scalar_mul(t, t, x); /* 1 */
  147. for (i = 0; i < 2; i++) { /* 0 */
  148. secp256k1_scalar_sqr(t, t);
  149. }
  150. secp256k1_scalar_mul(t, t, x); /* 1 */
  151. for (i = 0; i < 10; i++) { /* 0000000 */
  152. secp256k1_scalar_sqr(t, t);
  153. }
  154. secp256k1_scalar_mul(t, t, &x3); /* 111 */
  155. for (i = 0; i < 4; i++) { /* 0 */
  156. secp256k1_scalar_sqr(t, t);
  157. }
  158. secp256k1_scalar_mul(t, t, &x3); /* 111 */
  159. for (i = 0; i < 9; i++) { /* 0 */
  160. secp256k1_scalar_sqr(t, t);
  161. }
  162. secp256k1_scalar_mul(t, t, &x8); /* 11111111 */
  163. for (i = 0; i < 2; i++) { /* 0 */
  164. secp256k1_scalar_sqr(t, t);
  165. }
  166. secp256k1_scalar_mul(t, t, x); /* 1 */
  167. for (i = 0; i < 3; i++) { /* 00 */
  168. secp256k1_scalar_sqr(t, t);
  169. }
  170. secp256k1_scalar_mul(t, t, x); /* 1 */
  171. for (i = 0; i < 3; i++) { /* 00 */
  172. secp256k1_scalar_sqr(t, t);
  173. }
  174. secp256k1_scalar_mul(t, t, x); /* 1 */
  175. for (i = 0; i < 5; i++) { /* 0 */
  176. secp256k1_scalar_sqr(t, t);
  177. }
  178. secp256k1_scalar_mul(t, t, &x4); /* 1111 */
  179. for (i = 0; i < 2; i++) { /* 0 */
  180. secp256k1_scalar_sqr(t, t);
  181. }
  182. secp256k1_scalar_mul(t, t, x); /* 1 */
  183. for (i = 0; i < 5; i++) { /* 000 */
  184. secp256k1_scalar_sqr(t, t);
  185. }
  186. secp256k1_scalar_mul(t, t, &x2); /* 11 */
  187. for (i = 0; i < 4; i++) { /* 00 */
  188. secp256k1_scalar_sqr(t, t);
  189. }
  190. secp256k1_scalar_mul(t, t, &x2); /* 11 */
  191. for (i = 0; i < 2; i++) { /* 0 */
  192. secp256k1_scalar_sqr(t, t);
  193. }
  194. secp256k1_scalar_mul(t, t, x); /* 1 */
  195. for (i = 0; i < 8; i++) { /* 000000 */
  196. secp256k1_scalar_sqr(t, t);
  197. }
  198. secp256k1_scalar_mul(t, t, &x2); /* 11 */
  199. for (i = 0; i < 3; i++) { /* 0 */
  200. secp256k1_scalar_sqr(t, t);
  201. }
  202. secp256k1_scalar_mul(t, t, &x2); /* 11 */
  203. for (i = 0; i < 3; i++) { /* 00 */
  204. secp256k1_scalar_sqr(t, t);
  205. }
  206. secp256k1_scalar_mul(t, t, x); /* 1 */
  207. for (i = 0; i < 6; i++) { /* 00000 */
  208. secp256k1_scalar_sqr(t, t);
  209. }
  210. secp256k1_scalar_mul(t, t, x); /* 1 */
  211. for (i = 0; i < 8; i++) { /* 00 */
  212. secp256k1_scalar_sqr(t, t);
  213. }
  214. secp256k1_scalar_mul(r, t, &x6); /* 111111 */
  215. }
  216. static void secp256k1_scalar_inverse_var(secp256k1_scalar_t *r, const secp256k1_scalar_t *x) {
  217. #if defined(USE_SCALAR_INV_BUILTIN)
  218. secp256k1_scalar_inverse(r, x);
  219. #elif defined(USE_SCALAR_INV_NUM)
  220. unsigned char b[32];
  221. secp256k1_num_t n, m;
  222. secp256k1_scalar_get_b32(b, x);
  223. secp256k1_num_set_bin(&n, b, 32);
  224. secp256k1_scalar_order_get_num(&m);
  225. secp256k1_num_mod_inverse(&n, &n, &m);
  226. secp256k1_num_get_bin(b, 32, &n);
  227. secp256k1_scalar_set_b32(r, b, NULL);
  228. #else
  229. #error "Please select scalar inverse implementation"
  230. #endif
  231. }
  232. #ifdef USE_ENDOMORPHISM
  233. /**
  234. * The Secp256k1 curve has an endomorphism, where lambda * (x, y) = (beta * x, y), where
  235. * lambda is {0x53,0x63,0xad,0x4c,0xc0,0x5c,0x30,0xe0,0xa5,0x26,0x1c,0x02,0x88,0x12,0x64,0x5a,
  236. * 0x12,0x2e,0x22,0xea,0x20,0x81,0x66,0x78,0xdf,0x02,0x96,0x7c,0x1b,0x23,0xbd,0x72}
  237. *
  238. * "Guide to Elliptic Curve Cryptography" (Hankerson, Menezes, Vanstone) gives an algorithm
  239. * (algorithm 3.74) to find k1 and k2 given k, such that k1 + k2 * lambda == k mod n, and k1
  240. * and k2 have a small size.
  241. * It relies on constants a1, b1, a2, b2. These constants for the value of lambda above are:
  242. *
  243. * - a1 = {0x30,0x86,0xd2,0x21,0xa7,0xd4,0x6b,0xcd,0xe8,0x6c,0x90,0xe4,0x92,0x84,0xeb,0x15}
  244. * - b1 = -{0xe4,0x43,0x7e,0xd6,0x01,0x0e,0x88,0x28,0x6f,0x54,0x7f,0xa9,0x0a,0xbf,0xe4,0xc3}
  245. * - a2 = {0x01,0x14,0xca,0x50,0xf7,0xa8,0xe2,0xf3,0xf6,0x57,0xc1,0x10,0x8d,0x9d,0x44,0xcf,0xd8}
  246. * - b2 = {0x30,0x86,0xd2,0x21,0xa7,0xd4,0x6b,0xcd,0xe8,0x6c,0x90,0xe4,0x92,0x84,0xeb,0x15}
  247. *
  248. * The algorithm then computes c1 = round(b1 * k / n) and c2 = round(b2 * k / n), and gives
  249. * k1 = k - (c1*a1 + c2*a2) and k2 = -(c1*b1 + c2*b2). Instead, we use modular arithmetic, and
  250. * compute k1 as k - k2 * lambda, avoiding the need for constants a1 and a2.
  251. *
  252. * g1, g2 are precomputed constants used to replace division with a rounded multiplication
  253. * when decomposing the scalar for an endomorphism-based point multiplication.
  254. *
  255. * The possibility of using precomputed estimates is mentioned in "Guide to Elliptic Curve
  256. * Cryptography" (Hankerson, Menezes, Vanstone) in section 3.5.
  257. *
  258. * The derivation is described in the paper "Efficient Software Implementation of Public-Key
  259. * Cryptography on Sensor Networks Using the MSP430X Microcontroller" (Gouvea, Oliveira, Lopez),
  260. * Section 4.3 (here we use a somewhat higher-precision estimate):
  261. * d = a1*b2 - b1*a2
  262. * g1 = round((2^272)*b2/d)
  263. * g2 = round((2^272)*b1/d)
  264. *
  265. * (Note that 'd' is also equal to the curve order here because [a1,b1] and [a2,b2] are found
  266. * as outputs of the Extended Euclidean Algorithm on inputs 'order' and 'lambda').
  267. *
  268. * The function below splits a in r1 and r2, such that r1 + lambda * r2 == a (mod order).
  269. */
  270. static void secp256k1_scalar_split_lambda_var(secp256k1_scalar_t *r1, secp256k1_scalar_t *r2, const secp256k1_scalar_t *a) {
  271. secp256k1_scalar_t c1, c2;
  272. static const secp256k1_scalar_t minus_lambda = SECP256K1_SCALAR_CONST(
  273. 0xAC9C52B3UL, 0x3FA3CF1FUL, 0x5AD9E3FDUL, 0x77ED9BA4UL,
  274. 0xA880B9FCUL, 0x8EC739C2UL, 0xE0CFC810UL, 0xB51283CFUL
  275. );
  276. static const secp256k1_scalar_t minus_b1 = SECP256K1_SCALAR_CONST(
  277. 0x00000000UL, 0x00000000UL, 0x00000000UL, 0x00000000UL,
  278. 0xE4437ED6UL, 0x010E8828UL, 0x6F547FA9UL, 0x0ABFE4C3UL
  279. );
  280. static const secp256k1_scalar_t minus_b2 = SECP256K1_SCALAR_CONST(
  281. 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFEUL,
  282. 0x8A280AC5UL, 0x0774346DUL, 0xD765CDA8UL, 0x3DB1562CUL
  283. );
  284. static const secp256k1_scalar_t g1 = SECP256K1_SCALAR_CONST(
  285. 0x00000000UL, 0x00000000UL, 0x00000000UL, 0x00003086UL,
  286. 0xD221A7D4UL, 0x6BCDE86CUL, 0x90E49284UL, 0xEB153DABUL
  287. );
  288. static const secp256k1_scalar_t g2 = SECP256K1_SCALAR_CONST(
  289. 0x00000000UL, 0x00000000UL, 0x00000000UL, 0x0000E443UL,
  290. 0x7ED6010EUL, 0x88286F54UL, 0x7FA90ABFUL, 0xE4C42212UL
  291. );
  292. VERIFY_CHECK(r1 != a);
  293. VERIFY_CHECK(r2 != a);
  294. secp256k1_scalar_mul_shift_var(&c1, a, &g1, 272);
  295. secp256k1_scalar_mul_shift_var(&c2, a, &g2, 272);
  296. secp256k1_scalar_mul(&c1, &c1, &minus_b1);
  297. secp256k1_scalar_mul(&c2, &c2, &minus_b2);
  298. secp256k1_scalar_add(r2, &c1, &c2);
  299. secp256k1_scalar_mul(r1, r2, &minus_lambda);
  300. secp256k1_scalar_add(r1, r1, a);
  301. }
  302. #endif
  303. #endif