Skip to content

Commit e02d686

Browse files
selftest: Expose in public API
1 parent e383fbf commit e02d686

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

doc/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Each change falls into one of the following categories: Added, Changed, Deprecat
1313
- Deprecated context flags `SECP256K1_CONTEXT_VERIFY` and `SECP256K1_CONTEXT_SIGN`. Use `SECP256K1_CONTEXT_NONE` instead.
1414
- Renamed `secp256k1_context_no_precomp` to `secp256k1_context_static`.
1515

16+
### Added
17+
- Added `secp256k1_selftest`, to be used in conjunction with `secp256k1_context_static`.
18+
1619
## [MAJOR.MINOR.PATCH] - YYYY-MM-DD
1720

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

include/secp256k1.h

+23-1
Original file line numberDiff line numberDiff line change
@@ -214,20 +214,42 @@ 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 offers *only limited functionality* , i.e., it cannot be used
220221
* for API functions that perform computations involving secret keys, e.g., signing
221222
* and public key generation. If this restriction applies to a specific API function,
222223
* it is mentioned in its documentation. See secp256k1_context_create if you need a
223224
* full context object that supports all functionality offered by the library.
225+
*
226+
* It is highly recommended to call secp256k1_selftest before using this context.
224227
*/
225228
SECP256K1_API extern const secp256k1_context *secp256k1_context_static;
226229

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

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

src/secp256k1.c

+7-3
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ static const secp256k1_context secp256k1_context_static_ = {
7373
const secp256k1_context *secp256k1_context_static = &secp256k1_context_static_;
7474
const secp256k1_context *secp256k1_context_no_precomp = &secp256k1_context_static_;
7575

76+
void secp256k1_selftest(void) {
77+
if (!secp256k1_selftest_passes()) {
78+
secp256k1_callback_call(&default_error_callback, "self test failed");
79+
}
80+
}
81+
7682
size_t secp256k1_context_preallocated_size(unsigned int flags) {
7783
size_t ret = sizeof(secp256k1_context);
7884
/* A return value of 0 is reserved as an indicator for errors when we call this function internally. */
@@ -97,9 +103,7 @@ secp256k1_context* secp256k1_context_preallocated_create(void* prealloc, unsigne
97103
size_t prealloc_size;
98104
secp256k1_context* ret;
99105

100-
if (!secp256k1_selftest_passes()) {
101-
secp256k1_callback_call(&default_error_callback, "self test failed");
102-
}
106+
secp256k1_selftest();
103107

104108
prealloc_size = secp256k1_context_preallocated_size(flags);
105109
if (prealloc_size == 0) {

src/tests.c

+6
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ void random_scalar_order_b32(unsigned char *b32) {
141141
secp256k1_scalar_get_b32(b32, &num);
142142
}
143143

144+
void run_selftest_tests(void) {
145+
/* Test public API */
146+
secp256k1_selftest();
147+
}
148+
144149
void run_context_tests(int use_prealloc) {
145150
secp256k1_pubkey pubkey;
146151
secp256k1_pubkey zero_pubkey;
@@ -7388,6 +7393,7 @@ int main(int argc, char **argv) {
73887393
secp256k1_testrand_init(argc > 2 ? argv[2] : NULL);
73897394

73907395
/* initialize */
7396+
run_selftest_tests();
73917397
run_context_tests(0);
73927398
run_context_tests(1);
73937399
run_scratch_tests();

0 commit comments

Comments
 (0)