Rename msg32 to msghash32 in ecdsa_sign/verify and add explanation
This commit is contained in:
parent
8f0c6f1545
commit
f587f04e35
|
@ -452,7 +452,14 @@ SECP256K1_API int secp256k1_ecdsa_signature_serialize_compact(
|
||||||
* 0: incorrect or unparseable signature
|
* 0: incorrect or unparseable signature
|
||||||
* Args: ctx: a secp256k1 context object, initialized for verification.
|
* Args: ctx: a secp256k1 context object, initialized for verification.
|
||||||
* In: sig: the signature being verified (cannot be NULL)
|
* In: sig: the signature being verified (cannot be NULL)
|
||||||
* msg32: the 32-byte message hash being verified (cannot be NULL)
|
* msghash32: the 32-byte message hash being verified (cannot be NULL).
|
||||||
|
* The verifier must make sure to apply a cryptographic
|
||||||
|
* hash function to the message by itself and not accept an
|
||||||
|
* msghash32 value directly. Otherwise, it would be easy to
|
||||||
|
* create a "valid" signature without knowledge of the
|
||||||
|
* secret key. See also
|
||||||
|
* https://bitcoin.stackexchange.com/a/81116/35586 for more
|
||||||
|
* background on this topic.
|
||||||
* pubkey: pointer to an initialized public key to verify with (cannot be NULL)
|
* pubkey: pointer to an initialized public key to verify with (cannot be NULL)
|
||||||
*
|
*
|
||||||
* To avoid accepting malleable signatures, only ECDSA signatures in lower-S
|
* To avoid accepting malleable signatures, only ECDSA signatures in lower-S
|
||||||
|
@ -467,7 +474,7 @@ SECP256K1_API int secp256k1_ecdsa_signature_serialize_compact(
|
||||||
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdsa_verify(
|
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdsa_verify(
|
||||||
const secp256k1_context* ctx,
|
const secp256k1_context* ctx,
|
||||||
const secp256k1_ecdsa_signature *sig,
|
const secp256k1_ecdsa_signature *sig,
|
||||||
const unsigned char *msg32,
|
const unsigned char *msghash32,
|
||||||
const secp256k1_pubkey *pubkey
|
const secp256k1_pubkey *pubkey
|
||||||
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
|
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
|
||||||
|
|
||||||
|
@ -532,12 +539,12 @@ SECP256K1_API extern const secp256k1_nonce_function secp256k1_nonce_function_def
|
||||||
*
|
*
|
||||||
* Returns: 1: signature created
|
* Returns: 1: signature created
|
||||||
* 0: the nonce generation function failed, or the secret key was invalid.
|
* 0: the nonce generation function failed, or the secret key was invalid.
|
||||||
* Args: ctx: pointer to a context object, initialized for signing (cannot be NULL)
|
* Args: ctx: pointer to a context object, initialized for signing (cannot be NULL)
|
||||||
* Out: sig: pointer to an array where the signature will be placed (cannot be NULL)
|
* Out: sig: pointer to an array where the signature will be placed (cannot be NULL)
|
||||||
* In: msg32: the 32-byte message hash being signed (cannot be NULL)
|
* In: msghash32: the 32-byte message hash being signed (cannot be NULL)
|
||||||
* seckey: pointer to a 32-byte secret key (cannot be NULL)
|
* seckey: pointer to a 32-byte secret key (cannot be NULL)
|
||||||
* noncefp:pointer to a nonce generation function. If NULL, secp256k1_nonce_function_default is used
|
* noncefp: pointer to a nonce generation function. If NULL, secp256k1_nonce_function_default is used
|
||||||
* ndata: pointer to arbitrary data used by the nonce generation function (can be NULL)
|
* ndata: pointer to arbitrary data used by the nonce generation function (can be NULL)
|
||||||
*
|
*
|
||||||
* The created signature is always in lower-S form. See
|
* The created signature is always in lower-S form. See
|
||||||
* secp256k1_ecdsa_signature_normalize for more details.
|
* secp256k1_ecdsa_signature_normalize for more details.
|
||||||
|
@ -545,7 +552,7 @@ SECP256K1_API extern const secp256k1_nonce_function secp256k1_nonce_function_def
|
||||||
SECP256K1_API int secp256k1_ecdsa_sign(
|
SECP256K1_API int secp256k1_ecdsa_sign(
|
||||||
const secp256k1_context* ctx,
|
const secp256k1_context* ctx,
|
||||||
secp256k1_ecdsa_signature *sig,
|
secp256k1_ecdsa_signature *sig,
|
||||||
const unsigned char *msg32,
|
const unsigned char *msghash32,
|
||||||
const unsigned char *seckey,
|
const unsigned char *seckey,
|
||||||
secp256k1_nonce_function noncefp,
|
secp256k1_nonce_function noncefp,
|
||||||
const void *ndata
|
const void *ndata
|
||||||
|
|
|
@ -71,17 +71,17 @@ SECP256K1_API int secp256k1_ecdsa_recoverable_signature_serialize_compact(
|
||||||
*
|
*
|
||||||
* Returns: 1: signature created
|
* Returns: 1: signature created
|
||||||
* 0: the nonce generation function failed, or the secret key was invalid.
|
* 0: the nonce generation function failed, or the secret key was invalid.
|
||||||
* Args: ctx: pointer to a context object, initialized for signing (cannot be NULL)
|
* Args: ctx: pointer to a context object, initialized for signing (cannot be NULL)
|
||||||
* Out: sig: pointer to an array where the signature will be placed (cannot be NULL)
|
* Out: sig: pointer to an array where the signature will be placed (cannot be NULL)
|
||||||
* In: msg32: the 32-byte message hash being signed (cannot be NULL)
|
* In: msghash32: the 32-byte message hash being signed (cannot be NULL)
|
||||||
* seckey: pointer to a 32-byte secret key (cannot be NULL)
|
* seckey: pointer to a 32-byte secret key (cannot be NULL)
|
||||||
* noncefp:pointer to a nonce generation function. If NULL, secp256k1_nonce_function_default is used
|
* noncefp: pointer to a nonce generation function. If NULL, secp256k1_nonce_function_default is used
|
||||||
* ndata: pointer to arbitrary data used by the nonce generation function (can be NULL)
|
* ndata: pointer to arbitrary data used by the nonce generation function (can be NULL)
|
||||||
*/
|
*/
|
||||||
SECP256K1_API int secp256k1_ecdsa_sign_recoverable(
|
SECP256K1_API int secp256k1_ecdsa_sign_recoverable(
|
||||||
const secp256k1_context* ctx,
|
const secp256k1_context* ctx,
|
||||||
secp256k1_ecdsa_recoverable_signature *sig,
|
secp256k1_ecdsa_recoverable_signature *sig,
|
||||||
const unsigned char *msg32,
|
const unsigned char *msghash32,
|
||||||
const unsigned char *seckey,
|
const unsigned char *seckey,
|
||||||
secp256k1_nonce_function noncefp,
|
secp256k1_nonce_function noncefp,
|
||||||
const void *ndata
|
const void *ndata
|
||||||
|
@ -91,16 +91,16 @@ SECP256K1_API int secp256k1_ecdsa_sign_recoverable(
|
||||||
*
|
*
|
||||||
* Returns: 1: public key successfully recovered (which guarantees a correct signature).
|
* Returns: 1: public key successfully recovered (which guarantees a correct signature).
|
||||||
* 0: otherwise.
|
* 0: otherwise.
|
||||||
* Args: ctx: pointer to a context object, initialized for verification (cannot be NULL)
|
* Args: ctx: pointer to a context object, initialized for verification (cannot be NULL)
|
||||||
* Out: pubkey: pointer to the recovered public key (cannot be NULL)
|
* Out: pubkey: pointer to the recovered public key (cannot be NULL)
|
||||||
* In: sig: pointer to initialized signature that supports pubkey recovery (cannot be NULL)
|
* In: sig: pointer to initialized signature that supports pubkey recovery (cannot be NULL)
|
||||||
* msg32: the 32-byte message hash assumed to be signed (cannot be NULL)
|
* msghash32: the 32-byte message hash assumed to be signed (cannot be NULL)
|
||||||
*/
|
*/
|
||||||
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdsa_recover(
|
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdsa_recover(
|
||||||
const secp256k1_context* ctx,
|
const secp256k1_context* ctx,
|
||||||
secp256k1_pubkey *pubkey,
|
secp256k1_pubkey *pubkey,
|
||||||
const secp256k1_ecdsa_recoverable_signature *sig,
|
const secp256k1_ecdsa_recoverable_signature *sig,
|
||||||
const unsigned char *msg32
|
const unsigned char *msghash32
|
||||||
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
|
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
|
@ -120,34 +120,34 @@ static int secp256k1_ecdsa_sig_recover(const secp256k1_ecmult_context *ctx, cons
|
||||||
return !secp256k1_gej_is_infinity(&qj);
|
return !secp256k1_gej_is_infinity(&qj);
|
||||||
}
|
}
|
||||||
|
|
||||||
int secp256k1_ecdsa_sign_recoverable(const secp256k1_context* ctx, secp256k1_ecdsa_recoverable_signature *signature, const unsigned char *msg32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void* noncedata) {
|
int secp256k1_ecdsa_sign_recoverable(const secp256k1_context* ctx, secp256k1_ecdsa_recoverable_signature *signature, const unsigned char *msghash32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void* noncedata) {
|
||||||
secp256k1_scalar r, s;
|
secp256k1_scalar r, s;
|
||||||
int ret, recid;
|
int ret, recid;
|
||||||
VERIFY_CHECK(ctx != NULL);
|
VERIFY_CHECK(ctx != NULL);
|
||||||
ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
|
ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
|
||||||
ARG_CHECK(msg32 != NULL);
|
ARG_CHECK(msghash32 != NULL);
|
||||||
ARG_CHECK(signature != NULL);
|
ARG_CHECK(signature != NULL);
|
||||||
ARG_CHECK(seckey != NULL);
|
ARG_CHECK(seckey != NULL);
|
||||||
|
|
||||||
ret = secp256k1_ecdsa_sign_inner(ctx, &r, &s, &recid, msg32, seckey, noncefp, noncedata);
|
ret = secp256k1_ecdsa_sign_inner(ctx, &r, &s, &recid, msghash32, seckey, noncefp, noncedata);
|
||||||
secp256k1_ecdsa_recoverable_signature_save(signature, &r, &s, recid);
|
secp256k1_ecdsa_recoverable_signature_save(signature, &r, &s, recid);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int secp256k1_ecdsa_recover(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const secp256k1_ecdsa_recoverable_signature *signature, const unsigned char *msg32) {
|
int secp256k1_ecdsa_recover(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const secp256k1_ecdsa_recoverable_signature *signature, const unsigned char *msghash32) {
|
||||||
secp256k1_ge q;
|
secp256k1_ge q;
|
||||||
secp256k1_scalar r, s;
|
secp256k1_scalar r, s;
|
||||||
secp256k1_scalar m;
|
secp256k1_scalar m;
|
||||||
int recid;
|
int recid;
|
||||||
VERIFY_CHECK(ctx != NULL);
|
VERIFY_CHECK(ctx != NULL);
|
||||||
ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
|
ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
|
||||||
ARG_CHECK(msg32 != NULL);
|
ARG_CHECK(msghash32 != NULL);
|
||||||
ARG_CHECK(signature != NULL);
|
ARG_CHECK(signature != NULL);
|
||||||
ARG_CHECK(pubkey != NULL);
|
ARG_CHECK(pubkey != NULL);
|
||||||
|
|
||||||
secp256k1_ecdsa_recoverable_signature_load(ctx, &r, &s, &recid, signature);
|
secp256k1_ecdsa_recoverable_signature_load(ctx, &r, &s, &recid, signature);
|
||||||
VERIFY_CHECK(recid >= 0 && recid < 4); /* should have been caught in parse_compact */
|
VERIFY_CHECK(recid >= 0 && recid < 4); /* should have been caught in parse_compact */
|
||||||
secp256k1_scalar_set_b32(&m, msg32, NULL);
|
secp256k1_scalar_set_b32(&m, msghash32, NULL);
|
||||||
if (secp256k1_ecdsa_sig_recover(&ctx->ecmult_ctx, &r, &s, &q, &m, recid)) {
|
if (secp256k1_ecdsa_sig_recover(&ctx->ecmult_ctx, &r, &s, &q, &m, recid)) {
|
||||||
secp256k1_pubkey_save(pubkey, &q);
|
secp256k1_pubkey_save(pubkey, &q);
|
||||||
return 1;
|
return 1;
|
||||||
|
|
|
@ -422,17 +422,17 @@ int secp256k1_ecdsa_signature_normalize(const secp256k1_context* ctx, secp256k1_
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int secp256k1_ecdsa_verify(const secp256k1_context* ctx, const secp256k1_ecdsa_signature *sig, const unsigned char *msg32, const secp256k1_pubkey *pubkey) {
|
int secp256k1_ecdsa_verify(const secp256k1_context* ctx, const secp256k1_ecdsa_signature *sig, const unsigned char *msghash32, const secp256k1_pubkey *pubkey) {
|
||||||
secp256k1_ge q;
|
secp256k1_ge q;
|
||||||
secp256k1_scalar r, s;
|
secp256k1_scalar r, s;
|
||||||
secp256k1_scalar m;
|
secp256k1_scalar m;
|
||||||
VERIFY_CHECK(ctx != NULL);
|
VERIFY_CHECK(ctx != NULL);
|
||||||
ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
|
ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
|
||||||
ARG_CHECK(msg32 != NULL);
|
ARG_CHECK(msghash32 != NULL);
|
||||||
ARG_CHECK(sig != NULL);
|
ARG_CHECK(sig != NULL);
|
||||||
ARG_CHECK(pubkey != NULL);
|
ARG_CHECK(pubkey != NULL);
|
||||||
|
|
||||||
secp256k1_scalar_set_b32(&m, msg32, NULL);
|
secp256k1_scalar_set_b32(&m, msghash32, NULL);
|
||||||
secp256k1_ecdsa_signature_load(ctx, &r, &s, sig);
|
secp256k1_ecdsa_signature_load(ctx, &r, &s, sig);
|
||||||
return (!secp256k1_scalar_is_high(&s) &&
|
return (!secp256k1_scalar_is_high(&s) &&
|
||||||
secp256k1_pubkey_load(ctx, &q, pubkey) &&
|
secp256k1_pubkey_load(ctx, &q, pubkey) &&
|
||||||
|
@ -533,16 +533,16 @@ static int secp256k1_ecdsa_sign_inner(const secp256k1_context* ctx, secp256k1_sc
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int secp256k1_ecdsa_sign(const secp256k1_context* ctx, secp256k1_ecdsa_signature *signature, const unsigned char *msg32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void* noncedata) {
|
int secp256k1_ecdsa_sign(const secp256k1_context* ctx, secp256k1_ecdsa_signature *signature, const unsigned char *msghash32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void* noncedata) {
|
||||||
secp256k1_scalar r, s;
|
secp256k1_scalar r, s;
|
||||||
int ret;
|
int ret;
|
||||||
VERIFY_CHECK(ctx != NULL);
|
VERIFY_CHECK(ctx != NULL);
|
||||||
ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
|
ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
|
||||||
ARG_CHECK(msg32 != NULL);
|
ARG_CHECK(msghash32 != NULL);
|
||||||
ARG_CHECK(signature != NULL);
|
ARG_CHECK(signature != NULL);
|
||||||
ARG_CHECK(seckey != NULL);
|
ARG_CHECK(seckey != NULL);
|
||||||
|
|
||||||
ret = secp256k1_ecdsa_sign_inner(ctx, &r, &s, NULL, msg32, seckey, noncefp, noncedata);
|
ret = secp256k1_ecdsa_sign_inner(ctx, &r, &s, NULL, msghash32, seckey, noncefp, noncedata);
|
||||||
secp256k1_ecdsa_signature_save(signature, &r, &s);
|
secp256k1_ecdsa_signature_save(signature, &r, &s);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue