|
4 | 4 | #include <electionguard/constants.h> |
5 | 5 | #include <electionguard/elgamal.hpp> |
6 | 6 | #include <electionguard/group.hpp> |
| 7 | +#include <electionguard/precompute_buffers.hpp> |
7 | 8 |
|
8 | 9 | using namespace electionguard; |
9 | 10 | using namespace std; |
@@ -51,4 +52,57 @@ BENCHMARK_DEFINE_F(HashedElgamalEncryptFixture, HashedElGamalEncrypt)(benchmark: |
51 | 52 |
|
52 | 53 | BENCHMARK_REGISTER_F(HashedElgamalEncryptFixture, HashedElGamalEncrypt)->Unit(benchmark::kMillisecond); |
53 | 54 |
|
| 55 | +class HashedElgamalEncryptPrecomputeFixture : public benchmark::Fixture |
| 56 | +{ |
| 57 | + public: |
| 58 | + void SetUp(const ::benchmark::State &state) |
| 59 | + { |
| 60 | + |
| 61 | + nonce = rand_q(); |
| 62 | + secret = ElementModQ::fromHex(a_fixed_secret); |
| 63 | + keypair = ElGamalKeyPair::fromSecret(TWO_MOD_Q(), false); |
| 64 | + uint64_t qwords_to_use[4] = {0x0102030405060708, 0x090a0b0c0d0e0f10, 0x1112131415161718, |
| 65 | + 0x191a1b1c1d1e1f20}; |
| 66 | + descriptionHash = make_unique<ElementModQ>(qwords_to_use); |
| 67 | + uint8_t bytes_to_use[32] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 68 | + 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, |
| 69 | + 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, |
| 70 | + 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20}; |
| 71 | + vector<uint8_t> plain(bytes_to_use, bytes_to_use + sizeof(bytes_to_use)); |
| 72 | + plaintext = plain; |
| 73 | + |
| 74 | + std::unique_ptr<HashedElGamalCiphertext> HEGResult = hashedElgamalEncrypt( |
| 75 | + plaintext, *nonce, *keypair->getPublicKey(), *descriptionHash, NO_PADDING); |
| 76 | + |
| 77 | + // cause precomputed entries that will be used by the selection |
| 78 | + // encryptions, that should be more than enough and on teardown |
| 79 | + // the rest will be removed. |
| 80 | + PrecomputeBufferContext::init(300); |
| 81 | + PrecomputeBufferContext::populate(*keypair->getPublicKey()); |
| 82 | + } |
| 83 | + |
| 84 | + void TearDown(const ::benchmark::State &state) |
| 85 | + { |
| 86 | + PrecomputeBufferContext::empty_queues(); |
| 87 | + } |
| 88 | + |
| 89 | + unique_ptr<ElementModQ> nonce; |
| 90 | + unique_ptr<ElementModQ> secret; |
| 91 | + unique_ptr<ElGamalKeyPair> keypair; |
| 92 | + unique_ptr<ElementModQ> descriptionHash; |
| 93 | + vector<uint8_t> plaintext; |
| 94 | +}; |
| 95 | + |
| 96 | +BENCHMARK_DEFINE_F(HashedElgamalEncryptPrecomputeFixture, HashedElGamalEncryptPrecompute) |
| 97 | +(benchmark::State &state) |
| 98 | +{ |
| 99 | + for (auto _ : state) { |
| 100 | + hashedElgamalEncrypt(plaintext, *nonce, *keypair->getPublicKey(), *descriptionHash, |
| 101 | + NO_PADDING); |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +BENCHMARK_REGISTER_F(HashedElgamalEncryptPrecomputeFixture, HashedElGamalEncryptPrecompute) |
| 106 | + ->Unit(benchmark::kMillisecond); |
| 107 | + |
54 | 108 |
|
0 commit comments