tiny-secp256k1
Advanced tools
Comparing version 0.0.3 to 0.0.4
125
ecurve.js
@@ -6,4 +6,6 @@ let bigi = require('bigi') | ||
let EC_ZERO = Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex') | ||
let EC_UINT_MAX = Buffer.from('fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141', 'hex') | ||
let ZERO32 = Buffer.alloc(32, 0) | ||
let EC_GROUP_ORDER = Buffer.from('fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141', 'hex') | ||
let EC_P = Buffer.from('fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f', 'hex') | ||
let ONE1 = Buffer.alloc(1, 1) | ||
@@ -24,42 +26,78 @@ let ZERO1 = Buffer.alloc(1, 0) | ||
if (!isScalar(x)) return false | ||
return x.compare(EC_UINT_MAX) < 0 // < n | ||
return x.compare(EC_GROUP_ORDER) < 0 // < G | ||
} | ||
function isPoint (q) { | ||
if (!Buffer.isBuffer(q)) return false | ||
if (q.length < 33) return false | ||
function isPoint (p) { | ||
if (!Buffer.isBuffer(p)) return false | ||
if (p.length < 33) return false | ||
let t = q[0] | ||
if ((t === 0x02 || t === 0x03) && q.length === 33) return true | ||
if (t === 0x04 && q.length === 65) return true | ||
let t = p[0] | ||
let x = p.slice(1, 33) | ||
if (x.compare(ZERO32) === 0) return false | ||
if (x.compare(EC_P) >= 0) return false | ||
if ((t === 0x02 || t === 0x03) && p.length === 33) return true | ||
let y = p.slice(33) | ||
if (y.compare(ZERO32) === 0) return false | ||
if (y.compare(EC_P) >= 0) return false | ||
if (t === 0x04 && p.length === 65) return true | ||
return false | ||
} | ||
function __isPointCompressed (p) { | ||
return p[0] !== 0x04 | ||
} | ||
function isPointCompressed (p) { | ||
if (!isPoint(p)) return false | ||
return __isPointCompressed(p) | ||
} | ||
function isPrivate (x) { | ||
if (!isScalar(x)) return false | ||
return x.compare(EC_ZERO) > 0 && // > 0 | ||
x.compare(EC_UINT_MAX) < 0 // < n | ||
return x.compare(ZERO32) > 0 && // > 0 | ||
x.compare(EC_GROUP_ORDER) < 0 // < G | ||
} | ||
function isSignature (value) { | ||
return Buffer.isBuffer(value) && value.length === 64 | ||
let r = value.slice(0, 32) | ||
let s = value.slice(32, 64) | ||
return Buffer.isBuffer(value) && value.length === 64 && | ||
r.compare(EC_GROUP_ORDER) < 0 && | ||
s.compare(EC_GROUP_ORDER) < 0 | ||
} | ||
function pointAdd (pA, pB, compressed) { | ||
function assumeCompression (value, pubkey) { | ||
if (value === undefined && pubkey !== undefined) return __isPointCompressed(pubkey) | ||
if (value === undefined) return true | ||
return value | ||
} | ||
function pointAdd (pA, pB, __compressed) { | ||
if (!isPoint(pA)) throw new TypeError(THROW_BAD_POINT) | ||
if (!isPoint(pB)) throw new TypeError(THROW_BAD_POINT) | ||
let a = ecurve.Point.decodeFrom(secp256k1, pA) | ||
let b = ecurve.Point.decodeFrom(secp256k1, pB) | ||
let p = a.add(b) | ||
if (secp256k1.isInfinity(p)) return null | ||
return p.getEncoded(compressed) | ||
let pp = a.add(b) | ||
if (secp256k1.isInfinity(pp)) return null | ||
let compressed = assumeCompression(__compressed, pA) | ||
return pp.getEncoded(compressed) | ||
} | ||
function pointAddScalar (p, tweak, compressed) { | ||
function pointAddScalar (p, tweak, __compressed) { | ||
if (!isPoint(p)) throw new TypeError(THROW_BAD_POINT) | ||
if (!isOrderScalar(tweak)) throw new TypeError(THROW_BAD_TWEAK) | ||
let q = ecurve.Point.decodeFrom(secp256k1, p) | ||
let u = q.multiply(tweak) | ||
if (secp256k1.isInfinity(u)) return null | ||
return u.getEncoded(compressed) | ||
let compressed = assumeCompression(__compressed, p) | ||
let pp = ecurve.Point.decodeFrom(secp256k1, p) | ||
if (tweak.compare(ZERO32) === 0) return pp.getEncoded(compressed) | ||
let tt = bigi.fromBuffer(tweak) | ||
let qq = secp256k1.G.multiply(tt) | ||
let uu = pp.add(qq) | ||
if (secp256k1.isInfinity(uu)) return null | ||
return uu.getEncoded(compressed) | ||
} | ||
@@ -69,8 +107,18 @@ | ||
if (!isPoint(p)) throw new TypeError(THROW_BAD_POINT) | ||
let q = ecurve.Point.decodeFrom(secp256k1, p) | ||
return q.getEncoded(compressed) | ||
let pp = ecurve.Point.decodeFrom(secp256k1, p) | ||
if (secp256k1.isInfinity(pp)) throw new TypeError(THROW_BAD_POINT) | ||
return pp.getEncoded(compressed) | ||
} | ||
function pointFromScalar (d, compressed) { | ||
return secp256k1.G.multiply(d).getEncoded(compressed) | ||
function pointFromScalar (d, __compressed) { | ||
if (!isPrivate(d)) throw new TypeError(THROW_BAD_PRIVATE) | ||
let dd = bigi.fromBuffer(d) | ||
let pp = secp256k1.G.multiply(dd) | ||
if (secp256k1.isInfinity(pp)) return null | ||
let compressed = assumeCompression(__compressed) | ||
return pp.getEncoded(compressed) | ||
} | ||
@@ -81,2 +129,3 @@ | ||
if (!isOrderScalar(tweak)) throw new TypeError(THROW_BAD_TWEAK) | ||
let dd = bigi.fromBuffer(d) | ||
@@ -86,2 +135,3 @@ let tt = bigi.fromBuffer(tweak) | ||
if (!isPrivate(dt)) return null | ||
return dt | ||
@@ -175,21 +225,25 @@ } | ||
return { r, s } | ||
let buffer = Buffer.allocUnsafe(64) | ||
r.toBuffer(32).copy(buffer, 0) | ||
s.toBuffer(32).copy(buffer, 32) | ||
return buffer | ||
} | ||
function verify (hash, p, signature) { | ||
function verify (hash, q, signature) { | ||
if (!isScalar(hash)) throw new TypeError(THROW_BAD_HASH) | ||
if (!isPoint(p)) throw new TypeError(THROW_BAD_PRIVATE) | ||
if (!isPoint(q)) throw new TypeError(THROW_BAD_POINT) | ||
// 1.4.1 Enforce r and s are both integers in the interval [1, n − 1] (1, isSignature enforces '< n - 1') | ||
if (!isSignature(signature)) throw new TypeError(THROW_BAD_SIGNATURE) | ||
let Q = ecurve.Point.decodeFrom(secp256k1, p) | ||
let Q = ecurve.Point.decodeFrom(secp256k1, q) | ||
let n = secp256k1.n | ||
let G = secp256k1.G | ||
let r = bigi.fromBuffer(signature.slice(0, 32)) | ||
let s = bigi.fromBuffer(signature.slice(32, 64)) | ||
let r = signature.r | ||
let s = signature.s | ||
// 1.4.1 Enforce r and s are both integers in the interval [1, n − 1] (2, enforces '> 0') | ||
if (r.signum() <= 0 /* || r.compareTo(n) >= 0 */) return false | ||
if (s.signum() <= 0 /* || s.compareTo(n) >= 0 */) return false | ||
// 1.4.1 Enforce r and s are both integers in the interval [1, n − 1] | ||
if (r.signum() <= 0 || r.compareTo(n) >= 0) return false | ||
if (s.signum() <= 0 || s.compareTo(n) >= 0) return false | ||
// 1.4.2 H = Hash(M), already done by the user | ||
@@ -226,2 +280,3 @@ // 1.4.3 e = H | ||
isPoint, | ||
isPointCompressed, | ||
isPrivate, | ||
@@ -228,0 +283,0 @@ pointAdd, |
{ | ||
"name": "tiny-secp256k1", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "", | ||
@@ -11,3 +11,4 @@ "main": "index.js", | ||
"scripts": { | ||
"build": "node-gyp rebuild" | ||
"build": "node-gyp rebuild", | ||
"test": "tape tests/*.js" | ||
}, | ||
@@ -14,0 +15,0 @@ "repository": { |
@@ -5,6 +5,3 @@ let ecurve = require('../ecurve') | ||
let tape = require('tape') | ||
// let fpoints = require('./fixtures/points.json') | ||
let fprivates = require('./fixtures/privates.json') | ||
// let fecdsa = require('./fixtures/ecdsa.json') | ||
@@ -11,0 +8,0 @@ function test (binding) { |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
873000
115
421