Skip to content

Commit

Permalink
add some additional test cases in modexp benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
spoonincode committed Sep 13, 2023
1 parent d5e0814 commit aee9599
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions benchmark/modexp.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <fc/crypto/modular_arithmetic.hpp>
#include <fc/exception/exception.hpp>

#include <random>

Expand All @@ -24,15 +25,14 @@ void modexp_benchmarking() {
return result;
};

static constexpr unsigned int start_num_bytes = 128; // 64
static constexpr unsigned int end_num_bytes = 256; // 512
static constexpr unsigned int delta_num_bytes = 128; // 64
static constexpr unsigned int start_num_bytes = 8;
static constexpr unsigned int end_num_bytes = 256;

static_assert(start_num_bytes <= end_num_bytes);
static_assert(delta_num_bytes > 0);
static_assert((end_num_bytes - start_num_bytes) % delta_num_bytes == 0);
static_assert((start_num_bytes & (start_num_bytes - 1)) == 0);
static_assert((end_num_bytes & (end_num_bytes - 1)) == 0);

for (unsigned int n = start_num_bytes, slot = 0; n <= end_num_bytes; n += delta_num_bytes, ++slot) {
for (unsigned int n = start_num_bytes; n <= end_num_bytes; n *= 2) {
auto base = generate_random_bytes(r, n);
auto exponent = generate_random_bytes(r, n);
auto modulus = generate_random_bytes(r, n);
Expand All @@ -41,7 +41,21 @@ void modexp_benchmarking() {
fc::modexp(base, exponent, modulus);
};

benchmarking(std::to_string(n*8) + " bit width", f);
auto even_and_odd = [&](const std::string& bm) {
//some modexp implementations have drastically different performance characteristics depending on whether the modulus is
// even or odd (this can determine whether Montgomery multiplication is used). So test both cases.
modulus.back() &= ~1;
benchmarking(std::to_string(n*8) + " bit even M, " + bm, f);
modulus.back() |= 1;
benchmarking(std::to_string(n*8) + " bit odd M, " + bm, f);
};

//some modexp implementations need to take a minor different path if base is greater than modulus, try both
FC_ASSERT(modulus[0] != '\xff');
base.front() = 0;
even_and_odd("B<M");
base.front() = '\xff';
even_and_odd("B>M");
}

// Running the above benchmark (using commented values for num_trials and *_num_bytes) with a release build on an AMD 3.4 GHz CPU
Expand Down

0 comments on commit aee9599

Please sign in to comment.