Socket
Socket
Sign inDemoInstall

secp256k1

Package Overview
Dependencies
Maintainers
4
Versions
59
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

secp256k1 - npm Package Compare versions

Comparing version 3.0.1 to 3.1.0

index.js

16

API.md

@@ -22,4 +22,4 @@ # API Reference (v3.x)

- [`.recover(Buffer message, Buffer signature, Number recovery [, Boolean compressed = true])`](#recoverbuffer-message-buffer-signature-number-recovery--boolean-compressed--true---buffer)
- [`.ecdh(Buffer publicKey, Buffer privateKey [, Object options])`](#ecdhbuffer-publickey-buffer-privatekey--object-options---buffer)
- [Option: `Function hashfn`](#option-function-hashfn)
- [`.ecdh(Buffer publicKey, Buffer privateKey)`](#ecdhbuffer-publickey-buffer-privatekey---buffer)
- [`.ecdhUnsafe(Buffer publicKey, Buffer privateKey [, Boolean compressed = true])`](#ecdhunsafebuffer-publickey-buffer-privatekey--boolean-compressed--true---buffer)

@@ -134,3 +134,3 @@ #####`.privateKeyVerify(Buffer privateKey)` -> `Boolean`

#####`.recover(Buffer message, Buffer signature, Number recovery [, Boolean compressed = true]` -> `Buffer`
#####`.recover(Buffer message, Buffer signature, Number recovery [, Boolean compressed = true])` -> `Buffer`

@@ -141,10 +141,10 @@ Recover an ECDSA public key from a signature.

#####`.ecdh(Buffer publicKey, Buffer privateKey [, Object options])` -> `Buffer`
#####`.ecdh(Buffer publicKey, Buffer privateKey)` -> `Buffer`
Compute an EC Diffie-Hellman secret.
Compute an EC Diffie-Hellman secret and applied sha256 to compressed public key.
######Option: `Function hashfn`
<hr>
Hash function that is applied to a point that is result of ecdh. By default it is sha256 that applied to compressed public key.
#####`.ecdhUnsafe(Buffer publicKey, Buffer privateKey [, Boolean compressed = true])` -> `Buffer`
Function signature: `hashfn(Buffer x, Buffer y)` -> `Buffer`
Compute an EC Diffie-Hellman secret and return public key as result.
'use strict'
try {
module.exports = require('bindings')('secp256k1')
} catch (err) {
module.exports = require('./elliptic')
}
module.exports = require('bindings')('secp256k1')
'use strict'
module.exports = require('./lib')(require('./lib/elliptic'))
'use strict'
module.exports = require('./lib')(require('./lib/js'))
'use strict'
var toString = Object.prototype.toString

@@ -7,35 +6,23 @@

exports.isArray = function (value, message) {
if (!Array.isArray(value)) {
throw TypeError(message)
}
if (!Array.isArray(value)) throw TypeError(message)
}
exports.isBoolean = function (value, message) {
if (toString.call(value) !== '[object Boolean]') {
throw TypeError(message)
}
if (toString.call(value) !== '[object Boolean]') throw TypeError(message)
}
exports.isBuffer = function (value, message) {
if (!Buffer.isBuffer(value)) {
throw TypeError(message)
}
if (!Buffer.isBuffer(value)) throw TypeError(message)
}
exports.isFunction = function (value, message) {
if (toString.call(value) !== '[object Function]') {
throw TypeError(message)
}
if (toString.call(value) !== '[object Function]') throw TypeError(message)
}
exports.isNumber = function (value, message) {
if (toString.call(value) !== '[object Number]') {
throw TypeError(message)
}
if (toString.call(value) !== '[object Number]') throw TypeError(message)
}
exports.isObject = function (value, message) {
if (toString.call(value) !== '[object Object]') {
throw TypeError(message)
}
if (toString.call(value) !== '[object Object]') throw TypeError(message)
}

@@ -45,23 +32,15 @@

exports.isBufferLength = function (buffer, length, message) {
if (buffer.length !== length) {
throw RangeError(message)
}
if (buffer.length !== length) throw RangeError(message)
}
exports.isBufferLength2 = function (buffer, length1, length2, message) {
if (buffer.length !== length1 && buffer.length !== length2) {
throw RangeError(message)
}
if (buffer.length !== length1 && buffer.length !== length2) throw RangeError(message)
}
exports.isLengthGTZero = function (value, message) {
if (value.length === 0) {
throw RangeError(message)
}
if (value.length === 0) throw RangeError(message)
}
exports.isNumberInInterval = function (number, x, y, message) {
if (number <= x || number >= y) {
throw RangeError(message)
}
if (number <= x || number >= y) throw RangeError(message)
}
'use strict'
var createHash = require('create-hash')
var BN = require('bn.js')

@@ -11,72 +11,61 @@ var EC = require('elliptic').ec

/**
* @param {Buffer} publicKey
* @return {?KeyPair}
*/
function pairFromPublicKey (publicKey) {
var x
var y
function loadCompressedPublicKey (first, xBuffer) {
var x = new BN(xBuffer)
var first = publicKey[0]
if (publicKey.length === 33 && (first === 0x02 || first === 0x03)) {
x = new BN(publicKey.slice(1, 33))
// overflow
if (x.cmp(ecparams.p) >= 0) return null
x = x.toRed(ecparams.red)
// overflow
if (x.cmp(ecparams.p) >= 0) {
return null
}
// compute corresponding Y
var y = x.redSqr().redIMul(x).redIAdd(ecparams.b).redSqrt()
if ((first === 0x03) !== y.isOdd()) y = y.redNeg()
x = x.toRed(ecparams.red)
y = x.redSqr().redIMul(x).redIAdd(ecparams.b).redSqrt()
if ((first === 0x03) !== y.isOdd()) {
y = y.redNeg()
}
} else if (publicKey.length === 65 && (first === 0x04 || first === 0x06 || first === 0x07)) {
x = new BN(publicKey.slice(1, 33))
y = new BN(publicKey.slice(33, 65))
return ec.keyPair({ pub: { x: x, y: y } })
}
// overflow
if (x.cmp(ecparams.p) >= 0 || y.cmp(ecparams.p) >= 0) {
return null
}
function loadUncompressedPublicKey (first, xBuffer, yBuffer) {
var x = new BN(xBuffer)
var y = new BN(yBuffer)
x = x.toRed(ecparams.red)
y = y.toRed(ecparams.red)
// overflow
if (x.cmp(ecparams.p) >= 0 || y.cmp(ecparams.p) >= 0) return null
// is odd flag
if ((first === 0x06 || first === 0x07) && y.isOdd() !== (first === 0x07)) {
return null
}
x = x.toRed(ecparams.red)
y = y.toRed(ecparams.red)
// x*x*x + b = y*y
var x3 = x.redSqr().redIMul(x)
if (!y.redSqr().redISub(x3.redIAdd(ecparams.b)).isZero()) {
// is odd flag
if ((first === 0x06 || first === 0x07) && y.isOdd() !== (first === 0x07)) return null
// x*x*x + b = y*y
var x3 = x.redSqr().redIMul(x)
if (!y.redSqr().redISub(x3.redIAdd(ecparams.b)).isZero()) return null
return ec.keyPair({ pub: { x: x, y: y } })
}
function loadPublicKey (publicKey) {
var first = publicKey[0]
switch (first) {
case 0x02:
case 0x03:
if (publicKey.length !== 33) return null
return loadCompressedPublicKey(first, publicKey.slice(1, 33))
case 0x04:
case 0x06:
case 0x07:
if (publicKey.length !== 65) return null
return loadUncompressedPublicKey(first, publicKey.slice(1, 33), publicKey.slice(33, 65))
default:
return null
}
} else {
return null
}
return ec.keyPair({pub: {x: x, y: y}})
}
/**
* @param {Buffer} privateKey
* @return {boolean}
*/
exports.privateKeyVerify = function (privateKey) {
var bn = new BN(privateKey)
return !(bn.cmp(ecparams.n) >= 0 || bn.isZero())
return bn.cmp(ecparams.n) < 0 && !bn.isZero()
}
/**
* @param {Buffer} privateKey
* @param {boolean} compressed
* @return {Buffer}
*/
exports.privateKeyExport = function (privateKey, compressed) {
var d = new BN(privateKey)
if (d.cmp(ecparams.n) >= 0 || d.isZero()) {
throw new Error(messages.EC_PRIVATE_KEY_EXPORT_DER_FAIL)
}
if (d.cmp(ecparams.n) >= 0 || d.isZero()) throw new Error(messages.EC_PRIVATE_KEY_EXPORT_DER_FAIL)

@@ -86,54 +75,26 @@ return new Buffer(ec.keyFromPrivate(privateKey).getPublic(compressed, true))

/**
* @param {Buffer} privateKey
* @param {Buffer} tweak
* @return {Buffer}
*/
exports.privateKeyTweakAdd = function (privateKey, tweak) {
var bn = new BN(tweak)
if (bn.cmp(ecparams.n) >= 0) {
throw new Error(messages.EC_PRIVATE_KEY_TWEAK_ADD_FAIL)
}
if (bn.cmp(ecparams.n) >= 0) throw new Error(messages.EC_PRIVATE_KEY_TWEAK_ADD_FAIL)
bn.iadd(new BN(privateKey))
if (bn.cmp(ecparams.n) >= 0) {
bn.isub(ecparams.n)
}
if (bn.cmp(ecparams.n) >= 0) bn.isub(ecparams.n)
if (bn.isZero()) throw new Error(messages.EC_PRIVATE_KEY_TWEAK_ADD_FAIL)
if (bn.isZero()) {
throw new Error(messages.EC_PRIVATE_KEY_TWEAK_ADD_FAIL)
}
return bn.toArrayLike(Buffer, null, 32)
return bn.toArrayLike(Buffer, 'be', 32)
}
/**
* @param {Buffer} privateKey
* @param {Buffer} tweak
* @return {Buffer}
*/
exports.privateKeyTweakMul = function (privateKey, tweak) {
var bn = new BN(tweak)
if (bn.cmp(ecparams.n) >= 0 || bn.isZero()) {
throw new Error(messages.EC_PRIVATE_KEY_TWEAK_MUL_FAIL)
}
if (bn.cmp(ecparams.n) >= 0 || bn.isZero()) throw new Error(messages.EC_PRIVATE_KEY_TWEAK_MUL_FAIL)
bn.imul(new BN(privateKey))
if (bn.cmp(ecparams.n)) {
bn = bn.umod(ecparams.n)
}
if (bn.cmp(ecparams.n)) bn = bn.umod(ecparams.n)
return bn.toArrayLike(Buffer, null, 32)
return bn.toArrayLike(Buffer, 'be', 32)
}
/**
* @param {Buffer} privateKey
* @param {boolean} compressed
* @return {Buffer}
*/
exports.publicKeyCreate = function (privateKey, compressed) {
var d = new BN(privateKey)
if (d.cmp(ecparams.n) >= 0 || d.isZero()) {
throw new Error(messages.EC_PUBLIC_KEY_CREATE_FAIL)
}
if (d.cmp(ecparams.n) >= 0 || d.isZero()) throw new Error(messages.EC_PUBLIC_KEY_CREATE_FAIL)

@@ -143,12 +104,5 @@ return new Buffer(ec.keyFromPrivate(privateKey).getPublic(compressed, true))

/**
* @param {Buffer} publicKey
* @param {boolean} compressed
* @return {Buffer}
*/
exports.publicKeyConvert = function (publicKey, compressed) {
var pair = pairFromPublicKey(publicKey)
if (pair === null) {
throw new Error(messages.EC_PUBLIC_KEY_PARSE_FAIL)
}
var pair = loadPublicKey(publicKey)
if (pair === null) throw new Error(messages.EC_PUBLIC_KEY_PARSE_FAIL)

@@ -158,26 +112,12 @@ return new Buffer(pair.getPublic(compressed, true))

/**
* @param {Buffer} publicKey
* @return {boolean}
*/
exports.publicKeyVerify = function (publicKey) {
return pairFromPublicKey(publicKey) !== null
return loadPublicKey(publicKey) !== null
}
/**
* @param {Buffer} publicKey
* @param {Buffer} tweak
* @param {boolean} compressed
* @return {Buffer}
*/
exports.publicKeyTweakAdd = function (publicKey, tweak, compressed) {
var pair = pairFromPublicKey(publicKey)
if (pair === null) {
throw new Error(messages.EC_PUBLIC_KEY_PARSE_FAIL)
}
var pair = loadPublicKey(publicKey)
if (pair === null) throw new Error(messages.EC_PUBLIC_KEY_PARSE_FAIL)
tweak = new BN(tweak)
if (tweak.cmp(ecparams.n) >= 0) {
throw new Error(messages.EC_PUBLIC_KEY_TWEAK_ADD_FAIL)
}
if (tweak.cmp(ecparams.n) >= 0) throw new Error(messages.EC_PUBLIC_KEY_TWEAK_ADD_FAIL)

@@ -187,18 +127,8 @@ return new Buffer(ecparams.g.mul(tweak).add(pair.pub).encode(true, compressed))

/**
* @param {Buffer} publicKey
* @param {Buffer} tweak
* @param {boolean} compressed
* @return {Buffer}
*/
exports.publicKeyTweakMul = function (publicKey, tweak, compressed) {
var pair = pairFromPublicKey(publicKey)
if (pair === null) {
throw new Error(messages.EC_PUBLIC_KEY_PARSE_FAIL)
}
var pair = loadPublicKey(publicKey)
if (pair === null) throw new Error(messages.EC_PUBLIC_KEY_PARSE_FAIL)
tweak = new BN(tweak)
if (tweak.cmp(ecparams.n) >= 0 || tweak.isZero()) {
throw new Error(messages.EC_PUBLIC_KEY_TWEAK_MUL_FAIL)
}
if (tweak.cmp(ecparams.n) >= 0 || tweak.isZero()) throw new Error(messages.EC_PUBLIC_KEY_TWEAK_MUL_FAIL)

@@ -208,43 +138,23 @@ return new Buffer(pair.pub.mul(tweak).encode(true, compressed))

/**
* @param {Buffer[]} publicKeys
* @param {boolean} compressed
* @return {Buffer}
*/
exports.publicKeyCombine = function (publicKeys, compressed) {
var pairs = new Array(publicKeys.length)
for (var i = 0; i < publicKeys.length; ++i) {
pairs[i] = pairFromPublicKey(publicKeys[i])
if (pairs[i] === null) {
throw new Error(messages.EC_PUBLIC_KEY_PARSE_FAIL)
}
pairs[i] = loadPublicKey(publicKeys[i])
if (pairs[i] === null) throw new Error(messages.EC_PUBLIC_KEY_PARSE_FAIL)
}
var point = pairs[0].pub
for (var j = 1; j < pairs.length; ++j) {
point = point.add(pairs[j].pub)
}
for (var j = 1; j < pairs.length; ++j) point = point.add(pairs[j].pub)
if (point.isInfinity()) throw new Error(messages.EC_PUBLIC_KEY_COMBINE_FAIL)
if (point.isInfinity()) {
throw new Error(messages.EC_PUBLIC_KEY_COMBINE_FAIL)
}
return new Buffer(point.encode(true, compressed))
}
/**
* @param {Buffer} signature
* @return {Buffer}
*/
exports.signatureNormalize = function (signature) {
var r = new BN(signature.slice(0, 32))
var s = new BN(signature.slice(32, 64))
if (r.cmp(ecparams.n) >= 0 || s.cmp(ecparams.n) >= 0) {
throw new Error(messages.ECDSA_SIGNATURE_PARSE_FAIL)
}
if (r.cmp(ecparams.n) >= 0 || s.cmp(ecparams.n) >= 0) throw new Error(messages.ECDSA_SIGNATURE_PARSE_FAIL)
var result = new Buffer(signature)
if (s.cmp(ec.nh) === 1) {
new Buffer(ecparams.n.sub(s).toArray(null, 32)).copy(result, 32)
}
if (s.cmp(ec.nh) === 1) ecparams.n.sub(s).toArrayLike(Buffer, 'be', 32).copy(result, 32)

@@ -254,41 +164,23 @@ return result

/**
* @param {Buffer} signature
* @return {{r: Buffer, s: Buffer}}
*/
exports.signatureExport = function (signature) {
var r = signature.slice(0, 32)
var s = signature.slice(32, 64)
if (new BN(r).cmp(ecparams.n) >= 0 || new BN(s).cmp(ecparams.n) >= 0) {
throw new Error(messages.ECDSA_SIGNATURE_PARSE_FAIL)
}
if (new BN(r).cmp(ecparams.n) >= 0 || new BN(s).cmp(ecparams.n) >= 0) throw new Error(messages.ECDSA_SIGNATURE_PARSE_FAIL)
return {r: r, s: s}
return { r: r, s: s }
}
/**
* @param {{r: Buffer, s: Buffer}} sigObj
* @return {Buffer}
*/
exports.signatureImport = function (sigObj) {
var r = new BN(sigObj.r)
if (r.cmp(ecparams.n) >= 0) {
r = new BN(0)
}
if (r.cmp(ecparams.n) >= 0) r = new BN(0)
var s = new BN(sigObj.s)
if (s.cmp(ecparams.n) >= 0) {
s = new BN(0)
}
if (s.cmp(ecparams.n) >= 0) s = new BN(0)
return new Buffer(r.toArray(null, 32).concat(s.toArray(null, 32)))
return Buffer.concat([
r.toArrayLike(Buffer, 'be', 32),
s.toArrayLike(Buffer, 'be', 32)
])
}
/**
* @param {Buffer} message
* @param {Buffer} privateKey
* @param {?sign~noncefn} noncefn
* @param {?Buffer} data
* @return {{signature: Buffer, recovery: number}}
*/
exports.sign = function (message, privateKey, noncefn, data) {

@@ -299,5 +191,3 @@ if (typeof noncefn === 'function') {

var nonce = getNonce(message, privateKey, null, data, counter)
if (!Buffer.isBuffer(nonce) || nonce.length !== 32) {
throw new Error(messages.ECDSA_SIGN_FAIL)
}
if (!Buffer.isBuffer(nonce) || nonce.length !== 32) throw new Error(messages.ECDSA_SIGN_FAIL)

@@ -309,9 +199,10 @@ return new BN(nonce)

var d = new BN(privateKey)
if (d.cmp(ecparams.n) >= 0 || d.isZero()) {
throw new Error(messages.ECDSA_SIGN_FAIL)
}
if (d.cmp(ecparams.n) >= 0 || d.isZero()) throw new Error(messages.ECDSA_SIGN_FAIL)
var result = ec.sign(message, privateKey, {canonical: true, k: noncefn, pers: data})
var result = ec.sign(message, privateKey, { canonical: true, k: noncefn, pers: data })
return {
signature: new Buffer(result.r.toArray(null, 32).concat(result.s.toArray(null, 32))),
signature: Buffer.concat([
result.r.toArrayLike(Buffer, 'be', 32),
result.s.toArrayLike(Buffer, 'be', 32)
]),
recovery: result.recoveryParam

@@ -321,8 +212,2 @@ }

/**
* @param {Buffer} message
* @param {Buffer} signature
* @param {Buffer} publicKey
* @return {boolean}
*/
exports.verify = function (message, signature, publicKey) {

@@ -333,25 +218,11 @@ var sigObj = {r: signature.slice(0, 32), s: signature.slice(32, 64)}

var sigs = new BN(sigObj.s)
if (sigr.cmp(ecparams.n) >= 0 || sigs.cmp(ecparams.n) >= 0) {
throw new Error(messages.ECDSA_SIGNATURE_PARSE_FAIL)
}
if (sigr.cmp(ecparams.n) >= 0 || sigs.cmp(ecparams.n) >= 0) throw new Error(messages.ECDSA_SIGNATURE_PARSE_FAIL)
if (sigs.cmp(ec.nh) === 1 || sigr.isZero() || sigs.isZero()) return false
if (sigs.cmp(ec.nh) === 1 || sigr.isZero() || sigs.isZero()) {
return false
}
var pair = loadPublicKey(publicKey)
if (pair === null) throw new Error(messages.EC_PUBLIC_KEY_PARSE_FAIL)
var pair = pairFromPublicKey(publicKey)
if (pair === null) {
throw new Error(messages.EC_PUBLIC_KEY_PARSE_FAIL)
}
return ec.verify(message, sigObj, {x: pair.pub.x, y: pair.pub.y})
}
/**
* @param {Buffer} message
* @param {Buffer} signature
* @param {number} recovery
* @param {boolean} compressed
* @return {Buffer}
*/
exports.recover = function (message, signature, recovery, compressed) {

@@ -362,10 +233,6 @@ var sigObj = {r: signature.slice(0, 32), s: signature.slice(32, 64)}

var sigs = new BN(sigObj.s)
if (sigr.cmp(ecparams.n) >= 0 || sigs.cmp(ecparams.n) >= 0) {
throw new Error(messages.ECDSA_SIGNATURE_PARSE_FAIL)
}
if (sigr.cmp(ecparams.n) >= 0 || sigs.cmp(ecparams.n) >= 0) throw new Error(messages.ECDSA_SIGNATURE_PARSE_FAIL)
try {
if (sigr.isZero() || sigs.isZero()) {
throw new Error()
}
if (sigr.isZero() || sigs.isZero()) throw new Error()

@@ -379,8 +246,15 @@ var point = ec.recoverPubKey(message, sigObj, recovery)

/**
* @param {Buffer} publicKey
* @param {Buffer} privateKey
* @return {Buffer}
*/
exports.ecdh = function (publicKey, privateKey) {
var shared = exports.ecdhUnsafe(publicKey, privateKey, true)
return createHash('sha256').update(shared).digest()
}
exports.ecdhUnsafe = function (publicKey, privateKey, compressed) {
var pair = loadPublicKey(publicKey)
if (pair === null) throw new Error(messages.EC_PUBLIC_KEY_PARSE_FAIL)
var scalar = new BN(privateKey)
if (scalar.cmp(ecparams.n) >= 0 || scalar.isZero()) throw new Error(messages.ECDH_FAIL)
return new Buffer(pair.pub.mul(scalar).encode(true, compressed))
}
'use strict'
var bip66 = require('bip66')

@@ -15,11 +16,6 @@ var assert = require('./assert')

/**
* @param {*} value
* @param {boolean} defaultValue
* @return {boolean}
*/
var ZERO_BUFFER_32 = new Buffer('0000000000000000000000000000000000000000000000000000000000000000', 'hex')
function initCompressedValue (value, defaultValue) {
if (value === undefined) {
return defaultValue
}
if (value === undefined) return defaultValue

@@ -30,48 +26,4 @@ assert.isBoolean(value, messages.COMPRESSED_TYPE_INVALID)

/**
* @param {Object} o
* @param {Buffer} o.sig
* @param {number} o.idx
* @return {?Buffer}
*/
function parseScalar (o) {
if (o.sig[o.idx++] !== 0x02) {
return null
}
var rlen = o.sig[o.idx++]
if (rlen === 0 || rlen > 33 ||
(rlen > 1 &&
(o.sig[o.idx] === 0x00 && o.sig[o.idx + 1] < 0x80) ||
(o.sig[o.idx] === 0xff && o.sig[o.idx + 1] >= 0x80))) {
return null
}
if (o.sig[o.idx] === 0 && rlen === 33) {
o.idx += 1
rlen -= 1
}
o.idx += rlen
var result = new Buffer(32)
result.fill(0)
if (rlen <= 32) {
o.sig.slice(o.idx - rlen, o.idx).copy(result, 32 - rlen)
}
return result
}
/**
* @param {Object} secp256k1
* @return {Object}
*/
module.exports = function (secp256k1) {
return {
/**
* @param {Buffer} privateKey
* @return {boolean}
*/
privateKeyVerify: function (privateKey) {

@@ -82,7 +34,2 @@ assert.isBuffer(privateKey, messages.EC_PRIVATE_KEY_TYPE_INVALID)

/**
* @param {Buffer} privateKey
* @param {boolean} [compressed=true]
* @return {Buffer}
*/
privateKeyExport: function (privateKey, compressed) {

@@ -125,6 +72,2 @@ assert.isBuffer(privateKey, messages.EC_PRIVATE_KEY_TYPE_INVALID)

/**
* @param {Buffer} privateKey
* @return {Buffer}
*/
privateKeyImport: function (privateKey) {

@@ -138,20 +81,12 @@ assert.isBuffer(privateKey, messages.EC_PRIVATE_KEY_TYPE_INVALID)

var index = 0
if (length < index + 1 || privateKey[index] !== 0x30) {
break
}
if (length < index + 1 || privateKey[index] !== 0x30) break
index += 1
// sequence length constructor
if (length < index + 1 || !(privateKey[index] & 0x80)) {
break
}
if (length < index + 1 || !(privateKey[index] & 0x80)) break
var lenb = privateKey[index] & 0x7f
index += 1
if (lenb < 1 || lenb > 2) {
break
}
if (length < index + lenb) {
break
}
if (lenb < 1 || lenb > 2) break
if (length < index + lenb) break

@@ -161,5 +96,3 @@ // sequence length

index += lenb
if (length < index + len) {
break
}
if (length < index + len) break

@@ -184,5 +117,3 @@ // sequence element 0: version number (=1)

privateKey = privateKey.slice(index + 2, index + 2 + privateKey[index + 1])
if (privateKey.length === 32 && secp256k1.privateKeyVerify(privateKey)) {
return privateKey
}
if (privateKey.length === 32 && secp256k1.privateKeyVerify(privateKey)) return privateKey
} while (false)

@@ -193,7 +124,2 @@

/**
* @param {Buffer} privateKey
* @param {Buffer} tweak
* @return {Buffer}
*/
privateKeyTweakAdd: function (privateKey, tweak) {

@@ -209,7 +135,2 @@ assert.isBuffer(privateKey, messages.EC_PRIVATE_KEY_TYPE_INVALID)

/**
* @param {Buffer} privateKey
* @param {Buffer} tweak
* @return {Buffer}
*/
privateKeyTweakMul: function (privateKey, tweak) {

@@ -225,7 +146,2 @@ assert.isBuffer(privateKey, messages.EC_PRIVATE_KEY_TYPE_INVALID)

/**
* @param {Buffer} privateKey
* @param {boolean} [compressed=true]
* @return {Buffer}
*/
publicKeyCreate: function (privateKey, compressed) {

@@ -240,7 +156,2 @@ assert.isBuffer(privateKey, messages.EC_PRIVATE_KEY_TYPE_INVALID)

/**
* @param {Buffer} publicKey
* @param {boolean} [compressed=true]
* @return {Buffer}
*/
publicKeyConvert: function (publicKey, compressed) {

@@ -255,6 +166,2 @@ assert.isBuffer(publicKey, messages.EC_PUBLIC_KEY_TYPE_INVALID)

/**
* @param {Buffer} publicKey
* @return {boolean}
*/
publicKeyVerify: function (publicKey) {

@@ -265,8 +172,2 @@ assert.isBuffer(publicKey, messages.EC_PUBLIC_KEY_TYPE_INVALID)

/**
* @param {Buffer} publicKey
* @param {Buffer} tweak
* @param {boolean} [compressed=true]
* @return {Buffer}
*/
publicKeyTweakAdd: function (publicKey, tweak, compressed) {

@@ -284,8 +185,2 @@ assert.isBuffer(publicKey, messages.EC_PUBLIC_KEY_TYPE_INVALID)

/**
* @param {Buffer} publicKey
* @param {Buffer} tweak
* @param {boolean} [compressed=true]
* @return {Buffer}
*/
publicKeyTweakMul: function (publicKey, tweak, compressed) {

@@ -303,7 +198,2 @@ assert.isBuffer(publicKey, messages.EC_PUBLIC_KEY_TYPE_INVALID)

/**
* @param {Buffer[]} publicKeys
* @param {boolean} [compressed=true]
* @return {Buffer}
*/
publicKeyCombine: function (publicKeys, compressed) {

@@ -322,6 +212,2 @@ assert.isArray(publicKeys, messages.EC_PUBLIC_KEYS_TYPE_INVALID)

/**
* @param {Buffer} signature
* @return {Buffer}
*/
signatureNormalize: function (signature) {

@@ -334,6 +220,2 @@ assert.isBuffer(signature, messages.ECDSA_SIGNATURE_TYPE_INVALID)

/**
* @param {Buffer} signature
* @return {Buffer}
*/
signatureExport: function (signature) {

@@ -344,87 +226,32 @@ assert.isBuffer(signature, messages.ECDSA_SIGNATURE_TYPE_INVALID)

var sigObj = secp256k1.signatureExport(signature)
var r = Buffer.concat([new Buffer([0]), sigObj.r])
for (var lenR = 33, posR = 0; lenR > 1 && r[posR] === 0x00 && !(r[posR + 1] & 0x80); --lenR, ++posR);
var s = Buffer.concat([new Buffer([0]), sigObj.s])
for (var lenS = 33, posS = 0; lenS > 1 && s[posS] === 0x00 && !(s[posS + 1] & 0x80); --lenS, ++posS);
var lenR = 33
var posR = 0
while (lenR > 1 && r[posR] === 0x00 && r[posR + 1] < 0x80) {
--lenR
++posR
}
var lenS = 33
var posS = 0
while (lenS > 1 && s[posS] === 0x00 && s[posS + 1] < 0x80) {
--lenS
++posS
}
var result = new Buffer(lenR + lenS + 6)
result[0] = 0x30
result[1] = 4 + lenR + lenS
result[2] = 0x02
result[3] = lenR
r.copy(result, 4, posR)
result[lenR + 4] = 0x02
result[lenR + 5] = lenS
s.copy(result, lenR + 6, posS)
return result
return bip66.encode(r.slice(posR), s.slice(posS))
},
/**
* @param {Buffer} signature
* @return {Buffer}
*/
signatureImport: function (signature) {
assert.isBuffer(signature, messages.ECDSA_SIGNATURE_TYPE_INVALID)
assert.isLengthGTZero(signature, messages.ECDSA_SIGNATURE_LENGTH_INVALID)
signatureImport: function (sig) {
assert.isBuffer(sig, messages.ECDSA_SIGNATURE_TYPE_INVALID)
assert.isLengthGTZero(sig, messages.ECDSA_SIGNATURE_LENGTH_INVALID)
do {
var o = {sig: signature, idx: 0}
if ((o.sig.length < o.idx || o.sig[o.idx] !== 0x30) ||
(o.sig.length < o.idx + 1 || o.sig[o.idx + 1] > 0x80) ||
o.idx + o.sig[o.idx + 1] + 2 !== o.sig.length) {
break
}
o.idx += 2
try {
var sigObj = bip66.decode(sig)
if (sigObj.r.length === 33 && sigObj.r[0] === 0x00) sigObj.r = sigObj.r.slice(1)
if (sigObj.r.length > 32) throw new Error('R length is too long')
if (sigObj.s.length === 33 && sigObj.s[0] === 0x00) sigObj.s = sigObj.s.slice(1)
if (sigObj.s.length > 32) throw new Error('S length is too long')
} catch (err) {
throw new Error(messages.ECDSA_SIGNATURE_PARSE_DER_FAIL)
}
var r = parseScalar(o)
if (r === null) {
break
}
var s = parseScalar(o)
if (s === null) {
break
}
return secp256k1.signatureImport({r: r, s: s})
} while (false)
throw new Error(messages.ECDSA_SIGNATURE_PARSE_DER_FAIL)
return secp256k1.signatureImport({
r: Buffer.concat([ZERO_BUFFER_32, sigObj.r]).slice(-32),
s: Buffer.concat([ZERO_BUFFER_32, sigObj.s]).slice(-32)
})
},
/**
* @callback sign~noncefn
* @param {Buffer} message
* @param {Buffer} privateKey
* @param {?Buffer} algo
* @param {?Buffer} data
* @param {number} attempt
* @return {Buffer}
*/
/**
* @typedef {Object} sign~options
* @param {Buffer} [data]
* @param {sign~noncefn} [noncefn=nonce_function_rfc6979]
*/
/**
* @param {Buffer} message
* @param {Buffer} privateKey
* @param {sign~options} [options]
* @return {{signature: Buffer, recovery: number}}
*/
sign: function (message, privateKey, options) {

@@ -457,8 +284,2 @@ assert.isBuffer(message, messages.MSG32_TYPE_INVALID)

/**
* @param {Buffer} message
* @param {Buffer} signature
* @param {Buffer} publicKey
* @return {boolean}
*/
verify: function (message, signature, publicKey) {

@@ -477,9 +298,2 @@ assert.isBuffer(message, messages.MSG32_TYPE_INVALID)

/**
* @param {Buffer} message
* @param {Buffer} signature
* @param {number} recovery
* @param {boolean} [compressed=true]
* @return {Buffer}
*/
recover: function (message, signature, recovery, compressed) {

@@ -500,9 +314,3 @@ assert.isBuffer(message, messages.MSG32_TYPE_INVALID)

/**
* @param {Buffer} publicKey
* @param {Buffer} privateKey
* @param {?} options
* @return {Buffer}
*/
ecdh: function (publicKey, privateKey, options) {
ecdh: function (publicKey, privateKey) {
assert.isBuffer(publicKey, messages.EC_PUBLIC_KEY_TYPE_INVALID)

@@ -514,7 +322,17 @@ assert.isBufferLength2(publicKey, 33, 65, messages.EC_PUBLIC_KEY_LENGTH_INVALID)

if (options !== undefined) {
assert.isObject(options, messages.OPTIONS_TYPE_INVALID)
}
return secp256k1.ecdh(publicKey, privateKey)
},
ecdhUnsafe: function (publicKey, privateKey, compressed) {
assert.isBuffer(publicKey, messages.EC_PUBLIC_KEY_TYPE_INVALID)
assert.isBufferLength2(publicKey, 33, 65, messages.EC_PUBLIC_KEY_LENGTH_INVALID)
assert.isBuffer(privateKey, messages.EC_PRIVATE_KEY_TYPE_INVALID)
assert.isBufferLength(privateKey, 32, messages.EC_PRIVATE_KEY_LENGTH_INVALID)
compressed = initCompressedValue(compressed, true)
return secp256k1.ecdhUnsafe(publicKey, privateKey, compressed)
}
}
}
'use strict'
var optimized = require('./optimized')
/**
* @class BN
*/
function BN () {

@@ -14,6 +10,2 @@ this.negative = 0

/**
* @param {number} n
* @return {BN}
*/
BN.fromNumber = function (n) {

@@ -26,6 +18,2 @@ var bn = new BN()

/**
* @param {Buffer} b32
* @return {BN}
*/
BN.fromBuffer = function (b32) {

@@ -52,10 +40,5 @@ var bn = new BN()

/**
* @return {Buffer}
*/
BN.prototype.toBuffer = function () {
var w = this.words
for (var i = this.length; i < 10; ++i) {
w[i] = 0
}
for (var i = this.length; i < 10; ++i) w[i] = 0

@@ -78,11 +61,6 @@ return new Buffer([

/**
* @return {BN}
*/
BN.prototype.clone = function () {
var r = new BN()
r.words = new Array(this.length)
for (var i = 0; i < this.length; i++) {
r.words[i] = this.words[i]
}
for (var i = 0; i < this.length; i++) r.words[i] = this.words[i]
r.length = this.length

@@ -93,28 +71,13 @@ r.negative = this.negative

/**
* @return {BN}
*/
BN.prototype.strip = function () {
while (this.length > 1 && (this.words[this.length - 1] | 0) === 0) {
this.length--
}
while (this.length > 1 && (this.words[this.length - 1] | 0) === 0) this.length--
return this
}
/**
* @return {BN}
*/
BN.prototype.normSign = function () {
// -0 = 0
if (this.length === 1 && this.words[0] === 0) {
this.negative = 0
}
if (this.length === 1 && this.words[0] === 0) this.negative = 0
return this
}
/**
* @return {boolean}
*/
BN.prototype.isEven = function () {

@@ -124,5 +87,2 @@ return (this.words[0] & 1) === 0

/**
* @return {boolean}
*/
BN.prototype.isOdd = function () {

@@ -132,5 +92,2 @@ return (this.words[0] & 1) === 1

/**
* @return {boolean}
*/
BN.prototype.isZero = function () {

@@ -140,15 +97,7 @@ return this.length === 1 && this.words[0] === 0

/**
* @param {BN} num
* @return {number}
*/
BN.prototype.ucmp = function (num) {
if (this.length !== num.length) {
return this.length > num.length ? 1 : -1
}
if (this.length !== num.length) return this.length > num.length ? 1 : -1
for (var i = this.length - 1; i >= 0; --i) {
if (this.words[i] !== num.words[i]) {
return this.words[i] > num.words[i] ? 1 : -1
}
if (this.words[i] !== num.words[i]) return this.words[i] > num.words[i] ? 1 : -1
}

@@ -159,5 +108,2 @@

/**
* @return {boolean}
*/
BN.prototype.gtOne = function () {

@@ -167,5 +113,2 @@ return this.length > 1 || this.words[0] > 1

/**
* @return {boolean}
*/
BN.prototype.isOverflow = function () {

@@ -175,5 +118,2 @@ return this.ucmp(BN.n) >= 0

/**
* @return {boolean}
*/
BN.prototype.isHigh = function () {

@@ -183,5 +123,2 @@ return this.ucmp(BN.nh) === 1

/**
* @return {boolean}
*/
BN.prototype.bitLengthGT256 = function () {

@@ -191,6 +128,2 @@ return this.length > 10 || (this.length === 10 && this.words[9] > 0x003fffff)

/**
* @param {number} num
* @return {BN}
*/
BN.prototype.iuaddn = function (num) {

@@ -212,6 +145,2 @@ this.words[0] += num

/**
* @param {BN} num
* @return {BN}
*/
BN.prototype.iadd = function (num) {

@@ -269,6 +198,2 @@ // (-this) + num -> -(this - num)

/**
* @param {BN} num
* @return {BN}
*/
BN.prototype.add = function (num) {

@@ -278,6 +203,2 @@ return this.clone().iadd(num)

/**
* @param {BN} num
* @return {BN}
*/
BN.prototype.isub = function (num) {

@@ -332,5 +253,3 @@ // (-this) - num -> -(this + num)

if (carry === 0 && i < a.length && a !== this) {
for (; i < a.length; ++i) {
this.words[i] = a.words[i]
}
for (; i < a.length; ++i) this.words[i] = a.words[i]
}

@@ -340,5 +259,3 @@

if (a !== this) {
this.negative ^= 1
}
if (a !== this) this.negative ^= 1

@@ -348,6 +265,2 @@ return this.strip().normSign()

/**
* @param {BN} num
* @return {BN}
*/
BN.prototype.sub = function (num) {

@@ -357,7 +270,2 @@ return this.clone().isub(num)

/**
* @param {BN} num1
* @param {BN} num2
* @param {BN} out
*/
BN.umulTo = function (num1, num2, out) {

@@ -388,5 +296,3 @@ out.length = num1.length + num2.length - 1

if (carry !== 0) {
out.words[out.length++] = carry
}
if (carry !== 0) out.words[out.length++] = carry

@@ -398,7 +304,2 @@ return out.strip()

/**
* @param {BN} num
* @param {number} k
* @param {BN} out
*/
BN.umulnTo = function (num, k, out) {

@@ -427,6 +328,2 @@ if (k === 0) {

/**
* @param {BN} num
* @return {BN}
*/
BN.prototype.umul = function (num) {

@@ -447,11 +344,5 @@ var out = new BN()

/**
* @param {BN} output
* @return {BN}
*/
BN.prototype.isplit = function (output) {
output.length = Math.min(this.length, 9)
for (var i = 0; i < output.length; ++i) {
output.words[i] = this.words[i]
}
for (var i = 0; i < output.length; ++i) output.words[i] = this.words[i]

@@ -485,16 +376,7 @@ if (this.length <= 9) {

/**
* @return {BN}
*/
BN.prototype.fireduce = function () {
if (this.isOverflow()) {
this.isub(BN.n)
}
if (this.isOverflow()) this.isub(BN.n)
return this
}
/**
* @return {BN}
*/
BN.prototype.ureduce = function () {

@@ -504,5 +386,3 @@ var num = this.clone().isplit(BN.tmp).umul(BN.nc).iadd(BN.tmp)

num = num.isplit(BN.tmp).umul(BN.nc).iadd(BN.tmp)
if (num.bitLengthGT256()) {
num = num.isplit(BN.tmp).umul(BN.nc).iadd(BN.tmp)
}
if (num.bitLengthGT256()) num = num.isplit(BN.tmp).umul(BN.nc).iadd(BN.tmp)
}

@@ -513,6 +393,2 @@

/**
* @param {number} n
* @return {BN}
*/
BN.prototype.ishrn = function (n) {

@@ -528,5 +404,3 @@ var mask = (1 << n) - 1

if (this.length > 1 && this.words[this.length - 1] === 0) {
this.length -= 1
}
if (this.length > 1 && this.words[this.length - 1] === 0) this.length -= 1

@@ -536,5 +410,2 @@ return this

/**
* @return {BN}
*/
BN.prototype.uinvm = function () {

@@ -611,5 +482,2 @@ var x = this.clone()

/**
* @return {BN}
*/
BN.prototype.imulK = function () {

@@ -629,5 +497,3 @@ this.words[this.length] = 0

this.length -= 1
if (this.words[this.length - 1] === 0) {
this.length -= 1
}
if (this.words[this.length - 1] === 0) this.length -= 1
}

@@ -638,10 +504,5 @@

/**
* @return {BN}
*/
BN.prototype.redIReduce = function () {
this.isplit(BN.tmp).imulK().iadd(BN.tmp)
if (this.bitLengthGT256()) {
this.isplit(BN.tmp).imulK().iadd(BN.tmp)
}
if (this.bitLengthGT256()) this.isplit(BN.tmp).imulK().iadd(BN.tmp)

@@ -661,9 +522,4 @@ var cmp = this.ucmp(BN.p)

/**
* @return {BN}
*/
BN.prototype.redNeg = function () {
if (this.isZero()) {
return BN.fromNumber(0)
}
if (this.isZero()) return BN.fromNumber(0)

@@ -673,6 +529,2 @@ return BN.p.sub(this)

/**
* @param {BN} num
* @return {BN}
*/
BN.prototype.redAdd = function (num) {

@@ -682,11 +534,5 @@ return this.clone().redIAdd(num)

/**
* @param {BN} num
* @return {BN}
*/
BN.prototype.redIAdd = function (num) {
this.iadd(num)
if (this.ucmp(BN.p) >= 0) {
this.isub(BN.p)
}
if (this.ucmp(BN.p) >= 0) this.isub(BN.p)

@@ -696,10 +542,5 @@ return this

/**
* @return {BN}
*/
BN.prototype.redIAdd7 = function () {
this.iuaddn(7)
if (this.ucmp(BN.p) >= 0) {
this.isub(BN.p)
}
if (this.ucmp(BN.p) >= 0) this.isub(BN.p)

@@ -709,6 +550,2 @@ return this

/**
* @param {BN} num
* @return {BN}
*/
BN.prototype.redSub = function (num) {

@@ -718,11 +555,5 @@ return this.clone().redISub(num)

/**
* @param {BN} num
* @return {BN}
*/
BN.prototype.redISub = function (num) {
this.isub(num)
if (this.negative !== 0) {
this.iadd(BN.p)
}
if (this.negative !== 0) this.iadd(BN.p)

@@ -732,6 +563,2 @@ return this

/**
* @param {BN} num
* @return {BN}
*/
BN.prototype.redMul = function (num) {

@@ -741,5 +568,2 @@ return this.umul(num).redIReduce()

/**
* @return {BN}
*/
BN.prototype.redSqr = function () {

@@ -749,9 +573,4 @@ return this.umul(this).redIReduce()

/**
* @return {?BN}
*/
BN.prototype.redSqrt = function () {
if (this.isZero()) {
return this.clone()
}
if (this.isZero()) return this.clone()

@@ -765,9 +584,5 @@ var wv2 = this.redSqr()

var out = wv15
for (var i = 0; i < 54; ++i) {
out = out.redSqr().redSqr().redSqr().redSqr().redMul(wv15)
}
for (var i = 0; i < 54; ++i) out = out.redSqr().redSqr().redSqr().redSqr().redMul(wv15)
out = out.redSqr().redSqr().redSqr().redSqr().redMul(wv14)
for (i = 0; i < 5; ++i) {
out = out.redSqr().redSqr().redSqr().redSqr().redMul(wv15)
}
for (i = 0; i < 5; ++i) out = out.redSqr().redSqr().redSqr().redSqr().redMul(wv15)
out = out.redSqr().redSqr().redSqr().redSqr().redMul(wv12)

@@ -783,5 +598,2 @@ out = out.redSqr().redSqr().redSqr().redSqr().redSqr().redSqr().redMul(wv12)

/**
* @return {BN}
*/
BN.prototype.redInvm = function () {

@@ -799,6 +611,3 @@ var a = this.clone()

while (i-- > 0) {
if (x1.isOdd()) {
x1.iadd(BN.p)
}
if (x1.isOdd()) x1.iadd(BN.p)
x1.ishrn(1)

@@ -812,6 +621,3 @@ }

while (j-- > 0) {
if (x2.isOdd()) {
x2.iadd(BN.p)
}
if (x2.isOdd()) x2.iadd(BN.p)
x2.ishrn(1)

@@ -837,5 +643,3 @@ }

if (res.negative !== 0) {
res.iadd(BN.p)
}
if (res.negative !== 0) res.iadd(BN.p)

@@ -850,7 +654,2 @@ if (res.negative !== 0) {

/**
* Represent big number in a w-NAF form
* @param {number} w
* @return {number[]}
*/
BN.prototype.getNAF = function (w) {

@@ -864,5 +663,3 @@ var naf = []

while (!k.isZero()) {
for (var i = 0, m = 1; (k.words[0] & m) === 0 && i < 26; ++i, m <<= 1) {
naf.push(0)
}
for (var i = 0, m = 1; (k.words[0] & m) === 0 && i < 26; ++i, m <<= 1) naf.push(0)

@@ -880,6 +677,3 @@ if (i !== 0) {

if (!k.isZero()) {
for (i = w - 1; i > 0; --i) {
naf.push(0)
}
for (i = w - 1; i > 0; --i) naf.push(0)
k.ishrn(w)

@@ -894,9 +688,4 @@ }

/**
* @return {string}
*/
BN.prototype.inspect = function () {
if (this.isZero()) {
return '0'
}
if (this.isZero()) return '0'

@@ -903,0 +692,0 @@ var buffer = this.toBuffer().toString('hex')

'use strict'
/**
* @param {BN} num1
* @param {BN} num2
* @param {BN} out
*/
exports.umulTo10x10 = function (num1, num2, out) {

@@ -9,0 +3,0 @@ var a = num1.words

'use strict'
var BN = require('./bn')
/**
* @class ECJPoint
* @constructor
* @param {BN} x
* @param {BN} y
* @param {BN} z
*/
function ECJPoint (x, y, z) {

@@ -29,9 +21,4 @@ if (x === null && y === null && z === null) {

/**
* @return {ECJPoint}
*/
ECJPoint.prototype.neg = function () {
if (this.inf) {
return this
}
if (this.inf) return this

@@ -41,16 +28,8 @@ return new ECJPoint(this.x, this.y.redNeg(), this.z)

/**
* @param {ECJPoint} p
* @return {ECJPoint}
*/
ECJPoint.prototype.add = function (p) {
// O + P = P
if (this.inf) {
return p
}
if (this.inf) return p
// P + O = P
if (p.inf) {
return this
}
if (p.inf) return this

@@ -69,6 +48,3 @@ // http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-add-1998-cmo-2

if (h.isZero()) {
if (r.isZero()) {
return this.dbl()
}
if (r.isZero()) return this.dbl()
return new ECJPoint(null, null, null)

@@ -88,16 +64,8 @@ }

/**
* @param {ECPoint} p
* @return {ECJPoint}
*/
ECJPoint.prototype.mixedAdd = function (p) {
// O + P = P
if (this.inf) {
return p.toECJPoint()
}
if (this.inf) return p.toECJPoint()
// P + O = P
if (p.inf) {
return this
}
if (p.inf) return this

@@ -116,6 +84,3 @@ // http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-add-1998-cmo-2

if (h.isZero()) {
if (r.isZero()) {
return this.dbl()
}
if (r.isZero()) return this.dbl()
return new ECJPoint(null, null, null)

@@ -135,9 +100,4 @@ }

/**
* @return {ECJPoint}
*/
ECJPoint.prototype.dbl = function () {
if (this.inf) {
return this
}
if (this.inf) return this

@@ -209,15 +169,7 @@ var nx

/**
* @param {number} pow
* @return {ECJPoint}
*/
ECJPoint.prototype.dblp = function (pow) {
if (pow === 0 || this.inf) {
return this
}
if (pow === 0 || this.inf) return this
var point = this
for (var i = 0; i < pow; i++) {
point = point.dbl()
}
for (var i = 0; i < pow; i++) point = point.dbl()

@@ -224,0 +176,0 @@ return point

'use strict'
var BN = require('./bn')
var ECJPoint = require('./ecjpoint')
/**
* @class ECPoint
* @constructor
* @param {BN} x
* @param {BN} y
*/
function ECPoint (x, y) {

@@ -23,6 +16,2 @@ if (x === null && y === null) {

/**
* @param {Buffer} publicKey
* @return {?ECPoint}
*/
ECPoint.fromPublicKey = function (publicKey) {

@@ -37,16 +26,9 @@ var first = publicKey[0]

// overflow
if (x.ucmp(BN.p) >= 0) {
return null
}
if (x.ucmp(BN.p) >= 0) return null
// create from X
y = x.redSqr().redMul(x).redIAdd7().redSqrt()
if (y === null) {
return null
}
if (y === null) return null
if ((first === 0x03) !== y.isOdd()) y = y.redNeg()
if ((first === 0x03) !== y.isOdd()) {
y = y.redNeg()
}
return new ECPoint(x, y)

@@ -60,15 +42,9 @@ }

// overflow
if (x.ucmp(BN.p) >= 0 || y.ucmp(BN.p) >= 0) {
return null
}
if (x.ucmp(BN.p) >= 0 || y.ucmp(BN.p) >= 0) return null
// is odd flag
if ((first === 0x06 || first === 0x07) && y.isOdd() !== (first === 0x07)) {
return null
}
if ((first === 0x06 || first === 0x07) && y.isOdd() !== (first === 0x07)) return null
// x*x*x + 7 = y*y
if (x.redSqr().redMul(x).redIAdd7().ucmp(y.redSqr()) !== 0) {
return null
}
if (x.redSqr().redMul(x).redIAdd7().ucmp(y.redSqr()) !== 0) return null

@@ -81,6 +57,2 @@ return new ECPoint(x, y)

/**
* @param {boolean} compressed
* @return {Buffer}
*/
ECPoint.prototype.toPublicKey = function (compressed) {

@@ -105,10 +77,4 @@ var x = this.x

/**
* @param {ECJPoint} p
* @return {ECPoint}
*/
ECPoint.fromECJPoint = function (p) {
if (p.inf) {
return new ECPoint(null, null)
}
if (p.inf) return new ECPoint(null, null)

@@ -123,9 +89,4 @@ var zinv = p.z.redInvm()

/**
* @return {ECJPoint}
*/
ECPoint.prototype.toECJPoint = function () {
if (this.inf) {
return new ECJPoint(null, null, null)
}
if (this.inf) return new ECJPoint(null, null, null)

@@ -135,9 +96,4 @@ return new ECJPoint(this.x, this.y, ECJPoint.one)

/**
* @return {ECPoint}
*/
ECPoint.prototype.neg = function () {
if (this.inf) {
return this
}
if (this.inf) return this

@@ -147,23 +103,12 @@ return new ECPoint(this.x, this.y.redNeg())

/**
* @param {ECPoint} p
* @return {ECPoint}
*/
ECPoint.prototype.add = function (p) {
// O + P = P
if (this.inf) {
return p
}
if (this.inf) return p
// P + O = P
if (p.inf) {
return this
}
if (p.inf) return this
if (this.x.ucmp(p.x) === 0) {
// P + P = 2P
if (this.y.ucmp(p.y) === 0) {
return this.dbl()
}
if (this.y.ucmp(p.y) === 0) return this.dbl()
// P + (-P) = O

@@ -177,5 +122,3 @@ return new ECPoint(null, null)

var s = this.y.redSub(p.y)
if (!s.isZero()) {
s = s.redMul(this.x.redSub(p.x).redInvm())
}
if (!s.isZero()) s = s.redMul(this.x.redSub(p.x).redInvm())

@@ -187,15 +130,8 @@ var nx = s.redSqr().redISub(this.x).redISub(p.x)

/**
* @return {ECPoint}
*/
ECPoint.prototype.dbl = function () {
if (this.inf) {
return this
}
if (this.inf) return this
// 2P = O
var yy = this.y.redAdd(this.y)
if (yy.isZero()) {
return new ECPoint(null, null)
}
if (yy.isZero()) return new ECPoint(null, null)

@@ -213,6 +149,2 @@ // s = (3 * x^2) / (2 * y)

/**
* @param {BN} num
* @return {ECPoint}
*/
ECPoint.prototype.mul = function (num) {

@@ -230,13 +162,7 @@ // Algorithm 3.36 Window NAF method for point multiplication

// Count zeroes
for (var k = 0; i >= 0 && naf[i] === 0; i--) {
k++
}
if (i >= 0) {
k++
}
for (var k = 0; i >= 0 && naf[i] === 0; i--, ++k);
if (i >= 0) k += 1
acc = acc.dblp(k)
if (i < 0) {
break
}
if (i < 0) break

@@ -255,14 +181,6 @@ // J +- P

/**
* @param {number} wnd
* @return {{wnd: number, points: ECPoint[]}}
*/
ECPoint.prototype._getNAFPoints1 = function () {
return {wnd: 1, points: [this]}
return { wnd: 1, points: [this] }
}
/**
* @param {number} wnd
* @return {{wnd: number, points: ECPoint[]}}
*/
ECPoint.prototype._getNAFPoints = function (wnd) {

@@ -272,9 +190,6 @@ var points = new Array((1 << wnd) - 1)

var dbl = this.dbl()
for (var i = 1; i < points.length; ++i) {
points[i] = points[i - 1].add(dbl)
}
return {wnd: wnd, points: points}
for (var i = 1; i < points.length; ++i) points[i] = points[i - 1].add(dbl)
return { wnd: wnd, points: points }
}
module.exports = ECPoint
'use strict'
var BN = require('./bn')

@@ -7,6 +6,2 @@ var ECPoint = require('./ecpoint')

/**
* @class ECPointG
* @constructor
*/
function ECPointG () {

@@ -20,4 +15,2 @@ this.x = BN.fromBuffer(new Buffer('79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798', 'hex'))

/**
*/
ECPointG.prototype._precompute = function () {

@@ -30,6 +23,3 @@ var ecpoint = new ECPoint(this.x, this.y)

for (var i = 1; i < points.length; ++i) {
for (var j = 0; j < dstep; j++) {
acc = acc.dbl()
}
for (var j = 0; j < dstep; j++) acc = acc.dbl()
points[i] = acc

@@ -48,6 +38,2 @@ }

/**
* @param {BN} num
* @return {ECPointG}
*/
ECPointG.prototype.mul = function (num) {

@@ -66,6 +52,3 @@ // Algorithm 3.42 Fixed-base NAF windowing method for point multiplication

var nafW = 0
for (var k = j + step - 1; k >= j; k--) {
nafW = (nafW << 1) + naf[k]
}
for (var k = j + step - 1; k >= j; k--) nafW = (nafW << 1) + naf[k]
repr.push(nafW)

@@ -91,8 +74,2 @@ }

/**
* @param {BN} k1
* @param {ECPoint} p2
* @param {BN} k2
* @return {ECJPoint}
*/
ECPointG.prototype.mulAdd = function (k1, p2, k2) {

@@ -113,16 +90,9 @@ var nafPointsP1 = this.precomputed.naf

if (tmp[0] !== 0 || tmp[1] !== 0) {
break
}
if (tmp[0] !== 0 || tmp[1] !== 0) break
}
if (i >= 0) {
k++
}
if (i >= 0) k += 1
acc = acc.dblp(k)
if (i < 0) {
break
}
if (i < 0) break

@@ -129,0 +99,0 @@ for (var jj = 0; jj < 2; jj++) {

'use strict'
var createHash = require('create-hash')
var HmacDRBG = require('drbg.js/hmac')

@@ -9,6 +9,2 @@ var messages = require('../messages.json')

/**
* @param {Buffer} privateKey
* @return {boolean}
*/
exports.privateKeyVerify = function (privateKey) {

@@ -19,12 +15,5 @@ var bn = BN.fromBuffer(privateKey)

/**
* @param {Buffer} privateKey
* @param {boolean} compressed
* @return {Buffer}
*/
exports.privateKeyExport = function (privateKey, compressed) {
var d = BN.fromBuffer(privateKey)
if (d.isOverflow() || d.isZero()) {
throw new Error(messages.EC_PRIVATE_KEY_EXPORT_DER_FAIL)
}
if (d.isOverflow() || d.isZero()) throw new Error(messages.EC_PRIVATE_KEY_EXPORT_DER_FAIL)

@@ -34,35 +23,16 @@ return g.mul(d).toPublicKey(compressed)

/**
* @param {Buffer} privateKey
* @param {Buffer} tweak
* @return {Buffer}
*/
exports.privateKeyTweakAdd = function (privateKey, tweak) {
var bn = BN.fromBuffer(tweak)
if (bn.isOverflow()) {
throw new Error(messages.EC_PRIVATE_KEY_TWEAK_ADD_FAIL)
}
if (bn.isOverflow()) throw new Error(messages.EC_PRIVATE_KEY_TWEAK_ADD_FAIL)
bn.iadd(BN.fromBuffer(privateKey))
if (bn.isOverflow()) {
bn.isub(BN.n)
}
if (bn.isOverflow()) bn.isub(BN.n)
if (bn.isZero()) throw new Error(messages.EC_PRIVATE_KEY_TWEAK_ADD_FAIL)
if (bn.isZero()) {
throw new Error(messages.EC_PRIVATE_KEY_TWEAK_ADD_FAIL)
}
return bn.toBuffer()
}
/**
* @param {Buffer} privateKey
* @param {Buffer} tweak
* @return {Buffer}
*/
exports.privateKeyTweakMul = function (privateKey, tweak) {
var bn = BN.fromBuffer(tweak)
if (bn.isOverflow() || bn.isZero()) {
throw new Error(messages.EC_PRIVATE_KEY_TWEAK_MUL_FAIL)
}
if (bn.isOverflow() || bn.isZero()) throw new Error(messages.EC_PRIVATE_KEY_TWEAK_MUL_FAIL)

@@ -73,12 +43,5 @@ var d = BN.fromBuffer(privateKey)

/**
* @param {Buffer} privateKey
* @param {boolean} compressed
* @return {Buffer}
*/
exports.publicKeyCreate = function (privateKey, compressed) {
var d = BN.fromBuffer(privateKey)
if (d.isOverflow() || d.isZero()) {
throw new Error(messages.EC_PUBLIC_KEY_CREATE_FAIL)
}
if (d.isOverflow() || d.isZero()) throw new Error(messages.EC_PUBLIC_KEY_CREATE_FAIL)

@@ -88,12 +51,5 @@ return g.mul(d).toPublicKey(compressed)

/**
* @param {Buffer} publicKey
* @param {boolean} compressed
* @return {Buffer}
*/
exports.publicKeyConvert = function (publicKey, compressed) {
var point = ECPoint.fromPublicKey(publicKey)
if (point === null) {
throw new Error(messages.EC_PUBLIC_KEY_PARSE_FAIL)
}
if (point === null) throw new Error(messages.EC_PUBLIC_KEY_PARSE_FAIL)

@@ -103,6 +59,2 @@ return point.toPublicKey(compressed)

/**
* @param {Buffer} publicKey
* @return {boolean}
*/
exports.publicKeyVerify = function (publicKey) {

@@ -112,18 +64,8 @@ return ECPoint.fromPublicKey(publicKey) !== null

/**
* @param {Buffer} publicKey
* @param {Buffer} tweak
* @param {boolean} compressed
* @return {Buffer}
*/
exports.publicKeyTweakAdd = function (publicKey, tweak, compressed) {
var point = ECPoint.fromPublicKey(publicKey)
if (point === null) {
throw new Error(messages.EC_PUBLIC_KEY_PARSE_FAIL)
}
if (point === null) throw new Error(messages.EC_PUBLIC_KEY_PARSE_FAIL)
tweak = BN.fromBuffer(tweak)
if (tweak.isOverflow()) {
throw new Error(messages.EC_PUBLIC_KEY_TWEAK_ADD_FAIL)
}
if (tweak.isOverflow()) throw new Error(messages.EC_PUBLIC_KEY_TWEAK_ADD_FAIL)

@@ -133,18 +75,8 @@ return g.mul(tweak).add(point).toPublicKey(compressed)

/**
* @param {Buffer} publicKey
* @param {Buffer} tweak
* @param {boolean} compressed
* @return {Buffer}
*/
exports.publicKeyTweakMul = function (publicKey, tweak, compressed) {
var point = ECPoint.fromPublicKey(publicKey)
if (point === null) {
throw new Error(messages.EC_PUBLIC_KEY_PARSE_FAIL)
}
if (point === null) throw new Error(messages.EC_PUBLIC_KEY_PARSE_FAIL)
tweak = BN.fromBuffer(tweak)
if (tweak.isOverflow() || tweak.isZero()) {
throw new Error(messages.EC_PUBLIC_KEY_TWEAK_MUL_FAIL)
}
if (tweak.isOverflow() || tweak.isZero()) throw new Error(messages.EC_PUBLIC_KEY_TWEAK_MUL_FAIL)

@@ -154,7 +86,2 @@ return point.mul(tweak).toPublicKey(compressed)

/**
* @param {Buffer[]} publicKeys
* @param {boolean} compressed
* @return {Buffer}
*/
exports.publicKeyCombine = function (publicKeys, compressed) {

@@ -164,34 +91,19 @@ var points = new Array(publicKeys.length)

points[i] = ECPoint.fromPublicKey(publicKeys[i])
if (points[i] === null) {
throw new Error(messages.EC_PUBLIC_KEY_PARSE_FAIL)
}
if (points[i] === null) throw new Error(messages.EC_PUBLIC_KEY_PARSE_FAIL)
}
var point = points[0]
for (var j = 1; j < points.length; ++j) {
point = point.add(points[j])
}
for (var j = 1; j < points.length; ++j) point = point.add(points[j])
if (point.inf) throw new Error(messages.EC_PUBLIC_KEY_COMBINE_FAIL)
if (point.inf) {
throw new Error(messages.EC_PUBLIC_KEY_COMBINE_FAIL)
}
return point.toPublicKey(compressed)
}
/**
* @param {Buffer} signature
* @return {Buffer}
*/
exports.signatureNormalize = function (signature) {
var r = BN.fromBuffer(signature.slice(0, 32))
var s = BN.fromBuffer(signature.slice(32, 64))
if (r.isOverflow() || s.isOverflow()) {
throw new Error(messages.ECDSA_SIGNATURE_PARSE_FAIL)
}
if (r.isOverflow() || s.isOverflow()) throw new Error(messages.ECDSA_SIGNATURE_PARSE_FAIL)
var result = new Buffer(signature)
if (s.isHigh()) {
BN.n.sub(s).toBuffer().copy(result, 32)
}
if (s.isHigh()) BN.n.sub(s).toBuffer().copy(result, 32)

@@ -201,30 +113,16 @@ return result

/**
* @param {Buffer} signature
* @return {{r: Buffer, s: Buffer}}
*/
exports.signatureExport = function (signature) {
var r = signature.slice(0, 32)
var s = signature.slice(32, 64)
if (BN.fromBuffer(r).isOverflow() || BN.fromBuffer(s).isOverflow()) {
throw new Error(messages.ECDSA_SIGNATURE_PARSE_FAIL)
}
if (BN.fromBuffer(r).isOverflow() || BN.fromBuffer(s).isOverflow()) throw new Error(messages.ECDSA_SIGNATURE_PARSE_FAIL)
return {r: r, s: s}
return { r: r, s: s }
}
/**
* @param {{r: Buffer, s: Buffer}} sigObj
* @return {Buffer}
*/
exports.signatureImport = function (sigObj) {
var r = BN.fromBuffer(sigObj.r)
if (r.isOverflow()) {
r = BN.fromNumber(0)
}
if (r.isOverflow()) r = BN.fromNumber(0)
var s = BN.fromBuffer(sigObj.s)
if (s.isOverflow()) {
s = BN.fromNumber(0)
}
if (s.isOverflow()) s = BN.fromNumber(0)

@@ -234,20 +132,9 @@ return Buffer.concat([r.toBuffer(), s.toBuffer()])

/**
* @param {Buffer} message
* @param {Buffer} privateKey
* @param {?sign~noncefn} noncefn
* @param {?Buffer} data
* @return {{signature: Buffer, recovery: number}}
*/
exports.sign = function (message, privateKey, noncefn, data) {
var d = BN.fromBuffer(privateKey)
if (d.isOverflow() || d.isZero()) {
throw new Error(messages.ECDSA_SIGN_FAIL)
}
if (d.isOverflow() || d.isZero()) throw new Error(messages.ECDSA_SIGN_FAIL)
if (noncefn === null) {
var drbg = new HmacDRBG('sha256', privateKey, message, data)
noncefn = function () {
return drbg.generate(32)
}
noncefn = function () { return drbg.generate(32) }
}

@@ -258,21 +145,13 @@

var nonce = noncefn(message, privateKey, null, data, count)
if (!Buffer.isBuffer(nonce) || nonce.length !== 32) {
throw new Error(messages.ECDSA_SIGN_FAIL)
}
if (!Buffer.isBuffer(nonce) || nonce.length !== 32) throw new Error(messages.ECDSA_SIGN_FAIL)
var k = BN.fromBuffer(nonce)
if (k.isOverflow() || k.isZero()) {
continue
}
if (k.isOverflow() || k.isZero()) continue
var kp = g.mul(k)
var r = kp.x.fireduce()
if (r.isZero()) {
continue
}
if (r.isZero()) continue
var s = k.uinvm().umul(r.umul(d).ureduce().iadd(bnMessage).fireduce()).ureduce()
if (s.isZero()) {
continue
}
if (s.isZero()) continue

@@ -292,23 +171,11 @@ var recovery = (kp.x.ucmp(r) !== 0 ? 2 : 0) | (kp.y.isOdd() ? 1 : 0)

/**
* @param {Buffer} message
* @param {Buffer} signature
* @param {Buffer} publicKey
* @return {boolean}
*/
exports.verify = function (message, signature, publicKey) {
var sigr = BN.fromBuffer(signature.slice(0, 32))
var sigs = BN.fromBuffer(signature.slice(32, 64))
if (sigr.isOverflow() || sigs.isOverflow()) {
throw new Error(messages.ECDSA_SIGNATURE_PARSE_FAIL)
}
if (sigr.isOverflow() || sigs.isOverflow()) throw new Error(messages.ECDSA_SIGNATURE_PARSE_FAIL)
if (sigs.isHigh() || sigr.isZero() || sigs.isZero()) {
return false
}
if (sigs.isHigh() || sigr.isZero() || sigs.isZero()) return false
var pub = ECPoint.fromPublicKey(publicKey)
if (pub === null) {
throw new Error(messages.EC_PUBLIC_KEY_PARSE_FAIL)
}
if (pub === null) throw new Error(messages.EC_PUBLIC_KEY_PARSE_FAIL)

@@ -319,5 +186,3 @@ var sinv = sigs.uinvm()

var point = g.mulAdd(u1, pub, u2)
if (point.inf) {
return false
}
if (point.inf) return false

@@ -327,38 +192,19 @@ // return ECPoint.fromECJPoint(point).x.fireduce().ucmp(sigr) === 0

var z2 = point.z.redSqr()
if (sigr.redMul(z2).ucmp(point.x) === 0) {
return true
}
if (sigr.redMul(z2).ucmp(point.x) === 0) return true
if (sigr.ucmp(BN.psn) >= 0) return false
if (sigr.ucmp(BN.psn) >= 0) {
return false
}
return sigr.iadd(BN.psn).redMul(z2).ucmp(point.x) === 0
}
/**
* @param {Buffer} message
* @param {Buffer} signature
* @param {number} recovery
* @param {boolean} compressed
* @return {Buffer}
*/
exports.recover = function (message, signature, recovery, compressed) {
var sigr = BN.fromBuffer(signature.slice(0, 32))
var sigs = BN.fromBuffer(signature.slice(32, 64))
if (sigr.isOverflow() || sigs.isOverflow()) {
throw new Error(messages.ECDSA_SIGNATURE_PARSE_FAIL)
}
if (sigr.isOverflow() || sigs.isOverflow()) throw new Error(messages.ECDSA_SIGNATURE_PARSE_FAIL)
do {
if (sigr.isZero() || sigs.isZero()) {
break
}
if (sigr.isZero() || sigs.isZero()) break
var kpx = sigr
if (recovery >> 1) {
if (kpx.ucmp(BN.psn) >= 0) {
break
}
if (kpx.ucmp(BN.psn) >= 0) break
kpx = sigr.add(BN.n)

@@ -369,5 +215,3 @@ }

var kp = ECPoint.fromPublicKey(kpPublicKey)
if (kp === null) {
break
}
if (kp === null) break

@@ -383,8 +227,15 @@ var eNeg = BN.n.sub(BN.fromBuffer(message))

/**
* @param {Buffer} publicKey
* @param {Buffer} privateKey
* @return {Buffer}
*/
exports.ecdh = function (publicKey, privateKey) {
var shared = exports.ecdhUnsafe(publicKey, privateKey, true)
return createHash('sha256').update(shared).digest()
}
exports.ecdhUnsafe = function (publicKey, privateKey, compressed) {
var point = ECPoint.fromPublicKey(publicKey)
if (point === null) throw new Error(messages.EC_PUBLIC_KEY_PARSE_FAIL)
var scalar = BN.fromBuffer(privateKey)
if (scalar.isOverflow() || scalar.isZero()) throw new Error(messages.ECDH_FAIL)
return point.mul(scalar).toPublicKey(compressed)
}
{
"name": "secp256k1",
"version": "3.0.1",
"version": "3.1.0",
"description": "This module provides native bindings to ecdsa secp256k1 functions",

@@ -30,6 +30,7 @@ "keywords": [

"elliptic.js",
"index.js",
"js.js",
"utils/has_lib.sh"
],
"main": "./bindings.js",
"main": "./index.js",
"repository": {

@@ -41,37 +42,37 @@ "type": "git",

"clean": "node-gyp clean",
"coverage": "RANDOM_TESTS_REPEAT=1 nyc tape test/index.js",
"coverage-lcov": "npm run coverage && nyc report -r lcov",
"install": "npm run rebuild",
"lint": "standard",
"rebuild": "node-gyp rebuild",
"test": "npm run test:node && npm run test:browser",
"test:browser": "karma start karma.conf.js",
"test:node": "istanbul test node_modules/mocha/bin/_mocha -- --reporter spec test/index.js"
"test": "npm run unit",
"unit": "npm run unit:node && npm run unit:browser",
"unit:browser": "karma start karma.conf.js",
"unit:node": "tape test/index.js |faucet"
},
"dependencies": {
"bindings": "^1.2.1",
"bn.js": "^4.10.0",
"drbg.js": "^1.0.0",
"bip66": "^1.1.3",
"bn.js": "^4.11.3",
"create-hash": "^1.1.2",
"drbg.js": "^1.0.1",
"elliptic": "^6.2.3",
"nan": "^2.2.0"
"nan": "^2.2.1"
},
"devDependencies": {
"benchmark": "^2.0.0",
"bigi": "^1.3.0",
"bignum": "^0.11.0",
"chai": "^3.4.0",
"ecdsa": "^0.6.0",
"eckey": "^0.8.0",
"ecurve": "^1.0.2",
"istanbul": "^0.4.0",
"karma": "^0.13.14",
"karma-browserify": "^4.4.0",
"karma-chrome-launcher": "^0.2.1",
"karma-detect-browsers": "^2.0.2",
"browserify": "^13.0.0",
"faucet": "0.0.1",
"karma": "^0.13.22",
"karma-browserify": "^5.0.4",
"karma-chrome-launcher": "^0.2.3",
"karma-detect-browsers": "^2.1.0",
"karma-env-preprocessor": "^0.1.1",
"karma-firefox-launcher": "^0.1.6",
"karma-mocha": "^0.2.0",
"mocha": "^2.3.3",
"node-gyp": "^3.0.3",
"progress": "^1.1.8",
"standard": "^5.3.1",
"xorshift.js": "^1.0.1"
"karma-tap": "^1.0.4",
"node-gyp": "^3.3.1",
"nyc": "^6.4.0",
"standard": "^7.0.0",
"tape": "^4.5.1",
"xorshift.js": "^1.0.3"
},

@@ -83,4 +84,4 @@ "engines": {

"browser": {
"./bindings.js": "./elliptic.js"
"./index.js": "./elliptic.js"
}
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc