From ebe9d96bd2f02a580d44336b9fa819fcc0e1da7a Mon Sep 17 00:00:00 2001 From: Andrew Hutchings Date: Wed, 28 Oct 2026 09:54:12 +0000 Subject: [PATCH] Keep sp_int's fixed size ECC routines with sp-math-all sp_int.c has fixed size multiply and square routines for 256-bit and 384-bit numbers that speed up the generic ECC code, and leaves them out whenever WOLFSSL_HAVE_SP_ECC is defined. With WOLFSSL_SP_MATH_ALL the generic code still works on numbers of those sizes: for curves the SP code does not implement (P-384 when only SP P-256 is built, Brainpool and Koblitz curves) and for callers of the public mp API. On MIPS32 with SP code for P-256 only, generic P-384 signing and verification became about 19% slower than with no SP code at all. Keep them with WOLFSSL_SP_MATH_ALL. --- wolfcrypt/src/sp_int.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/wolfcrypt/src/sp_int.c b/wolfcrypt/src/sp_int.c index d6a4bebd4e1..0d52d4caa05 100644 --- a/wolfcrypt/src/sp_int.c +++ b/wolfcrypt/src/sp_int.c @@ -9561,7 +9561,8 @@ static int _sp_mul(const sp_int* a, const sp_int* b, sp_int* r) #endif #ifndef WOLFSSL_SP_SMALL -#if !defined(WOLFSSL_HAVE_SP_ECC) && defined(HAVE_ECC) +#if defined(HAVE_ECC) && (!defined(WOLFSSL_HAVE_SP_ECC) || \ + defined(WOLFSSL_SP_MATH_ALL)) #if (SP_WORD_SIZE == 64 && SP_INT_BITS >= 256) #ifndef SQR_MUL_ASM /* Multiply a by b and store in r: r = a * b @@ -10257,7 +10258,7 @@ static int _sp_mul_12(const sp_int* a, const sp_int* b, sp_int* r) } #endif /* SQR_MUL_ASM */ #endif /* SP_WORD_SIZE == 32 */ -#endif /* !WOLFSSL_HAVE_SP_ECC && HAVE_ECC */ +#endif /* HAVE_ECC && (!WOLFSSL_HAVE_SP_ECC || WOLFSSL_SP_MATH_ALL) */ #if defined(SQR_MUL_ASM) && (defined(WOLFSSL_SP_INT_LARGE_COMBA) || \ (!defined(WOLFSSL_SP_MATH) && defined(WOLFCRYPT_HAVE_SAKKE) && \ @@ -12203,7 +12204,8 @@ int sp_mul(const sp_int* a, const sp_int* b, sp_int* r) } else #ifndef WOLFSSL_SP_SMALL -#if !defined(WOLFSSL_HAVE_SP_ECC) && defined(HAVE_ECC) +#if defined(HAVE_ECC) && (!defined(WOLFSSL_HAVE_SP_ECC) || \ + defined(WOLFSSL_SP_MATH_ALL)) #if (SP_WORD_SIZE == 64 && SP_INT_BITS >= 256) if ((a->used == 4) && (b->used == 4)) { err = _sp_mul_4(a, b, r); @@ -12234,7 +12236,7 @@ int sp_mul(const sp_int* a, const sp_int* b, sp_int* r) else #endif /* SQR_MUL_ASM */ #endif /* SP_WORD_SIZE == 32 */ -#endif /* !WOLFSSL_HAVE_SP_ECC && HAVE_ECC */ +#endif /* HAVE_ECC && (!WOLFSSL_HAVE_SP_ECC || WOLFSSL_SP_MATH_ALL) */ #if defined(SQR_MUL_ASM) && (defined(WOLFSSL_SP_INT_LARGE_COMBA) || \ (!defined(WOLFSSL_SP_MATH) && defined(WOLFCRYPT_HAVE_SAKKE) && \ (SP_WORD_SIZE == 64))) @@ -15324,7 +15326,8 @@ static int _sp_sqr(const sp_int* a, sp_int* r) #endif /* !WOLFSSL_SP_MATH || !WOLFSSL_SP_SMALL */ #ifndef WOLFSSL_SP_SMALL -#if !defined(WOLFSSL_HAVE_SP_ECC) && defined(HAVE_ECC) +#if defined(HAVE_ECC) && (!defined(WOLFSSL_HAVE_SP_ECC) || \ + defined(WOLFSSL_SP_MATH_ALL)) #if (SP_WORD_SIZE == 64 && SP_INT_BITS >= 256) #ifndef SQR_MUL_ASM /* Square a and store in r. r = a * a @@ -15928,7 +15931,7 @@ static int _sp_sqr_12(const sp_int* a, sp_int* r) } #endif /* SQR_MUL_ASM */ #endif /* SP_WORD_SIZE == 32 */ -#endif /* !WOLFSSL_HAVE_SP_ECC && HAVE_ECC */ +#endif /* HAVE_ECC && (!WOLFSSL_HAVE_SP_ECC || WOLFSSL_SP_MATH_ALL) */ #if defined(SQR_MUL_ASM) && (defined(WOLFSSL_SP_INT_LARGE_COMBA) || \ (!defined(WOLFSSL_SP_MATH) && defined(WOLFCRYPT_HAVE_SAKKE) && \ @@ -17428,7 +17431,8 @@ int sp_sqr(const sp_int* a, sp_int* r) } else #ifndef WOLFSSL_SP_SMALL -#if !defined(WOLFSSL_HAVE_SP_ECC) && defined(HAVE_ECC) +#if defined(HAVE_ECC) && (!defined(WOLFSSL_HAVE_SP_ECC) || \ + defined(WOLFSSL_SP_MATH_ALL)) #if (SP_WORD_SIZE == 64 && SP_INT_BITS >= 256) if (a->used == 4) { err = _sp_sqr_4(a, r); @@ -17459,7 +17463,7 @@ int sp_sqr(const sp_int* a, sp_int* r) else #endif /* SQR_MUL_ASM */ #endif /* SP_WORD_SIZE == 32 */ -#endif /* !WOLFSSL_HAVE_SP_ECC && HAVE_ECC */ +#endif /* HAVE_ECC && (!WOLFSSL_HAVE_SP_ECC || WOLFSSL_SP_MATH_ALL) */ #if defined(SQR_MUL_ASM) && (defined(WOLFSSL_SP_INT_LARGE_COMBA) || \ (!defined(WOLFSSL_SP_MATH) && defined(WOLFCRYPT_HAVE_SAKKE) && \ (SP_WORD_SIZE == 64)))