rust-secp256k1-unsafe-fast/secp256k1-sys/depend/secp256k1/src/bench_schnorrsig.c

103 lines
3.7 KiB
C
Raw Normal View History

/***********************************************************************
* Copyright (c) 2018-2020 Andrew Poelstra, Jonas Nick *
* Distributed under the MIT software license, see the accompanying *
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
***********************************************************************/
2020-09-15 01:39:26 +00:00
#include <string.h>
#include <stdlib.h>
#include "include/secp256k1.h"
#include "include/secp256k1_schnorrsig.h"
#include "util.h"
#include "bench.h"
typedef struct {
rustsecp256k1_v0_4_0_context *ctx;
2020-09-15 01:39:26 +00:00
int n;
const rustsecp256k1_v0_4_0_keypair **keypairs;
2020-09-15 01:39:26 +00:00
const unsigned char **pk;
const unsigned char **sigs;
const unsigned char **msgs;
} bench_schnorrsig_data;
void bench_schnorrsig_sign(void* arg, int iters) {
bench_schnorrsig_data *data = (bench_schnorrsig_data *)arg;
int i;
unsigned char msg[32] = "benchmarkexamplemessagetemplate";
unsigned char sig[64];
for (i = 0; i < iters; i++) {
msg[0] = i;
msg[1] = i >> 8;
CHECK(rustsecp256k1_v0_4_0_schnorrsig_sign(data->ctx, sig, msg, data->keypairs[i], NULL, NULL));
2020-09-15 01:39:26 +00:00
}
}
void bench_schnorrsig_verify(void* arg, int iters) {
bench_schnorrsig_data *data = (bench_schnorrsig_data *)arg;
int i;
for (i = 0; i < iters; i++) {
rustsecp256k1_v0_4_0_xonly_pubkey pk;
CHECK(rustsecp256k1_v0_4_0_xonly_pubkey_parse(data->ctx, &pk, data->pk[i]) == 1);
CHECK(rustsecp256k1_v0_4_0_schnorrsig_verify(data->ctx, data->sigs[i], data->msgs[i], &pk));
2020-09-15 01:39:26 +00:00
}
}
int main(void) {
int i;
bench_schnorrsig_data data;
int iters = get_iters(10000);
data.ctx = rustsecp256k1_v0_4_0_context_create(SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN);
data.keypairs = (const rustsecp256k1_v0_4_0_keypair **)malloc(iters * sizeof(rustsecp256k1_v0_4_0_keypair *));
2020-09-15 01:39:26 +00:00
data.pk = (const unsigned char **)malloc(iters * sizeof(unsigned char *));
data.msgs = (const unsigned char **)malloc(iters * sizeof(unsigned char *));
data.sigs = (const unsigned char **)malloc(iters * sizeof(unsigned char *));
for (i = 0; i < iters; i++) {
unsigned char sk[32];
unsigned char *msg = (unsigned char *)malloc(32);
unsigned char *sig = (unsigned char *)malloc(64);
rustsecp256k1_v0_4_0_keypair *keypair = (rustsecp256k1_v0_4_0_keypair *)malloc(sizeof(*keypair));
2020-09-15 01:39:26 +00:00
unsigned char *pk_char = (unsigned char *)malloc(32);
rustsecp256k1_v0_4_0_xonly_pubkey pk;
2020-09-15 01:39:26 +00:00
msg[0] = sk[0] = i;
msg[1] = sk[1] = i >> 8;
msg[2] = sk[2] = i >> 16;
msg[3] = sk[3] = i >> 24;
memset(&msg[4], 'm', 28);
memset(&sk[4], 's', 28);
data.keypairs[i] = keypair;
data.pk[i] = pk_char;
data.msgs[i] = msg;
data.sigs[i] = sig;
CHECK(rustsecp256k1_v0_4_0_keypair_create(data.ctx, keypair, sk));
CHECK(rustsecp256k1_v0_4_0_schnorrsig_sign(data.ctx, sig, msg, keypair, NULL, NULL));
CHECK(rustsecp256k1_v0_4_0_keypair_xonly_pub(data.ctx, &pk, NULL, keypair));
CHECK(rustsecp256k1_v0_4_0_xonly_pubkey_serialize(data.ctx, pk_char, &pk) == 1);
2020-09-15 01:39:26 +00:00
}
run_benchmark("schnorrsig_sign", bench_schnorrsig_sign, NULL, NULL, (void *) &data, 10, iters);
run_benchmark("schnorrsig_verify", bench_schnorrsig_verify, NULL, NULL, (void *) &data, 10, iters);
for (i = 0; i < iters; i++) {
free((void *)data.keypairs[i]);
free((void *)data.pk[i]);
free((void *)data.msgs[i]);
free((void *)data.sigs[i]);
}
free(data.keypairs);
free(data.pk);
free(data.msgs);
free(data.sigs);
rustsecp256k1_v0_4_0_context_destroy(data.ctx);
2020-09-15 01:39:26 +00:00
return 0;
}