First commit
This commit is contained in:
241
hGameTest/node_modules/elliptic/lib/elliptic/ec/index.js
generated
vendored
Normal file
241
hGameTest/node_modules/elliptic/lib/elliptic/ec/index.js
generated
vendored
Normal file
@@ -0,0 +1,241 @@
|
||||
'use strict';
|
||||
|
||||
var BN = require('bn.js');
|
||||
var HmacDRBG = require('hmac-drbg');
|
||||
var utils = require('../utils');
|
||||
var curves = require('../curves');
|
||||
var rand = require('brorand');
|
||||
var assert = utils.assert;
|
||||
|
||||
var KeyPair = require('./key');
|
||||
var Signature = require('./signature');
|
||||
|
||||
function EC(options) {
|
||||
if (!(this instanceof EC))
|
||||
return new EC(options);
|
||||
|
||||
// Shortcut `elliptic.ec(curve-name)`
|
||||
if (typeof options === 'string') {
|
||||
assert(curves.hasOwnProperty(options), 'Unknown curve ' + options);
|
||||
|
||||
options = curves[options];
|
||||
}
|
||||
|
||||
// Shortcut for `elliptic.ec(elliptic.curves.curveName)`
|
||||
if (options instanceof curves.PresetCurve)
|
||||
options = { curve: options };
|
||||
|
||||
this.curve = options.curve.curve;
|
||||
this.n = this.curve.n;
|
||||
this.nh = this.n.ushrn(1);
|
||||
this.g = this.curve.g;
|
||||
|
||||
// Point on curve
|
||||
this.g = options.curve.g;
|
||||
this.g.precompute(options.curve.n.bitLength() + 1);
|
||||
|
||||
// Hash for function for DRBG
|
||||
this.hash = options.hash || options.curve.hash;
|
||||
}
|
||||
module.exports = EC;
|
||||
|
||||
EC.prototype.keyPair = function keyPair(options) {
|
||||
return new KeyPair(this, options);
|
||||
};
|
||||
|
||||
EC.prototype.keyFromPrivate = function keyFromPrivate(priv, enc) {
|
||||
return KeyPair.fromPrivate(this, priv, enc);
|
||||
};
|
||||
|
||||
EC.prototype.keyFromPublic = function keyFromPublic(pub, enc) {
|
||||
return KeyPair.fromPublic(this, pub, enc);
|
||||
};
|
||||
|
||||
EC.prototype.genKeyPair = function genKeyPair(options) {
|
||||
if (!options)
|
||||
options = {};
|
||||
|
||||
// Instantiate Hmac_DRBG
|
||||
var drbg = new HmacDRBG({
|
||||
hash: this.hash,
|
||||
pers: options.pers,
|
||||
persEnc: options.persEnc || 'utf8',
|
||||
entropy: options.entropy || rand(this.hash.hmacStrength),
|
||||
entropyEnc: options.entropy && options.entropyEnc || 'utf8',
|
||||
nonce: this.n.toArray()
|
||||
});
|
||||
|
||||
var bytes = this.n.byteLength();
|
||||
var ns2 = this.n.sub(new BN(2));
|
||||
do {
|
||||
var priv = new BN(drbg.generate(bytes));
|
||||
if (priv.cmp(ns2) > 0)
|
||||
continue;
|
||||
|
||||
priv.iaddn(1);
|
||||
return this.keyFromPrivate(priv);
|
||||
} while (true);
|
||||
};
|
||||
|
||||
EC.prototype._truncateToN = function truncateToN(msg, truncOnly) {
|
||||
var delta = msg.byteLength() * 8 - this.n.bitLength();
|
||||
if (delta > 0)
|
||||
msg = msg.ushrn(delta);
|
||||
if (!truncOnly && msg.cmp(this.n) >= 0)
|
||||
return msg.sub(this.n);
|
||||
else
|
||||
return msg;
|
||||
};
|
||||
|
||||
EC.prototype.sign = function sign(msg, key, enc, options) {
|
||||
if (typeof enc === 'object') {
|
||||
options = enc;
|
||||
enc = null;
|
||||
}
|
||||
if (!options)
|
||||
options = {};
|
||||
|
||||
key = this.keyFromPrivate(key, enc);
|
||||
msg = this._truncateToN(new BN(msg, 16));
|
||||
|
||||
// Zero-extend key to provide enough entropy
|
||||
var bytes = this.n.byteLength();
|
||||
var bkey = key.getPrivate().toArray('be', bytes);
|
||||
|
||||
// Zero-extend nonce to have the same byte size as N
|
||||
var nonce = msg.toArray('be', bytes);
|
||||
|
||||
// Instantiate Hmac_DRBG
|
||||
var drbg = new HmacDRBG({
|
||||
hash: this.hash,
|
||||
entropy: bkey,
|
||||
nonce: nonce,
|
||||
pers: options.pers,
|
||||
persEnc: options.persEnc || 'utf8'
|
||||
});
|
||||
|
||||
// Number of bytes to generate
|
||||
var ns1 = this.n.sub(new BN(1));
|
||||
|
||||
for (var iter = 0; true; iter++) {
|
||||
var k = options.k ?
|
||||
options.k(iter) :
|
||||
new BN(drbg.generate(this.n.byteLength()));
|
||||
k = this._truncateToN(k, true);
|
||||
if (k.cmpn(1) <= 0 || k.cmp(ns1) >= 0)
|
||||
continue;
|
||||
|
||||
var kp = this.g.mul(k);
|
||||
if (kp.isInfinity())
|
||||
continue;
|
||||
|
||||
var kpX = kp.getX();
|
||||
var r = kpX.umod(this.n);
|
||||
if (r.cmpn(0) === 0)
|
||||
continue;
|
||||
|
||||
var s = k.invm(this.n).mul(r.mul(key.getPrivate()).iadd(msg));
|
||||
s = s.umod(this.n);
|
||||
if (s.cmpn(0) === 0)
|
||||
continue;
|
||||
|
||||
var recoveryParam = (kp.getY().isOdd() ? 1 : 0) |
|
||||
(kpX.cmp(r) !== 0 ? 2 : 0);
|
||||
|
||||
// Use complement of `s`, if it is > `n / 2`
|
||||
if (options.canonical && s.cmp(this.nh) > 0) {
|
||||
s = this.n.sub(s);
|
||||
recoveryParam ^= 1;
|
||||
}
|
||||
|
||||
return new Signature({ r: r, s: s, recoveryParam: recoveryParam });
|
||||
}
|
||||
};
|
||||
|
||||
EC.prototype.verify = function verify(msg, signature, key, enc) {
|
||||
msg = this._truncateToN(new BN(msg, 16));
|
||||
key = this.keyFromPublic(key, enc);
|
||||
signature = new Signature(signature, 'hex');
|
||||
|
||||
// Perform primitive values validation
|
||||
var r = signature.r;
|
||||
var s = signature.s;
|
||||
if (r.cmpn(1) < 0 || r.cmp(this.n) >= 0)
|
||||
return false;
|
||||
if (s.cmpn(1) < 0 || s.cmp(this.n) >= 0)
|
||||
return false;
|
||||
|
||||
// Validate signature
|
||||
var sinv = s.invm(this.n);
|
||||
var u1 = sinv.mul(msg).umod(this.n);
|
||||
var u2 = sinv.mul(r).umod(this.n);
|
||||
|
||||
if (!this.curve._maxwellTrick) {
|
||||
var p = this.g.mulAdd(u1, key.getPublic(), u2);
|
||||
if (p.isInfinity())
|
||||
return false;
|
||||
|
||||
return p.getX().umod(this.n).cmp(r) === 0;
|
||||
}
|
||||
|
||||
// NOTE: Greg Maxwell's trick, inspired by:
|
||||
// https://git.io/vad3K
|
||||
|
||||
var p = this.g.jmulAdd(u1, key.getPublic(), u2);
|
||||
if (p.isInfinity())
|
||||
return false;
|
||||
|
||||
// Compare `p.x` of Jacobian point with `r`,
|
||||
// this will do `p.x == r * p.z^2` instead of multiplying `p.x` by the
|
||||
// inverse of `p.z^2`
|
||||
return p.eqXToP(r);
|
||||
};
|
||||
|
||||
EC.prototype.recoverPubKey = function(msg, signature, j, enc) {
|
||||
assert((3 & j) === j, 'The recovery param is more than two bits');
|
||||
signature = new Signature(signature, enc);
|
||||
|
||||
var n = this.n;
|
||||
var e = new BN(msg);
|
||||
var r = signature.r;
|
||||
var s = signature.s;
|
||||
|
||||
// A set LSB signifies that the y-coordinate is odd
|
||||
var isYOdd = j & 1;
|
||||
var isSecondKey = j >> 1;
|
||||
if (r.cmp(this.curve.p.umod(this.curve.n)) >= 0 && isSecondKey)
|
||||
throw new Error('Unable to find sencond key candinate');
|
||||
|
||||
// 1.1. Let x = r + jn.
|
||||
if (isSecondKey)
|
||||
r = this.curve.pointFromX(r.add(this.curve.n), isYOdd);
|
||||
else
|
||||
r = this.curve.pointFromX(r, isYOdd);
|
||||
|
||||
var rInv = signature.r.invm(n);
|
||||
var s1 = n.sub(e).mul(rInv).umod(n);
|
||||
var s2 = s.mul(rInv).umod(n);
|
||||
|
||||
// 1.6.1 Compute Q = r^-1 (sR - eG)
|
||||
// Q = r^-1 (sR + -eG)
|
||||
return this.g.mulAdd(s1, r, s2);
|
||||
};
|
||||
|
||||
EC.prototype.getKeyRecoveryParam = function(e, signature, Q, enc) {
|
||||
signature = new Signature(signature, enc);
|
||||
if (signature.recoveryParam !== null)
|
||||
return signature.recoveryParam;
|
||||
|
||||
for (var i = 0; i < 4; i++) {
|
||||
var Qprime;
|
||||
try {
|
||||
Qprime = this.recoverPubKey(e, signature, i);
|
||||
} catch (e) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (Qprime.eq(Q))
|
||||
return i;
|
||||
}
|
||||
throw new Error('Unable to find valid recovery factor');
|
||||
};
|
||||
118
hGameTest/node_modules/elliptic/lib/elliptic/ec/key.js
generated
vendored
Normal file
118
hGameTest/node_modules/elliptic/lib/elliptic/ec/key.js
generated
vendored
Normal file
@@ -0,0 +1,118 @@
|
||||
'use strict';
|
||||
|
||||
var BN = require('bn.js');
|
||||
var utils = require('../utils');
|
||||
var assert = utils.assert;
|
||||
|
||||
function KeyPair(ec, options) {
|
||||
this.ec = ec;
|
||||
this.priv = null;
|
||||
this.pub = null;
|
||||
|
||||
// KeyPair(ec, { priv: ..., pub: ... })
|
||||
if (options.priv)
|
||||
this._importPrivate(options.priv, options.privEnc);
|
||||
if (options.pub)
|
||||
this._importPublic(options.pub, options.pubEnc);
|
||||
}
|
||||
module.exports = KeyPair;
|
||||
|
||||
KeyPair.fromPublic = function fromPublic(ec, pub, enc) {
|
||||
if (pub instanceof KeyPair)
|
||||
return pub;
|
||||
|
||||
return new KeyPair(ec, {
|
||||
pub: pub,
|
||||
pubEnc: enc
|
||||
});
|
||||
};
|
||||
|
||||
KeyPair.fromPrivate = function fromPrivate(ec, priv, enc) {
|
||||
if (priv instanceof KeyPair)
|
||||
return priv;
|
||||
|
||||
return new KeyPair(ec, {
|
||||
priv: priv,
|
||||
privEnc: enc
|
||||
});
|
||||
};
|
||||
|
||||
KeyPair.prototype.validate = function validate() {
|
||||
var pub = this.getPublic();
|
||||
|
||||
if (pub.isInfinity())
|
||||
return { result: false, reason: 'Invalid public key' };
|
||||
if (!pub.validate())
|
||||
return { result: false, reason: 'Public key is not a point' };
|
||||
if (!pub.mul(this.ec.curve.n).isInfinity())
|
||||
return { result: false, reason: 'Public key * N != O' };
|
||||
|
||||
return { result: true, reason: null };
|
||||
};
|
||||
|
||||
KeyPair.prototype.getPublic = function getPublic(compact, enc) {
|
||||
// compact is optional argument
|
||||
if (typeof compact === 'string') {
|
||||
enc = compact;
|
||||
compact = null;
|
||||
}
|
||||
|
||||
if (!this.pub)
|
||||
this.pub = this.ec.g.mul(this.priv);
|
||||
|
||||
if (!enc)
|
||||
return this.pub;
|
||||
|
||||
return this.pub.encode(enc, compact);
|
||||
};
|
||||
|
||||
KeyPair.prototype.getPrivate = function getPrivate(enc) {
|
||||
if (enc === 'hex')
|
||||
return this.priv.toString(16, 2);
|
||||
else
|
||||
return this.priv;
|
||||
};
|
||||
|
||||
KeyPair.prototype._importPrivate = function _importPrivate(key, enc) {
|
||||
this.priv = new BN(key, enc || 16);
|
||||
|
||||
// Ensure that the priv won't be bigger than n, otherwise we may fail
|
||||
// in fixed multiplication method
|
||||
this.priv = this.priv.umod(this.ec.curve.n);
|
||||
};
|
||||
|
||||
KeyPair.prototype._importPublic = function _importPublic(key, enc) {
|
||||
if (key.x || key.y) {
|
||||
// Montgomery points only have an `x` coordinate.
|
||||
// Weierstrass/Edwards points on the other hand have both `x` and
|
||||
// `y` coordinates.
|
||||
if (this.ec.curve.type === 'mont') {
|
||||
assert(key.x, 'Need x coordinate');
|
||||
} else if (this.ec.curve.type === 'short' ||
|
||||
this.ec.curve.type === 'edwards') {
|
||||
assert(key.x && key.y, 'Need both x and y coordinate');
|
||||
}
|
||||
this.pub = this.ec.curve.point(key.x, key.y);
|
||||
return;
|
||||
}
|
||||
this.pub = this.ec.curve.decodePoint(key, enc);
|
||||
};
|
||||
|
||||
// ECDH
|
||||
KeyPair.prototype.derive = function derive(pub) {
|
||||
return pub.mul(this.priv).getX();
|
||||
};
|
||||
|
||||
// ECDSA
|
||||
KeyPair.prototype.sign = function sign(msg, enc, options) {
|
||||
return this.ec.sign(msg, this, enc, options);
|
||||
};
|
||||
|
||||
KeyPair.prototype.verify = function verify(msg, signature) {
|
||||
return this.ec.verify(msg, signature, this);
|
||||
};
|
||||
|
||||
KeyPair.prototype.inspect = function inspect() {
|
||||
return '<Key priv: ' + (this.priv && this.priv.toString(16, 2)) +
|
||||
' pub: ' + (this.pub && this.pub.inspect()) + ' >';
|
||||
};
|
||||
166
hGameTest/node_modules/elliptic/lib/elliptic/ec/signature.js
generated
vendored
Normal file
166
hGameTest/node_modules/elliptic/lib/elliptic/ec/signature.js
generated
vendored
Normal file
@@ -0,0 +1,166 @@
|
||||
'use strict';
|
||||
|
||||
var BN = require('bn.js');
|
||||
|
||||
var utils = require('../utils');
|
||||
var assert = utils.assert;
|
||||
|
||||
function Signature(options, enc) {
|
||||
if (options instanceof Signature)
|
||||
return options;
|
||||
|
||||
if (this._importDER(options, enc))
|
||||
return;
|
||||
|
||||
assert(options.r && options.s, 'Signature without r or s');
|
||||
this.r = new BN(options.r, 16);
|
||||
this.s = new BN(options.s, 16);
|
||||
if (options.recoveryParam === undefined)
|
||||
this.recoveryParam = null;
|
||||
else
|
||||
this.recoveryParam = options.recoveryParam;
|
||||
}
|
||||
module.exports = Signature;
|
||||
|
||||
function Position() {
|
||||
this.place = 0;
|
||||
}
|
||||
|
||||
function getLength(buf, p) {
|
||||
var initial = buf[p.place++];
|
||||
if (!(initial & 0x80)) {
|
||||
return initial;
|
||||
}
|
||||
var octetLen = initial & 0xf;
|
||||
|
||||
// Indefinite length or overflow
|
||||
if (octetLen === 0 || octetLen > 4) {
|
||||
return false;
|
||||
}
|
||||
|
||||
var val = 0;
|
||||
for (var i = 0, off = p.place; i < octetLen; i++, off++) {
|
||||
val <<= 8;
|
||||
val |= buf[off];
|
||||
val >>>= 0;
|
||||
}
|
||||
|
||||
// Leading zeroes
|
||||
if (val <= 0x7f) {
|
||||
return false;
|
||||
}
|
||||
|
||||
p.place = off;
|
||||
return val;
|
||||
}
|
||||
|
||||
function rmPadding(buf) {
|
||||
var i = 0;
|
||||
var len = buf.length - 1;
|
||||
while (!buf[i] && !(buf[i + 1] & 0x80) && i < len) {
|
||||
i++;
|
||||
}
|
||||
if (i === 0) {
|
||||
return buf;
|
||||
}
|
||||
return buf.slice(i);
|
||||
}
|
||||
|
||||
Signature.prototype._importDER = function _importDER(data, enc) {
|
||||
data = utils.toArray(data, enc);
|
||||
var p = new Position();
|
||||
if (data[p.place++] !== 0x30) {
|
||||
return false;
|
||||
}
|
||||
var len = getLength(data, p);
|
||||
if (len === false) {
|
||||
return false;
|
||||
}
|
||||
if ((len + p.place) !== data.length) {
|
||||
return false;
|
||||
}
|
||||
if (data[p.place++] !== 0x02) {
|
||||
return false;
|
||||
}
|
||||
var rlen = getLength(data, p);
|
||||
if (rlen === false) {
|
||||
return false;
|
||||
}
|
||||
var r = data.slice(p.place, rlen + p.place);
|
||||
p.place += rlen;
|
||||
if (data[p.place++] !== 0x02) {
|
||||
return false;
|
||||
}
|
||||
var slen = getLength(data, p);
|
||||
if (slen === false) {
|
||||
return false;
|
||||
}
|
||||
if (data.length !== slen + p.place) {
|
||||
return false;
|
||||
}
|
||||
var s = data.slice(p.place, slen + p.place);
|
||||
if (r[0] === 0) {
|
||||
if (r[1] & 0x80) {
|
||||
r = r.slice(1);
|
||||
} else {
|
||||
// Leading zeroes
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (s[0] === 0) {
|
||||
if (s[1] & 0x80) {
|
||||
s = s.slice(1);
|
||||
} else {
|
||||
// Leading zeroes
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
this.r = new BN(r);
|
||||
this.s = new BN(s);
|
||||
this.recoveryParam = null;
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
function constructLength(arr, len) {
|
||||
if (len < 0x80) {
|
||||
arr.push(len);
|
||||
return;
|
||||
}
|
||||
var octets = 1 + (Math.log(len) / Math.LN2 >>> 3);
|
||||
arr.push(octets | 0x80);
|
||||
while (--octets) {
|
||||
arr.push((len >>> (octets << 3)) & 0xff);
|
||||
}
|
||||
arr.push(len);
|
||||
}
|
||||
|
||||
Signature.prototype.toDER = function toDER(enc) {
|
||||
var r = this.r.toArray();
|
||||
var s = this.s.toArray();
|
||||
|
||||
// Pad values
|
||||
if (r[0] & 0x80)
|
||||
r = [ 0 ].concat(r);
|
||||
// Pad values
|
||||
if (s[0] & 0x80)
|
||||
s = [ 0 ].concat(s);
|
||||
|
||||
r = rmPadding(r);
|
||||
s = rmPadding(s);
|
||||
|
||||
while (!s[0] && !(s[1] & 0x80)) {
|
||||
s = s.slice(1);
|
||||
}
|
||||
var arr = [ 0x02 ];
|
||||
constructLength(arr, r.length);
|
||||
arr = arr.concat(r);
|
||||
arr.push(0x02);
|
||||
constructLength(arr, s.length);
|
||||
var backHalf = arr.concat(s);
|
||||
var res = [ 0x30 ];
|
||||
constructLength(res, backHalf.length);
|
||||
res = res.concat(backHalf);
|
||||
return utils.encode(res, enc);
|
||||
};
|
||||
Reference in New Issue
Block a user