Browse Source

validation bugfixes: validates main bitcoin chain

master
Pieter Wuille 10 years ago
parent
commit
e3f741f1d4
  1. 17
      ecdsa.h
  2. 6
      field.h
  3. 4
      group.h
  4. 8
      num_gmp.h

17
ecdsa.h

@ -30,11 +30,11 @@ public: @@ -30,11 +30,11 @@ public:
bool Parse(const unsigned char *sig, int size) {
if (sig[0] != 0x30) return false;
if (sig[1] != size-2) return false;
int lenr = sig[3];
if (4+lenr >= size) return false;
if (5+lenr >= size) return false;
int lens = sig[lenr+5];
if (lenr+lens+6 != size) return false;
if (sig[1] != lenr+lens+4) return false;
if (lenr+lens+6 > size) return false;
if (sig[2] != 0x02) return false;
if (lenr == 0) return false;
if (sig[lenr+4] != 0x02) return false;
@ -80,6 +80,10 @@ public: @@ -80,6 +80,10 @@ public:
r = rin;
s = sin;
}
std::string ToString() const {
return "(" + r.ToString() + "," + s.ToString() + ")";
}
};
int VerifyECDSA(const unsigned char *msg, int msglen, const unsigned char *sig, int siglen, const unsigned char *pubkey, int pubkeylen) {
@ -90,8 +94,13 @@ int VerifyECDSA(const unsigned char *msg, int msglen, const unsigned char *sig, @@ -90,8 +94,13 @@ int VerifyECDSA(const unsigned char *msg, int msglen, const unsigned char *sig,
m.SetBytes(msg, msglen);
if (!ParsePubKey(q, pubkey, pubkeylen))
return -1;
if (!s.Parse(sig, siglen))
if (!s.Parse(sig, siglen)) {
fprintf(stderr, "Can't parse signature: ");
for (int i=0; i<siglen; i++) fprintf(stderr,"%02x", sig[i]);
fprintf(stderr, "\n");
return -2;
}
// fprintf(stderr, "Verifying ECDSA: msg=%s pubkey=%s sig=%s\n", m.ToString().c_str(), q.ToString().c_str(), s.ToString().c_str());
if (!s.Verify(ctx, q, m))
return 0;
return 1;

6
field.h

@ -308,7 +308,7 @@ public: @@ -308,7 +308,7 @@ public:
}
/** Set this to be the (modular) inverse of another FieldElem. Magnitude=1 */
void SetInverse(Context &ctx, const FieldElem &a);
void SetInverse(Context &ctx, FieldElem &a);
std::string ToString() {
unsigned char tmp[32];
@ -368,7 +368,7 @@ const FieldConstants &GetFieldConst() { @@ -368,7 +368,7 @@ const FieldConstants &GetFieldConst() {
return field_const;
}
void FieldElem::SetInverse(Context &ctx, const FieldElem &a) {
void FieldElem::SetInverse(Context &ctx, FieldElem &a) {
#if 0
// calculate a^p, with p={45,63,1019,1023}
FieldElem a2; a2.SetSquare(a);
@ -402,7 +402,7 @@ void FieldElem::SetInverse(Context &ctx, const FieldElem &a) { @@ -402,7 +402,7 @@ void FieldElem::SetInverse(Context &ctx, const FieldElem &a) {
SetMult(x,a45);
#else
unsigned char b[32];
GetBytes(b);
a.GetBytes(b);
{
const Number &p = GetFieldConst().field_p;
Context ct(ctx);

4
group.h

@ -39,11 +39,11 @@ public: @@ -39,11 +39,11 @@ public:
y.SetNeg(y, 1);
}
void GetX(FieldElem &xout) const {
void GetX(Context &ctx, FieldElem &xout) {
xout = x;
}
void GetY(FieldElem &yout) const {
void GetY(Context &ctx, FieldElem &yout) {
yout = y;
}

8
num_gmp.h

@ -70,7 +70,7 @@ public: @@ -70,7 +70,7 @@ public:
memset(bin,0,len);
size_t count = 0;
mpz_export(bin + len - size, &count, 1, 1, 1, 0, bn);
assert(size == count);
assert(count == 0 || size == count);
}
void SetInt(int x) {
mpz_set_si(bn, x);
@ -80,7 +80,7 @@ public: @@ -80,7 +80,7 @@ public:
}
void SetModMul(Context &ctx, const Number &a, const Number &b, const Number &m) {
mpz_mul(bn, a.bn, b.bn);
mpz_mod(bn, a.bn, m.bn);
mpz_mod(bn, bn, m.bn);
}
void SetAdd(Context &ctx, const Number &a1, const Number &a2) {
mpz_add(bn, a1.bn, a2.bn);
@ -144,8 +144,8 @@ public: @@ -144,8 +144,8 @@ public:
mpz_fdiv_q_2exp(high.bn, bn, bits);
}
std::string ToString() {
char *str = (char*)malloc((GetBits() + 7)/8 + 2);
std::string ToString() const {
char *str = (char*)malloc(mpz_sizeinbase(bn,16) + 2);
mpz_get_str(str, 16, bn);
std::string ret(str);
free(str);

Loading…
Cancel
Save