csecp256k1

Haskell FFI bindings to bitcoin-core/secp256k1 (docs.ppad.tech/csecp256k1).
git clone git://git.ppad.tech/csecp256k1.git
Log | Files | Refs | README | LICENSE

bench_impl.h (1959B)


      1 /***********************************************************************
      2  * Copyright (c) 2015 Pieter Wuille, Andrew Poelstra                   *
      3  * Distributed under the MIT software license, see the accompanying    *
      4  * file COPYING or https://www.opensource.org/licenses/mit-license.php.*
      5  ***********************************************************************/
      6 
      7 #ifndef SECP256K1_MODULE_ECDH_BENCH_H
      8 #define SECP256K1_MODULE_ECDH_BENCH_H
      9 
     10 #include "../../../include/secp256k1_ecdh.h"
     11 
     12 typedef struct {
     13     haskellsecp256k1_v0_1_0_context *ctx;
     14     haskellsecp256k1_v0_1_0_pubkey point;
     15     unsigned char scalar[32];
     16 } bench_ecdh_data;
     17 
     18 static void bench_ecdh_setup(void* arg) {
     19     int i;
     20     bench_ecdh_data *data = (bench_ecdh_data*)arg;
     21     const unsigned char point[] = {
     22         0x03,
     23         0x54, 0x94, 0xc1, 0x5d, 0x32, 0x09, 0x97, 0x06,
     24         0xc2, 0x39, 0x5f, 0x94, 0x34, 0x87, 0x45, 0xfd,
     25         0x75, 0x7c, 0xe3, 0x0e, 0x4e, 0x8c, 0x90, 0xfb,
     26         0xa2, 0xba, 0xd1, 0x84, 0xf8, 0x83, 0xc6, 0x9f
     27     };
     28 
     29     for (i = 0; i < 32; i++) {
     30         data->scalar[i] = i + 1;
     31     }
     32     CHECK(haskellsecp256k1_v0_1_0_ec_pubkey_parse(data->ctx, &data->point, point, sizeof(point)) == 1);
     33 }
     34 
     35 static void bench_ecdh(void* arg, int iters) {
     36     int i;
     37     unsigned char res[32];
     38     bench_ecdh_data *data = (bench_ecdh_data*)arg;
     39 
     40     for (i = 0; i < iters; i++) {
     41         CHECK(haskellsecp256k1_v0_1_0_ecdh(data->ctx, res, &data->point, data->scalar, NULL, NULL) == 1);
     42     }
     43 }
     44 
     45 static void run_ecdh_bench(int iters, int argc, char** argv) {
     46     bench_ecdh_data data;
     47     int d = argc == 1;
     48 
     49     /* create a context with no capabilities */
     50     data.ctx = haskellsecp256k1_v0_1_0_context_create(SECP256K1_FLAGS_TYPE_CONTEXT);
     51 
     52     if (d || have_flag(argc, argv, "ecdh")) run_benchmark("ecdh", bench_ecdh, bench_ecdh_setup, NULL, &data, 10, iters);
     53 
     54     haskellsecp256k1_v0_1_0_context_destroy(data.ctx);
     55 }
     56 
     57 #endif /* SECP256K1_MODULE_ECDH_BENCH_H */