// Copyright (c) 2025-2026, Brandon Lehmann // BSD 3-Clause License (see LICENSE) #include "tinyaes.h" #include #include #include #include static constexpr size_t BENCH_SIZE = 1024 * 1024; // 1 MiB static constexpr int ITERATIONS = 100; template static double bench(const char *name, Fn &&fn) { // Warmup fn(); auto start = std::chrono::high_resolution_clock::now(); for (int i = 0; i < ITERATIONS; ++i) { fn(); } auto end = std::chrono::high_resolution_clock::now(); double ms = std::chrono::duration(end - start).count(); double total_bytes = static_cast(BENCH_SIZE) * ITERATIONS; double mib_per_sec = (total_bytes / (1024.0 * 1024.0)) / (ms / 1000.0); std::printf("%-30s %8.2f MiB/s (%6.2f ms total)\n", name, mib_per_sec, ms); return mib_per_sec; } int main() { std::vector key_128(16, 0x42); std::vector key_256(32, 0x42); std::vector iv_16(16, 0x01); std::vector iv_12(12, 0x01); std::vector plaintext(BENCH_SIZE, 0x55); std::vector aad = {0xAA, 0xBB, 0xCC, 0xDD}; std::printf("TinyAES Benchmarks (%zu bytes x %d iterations)\n", BENCH_SIZE, ITERATIONS); std::printf("================================================================\n"); // ECB bench("ECB-128 encrypt", [&]() { std::vector ct; tinyaes::ecb_encrypt(key_128, plaintext, ct); }); bench("ECB-256 encrypt", [&]() { std::vector ct; tinyaes::ecb_encrypt(key_256, plaintext, ct); }); // CBC bench("CBC-128 encrypt", [&]() { std::vector ct; tinyaes::cbc_encrypt(key_128, iv_16, plaintext, ct); }); bench("CBC-256 encrypt", [&]() { std::vector ct; tinyaes::cbc_encrypt(key_256, iv_16, plaintext, ct); }); // CTR bench("CTR-128 encrypt", [&]() { std::vector ct; tinyaes::ctr_crypt(key_128, iv_16, plaintext, ct); }); bench("CTR-256 encrypt", [&]() { std::vector ct; tinyaes::ctr_crypt(key_256, iv_16, plaintext, ct); }); // GCM bench("GCM-128 encrypt", [&]() { std::vector ct, tag; tinyaes::gcm_encrypt(key_128, iv_12, aad, plaintext, ct, tag); }); bench("GCM-256 encrypt", [&]() { std::vector ct, tag; tinyaes::gcm_encrypt(key_256, iv_12, aad, plaintext, ct, tag); }); return 0; }