|
| 1 | +/*********************************************************************** |
| 2 | + * Copyright (c) 2022 Pieter Wuille * |
| 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_ELLSWIFT_BENCH_H |
| 8 | +#define SECP256K1_MODULE_ELLSWIFT_BENCH_H |
| 9 | + |
| 10 | +#include "../include/secp256k1_ellswift.h" |
| 11 | + |
| 12 | +typedef struct { |
| 13 | + secp256k1_context *ctx; |
| 14 | + secp256k1_pubkey point; |
| 15 | + unsigned char rnd64[64]; |
| 16 | +} bench_ellswift_data; |
| 17 | + |
| 18 | +static void bench_ellswift_setup(void* arg) { |
| 19 | + bench_ellswift_data *data = (bench_ellswift_data*)arg; |
| 20 | + static const unsigned char point[] = { |
| 21 | + 0x03, |
| 22 | + 0x54, 0x94, 0xc1, 0x5d, 0x32, 0x09, 0x97, 0x06, |
| 23 | + 0xc2, 0x39, 0x5f, 0x94, 0x34, 0x87, 0x45, 0xfd, |
| 24 | + 0x75, 0x7c, 0xe3, 0x0e, 0x4e, 0x8c, 0x90, 0xfb, |
| 25 | + 0xa2, 0xba, 0xd1, 0x84, 0xf8, 0x83, 0xc6, 0x9f |
| 26 | + }; |
| 27 | + memcpy(data->rnd64, point, 32); |
| 28 | + memcpy(data->rnd64 + 32, point + 1, 32); |
| 29 | + CHECK(secp256k1_ec_pubkey_parse(data->ctx, &data->point, point, sizeof(point)) == 1); |
| 30 | +} |
| 31 | + |
| 32 | +static void bench_ellswift_encode(void* arg, int iters) { |
| 33 | + int i; |
| 34 | + bench_ellswift_data *data = (bench_ellswift_data*)arg; |
| 35 | + |
| 36 | + for (i = 0; i < iters; i++) { |
| 37 | + data->rnd64[19] ^= 247; |
| 38 | + data->rnd64[47] ^= 113; |
| 39 | + CHECK(secp256k1_ellswift_encode(data->ctx, data->rnd64, &data->point, data->rnd64 + 16) == 1); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +static void bench_ellswift_create(void* arg, int iters) { |
| 44 | + int i, j; |
| 45 | + bench_ellswift_data *data = (bench_ellswift_data*)arg; |
| 46 | + |
| 47 | + for (i = 0; i < iters; i++) { |
| 48 | + unsigned char out64[64]; |
| 49 | + CHECK(secp256k1_ellswift_create(data->ctx, out64, data->rnd64, data->rnd64 + 32) == 1); |
| 50 | + for (j = 0; j < 64; j++) data->rnd64[j] ^= out64[j]; |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +static void bench_ellswift_decode(void* arg, int iters) { |
| 55 | + int i; |
| 56 | + secp256k1_pubkey out; |
| 57 | + bench_ellswift_data *data = (bench_ellswift_data*)arg; |
| 58 | + |
| 59 | + for (i = 0; i < iters; i++) { |
| 60 | + data->rnd64[13] ^= 247; |
| 61 | + data->rnd64[49] ^= 113; |
| 62 | + CHECK(secp256k1_ellswift_decode(data->ctx, &out, data->rnd64) == 1); |
| 63 | + memcpy(data->rnd64 + 16, &out.data, 32); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +static void bench_ellswift_xdh(void* arg, int iters) { |
| 68 | + int i; |
| 69 | + bench_ellswift_data *data = (bench_ellswift_data*)arg; |
| 70 | + |
| 71 | + for (i = 0; i < iters; i++) { |
| 72 | + data->rnd64[13] ^= 247; |
| 73 | + data->rnd64[49] ^= 113; |
| 74 | + CHECK(secp256k1_ellswift_xdh(data->ctx, data->rnd64 + 16, data->rnd64, data->rnd64, data->rnd64 + 13, NULL, NULL) == 1); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +void run_ellswift_bench(int iters, int argc, char** argv) { |
| 79 | + bench_ellswift_data data; |
| 80 | + int d = argc == 1; |
| 81 | + |
| 82 | + /* create a context with signing capabilities */ |
| 83 | + data.ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN); |
| 84 | + memset(data.rnd64, 11, sizeof(data.rnd64)); |
| 85 | + |
| 86 | + 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); |
| 87 | + 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); |
| 88 | + if (d || have_flag(argc, argv, "ellswift") || have_flag(argc, argv, "create") || have_flag(argc, argv, "ellswift_create")) run_benchmark("ellswift_create", bench_ellswift_create, bench_ellswift_setup, NULL, &data, 10, iters); |
| 89 | + if (d || have_flag(argc, argv, "ellswift") || have_flag(argc, argv, "xdh") || have_flag(argc, argv, "ellswift_xdh")) run_benchmark("ellswift_xdh", bench_ellswift_xdh, bench_ellswift_setup, NULL, &data, 10, iters); |
| 90 | + |
| 91 | + secp256k1_context_destroy(data.ctx); |
| 92 | +} |
| 93 | + |
| 94 | +#endif /* SECP256K1_MODULE_ellswift_BENCH_H */ |
0 commit comments