5
5
***********************************************************************/
6
6
7
7
#include <valgrind/memcheck.h>
8
+ #include <stdio.h>
9
+
8
10
#include "include/secp256k1.h"
9
11
#include "assumptions.h"
10
12
#include "util.h"
25
27
#include "include/secp256k1_schnorrsig.h"
26
28
#endif
27
29
30
+ void run_tests (secp256k1_context * ctx , unsigned char * key );
31
+
28
32
int main (void ) {
29
33
secp256k1_context * ctx ;
34
+ unsigned char key [32 ];
35
+ int ret , i ;
36
+
37
+ if (!RUNNING_ON_VALGRIND ) {
38
+ fprintf (stderr , "This test can only usefully be run inside valgrind.\n" );
39
+ fprintf (stderr , "Usage: libtool --mode=execute valgrind ./valgrind_ctime_test\n" );
40
+ return 1 ;
41
+ }
42
+ ctx = secp256k1_context_create (SECP256K1_CONTEXT_SIGN
43
+ | SECP256K1_CONTEXT_VERIFY
44
+ | SECP256K1_CONTEXT_DECLASSIFY );
45
+ /** In theory, testing with a single secret input should be sufficient:
46
+ * If control flow depended on secrets the tool would generate an error.
47
+ */
48
+ for (i = 0 ; i < 32 ; i ++ ) {
49
+ key [i ] = i + 65 ;
50
+ }
51
+
52
+ run_tests (ctx , key );
53
+
54
+ /* Test context randomisation. Do this last because it leaves the context
55
+ * tainted. */
56
+ VALGRIND_MAKE_MEM_UNDEFINED (key , 32 );
57
+ ret = secp256k1_context_randomize (ctx , key );
58
+ VALGRIND_MAKE_MEM_DEFINED (& ret , sizeof (ret ));
59
+ CHECK (ret );
60
+
61
+ secp256k1_context_destroy (ctx );
62
+ return 0 ;
63
+ }
64
+
65
+ void run_tests (secp256k1_context * ctx , unsigned char * key ) {
30
66
secp256k1_ecdsa_signature signature ;
31
67
secp256k1_pubkey pubkey ;
32
68
size_t siglen = 74 ;
33
69
size_t outputlen = 33 ;
34
70
int i ;
35
71
int ret ;
36
72
unsigned char msg [32 ];
37
- unsigned char key [32 ];
38
73
unsigned char sig [74 ];
39
74
unsigned char spubkey [33 ];
40
75
#ifdef ENABLE_MODULE_RECOVERY
@@ -45,26 +80,10 @@ int main(void) {
45
80
secp256k1_keypair keypair ;
46
81
#endif
47
82
48
- if (!RUNNING_ON_VALGRIND ) {
49
- fprintf (stderr , "This test can only usefully be run inside valgrind.\n" );
50
- fprintf (stderr , "Usage: libtool --mode=execute valgrind ./valgrind_ctime_test\n" );
51
- exit (1 );
52
- }
53
-
54
- /** In theory, testing with a single secret input should be sufficient:
55
- * If control flow depended on secrets the tool would generate an error.
56
- */
57
- for (i = 0 ; i < 32 ; i ++ ) {
58
- key [i ] = i + 65 ;
59
- }
60
83
for (i = 0 ; i < 32 ; i ++ ) {
61
84
msg [i ] = i + 1 ;
62
85
}
63
86
64
- ctx = secp256k1_context_create (SECP256K1_CONTEXT_SIGN
65
- | SECP256K1_CONTEXT_VERIFY
66
- | SECP256K1_CONTEXT_DECLASSIFY );
67
-
68
87
/* Test keygen. */
69
88
VALGRIND_MAKE_MEM_UNDEFINED (key , 32 );
70
89
ret = secp256k1_ec_pubkey_create (ctx , & pubkey , key );
@@ -122,12 +141,6 @@ int main(void) {
122
141
VALGRIND_MAKE_MEM_DEFINED (& ret , sizeof (ret ));
123
142
CHECK (ret == 1 );
124
143
125
- /* Test context randomisation. Do this last because it leaves the context tainted. */
126
- VALGRIND_MAKE_MEM_UNDEFINED (key , 32 );
127
- ret = secp256k1_context_randomize (ctx , key );
128
- VALGRIND_MAKE_MEM_DEFINED (& ret , sizeof (ret ));
129
- CHECK (ret );
130
-
131
144
/* Test keypair_create and keypair_xonly_tweak_add. */
132
145
#ifdef ENABLE_MODULE_EXTRAKEYS
133
146
VALGRIND_MAKE_MEM_UNDEFINED (key , 32 );
@@ -157,7 +170,4 @@ int main(void) {
157
170
VALGRIND_MAKE_MEM_DEFINED (& ret , sizeof (ret ));
158
171
CHECK (ret == 1 );
159
172
#endif
160
-
161
- secp256k1_context_destroy (ctx );
162
- return 0 ;
163
173
}
0 commit comments