Skip to content

Commit aa5d34a

Browse files
Merge #783: Make the public API docs more consistent and explicit
adec5a1 Add missing null check for ctx and input keys in the public API (Elichai Turkel) f4edfc7 Improve consistency for NULL arguments in the public interface (Elichai Turkel) Pull request description: I went over the public API and added missing explanations on when a pointer can be null and when it cannot, and added some missing checks for null ctx and null pubkey pointers. Open questions IMHO: 1. Can `secp256k1_context_create` return NULL? right now it could return null if you replaced the callbacks at compile time to ones that do return(unlike the default ones which never return). 2. Related to the first, should we document that the callbacks should never return? (in the tests we use returning callbacks but we can violate our own API) right now we say the following: > After this callback returns, anything may happen, including crashing. Is this enough to document answer `no` for the first question and just saying that if the callback returned then you violated the API so `secp256k1_context_create` can return NULL even though it is promised not to? Right now we AFAICT we never check if it returns null Another nit I'm not sure about is wording `(does nothing if NULL)`/`(ignored if NULL)`/`(can be NULL)` More missing docs: 1. Documenting the `data` argument to the default nonce functions ACKs for top commit: ariard: ACK adec5a1 jonasnick: ACK adec5a1 Tree-SHA512: 6fe785776b7e451e9e8cae944987f927b1eb2e2d404dfcb1b0ceb0a30bda4ce16469708920269417e5ada09739723a430e270dea1868fe7d12ccd5699dde5976
2 parents 9a5a87e + adec5a1 commit aa5d34a

8 files changed

+125
-134
lines changed

include/secp256k1.h

