<commit_before>#include <botan/dsa.h>
#include <botan/rsa.h>
#include <botan/dh.h>
#include <botan/pkcs8.h>
#include <botan/look_pk.h>
#include <botan/rng.h>

#if !defined(BOTAN_NO_NR)
  #include <botan/nr.h>
#endif

#if !defined(BOTAN_NO_RW)
  #include <botan/rw.h>
#endif

#if !defined(BOTAN_NO_ELG)
  #include <botan/elgamal.h>
#endif

using namespace Botan;

#include "common.h"

#include <iostream>
#include <fstream>
#include <string>

#define DEBUG 0

#define PRINT_MS_PER_OP 0 /* If 0, print ops / second */

RSA_PrivateKey* load_rsa_key(const std::string&);
#if !defined(BOTAN_NO_RW)
RW_PrivateKey  load_rw_key(const std::string&);
#endif

static BigInt to_bigint(const std::string& h)
   {
   return BigInt::decode((const byte*)h.data(),
                         h.length(), BigInt::Hexadecimal);
   }

void bench_enc(PK_Encryptor*, const std::string&, double, bool);
void bench_dec(PK_Encryptor*, PK_Decryptor*, const std::string&, double, bool);
void bench_sig(PK_Signer*, const std::string&, double, bool);
void bench_ver(PK_Signer*, PK_Verifier*, const std::string&, double, bool);
void bench_kas(PK_Key_Agreement*, const std::string&, double, bool);

void bench_rsa(RSA_PrivateKey& key, const std::string keybits,
               double seconds, bool html)
   {
   bench_enc(get_pk_encryptor(key, "Raw"),
             "RSA-" + keybits, seconds, html);
   bench_dec(get_pk_encryptor(key, "Raw"),
             get_pk_decryptor(key, "Raw"),
             "RSA-" + keybits, seconds, html);
   }

void bench_dsa(DSA_PrivateKey& key, const std::string keybits,
               double seconds, bool html)
   {
   bench_ver(get_pk_signer(key, "EMSA1(SHA-1)"),
             get_pk_verifier(key, "EMSA1(SHA-1)"),
             "DSA-" + keybits, seconds, html);
   bench_sig(get_pk_signer(key, "EMSA1(SHA-1)"),
             "DSA-" + keybits, seconds, html);
   }

void bench_dh(DH_PrivateKey& key, const std::string keybits,
              double seconds, bool html)
   {
   bench_kas(get_pk_kas(key, "Raw"),
             "DH-" + keybits, seconds, html);
   }

#if !defined(BOTAN_NO_RW)
void bench_rw(RW_PrivateKey& key, const std::string keybits,
              double seconds, bool html)
   {
   bench_ver(get_pk_signer(key, "EMSA2(SHA-1)"),
             get_pk_verifier(key, "EMSA2(SHA-1)"),
             "RW-" + keybits, seconds, html);
   bench_sig(get_pk_signer(key, "EMSA2(SHA-1)"),
             "RW-" + keybits, seconds, html);
   }
#endif

#if !defined(BOTAN_NO_NR)
void bench_nr(NR_PrivateKey& key, const std::string keybits,
              double seconds, bool html)
   {
   bench_ver(get_pk_signer(key, "EMSA1(SHA-1)"),
             get_pk_verifier(key, "EMSA1(SHA-1)"),
             "NR-" + keybits, seconds, html);
   bench_sig(get_pk_signer(key, "EMSA1(SHA-1)"),
             "NR-" + keybits, seconds, html);
   }
#endif

#if !defined(BOTAN_NO_ELG)
void bench_elg(ElGamal_PrivateKey& key, const std::string keybits,
               double seconds, bool html)
   {
   bench_enc(get_pk_encryptor(key, "Raw"),
             "ELG-" + keybits, seconds, html);
   bench_dec(get_pk_encryptor(key, "Raw"),
             get_pk_decryptor(key, "Raw"),
             "ELG-" + keybits, seconds, html);
   }
#endif

