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) {
if (prv.cmpn(0) === 0)
if (prv.isZero())
throw new Error('Bad private key.');
if (prv.cmp(curve.n) >= 0)
if (prv.gte(curve.n))
throw new Error('Bad private key.');
if (k.cmpn(0) === 0)
if (k.isZero())
return null;
if (k.cmp(curve.n) >= 0)
if (k.gte(curve.n))
return null;
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());
if (h.cmpn(0) === 0)
if (h.isZero())
return null;
if (h.cmp(curve.n) >= 0)
if (h.gte(curve.n))
return null;
let s = h.imul(prv);
s = k.isub(s);
s = s.umod(curve.n);
if (s.cmpn(0) === 0)
if (s.isZero())
return null;
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 h = schnorr.hash(msg, sig.r);
if (h.cmp(curve.n) >= 0)
if (h.gte(curve.n))
throw new Error('Invalid hash.');
if (h.cmpn(0) === 0)
if (h.isZero())
throw new Error('Invalid hash.');
if (sig.s.cmp(curve.n) >= 0)
if (sig.s.gte(curve.n))
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.');
const k = curve.decodePoint(key);
@ -147,7 +147,7 @@ schnorr.verify = function verify(msg, signature, key) {
if (rl.y.isOdd())
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 h = schnorr.hash(msg, sig.r);
if (h.cmp(curve.n) >= 0)
if (h.gte(curve.n))
throw new Error('Invalid hash.');
if (h.cmpn(0) === 0)
if (h.isZero())
throw new Error('Invalid hash.');
if (sig.s.cmp(curve.n) >= 0)
if (sig.s.gte(curve.n))
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.');
let hinv = h.invm(curve.n);
@ -196,7 +196,7 @@ schnorr.recover = function recover(signature, msg) {
if (rl.y.isOdd())
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.');
return Buffer.from(k.encode('array', true));
@ -215,16 +215,16 @@ schnorr.combineSigs = function combineSigs(sigs) {
for (let i = 0; i < sigs.length; i++) {
const sig = new Signature(sigs[i]);
if (sig.s.cmpn(0) === 0)
if (sig.s.isZero())
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.');
if (!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.');
s = s.iadd(sig.s);
@ -233,7 +233,7 @@ schnorr.combineSigs = function combineSigs(sigs) {
last = sig;
}
if (s.cmpn(0) === 0)
if (s.isZero())
throw new Error('Bad combined signature.');
return new Signature({ r: r, s: s });
@ -329,10 +329,10 @@ schnorr.generateNoncePair = function generateNoncePair(msg, priv, data) {
for (;;) {
k = new BN(drbg.generate(len));
if (k.cmpn(0) === 0)
if (k.isZero())
continue;
if (k.cmp(curve.n) >= 0)
if (k.gte(curve.n))
continue;
break;

View File

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

View File

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

View File

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