react / wstein / node_modules / browserify / node_modules / crypto-browserify / node_modules / browserify-sign / node_modules / elliptic / lib / elliptic / ec / key.js
80621 views'use strict';12var bn = require('bn.js');34var elliptic = require('../../elliptic');5var utils = elliptic.utils;67function KeyPair(ec, options) {8this.ec = ec;9this.priv = null;10this.pub = null;1112// KeyPair(ec, { priv: ..., pub: ... })13if (options.priv)14this._importPrivate(options.priv, options.privEnc);15if (options.pub)16this._importPublic(options.pub, options.pubEnc);17}18module.exports = KeyPair;1920KeyPair.fromPublic = function fromPublic(ec, pub, enc) {21if (pub instanceof KeyPair)22return pub;2324return new KeyPair(ec, {25pub: pub,26pubEnc: enc27});28};2930KeyPair.fromPrivate = function fromPrivate(ec, priv, enc) {31if (priv instanceof KeyPair)32return priv;3334return new KeyPair(ec, {35priv: priv,36privEnc: enc37});38};3940KeyPair.prototype.validate = function validate() {41var pub = this.getPublic();4243if (pub.isInfinity())44return { result: false, reason: 'Invalid public key' };45if (!pub.validate())46return { result: false, reason: 'Public key is not a point' };47if (!pub.mul(this.ec.curve.n).isInfinity())48return { result: false, reason: 'Public key * N != O' };4950return { result: true, reason: null };51};5253KeyPair.prototype.getPublic = function getPublic(compact, enc) {54if (!this.pub)55this.pub = this.ec.g.mul(this.priv);5657// compact is optional argument58if (typeof compact === 'string') {59enc = compact;60compact = null;61}6263if (!enc)64return this.pub;6566var len = this.ec.curve.p.byteLength();67var x = this.pub.getX().toArray();6869for (var i = x.length; i < len; i++)70x.unshift(0);7172var res;73if (this.ec.curve.type !== 'mont') {74if (compact) {75res = [ this.pub.getY().isEven() ? 0x02 : 0x03 ].concat(x);76} else {77var y = this.pub.getY().toArray();78for (var i = y.length; i < len; i++)79y.unshift(0);80var res = [ 0x04 ].concat(x, y);81}82} else {83res = x;84}8586return utils.encode(res, enc);87};8889KeyPair.prototype.getPrivate = function getPrivate(enc) {90if (enc === 'hex')91return this.priv.toString(16, 2);92else93return this.priv;94};9596KeyPair.prototype._importPrivate = function _importPrivate(key, enc) {97this.priv = new bn(key, enc || 16);9899// Ensure that the priv won't be bigger than n, otherwise we may fail100// in fixed multiplication method101this.priv = this.priv.mod(this.ec.curve.n);102};103104KeyPair.prototype._importPublic = function _importPublic(key, enc) {105if (key.x || key.y) {106this.pub = this.ec.curve.point(key.x, key.y);107return;108}109110key = utils.toArray(key, enc);111if (this.ec.curve.type !== 'mont')112return this._importPublicShort(key);113else114return this._importPublicMont(key);115};116117KeyPair.prototype._importPublicShort = function _importPublicShort(key) {118var len = this.ec.curve.p.byteLength();119if (key[0] === 0x04 && key.length - 1 === 2 * len) {120this.pub = this.ec.curve.point(121key.slice(1, 1 + len),122key.slice(1 + len, 1 + 2 * len));123} else if ((key[0] === 0x02 || key[0] === 0x03) && key.length - 1 === len) {124this.pub = this.ec.curve.pointFromX(key[0] === 0x03, key.slice(1, 1 + len));125}126};127128KeyPair.prototype._importPublicMont = function _importPublicMont(key) {129this.pub = this.ec.curve.point(key, 1);130};131132// ECDH133KeyPair.prototype.derive = function derive(pub) {134return pub.mul(this.priv).getX();135};136137// ECDSA138KeyPair.prototype.sign = function sign(msg) {139return this.ec.sign(msg, this);140};141142KeyPair.prototype.verify = function verify(msg, signature) {143return this.ec.verify(msg, signature, this);144};145146KeyPair.prototype.inspect = function inspect() {147return '<Key priv: ' + (this.priv && this.priv.toString(16, 2)) +148' pub: ' + (this.pub && this.pub.inspect()) + ' >';149};150151152