Benchmark against OpenSSL verification

This commit is contained in:
Pieter Wuille 2015-11-15 15:46:17 +01:00
parent 2bfb82b10e
commit 03d84a427f
2 changed files with 46 additions and 2 deletions

View File

@ -57,9 +57,9 @@ noinst_PROGRAMS =
if USE_BENCHMARK if USE_BENCHMARK
noinst_PROGRAMS += bench_verify bench_sign bench_internal noinst_PROGRAMS += bench_verify bench_sign bench_internal
bench_verify_SOURCES = src/bench_verify.c bench_verify_SOURCES = src/bench_verify.c
bench_verify_LDADD = libsecp256k1.la $(SECP_LIBS) bench_verify_LDADD = libsecp256k1.la $(SECP_LIBS) $(SECP_TEST_LIBS)
bench_sign_SOURCES = src/bench_sign.c bench_sign_SOURCES = src/bench_sign.c
bench_sign_LDADD = libsecp256k1.la $(SECP_LIBS) bench_sign_LDADD = libsecp256k1.la $(SECP_LIBS) $(SECP_TEST_LIBS)
bench_internal_SOURCES = src/bench_internal.c bench_internal_SOURCES = src/bench_internal.c
bench_internal_LDADD = $(SECP_LIBS) bench_internal_LDADD = $(SECP_LIBS)
bench_internal_CPPFLAGS = $(SECP_INCLUDES) bench_internal_CPPFLAGS = $(SECP_INCLUDES)

View File

@ -11,6 +11,12 @@
#include "util.h" #include "util.h"
#include "bench.h" #include "bench.h"
#ifdef ENABLE_OPENSSL_TESTS
#include <openssl/bn.h>
#include <openssl/ecdsa.h>
#include <openssl/obj_mac.h>
#endif
typedef struct { typedef struct {
secp256k1_context *ctx; secp256k1_context *ctx;
unsigned char msg[32]; unsigned char msg[32];
@ -19,6 +25,9 @@ typedef struct {
size_t siglen; size_t siglen;
unsigned char pubkey[33]; unsigned char pubkey[33];
size_t pubkeylen; size_t pubkeylen;
#ifdef ENABLE_OPENSSL_TESTS
EC_GROUP* ec_group;
#endif
} benchmark_verify_t; } benchmark_verify_t;
static void benchmark_verify(void* arg) { static void benchmark_verify(void* arg) {
@ -40,6 +49,36 @@ static void benchmark_verify(void* arg) {
} }
} }
#ifdef ENABLE_OPENSSL_TESTS
static void benchmark_verify_openssl(void* arg) {
int i;
benchmark_verify_t* data = (benchmark_verify_t*)arg;
for (i = 0; i < 20000; i++) {
data->sig[data->siglen - 1] ^= (i & 0xFF);
data->sig[data->siglen - 2] ^= ((i >> 8) & 0xFF);
data->sig[data->siglen - 3] ^= ((i >> 16) & 0xFF);
{
EC_KEY *pkey = EC_KEY_new();
const unsigned char *pubkey = &data->pubkey[0];
int result;
CHECK(pkey != NULL);
result = EC_KEY_set_group(pkey, data->ec_group);
CHECK(result);
result = (o2i_ECPublicKey(&pkey, &pubkey, data->pubkeylen)) != NULL;
CHECK(result);
result = ECDSA_verify(0, &data->msg[0], sizeof(data->msg), &data->sig[0], data->siglen, pkey) == (i == 0);
CHECK(result);
EC_KEY_free(pkey);
}
data->sig[data->siglen - 1] ^= (i & 0xFF);
data->sig[data->siglen - 2] ^= ((i >> 8) & 0xFF);
data->sig[data->siglen - 3] ^= ((i >> 16) & 0xFF);
}
}
#endif
int main(void) { int main(void) {
int i; int i;
secp256k1_pubkey pubkey; secp256k1_pubkey pubkey;
@ -62,6 +101,11 @@ int main(void) {
CHECK(secp256k1_ec_pubkey_serialize(data.ctx, data.pubkey, &data.pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED) == 1); CHECK(secp256k1_ec_pubkey_serialize(data.ctx, data.pubkey, &data.pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED) == 1);
run_benchmark("ecdsa_verify", benchmark_verify, NULL, NULL, &data, 10, 20000); run_benchmark("ecdsa_verify", benchmark_verify, NULL, NULL, &data, 10, 20000);
#ifdef ENABLE_OPENSSL_TESTS
data.ec_group = EC_GROUP_new_by_curve_name(NID_secp256k1);
run_benchmark("ecdsa_verify_openssl", benchmark_verify_openssl, NULL, NULL, &data, 10, 20000);
EC_GROUP_free(data.ec_group);
#endif
secp256k1_context_destroy(data.ctx); secp256k1_context_destroy(data.ctx);
return 0; return 0;