Skip to content

Commit

Permalink
Redirect from the template Comba to the unrolled Combas where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
randombit committed Apr 12, 2024
1 parent 283b1c4 commit cb24a71
Showing 1 changed file with 53 additions and 17 deletions.
70 changes: 53 additions & 17 deletions src/lib/math/mp/mp_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -905,11 +905,46 @@ constexpr 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 <size_t N, WordType W>
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<W, word> && N == 4) {
return bigint_comba_mul4(z, x, y);
}
if constexpr(std::same_as<W, word> && N == 6) {
return bigint_comba_mul6(z, x, y);
}
if constexpr(std::same_as<W, word> && N == 8) {
return bigint_comba_mul8(z, x, y);
}
if constexpr(std::same_as<W, word> && N == 9) {
return bigint_comba_mul9(z, x, y);
}
if constexpr(std::same_as<W, word> && N == 16) {
return bigint_comba_mul16(z, x, y);
}
}

word3<W> accum;

for(size_t i = 0; i != 2 * N; ++i) {
Expand All @@ -925,6 +960,24 @@ constexpr inline void comba_mul(W z[2 * N], const W x[N], const W y[N]) {

template <size_t N, WordType W>
constexpr inline void comba_sqr(W z[2 * N], const W x[N]) {
if(!std::is_constant_evaluated()) {
if constexpr(std::same_as<W, word> && N == 4) {
return bigint_comba_sqr4(z, x);
}
if constexpr(std::same_as<W, word> && N == 6) {
return bigint_comba_sqr6(z, x);
}
if constexpr(std::same_as<W, word> && N == 8) {
return bigint_comba_sqr8(z, x);
}
if constexpr(std::same_as<W, word> && N == 9) {
return bigint_comba_sqr9(z, x);
}
if constexpr(std::same_as<W, word> && N == 16) {
return bigint_comba_sqr16(z, x);
}
}

word3<W> accum;

for(size_t i = 0; i != 2 * N; ++i) {
Expand All @@ -938,23 +991,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
*
Expand Down

0 comments on commit cb24a71

Please sign in to comment.