bn: stop using bn#cmp.

This commit is contained in:
Christopher Jeffrey 2017-09-06 22:43:46 -07:00
parent 5107cfd27a
commit f63c19cb19
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
4 changed files with 36 additions and 36 deletions

View File

@ -49,16 +49,16 @@ schnorr.hash = function hash(msg, r) {
*/ */
schnorr.trySign = function trySign(msg, prv, k, pn) { schnorr.trySign = function trySign(msg, prv, k, pn) {
if (prv.cmpn(0) === 0) if (prv.isZero())
throw new Error('Bad private key.'); throw new Error('Bad private key.');
if (prv.cmp(curve.n) >= 0) if (prv.gte(curve.n))
throw new Error('Bad private key.'); throw new Error('Bad private key.');
if (k.cmpn(0) === 0) if (k.isZero())
return null; return null;
if (k.cmp(curve.n) >= 0) if (k.gte(curve.n))
return null; return null;
let r = curve.g.mul(k); let r = curve.g.mul(k);
@ -73,17 +73,17 @@ schnorr.trySign = function trySign(msg, prv, k, pn) {
const h = schnorr.hash(msg, r.getX()); const h = schnorr.hash(msg, r.getX());
if (h.cmpn(0) === 0) if (h.isZero())
return null; return null;
if (h.cmp(curve.n) >= 0) if (h.gte(curve.n))
return null; return null;
let s = h.imul(prv); let s = h.imul(prv);
s = k.isub(s); s = k.isub(s);
s = s.umod(curve.n); s = s.umod(curve.n);
if (s.cmpn(0) === 0) if (s.isZero())
return null; return null;
return new Signature({ r: r.getX(), s: s }); return new Signature({ r: r.getX(), s: s });
@ -127,16 +127,16 @@ schnorr.verify = function verify(msg, signature, key) {
const sig = new Signature(signature); const sig = new Signature(signature);
const h = schnorr.hash(msg, sig.r); const h = schnorr.hash(msg, sig.r);
if (h.cmp(curve.n) >= 0) if (h.gte(curve.n))
throw new Error('Invalid hash.'); throw new Error('Invalid hash.');
if (h.cmpn(0) === 0) if (h.isZero())
throw new Error('Invalid hash.'); throw new Error('Invalid hash.');
if (sig.s.cmp(curve.n) >= 0) if (sig.s.gte(curve.n))
throw new Error('Invalid S value.'); throw new Error('Invalid S value.');
if (sig.r.cmp(curve.p) > 0) if (sig.r.gt(curve.p))
throw new Error('Invalid R value.'); throw new Error('Invalid R value.');
const k = curve.decodePoint(key); const k = curve.decodePoint(key);
@ -147,7 +147,7 @@ schnorr.verify = function verify(msg, signature, key) {
if (rl.y.isOdd()) if (rl.y.isOdd())
throw new Error('Odd R value.'); throw new Error('Odd R value.');
return rl.getX().cmp(sig.r) === 0; return rl.getX().eq(sig.r);
}; };
/** /**
@ -161,16 +161,16 @@ schnorr.recover = function recover(signature, msg) {
const sig = new Signature(signature); const sig = new Signature(signature);
const h = schnorr.hash(msg, sig.r); const h = schnorr.hash(msg, sig.r);
if (h.cmp(curve.n) >= 0) if (h.gte(curve.n))
throw new Error('Invalid hash.'); throw new Error('Invalid hash.');
if (h.cmpn(0) === 0) if (h.isZero())
throw new Error('Invalid hash.'); throw new Error('Invalid hash.');
if (sig.s.cmp(curve.n) >= 0) if (sig.s.gte(curve.n))
throw new Error('Invalid S value.'); throw new Error('Invalid S value.');
if (sig.r.cmp(curve.p) > 0) if (sig.r.gt(curve.p))
throw new Error('Invalid R value.'); throw new Error('Invalid R value.');
let hinv = h.invm(curve.n); let hinv = h.invm(curve.n);
@ -196,7 +196,7 @@ schnorr.recover = function recover(signature, msg) {
if (rl.y.isOdd()) if (rl.y.isOdd())
throw new Error('Odd R value.'); throw new Error('Odd R value.');
if (rl.getX().cmp(sig.r) !== 0) if (!rl.getX().eq(sig.r))
throw new Error('Could not recover pubkey.'); throw new Error('Could not recover pubkey.');
return Buffer.from(k.encode('array', true)); return Buffer.from(k.encode('array', true));
@ -215,16 +215,16 @@ schnorr.combineSigs = function combineSigs(sigs) {
for (let i = 0; i < sigs.length; i++) { for (let i = 0; i < sigs.length; i++) {
const sig = new Signature(sigs[i]); const sig = new Signature(sigs[i]);
if (sig.s.cmpn(0) === 0) if (sig.s.isZero())
throw new Error('Bad S value.'); throw new Error('Bad S value.');
if (sig.s.cmp(curve.n) >= 0) if (sig.s.gte(curve.n))
throw new Error('Bad S value.'); throw new Error('Bad S value.');
if (!r) if (!r)
r = sig.r; r = sig.r;
if (last && last.r.cmp(sig.r) !== 0) if (last && !last.r.eq(sig.r))
throw new Error('Bad signature combination.'); throw new Error('Bad signature combination.');
s = s.iadd(sig.s); s = s.iadd(sig.s);
@ -233,7 +233,7 @@ schnorr.combineSigs = function combineSigs(sigs) {
last = sig; last = sig;
} }
if (s.cmpn(0) === 0) if (s.isZero())
throw new Error('Bad combined signature.'); throw new Error('Bad combined signature.');
return new Signature({ r: r, s: s }); return new Signature({ r: r, s: s });
@ -329,10 +329,10 @@ schnorr.generateNoncePair = function generateNoncePair(msg, priv, data) {
for (;;) { for (;;) {
k = new BN(drbg.generate(len)); k = new BN(drbg.generate(len));
if (k.cmpn(0) === 0) if (k.isZero())
continue; continue;
if (k.cmp(curve.n) >= 0) if (k.gte(curve.n))
continue; continue;
break; break;

View File

@ -46,11 +46,11 @@ ec.generatePrivateKey = function generatePrivateKey() {
*/ */
ec.publicKeyCreate = function publicKeyCreate(priv, compress) { ec.publicKeyCreate = function publicKeyCreate(priv, compress) {
assert(Buffer.isBuffer(priv));
if (compress == null) if (compress == null)
compress = true; compress = true;
assert(Buffer.isBuffer(priv));
const key = secp256k1.keyPair({ priv: priv }); const key = secp256k1.keyPair({ priv: priv });
return Buffer.from(key.getPublic(compress, 'array')); return Buffer.from(key.getPublic(compress, 'array'));
@ -63,11 +63,11 @@ ec.publicKeyCreate = function publicKeyCreate(priv, compress) {
*/ */
ec.publicKeyConvert = function publicKeyConvert(key, compress) { ec.publicKeyConvert = function publicKeyConvert(key, compress) {
const point = curve.decodePoint(key);
if (compress == null) if (compress == null)
compress = true; compress = true;
const point = curve.decodePoint(key);
return Buffer.from(point.encode('array', compress)); return Buffer.from(point.encode('array', compress));
}; };
@ -99,12 +99,11 @@ ec.privateKeyTweakAdd = function privateKeyTweakAdd(privateKey, tweak) {
*/ */
ec.publicKeyTweakAdd = function publicKeyTweakAdd(publicKey, tweak, compress) { ec.publicKeyTweakAdd = function publicKeyTweakAdd(publicKey, tweak, compress) {
const key = curve.decodePoint(publicKey);
const point = curve.g.mul(new BN(tweak)).add(key);
if (compress == null) if (compress == null)
compress = true; compress = true;
const key = curve.decodePoint(publicKey);
const point = curve.g.mul(new BN(tweak)).add(key);
const pub = Buffer.from(point.encode('array', compress)); const pub = Buffer.from(point.encode('array', compress));
if (!ec.publicKeyVerify(pub)) if (!ec.publicKeyVerify(pub))
@ -210,7 +209,7 @@ ec.privateKeyVerify = function privateKeyVerify(key) {
key = new BN(key); key = new BN(key);
return key.cmpn(0) !== 0 && key.cmp(curve.n) < 0; return !key.isZero() && key.lt(curve.n);
}; };
/** /**
@ -280,12 +279,12 @@ ec.isLowS = function isLowS(raw) {
return false; return false;
} }
if (sig.s.cmpn(0) === 0) if (sig.s.isZero())
return false; return false;
// If S is greater than half the order, // If S is greater than half the order,
// it's too high. // it's too high.
if (sig.s.cmp(secp256k1.nh) > 0) if (sig.s.gt(secp256k1.nh))
return false; return false;
return true; return true;

View File

@ -119,8 +119,6 @@ ec.ecdh = function ecdh(pub, priv) {
*/ */
ec.recover = function recover(msg, sig, j, compress) { ec.recover = function recover(msg, sig, j, compress) {
let key;
if (!j) if (!j)
j = 0; j = 0;
@ -130,6 +128,7 @@ ec.recover = function recover(msg, sig, j, compress) {
return null; return null;
} }
let key;
try { try {
key = secp256k1.recover(msg, sig, j, compress); key = secp256k1.recover(msg, sig, j, compress);
} catch (e) { } catch (e) {

View File

@ -38,6 +38,7 @@ GCSFilter.prototype.header = function header(prev) {
GCSFilter.prototype.match = function match(key, data) { GCSFilter.prototype.match = function match(key, data) {
const br = new BitReader(this.data); const br = new BitReader(this.data);
const term = siphash24(data, key).imod(this.m); const term = siphash24(data, key).imod(this.m);
let last = new U64(0); let last = new U64(0);
while (last.lt(term)) { while (last.lt(term)) {
@ -179,6 +180,7 @@ GCSFilter.prototype.fromItems = function fromItems(P, key, items) {
values.sort(compare); values.sort(compare);
const bw = new BitWriter(); const bw = new BitWriter();
let last = new U64(0); let last = new U64(0);
for (const hash of values) { for (const hash of values) {
@ -507,7 +509,7 @@ BitReader.prototype.readBits64 = function readBits64(count) {
*/ */
function compare(a, b) { function compare(a, b) {
return a.lt(b) ? -1 : 1; return a.cmp(b);
} }
function siphash24(data, key) { function siphash24(data, key) {