github.com/googgoog/go-ethereum@v1.9.7/crypto/secp256k1/libsecp256k1/src/bench_schnorr_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 "include/secp256k1_schnorr.h"
    12  #include "util.h"
    13  #include "bench.h"
    14  
    15  typedef struct {
    16      unsigned char key[32];
    17      unsigned char sig[64];
    18      unsigned char pubkey[33];
    19      size_t pubkeylen;
    20  } benchmark_schnorr_sig_t;
    21  
    22  typedef struct {
    23      secp256k1_context *ctx;
    24      unsigned char msg[32];
    25      benchmark_schnorr_sig_t sigs[64];
    26      int numsigs;
    27  } benchmark_schnorr_verify_t;
    28  
    29  static void benchmark_schnorr_init(void* arg) {
    30      int i, k;
    31      benchmark_schnorr_verify_t* data = (benchmark_schnorr_verify_t*)arg;
    32  
    33      for (i = 0; i < 32; i++) {
    34          data->msg[i] = 1 + i;
    35      }
    36      for (k = 0; k < data->numsigs; k++) {
    37          secp256k1_pubkey pubkey;
    38          for (i = 0; i < 32; i++) {
    39              data->sigs[k].key[i] = 33 + i + k;
    40          }
    41          secp256k1_schnorr_sign(data->ctx, data->sigs[k].sig, data->msg, data->sigs[k].key, NULL, NULL);
    42          data->sigs[k].pubkeylen = 33;
    43          CHECK(secp256k1_ec_pubkey_create(data->ctx, &pubkey, data->sigs[k].key));
    44          CHECK(secp256k1_ec_pubkey_serialize(data->ctx, data->sigs[k].pubkey, &data->sigs[k].pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED));
    45      }
    46  }
    47  
    48  static void benchmark_schnorr_verify(void* arg) {
    49      int i;
    50      benchmark_schnorr_verify_t* data = (benchmark_schnorr_verify_t*)arg;
    51  
    52      for (i = 0; i < 20000 / data->numsigs; i++) {
    53          secp256k1_pubkey pubkey;
    54          data->sigs[0].sig[(i >> 8) % 64] ^= (i & 0xFF);
    55          CHECK(secp256k1_ec_pubkey_parse(data->ctx, &pubkey, data->sigs[0].pubkey, data->sigs[0].pubkeylen));
    56          CHECK(secp256k1_schnorr_verify(data->ctx, data->sigs[0].sig, data->msg, &pubkey) == ((i & 0xFF) == 0));
    57          data->sigs[0].sig[(i >> 8) % 64] ^= (i & 0xFF);
    58      }
    59  }
    60  
    61  
    62  
    63  int main(void) {
    64      benchmark_schnorr_verify_t data;
    65  
    66      data.ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
    67  
    68      data.numsigs = 1;
    69      run_benchmark("schnorr_verify", benchmark_schnorr_verify, benchmark_schnorr_init, NULL, &data, 10, 20000);
    70  
    71      secp256k1_context_destroy(data.ctx);
    72      return 0;
    73  }