github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/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  typedef struct {
    15      secp256k1_context *ctx;
    16      unsigned char msg[32];
    17      unsigned char key[32];
    18      unsigned char sig[72];
    19      size_t siglen;
    20      unsigned char pubkey[33];
    21      size_t pubkeylen;
    22  } benchmark_verify_t;
    23  
    24  static void benchmark_verify(void* arg) {
    25      int i;
    26      benchmark_verify_t* data = (benchmark_verify_t*)arg;
    27  
    28      for (i = 0; i < 20000; i++) {
    29          secp256k1_pubkey pubkey;
    30          secp256k1_ecdsa_signature sig;
    31          data->sig[data->siglen - 1] ^= (i & 0xFF);
    32          data->sig[data->siglen - 2] ^= ((i >> 8) & 0xFF);
    33          data->sig[data->siglen - 3] ^= ((i >> 16) & 0xFF);
    34          CHECK(secp256k1_ec_pubkey_parse(data->ctx, &pubkey, data->pubkey, data->pubkeylen) == 1);
    35          CHECK(secp256k1_ecdsa_signature_parse_der(data->ctx, &sig, data->sig, data->siglen) == 1);
    36          CHECK(secp256k1_ecdsa_verify(data->ctx, &sig, data->msg, &pubkey) == (i == 0));
    37          data->sig[data->siglen - 1] ^= (i & 0xFF);
    38          data->sig[data->siglen - 2] ^= ((i >> 8) & 0xFF);
    39          data->sig[data->siglen - 3] ^= ((i >> 16) & 0xFF);
    40      }
    41  }
    42  
    43  int main(void) {
    44      int i;
    45      secp256k1_pubkey pubkey;
    46      secp256k1_ecdsa_signature sig;
    47      benchmark_verify_t data;
    48  
    49      data.ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
    50  
    51      for (i = 0; i < 32; i++) {
    52          data.msg[i] = 1 + i;
    53      }
    54      for (i = 0; i < 32; i++) {
    55          data.key[i] = 33 + i;
    56      }
    57      data.siglen = 72;
    58      CHECK(secp256k1_ecdsa_sign(data.ctx, &sig, data.msg, data.key, NULL, NULL));
    59      CHECK(secp256k1_ecdsa_signature_serialize_der(data.ctx, data.sig, &data.siglen, &sig));
    60      CHECK(secp256k1_ec_pubkey_create(data.ctx, &pubkey, data.key));
    61      CHECK(secp256k1_ec_pubkey_serialize(data.ctx, data.pubkey, &data.pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED) == 1);
    62  
    63      run_benchmark("ecdsa_verify", benchmark_verify, NULL, NULL, &data, 10, 20000);
    64  
    65      secp256k1_context_destroy(data.ctx);
    66      return 0;
    67  }