From 4f574d29a5f27930766fd43cdaf0f44730615952 Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Thu, 11 Apr 2024 20:05:23 -0400 Subject: [PATCH] Redirect from the template Comba to the unrolled Combas where possible --- src/lib/math/mp/mp_core.h | 70 +++++++++++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 17 deletions(-) diff --git a/src/lib/math/mp/mp_core.h b/src/lib/math/mp/mp_core.h index 27307767579..e80116cee6e 100644 --- a/src/lib/math/mp/mp_core.h +++ b/src/lib/math/mp/mp_core.h @@ -890,11 +890,46 @@ consteval auto hex_to_words(const char (&s)[N]) { return r; } +/* +* Comba Multiplication / Squaring +*/ +BOTAN_FUZZER_API void bigint_comba_mul4(word z[8], const word x[4], const word y[4]); +BOTAN_FUZZER_API void bigint_comba_mul6(word z[12], const word x[6], const word y[6]); +BOTAN_FUZZER_API void bigint_comba_mul8(word z[16], const word x[8], const word y[8]); +BOTAN_FUZZER_API void bigint_comba_mul9(word z[18], const word x[9], const word y[9]); +BOTAN_FUZZER_API void bigint_comba_mul16(word z[32], const word x[16], const word y[16]); +BOTAN_FUZZER_API void bigint_comba_mul24(word z[48], const word x[24], const word y[24]); + +BOTAN_FUZZER_API void bigint_comba_sqr4(word out[8], const word in[4]); +BOTAN_FUZZER_API void bigint_comba_sqr6(word out[12], const word in[6]); +BOTAN_FUZZER_API void bigint_comba_sqr8(word out[16], const word in[8]); +BOTAN_FUZZER_API void bigint_comba_sqr9(word out[18], const word in[9]); +BOTAN_FUZZER_API void bigint_comba_sqr16(word out[32], const word in[16]); +BOTAN_FUZZER_API void bigint_comba_sqr24(word out[48], const word in[24]); + /* * Comba Fixed Length Multiplication */ template constexpr inline void comba_mul(W z[2 * N], const W x[N], const W y[N]) { + if(!std::is_constant_evaluated()) { + if constexpr(std::same_as && N == 4) { + return bigint_comba_mul4(z, x, y); + } + if constexpr(std::same_as && N == 6) { + return bigint_comba_mul6(z, x, y); + } + if constexpr(std::same_as && N == 8) { + return bigint_comba_mul8(z, x, y); + } + if constexpr(std::same_as && N == 9) { + return bigint_comba_mul9(z, x, y); + } + if constexpr(std::same_as && N == 16) { + return bigint_comba_mul16(z, x, y); + } + } + word3 accum; for(size_t i = 0; i != 2 * N; ++i) { @@ -910,6 +945,24 @@ constexpr inline void comba_mul(W z[2 * N], const W x[N], const W y[N]) { template constexpr inline void comba_sqr(W z[2 * N], const W x[N]) { + if(!std::is_constant_evaluated()) { + if constexpr(std::same_as && N == 4) { + return bigint_comba_sqr4(z, x); + } + if constexpr(std::same_as && N == 6) { + return bigint_comba_sqr6(z, x); + } + if constexpr(std::same_as && N == 8) { + return bigint_comba_sqr8(z, x); + } + if constexpr(std::same_as && N == 9) { + return bigint_comba_sqr9(z, x); + } + if constexpr(std::same_as && N == 16) { + return bigint_comba_sqr16(z, x); + } + } + word3 accum; for(size_t i = 0; i != 2 * N; ++i) { @@ -923,23 +976,6 @@ constexpr inline void comba_sqr(W z[2 * N], const W x[N]) { } } -/* -* Comba Multiplication / Squaring -*/ -BOTAN_FUZZER_API void bigint_comba_mul4(word z[8], const word x[4], const word y[4]); -BOTAN_FUZZER_API void bigint_comba_mul6(word z[12], const word x[6], const word y[6]); -BOTAN_FUZZER_API void bigint_comba_mul8(word z[16], const word x[8], const word y[8]); -BOTAN_FUZZER_API void bigint_comba_mul9(word z[18], const word x[9], const word y[9]); -BOTAN_FUZZER_API void bigint_comba_mul16(word z[32], const word x[16], const word y[16]); -BOTAN_FUZZER_API void bigint_comba_mul24(word z[48], const word x[24], const word y[24]); - -BOTAN_FUZZER_API void bigint_comba_sqr4(word out[8], const word in[4]); -BOTAN_FUZZER_API void bigint_comba_sqr6(word out[12], const word in[6]); -BOTAN_FUZZER_API void bigint_comba_sqr8(word out[16], const word in[8]); -BOTAN_FUZZER_API void bigint_comba_sqr9(word out[18], const word in[9]); -BOTAN_FUZZER_API void bigint_comba_sqr16(word out[32], const word in[16]); -BOTAN_FUZZER_API void bigint_comba_sqr24(word out[48], const word in[24]); - /* * Montgomery reduction *