github.com/ethereum/go-ethereum@v1.16.1/crypto/secp256k1/libsecp256k1/src/modules/ellswift/bench_impl.h (about)

     1  /***********************************************************************
     2   * Distributed under the MIT software license, see the accompanying    *
     3   * file COPYING or https://www.opensource.org/licenses/mit-license.php.*
     4   ***********************************************************************/
     5  
     6  #ifndef SECP256K1_MODULE_ELLSWIFT_BENCH_H
     7  #define SECP256K1_MODULE_ELLSWIFT_BENCH_H
     8  
     9  #include "../../../include/secp256k1_ellswift.h"
    10  
    11  typedef struct {
    12      secp256k1_context *ctx;
    13      secp256k1_pubkey point[256];
    14      unsigned char rnd64[64];
    15  } bench_ellswift_data;
    16  
    17  static void bench_ellswift_setup(void *arg) {
    18      int i;
    19      bench_ellswift_data *data = (bench_ellswift_data*)arg;
    20      static const unsigned char init[64] = {
    21          0x78, 0x1f, 0xb7, 0xd4, 0x67, 0x7f, 0x08, 0x68,
    22          0xdb, 0xe3, 0x1d, 0x7f, 0x1b, 0xb0, 0xf6, 0x9e,
    23          0x0a, 0x64, 0xca, 0x32, 0x9e, 0xc6, 0x20, 0x79,
    24          0x03, 0xf3, 0xd0, 0x46, 0x7a, 0x0f, 0xd2, 0x21,
    25          0xb0, 0x2c, 0x46, 0xd8, 0xba, 0xca, 0x26, 0x4f,
    26          0x8f, 0x8c, 0xd4, 0xdd, 0x2d, 0x04, 0xbe, 0x30,
    27          0x48, 0x51, 0x1e, 0xd4, 0x16, 0xfd, 0x42, 0x85,
    28          0x62, 0xc9, 0x02, 0xf9, 0x89, 0x84, 0xff, 0xdc
    29      };
    30      memcpy(data->rnd64, init, 64);
    31      for (i = 0; i < 256; ++i) {
    32          int j;
    33          CHECK(secp256k1_ellswift_decode(data->ctx, &data->point[i], data->rnd64));
    34          for (j = 0; j < 64; ++j) {
    35              data->rnd64[j] += 1;
    36          }
    37      }
    38      CHECK(secp256k1_ellswift_encode(data->ctx, data->rnd64, &data->point[255], init + 16));
    39  }
    40  
    41  static void bench_ellswift_encode(void *arg, int iters) {
    42      int i;
    43      bench_ellswift_data *data = (bench_ellswift_data*)arg;
    44  
    45      for (i = 0; i < iters; i++) {
    46          CHECK(secp256k1_ellswift_encode(data->ctx, data->rnd64, &data->point[i & 255], data->rnd64 + 16));
    47      }
    48  }
    49  
    50  static void bench_ellswift_create(void *arg, int iters) {
    51      int i;
    52      bench_ellswift_data *data = (bench_ellswift_data*)arg;
    53  
    54      for (i = 0; i < iters; i++) {
    55          unsigned char buf[64];
    56          CHECK(secp256k1_ellswift_create(data->ctx, buf, data->rnd64, data->rnd64 + 32));
    57          memcpy(data->rnd64, buf, 64);
    58      }
    59  }
    60  
    61  static void bench_ellswift_decode(void *arg, int iters) {
    62      int i;
    63      secp256k1_pubkey out;
    64      size_t len;
    65      bench_ellswift_data *data = (bench_ellswift_data*)arg;
    66  
    67      for (i = 0; i < iters; i++) {
    68          CHECK(secp256k1_ellswift_decode(data->ctx, &out, data->rnd64) == 1);
    69          len = 33;
    70          CHECK(secp256k1_ec_pubkey_serialize(data->ctx, data->rnd64 + (i % 32), &len, &out, SECP256K1_EC_COMPRESSED));
    71      }
    72  }
    73  
    74  static void bench_ellswift_xdh(void *arg, int iters) {
    75      int i;
    76      bench_ellswift_data *data = (bench_ellswift_data*)arg;
    77  
    78      for (i = 0; i < iters; i++) {
    79          int party = i & 1;
    80          CHECK(secp256k1_ellswift_xdh(data->ctx,
    81                                       data->rnd64 + (i % 33),
    82                                       data->rnd64,
    83                                       data->rnd64,
    84                                       data->rnd64 + ((i + 16) % 33),
    85                                       party,
    86                                       secp256k1_ellswift_xdh_hash_function_bip324,
    87                                       NULL) == 1);
    88      }
    89  }
    90  
    91  void run_ellswift_bench(int iters, int argc, char **argv) {
    92      bench_ellswift_data data;
    93      int d = argc == 1;
    94  
    95      /* create a context with signing capabilities */
    96      data.ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
    97  
    98      if (d || have_flag(argc, argv, "ellswift") || have_flag(argc, argv, "encode") || have_flag(argc, argv, "ellswift_encode")) run_benchmark("ellswift_encode", bench_ellswift_encode, bench_ellswift_setup, NULL, &data, 10, iters);
    99      if (d || have_flag(argc, argv, "ellswift") || have_flag(argc, argv, "decode") || have_flag(argc, argv, "ellswift_decode")) run_benchmark("ellswift_decode", bench_ellswift_decode, bench_ellswift_setup, NULL, &data, 10, iters);
   100      if (d || have_flag(argc, argv, "ellswift") || have_flag(argc, argv, "keygen") || have_flag(argc, argv, "ellswift_keygen")) run_benchmark("ellswift_keygen", bench_ellswift_create, bench_ellswift_setup, NULL, &data, 10, iters);
   101      if (d || have_flag(argc, argv, "ellswift") || have_flag(argc, argv, "ecdh") || have_flag(argc, argv, "ellswift_ecdh")) run_benchmark("ellswift_ecdh", bench_ellswift_xdh, bench_ellswift_setup, NULL, &data, 10, iters);
   102  
   103      secp256k1_context_destroy(data.ctx);
   104  }
   105  
   106  #endif