|
| 1 | +/*********************************************************************** |
| 2 | + * Copyright (c) 2021 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 | +#include <string.h> |
| 8 | + |
| 9 | +#include "../include/secp256k1.h" |
| 10 | +#include "../include/secp256k1_ellsq.h" |
| 11 | +#include "util.h" |
| 12 | +#include "bench.h" |
| 13 | + |
| 14 | +typedef struct { |
| 15 | + secp256k1_context *ctx; |
| 16 | + secp256k1_pubkey point; |
| 17 | + unsigned char rnd64[64]; |
| 18 | +} bench_ellsq_data; |
| 19 | + |
| 20 | +static void bench_ellsq_setup(void* arg) { |
| 21 | + bench_ellsq_data *data = (bench_ellsq_data*)arg; |
| 22 | + const unsigned char point[] = { |
| 23 | + 0x03, |
| 24 | + 0x54, 0x94, 0xc1, 0x5d, 0x32, 0x09, 0x97, 0x06, |
| 25 | + 0xc2, 0x37, 0x5f, 0x94, 0x34, 0x87, 0x45, 0xfd, |
| 26 | + 0x75, 0x7c, 0xe3, 0x0e, 0x4e, 0x8c, 0x90, 0xfb, |
| 27 | + 0xa2, 0xba, 0xd1, 0x84, 0xf8, 0x83, 0xc6, 0x9f |
| 28 | + }; |
| 29 | + CHECK(secp256k1_ec_pubkey_parse(data->ctx, &data->point, point, sizeof(point)) == 1); |
| 30 | +} |
| 31 | + |
| 32 | +static void bench_ellsq_encode(void* arg, int iters) { |
| 33 | + int i; |
| 34 | + bench_ellsq_data *data = (bench_ellsq_data*)arg; |
| 35 | + |
| 36 | + for (i = 0; i < iters; i++) { |
| 37 | + data->rnd64[29] ^= 145; |
| 38 | + CHECK(secp256k1_ellsq_encode(data->ctx, data->rnd64, data->rnd64 + 16, &data->point) == 1); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +static void bench_ellsq_decode(void* arg, int iters) { |
| 43 | + int i; |
| 44 | + secp256k1_pubkey out; |
| 45 | + bench_ellsq_data *data = (bench_ellsq_data*)arg; |
| 46 | + |
| 47 | + for (i = 0; i < iters; i++) { |
| 48 | + data->rnd64[13] ^= 247; |
| 49 | + data->rnd64[47] ^= 113; |
| 50 | + CHECK(secp256k1_ellsq_decode(data->ctx, &out, data->rnd64) == 1); |
| 51 | + memcpy(data->rnd64, &out.data, 64); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +int main(void) { |
| 56 | + bench_ellsq_data data; |
| 57 | + |
| 58 | + int iters = get_iters(10000); |
| 59 | + |
| 60 | + /* create a context with no capabilities */ |
| 61 | + data.ctx = secp256k1_context_create(SECP256K1_FLAGS_TYPE_CONTEXT); |
| 62 | + memset(data.rnd64, 11, sizeof(data.rnd64)); |
| 63 | + |
| 64 | + run_benchmark("ellsq_encode", bench_ellsq_encode, bench_ellsq_setup, NULL, &data, 10, iters); |
| 65 | + run_benchmark("ellsq_decode", bench_ellsq_decode, bench_ellsq_setup, NULL, &data, 10, iters); |
| 66 | + |
| 67 | + secp256k1_context_destroy(data.ctx); |
| 68 | + |
| 69 | + return 0; |
| 70 | +} |
0 commit comments