Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue/pyn 731 fix unset vars to work with latest esbuild upgrade #22

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
env: {
node: true
},
parserOptions: {
ecmaVersion: 'latest'
},
extends: [
'eslint:recommended'
]
}
18 changes: 18 additions & 0 deletions .github/workflows/npm-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: ecdsa-node Test
on:
push:
branches:
- master
pull_request:
types: [opened, synchronize, reopened]
jobs:
test:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 16
- run: npm ci
- run: npm run lint
- run: npm test
8 changes: 4 additions & 4 deletions ellipticcurve/curve.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CurveFp {
this.name = name;
this.nistName = nistName;
this._oid = oid;
};
}

contains(p) {
if (p.x < 0 || p.x > this.P.minus(1)) {
Expand All @@ -32,16 +32,16 @@ class CurveFp {
return false;
}
return true;
};
}

length() {
return Math.floor((1 + this.N.toString(16).length) / 2);
};
}

get oid() {
return this._oid.slice();
}
};
}


let secp256k1 = new CurveFp(
Expand Down
50 changes: 25 additions & 25 deletions ellipticcurve/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var inv = function (x, n) {

if (x.eq(0)) {
return BigInt(0);
};
}

let lm = BigInt(1);
let hm = BigInt(0);
Expand All @@ -58,7 +58,7 @@ var inv = function (x, n) {
hm = lm;
low = newLow;
lm = nm;
};
}

return modulo(lm, n);
};
Expand All @@ -81,7 +81,7 @@ var fromJacobian = function (p, P) {
// :param P: Prime number in the module of the equation Y^2 = X^3 + A*X + B (mod p)
// :return: Point in default coordinates

z = inv(p.z, P);
let z = inv(p.z, P);

var point = new Point(
modulo(p.x.multiply(z.pow(2)), P),
Expand All @@ -102,7 +102,7 @@ var jacobianDouble = function (p, A, P) {

if (p.y == 0) {
return new Point(BigInt(0), BigInt(0), BigInt(0));
};
}
let ysq = modulo((p.y.pow(2)), P);
let S = modulo((p.x.multiply(ysq).multiply(4)), P);
let M = modulo((((p.x.pow(2)).multiply(3)).add(A.multiply(p.z.pow(4)))), P);
Expand All @@ -125,31 +125,31 @@ var jacobianAdd = function (p, q, A, P) {

if (p.y == 0) {
return q;
};
}
if (q.y == 0) {
return p;
};
}

U1 = modulo(p.x.multiply(q.z.pow(2)), P);
U2 = modulo(q.x.multiply(p.z.pow(2)), P);
S1 = modulo(p.y.multiply(q.z.pow(3)), P);
S2 = modulo(q.y.multiply(p.z.pow(3)), P);
let U1 = modulo(p.x.multiply(q.z.pow(2)), P);
let U2 = modulo(q.x.multiply(p.z.pow(2)), P);
let S1 = modulo(p.y.multiply(q.z.pow(3)), P);
let S2 = modulo(q.y.multiply(p.z.pow(3)), P);

if (U1.eq(U2)) {
if (S1.neq(S2)) {
return Point(BigInt(0), BigInt(0), BigInt(1));
};
}
return jacobianDouble(p, A, P);
};
}

H = U2.minus(U1);
R = S2.minus(S1);
H2 = modulo((H.multiply(H)), P);
H3 = modulo((H.multiply(H2)), P);
U1H2 = modulo((U1.multiply(H2)), P);
nx = modulo(((R.pow(2)).minus(H3).minus(U1H2.multiply(2))), P);
ny = modulo((R.multiply(U1H2.minus(nx)).minus(S1.multiply(H3))), P);
nz = modulo((H.multiply(p.z).multiply(q.z)), P);
let H = U2.minus(U1);
let R = S2.minus(S1);
let H2 = modulo((H.multiply(H)), P);
let H3 = modulo((H.multiply(H2)), P);
let U1H2 = modulo((U1.multiply(H2)), P);
let nx = modulo(((R.pow(2)).minus(H3).minus(U1H2.multiply(2))), P);
let ny = modulo((R.multiply(U1H2.minus(nx)).minus(S1.multiply(H3))), P);
let nz = modulo((H.multiply(p.z).multiply(q.z)), P);

return new Point(nx, ny, nz);
};
Expand All @@ -167,19 +167,19 @@ var jacobianMultiply = function (p, n, N, A, P) {

if (p.y.eq(0) | n.eq(0)) {
return new Point(BigInt(0), BigInt(0), BigInt(1));
};
}
if (n.eq(1)) {
return p;
};
}
if (n.lesser(0) | n.greaterOrEquals(N)) {
return jacobianMultiply(p, modulo(n, N), N, A, P);
};
}
if (modulo(n, 2).eq(0)) {
return jacobianDouble(jacobianMultiply(p, n.over(2), N, A, P), A, P); // bigint division floors result automaticaly
};
}
if (modulo(n, 2).eq(1)) {
return jacobianAdd(jacobianDouble(jacobianMultiply(p, n.over(2), N, A, P), A, P), p, A, P); // bigint division floors result automaticaly
};
}

