ecdsa_impl.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /**********************************************************************
  2. * Copyright (c) 2013, 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_ECDSA_IMPL_H_
  7. #define _SECP256K1_ECDSA_IMPL_H_
  8. #include "scalar.h"
  9. #include "field.h"
  10. #include "group.h"
  11. #include "ecmult.h"
  12. #include "ecmult_gen.h"
  13. #include "ecdsa.h"
  14. /** Group order for secp256k1 defined as 'n' in "Standards for Efficient Cryptography" (SEC2) 2.7.1
  15. * sage: for t in xrange(1023, -1, -1):
  16. * .. p = 2**256 - 2**32 - t
  17. * .. if p.is_prime():
  18. * .. print '%x'%p
  19. * .. break
  20. * 'fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f'
  21. * sage: a = 0
  22. * sage: b = 7
  23. * sage: F = FiniteField (p)
  24. * sage: '%x' % (EllipticCurve ([F (a), F (b)]).order())
  25. * 'fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141'
  26. */
  27. static const secp256k1_fe_t secp256k1_ecdsa_const_order_as_fe = SECP256K1_FE_CONST(
  28. 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFEUL,
  29. 0xBAAEDCE6UL, 0xAF48A03BUL, 0xBFD25E8CUL, 0xD0364141UL
  30. );
  31. /** Difference between field and order, values 'p' and 'n' values defined in
  32. * "Standards for Efficient Cryptography" (SEC2) 2.7.1.
  33. * sage: p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
  34. * sage: a = 0
  35. * sage: b = 7
  36. * sage: F = FiniteField (p)
  37. * sage: '%x' % (p - EllipticCurve ([F (a), F (b)]).order())
  38. * '14551231950b75fc4402da1722fc9baee'
  39. */
  40. static const secp256k1_fe_t secp256k1_ecdsa_const_p_minus_order = SECP256K1_FE_CONST(
  41. 0, 0, 0, 1, 0x45512319UL, 0x50B75FC4UL, 0x402DA172UL, 0x2FC9BAEEUL
  42. );
  43. static int secp256k1_ecdsa_sig_parse(secp256k1_ecdsa_sig_t *r, const unsigned char *sig, int size) {
  44. unsigned char ra[32] = {0}, sa[32] = {0};
  45. const unsigned char *rp;
  46. const unsigned char *sp;
  47. int lenr;
  48. int lens;
  49. int overflow;
  50. if (sig[0] != 0x30) {
  51. return 0;
  52. }
  53. lenr = sig[3];
  54. if (5+lenr >= size) {
  55. return 0;
  56. }
  57. lens = sig[lenr+5];
  58. if (sig[1] != lenr+lens+4) {
  59. return 0;
  60. }
  61. if (lenr+lens+6 > size) {
  62. return 0;
  63. }
  64. if (sig[2] != 0x02) {
  65. return 0;
  66. }
  67. if (lenr == 0) {
  68. return 0;
  69. }
  70. if (sig[lenr+4] != 0x02) {
  71. return 0;
  72. }
  73. if (lens == 0) {
  74. return 0;
  75. }
  76. sp = sig + 6 + lenr;
  77. while (lens > 0 && sp[0] == 0) {
  78. lens--;
  79. sp++;
  80. }
  81. if (lens > 32) {
  82. return 0;
  83. }
  84. rp = sig + 4;
  85. while (lenr > 0 && rp[0] == 0) {
  86. lenr--;
  87. rp++;
  88. }
  89. if (lenr > 32) {
  90. return 0;
  91. }
  92. memcpy(ra + 32 - lenr, rp, lenr);
  93. memcpy(sa + 32 - lens, sp, lens);
  94. overflow = 0;
  95. secp256k1_scalar_set_b32(&r->r, ra, &overflow);
  96. if (overflow) {
  97. return 0;
  98. }
  99. secp256k1_scalar_set_b32(&r->s, sa, &overflow);
  100. if (overflow) {
  101. return 0;
  102. }
  103. return 1;
  104. }
  105. static int secp256k1_ecdsa_sig_serialize(unsigned char *sig, int *size, const secp256k1_ecdsa_sig_t *a) {
  106. unsigned char r[33] = {0}, s[33] = {0};
  107. unsigned char *rp = r, *sp = s;
  108. int lenR = 33, lenS = 33;
  109. secp256k1_scalar_get_b32(&r[1], &a->r);
  110. secp256k1_scalar_get_b32(&s[1], &a->s);
  111. while (lenR > 1 && rp[0] == 0 && rp[1] < 0x80) { lenR--; rp++; }
  112. while (lenS > 1 && sp[0] == 0 && sp[1] < 0x80) { lenS--; sp++; }
  113. if (*size < 6+lenS+lenR) {
  114. return 0;
  115. }
  116. *size = 6 + lenS + lenR;
  117. sig[0] = 0x30;
  118. sig[1] = 4 + lenS + lenR;
  119. sig[2] = 0x02;
  120. sig[3] = lenR;
  121. memcpy(sig+4, rp, lenR);
  122. sig[4+lenR] = 0x02;
  123. sig[5+lenR] = lenS;
  124. memcpy(sig+lenR+6, sp, lenS);
  125. return 1;
  126. }
  127. static int secp256k1_ecdsa_sig_verify(const secp256k1_ecmult_context_t *ctx, const secp256k1_ecdsa_sig_t *sig, const secp256k1_ge_t *pubkey, const secp256k1_scalar_t *message) {
  128. unsigned char c[32];
  129. secp256k1_scalar_t sn, u1, u2;
  130. secp256k1_fe_t xr;
  131. secp256k1_gej_t pubkeyj;
  132. secp256k1_gej_t pr;
  133. if (secp256k1_scalar_is_zero(&sig->r) || secp256k1_scalar_is_zero(&sig->s)) {
  134. return 0;
  135. }
  136. secp256k1_scalar_inverse_var(&sn, &sig->s);
  137. secp256k1_scalar_mul(&u1, &sn, message);
  138. secp256k1_scalar_mul(&u2, &sn, &sig->r);
  139. secp256k1_gej_set_ge(&pubkeyj, pubkey);
  140. secp256k1_ecmult(ctx, &pr, &pubkeyj, &u2, &u1);
  141. if (secp256k1_gej_is_infinity(&pr)) {
  142. return 0;
  143. }
  144. secp256k1_scalar_get_b32(c, &sig->r);
  145. secp256k1_fe_set_b32(&xr, c);
  146. /** We now have the recomputed R point in pr, and its claimed x coordinate (modulo n)
  147. * in xr. Naively, we would extract the x coordinate from pr (requiring a inversion modulo p),
  148. * compute the remainder modulo n, and compare it to xr. However:
  149. *
  150. * xr == X(pr) mod n
  151. * <=> exists h. (xr + h * n < p && xr + h * n == X(pr))
  152. * [Since 2 * n > p, h can only be 0 or 1]
  153. * <=> (xr == X(pr)) || (xr + n < p && xr + n == X(pr))
  154. * [In Jacobian coordinates, X(pr) is pr.x / pr.z^2 mod p]
  155. * <=> (xr == pr.x / pr.z^2 mod p) || (xr + n < p && xr + n == pr.x / pr.z^2 mod p)
  156. * [Multiplying both sides of the equations by pr.z^2 mod p]
  157. * <=> (xr * pr.z^2 mod p == pr.x) || (xr + n < p && (xr + n) * pr.z^2 mod p == pr.x)
  158. *
  159. * Thus, we can avoid the inversion, but we have to check both cases separately.
  160. * secp256k1_gej_eq_x implements the (xr * pr.z^2 mod p == pr.x) test.
  161. */
  162. if (secp256k1_gej_eq_x_var(&xr, &pr)) {
  163. /* xr.x == xr * xr.z^2 mod p, so the signature is valid. */
  164. return 1;
  165. }
  166. if (secp256k1_fe_cmp_var(&xr, &secp256k1_ecdsa_const_p_minus_order) >= 0) {
  167. /* xr + p >= n, so we can skip testing the second case. */
  168. return 0;
  169. }
  170. secp256k1_fe_add(&xr, &secp256k1_ecdsa_const_order_as_fe);
  171. if (secp256k1_gej_eq_x_var(&xr, &pr)) {
  172. /* (xr + n) * pr.z^2 mod p == pr.x, so the signature is valid. */
  173. return 1;
  174. }
  175. return 0;
  176. }
  177. static int secp256k1_ecdsa_sig_recover(const secp256k1_ecmult_context_t *ctx, const secp256k1_ecdsa_sig_t *sig, secp256k1_ge_t *pubkey, const secp256k1_scalar_t *message, int recid) {
  178. unsigned char brx[32];
  179. secp256k1_fe_t fx;
  180. secp256k1_ge_t x;
  181. secp256k1_gej_t xj;
  182. secp256k1_scalar_t rn, u1, u2;
  183. secp256k1_gej_t qj;
  184. if (secp256k1_scalar_is_zero(&sig->r) || secp256k1_scalar_is_zero(&sig->s)) {
  185. return 0;
  186. }
  187. secp256k1_scalar_get_b32(brx, &sig->r);
  188. VERIFY_CHECK(secp256k1_fe_set_b32(&fx, brx)); /* brx comes from a scalar, so is less than the order; certainly less than p */
  189. if (recid & 2) {
  190. if (secp256k1_fe_cmp_var(&fx, &secp256k1_ecdsa_const_p_minus_order) >= 0) {
  191. return 0;
  192. }
  193. secp256k1_fe_add(&fx, &secp256k1_ecdsa_const_order_as_fe);
  194. }
  195. if (!secp256k1_ge_set_xo_var(&x, &fx, recid & 1)) {
  196. return 0;
  197. }
  198. secp256k1_gej_set_ge(&xj, &x);
  199. secp256k1_scalar_inverse_var(&rn, &sig->r);
  200. secp256k1_scalar_mul(&u1, &rn, message);
  201. secp256k1_scalar_negate(&u1, &u1);
  202. secp256k1_scalar_mul(&u2, &rn, &sig->s);
  203. secp256k1_ecmult(ctx, &qj, &xj, &u2, &u1);
  204. secp256k1_ge_set_gej_var(pubkey, &qj);
  205. return !secp256k1_gej_is_infinity(&qj);
  206. }
  207. static int secp256k1_ecdsa_sig_sign(const secp256k1_ecmult_gen_context_t *ctx, secp256k1_ecdsa_sig_t *sig, const secp256k1_scalar_t *seckey, const secp256k1_scalar_t *message, const secp256k1_scalar_t *nonce, int *recid) {
  208. unsigned char b[32];
  209. secp256k1_gej_t rp;
  210. secp256k1_ge_t r;
  211. secp256k1_scalar_t n;
  212. int overflow = 0;
  213. secp256k1_ecmult_gen(ctx, &rp, nonce);
  214. secp256k1_ge_set_gej(&r, &rp);
  215. secp256k1_fe_normalize(&r.x);
  216. secp256k1_fe_normalize(&r.y);
  217. secp256k1_fe_get_b32(b, &r.x);
  218. secp256k1_scalar_set_b32(&sig->r, b, &overflow);
  219. if (secp256k1_scalar_is_zero(&sig->r)) {
  220. /* P.x = order is on the curve, so technically sig->r could end up zero, which would be an invalid signature. */
  221. secp256k1_gej_clear(&rp);
  222. secp256k1_ge_clear(&r);
  223. return 0;
  224. }
  225. if (recid) {
  226. *recid = (overflow ? 2 : 0) | (secp256k1_fe_is_odd(&r.y) ? 1 : 0);
  227. }
  228. secp256k1_scalar_mul(&n, &sig->r, seckey);
  229. secp256k1_scalar_add(&n, &n, message);
  230. secp256k1_scalar_inverse(&sig->s, nonce);
  231. secp256k1_scalar_mul(&sig->s, &sig->s, &n);
  232. secp256k1_scalar_clear(&n);
  233. secp256k1_gej_clear(&rp);
  234. secp256k1_ge_clear(&r);
  235. if (secp256k1_scalar_is_zero(&sig->s)) {
  236. return 0;
  237. }
  238. if (secp256k1_scalar_is_high(&sig->s)) {
  239. secp256k1_scalar_negate(&sig->s, &sig->s);
  240. if (recid) {
  241. *recid ^= 1;
  242. }
  243. }
  244. return 1;
  245. }
  246. #endif