Skip to content

Commit 19dd205

Browse files
selftest: Expose in public API
1 parent da62d01 commit 19dd205

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

doc/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Each change falls into one of the following categories: Added, Changed, Deprecat
1010
- Deprecated context flags `SECP256K1_CONTEXT_VERIFY` and `SECP256K1_CONTEXT_NONE`. Use `SECP256K1_CONTEXT_SIGN` instead.
1111
- Renamed `secp256k1_context_no_precomp` to `secp256k1_context_static`.
1212

13+
### Added
14+
- Added `secp256k1_selftest`, to be used in conjunction with `secp256k1_context_static`.
15+
1316
## [MAJOR.MINOR.PATCH] - YYYY-MM-DD
1417

1518
### Added/Changed/Deprecated/Removed/Fixed/Security

include/secp256k1.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,19 +214,41 @@ typedef int (*secp256k1_nonce_function)(
214214
#define SECP256K1_TAG_PUBKEY_HYBRID_EVEN 0x06
215215
#define SECP256K1_TAG_PUBKEY_HYBRID_ODD 0x07
216216

217-
/** A built-in constant secp256k1 context object with static storage duration.
217+
/** A built-in constant secp256k1 context object with static storage duration, to be
218+
* used in conjunction with secp256k1_selftest.
218219
*
219220
* This context object is *not* "initialized for signing", i.e., it cannot be used
220221
* for certain functions that operate on secret keys, e.g., signing and public key
221222
* generation. See secp256k1_context_create if you need a context object initialized
222223
* for signing.
224+
*
225+
* It is highly recommended to call secp256k1_selftest before using this context.
223226
*/
224227
SECP256K1_API extern const secp256k1_context *secp256k1_context_static;
225228

226229
/** Deprecated alias for secp256k1_context_no_precomp. */
227230
SECP256K1_API extern const secp256k1_context *secp256k1_context_no_precomp
228231
SECP256K1_DEPRECATED("Use secp256k1_context_static instead");
229232

233+
/** Perform basic self tests (to be used in conjunction with secp256k1_context_static)
234+
*
235+
* This function performs self tests that detect some serious usage errors and
236+
* similar conditions, e.g., when the library is compiled for the wrong endianness.
237+
* This is a last resort measure to be used in production. The performed tests are
238+
* very rudimentary and are not intended as a replacement for running the test
239+
* binaries.
240+
*
241+
* It is highly recommended to call this before using secp256k1_context_static.
242+
* It is not necessary to call this function before using a context created with
243+
* secp256k1_context_create (or secp256k1_context_preallocated_create), which will
244+
* take care of performing the self tests.
245+
*
246+
* If the tests fail, this function will call the default error handler to abort the
247+
* program (see secp256k1_context_set_error_callback).
248+
*/
249+
SECP256K1_API void secp256k1_selftest(void);
250+
251+
230252
/** Create a secp256k1 context object (in dynamically allocated memory).
231253
*
232254
* This function uses malloc to allocate memory. It is guaranteed that malloc is

src/secp256k1.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ static const secp256k1_context secp256k1_context_static_ = {
7171
const secp256k1_context *secp256k1_context_static = &secp256k1_context_static_;
7272
const secp256k1_context *secp256k1_context_no_precomp = &secp256k1_context_static_;
7373

74+
void secp256k1_selftest(void) {
75+
if (!secp256k1_selftest_passes()) {
76+
secp256k1_callback_call(&default_error_callback, "self test failed");
77+
}
78+
}
79+
7480
size_t secp256k1_context_preallocated_size(unsigned int flags) {
7581
size_t ret = sizeof(secp256k1_context);
7682
/* A return value of 0 is reserved as an indicator for errors when we call this function internally. */
@@ -95,9 +101,7 @@ secp256k1_context* secp256k1_context_preallocated_create(void* prealloc, unsigne
95101
size_t prealloc_size;
96102
secp256k1_context* ret;
97103

98-
if (!secp256k1_selftest_passes()) {
99-
secp256k1_callback_call(&default_error_callback, "self test failed");
100-
}
104+
secp256k1_selftest();
101105

102106
prealloc_size = secp256k1_context_preallocated_size(flags);
103107
if (prealloc_size == 0) {

src/tests.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ void random_scalar_order_b32(unsigned char *b32) {
140140
secp256k1_scalar_get_b32(b32, &num);
141141
}
142142

143+
void run_selftest_tests(void) {
144+
/* Test public API */
145+
secp256k1_selftest();
146+
}
147+
143148
void run_context_tests(int use_prealloc) {
144149
secp256k1_pubkey pubkey;
145150
secp256k1_pubkey zero_pubkey;
@@ -7083,6 +7088,7 @@ int main(int argc, char **argv) {
70837088
secp256k1_testrand_init(argc > 2 ? argv[2] : NULL);
70847089

70857090
/* initialize */
7091+
run_selftest_tests();
70867092
run_context_tests(0);
70877093
run_context_tests(1);
70887094
run_scratch_tests();

0 commit comments

Comments
 (0)