In rust-secp256k1 we’re exploring how we can best support a signing API for systems which potentially have no allocator, may be operating multiple threads, but which have very limited threading primitives (e.g. we have atomics with acquire/release semantics but no stdlib with prepackaged mutexes or other locks).
I think a useful function would be something like
0int secp256k1_with_randomized_context(const unsigned char* seed32, cb callback, void* callback_data) {
1 /* create context object on the stack, which can't be done from the public API */
2 secp256k1_context_rerandomize(&ctx, seed32);
3 return callback(&ctx, callback_data);
4}
(where cb
is a type alias for a callback that takes a context and void pointer and returns an int).
Our usage here would be to implement a signing function that used a freshly randomized context but which did not require the user pass context objects through every API function, nor would it require us to maintain a global mutable context object, which is really hard to do without mutexes. The resulting function would be ~twice as slow as normal signing function but for many usecases this is acceptable since signing is not a frequent operation.
On the other hand, maintaining a global mutable 32-byte random seed would be super easy because we don’t need any synchronization beyond the use of atomics to avoid UB.
cc #780 which is closely related to this but more general.