2020-12-17 08:33:49 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* Copyright (c) 2014-2015 Pieter Wuille *
|
|
|
|
* Distributed under the MIT software license, see the accompanying *
|
|
|
|
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
|
|
|
|
***********************************************************************/
|
2013-05-05 00:21:03 +02:00
|
|
|
|
2021-10-17 13:56:59 -04:00
|
|
|
#ifndef SECP256K1_MODULE_RECOVERY_BENCH_H
|
|
|
|
#define SECP256K1_MODULE_RECOVERY_BENCH_H
|
|
|
|
|
2022-06-30 17:16:21 +02:00
|
|
|
#include "../../../include/secp256k1_recovery.h"
|
2013-04-20 23:34:41 +02:00
|
|
|
|
2014-12-04 20:26:54 +01:00
|
|
|
typedef struct {
|
2015-09-21 20:57:54 +02:00
|
|
|
secp256k1_context *ctx;
|
2014-10-31 03:15:25 -07:00
|
|
|
unsigned char msg[32];
|
|
|
|
unsigned char sig[64];
|
2017-12-18 18:22:09 -08:00
|
|
|
} bench_recover_data;
|
2014-10-31 03:15:25 -07:00
|
|
|
|
2020-03-04 15:13:35 +02:00
|
|
|
void bench_recover(void* arg, int iters) {
|
2015-01-25 17:32:08 +00:00
|
|
|
int i;
|
2017-12-18 18:22:09 -08:00
|
|
|
bench_recover_data *data = (bench_recover_data*)arg;
|
2015-09-21 20:57:54 +02:00
|
|
|
secp256k1_pubkey pubkey;
|
2015-07-20 13:36:55 -04:00
|
|
|
unsigned char pubkeyc[33];
|
2015-01-25 17:32:08 +00:00
|
|
|
|
2020-03-04 15:13:35 +02:00
|
|
|
for (i = 0; i < iters; i++) {
|
2015-01-25 17:32:08 +00:00
|
|
|
int j;
|
2015-09-01 04:35:10 +00:00
|
|
|
size_t pubkeylen = 33;
|
2015-09-21 20:57:54 +02:00
|
|
|
secp256k1_ecdsa_recoverable_signature sig;
|
2015-08-27 03:09:23 +02:00
|
|
|
CHECK(secp256k1_ecdsa_recoverable_signature_parse_compact(data->ctx, &sig, data->sig, i % 2));
|
2015-08-28 01:50:47 +02:00
|
|
|
CHECK(secp256k1_ecdsa_recover(data->ctx, &pubkey, &sig, data->msg));
|
2015-09-01 06:00:35 +00:00
|
|
|
CHECK(secp256k1_ec_pubkey_serialize(data->ctx, pubkeyc, &pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED));
|
2015-01-25 17:32:08 +00:00
|
|
|
for (j = 0; j < 32; j++) {
|
2014-12-04 20:26:54 +01:00
|
|
|
data->sig[j + 32] = data->msg[j]; /* Move former message to S. */
|
|
|
|
data->msg[j] = data->sig[j]; /* Move former R to message. */
|
2015-07-20 13:36:55 -04:00
|
|
|
data->sig[j] = pubkeyc[j + 1]; /* Move recovered pubkey X coordinate to R (which must be a valid X coordinate). */
|
2014-10-31 03:15:25 -07:00
|
|
|
}
|
2014-09-29 08:20:07 +02:00
|
|
|
}
|
2014-12-04 20:26:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void bench_recover_setup(void* arg) {
|
2015-01-25 17:32:08 +00:00
|
|
|
int i;
|
2017-12-18 18:22:09 -08:00
|
|
|
bench_recover_data *data = (bench_recover_data*)arg;
|
2014-12-04 20:26:54 +01:00
|
|
|
|
2015-09-21 17:21:35 +00:00
|
|
|
for (i = 0; i < 32; i++) {
|
|
|
|
data->msg[i] = 1 + i;
|
|
|
|
}
|
|
|
|
for (i = 0; i < 64; i++) {
|
|
|
|
data->sig[i] = 65 + i;
|
|
|
|
}
|
2014-12-04 20:26:54 +01:00
|
|
|
}
|
|
|
|
|
2021-11-05 17:48:18 -04:00
|
|
|
void run_recovery_bench(int iters, int argc, char** argv) {
|
2017-12-18 18:22:09 -08:00
|
|
|
bench_recover_data data;
|
2021-11-05 17:48:18 -04:00
|
|
|
int d = argc == 1;
|
2015-02-03 17:27:00 -08:00
|
|
|
|
2022-11-28 15:46:25 +00:00
|
|
|
data.ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
|
2014-10-31 03:15:25 -07:00
|
|
|
|
2021-11-05 17:48:18 -04:00
|
|
|
if (d || have_flag(argc, argv, "ecdsa") || have_flag(argc, argv, "recover") || have_flag(argc, argv, "ecdsa_recover")) run_benchmark("ecdsa_recover", bench_recover, bench_recover_setup, NULL, &data, 10, iters);
|
2013-03-30 22:32:16 +01:00
|
|
|
|
2015-02-03 17:27:00 -08:00
|
|
|
secp256k1_context_destroy(data.ctx);
|
2013-03-16 15:51:55 +01:00
|
|
|
}
|
2021-10-17 13:56:59 -04:00
|
|
|
|
|
|
|
#endif /* SECP256K1_MODULE_RECOVERY_BENCH_H */
|