7 #ifndef SECP256K1_MODULE_RECOVERY_MAIN_H 8 #define SECP256K1_MODULE_RECOVERY_MAIN_H 21 secp256k1_scalar_set_b32(r, &sig->
data[0], NULL);
22 secp256k1_scalar_set_b32(s, &sig->
data[32], NULL);
24 *recid = sig->
data[64];
32 secp256k1_scalar_get_b32(&sig->
data[0], r);
33 secp256k1_scalar_get_b32(&sig->
data[32], s);
35 sig->
data[64] = recid;
48 secp256k1_scalar_set_b32(&r, &input64[0], &overflow);
50 secp256k1_scalar_set_b32(&s, &input64[32], &overflow);
53 secp256k1_ecdsa_recoverable_signature_save(sig, &r, &s, recid);
55 memset(sig, 0,
sizeof(*sig));
68 secp256k1_ecdsa_recoverable_signature_load(ctx, &r, &s, recid, sig);
69 secp256k1_scalar_get_b32(&output64[0], &r);
70 secp256k1_scalar_get_b32(&output64[32], &s);
82 secp256k1_ecdsa_recoverable_signature_load(ctx, &r, &s, &recid, sigin);
83 secp256k1_ecdsa_signature_save(sig, &r, &s);
88 unsigned char brx[32];
96 if (secp256k1_scalar_is_zero(sigr) || secp256k1_scalar_is_zero(sigs)) {
100 secp256k1_scalar_get_b32(brx, sigr);
101 r = secp256k1_fe_set_b32(&fx, brx);
105 if (secp256k1_fe_cmp_var(&fx, &secp256k1_ecdsa_const_p_minus_order) >= 0) {
108 secp256k1_fe_add(&fx, &secp256k1_ecdsa_const_order_as_fe);
110 if (!secp256k1_ge_set_xo_var(&x, &fx, recid & 1)) {
113 secp256k1_gej_set_ge(&xj, &x);
114 secp256k1_scalar_inverse_var(&rn, sigr);
115 secp256k1_scalar_mul(&u1, &rn, message);
116 secp256k1_scalar_negate(&u1, &u1);
117 secp256k1_scalar_mul(&u2, &rn, sigs);
118 secp256k1_ecmult(ctx, &qj, &xj, &u2, &u1);
119 secp256k1_ge_set_gej_var(pubkey, &qj);
120 return !secp256k1_gej_is_infinity(&qj);
134 if (noncefp == NULL) {
138 secp256k1_scalar_set_b32(&sec, seckey, &overflow);
140 if (!overflow && !secp256k1_scalar_is_zero(&sec)) {
141 unsigned char nonce32[32];
142 unsigned int count = 0;
143 secp256k1_scalar_set_b32(&msg, msg32, NULL);
145 ret = noncefp(nonce32, msg32, seckey, NULL, (
void*)noncedata, count);
149 secp256k1_scalar_set_b32(&non, nonce32, &overflow);
150 if (!secp256k1_scalar_is_zero(&non) && !overflow) {
151 if (secp256k1_ecdsa_sig_sign(&ctx->
ecmult_gen_ctx, &r, &s, &sec, &msg, &non, &recid)) {
157 memset(nonce32, 0, 32);
158 secp256k1_scalar_clear(&msg);
159 secp256k1_scalar_clear(&non);
160 secp256k1_scalar_clear(&sec);
163 secp256k1_ecdsa_recoverable_signature_save(signature, &r, &s, recid);
165 memset(signature, 0,
sizeof(*signature));
181 secp256k1_ecdsa_recoverable_signature_load(ctx, &r, &s, &recid, signature);
183 secp256k1_scalar_set_b32(&m, msg32, NULL);
184 if (secp256k1_ecdsa_sig_recover(&ctx->
ecmult_ctx, &r, &s, &q, &m, recid)) {
185 secp256k1_pubkey_save(pubkey, &q);
188 memset(pubkey, 0,
sizeof(*pubkey));
#define VERIFY_CHECK(cond)
SECP256K1_API const secp256k1_nonce_function secp256k1_nonce_function_default
A default safe nonce generation function (currently equal to secp256k1_nonce_function_rfc6979).
int secp256k1_ecdsa_recoverable_signature_convert(const secp256k1_context *ctx, secp256k1_ecdsa_signature *sig, const secp256k1_ecdsa_recoverable_signature *sigin)
Convert a recoverable signature into a normal signature.
Opaque data structured that holds a parsed ECDSA signature, supporting pubkey recovery.
int secp256k1_ecdsa_recoverable_signature_serialize_compact(const secp256k1_context *ctx, unsigned char *output64, int *recid, const secp256k1_ecdsa_recoverable_signature *sig)
Serialize an ECDSA signature in compact format (64 bytes + recovery id).
A group element of the secp256k1 curve, in jacobian coordinates.
int secp256k1_ecdsa_recoverable_signature_parse_compact(const secp256k1_context *ctx, secp256k1_ecdsa_recoverable_signature *sig, const unsigned char *input64, int recid)
Parse a compact ECDSA signature (64 bytes + recovery id).
secp256k1_ecmult_gen_context ecmult_gen_ctx
int secp256k1_ecdsa_recover(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const secp256k1_ecdsa_recoverable_signature *signature, const unsigned char *msg32)
Recover an ECDSA public key from a signature.
secp256k1_ecmult_context ecmult_ctx
A group element of the secp256k1 curve, in affine coordinates.
Opaque data structured that holds a parsed ECDSA signature.
A scalar modulo the group order of the secp256k1 curve.
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)
Create a recoverable ECDSA signature.
int(* secp256k1_nonce_function)(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int attempt)
A pointer to a function to deterministically generate a nonce.
void * memcpy(void *a, const void *b, size_t c)
Opaque data structure that holds a parsed and valid public key.