+48-49
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ SECP256K1_API secp256k1_context* secp256k1_context_create(
226226
* memory allocation entirely, see the functions in secp256k1_preallocated.h.
227227
*
228228
* Returns: a newly created context object.
229-
* Args: ctx: an existing context to copy (cannot be NULL)
229+
* Args: ctx: an existing context to copy
230230
*/
231231
SECP256K1_API secp256k1_context* secp256k1_context_clone(
232232
const secp256k1_context* ctx
@@ -247,7 +247,7 @@ SECP256K1_API secp256k1_context* secp256k1_context_clone(
247247
*/
248248
SECP256K1_API void secp256k1_context_destroy(
249249
secp256k1_context* ctx
250-
);
250+
) SECP256K1_ARG_NONNULL(1);
251251

252252
/** Set a callback function to be called when an illegal argument is passed to
253253
* an API call. It will only trigger for violations that are mentioned
@@ -278,11 +278,11 @@ SECP256K1_API void secp256k1_context_destroy(
278278
* fails. In this case, the corresponding default handler will be called with
279279
* the data pointer argument set to NULL.
280280
*
281-
* Args: ctx: an existing context object (cannot be NULL)
281+
* Args: ctx: an existing context object.
282282
* In: fun: a pointer to a function to call when an illegal argument is
283283
* passed to the API, taking a message and an opaque pointer.
284284
* (NULL restores the default handler.)
285-
* data: the opaque pointer to pass to fun above.
285+
* data: the opaque pointer to pass to fun above, must be NULL for the default handler.
286286
*
287287
* See also secp256k1_context_set_error_callback.
288288
*/
@@ -302,12 +302,12 @@ SECP256K1_API void secp256k1_context_set_illegal_callback(
302302
* for that). After this callback returns, anything may happen, including
303303
* crashing.
304304
*
305-
* Args: ctx: an existing context object (cannot be NULL)
305+
* Args: ctx: an existing context object.
306306
* In: fun: a pointer to a function to call when an internal error occurs,
307307
* taking a message and an opaque pointer (NULL restores the
308308
* default handler, see secp256k1_context_set_illegal_callback
309309
* for details).
310-
* data: the opaque pointer to pass to fun above.
310+
* data: the opaque pointer to pass to fun above, must be NULL for the default handler.
311311
*
312312
* See also secp256k1_context_set_illegal_callback.
313313
*/
@@ -320,7 +320,7 @@ SECP256K1_API void secp256k1_context_set_error_callback(
320320
/** Create a secp256k1 scratch space object.
321321
*
322322
* Returns: a newly created scratch space.
323-
* Args: ctx: an existing context object (cannot be NULL)
323+
* Args: ctx: an existing context object.
324324
* In: size: amount of memory to be available as scratch space. Some extra
325325
* (<100 bytes) will be allocated for extra accounting.
326326
*/
@@ -480,16 +480,16 @@ SECP256K1_API int secp256k1_ecdsa_signature_serialize_compact(
480480
* Returns: 1: correct signature
481481
* 0: incorrect or unparseable signature
482482
* Args: ctx: a secp256k1 context object, initialized for verification.
483-
* In: sig: the signature being verified (cannot be NULL)
484-
* msghash32: the 32-byte message hash being verified (cannot be NULL).
483+
* In: sig: the signature being verified.
484+
* msghash32: the 32-byte message hash being verified.
485485
* The verifier must make sure to apply a cryptographic
486486
* hash function to the message by itself and not accept an
487487
* msghash32 value directly. Otherwise, it would be easy to
488488
* create a "valid" signature without knowledge of the
489489
* secret key. See also
490490
* https://bitcoin.stackexchange.com/a/81116/35586 for more
491491
* background on this topic.
492-
* pubkey: pointer to an initialized public key to verify with (cannot be NULL)
492+
* pubkey: pointer to an initialized public key to verify with.
493493
*
494494
* To avoid accepting malleable signatures, only ECDSA signatures in lower-S
495495
* form are accepted.
@@ -515,8 +515,7 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdsa_verify(
515515
* or copy if the input was already normalized. (can be NULL if
516516
* you're only interested in whether the input was already
517517
* normalized).
518-
* In: sigin: a pointer to a signature to check/normalize (cannot be NULL,
519-
* can be identical to sigout)
518+
* In: sigin: a pointer to a signature to check/normalize (can be identical to sigout)
520519
*
521520
* With ECDSA a third-party can forge a second distinct signature of the same
522521
* message, given a single initial signature, but without knowing the key. This
@@ -568,12 +567,16 @@ SECP256K1_API extern const secp256k1_nonce_function secp256k1_nonce_function_def
568567
*
569568
* Returns: 1: signature created
570569
* 0: the nonce generation function failed, or the secret key was invalid.
571-
* Args: ctx: pointer to a context object, initialized for signing (cannot be NULL)
572-
* Out: sig: pointer to an array where the signature will be placed (cannot be NULL)
573-
* In: msghash32: the 32-byte message hash being signed (cannot be NULL)
574-
* seckey: pointer to a 32-byte secret key (cannot be NULL)
575-
* noncefp: pointer to a nonce generation function. If NULL, secp256k1_nonce_function_default is used
576-
* ndata: pointer to arbitrary data used by the nonce generation function (can be NULL)
570+
* Args: ctx: pointer to a context object, initialized for signing.
571+
* Out: sig: pointer to an array where the signature will be placed.
572+
* In: msghash32: the 32-byte message hash being signed.
573+
* seckey: pointer to a 32-byte secret key.
574+
* noncefp: pointer to a nonce generation function. If NULL,
575+
* secp256k1_nonce_function_default is used.
576+
* ndata: pointer to arbitrary data used by the nonce generation function
577+
* (can be NULL). If it is non-NULL and
578+
* secp256k1_nonce_function_default is used, then ndata must be a
579+
* pointer to 32-bytes of additional data.
577580
*
578581
* The created signature is always in lower-S form. See
579582
* secp256k1_ecdsa_signature_normalize for more details.
@@ -596,8 +599,8 @@ SECP256K1_API int secp256k1_ecdsa_sign(
596599
*
597600
* Returns: 1: secret key is valid
598601
* 0: secret key is invalid
599-
* Args: ctx: pointer to a context object (cannot be NULL)
600-
* In: seckey: pointer to a 32-byte secret key (cannot be NULL)
602+
* Args: ctx: pointer to a context object.
603+
* In: seckey: pointer to a 32-byte secret key.
601604
*/
602605
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_verify(
603606
const secp256k1_context* ctx,
@@ -606,11 +609,11 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_verify(
606609

607610
/** Compute the public key for a secret key.
608611
*
609-
* Returns: 1: secret was valid, public key stores
610-
* 0: secret was invalid, try again
611-
* Args: ctx: pointer to a context object, initialized for signing (cannot be NULL)
612-
* Out: pubkey: pointer to the created public key (cannot be NULL)
613-
* In: seckey: pointer to a 32-byte secret key (cannot be NULL)
612+
* Returns: 1: secret was valid, public key stores.
613+
* 0: secret was invalid, try again.
614+
* Args: ctx: pointer to a context object, initialized for signing.
615+
* Out: pubkey: pointer to the created public key.
616+
* In: seckey: pointer to a 32-byte secret key.
614617
*/
615618
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create(
616619
const secp256k1_context* ctx,
@@ -626,8 +629,7 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create(
626629
* In/Out: seckey: pointer to the 32-byte secret key to be negated. If the
627630
* secret key is invalid according to
628631
* secp256k1_ec_seckey_verify, this function returns 0 and
629-
* seckey will be set to some unspecified value. (cannot be
630-
* NULL)
632+
* seckey will be set to some unspecified value.
631633
*/
632634
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_negate(
633635
const secp256k1_context* ctx,
@@ -645,7 +647,7 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_negate(
645647
*
646648
* Returns: 1 always
647649
* Args: ctx: pointer to a context object
648-
* In/Out: pubkey: pointer to the public key to be negated (cannot be NULL)
650+
* In/Out: pubkey: pointer to the public key to be negated.
649651
*/
650652
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_negate(
651653
const secp256k1_context* ctx,
@@ -657,15 +659,15 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_negate(
657659
* Returns: 0 if the arguments are invalid or the resulting secret key would be
658660
* invalid (only when the tweak is the negation of the secret key). 1
659661
* otherwise.
660-
* Args: ctx: pointer to a context object (cannot be NULL).
662+
* Args: ctx: pointer to a context object.
661663
* In/Out: seckey: pointer to a 32-byte secret key. If the secret key is
662664
* invalid according to secp256k1_ec_seckey_verify, this
663665
* function returns 0. seckey will be set to some unspecified
664-
* value if this function returns 0. (cannot be NULL)
666+
* value if this function returns 0.
665667
* In: tweak32: pointer to a 32-byte tweak. If the tweak is invalid according to
666668
* secp256k1_ec_seckey_verify, this function returns 0. For
667669
* uniformly random 32-byte arrays the chance of being invalid
668-
* is negligible (around 1 in 2^128) (cannot be NULL).
670+
* is negligible (around 1 in 2^128).
669671
*/
670672
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_tweak_add(
671673
const secp256k1_context* ctx,
@@ -686,14 +688,13 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_add(
686688
* Returns: 0 if the arguments are invalid or the resulting public key would be
687689
* invalid (only when the tweak is the negation of the corresponding
688690
* secret key). 1 otherwise.
689-
* Args: ctx: pointer to a context object initialized for validation
690-
* (cannot be NULL).
691+
* Args: ctx: pointer to a context object initialized for validation.
691692
* In/Out: pubkey: pointer to a public key object. pubkey will be set to an
692-
* invalid value if this function returns 0 (cannot be NULL).
693+
* invalid value if this function returns 0.
693694
* In: tweak32: pointer to a 32-byte tweak. If the tweak is invalid according to
694695
* secp256k1_ec_seckey_verify, this function returns 0. For
695696
* uniformly random 32-byte arrays the chance of being invalid
696-
* is negligible (around 1 in 2^128) (cannot be NULL).
697+
* is negligible (around 1 in 2^128).
697698
*/
698699
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_add(
699700
const secp256k1_context* ctx,
@@ -704,15 +705,15 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_add(
704705
/** Tweak a secret key by multiplying it by a tweak.
705706
*
706707
* Returns: 0 if the arguments are invalid. 1 otherwise.
707-
* Args: ctx: pointer to a context object (cannot be NULL).
708+
* Args: ctx: pointer to a context object.
708709
* In/Out: seckey: pointer to a 32-byte secret key. If the secret key is
709710
* invalid according to secp256k1_ec_seckey_verify, this
710711
* function returns 0. seckey will be set to some unspecified
711-
* value if this function returns 0. (cannot be NULL)
712+
* value if this function returns 0.
712713
* In: tweak32: pointer to a 32-byte tweak. If the tweak is invalid according to
713714
* secp256k1_ec_seckey_verify, this function returns 0. For
714715
* uniformly random 32-byte arrays the chance of being invalid
715-
* is negligible (around 1 in 2^128) (cannot be NULL).
716+
* is negligible (around 1 in 2^128).
716717
*/
717718
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_tweak_mul(
718719
const secp256k1_context* ctx,
@@ -731,14 +732,13 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_mul(
731732
/** Tweak a public key by multiplying it by a tweak value.
732733
*
733734
* Returns: 0 if the arguments are invalid. 1 otherwise.
734-
* Args: ctx: pointer to a context object initialized for validation
735-
* (cannot be NULL).
735+
* Args: ctx: pointer to a context object initialized for validation.
736736
* In/Out: pubkey: pointer to a public key object. pubkey will be set to an
737-
* invalid value if this function returns 0 (cannot be NULL).
737+
* invalid value if this function returns 0.
738738
* In: tweak32: pointer to a 32-byte tweak. If the tweak is invalid according to
739739
* secp256k1_ec_seckey_verify, this function returns 0. For
740740
* uniformly random 32-byte arrays the chance of being invalid
741-
* is negligible (around 1 in 2^128) (cannot be NULL).
741+
* is negligible (around 1 in 2^128).
742742
*/
743743
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_mul(
744744
const secp256k1_context* ctx,
@@ -749,7 +749,7 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_mul(
749749
/** Updates the context randomization to protect against side-channel leakage.
750750
* Returns: 1: randomization successfully updated or nothing to randomize
751751
* 0: error
752-
* Args: ctx: pointer to a context object (cannot be NULL)
752+
* Args: ctx: pointer to a context object.
753753
* In: seed32: pointer to a 32-byte random seed (NULL resets to initial state)
754754
*
755755
* While secp256k1 code is written to be constant-time no matter what secret
@@ -780,18 +780,17 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize(
780780
*
781781
* Returns: 1: the sum of the public keys is valid.
782782
* 0: the sum of the public keys is not valid.
783-
* Args: ctx: pointer to a context object
784-
* Out: out: pointer to a public key object for placing the resulting public key
785-
* (cannot be NULL)
786-
* In: ins: pointer to array of pointers to public keys (cannot be NULL)
787-
* n: the number of public keys to add together (must be at least 1)
783+
* Args: ctx: pointer to a context object.
784+
* Out: out: pointer to a public key object for placing the resulting public key.
785+
* In: ins: pointer to array of pointers to public keys.
786+
* n: the number of public keys to add together (must be at least 1).
788787
*/
789788
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_combine(
790789
const secp256k1_context* ctx,
791790
secp256k1_pubkey *out,
792791
const secp256k1_pubkey * const * ins,
793792
size_t n
794-
) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
793+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
795794

796795
/** Compute a tagged hash as defined in BIP-340.
797796
*

include/secp256k1_ecdh.h

+8-7
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@ SECP256K1_API extern const secp256k1_ecdh_hash_function secp256k1_ecdh_hash_func
3737
*
3838
* Returns: 1: exponentiation was successful
3939
* 0: scalar was invalid (zero or overflow) or hashfp returned 0
40-
* Args: ctx: pointer to a context object (cannot be NULL)
41-
* Out: output: pointer to an array to be filled by hashfp
42-
* In: pubkey: a pointer to a secp256k1_pubkey containing an
43-
* initialized public key
44-
* seckey: a 32-byte scalar with which to multiply the point
45-
* hashfp: pointer to a hash function. If NULL, secp256k1_ecdh_hash_function_sha256 is used
46-
* (in which case, 32 bytes will be written to output)
40+
* Args: ctx: pointer to a context object.
41+
* Out: output: pointer to an array to be filled by hashfp.
42+
* In: pubkey: a pointer to a secp256k1_pubkey containing an initialized public key.
43+
* seckey: a 32-byte scalar with which to multiply the point.
44+
* hashfp: pointer to a hash function. If NULL,
45+
* secp256k1_ecdh_hash_function_sha256 is used
46+
* (in which case, 32 bytes will be written to output).
4747
* data: arbitrary data pointer that is passed through to hashfp
48+
* (can be NULL for secp256k1_ecdh_hash_function_sha256).
4849
*/
4950
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdh(
5051
const secp256k1_context* ctx,

0 commit comments

Comments
 (0)