throw new Error("logical failure: p: " + p + ", n: " + n + ", N: " + N + ", A: " + A + ", P: " + P);
};
Expand Down
28 changes: 14 additions & 14 deletions ellipticcurve/privateKey.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ class PrivateKey {
} else {
this.secret = RandomInteger.between(BigInt(1), curve.N.minus(1));
}
};
}

publicKey () {
let curve = this.curve;
let publicPoint = EcdsaMath.multiply(curve.G, this.secret, curve.N, curve.A, curve.P);
return new PublicKey(publicPoint, curve);
};
}

toString () {
return BinaryAscii.stringFromNumber(this.secret, this.curve.length());
};
}

toDer () {
let encodedPublicKey = this.publicKey().toString(true);
Expand All @@ -42,27 +42,27 @@ class PrivateKey {

toPem () {
return der.toPem(this.toDer(), "EC PRIVATE KEY");
};
}

static fromPem (string) {
let privateKeyPem = string.split("-----BEGIN EC PRIVATE KEY-----")[1];
return this.fromDer(der.fromPem(privateKeyPem));
};
}

static fromDer(string) {
let result = der.removeSequence(string);
let t = result[0];
let empty = result[1];
if (empty) {
throw new Error("trailing junk after DER private key: " + BinaryAscii.hexFromBinary(empty));
};
}

result = der.removeInteger(t);
let one = result[0];
t = result[1];
if (one != 1) {
throw new Error("expected '1' at start of DER private key, got " + one);
};
}

result = der.removeOctetString(t);
let privateKeyStr = result[0];
Expand All @@ -75,15 +75,15 @@ class PrivateKey {

if (tag != 0) {
throw new Error("expected tag 0 in DER private key, got " + tag);
};
}

result = der.removeObject(curveOidStr);
let oidCurve = result[0];
empty = result[1];

if (empty) {
throw new Error("trailing junk after DER private key curve_oid: " + BinaryAscii.hexFromBinary(empty));
};
}

let curve = EcdsaCurve.curvesByOid[oidCurve];

Expand All @@ -94,19 +94,19 @@ class PrivateKey {
"Unknown curve with oid " + oidCurve
+ ". Only the following are available: " + supportedCurvesNames
);
};
}

if (privateKeyStr.length < curve.length()) {
privateKeyStr = hexAt.repeat(curve.length() - privateKeyStr.length) + privateKeyStr;
};
}

return this.fromString(privateKeyStr, curve);
};
}

static fromString (string, curve=EcdsaCurve.secp256k1) {
return new PrivateKey(curve, BinaryAscii.numberFromString(string));
};
};
}
}


exports.PrivateKey = PrivateKey;
24 changes: 12 additions & 12 deletions ellipticcurve/publicKey.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class PublicKey {
constructor (point, curve) {
this.point = point;
this.curve = curve;
};
}

toString (encoded=false) {
let xString = BinaryAscii.stringFromNumber(this.point.x, this.curve.length());
Expand All @@ -19,29 +19,29 @@ class PublicKey {
return "\x00\x04" + xString + yString;
}
return xString + yString;
};
}

toDer () {
let encodeEcAndOid = der.encodeSequence(der.encodeOid([1, 2, 840, 10045, 2, 1]), der.encodeOid(this.curve.oid));

return der.encodeSequence(encodeEcAndOid, der.encodeBitstring(this.toString(true)))
};
}

toPem () {
return der.toPem(this.toDer(), "PUBLIC KEY")
};
}

static fromPem (string) {
return this.fromDer(der.fromPem(string));
};
}

static fromDer (string) {
let result = der.removeSequence(string);
let s1 = result[0];
let empty = result[1];
if (empty) {
throw new Error("trailing junk after DER public key: " + BinaryAscii.hexFromBinary(empty));
};
}

result = der.removeSequence(s1);
let s2 = result[0];
Expand All @@ -55,7 +55,7 @@ class PublicKey {
empty = result[1];
if (empty) {
throw new Error("trailing junk after DER public key objects: " + BinaryAscii.hexFromBinary(empty));
};
}

let curve = EcdsaCurve.curvesByOid[oidCurve];
if (!curve) {
Expand All @@ -67,17 +67,17 @@ class PublicKey {
+ ". Only the following are available: "
+ supportedCurvesNames
);
};
}

result = der.removeBitString(pointBitString);
let pointStr = result[0];
empty = result[1];
if (empty) {
throw new Error("trailing junk after public key point-string: " + BinaryAscii.hexFromBinary(empty));
};
}

return this.fromString(pointStr.slice(2), curve);
};
}

static fromString (string, curve=EcdsaCurve.secp256k1, validatePoint=true) {
let baseLen = curve.length();
Expand All @@ -101,8 +101,8 @@ class PublicKey {
throw new Error("Point (" + p.x + "," + p.y + " * " + curve.name + ".N is not at infinity");
}
return publicKey
};
};
}
}


exports.PublicKey = PublicKey;
Loading