<commit_before>
#include <iostream>
#include <iomanip>

#include <botan/benchmark.h>
#include <botan/libstate.h>
#include <botan/pipe.h>
#include <botan/filters.h>
#include <botan/engine.h>
#include <botan/parsing.h>
#include <botan/symkey.h>

#include "common.h"
#include "bench.h"

namespace {

const std::string algos[] = {

   /* Block ciphers */
   "AES-128",
   "AES-192",
   "AES-256",
   "Blowfish",
   "CAST-128",
   "CAST-256",
   "DES",
   "DESX",
   "GOST",
   "IDEA",
   "KASUMI",
   "Lion(SHA-256,Turing,8192)",
   "Luby-Rackoff(SHA-512)",
   "MARS",
   "MISTY1",
   "Noekeon",
   "RC2",
   "RC5(12)",
   "RC5(16)",
   "RC6",
   "SAFER-SK(10)",
   "SEED",
   "Serpent",
   "Skipjack",
   "Square",
   "TEA",
   "TripleDES",
   "Twofish",
   "XTEA",

   /* Cipher modes */
   "TripleDES/CBC/PKCS7",
   "TripleDES/CBC/CTS",
   "TripleDES/CTR-BE",
   "TripleDES/EAX",
   "TripleDES/OFB",
   "TripleDES/CFB(64)",
   "TripleDES/CFB(32)",
   "TripleDES/CFB(16)",
   "TripleDES/CFB(8)",

   "AES-128/CBC/PKCS7",
   "AES-128/CBC/CTS",
   "AES-128/CTR-BE",
   "AES-128/EAX",
   "AES-128/OFB",
   "AES-128/XTS",
   "AES-128/CFB(128)",
   "AES-128/CFB(64)",
   "AES-128/CFB(32)",
   "AES-128/CFB(16)",
   "AES-128/CFB(8)",

   "Serpent/CBC/PKCS7",
   "Serpent/CBC/CTS",
   "Serpent/CTR-BE",
   "Serpent/EAX",
   "Serpent/OFB",
   "Serpent/XTS",
   "Serpent/CFB(128)",
   "Serpent/CFB(64)",
   "Serpent/CFB(32)",
   "Serpent/CFB(16)",
   "Serpent/CFB(8)",

   /* Stream ciphers */
   "ARC4",
   "Salsa20",
   "Turing",
   "WiderWake4+1-BE",

   /* Checksums */
   "Adler32",
   "CRC24",
   "CRC32",

   /* Hashes */
   "BMW-512",
   "FORK-256",
   "GOST-34.11",
   "HAS-160",
   "MD2",
   "MD4",
   "MD5",
   "RIPEMD-128",
   "RIPEMD-160",
   "SHA-160",
   "SHA-256",
   "SHA-384",
   "SHA-512",
   "Skein-512",
   "Tiger",
   "Whirlpool",

   /* MACs */
   "CMAC(AES-128)",
   "HMAC(SHA-1)",
   "X9.19-MAC",
   "",
};

void report_results(const std::string& algo,
                    const std::map<std::string, double>& speeds)
   {
   // invert, showing fastest impl first
   std::map<double, std::string> results;

   for(std::map<std::string, double>::const_iterator i = speeds.begin();
       i != speeds.end(); ++i)
      {
      // Speeds might collide, tweak slightly to handle this
      if(results[i->second] == "")
         results[i->second] = i->first;
      else
         results[i->second - .01] = i->first;
      }

   std::cout << algo;

   for(std::map<double, std::string>::const_reverse_iterator i = results.rbegin();
       i != results.rend(); ++i)
      {
      std::cout << " [" << i->second << "] "
                << std::fixed << std::setprecision(2) << i->first;
      }
   std::cout << "\n";
   }

}

