mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-12 04:42:36 -03:00
Fifth step in converting to C: ecdsa
This commit is contained in:
parent
b1483f874c
commit
d41e93a5e2
5 changed files with 107 additions and 114 deletions
|
@ -6,8 +6,6 @@
|
||||||
#include "ecmult.cpp"
|
#include "ecmult.cpp"
|
||||||
#include "ecdsa.cpp"
|
#include "ecdsa.cpp"
|
||||||
|
|
||||||
using namespace secp256k1;
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
secp256k1_num_start();
|
secp256k1_num_start();
|
||||||
secp256k1_fe_start();
|
secp256k1_fe_start();
|
||||||
|
@ -20,7 +18,8 @@ int main() {
|
||||||
secp256k1_num_init(&r);
|
secp256k1_num_init(&r);
|
||||||
secp256k1_num_init(&s);
|
secp256k1_num_init(&s);
|
||||||
secp256k1_num_init(&m);
|
secp256k1_num_init(&m);
|
||||||
Signature sig;
|
secp256k1_ecdsa_sig_t sig;
|
||||||
|
secp256k1_ecdsa_sig_init(&sig);
|
||||||
secp256k1_fe_set_hex(&x, "a357ae915c4a65281309edf20504740f0eb3343990216b4f81063cb65f2f7e0f", 64);
|
secp256k1_fe_set_hex(&x, "a357ae915c4a65281309edf20504740f0eb3343990216b4f81063cb65f2f7e0f", 64);
|
||||||
int cnt = 0;
|
int cnt = 0;
|
||||||
int good = 0;
|
int good = 0;
|
||||||
|
@ -28,17 +27,18 @@ int main() {
|
||||||
secp256k1_num_set_rand(&r, order);
|
secp256k1_num_set_rand(&r, order);
|
||||||
secp256k1_num_set_rand(&s, order);
|
secp256k1_num_set_rand(&s, order);
|
||||||
secp256k1_num_set_rand(&m, order);
|
secp256k1_num_set_rand(&m, order);
|
||||||
sig.SetRS(r,s);
|
secp256k1_ecdsa_sig_set_rs(&sig, &r, &s);
|
||||||
secp256k1_gej_t pubkey; secp256k1_gej_set_xo(&pubkey, &x, 1);
|
secp256k1_gej_t pubkey; secp256k1_gej_set_xo(&pubkey, &x, 1);
|
||||||
if (secp256k1_gej_is_valid(&pubkey)) {
|
if (secp256k1_gej_is_valid(&pubkey)) {
|
||||||
cnt++;
|
cnt++;
|
||||||
good += sig.Verify(pubkey, m);
|
good += secp256k1_ecdsa_sig_verify(&sig, &pubkey, &m);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
printf("%i/%i\n", good, cnt);
|
printf("%i/%i\n", good, cnt);
|
||||||
secp256k1_num_free(&r);
|
secp256k1_num_free(&r);
|
||||||
secp256k1_num_free(&s);
|
secp256k1_num_free(&s);
|
||||||
secp256k1_num_free(&m);
|
secp256k1_num_free(&m);
|
||||||
|
secp256k1_ecdsa_sig_free(&sig);
|
||||||
|
|
||||||
secp256k1_ecmult_stop();
|
secp256k1_ecmult_stop();
|
||||||
secp256k1_ge_stop();
|
secp256k1_ge_stop();
|
||||||
|
|
142
src/ecdsa.cpp
142
src/ecdsa.cpp
|
@ -4,89 +4,99 @@
|
||||||
#include "ecmult.h"
|
#include "ecmult.h"
|
||||||
#include "ecdsa.h"
|
#include "ecdsa.h"
|
||||||
|
|
||||||
namespace secp256k1 {
|
extern "C" {
|
||||||
|
|
||||||
bool ParsePubKey(secp256k1_gej_t &elem, const unsigned char *pub, int size) {
|
void static secp256k1_ecdsa_sig_init(secp256k1_ecdsa_sig_t *r) {
|
||||||
|
secp256k1_num_init(&r->r);
|
||||||
|
secp256k1_num_init(&r->s);
|
||||||
|
}
|
||||||
|
|
||||||
|
void static secp256k1_ecdsa_sig_free(secp256k1_ecdsa_sig_t *r) {
|
||||||
|
secp256k1_num_free(&r->r);
|
||||||
|
secp256k1_num_free(&r->s);
|
||||||
|
}
|
||||||
|
|
||||||
|
int static secp256k1_ecdsa_pubkey_parse(secp256k1_gej_t *elem, const unsigned char *pub, int size) {
|
||||||
if (size == 33 && (pub[0] == 0x02 || pub[0] == 0x03)) {
|
if (size == 33 && (pub[0] == 0x02 || pub[0] == 0x03)) {
|
||||||
secp256k1_fe_t x;
|
secp256k1_fe_t x;
|
||||||
secp256k1_fe_set_b32(&x, pub+1);
|
secp256k1_fe_set_b32(&x, pub+1);
|
||||||
secp256k1_gej_set_xo(&elem, &x, pub[0] == 0x03);
|
secp256k1_gej_set_xo(elem, &x, pub[0] == 0x03);
|
||||||
} else if (size == 65 && (pub[0] == 0x04 || pub[0] == 0x06 || pub[0] == 0x07)) {
|
} else if (size == 65 && (pub[0] == 0x04 || pub[0] == 0x06 || pub[0] == 0x07)) {
|
||||||
secp256k1_fe_t x,y;
|
secp256k1_fe_t x, y;
|
||||||
secp256k1_fe_set_b32(&x, pub+1);
|
secp256k1_fe_set_b32(&x, pub+1);
|
||||||
secp256k1_fe_set_b32(&y, pub+33);
|
secp256k1_fe_set_b32(&y, pub+33);
|
||||||
secp256k1_gej_set_xy(&elem, &x, &y);
|
secp256k1_gej_set_xy(elem, &x, &y);
|
||||||
if ((pub[0] == 0x06 || pub[0] == 0x07) && secp256k1_fe_is_odd(&y) != (pub[0] == 0x07))
|
if ((pub[0] == 0x06 || pub[0] == 0x07) && secp256k1_fe_is_odd(&y) != (pub[0] == 0x07))
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return secp256k1_gej_is_valid(&elem);
|
return secp256k1_gej_is_valid(elem);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Signature::Parse(const unsigned char *sig, int size) {
|
int static secp256k1_ecdsa_sig_parse(secp256k1_ecdsa_sig_t *r, const unsigned char *sig, int size) {
|
||||||
if (sig[0] != 0x30) return false;
|
if (sig[0] != 0x30) return 0;
|
||||||
int lenr = sig[3];
|
int lenr = sig[3];
|
||||||
if (5+lenr >= size) return false;
|
if (5+lenr >= size) return 0;
|
||||||
int lens = sig[lenr+5];
|
int lens = sig[lenr+5];
|
||||||
if (sig[1] != lenr+lens+4) return false;
|
if (sig[1] != lenr+lens+4) return 0;
|
||||||
if (lenr+lens+6 > size) return false;
|
if (lenr+lens+6 > size) return 0;
|
||||||
if (sig[2] != 0x02) return false;
|
if (sig[2] != 0x02) return 0;
|
||||||
if (lenr == 0) return false;
|
if (lenr == 0) return 0;
|
||||||
if (sig[lenr+4] != 0x02) return false;
|
if (sig[lenr+4] != 0x02) return 0;
|
||||||
if (lens == 0) return false;
|
if (lens == 0) return 0;
|
||||||
secp256k1_num_set_bin(&r, sig+4, lenr);
|
secp256k1_num_set_bin(&r->r, sig+4, lenr);
|
||||||
secp256k1_num_set_bin(&s, sig+6+lenr, lens);
|
secp256k1_num_set_bin(&r->s, sig+6+lenr, lens);
|
||||||
return true;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Signature::Serialize(unsigned char *sig, int *size) {
|
int static secp256k1_ecdsa_sig_serialize(unsigned char *sig, int *size, const secp256k1_ecdsa_sig_t *a) {
|
||||||
int lenR = (secp256k1_num_bits(&r) + 7)/8;
|
int lenR = (secp256k1_num_bits(&a->r) + 7)/8;
|
||||||
if (lenR == 0 || secp256k1_num_get_bit(&r, lenR*8-1))
|
if (lenR == 0 || secp256k1_num_get_bit(&a->r, lenR*8-1))
|
||||||
lenR++;
|
lenR++;
|
||||||
int lenS = (secp256k1_num_bits(&s) + 7)/8;
|
int lenS = (secp256k1_num_bits(&a->s) + 7)/8;
|
||||||
if (lenS == 0 || secp256k1_num_get_bit(&s, lenS*8-1))
|
if (lenS == 0 || secp256k1_num_get_bit(&a->s, lenS*8-1))
|
||||||
lenS++;
|
lenS++;
|
||||||
if (*size < 6+lenS+lenR)
|
if (*size < 6+lenS+lenR)
|
||||||
return false;
|
return 0;
|
||||||
*size = 6 + lenS + lenR;
|
*size = 6 + lenS + lenR;
|
||||||
sig[0] = 0x30;
|
sig[0] = 0x30;
|
||||||
sig[1] = 4 + lenS + lenR;
|
sig[1] = 4 + lenS + lenR;
|
||||||
sig[2] = 0x02;
|
sig[2] = 0x02;
|
||||||
sig[3] = lenR;
|
sig[3] = lenR;
|
||||||
secp256k1_num_get_bin(sig+4, lenR, &r);
|
secp256k1_num_get_bin(sig+4, lenR, &a->r);
|
||||||
sig[4+lenR] = 0x02;
|
sig[4+lenR] = 0x02;
|
||||||
sig[5+lenR] = lenS;
|
sig[5+lenR] = lenS;
|
||||||
secp256k1_num_get_bin(sig+lenR+6, lenS, &s);
|
secp256k1_num_get_bin(sig+lenR+6, lenS, &a->s);
|
||||||
return true;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Signature::RecomputeR(secp256k1_num_t &r2, const secp256k1_gej_t &pubkey, const secp256k1_num_t &message) const {
|
int static secp256k1_ecdsa_sig_recompute(secp256k1_num_t *r2, const secp256k1_ecdsa_sig_t *sig, const secp256k1_gej_t *pubkey, const secp256k1_num_t *message) {
|
||||||
const secp256k1_ge_consts_t &c = *secp256k1_ge_consts;
|
const secp256k1_ge_consts_t *c = secp256k1_ge_consts;
|
||||||
|
|
||||||
if (secp256k1_num_is_neg(&r) || secp256k1_num_is_neg(&s))
|
if (secp256k1_num_is_neg(&sig->r) || secp256k1_num_is_neg(&sig->s))
|
||||||
return false;
|
return 0;
|
||||||
if (secp256k1_num_is_zero(&r) || secp256k1_num_is_zero(&s))
|
if (secp256k1_num_is_zero(&sig->r) || secp256k1_num_is_zero(&sig->s))
|
||||||
return false;
|
return 0;
|
||||||
if (secp256k1_num_cmp(&r, &c.order) >= 0 || secp256k1_num_cmp(&s, &c.order) >= 0)
|
if (secp256k1_num_cmp(&sig->r, &c->order) >= 0 || secp256k1_num_cmp(&sig->s, &c->order) >= 0)
|
||||||
return false;
|
return 0;
|
||||||
|
|
||||||
bool ret = false;
|
int ret = 0;
|
||||||
secp256k1_num_t sn, u1, u2;
|
secp256k1_num_t sn, u1, u2;
|
||||||
secp256k1_num_init(&sn);
|
secp256k1_num_init(&sn);
|
||||||
secp256k1_num_init(&u1);
|
secp256k1_num_init(&u1);
|
||||||
secp256k1_num_init(&u2);
|
secp256k1_num_init(&u2);
|
||||||
secp256k1_num_mod_inverse(&sn, &s, &c.order);
|
secp256k1_num_mod_inverse(&sn, &sig->s, &c->order);
|
||||||
secp256k1_num_mod_mul(&u1, &sn, &message, &c.order);
|
secp256k1_num_mod_mul(&u1, &sn, message, &c->order);
|
||||||
secp256k1_num_mod_mul(&u2, &sn, &r, &c.order);
|
secp256k1_num_mod_mul(&u2, &sn, &sig->r, &c->order);
|
||||||
secp256k1_gej_t pr; secp256k1_ecmult(&pr, &pubkey, &u2, &u1);
|
secp256k1_gej_t pr; secp256k1_ecmult(&pr, pubkey, &u2, &u1);
|
||||||
if (!secp256k1_gej_is_infinity(&pr)) {
|
if (!secp256k1_gej_is_infinity(&pr)) {
|
||||||
secp256k1_fe_t xr; secp256k1_gej_get_x(&xr, &pr);
|
secp256k1_fe_t xr; secp256k1_gej_get_x(&xr, &pr);
|
||||||
secp256k1_fe_normalize(&xr);
|
secp256k1_fe_normalize(&xr);
|
||||||
unsigned char xrb[32]; secp256k1_fe_get_b32(xrb, &xr);
|
unsigned char xrb[32]; secp256k1_fe_get_b32(xrb, &xr);
|
||||||
secp256k1_num_set_bin(&r2, xrb, 32);
|
secp256k1_num_set_bin(r2, xrb, 32);
|
||||||
secp256k1_num_mod(&r2, &r2, &c.order);
|
secp256k1_num_mod(r2, r2, &c->order);
|
||||||
ret = true;
|
ret = 1;
|
||||||
}
|
}
|
||||||
secp256k1_num_free(&sn);
|
secp256k1_num_free(&sn);
|
||||||
secp256k1_num_free(&u1);
|
secp256k1_num_free(&u1);
|
||||||
|
@ -94,52 +104,44 @@ bool Signature::RecomputeR(secp256k1_num_t &r2, const secp256k1_gej_t &pubkey, c
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Signature::Verify(const secp256k1_gej_t &pubkey, const secp256k1_num_t &message) const {
|
int secp256k1_ecdsa_sig_verify(const secp256k1_ecdsa_sig_t *sig, const secp256k1_gej_t *pubkey, const secp256k1_num_t *message) {
|
||||||
secp256k1_num_t r2;
|
secp256k1_num_t r2;
|
||||||
secp256k1_num_init(&r2);
|
secp256k1_num_init(&r2);
|
||||||
bool ret = false;
|
int ret = 0;
|
||||||
ret = RecomputeR(r2, pubkey, message) && secp256k1_num_cmp(&r, &r2) == 0;
|
ret = secp256k1_ecdsa_sig_recompute(&r2, sig, pubkey, message) && secp256k1_num_cmp(&sig->r, &r2) == 0;
|
||||||
secp256k1_num_free(&r2);
|
secp256k1_num_free(&r2);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Signature::Sign(const secp256k1_num_t &seckey, const secp256k1_num_t &message, const secp256k1_num_t &nonce) {
|
int static secp256k1_ecdsa_sig_sign(secp256k1_ecdsa_sig_t *sig, const secp256k1_num_t *seckey, const secp256k1_num_t *message, const secp256k1_num_t *nonce) {
|
||||||
const secp256k1_ge_consts_t &c = *secp256k1_ge_consts;
|
const secp256k1_ge_consts_t *c = secp256k1_ge_consts;
|
||||||
|
|
||||||
secp256k1_gej_t rp;
|
secp256k1_gej_t rp;
|
||||||
secp256k1_ecmult_gen(&rp, &nonce);
|
secp256k1_ecmult_gen(&rp, nonce);
|
||||||
secp256k1_fe_t rx;
|
secp256k1_fe_t rx;
|
||||||
secp256k1_gej_get_x(&rx, &rp);
|
secp256k1_gej_get_x(&rx, &rp);
|
||||||
unsigned char b[32];
|
unsigned char b[32];
|
||||||
secp256k1_fe_normalize(&rx);
|
secp256k1_fe_normalize(&rx);
|
||||||
secp256k1_fe_get_b32(b, &rx);
|
secp256k1_fe_get_b32(b, &rx);
|
||||||
secp256k1_num_set_bin(&r, b, 32);
|
secp256k1_num_set_bin(&sig->r, b, 32);
|
||||||
secp256k1_num_mod(&r, &r, &c.order);
|
secp256k1_num_mod(&sig->r, &sig->r, &c->order);
|
||||||
secp256k1_num_t n;
|
secp256k1_num_t n;
|
||||||
secp256k1_num_init(&n);
|
secp256k1_num_init(&n);
|
||||||
secp256k1_num_mod_mul(&n, &r, &seckey, &c.order);
|
secp256k1_num_mod_mul(&n, &sig->r, seckey, &c->order);
|
||||||
secp256k1_num_add(&n, &n, &message);
|
secp256k1_num_add(&n, &n, message);
|
||||||
secp256k1_num_mod_inverse(&s, &nonce, &c.order);
|
secp256k1_num_mod_inverse(&sig->s, nonce, &c->order);
|
||||||
secp256k1_num_mod_mul(&s, &s, &n, &c.order);
|
secp256k1_num_mod_mul(&sig->s, &sig->s, &n, &c->order);
|
||||||
secp256k1_num_free(&n);
|
secp256k1_num_free(&n);
|
||||||
if (secp256k1_num_is_zero(&s))
|
if (secp256k1_num_is_zero(&sig->s))
|
||||||
return false;
|
return 0;
|
||||||
if (secp256k1_num_is_odd(&s))
|
if (secp256k1_num_is_odd(&sig->s))
|
||||||
secp256k1_num_sub(&s, &c.order, &s);
|
secp256k1_num_sub(&sig->s, &c->order, &sig->s);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Signature::SetRS(const secp256k1_num_t &rin, const secp256k1_num_t &sin) {
|
void static secp256k1_ecdsa_sig_set_rs(secp256k1_ecdsa_sig_t *sig, const secp256k1_num_t *r, const secp256k1_num_t *s) {
|
||||||
secp256k1_num_copy(&r, &rin);
|
secp256k1_num_copy(&sig->r, r);
|
||||||
secp256k1_num_copy(&s, &sin);
|
secp256k1_num_copy(&sig->s, s);
|
||||||
}
|
|
||||||
|
|
||||||
std::string Signature::ToString() const {
|
|
||||||
char rs[65], ss[65];
|
|
||||||
int rl = 65, sl = 65;
|
|
||||||
secp256k1_num_get_hex(rs, &rl, &r);
|
|
||||||
secp256k1_num_get_hex(ss, &sl, &s);
|
|
||||||
return "(" + std::string(rs) + "," + std::string(ss) + ")";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
34
src/ecdsa.h
34
src/ecdsa.h
|
@ -1,30 +1,22 @@
|
||||||
#ifndef _SECP256K1_ECDSA_
|
#ifndef _SECP256K1_ECDSA_
|
||||||
#define _SECP256K1_ECDSA_
|
#define _SECP256K1_ECDSA_
|
||||||
|
|
||||||
namespace secp256k1 {
|
#include "num.h"
|
||||||
|
|
||||||
class Signature {
|
extern "C" {
|
||||||
private:
|
|
||||||
secp256k1_num_t r,s;
|
|
||||||
|
|
||||||
public:
|
typedef struct {
|
||||||
Signature() {
|
secp256k1_num_t r, s;
|
||||||
secp256k1_num_init(&r);
|
} secp256k1_ecdsa_sig_t;
|
||||||
secp256k1_num_init(&s);
|
|
||||||
}
|
|
||||||
~Signature() {
|
|
||||||
secp256k1_num_free(&r);
|
|
||||||
secp256k1_num_free(&s);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Parse(const unsigned char *sig, int size);
|
void static secp256k1_ecdsa_sig_init(secp256k1_ecdsa_sig_t *r);
|
||||||
bool Serialize(unsigned char *sig, int *size);
|
void static secp256k1_ecdsa_sig_free(secp256k1_ecdsa_sig_t *r);
|
||||||
bool RecomputeR(secp256k1_num_t &r2, const secp256k1_gej_t &pubkey, const secp256k1_num_t &message) const;
|
|
||||||
bool Verify(const secp256k1_gej_t &pubkey, const secp256k1_num_t &message) const;
|
int static secp256k1_ecdsa_pubkey_parse(secp256k1_gej_t *elem, const unsigned char *pub, int size);
|
||||||
bool Sign(const secp256k1_num_t &seckey, const secp256k1_num_t &message, const secp256k1_num_t &nonce);
|
int static secp256k1_ecdsa_sig_parse(secp256k1_ecdsa_sig_t *r, const unsigned char *sig, int size);
|
||||||
void SetRS(const secp256k1_num_t &rin, const secp256k1_num_t &sin);
|
int static secp256k1_ecdsa_sig_serialize(unsigned char *sig, int *size, const secp256k1_ecdsa_sig_t *a);
|
||||||
std::string ToString() const;
|
int static secp256k1_ecdsa_sig_sign(secp256k1_ecdsa_sig_t *sig, const secp256k1_num_t *seckey, const secp256k1_num_t *message, const secp256k1_num_t *nonce);
|
||||||
};
|
void static secp256k1_ecdsa_sig_set_rs(secp256k1_ecdsa_sig_t *sig, const secp256k1_num_t *r, const secp256k1_num_t *s);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,47 +4,46 @@
|
||||||
#include "ecmult.cpp"
|
#include "ecmult.cpp"
|
||||||
#include "ecdsa.cpp"
|
#include "ecdsa.cpp"
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
|
||||||
namespace secp256k1 {
|
void secp256k1_start(void) {
|
||||||
|
|
||||||
extern "C" void secp256k1_start(void) {
|
|
||||||
secp256k1_num_start();
|
secp256k1_num_start();
|
||||||
secp256k1_fe_start();
|
secp256k1_fe_start();
|
||||||
secp256k1_ge_start();
|
secp256k1_ge_start();
|
||||||
secp256k1_ecmult_start();
|
secp256k1_ecmult_start();
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" void secp256k1_stop(void) {
|
void secp256k1_stop(void) {
|
||||||
secp256k1_ecmult_stop();
|
secp256k1_ecmult_stop();
|
||||||
secp256k1_ge_stop();
|
secp256k1_ge_stop();
|
||||||
secp256k1_fe_stop();
|
secp256k1_fe_stop();
|
||||||
secp256k1_num_stop();
|
secp256k1_num_stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" int secp256k1_ecdsa_verify(const unsigned char *msg, int msglen, const unsigned char *sig, int siglen, const unsigned char *pubkey, int pubkeylen) {
|
int secp256k1_ecdsa_verify(const unsigned char *msg, int msglen, const unsigned char *sig, int siglen, const unsigned char *pubkey, int pubkeylen) {
|
||||||
int ret = -3;
|
int ret = -3;
|
||||||
secp256k1_num_t m;
|
secp256k1_num_t m;
|
||||||
secp256k1_num_init(&m);
|
secp256k1_num_init(&m);
|
||||||
Signature s;
|
secp256k1_ecdsa_sig_t s;
|
||||||
|
secp256k1_ecdsa_sig_init(&s);
|
||||||
secp256k1_gej_t q;
|
secp256k1_gej_t q;
|
||||||
secp256k1_num_set_bin(&m, msg, msglen);
|
secp256k1_num_set_bin(&m, msg, msglen);
|
||||||
if (!ParsePubKey(q, pubkey, pubkeylen)) {
|
|
||||||
|
if (!secp256k1_ecdsa_pubkey_parse(&q, pubkey, pubkeylen)) {
|
||||||
ret = -1;
|
ret = -1;
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
if (!s.Parse(sig, siglen)) {
|
if (!secp256k1_ecdsa_sig_parse(&s, sig, siglen)) {
|
||||||
fprintf(stderr, "Can't parse signature: ");
|
|
||||||
for (int i=0; i<siglen; i++) fprintf(stderr,"%02x", sig[i]);
|
|
||||||
fprintf(stderr, "\n");
|
|
||||||
ret = -2;
|
ret = -2;
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
if (!s.Verify(q, m)) {
|
if (!secp256k1_ecdsa_sig_verify(&s, &q, &m)) {
|
||||||
ret = 0;
|
ret = 0;
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
ret = 1;
|
ret = 1;
|
||||||
end:
|
end:
|
||||||
|
secp256k1_ecdsa_sig_free(&s);
|
||||||
secp256k1_num_free(&m);
|
secp256k1_num_free(&m);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,8 +9,6 @@
|
||||||
// #define COUNT 2
|
// #define COUNT 2
|
||||||
#define COUNT 100
|
#define COUNT 100
|
||||||
|
|
||||||
using namespace secp256k1;
|
|
||||||
|
|
||||||
void test_run_ecmult_chain() {
|
void test_run_ecmult_chain() {
|
||||||
// random starting point A (on the curve)
|
// random starting point A (on the curve)
|
||||||
secp256k1_fe_t ax; secp256k1_fe_set_hex(&ax, "8b30bbe9ae2a990696b22f670709dff3727fd8bc04d3362c6c7bf458e2846004", 64);
|
secp256k1_fe_t ax; secp256k1_fe_set_hex(&ax, "8b30bbe9ae2a990696b22f670709dff3727fd8bc04d3362c6c7bf458e2846004", 64);
|
||||||
|
@ -159,13 +157,15 @@ void test_ecdsa_sign_verify() {
|
||||||
secp256k1_num_set_rand(&key, &c.order);
|
secp256k1_num_set_rand(&key, &c.order);
|
||||||
secp256k1_num_init(&nonce);
|
secp256k1_num_init(&nonce);
|
||||||
secp256k1_gej_t pub; secp256k1_ecmult_gen(&pub, &key);
|
secp256k1_gej_t pub; secp256k1_ecmult_gen(&pub, &key);
|
||||||
Signature sig;
|
secp256k1_ecdsa_sig_t sig;
|
||||||
|
secp256k1_ecdsa_sig_init(&sig);
|
||||||
do {
|
do {
|
||||||
secp256k1_num_set_rand(&nonce, &c.order);
|
secp256k1_num_set_rand(&nonce, &c.order);
|
||||||
} while(!sig.Sign(key, msg, nonce));
|
} while(!secp256k1_ecdsa_sig_sign(&sig, &key, &msg, &nonce));
|
||||||
assert(sig.Verify(pub, msg));
|
assert(secp256k1_ecdsa_sig_verify(&sig, &pub, &msg));
|
||||||
secp256k1_num_inc(&msg);
|
secp256k1_num_inc(&msg);
|
||||||
assert(!sig.Verify(pub, msg));
|
assert(!secp256k1_ecdsa_sig_verify(&sig, &pub, &msg));
|
||||||
|
secp256k1_ecdsa_sig_free(&sig);
|
||||||
secp256k1_num_free(&msg);
|
secp256k1_num_free(&msg);
|
||||||
secp256k1_num_free(&key);
|
secp256k1_num_free(&key);
|
||||||
secp256k1_num_free(&nonce);
|
secp256k1_num_free(&nonce);
|
||||||
|
|
Loading…
Reference in a new issue