Skip to content

Commit 91c0ce9

Browse files
committed
Add benchmarks for ECDH and const-time multiplication
1 parent 0739bbb commit 91c0ce9

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
bench_inv
2+
bench_ecdh
23
bench_sign
34
bench_verify
45
bench_recover

src/bench_ecdh.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**********************************************************************
2+
* Copyright (c) 2015 Pieter Wuille, Andrew Poelstra *
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 <string.h>
8+
9+
#include "include/secp256k1.h"
10+
#include "include/secp256k1_ecdh.h"
11+
#include "util.h"
12+
#include "bench.h"
13+
14+
typedef struct {
15+
secp256k1_context_t *ctx;
16+
secp256k1_pubkey_t point;
17+
unsigned char scalar[32];
18+
} bench_ecdh_t;
19+
20+
static void bench_ecdh_setup(void* arg) {
21+
int i;
22+
bench_ecdh_t *data = (bench_ecdh_t*)arg;
23+
const unsigned char point[] = {
24+
0x03,
25+
0x54, 0x94, 0xc1, 0x5d, 0x32, 0x09, 0x97, 0x06,
26+
0xc2, 0x39, 0x5f, 0x94, 0x34, 0x87, 0x45, 0xfd,
27+
0x75, 0x7c, 0xe3, 0x0e, 0x4e, 0x8c, 0x90, 0xfb,
28+
0xa2, 0xba, 0xd1, 0x84, 0xf8, 0x83, 0xc6, 0x9f
29+
};
30+
31+
data->ctx = secp256k1_context_create(0);
32+
for (i = 0; i < 32; i++) data->scalar[i] = i + 1;
33+
CHECK(secp256k1_ec_pubkey_parse(data->ctx, &data->point, point, sizeof(point)) == 1);
34+
}
35+
36+
static void bench_ecdh(void* arg) {
37+
int i;
38+
unsigned char res[32];
39+
bench_ecdh_t *data = (bench_ecdh_t*)arg;
40+
41+
for (i = 0; i < 20000; i++) {
42+
CHECK(secp256k1_ecdh(data->ctx, res, &data->point, data->scalar) == 1);
43+
}
44+
}
45+
46+
int main(void) {
47+
bench_ecdh_t data;
48+
49+
run_benchmark("ecdh", bench_ecdh, bench_ecdh_setup, NULL, &data, 10, 20000);
50+
return 0;
51+
}

src/bench_internal.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "field_impl.h"
1414
#include "group_impl.h"
1515
#include "scalar_impl.h"
16+
#include "ecmult_const_impl.h"
1617
#include "ecmult_impl.h"
1718
#include "bench.h"
1819

@@ -235,6 +236,16 @@ void bench_ecmult_wnaf(void* arg) {
235236
}
236237
}
237238

239+
void bench_wnaf_const(void* arg) {
240+
int i;
241+
bench_inv_t *data = (bench_inv_t*)arg;
242+
243+
for (i = 0; i < 20000; i++) {
244+
secp256k1_wnaf_const(data->wnaf, &data->scalar_x, WINDOW_A);
245+
secp256k1_scalar_add(&data->scalar_x, &data->scalar_x, &data->scalar_y);
246+
}
247+
}
248+
238249

239250
void bench_sha256(void* arg) {
240251
int i;
@@ -310,6 +321,7 @@ int main(int argc, char **argv) {
310321
if (have_flag(argc, argv, "group") || have_flag(argc, argv, "add")) run_benchmark("group_add_affine", bench_group_add_affine, bench_setup, NULL, &data, 10, 200000);
311322
if (have_flag(argc, argv, "group") || have_flag(argc, argv, "add")) run_benchmark("group_add_affine_var", bench_group_add_affine_var, bench_setup, NULL, &data, 10, 200000);
312323

324+
if (have_flag(argc, argv, "ecmult") || have_flag(argc, argv, "wnaf")) run_benchmark("wnaf_const", bench_wnaf_const, bench_setup, NULL, &data, 10, 20000);
313325
if (have_flag(argc, argv, "ecmult") || have_flag(argc, argv, "wnaf")) run_benchmark("ecmult_wnaf", bench_ecmult_wnaf, bench_setup, NULL, &data, 10, 20000);
314326

315327
if (have_flag(argc, argv, "hash") || have_flag(argc, argv, "sha256")) run_benchmark("hash_sha256", bench_sha256, bench_setup, NULL, &data, 10, 20000);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
include_HEADERS += include/secp256k1_ecdh.h
22
noinst_HEADERS += src/modules/ecdh/main_impl.h
33
noinst_HEADERS += src/modules/ecdh/tests_impl.h
4+
if USE_BENCHMARK
5+
noinst_PROGRAMS += bench_ecdh
6+
bench_ecdh_SOURCES = src/bench_ecdh.c
7+
bench_ecdh_LDADD = libsecp256k1.la $(SECP_LIBS)
8+
bench_ecdh_LDFLAGS = -static
9+
endif

0 commit comments

Comments
 (0)