bool bench_algo(const std::string& algo,
                Botan::RandomNumberGenerator& rng,
                double seconds)
   {
   Botan::Default_Benchmark_Timer timer;
   Botan::Algorithm_Factory& af = Botan::global_state().algorithm_factory();

   u32bit milliseconds = static_cast<u32bit>(seconds * 1000);

   std::map<std::string, double> speeds =
      algorithm_benchmark(algo, milliseconds, timer, rng, af);

   if(speeds.empty()) // maybe a cipher mode, then?
      {
      Botan::Algorithm_Factory::Engine_Iterator i(af);

      std::vector<std::string> algo_parts = Botan::split_on(algo, '/');

      if(algo_parts.empty())
         return false;

      std::string cipher = algo_parts[0];

      u32bit cipher_keylen =
         af.prototype_block_cipher(cipher)->MAXIMUM_KEYLENGTH;<commit_msg>Fix benchmarking of PK stuff<commit_after>
#include <iostream>
#include <iomanip>

#include <botan/benchmark.h>
#include <botan/libstate.h>
#include <botan/pipe.h>
#include <botan/filters.h>
#include <botan/engine.h>
#include <botan/parsing.h>
#include <botan/symkey.h>

#include "common.h"
#include "bench.h"

namespace {

const std::string algos[] = {

   /* Block ciphers */
   "AES-128",
   "AES-192",
   "AES-256",
   "Blowfish",
   "CAST-128",
   "CAST-256",
   "DES",
   "DESX",
   "GOST",
   "IDEA",
   "KASUMI",
   "Lion(SHA-256,Turing,8192)",
   "Luby-Rackoff(SHA-512)",
   "MARS",
   "MISTY1",
   "Noekeon",
   "RC2",
   "RC5(12)",
   "RC5(16)",
   "RC6",
   "SAFER-SK(10)",
   "SEED",
   "Serpent",
   "Skipjack",
   "Square",
   "TEA",
   "TripleDES",
   "Twofish",
   "XTEA",

   /* Cipher modes */
   "TripleDES/CBC/PKCS7",
   "TripleDES/CBC/CTS",
   "TripleDES/CTR-BE",
   "TripleDES/EAX",
   "TripleDES/OFB",
   "TripleDES/CFB(64)",
   "TripleDES/CFB(32)",
   "TripleDES/CFB(16)",
   "TripleDES/CFB(8)",

   "AES-128/CBC/PKCS7",
   "AES-128/CBC/CTS",
   "AES-128/CTR-BE",
   "AES-128/EAX",
   "AES-128/OFB",
   "AES-128/XTS",
   "AES-128/CFB(128)",
   "AES-128/CFB(64)",
   "AES-128/CFB(32)",
   "AES-128/CFB(16)",
   "AES-128/CFB(8)",

   "Serpent/CBC/PKCS7",
   "Serpent/CBC/CTS",
   "Serpent/CTR-BE",
   "Serpent/EAX",
   "Serpent/OFB",
   "Serpent/XTS",
   "Serpent/CFB(128)",
   "Serpent/CFB(64)",
   "Serpent/CFB(32)",
   "Serpent/CFB(16)",
   "Serpent/CFB(8)",

   /* Stream ciphers */
   "ARC4",
   "Salsa20",
   "Turing",
   "WiderWake4+1-BE",

   /* Checksums */
   "Adler32",
   "CRC24",
   "CRC32",

   /* Hashes */
   "BMW-512",
   "FORK-256",
   "GOST-34.11",
   "HAS-160",
   "MD2",
   "MD4",
   "MD5",
   "RIPEMD-128",
   "RIPEMD-160",
   "SHA-160",
   "SHA-256",
   "SHA-384",
   "SHA-512",
   "Skein-512",
   "Tiger",
   "Whirlpool",

   /* MACs */
   "CMAC(AES-128)",
   "HMAC(SHA-1)",
   "X9.19-MAC",
   "",
};

void report_results(const std::string& algo,
                    const std::map<std::string, double>& speeds)
   {
   // invert, showing fastest impl first
   std::map<double, std::string> results;

   for(std::map<std::string, double>::const_iterator i = speeds.begin();
       i != speeds.end(); ++i)
      {
      // Speeds might collide, tweak slightly to handle this
      if(results[i->second] == "")
         results[i->second] = i->first;
      else
         results[i->second - .01] = i->first;
      }

   std::cout << algo;

   for(std::map<double, std::string>::const_reverse_iterator i = results.rbegin();
       i != results.rend(); ++i)
      {
      std::cout << " [" << i->second << "] "
                << std::fixed << std::setprecision(2) << i->first;
      }
   std::cout << "\n";
   }

}

bool bench_algo(const std::string& algo,
                Botan::RandomNumberGenerator& rng,
                double seconds)
   {
   Botan::Default_Benchmark_Timer timer;
   Botan::Algorithm_Factory& af = Botan::global_state().algorithm_factory();

   u32bit milliseconds = static_cast<u32bit>(seconds * 1000);

   std::map<std::string, double> speeds =
      algorithm_benchmark(algo, milliseconds, timer, rng, af);

   if(speeds.empty()) // maybe a cipher mode, then?
      {
      Botan::Algorithm_Factory::Engine_Iterator i(af);

      std::vector<std::string> algo_parts = Botan::split_on(algo, '/');

      if(algo_parts.size() < 2) // not a cipher mode
         return false;

      std::string cipher = algo_parts[0];

      u32bit cipher_keylen =
         af.prototype_block_cipher(cipher)->MAXIMUM_KEYLENGTH;
{'fraction_non_alphanumeric': 0.20365425158116654, 'fraction_numerical': 0.04736472241742797, 'mean_word_length': 2.668041237113402, 'pattern_counts': {'":': 0, '<': 68, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3899027', 'n_tokens_mistral': 3225, 'n_tokens_neox': 2900, 'n_words': 450}