|
|
|
@ -109,25 +109,35 @@ static int secp256k1_ecdsa_sig_serialize(unsigned char *sig, int *size, const se
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int secp256k1_ecdsa_sig_recompute(secp256k1_scalar_t *r2, const secp256k1_ecdsa_sig_t *sig, const secp256k1_ge_t *pubkey, const secp256k1_scalar_t *message) {
|
|
|
|
|
static int secp256k1_ecdsa_sig_verify(const secp256k1_ecdsa_sig_t *sig, const secp256k1_ge_t *pubkey, const secp256k1_scalar_t *message) {
|
|
|
|
|
if (secp256k1_scalar_is_zero(&sig->r) || secp256k1_scalar_is_zero(&sig->s))
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
int ret = 0;
|
|
|
|
|
secp256k1_scalar_t sn, u1, u2;
|
|
|
|
|
secp256k1_scalar_inverse_var(&sn, &sig->s);
|
|
|
|
|
secp256k1_scalar_mul(&u1, &sn, message);
|
|
|
|
|
secp256k1_scalar_mul(&u2, &sn, &sig->r);
|
|
|
|
|
secp256k1_gej_t pubkeyj; secp256k1_gej_set_ge(&pubkeyj, pubkey);
|
|
|
|
|
secp256k1_gej_t pr; secp256k1_ecmult(&pr, &pubkeyj, &u2, &u1);
|
|
|
|
|
if (!secp256k1_gej_is_infinity(&pr)) {
|
|
|
|
|
secp256k1_fe_t xr; secp256k1_gej_get_x_var(&xr, &pr);
|
|
|
|
|
secp256k1_fe_normalize_var(&xr);
|
|
|
|
|
unsigned char xrb[32]; secp256k1_fe_get_b32(xrb, &xr);
|
|
|
|
|
secp256k1_scalar_set_b32(r2, xrb, NULL);
|
|
|
|
|
ret = 1;
|
|
|
|
|
if (secp256k1_gej_is_infinity(&pr)) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
unsigned char c[32];
|
|
|
|
|
secp256k1_scalar_get_b32(c, &sig->r);
|
|
|
|
|
secp256k1_fe_t xr;
|
|
|
|
|
secp256k1_fe_set_b32(&xr, c);
|
|
|
|
|
if (secp256k1_gej_eq_x_var(&xr, &pr)) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
if (secp256k1_fe_cmp_var(&xr, &secp256k1_ecdsa_consts->p_minus_order) >= 0) {
|
|
|
|
|
// We can't add the order to r. This will be the case for almost every r.
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
secp256k1_fe_add(&xr, &secp256k1_ecdsa_consts->order_as_fe);
|
|
|
|
|
if (secp256k1_gej_eq_x_var(&xr, &pr)) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int secp256k1_ecdsa_sig_recover(const secp256k1_ecdsa_sig_t *sig, secp256k1_ge_t *pubkey, const secp256k1_scalar_t *message, int recid) {
|
|
|
|
@ -159,13 +169,6 @@ static int secp256k1_ecdsa_sig_recover(const secp256k1_ecdsa_sig_t *sig, secp256
|
|
|
|
|
return !secp256k1_gej_is_infinity(&qj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int secp256k1_ecdsa_sig_verify(const secp256k1_ecdsa_sig_t *sig, const secp256k1_ge_t *pubkey, const secp256k1_scalar_t *message) {
|
|
|
|
|
secp256k1_scalar_t r2;
|
|
|
|
|
int ret = 0;
|
|
|
|
|
ret = secp256k1_ecdsa_sig_recompute(&r2, sig, pubkey, message) && secp256k1_scalar_eq(&sig->r, &r2);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int secp256k1_ecdsa_sig_sign(secp256k1_ecdsa_sig_t *sig, const secp256k1_scalar_t *seckey, const secp256k1_scalar_t *message, const secp256k1_scalar_t *nonce, int *recid) {
|
|
|
|
|
secp256k1_gej_t rp;
|
|
|
|
|
secp256k1_ecmult_gen(&rp, nonce);
|
|
|
|
|