void bench_pk(const std::string& algo, bool html, double seconds)
   {
   /*
     There is some strangeness going on here. It looks like algorithms
     at the end take some kind of pentalty. For example, running the RW tests
     first got a result of:
         RW-1024: 148.14 ms / private operation
     but running them last output:
         RW-1024: 363.54 ms / private operation

     I think it's from memory fragmentation in the allocators, but I'm
     not really sure. Need to investigate.

     Until then, I've basically ordered the tests in order of most important
     algorithms (RSA, DSA) to least important (NR, RW).

     This strange behaviour does not seem to occur with DH (?)

     To get more accurate runs, use --bench-algo (RSA|DSA|DH|ELG|NR); in this
     case the distortion is less than 5%, which is good enough.

     We do random keys with the DL schemes, since it's so easy and fast to
     generate keys for them. For RSA and RW, we load the keys from a file.  The
     RSA keys are stored in a PKCS #8 structure, while RW is stored in a more
     ad-hoc format (the RW algorithm has no assigned OID that I know of, so
     there is no way to encode a RW key into a PKCS #8 structure).
   */
   try {

   if(algo == "All" || algo == "RSA")
      {
      #define DO_RSA(NUM_STR, FILENAME)                \
         {                                             \
         RSA_PrivateKey* rsa = load_rsa_key(FILENAME); \
         bench_rsa(*rsa, NUM_STR, seconds, html);      \
         delete rsa;                                   \
         }

      DO_RSA("512", "checks/keys/rsa512.key")
      DO_RSA("1024", "checks/keys/rsa1024.key")
      DO_RSA("1536", "checks/keys/rsa1536.key")
      DO_RSA("2048", "checks/keys/rsa2048.key")
      DO_RSA("3072", "checks/keys/rsa3072.key")
      DO_RSA("4096", "checks/keys/rsa4096.key")
      #undef DO_RSA
      }
   if(algo == "All" || algo == "DSA")
      {
      #define DO_DSA(NUM_STR, GROUP)              \
         {                                        \
         DSA_PrivateKey dsa(DL_Group(GROUP));     \
         bench_dsa(dsa, NUM_STR, seconds, html);  \
         }

      DO_DSA("512",  "dsa/jce/512");
      DO_DSA("768",  "dsa/jce/768");
      DO_DSA("1024", "dsa/jce/1024");
      #undef DO_DSA
      }
   if(algo == "All" || algo == "DH")
      {
      #define DO_DH(NUM_STR, GROUP)             \
         {                                      \
         DH_PrivateKey dh(DL_Group(GROUP)); \
         bench_dh(dh, NUM_STR, seconds, html);  \
         }

      DO_DH("768", "modp/ietf/768");
      DO_DH("1024", "modp/ietf/1024");
      DO_DH("1536", "modp/ietf/1536");
      DO_DH("2048", "modp/ietf/2048");
      DO_DH("3072", "modp/ietf/3072");
      DO_DH("4096", "modp/ietf/4096");
      #undef DO_DH
      }
#if !defined(BOTAN_NO_ELG)
   if(algo == "All" || algo == "ELG" || algo == "ElGamal")
      {
      #define DO_ELG(NUM_STR, GROUP)                  \
         {                                            \
         ElGamal_PrivateKey elg(DL_Group(GROUP)); \<commit_msg>DSA-2048 and DSA-3072 benchmarks, disabled for a while due to lack of 'standard' groups (I may just go ahead and do this).<commit_after>#include <botan/dsa.h>
#include <botan/rsa.h>
#include <botan/dh.h>
#include <botan/pkcs8.h>
#include <botan/look_pk.h>
#include <botan/rng.h>

#if !defined(BOTAN_NO_NR)
  #include <botan/nr.h>
#endif

#if !defined(BOTAN_NO_RW)
  #include <botan/rw.h>
#endif

#if !defined(BOTAN_NO_ELG)
  #include <botan/elgamal.h>
#endif

using namespace Botan;

#include "common.h"

#include <iostream>
#include <fstream>
#include <string>

#define DEBUG 0

#define PRINT_MS_PER_OP 0 /* If 0, print ops / second */

RSA_PrivateKey* load_rsa_key(const std::string&);
#if !defined(BOTAN_NO_RW)
RW_PrivateKey  load_rw_key(const std::string&);
#endif

static BigInt to_bigint(const std::string& h)
   {
   return BigInt::decode((const byte*)h.data(),
                         h.length(), BigInt::Hexadecimal);
   }

void bench_enc(PK_Encryptor*, const std::string&, double, bool);
void bench_dec(PK_Encryptor*, PK_Decryptor*, const std::string&, double, bool);
void bench_sig(PK_Signer*, const std::string&, double, bool);
void bench_ver(PK_Signer*, PK_Verifier*, const std::string&, double, bool);
void bench_kas(PK_Key_Agreement*, const std::string&, double, bool);

void bench_rsa(RSA_PrivateKey& key, const std::string keybits,
               double seconds, bool html)
   {
   bench_enc(get_pk_encryptor(key, "Raw"),
             "RSA-" + keybits, seconds, html);
   bench_dec(get_pk_encryptor(key, "Raw"),
             get_pk_decryptor(key, "Raw"),
             "RSA-" + keybits, seconds, html);
   }

void bench_dsa(DSA_PrivateKey& key, const std::string keybits,
               double seconds, bool html)
   {
   bench_ver(get_pk_signer(key, "EMSA1(SHA-1)"),
             get_pk_verifier(key, "EMSA1(SHA-1)"),
             "DSA-" + keybits, seconds, html);
   bench_sig(get_pk_signer(key, "EMSA1(SHA-1)"),
             "DSA-" + keybits, seconds, html);
   }

void bench_dh(DH_PrivateKey& key, const std::string keybits,
              double seconds, bool html)
   {
   bench_kas(get_pk_kas(key, "Raw"),
             "DH-" + keybits, seconds, html);
   }

#if !defined(BOTAN_NO_RW)
void bench_rw(RW_PrivateKey& key, const std::string keybits,
              double seconds, bool html)
   {
   bench_ver(get_pk_signer(key, "EMSA2(SHA-1)"),
             get_pk_verifier(key, "EMSA2(SHA-1)"),
             "RW-" + keybits, seconds, html);
   bench_sig(get_pk_signer(key, "EMSA2(SHA-1)"),
             "RW-" + keybits, seconds, html);
   }
#endif

#if !defined(BOTAN_NO_NR)
void bench_nr(NR_PrivateKey& key, const std::string keybits,
              double seconds, bool html)
   {
   bench_ver(get_pk_signer(key, "EMSA1(SHA-1)"),
             get_pk_verifier(key, "EMSA1(SHA-1)"),
             "NR-" + keybits, seconds, html);
   bench_sig(get_pk_signer(key, "EMSA1(SHA-1)"),
             "NR-" + keybits, seconds, html);
   }
#endif

#if !defined(BOTAN_NO_ELG)
void bench_elg(ElGamal_PrivateKey& key, const std::string keybits,
               double seconds, bool html)
   {
   bench_enc(get_pk_encryptor(key, "Raw"),
             "ELG-" + keybits, seconds, html);
   bench_dec(get_pk_encryptor(key, "Raw"),
             get_pk_decryptor(key, "Raw"),
             "ELG-" + keybits, seconds, html);
   }
#endif

void bench_pk(const std::string& algo, bool html, double seconds)
   {
   /*
     There is some strangeness going on here. It looks like algorithms
     at the end take some kind of pentalty. For example, running the RW tests
     first got a result of:
         RW-1024: 148.14 ms / private operation
     but running them last output:
         RW-1024: 363.54 ms / private operation

     I think it's from memory fragmentation in the allocators, but I'm
     not really sure. Need to investigate.

     Until then, I've basically ordered the tests in order of most important
     algorithms (RSA, DSA) to least important (NR, RW).

     This strange behaviour does not seem to occur with DH (?)

     To get more accurate runs, use --bench-algo (RSA|DSA|DH|ELG|NR); in this
     case the distortion is less than 5%, which is good enough.

     We do random keys with the DL schemes, since it's so easy and fast to
     generate keys for them. For RSA and RW, we load the keys from a file.  The
     RSA keys are stored in a PKCS #8 structure, while RW is stored in a more
     ad-hoc format (the RW algorithm has no assigned OID that I know of, so
     there is no way to encode a RW key into a PKCS #8 structure).
   */
   try {

   if(algo == "All" || algo == "RSA")
      {
      #define DO_RSA(NUM_STR, FILENAME)                \
         {                                             \
         RSA_PrivateKey* rsa = load_rsa_key(FILENAME); \
         bench_rsa(*rsa, NUM_STR, seconds, html);      \
         delete rsa;                                   \
         }

      DO_RSA("512", "checks/keys/rsa512.key")
      DO_RSA("1024", "checks/keys/rsa1024.key")
      DO_RSA("1536", "checks/keys/rsa1536.key")
      DO_RSA("2048", "checks/keys/rsa2048.key")
      DO_RSA("3072", "checks/keys/rsa3072.key")
      DO_RSA("4096", "checks/keys/rsa4096.key")
      #undef DO_RSA
      }
   if(algo == "All" || algo == "DSA")
      {
      #define DO_DSA(NUM_STR, GROUP)              \
         {                                        \
         DSA_PrivateKey dsa(GROUP);               \
         bench_dsa(dsa, NUM_STR, seconds, html);  \
         }

      DO_DSA("512",  DL_Group("dsa/jce/512"));
      DO_DSA("768",  DL_Group("dsa/jce/768"));
      DO_DSA("1024", DL_Group("dsa/jce/1024"));
      //DO_DSA("2048", DL_Group(DL_Group::DSA_Kosherizer, 2048, 256));
      //DO_DSA("3072", DL_Group(DL_Group::DSA_Kosherizer, 3072, 256));
      #undef DO_DSA
      }
   if(algo == "All" || algo == "DH")
      {
      #define DO_DH(NUM_STR, GROUP)             \
         {                                      \
         DH_PrivateKey dh(DL_Group(GROUP)); \
         bench_dh(dh, NUM_STR, seconds, html);  \
         }

      DO_DH("768", "modp/ietf/768");
      DO_DH("1024", "modp/ietf/1024");
      DO_DH("1536", "modp/ietf/1536");
      DO_DH("2048", "modp/ietf/2048");
      DO_DH("3072", "modp/ietf/3072");
      DO_DH("4096", "modp/ietf/4096");
      #undef DO_DH
      }
#if !defined(BOTAN_NO_ELG)
   if(algo == "All" || algo == "ELG" || algo == "ElGamal")
      {
      #define DO_ELG(NUM_STR, GROUP)                  \
         {                                            \
         ElGamal_PrivateKey elg(DL_Group(GROUP)); \
{'fraction_non_alphanumeric': 0.13469165564973948, 'fraction_numerical': 0.026440625243020453, 'mean_word_length': 2.4030166710770047, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7616505', 'n_tokens_mistral': 4909, 'n_tokens_neox': 4526, 'n_words': 1095}