summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorjsing <>2023-01-16 17:56:25 +0000
committerjsing <>2023-01-16 17:56:25 +0000
commit09a139d6e2957a9f591459638b42ee548e5201dd (patch)
tree9f367501b0244cdf0d41782b09870c4cf24c5f8f /src/lib
parentc697a4ee90cf2e8daab7bbb471d2afd609aff929 (diff)
downloadopenbsd-09a139d6e2957a9f591459638b42ee548e5201dd.tar.gz
openbsd-09a139d6e2957a9f591459638b42ee548e5201dd.tar.bz2
openbsd-09a139d6e2957a9f591459638b42ee548e5201dd.zip
Move BN_sqr() to the bottom of the file.
This will simplify review/upcoming changes. No functional change.
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/libcrypto/bn/bn_sqr.c170
1 files changed, 85 insertions, 85 deletions
diff --git a/src/lib/libcrypto/bn/bn_sqr.c b/src/lib/libcrypto/bn/bn_sqr.c
index e150191fa1..56ea378527 100644
--- a/src/lib/libcrypto/bn/bn_sqr.c
+++ b/src/lib/libcrypto/bn/bn_sqr.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_sqr.c,v 1.17 2023/01/16 16:53:19 jsing Exp $ */ 1/* $OpenBSD: bn_sqr.c,v 1.18 2023/01/16 17:56:25 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -61,90 +61,6 @@
61 61
62#include "bn_local.h" 62#include "bn_local.h"
63 63
64/* I've just gone over this and it is now %20 faster on x86 - eay - 27 Jun 96 */
65int
66BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
67{
68 int max, al;
69 int ret = 0;
70 BIGNUM *tmp, *rr;
71
72
73 al = a->top;
74 if (al <= 0) {
75 r->top = 0;
76 r->neg = 0;
77 return 1;
78 }
79
80 BN_CTX_start(ctx);
81 rr = (a != r) ? r : BN_CTX_get(ctx);
82 tmp = BN_CTX_get(ctx);
83 if (rr == NULL || tmp == NULL)
84 goto err;
85
86 max = 2 * al; /* Non-zero (from above) */
87 if (!bn_wexpand(rr, max))
88 goto err;
89
90 if (al == 4) {
91#ifndef BN_SQR_COMBA
92 BN_ULONG t[8];
93 bn_sqr_normal(rr->d, a->d, 4, t);
94#else
95 bn_sqr_comba4(rr->d, a->d);
96#endif
97 } else if (al == 8) {
98#ifndef BN_SQR_COMBA
99 BN_ULONG t[16];
100 bn_sqr_normal(rr->d, a->d, 8, t);
101#else
102 bn_sqr_comba8(rr->d, a->d);
103#endif
104 } else {
105#if defined(BN_RECURSION)
106 if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {
107 BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL*2];
108 bn_sqr_normal(rr->d, a->d, al, t);
109 } else {
110 int j, k;
111
112 j = BN_num_bits_word((BN_ULONG)al);
113 j = 1 << (j - 1);
114 k = j + j;
115 if (al == j) {
116 if (!bn_wexpand(tmp, k * 2))
117 goto err;
118 bn_sqr_recursive(rr->d, a->d, al, tmp->d);
119 } else {
120 if (!bn_wexpand(tmp, max))
121 goto err;
122 bn_sqr_normal(rr->d, a->d, al, tmp->d);
123 }
124 }
125#else
126 if (!bn_wexpand(tmp, max))
127 goto err;
128 bn_sqr_normal(rr->d, a->d, al, tmp->d);
129#endif
130 }
131
132 rr->neg = 0;
133 /* If the most-significant half of the top word of 'a' is zero, then
134 * the square of 'a' will max-1 words. */
135 if (a->d[al - 1] == (a->d[al - 1] & BN_MASK2l))
136 rr->top = max - 1;
137 else
138 rr->top = max;
139 if (rr != r)
140 BN_copy(r, rr);
141 ret = 1;
142
143err:
144 BN_CTX_end(ctx);
145 return (ret);
146}
147
148/* tmp must have 2*n words */ 64/* tmp must have 2*n words */
149void 65void
150bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp) 66bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)
@@ -274,3 +190,87 @@ bn_sqr_recursive(BN_ULONG *r, const BN_ULONG *a, int n2, BN_ULONG *t)
274 } 190 }
275} 191}
276#endif 192#endif
193
194/* I've just gone over this and it is now %20 faster on x86 - eay - 27 Jun 96 */
195int
196BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
197{
198 int max, al;
199 int ret = 0;
200 BIGNUM *tmp, *rr;
201
202
203 al = a->top;
204 if (al <= 0) {
205 r->top = 0;
206 r->neg = 0;
207 return 1;
208 }
209
210 BN_CTX_start(ctx);
211 rr = (a != r) ? r : BN_CTX_get(ctx);
212 tmp = BN_CTX_get(ctx);
213 if (rr == NULL || tmp == NULL)
214 goto err;
215
216 max = 2 * al; /* Non-zero (from above) */
217 if (!bn_wexpand(rr, max))
218 goto err;
219
220 if (al == 4) {
221#ifndef BN_SQR_COMBA
222 BN_ULONG t[8];
223 bn_sqr_normal(rr->d, a->d, 4, t);
224#else
225 bn_sqr_comba4(rr->d, a->d);
226#endif
227 } else if (al == 8) {
228#ifndef BN_SQR_COMBA
229 BN_ULONG t[16];
230 bn_sqr_normal(rr->d, a->d, 8, t);
231#else
232 bn_sqr_comba8(rr->d, a->d);
233#endif
234 } else {
235#if defined(BN_RECURSION)
236 if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {
237 BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL*2];
238 bn_sqr_normal(rr->d, a->d, al, t);
239 } else {
240 int j, k;
241
242 j = BN_num_bits_word((BN_ULONG)al);
243 j = 1 << (j - 1);
244 k = j + j;
245 if (al == j) {
246 if (!bn_wexpand(tmp, k * 2))
247 goto err;
248 bn_sqr_recursive(rr->d, a->d, al, tmp->d);
249 } else {
250 if (!bn_wexpand(tmp, max))
251 goto err;
252 bn_sqr_normal(rr->d, a->d, al, tmp->d);
253 }
254 }
255#else
256 if (!bn_wexpand(tmp, max))
257 goto err;
258 bn_sqr_normal(rr->d, a->d, al, tmp->d);
259#endif
260 }
261
262 rr->neg = 0;
263 /* If the most-significant half of the top word of 'a' is zero, then
264 * the square of 'a' will max-1 words. */
265 if (a->d[al - 1] == (a->d[al - 1] & BN_MASK2l))
266 rr->top = max - 1;
267 else
268 rr->top = max;
269 if (rr != r)
270 BN_copy(r, rr);
271 ret = 1;
272
273err:
274 BN_CTX_end(ctx);
275 return (ret);
276}