github.com/kisexp/xdchain@v0.0.0-20211206025815-490d6b732aa7/crypto/secp256k1/libsecp256k1/src/bench_verify.c (about)

     1  /**********************************************************************
     2   * Copyright (c) 2014 Pieter Wuille                                   *
     3   * Distributed under the MIT software license, see the accompanying   *
     4   * file COPYING or http://www.opensource.org/licenses/mit-license.php.*
     5   **********************************************************************/
     6  
     7  #include <stdio.h>
     8  #include <string.h>
     9  
    10  #include "include/secp256k1.h"
    11  #include "util.h"
    12  #include "bench.h"
    13  
    14  #ifdef ENABLE_OPENSSL_TESTS
    15  #include <openssl/bn.h>
    16  #include <openssl/ecdsa.h>
    17  #include <openssl/obj_mac.h>
    18  #endif
    19  
    20  typedef struct {
    21      secp256k1_context *ctx;
    22      unsigned char msg[32];
    23      unsigned char key[32];
    24      unsigned char sig[72];
    25      size_t siglen;
    26      unsigned char pubkey[33];
    27      size_t pubkeylen;
    28  #ifdef ENABLE_OPENSSL_TESTS
    29      EC_GROUP* ec_group;
    30  #endif
    31  } benchmark_verify_t;
    32  
    33  static void benchmark_verify(void* arg) {
    34      int i;
    35      benchmark_verify_t* data = (benchmark_verify_t*)arg;
    36  
    37      for (i = 0; i < 20000; i++) {
    38          secp256k1_pubkey pubkey;
    39          secp256k1_ecdsa_signature sig;
    40          data->sig[data->siglen - 1] ^= (i & 0xFF);
    41          data->sig[data->siglen - 2] ^= ((i >> 8) & 0xFF);
    42          data->sig[data->siglen - 3] ^= ((i >> 16) & 0xFF);
    43          CHECK(secp256k1_ec_pubkey_parse(data->ctx, &pubkey, data->pubkey, data->pubkeylen) == 1);
    44          CHECK(secp256k1_ecdsa_signature_parse_der(data->ctx, &sig, data->sig, data->siglen) == 1);
    45          CHECK(secp256k1_ecdsa_verify(data->ctx, &sig, data->msg, &pubkey) == (i == 0));
    46          data->sig[data->siglen - 1] ^= (i & 0xFF);
    47          data->sig[data->siglen - 2] ^= ((i >> 8) & 0xFF);
    48          data->sig[data->siglen - 3] ^= ((i >> 16) & 0xFF);
    49      }
    50  }
    51  
    52  #ifdef ENABLE_OPENSSL_TESTS
    53  static void benchmark_verify_openssl(void* arg) {
    54      int i;
    55      benchmark_verify_t* data = (benchmark_verify_t*)arg;
    56  
    57      for (i = 0; i < 20000; i++) {
    58          data->sig[data->siglen - 1] ^= (i & 0xFF);
    59          data->sig[data->siglen - 2] ^= ((i >> 8) & 0xFF);
    60          data->sig[data->siglen - 3] ^= ((i >> 16) & 0xFF);
    61          {
    62              EC_KEY *pkey = EC_KEY_new();
    63              const unsigned char *pubkey = &data->pubkey[0];
    64              int result;
    65  
    66              CHECK(pkey != NULL);
    67              result = EC_KEY_set_group(pkey, data->ec_group);
    68              CHECK(result);
    69              result = (o2i_ECPublicKey(&pkey, &pubkey, data->pubkeylen)) != NULL;
    70              CHECK(result);
    71              result = ECDSA_verify(0, &data->msg[0], sizeof(data->msg), &data->sig[0], data->siglen, pkey) == (i == 0);
    72              CHECK(result);
    73              EC_KEY_free(pkey);
    74          }
    75          data->sig[data->siglen - 1] ^= (i & 0xFF);
    76          data->sig[data->siglen - 2] ^= ((i >> 8) & 0xFF);
    77          data->sig[data->siglen - 3] ^= ((i >> 16) & 0xFF);
    78      }
    79  }
    80  #endif
    81  
    82  int main(void) {
    83      int i;
    84      secp256k1_pubkey pubkey;
    85      secp256k1_ecdsa_signature sig;
    86      benchmark_verify_t data;
    87  
    88      data.ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
    89  
    90      for (i = 0; i < 32; i++) {
    91          data.msg[i] = 1 + i;
    92      }
    93      for (i = 0; i < 32; i++) {
    94          data.key[i] = 33 + i;
    95      }
    96      data.siglen = 72;
    97      CHECK(secp256k1_ecdsa_sign(data.ctx, &sig, data.msg, data.key, NULL, NULL));
    98      CHECK(secp256k1_ecdsa_signature_serialize_der(data.ctx, data.sig, &data.siglen, &sig));
    99      CHECK(secp256k1_ec_pubkey_create(data.ctx, &pubkey, data.key));
   100      data.pubkeylen = 33;
   101      CHECK(secp256k1_ec_pubkey_serialize(data.ctx, data.pubkey, &data.pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED) == 1);
   102  
   103      run_benchmark("ecdsa_verify", benchmark_verify, NULL, NULL, &data, 10, 20000);
   104  #ifdef ENABLE_OPENSSL_TESTS
   105      data.ec_group = EC_GROUP_new_by_curve_name(NID_secp256k1);
   106      run_benchmark("ecdsa_verify_openssl", benchmark_verify_openssl, NULL, NULL, &data, 10, 20000);
   107      EC_GROUP_free(data.ec_group);
   108  #endif
   109  
   110      secp256k1_context_destroy(data.ctx);
   111      return 0;
   112  }