2014-11-15 15:28:10 +00:00
|
|
|
/**********************************************************************
|
2015-08-27 03:42:57 +02:00
|
|
|
* Copyright (c) 2014-2015 Pieter Wuille *
|
2014-11-15 15:28:10 +00:00
|
|
|
* Distributed under the MIT software license, see the accompanying *
|
|
|
|
* file COPYING or http://www.opensource.org/licenses/mit-license.php.*
|
|
|
|
**********************************************************************/
|
2013-05-05 00:21:03 +02:00
|
|
|
|
2014-09-29 08:20:07 +02:00
|
|
|
#include "include/secp256k1.h"
|
2015-08-27 03:42:57 +02:00
|
|
|
#include "include/secp256k1_recovery.h"
|
2014-10-31 03:15:25 -07:00
|
|
|
#include "util.h"
|
2014-12-04 20:26:54 +01:00
|
|
|
#include "bench.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
|
|
|
|
2014-12-04 20:26:54 +01:00
|
|
|
void bench_recover(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;
|
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
|
|
|
|
|
|
|
for (i = 0; i < 20000; i++) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
int main(void) {
|
2017-12-18 18:22:09 -08:00
|
|
|
bench_recover_data data;
|
2015-02-03 17:27:00 -08:00
|
|
|
|
|
|
|
data.ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY);
|
2014-10-31 03:15:25 -07:00
|
|
|
|
2015-01-31 16:12:10 -04:00
|
|
|
run_benchmark("ecdsa_recover", bench_recover, bench_recover_setup, NULL, &data, 10, 20000);
|
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
|
|
|
return 0;
|
|
|
|
}
|