Skip to content

Commit a209e98

Browse files
author
Bernard Snowden
committed
Merge branch 'master' of https://github.com/bitpay/bitcore
2 parents 10e0f84 + ff706b1 commit a209e98

File tree

24 files changed

+1844
-334
lines changed

24 files changed

+1844
-334
lines changed

packages/bitcore-lib-cash/lib/crypto/schnorr.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Schnorr.prototype._findSignature = function(d, e) {
7272
$.checkState(!d.gte(n), new Error('privkey out of field of curve'));
7373

7474

75-
let k = nonceFunctionRFC6979(d.toBuffer(), e.toBuffer({ size: 32 }));
75+
let k = nonceFunctionRFC6979(d.toBuffer({ size: 32 }), e.toBuffer({ size: 32 }));
7676

7777
let P = G.mul(d);
7878
let R = G.mul(k);
@@ -106,9 +106,8 @@ Schnorr.prototype._findSignature = function(d, e) {
106106
return 'signature must be a 64 byte or 65 byte array';
107107
}
108108

109-
if(!(this.pubkey.toBuffer().length === 32 || this.pubkey.toBuffer().length === 33)) {
110-
return 'pubkey must be 32 byte buffer';
111-
}
109+
110+
let hashbuf = this.endian === 'little' ? BufferUtil.reverse(this.hashbuf) : this.hashbuf
112111

113112
let P = this.pubkey.point;
114113
let G = Point.getG();
@@ -129,7 +128,7 @@ Schnorr.prototype._findSignature = function(d, e) {
129128
let Br = r.toBuffer();
130129
let Bp = Point.pointToCompressed(P);
131130

132-
let hash = Hash.sha256(Buffer.concat([Br, Bp, this.hashbuf]));
131+
let hash = Hash.sha256(Buffer.concat([Br, Bp, hashbuf]));
133132
let e = BN.fromBuffer(hash, 'big').umod(n);
134133

135134
let sG = G.mul(s);
@@ -143,6 +142,7 @@ Schnorr.prototype._findSignature = function(d, e) {
143142
};
144143

145144
Schnorr.prototype.verify = function() {
145+
146146
if (!this.sigError()) {
147147
this.verified = true;
148148
} else {
@@ -161,10 +161,11 @@ Schnorr.prototype._findSignature = function(d, e) {
161161
let V = Buffer.from("0101010101010101010101010101010101010101010101010101010101010101","hex");
162162
let K = Buffer.from("0000000000000000000000000000000000000000000000000000000000000000","hex");
163163

164-
let blob = Buffer.concat([privkey, msgbuf, Buffer.from("Schnorr+SHA256 ", "utf-8")]);
164+
let blob = Buffer.concat([privkey, msgbuf, Buffer.from("", "ascii"), Buffer.from("Schnorr+SHA256 ", "ascii")]);
165165

166166
K = Hash.sha256hmac(Buffer.concat([V, Buffer.from('00', 'hex'), blob]), K);
167167
V = Hash.sha256hmac(V,K);
168+
168169
K = Hash.sha256hmac(Buffer.concat([V,Buffer.from('01','hex'), blob]), K);
169170
V = Hash.sha256hmac(V,K);
170171

@@ -173,13 +174,14 @@ Schnorr.prototype._findSignature = function(d, e) {
173174
while (true) {
174175
V = Hash.sha256hmac(V,K);
175176
T = BN.fromBuffer(V);
176-
177+
$.checkState(T.toBuffer().length >= 32, "T failed test");
177178
k = T;
179+
178180
if (k.gt(new BN(0) && k.lt(Point.getN()))) {
179181
break;
180182
}
181183
K = Hash.sha256hmac(Buffer.concat([V, Buffer.from("00", 'hex')]), K);
182-
V = Hash.hmac(Hash.sha256sha256, V, K);
184+
V = Hash.hmac(Hash.sha256, V, K);
183185
}
184186
return k;
185187
}
@@ -196,11 +198,9 @@ Schnorr.prototype._findSignature = function(d, e) {
196198
return Schnorr().set({
197199
hashbuf: hashbuf,
198200
endian: endian,
199-
sig: sig,
201+
sig: {...sig, isSchnorr: true },
200202
pubkey: pubkey
201203
}).verify().verified;
202204
};
203205

204-
module.exports = Schnorr;
205-
206-
206+
module.exports = Schnorr;

packages/bitcore-lib-cash/lib/crypto/signature.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,18 @@ Signature.fromCompact = function(buf) {
6363
};
6464

6565
Signature.fromDER = Signature.fromBuffer = function(buf, strict) {
66-
// Schnorr Signatures use 64/65 byte for in tx r [len] 32 , s [len] 32, nhashtype
67-
if((buf.length === 64 || buf.length === 65)) {
66+
// Schnorr Signatures use 65 byte for in tx r [len] 32 , s [len] 32, nhashtype
67+
if((buf.length === 64) && buf[0] != 0x30) {
6868
let obj = Signature.parseSchnorrEncodedSig(buf);
6969
let sig = new Signature();
7070
sig.r = obj.r;
7171
sig.s = obj.s;
7272
sig.isSchnorr = true;
7373
return sig;
74+
} if (buf.length === 64 && buf[0] === 0x30) {
75+
return "64 DER (ecdsa) signautres not allowed";
7476
}
75-
77+
7678
var obj = Signature.parseDER(buf, strict);
7779
var sig = new Signature();
7880

@@ -112,13 +114,14 @@ Signature.parseSchnorrEncodedSig = function(buf) {
112114
let s = buf.slice(32, 64);
113115
let hashtype;
114116
if (buf.length === 65) {
115-
hashtype = buf.slice(64,66);
116-
this.hashtype = hashtype;
117+
hashtype = buf.slice(64,65);
118+
this.nhashtype = hashtype;
117119
}
118120

119121
var obj = {
120122
r: BN.fromBuffer(r),
121-
s: BN.fromBuffer(s)
123+
s: BN.fromBuffer(s),
124+
nhashtype: hashtype
122125
};
123126

124127
return obj;
@@ -204,11 +207,18 @@ Signature.prototype.toCompact = function(i, compressed) {
204207
return Buffer.concat([b1, b2, b3]);
205208
};
206209

207-
Signature.prototype.toBuffer = Signature.prototype.toDER = function() {
210+
Signature.prototype.toBuffer = Signature.prototype.toDER = function(signingMethod) {
211+
212+
// Schnorr signatures use a 64 byte r,s format, where as ECDSA takes the form decribed
213+
// below, above the isDER function signature.
214+
215+
signingMethod = signingMethod || "ecdsa";
216+
217+
208218
var rnbuf = this.r.toBuffer();
209219
var snbuf = this.s.toBuffer();
210220

211-
if(this.isSchnorr) {
221+
if(signingMethod === "schnorr") {
212222
return Buffer.concat([rnbuf, snbuf]);
213223
}
214224

@@ -411,8 +421,8 @@ Signature.prototype.hasDefinedHashtype = function() {
411421
return true;
412422
};
413423

414-
Signature.prototype.toTxFormat = function() {
415-
var derbuf = this.toDER();
424+
Signature.prototype.toTxFormat = function(signingMethod) {
425+
var derbuf = this.toDER(signingMethod);
416426
var buf = Buffer.alloc(1);
417427
buf.writeUInt8(this.nhashtype, 0);
418428
return Buffer.concat([derbuf, buf]);

0 commit comments

Comments
 (0)