bn_sqr.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /* crypto/bn/bn_sqr.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #include "internal/cryptlib.h"
  59. #include "bn_lcl.h"
  60. /* r must not be a */
  61. /*
  62. * I've just gone over this and it is now %20 faster on x86 - eay - 27 Jun 96
  63. */
  64. int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
  65. {
  66. int max, al;
  67. int ret = 0;
  68. BIGNUM *tmp, *rr;
  69. bn_check_top(a);
  70. al = a->top;
  71. if (al <= 0) {
  72. r->top = 0;
  73. r->neg = 0;
  74. return 1;
  75. }
  76. BN_CTX_start(ctx);
  77. rr = (a != r) ? r : BN_CTX_get(ctx);
  78. tmp = BN_CTX_get(ctx);
  79. if (!rr || !tmp)
  80. goto err;
  81. max = 2 * al; /* Non-zero (from above) */
  82. if (bn_wexpand(rr, max) == NULL)
  83. goto err;
  84. if (al == 4) {
  85. #ifndef BN_SQR_COMBA
  86. BN_ULONG t[8];
  87. bn_sqr_normal(rr->d, a->d, 4, t);
  88. #else
  89. bn_sqr_comba4(rr->d, a->d);
  90. #endif
  91. } else if (al == 8) {
  92. #ifndef BN_SQR_COMBA
  93. BN_ULONG t[16];
  94. bn_sqr_normal(rr->d, a->d, 8, t);
  95. #else
  96. bn_sqr_comba8(rr->d, a->d);
  97. #endif
  98. } else {
  99. #if defined(BN_RECURSION)
  100. if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {
  101. BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];
  102. bn_sqr_normal(rr->d, a->d, al, t);
  103. } else {
  104. int j, k;
  105. j = BN_num_bits_word((BN_ULONG)al);
  106. j = 1 << (j - 1);
  107. k = j + j;
  108. if (al == j) {
  109. if (bn_wexpand(tmp, k * 2) == NULL)
  110. goto err;
  111. bn_sqr_recursive(rr->d, a->d, al, tmp->d);
  112. } else {
  113. if (bn_wexpand(tmp, max) == NULL)
  114. goto err;
  115. bn_sqr_normal(rr->d, a->d, al, tmp->d);
  116. }
  117. }
  118. #else
  119. if (bn_wexpand(tmp, max) == NULL)
  120. goto err;
  121. bn_sqr_normal(rr->d, a->d, al, tmp->d);
  122. #endif
  123. }
  124. rr->neg = 0;
  125. /*
  126. * If the most-significant half of the top word of 'a' is zero, then the
  127. * square of 'a' will max-1 words.
  128. */
  129. if (a->d[al - 1] == (a->d[al - 1] & BN_MASK2l))
  130. rr->top = max - 1;
  131. else
  132. rr->top = max;
  133. if (rr != r)
  134. BN_copy(r, rr);
  135. ret = 1;
  136. err:
  137. bn_check_top(rr);
  138. bn_check_top(tmp);
  139. BN_CTX_end(ctx);
  140. return (ret);
  141. }
  142. /* tmp must have 2*n words */
  143. void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)
  144. {
  145. int i, j, max;
  146. const BN_ULONG *ap;
  147. BN_ULONG *rp;
  148. max = n * 2;
  149. ap = a;
  150. rp = r;
  151. rp[0] = rp[max - 1] = 0;
  152. rp++;
  153. j = n;
  154. if (--j > 0) {
  155. ap++;
  156. rp[j] = bn_mul_words(rp, ap, j, ap[-1]);
  157. rp += 2;
  158. }
  159. for (i = n - 2; i > 0; i--) {
  160. j--;
  161. ap++;
  162. rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);
  163. rp += 2;
  164. }
  165. bn_add_words(r, r, r, max);
  166. /* There will not be a carry */
  167. bn_sqr_words(tmp, a, n);
  168. bn_add_words(r, r, tmp, max);
  169. }
  170. #ifdef BN_RECURSION
  171. /*-
  172. * r is 2*n words in size,
  173. * a and b are both n words in size. (There's not actually a 'b' here ...)
  174. * n must be a power of 2.
  175. * We multiply and return the result.
  176. * t must be 2*n words in size
  177. * We calculate
  178. * a[0]*b[0]
  179. * a[0]*b[0]+a[1]*b[1]+(a[0]-a[1])*(b[1]-b[0])
  180. * a[1]*b[1]
  181. */
  182. void bn_sqr_recursive(BN_ULONG *r, const BN_ULONG *a, int n2, BN_ULONG *t)
  183. {
  184. int n = n2 / 2;
  185. int zero, c1;
  186. BN_ULONG ln, lo, *p;
  187. if (n2 == 4) {
  188. # ifndef BN_SQR_COMBA
  189. bn_sqr_normal(r, a, 4, t);
  190. # else
  191. bn_sqr_comba4(r, a);
  192. # endif
  193. return;
  194. } else if (n2 == 8) {
  195. # ifndef BN_SQR_COMBA
  196. bn_sqr_normal(r, a, 8, t);
  197. # else
  198. bn_sqr_comba8(r, a);
  199. # endif
  200. return;
  201. }
  202. if (n2 < BN_SQR_RECURSIVE_SIZE_NORMAL) {
  203. bn_sqr_normal(r, a, n2, t);
  204. return;
  205. }
  206. /* r=(a[0]-a[1])*(a[1]-a[0]) */
  207. c1 = bn_cmp_words(a, &(a[n]), n);
  208. zero = 0;
  209. if (c1 > 0)
  210. bn_sub_words(t, a, &(a[n]), n);
  211. else if (c1 < 0)
  212. bn_sub_words(t, &(a[n]), a, n);
  213. else
  214. zero = 1;
  215. /* The result will always be negative unless it is zero */
  216. p = &(t[n2 * 2]);
  217. if (!zero)
  218. bn_sqr_recursive(&(t[n2]), t, n, p);
  219. else
  220. memset(&t[n2], 0, sizeof(*t) * n2);
  221. bn_sqr_recursive(r, a, n, p);
  222. bn_sqr_recursive(&(r[n2]), &(a[n]), n, p);
  223. /*-
  224. * t[32] holds (a[0]-a[1])*(a[1]-a[0]), it is negative or zero
  225. * r[10] holds (a[0]*b[0])
  226. * r[32] holds (b[1]*b[1])
  227. */
  228. c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
  229. /* t[32] is negative */
  230. c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
  231. /*-
  232. * t[32] holds (a[0]-a[1])*(a[1]-a[0])+(a[0]*a[0])+(a[1]*a[1])
  233. * r[10] holds (a[0]*a[0])
  234. * r[32] holds (a[1]*a[1])
  235. * c1 holds the carry bits
  236. */
  237. c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
  238. if (c1) {
  239. p = &(r[n + n2]);
  240. lo = *p;
  241. ln = (lo + c1) & BN_MASK2;
  242. *p = ln;
  243. /*
  244. * The overflow will stop before we over write words we should not
  245. * overwrite
  246. */
  247. if (ln < (BN_ULONG)c1) {
  248. do {
  249. p++;
  250. lo = *p;
  251. ln = (lo + 1) & BN_MASK2;
  252. *p = ln;
  253. } while (ln == 0);
  254. }
  255. }
  256. }
  257. #endif