2 * Copyright 1995-2016 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)
33 rr = (a != r) ? r : BN_CTX_get(ctx);
34 tmp = BN_CTX_get(ctx);
38 max = 2 * al; /* Non-zero (from above) */
39 if (bn_wexpand(rr, max) == NULL)
45 bn_sqr_normal(rr->d, a->d, 4, t);
47 bn_sqr_comba4(rr->d, a->d);
52 bn_sqr_normal(rr->d, a->d, 8, t);
54 bn_sqr_comba8(rr->d, a->d);
57 #if defined(BN_RECURSION)
58 if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {
59 BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];
60 bn_sqr_normal(rr->d, a->d, al, t);
64 j = BN_num_bits_word((BN_ULONG)al);
68 if (bn_wexpand(tmp, k * 2) == NULL)
70 bn_sqr_recursive(rr->d, a->d, al, tmp->d);
72 if (bn_wexpand(tmp, max) == NULL)
74 bn_sqr_normal(rr->d, a->d, al, tmp->d);
78 if (bn_wexpand(tmp, max) == NULL)
80 bn_sqr_normal(rr->d, a->d, al, tmp->d);
86 * If the most-significant half of the top word of 'a' is zero, then the
87 * square of 'a' will max-1 words.
89 if (a->d[al - 1] == (a->d[al - 1] & BN_MASK2l))
103 /* tmp must have 2*n words */
104 void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)
113 rp[0] = rp[max - 1] = 0;
119 rp[j] = bn_mul_words(rp, ap, j, ap[-1]);
123 for (i = n - 2; i > 0; i--) {
126 rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);
130 bn_add_words(r, r, r, max);
132 /* There will not be a carry */
134 bn_sqr_words(tmp, a, n);
136 bn_add_words(r, r, tmp, max);
141 * r is 2*n words in size,
142 * a and b are both n words in size. (There's not actually a 'b' here ...)
143 * n must be a power of 2.
144 * We multiply and return the result.
145 * t must be 2*n words in size
148 * a[0]*b[0]+a[1]*b[1]+(a[0]-a[1])*(b[1]-b[0])
151 void bn_sqr_recursive(BN_ULONG *r, const BN_ULONG *a, int n2, BN_ULONG *t)
158 # ifndef BN_SQR_COMBA
159 bn_sqr_normal(r, a, 4, t);
164 } else if (n2 == 8) {
165 # ifndef BN_SQR_COMBA
166 bn_sqr_normal(r, a, 8, t);
172 if (n2 < BN_SQR_RECURSIVE_SIZE_NORMAL) {
173 bn_sqr_normal(r, a, n2, t);
176 /* r=(a[0]-a[1])*(a[1]-a[0]) */
177 c1 = bn_cmp_words(a, &(a[n]), n);
180 bn_sub_words(t, a, &(a[n]), n);
182 bn_sub_words(t, &(a[n]), a, n);
186 /* The result will always be negative unless it is zero */
190 bn_sqr_recursive(&(t[n2]), t, n, p);
192 memset(&t[n2], 0, sizeof(*t) * n2);
193 bn_sqr_recursive(r, a, n, p);
194 bn_sqr_recursive(&(r[n2]), &(a[n]), n, p);
197 * t[32] holds (a[0]-a[1])*(a[1]-a[0]), it is negative or zero
198 * r[10] holds (a[0]*b[0])
199 * r[32] holds (b[1]*b[1])
202 c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
204 /* t[32] is negative */
205 c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
208 * t[32] holds (a[0]-a[1])*(a[1]-a[0])+(a[0]*a[0])+(a[1]*a[1])
209 * r[10] holds (a[0]*a[0])
210 * r[32] holds (a[1]*a[1])
211 * c1 holds the carry bits
213 c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
217 ln = (lo + c1) & BN_MASK2;
221 * The overflow will stop before we over write words we should not
224 if (ln < (BN_ULONG)c1) {
228 ln = (lo + 1) & BN_MASK2;