2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include "internal/cryptlib.h"
15 * I've just gone over this and it is now %20 faster on x86 - eay - 27 Jun 96
17 int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
19 int ret = bn_sqr_fixed_top(r, a, ctx);
27 int bn_sqr_fixed_top(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
43 rr = (a != r) ? r : BN_CTX_get(ctx);
44 tmp = BN_CTX_get(ctx);
45 if (rr == NULL || tmp == NULL)
48 max = 2 * al; /* Non-zero (from above) */
49 if (bn_wexpand(rr, max) == NULL)
55 bn_sqr_normal(rr->d, a->d, 4, t);
57 bn_sqr_comba4(rr->d, a->d);
62 bn_sqr_normal(rr->d, a->d, 8, t);
64 bn_sqr_comba8(rr->d, a->d);
67 #if defined(BN_RECURSION)
68 if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {
69 BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];
70 bn_sqr_normal(rr->d, a->d, al, t);
74 j = BN_num_bits_word((BN_ULONG)al);
78 if (bn_wexpand(tmp, k * 2) == NULL)
80 bn_sqr_recursive(rr->d, a->d, al, tmp->d);
82 if (bn_wexpand(tmp, max) == NULL)
84 bn_sqr_normal(rr->d, a->d, al, tmp->d);
88 if (bn_wexpand(tmp, max) == NULL)
90 bn_sqr_normal(rr->d, a->d, al, tmp->d);
96 rr->flags |= BN_FLG_FIXED_TOP;
97 if (r != rr && BN_copy(r, rr) == NULL)
108 /* tmp must have 2*n words */
109 void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)
118 rp[0] = rp[max - 1] = 0;
124 rp[j] = bn_mul_words(rp, ap, j, ap[-1]);
128 for (i = n - 2; i > 0; i--) {
131 rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);
135 bn_add_words(r, r, r, max);
137 /* There will not be a carry */
139 bn_sqr_words(tmp, a, n);
141 bn_add_words(r, r, tmp, max);
146 * r is 2*n words in size,
147 * a and b are both n words in size. (There's not actually a 'b' here ...)
148 * n must be a power of 2.
149 * We multiply and return the result.
150 * t must be 2*n words in size
153 * a[0]*b[0]+a[1]*b[1]+(a[0]-a[1])*(b[1]-b[0])
156 void bn_sqr_recursive(BN_ULONG *r, const BN_ULONG *a, int n2, BN_ULONG *t)
163 # ifndef BN_SQR_COMBA
164 bn_sqr_normal(r, a, 4, t);
169 } else if (n2 == 8) {
170 # ifndef BN_SQR_COMBA
171 bn_sqr_normal(r, a, 8, t);
177 if (n2 < BN_SQR_RECURSIVE_SIZE_NORMAL) {
178 bn_sqr_normal(r, a, n2, t);
181 /* r=(a[0]-a[1])*(a[1]-a[0]) */
182 c1 = bn_cmp_words(a, &(a[n]), n);
185 bn_sub_words(t, a, &(a[n]), n);
187 bn_sub_words(t, &(a[n]), a, n);
191 /* The result will always be negative unless it is zero */
195 bn_sqr_recursive(&(t[n2]), t, n, p);
197 memset(&t[n2], 0, sizeof(*t) * n2);
198 bn_sqr_recursive(r, a, n, p);
199 bn_sqr_recursive(&(r[n2]), &(a[n]), n, p);
202 * t[32] holds (a[0]-a[1])*(a[1]-a[0]), it is negative or zero
203 * r[10] holds (a[0]*b[0])
204 * r[32] holds (b[1]*b[1])
207 c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
209 /* t[32] is negative */
210 c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
213 * t[32] holds (a[0]-a[1])*(a[1]-a[0])+(a[0]*a[0])+(a[1]*a[1])
214 * r[10] holds (a[0]*a[0])
215 * r[32] holds (a[1]*a[1])
216 * c1 holds the carry bits
218 c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
222 ln = (lo + c1) & BN_MASK2;
226 * The overflow will stop before we over write words we should not
229 if (ln < (BN_ULONG)c1) {
233 ln = (lo + 1) & BN_MASK2;