bn: Remove the BN_RECURSION cpp define

Just like in previous commit, this define does not represent a toggleable
feature, but is entirely dependent on the OPENSSL_SMALL_FOOTPRINT define.

Reviewed-by: Frederik Wedel-Heinen <fwh.openssl@gmail.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
MergeDate: Mon Jan 12 18:44:27 2026
(Merged from https://github.com/openssl/openssl/pull/29204)
This commit is contained in:
Gleb Popov
2026-01-09 17:48:00 +03:00
committed by Tomas Mraz
parent 2dcf5361dd
commit ea7cbff76d
3 changed files with 16 additions and 36 deletions

View File

@@ -52,10 +52,6 @@
#define BN_SOFT_LIMIT (4096 / BN_BYTES) #define BN_SOFT_LIMIT (4096 / BN_BYTES)
#endif #endif
#ifndef OPENSSL_SMALL_FOOTPRINT
#define BN_RECURSION
#endif
/* /*
* This next option uses the C libraries (2 word)/(1 word) function. If it is * This next option uses the C libraries (2 word)/(1 word) function. If it is
* not defined, I use my C version (which is slower). The reason for this * not defined, I use my C version (which is slower). The reason for this

View File

@@ -154,7 +154,7 @@ BN_ULONG bn_sub_part_words(BN_ULONG *r,
} }
#endif #endif
#ifdef BN_RECURSION #ifndef OPENSSL_SMALL_FOOTPRINT
/* /*
* Karatsuba recursive multiplication algorithm (cf. Knuth, The Art of * Karatsuba recursive multiplication algorithm (cf. Knuth, The Art of
* Computer Programming, Vol. 2) * Computer Programming, Vol. 2)
@@ -180,7 +180,6 @@ void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
unsigned int neg, zero; unsigned int neg, zero;
BN_ULONG ln, lo, *p; BN_ULONG ln, lo, *p;
#ifndef OPENSSL_SMALL_FOOTPRINT
/* /*
* Only call bn_mul_comba 8 if n2 == 8 and the two arrays are complete * Only call bn_mul_comba 8 if n2 == 8 and the two arrays are complete
* [steve] * [steve]
@@ -189,7 +188,7 @@ void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
bn_mul_comba8(r, a, b); bn_mul_comba8(r, a, b);
return; return;
} }
#endif /* OPENSSL_SMALL_FOOTPRINT */
/* Else do normal multiply */ /* Else do normal multiply */
if (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL) { if (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL) {
bn_mul_normal(r, a, n2 + dna, b, n2 + dnb); bn_mul_normal(r, a, n2 + dna, b, n2 + dnb);
@@ -234,7 +233,6 @@ void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
break; break;
} }
#ifndef OPENSSL_SMALL_FOOTPRINT
if (n == 4 && dna == 0 && dnb == 0) { /* XXX: bn_mul_comba4 could take if (n == 4 && dna == 0 && dnb == 0) { /* XXX: bn_mul_comba4 could take
* extra args to do this well */ * extra args to do this well */
if (!zero) if (!zero)
@@ -254,9 +252,7 @@ void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
bn_mul_comba8(r, a, b); bn_mul_comba8(r, a, b);
bn_mul_comba8(&(r[n2]), &(a[n]), &(b[n])); bn_mul_comba8(&(r[n2]), &(a[n]), &(b[n]));
} else } else {
#endif /* OPENSSL_SMALL_FOOTPRINT */
{
p = &(t[n2 * 2]); p = &(t[n2 * 2]);
if (!zero) if (!zero)
bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p); bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);
@@ -486,7 +482,7 @@ void bn_mul_low_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
bn_add_words(&(r[n]), &(r[n]), &(t[n]), n); bn_add_words(&(r[n]), &(r[n]), &(t[n]), n);
} }
} }
#endif /* BN_RECURSION */ #endif /* OPENSSL_SMALL_FOOTPRINT */
int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
{ {
@@ -503,10 +499,8 @@ int bn_mul_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
int ret = 0; int ret = 0;
int top, al, bl; int top, al, bl;
BIGNUM *rr; BIGNUM *rr;
#if !defined(OPENSSL_SMALL_FOOTPRINT) || defined(BN_RECURSION) #if !defined(OPENSSL_SMALL_FOOTPRINT)
int i; int i;
#endif
#ifdef BN_RECURSION
BIGNUM *t = NULL; BIGNUM *t = NULL;
int j = 0, k; int j = 0, k;
#endif #endif
@@ -531,10 +525,9 @@ int bn_mul_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
} else } else
rr = r; rr = r;
#if !defined(OPENSSL_SMALL_FOOTPRINT) || defined(BN_RECURSION) #if !defined(OPENSSL_SMALL_FOOTPRINT)
i = al - bl; i = al - bl;
#endif
#ifndef OPENSSL_SMALL_FOOTPRINT
if (i == 0) { if (i == 0) {
#if 0 #if 0
if (al == 4) { if (al == 4) {
@@ -553,8 +546,7 @@ int bn_mul_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
goto end; goto end;
} }
} }
#endif /* OPENSSL_SMALL_FOOTPRINT */
#ifdef BN_RECURSION
if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) { if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {
if (i >= -1 && i <= 1) { if (i >= -1 && i <= 1) {
/* /*
@@ -592,13 +584,13 @@ int bn_mul_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
goto end; goto end;
} }
} }
#endif /* BN_RECURSION */ #endif /* OPENSSL_SMALL_FOOTPRINT */
if (bn_wexpand(rr, top) == NULL) if (bn_wexpand(rr, top) == NULL)
goto err; goto err;
rr->top = top; rr->top = top;
bn_mul_normal(rr->d, a->d, al, b->d, bl); bn_mul_normal(rr->d, a->d, al, b->d, bl);
#if !defined(OPENSSL_SMALL_FOOTPRINT) || defined(BN_RECURSION) #if !defined(OPENSSL_SMALL_FOOTPRINT)
end: end:
#endif #endif
rr->neg = a->neg ^ b->neg; rr->neg = a->neg ^ b->neg;

View File

@@ -64,7 +64,11 @@ int bn_sqr_fixed_top(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
bn_sqr_comba8(rr->d, a->d); bn_sqr_comba8(rr->d, a->d);
#endif #endif
} else { } else {
#if defined(BN_RECURSION) #ifdef OPENSSL_SMALL_FOOTPRINT
if (bn_wexpand(tmp, max) == NULL)
goto err;
bn_sqr_normal(rr->d, a->d, al, tmp->d);
#else
if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) { if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {
BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2]; BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];
bn_sqr_normal(rr->d, a->d, al, t); bn_sqr_normal(rr->d, a->d, al, t);
@@ -84,10 +88,6 @@ int bn_sqr_fixed_top(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
bn_sqr_normal(rr->d, a->d, al, tmp->d); bn_sqr_normal(rr->d, a->d, al, tmp->d);
} }
} }
#else
if (bn_wexpand(tmp, max) == NULL)
goto err;
bn_sqr_normal(rr->d, a->d, al, tmp->d);
#endif #endif
} }
@@ -141,7 +141,7 @@ void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)
bn_add_words(r, r, tmp, max); bn_add_words(r, r, tmp, max);
} }
#ifdef BN_RECURSION #ifndef OPENSSL_SMALL_FOOTPRINT
/*- /*-
* r is 2*n words in size, * r is 2*n words in size,
* a and b are both n words in size. (There's not actually a 'b' here ...) * a and b are both n words in size. (There's not actually a 'b' here ...)
@@ -160,18 +160,10 @@ void bn_sqr_recursive(BN_ULONG *r, const BN_ULONG *a, int n2, BN_ULONG *t)
BN_ULONG ln, lo, *p; BN_ULONG ln, lo, *p;
if (n2 == 4) { if (n2 == 4) {
#ifdef OPENSSL_SMALL_FOOTPRINT
bn_sqr_normal(r, a, 4, t);
#else
bn_sqr_comba4(r, a); bn_sqr_comba4(r, a);
#endif
return; return;
} else if (n2 == 8) { } else if (n2 == 8) {
#ifdef OPENSSL_SMALL_FOOTPRINT
bn_sqr_normal(r, a, 8, t);
#else
bn_sqr_comba8(r, a); bn_sqr_comba8(r, a);
#endif
return; return;
} }
if (n2 < BN_SQR_RECURSIVE_SIZE_NORMAL) { if (n2 < BN_SQR_RECURSIVE_SIZE_NORMAL) {