Squashed 'src/secp256k1/' changes from 84973d393..0b7024185
[bitcoinplatinum.git] / src / bench_recover.c
blob6489378cc64ab46f79c0c35f556625ab46f5b195
1 /**********************************************************************
2 * Copyright (c) 2014-2015 Pieter Wuille *
3 * Distributed under the MIT software license, see the accompanying *
4 * file COPYING or http://www.opensource.org/licenses/mit-license.php.*
5 **********************************************************************/
7 #include "include/secp256k1.h"
8 #include "include/secp256k1_recovery.h"
9 #include "util.h"
10 #include "bench.h"
12 typedef struct {
13 secp256k1_context *ctx;
14 unsigned char msg[32];
15 unsigned char sig[64];
16 } bench_recover_t;
18 void bench_recover(void* arg) {
19 int i;
20 bench_recover_t *data = (bench_recover_t*)arg;
21 secp256k1_pubkey pubkey;
22 unsigned char pubkeyc[33];
24 for (i = 0; i < 20000; i++) {
25 int j;
26 size_t pubkeylen = 33;
27 secp256k1_ecdsa_recoverable_signature sig;
28 CHECK(secp256k1_ecdsa_recoverable_signature_parse_compact(data->ctx, &sig, data->sig, i % 2));
29 CHECK(secp256k1_ecdsa_recover(data->ctx, &pubkey, &sig, data->msg));
30 CHECK(secp256k1_ec_pubkey_serialize(data->ctx, pubkeyc, &pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED));
31 for (j = 0; j < 32; j++) {
32 data->sig[j + 32] = data->msg[j]; /* Move former message to S. */
33 data->msg[j] = data->sig[j]; /* Move former R to message. */
34 data->sig[j] = pubkeyc[j + 1]; /* Move recovered pubkey X coordinate to R (which must be a valid X coordinate). */
39 void bench_recover_setup(void* arg) {
40 int i;
41 bench_recover_t *data = (bench_recover_t*)arg;
43 for (i = 0; i < 32; i++) {
44 data->msg[i] = 1 + i;
46 for (i = 0; i < 64; i++) {
47 data->sig[i] = 65 + i;
51 int main(void) {
52 bench_recover_t data;
54 data.ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY);
56 run_benchmark("ecdsa_recover", bench_recover, bench_recover_setup, NULL, &data, 10, 20000);
58 secp256k1_context_destroy(data.ctx);
59 return 0;