secp256k1: es6ify length normalization.

This commit is contained in:
Christopher Jeffrey 2017-08-13 12:21:44 -07:00
parent e1e71a6e81
commit ebb79e5ff1
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -297,48 +297,50 @@ ec.isLowS = function isLowS(raw) {
function normalizeLength(sig) {
let data = sig;
const p = { place: 0 };
let pos = 0;
let len;
if (data[p.place++] !== 0x30)
if (data[pos++] !== 0x30)
return sig;
const len = getLength(data, p);
[len, pos] = getLength(data, pos);
if (data.length > len + p.place)
data = data.slice(0, len + p.place);
if (data.length > len + pos)
data = data.slice(0, len + pos);
if (data[p.place++] !== 0x02)
if (data[pos++] !== 0x02)
return sig;
const rlen = getLength(data, p);
p.place += rlen;
// R length.
[len, pos] = getLength(data, pos);
if (data[p.place++] !== 0x02)
pos += len;
if (data[pos++] !== 0x02)
return sig;
const slen = getLength(data, p);
if (data.length > slen + p.place)
data = data.slice(0, slen + p.place);
// S length.
[len, pos] = getLength(data, pos);
if (data.length > len + pos)
data = data.slice(0, len + pos);
return data;
}
function getLength(buf, p) {
const initial = buf[p.place++];
function getLength(buf, pos) {
const initial = buf[pos++];
if (!(initial & 0x80))
return initial;
return [initial, pos];
const len = initial & 0xf;
let off = p.place;
let val = 0;
for (let i = 0; i < len; i++, off++) {
for (let i = 0; i < len; i++) {
val <<= 8;
val |= buf[off];
val |= buf[pos++];
}
p.place = off;
return val;
return [val, pos];
}