11
11
12
12
typedef struct {
13
13
secp256k1_context * ctx ;
14
- secp256k1_pubkey point ;
14
+ unsigned char point [ 33 ] ;
15
15
unsigned char scalar [32 ];
16
16
} bench_ecdh_data ;
17
17
18
+
19
+ /* Outputs a hash of the coordinates, but also updates data->point with the coordinates. */
20
+ static int ecdh_hash_function_bench (unsigned char * output , const unsigned char * x32 , const unsigned char * y32 , void * arg ) {
21
+ bench_ecdh_data * data = arg ;
22
+ int ret = secp256k1_ecdh_hash_function_sha256 (output , x32 , y32 , NULL );
23
+ data -> point [0 ] ^= y32 [17 ] & 1 ;
24
+ memcpy (data -> point + 1 , x32 , 32 );
25
+ return ret ;
26
+ }
27
+
28
+ static int ecdh_xonly_hash_function_bench (unsigned char * output , const unsigned char * x32 , void * arg ) {
29
+ bench_ecdh_data * data = arg ;
30
+ int ret = secp256k1_ecdh_xonly_hash_function_sha256 (output , x32 , NULL );
31
+ memcpy (data -> point + 1 , x32 , 32 );
32
+ return ret ;
33
+ }
34
+
18
35
static void bench_ecdh_setup (void * arg ) {
19
36
int i ;
20
37
bench_ecdh_data * data = (bench_ecdh_data * )arg ;
21
- const unsigned char point [] = {
38
+ static const unsigned char point [33 ] = {
22
39
0x03 ,
23
40
0x54 , 0x94 , 0xc1 , 0x5d , 0x32 , 0x09 , 0x97 , 0x06 ,
24
41
0xc2 , 0x39 , 0x5f , 0x94 , 0x34 , 0x87 , 0x45 , 0xfd ,
@@ -29,16 +46,33 @@ static void bench_ecdh_setup(void* arg) {
29
46
for (i = 0 ; i < 32 ; i ++ ) {
30
47
data -> scalar [i ] = i + 1 ;
31
48
}
32
- CHECK ( secp256k1_ec_pubkey_parse ( data -> ctx , & data -> point , point , sizeof (point )) == 1 );
49
+ memcpy ( data -> point , point , sizeof (point ));
33
50
}
34
51
35
52
static void bench_ecdh (void * arg , int iters ) {
36
53
int i ;
37
- unsigned char res [32 ];
38
54
bench_ecdh_data * data = (bench_ecdh_data * )arg ;
39
55
40
56
for (i = 0 ; i < iters ; i ++ ) {
41
- CHECK (secp256k1_ecdh (data -> ctx , res , & data -> point , data -> scalar , NULL , NULL ) == 1 );
57
+ /* Compute point multiplication of data->point with data->scalar, and then update:
58
+ * - data->scalar to be the computed shared secret (hash of point multiplication output)
59
+ * - data->point to have X coordinate equal to X coordinate of point multiplication output,
60
+ * and optionally flipped Y coordinate. */
61
+ secp256k1_pubkey pubkey ;
62
+ CHECK (secp256k1_ec_pubkey_parse (data -> ctx , & pubkey , data -> point , sizeof (data -> point )) == 1 );
63
+ CHECK (secp256k1_ecdh (data -> ctx , data -> scalar , & pubkey , data -> scalar , & ecdh_hash_function_bench , arg ) == 1 );
64
+ }
65
+ }
66
+
67
+ static void bench_ecdh_xonly (void * arg , int iters ) {
68
+ int i ;
69
+ bench_ecdh_data * data = (bench_ecdh_data * )arg ;
70
+
71
+ for (i = 0 ; i < iters ; i ++ ) {
72
+ /* Compute X-only point multiplication of data->point with data->scalar, and then update:
73
+ * - data->scalar to be the computed shared secret (hash of point multiplication output X coordinate)
74
+ * - data->point to have X coordinate equal to X coordinate of point multiplication output. */
75
+ CHECK (secp256k1_ecdh_xonly (data -> ctx , data -> scalar , data -> point + 1 , data -> scalar , & ecdh_xonly_hash_function_bench , arg ) == 1 );
42
76
}
43
77
}
44
78
@@ -50,6 +84,7 @@ static void run_ecdh_bench(int iters, int argc, char** argv) {
50
84
data .ctx = secp256k1_context_create (SECP256K1_FLAGS_TYPE_CONTEXT );
51
85
52
86
if (d || have_flag (argc , argv , "ecdh" )) run_benchmark ("ecdh" , bench_ecdh , bench_ecdh_setup , NULL , & data , 10 , iters );
87
+ if (d || have_flag (argc , argv , "ecdh" ) || have_flag (argc , argv , "ecdh_xonly" )) run_benchmark ("ecdh_xonly" , bench_ecdh_xonly , bench_ecdh_setup , NULL , & data , 10 , iters );
53
88
54
89
secp256k1_context_destroy (data .ctx );
55
90
}
0 commit comments