]>
Commit | Line | Data |
---|---|---|
6558a267 PW |
1 | /********************************************************************** |
2 | * Copyright (c) 2014 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 | **********************************************************************/ | |
6 | ||
7 | #include <stdio.h> | |
8 | #include <string.h> | |
9 | ||
10 | #include "include/secp256k1.h" | |
11 | #include "util.h" | |
12 | #include "bench.h" | |
13 | ||
03d84a42 PW |
14 | #ifdef ENABLE_OPENSSL_TESTS |
15 | #include <openssl/bn.h> | |
16 | #include <openssl/ecdsa.h> | |
17 | #include <openssl/obj_mac.h> | |
18 | #endif | |
19 | ||
ca4906b0 | 20 | |
6558a267 | 21 | typedef struct { |
dd891e0e | 22 | secp256k1_context *ctx; |
6558a267 PW |
23 | unsigned char msg[32]; |
24 | unsigned char key[32]; | |
6558a267 | 25 | unsigned char sig[72]; |
788038d3 | 26 | size_t siglen; |
6558a267 | 27 | unsigned char pubkey[33]; |
788038d3 | 28 | size_t pubkeylen; |
03d84a42 PW |
29 | #ifdef ENABLE_OPENSSL_TESTS |
30 | EC_GROUP* ec_group; | |
31 | #endif | |
6558a267 PW |
32 | } benchmark_verify_t; |
33 | ||
ca4906b0 | 34 | static void benchmark_verify(void* arg, int iters) { |
f735446c | 35 | int i; |
6558a267 PW |
36 | benchmark_verify_t* data = (benchmark_verify_t*)arg; |
37 | ||
ca4906b0 | 38 | for (i = 0; i < iters; i++) { |
dd891e0e PW |
39 | secp256k1_pubkey pubkey; |
40 | secp256k1_ecdsa_signature sig; | |
6558a267 PW |
41 | data->sig[data->siglen - 1] ^= (i & 0xFF); |
42 | data->sig[data->siglen - 2] ^= ((i >> 8) & 0xFF); | |
43 | data->sig[data->siglen - 3] ^= ((i >> 16) & 0xFF); | |
23cfa914 | 44 | CHECK(secp256k1_ec_pubkey_parse(data->ctx, &pubkey, data->pubkey, data->pubkeylen) == 1); |
74a2acdb | 45 | CHECK(secp256k1_ecdsa_signature_parse_der(data->ctx, &sig, data->sig, data->siglen) == 1); |
dc0ce9fc | 46 | CHECK(secp256k1_ecdsa_verify(data->ctx, &sig, data->msg, &pubkey) == (i == 0)); |
6558a267 PW |
47 | data->sig[data->siglen - 1] ^= (i & 0xFF); |
48 | data->sig[data->siglen - 2] ^= ((i >> 8) & 0xFF); | |
49 | data->sig[data->siglen - 3] ^= ((i >> 16) & 0xFF); | |
50 | } | |
51 | } | |
52 | ||
03d84a42 | 53 | #ifdef ENABLE_OPENSSL_TESTS |
ca4906b0 | 54 | static void benchmark_verify_openssl(void* arg, int iters) { |
03d84a42 PW |
55 | int i; |
56 | benchmark_verify_t* data = (benchmark_verify_t*)arg; | |
57 | ||
ca4906b0 | 58 | for (i = 0; i < iters; i++) { |
03d84a42 PW |
59 | data->sig[data->siglen - 1] ^= (i & 0xFF); |
60 | data->sig[data->siglen - 2] ^= ((i >> 8) & 0xFF); | |
61 | data->sig[data->siglen - 3] ^= ((i >> 16) & 0xFF); | |
62 | { | |
63 | EC_KEY *pkey = EC_KEY_new(); | |
64 | const unsigned char *pubkey = &data->pubkey[0]; | |
65 | int result; | |
66 | ||
67 | CHECK(pkey != NULL); | |
68 | result = EC_KEY_set_group(pkey, data->ec_group); | |
69 | CHECK(result); | |
70 | result = (o2i_ECPublicKey(&pkey, &pubkey, data->pubkeylen)) != NULL; | |
71 | CHECK(result); | |
72 | result = ECDSA_verify(0, &data->msg[0], sizeof(data->msg), &data->sig[0], data->siglen, pkey) == (i == 0); | |
73 | CHECK(result); | |
74 | EC_KEY_free(pkey); | |
75 | } | |
76 | data->sig[data->siglen - 1] ^= (i & 0xFF); | |
77 | data->sig[data->siglen - 2] ^= ((i >> 8) & 0xFF); | |
78 | data->sig[data->siglen - 3] ^= ((i >> 16) & 0xFF); | |
79 | } | |
80 | } | |
81 | #endif | |
82 | ||
6558a267 | 83 | int main(void) { |
f735446c | 84 | int i; |
dd891e0e PW |
85 | secp256k1_pubkey pubkey; |
86 | secp256k1_ecdsa_signature sig; | |
6558a267 PW |
87 | benchmark_verify_t data; |
88 | ||
ca4906b0 ET |
89 | int iters = get_iters(20000); |
90 | ||
a9b6595e | 91 | data.ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY); |
f735446c | 92 | |
912f203f GM |
93 | for (i = 0; i < 32; i++) { |
94 | data.msg[i] = 1 + i; | |
95 | } | |
96 | for (i = 0; i < 32; i++) { | |
97 | data.key[i] = 33 + i; | |
98 | } | |
6558a267 | 99 | data.siglen = 72; |
dc0ce9fc | 100 | CHECK(secp256k1_ecdsa_sign(data.ctx, &sig, data.msg, data.key, NULL, NULL)); |
74a2acdb | 101 | CHECK(secp256k1_ecdsa_signature_serialize_der(data.ctx, data.sig, &data.siglen, &sig)); |
23cfa914 | 102 | CHECK(secp256k1_ec_pubkey_create(data.ctx, &pubkey, data.key)); |
06aeea55 | 103 | data.pubkeylen = 33; |
486b9bb8 | 104 | CHECK(secp256k1_ec_pubkey_serialize(data.ctx, data.pubkey, &data.pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED) == 1); |
6558a267 | 105 | |
ca4906b0 | 106 | run_benchmark("ecdsa_verify", benchmark_verify, NULL, NULL, &data, 10, iters); |
03d84a42 PW |
107 | #ifdef ENABLE_OPENSSL_TESTS |
108 | data.ec_group = EC_GROUP_new_by_curve_name(NID_secp256k1); | |
ca4906b0 | 109 | run_benchmark("ecdsa_verify_openssl", benchmark_verify_openssl, NULL, NULL, &data, 10, iters); |
03d84a42 PW |
110 | EC_GROUP_free(data.ec_group); |
111 | #endif | |
6558a267 | 112 | |
a9b6595e | 113 | secp256k1_context_destroy(data.ctx); |
6558a267 PW |
114 | return 0; |
115 | } |