meteor-node-stubs 1.2.18 → 1.2.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/node_modules/@meteorjs/browserify-sign/browser/sign.js +1 -1
- package/node_modules/@meteorjs/browserify-sign/elliptic/README.md +238 -0
- package/node_modules/@meteorjs/browserify-sign/elliptic/benchmarks/deps/jodid.js +82 -0
- package/node_modules/@meteorjs/browserify-sign/elliptic/benchmarks/deps/secp256k1.js +41 -0
- package/node_modules/@meteorjs/browserify-sign/elliptic/benchmarks/index.js +153 -0
- package/node_modules/@meteorjs/browserify-sign/elliptic/benchmarks/package.json +21 -0
- package/node_modules/@meteorjs/browserify-sign/elliptic/dist/.gitkeep +0 -0
- package/node_modules/@meteorjs/browserify-sign/elliptic/dist/elliptic.js +8961 -0
- package/node_modules/@meteorjs/browserify-sign/elliptic/dist/elliptic.min.js +1 -0
- package/node_modules/@meteorjs/browserify-sign/elliptic/lib/elliptic/curve/base.js +381 -0
- package/node_modules/@meteorjs/browserify-sign/elliptic/lib/elliptic/curve/edwards.js +435 -0
- package/node_modules/@meteorjs/browserify-sign/elliptic/lib/elliptic/curve/index.js +8 -0
- package/node_modules/@meteorjs/browserify-sign/elliptic/lib/elliptic/curve/mont.js +178 -0
- package/node_modules/@meteorjs/browserify-sign/elliptic/lib/elliptic/curve/short.js +938 -0
- package/node_modules/@meteorjs/browserify-sign/elliptic/lib/elliptic/curves.js +206 -0
- package/node_modules/@meteorjs/browserify-sign/elliptic/lib/elliptic/ec/index.js +278 -0
- package/node_modules/@meteorjs/browserify-sign/elliptic/lib/elliptic/ec/key.js +121 -0
- package/node_modules/@meteorjs/browserify-sign/elliptic/lib/elliptic/ec/signature.js +176 -0
- package/node_modules/@meteorjs/browserify-sign/elliptic/lib/elliptic/eddsa/index.js +121 -0
- package/node_modules/@meteorjs/browserify-sign/elliptic/lib/elliptic/eddsa/key.js +95 -0
- package/node_modules/@meteorjs/browserify-sign/elliptic/lib/elliptic/eddsa/signature.js +66 -0
- package/node_modules/@meteorjs/browserify-sign/elliptic/lib/elliptic/precomputed/secp256k1.js +780 -0
- package/node_modules/@meteorjs/browserify-sign/elliptic/lib/elliptic/utils.js +122 -0
- package/node_modules/@meteorjs/browserify-sign/elliptic/lib/elliptic.js +13 -0
- package/node_modules/@meteorjs/browserify-sign/elliptic/test/api-test.js +20 -0
- package/node_modules/@meteorjs/browserify-sign/elliptic/test/curve-test.js +357 -0
- package/node_modules/@meteorjs/browserify-sign/elliptic/test/ecdh-test.js +43 -0
- package/node_modules/@meteorjs/browserify-sign/elliptic/test/ecdsa-test.js +547 -0
- package/node_modules/@meteorjs/browserify-sign/elliptic/test/ed25519-test.js +138 -0
- package/node_modules/@meteorjs/browserify-sign/elliptic/test/fixtures/derivation-fixtures.js +3842 -0
- package/node_modules/@meteorjs/browserify-sign/elliptic/test/fixtures/sign.input +1024 -0
- package/node_modules/@meteorjs/browserify-sign/elliptic/test/index.js +10 -0
- package/node_modules/@meteorjs/browserify-sign/elliptic/test/unittests.html +39 -0
- package/node_modules/@meteorjs/browserify-sign/package.json +2 -1
- package/package.json +1 -1
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var utils = exports;
|
|
4
|
+
var BN = require('bn.js');
|
|
5
|
+
var minAssert = require('minimalistic-assert');
|
|
6
|
+
var minUtils = require('minimalistic-crypto-utils');
|
|
7
|
+
|
|
8
|
+
utils.assert = minAssert;
|
|
9
|
+
utils.toArray = minUtils.toArray;
|
|
10
|
+
utils.zero2 = minUtils.zero2;
|
|
11
|
+
utils.toHex = minUtils.toHex;
|
|
12
|
+
utils.encode = minUtils.encode;
|
|
13
|
+
|
|
14
|
+
// Represent num in a w-NAF form
|
|
15
|
+
function getNAF(num, w, bits) {
|
|
16
|
+
var naf = new Array(Math.max(num.bitLength(), bits) + 1);
|
|
17
|
+
var i;
|
|
18
|
+
for (i = 0; i < naf.length; i += 1) {
|
|
19
|
+
naf[i] = 0;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
var ws = 1 << (w + 1);
|
|
23
|
+
var k = num.clone();
|
|
24
|
+
|
|
25
|
+
for (i = 0; i < naf.length; i++) {
|
|
26
|
+
var z;
|
|
27
|
+
var mod = k.andln(ws - 1);
|
|
28
|
+
if (k.isOdd()) {
|
|
29
|
+
if (mod > (ws >> 1) - 1)
|
|
30
|
+
z = (ws >> 1) - mod;
|
|
31
|
+
else
|
|
32
|
+
z = mod;
|
|
33
|
+
k.isubn(z);
|
|
34
|
+
} else {
|
|
35
|
+
z = 0;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
naf[i] = z;
|
|
39
|
+
k.iushrn(1);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return naf;
|
|
43
|
+
}
|
|
44
|
+
utils.getNAF = getNAF;
|
|
45
|
+
|
|
46
|
+
// Represent k1, k2 in a Joint Sparse Form
|
|
47
|
+
function getJSF(k1, k2) {
|
|
48
|
+
var jsf = [
|
|
49
|
+
[],
|
|
50
|
+
[],
|
|
51
|
+
];
|
|
52
|
+
|
|
53
|
+
k1 = k1.clone();
|
|
54
|
+
k2 = k2.clone();
|
|
55
|
+
var d1 = 0;
|
|
56
|
+
var d2 = 0;
|
|
57
|
+
var m8;
|
|
58
|
+
while (k1.cmpn(-d1) > 0 || k2.cmpn(-d2) > 0) {
|
|
59
|
+
// First phase
|
|
60
|
+
var m14 = (k1.andln(3) + d1) & 3;
|
|
61
|
+
var m24 = (k2.andln(3) + d2) & 3;
|
|
62
|
+
if (m14 === 3)
|
|
63
|
+
m14 = -1;
|
|
64
|
+
if (m24 === 3)
|
|
65
|
+
m24 = -1;
|
|
66
|
+
var u1;
|
|
67
|
+
if ((m14 & 1) === 0) {
|
|
68
|
+
u1 = 0;
|
|
69
|
+
} else {
|
|
70
|
+
m8 = (k1.andln(7) + d1) & 7;
|
|
71
|
+
if ((m8 === 3 || m8 === 5) && m24 === 2)
|
|
72
|
+
u1 = -m14;
|
|
73
|
+
else
|
|
74
|
+
u1 = m14;
|
|
75
|
+
}
|
|
76
|
+
jsf[0].push(u1);
|
|
77
|
+
|
|
78
|
+
var u2;
|
|
79
|
+
if ((m24 & 1) === 0) {
|
|
80
|
+
u2 = 0;
|
|
81
|
+
} else {
|
|
82
|
+
m8 = (k2.andln(7) + d2) & 7;
|
|
83
|
+
if ((m8 === 3 || m8 === 5) && m14 === 2)
|
|
84
|
+
u2 = -m24;
|
|
85
|
+
else
|
|
86
|
+
u2 = m24;
|
|
87
|
+
}
|
|
88
|
+
jsf[1].push(u2);
|
|
89
|
+
|
|
90
|
+
// Second phase
|
|
91
|
+
if (2 * d1 === u1 + 1)
|
|
92
|
+
d1 = 1 - d1;
|
|
93
|
+
if (2 * d2 === u2 + 1)
|
|
94
|
+
d2 = 1 - d2;
|
|
95
|
+
k1.iushrn(1);
|
|
96
|
+
k2.iushrn(1);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return jsf;
|
|
100
|
+
}
|
|
101
|
+
utils.getJSF = getJSF;
|
|
102
|
+
|
|
103
|
+
function cachedProperty(obj, name, computer) {
|
|
104
|
+
var key = '_' + name;
|
|
105
|
+
obj.prototype[name] = function cachedProperty() {
|
|
106
|
+
return this[key] !== undefined ? this[key] :
|
|
107
|
+
this[key] = computer.call(this);
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
utils.cachedProperty = cachedProperty;
|
|
111
|
+
|
|
112
|
+
function parseBytes(bytes) {
|
|
113
|
+
return typeof bytes === 'string' ? utils.toArray(bytes, 'hex') :
|
|
114
|
+
bytes;
|
|
115
|
+
}
|
|
116
|
+
utils.parseBytes = parseBytes;
|
|
117
|
+
|
|
118
|
+
function intFromLE(bytes) {
|
|
119
|
+
return new BN(bytes, 'hex', 'le');
|
|
120
|
+
}
|
|
121
|
+
utils.intFromLE = intFromLE;
|
|
122
|
+
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var elliptic = exports;
|
|
4
|
+
|
|
5
|
+
elliptic.version = '6.6.1';
|
|
6
|
+
elliptic.utils = require('./elliptic/utils');
|
|
7
|
+
elliptic.rand = require('brorand');
|
|
8
|
+
elliptic.curve = require('./elliptic/curve');
|
|
9
|
+
elliptic.curves = require('./elliptic/curves');
|
|
10
|
+
|
|
11
|
+
// Protocols
|
|
12
|
+
elliptic.ec = require('./elliptic/ec');
|
|
13
|
+
elliptic.eddsa = require('./elliptic/eddsa');
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/* eslint-env node, mocha */
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var assert = require('assert');
|
|
5
|
+
var elliptic = require('../');
|
|
6
|
+
|
|
7
|
+
describe('EC API', function() {
|
|
8
|
+
it('should instantiate with valid curve (secp256k1)', function() {
|
|
9
|
+
var ec = new elliptic.ec('secp256k1');
|
|
10
|
+
|
|
11
|
+
assert(ec);
|
|
12
|
+
assert(typeof ec === 'object');
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('should throw error with invalid curve', function() {
|
|
16
|
+
assert.throws(function() {
|
|
17
|
+
new elliptic.ec('nonexistent-curve');
|
|
18
|
+
}, Error);
|
|
19
|
+
});
|
|
20
|
+
});
|
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
/* eslint-env node, mocha */
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var assert = require('assert');
|
|
5
|
+
var BN = require('bn.js');
|
|
6
|
+
var elliptic = require('../');
|
|
7
|
+
|
|
8
|
+
describe('Curve', function() {
|
|
9
|
+
it('should work with example curve', function() {
|
|
10
|
+
var curve = new elliptic.curve.short({
|
|
11
|
+
p: '1d',
|
|
12
|
+
a: '4',
|
|
13
|
+
b: '14',
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
var p = curve.point('18', '16');
|
|
17
|
+
assert(p.validate());
|
|
18
|
+
assert(p.dbl().validate());
|
|
19
|
+
assert(p.dbl().add(p).validate());
|
|
20
|
+
assert(p.dbl().add(p.dbl()).validate());
|
|
21
|
+
assert(p.dbl().add(p.dbl()).eq(p.add(p).add(p).add(p)));
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('should dbl points on edwards curve using proj coordinates', function() {
|
|
25
|
+
var curve = new elliptic.curve.edwards({
|
|
26
|
+
p: new BN('97ffffffffffffffffffffffffffffffffffffffffffffffffffffffffff' +
|
|
27
|
+
'ffffffffffffffffffffffffffffffffff3f', 16, 'le'),
|
|
28
|
+
q: new BN('19973cfd137ee273272d101b28695e7ce1ee951ef221fbd5ffffffffff' +
|
|
29
|
+
'ffffffffffffffffffffffffffffffffffff0f', 16, 'le'),
|
|
30
|
+
r: '8',
|
|
31
|
+
a: '1',
|
|
32
|
+
c: '1',
|
|
33
|
+
// -67254 mod p
|
|
34
|
+
d: new BN('e1f8feffffffffffffffffffffffffffffffffffffffffffffffffff' +
|
|
35
|
+
'ffffffffffffffffffffffffffffffffffffff3f', 16, 'le'),
|
|
36
|
+
g: [
|
|
37
|
+
new BN('0396f77094ccc0eb985310e8bc7d519311846453b8ba232935640b2b0' +
|
|
38
|
+
'340f868ae208d6ee95bf0e59103b2ead08d6f19', 16, 'le'),
|
|
39
|
+
new BN('11', 16, 'le'),
|
|
40
|
+
],
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
var point = [
|
|
44
|
+
'21fd21b36cbdbe0d77ad8692c25d918774f5d3bc179c4cb0ae3c364bf1bea981d0' +
|
|
45
|
+
'2e9f97cc62f20acacf0c553887e5fb',
|
|
46
|
+
'29f994329799dba72aa12ceb06312300167b6e18fbed607c63709826c57292cf29' +
|
|
47
|
+
'f5bab4f5c99c739cf107a3833bb553',
|
|
48
|
+
];
|
|
49
|
+
|
|
50
|
+
var double = [
|
|
51
|
+
'0561c8722cf82b2f0d7c36bc72e34539dcbf181e8d98f5244480e79f5b51a4a541' +
|
|
52
|
+
'457016c9c0509d49078eb5909a1121',
|
|
53
|
+
'05b7812fae9d164ee9249c56a16e29a1ad2cdc6353227074dd96d59df363a0bcb5' +
|
|
54
|
+
'bc67d50b44843ea833156bdc0ac6a2',
|
|
55
|
+
];
|
|
56
|
+
|
|
57
|
+
var p = curve.pointFromJSON(point);
|
|
58
|
+
var d = curve.pointFromJSON(double);
|
|
59
|
+
assert(p.dbl().eq(d));
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('should be able to find a point given y coordinate for all edwards curves',
|
|
63
|
+
function() {
|
|
64
|
+
var curve = new elliptic.curve.edwards({
|
|
65
|
+
p: new BN('f7' +
|
|
66
|
+
'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff07',
|
|
67
|
+
16, 'le'),
|
|
68
|
+
q: new BN('71' +
|
|
69
|
+
'c966d15fd444893407d3dfc46579f7ffffffffffffffffffffffffffffff01',
|
|
70
|
+
16, 'le'),
|
|
71
|
+
r: '4',
|
|
72
|
+
a: '1',
|
|
73
|
+
// -1174 mod p
|
|
74
|
+
d: new BN('61' +
|
|
75
|
+
'fbffffffffffffffffffffffffffffffffffffffffffffffffffffffffff07',
|
|
76
|
+
16, 'le'),
|
|
77
|
+
c: '1',
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
var target = curve.point(
|
|
81
|
+
'05d040ddaa645bf27d2d2f302c5697231425185fd9a410f220ac5c5c7fbeb8a1',
|
|
82
|
+
'02f8ca771306cd23e929775177f2c213843a017a6487b2ec5f9b2a3808108ef2',
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
var point = curve.pointFromY('02' +
|
|
86
|
+
'f8ca771306cd23e929775177f2c213843a017a6487b2ec5f9b2a3808108ef2');
|
|
87
|
+
assert(point.eq(target));
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('should find an odd point given a y coordinate', function() {
|
|
91
|
+
var curve = new elliptic.curve.edwards({
|
|
92
|
+
p: '7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed',
|
|
93
|
+
a: '-1',
|
|
94
|
+
c: '1',
|
|
95
|
+
// -121665 * (121666^(-1)) (mod P)
|
|
96
|
+
d: '52036cee2b6ffe73 8cc740797779e898 00700a4d4141d8ab 75eb4dca135978a3',
|
|
97
|
+
n: '1000000000000000 0000000000000000 14def9dea2f79cd6 5812631a5cf5d3ed',
|
|
98
|
+
gRed: false,
|
|
99
|
+
g: [
|
|
100
|
+
'216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a',
|
|
101
|
+
|
|
102
|
+
// 4/5
|
|
103
|
+
'6666666666666666666666666666666666666666666666666666666666666658',
|
|
104
|
+
],
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
var bytes = new Uint8Array([ 5, 69, 248, 173, 171, 254, 19, 253, 143, 140, 146, 174, 26, 128, 3, 52, 106, 55, 112, 245, 62, 127, 42, 93, 0, 81, 47, 177, 30, 25, 39, 70 ]);
|
|
108
|
+
var y = new BN(bytes, 16, 'le');
|
|
109
|
+
var point = curve.pointFromY(y, true);
|
|
110
|
+
var target = '2cd591ae3789fd62dc420a152002f79973a387eacecadc6a9a00c1a89488c15d';
|
|
111
|
+
assert.deepStrictEqual(point.getX().toString(16), target);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('should work with secp112k1', function() {
|
|
115
|
+
var curve = new elliptic.curve.short({
|
|
116
|
+
p: 'db7c 2abf62e3 5e668076 bead208b',
|
|
117
|
+
a: 'db7c 2abf62e3 5e668076 bead2088',
|
|
118
|
+
b: '659e f8ba0439 16eede89 11702b22',
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
var p = curve.point(
|
|
122
|
+
'0948 7239995a 5ee76b55 f9c2f098',
|
|
123
|
+
'a89c e5af8724 c0a23e0e 0ff77500');
|
|
124
|
+
assert(p.validate());
|
|
125
|
+
assert(p.dbl().validate());
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('should work with secp256k1', function() {
|
|
129
|
+
var curve = new elliptic.curve.short({
|
|
130
|
+
p: 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe ' +
|
|
131
|
+
'fffffc2f',
|
|
132
|
+
a: '0',
|
|
133
|
+
b: '7',
|
|
134
|
+
n: 'ffffffff ffffffff ffffffff fffffffe ' +
|
|
135
|
+
'baaedce6 af48a03b bfd25e8c d0364141',
|
|
136
|
+
g: [
|
|
137
|
+
'79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',
|
|
138
|
+
'483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8',
|
|
139
|
+
],
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
var p = curve.point(
|
|
143
|
+
'79be667e f9dcbbac 55a06295 ce870b07 029bfcdb 2dce28d9 59f2815b 16f81798',
|
|
144
|
+
'483ada77 26a3c465 5da4fbfc 0e1108a8 fd17b448 a6855419 9c47d08f fb10d4b8',
|
|
145
|
+
);
|
|
146
|
+
assert(p.validate());
|
|
147
|
+
assert(p.dbl().validate());
|
|
148
|
+
assert(p.toJ().dbl().toP().validate());
|
|
149
|
+
assert(p.mul(new BN('79be667e f9dcbbac 55a06295 ce870b07', 16)).validate());
|
|
150
|
+
|
|
151
|
+
var j = p.toJ();
|
|
152
|
+
assert(j.trpl().eq(j.dbl().add(j)));
|
|
153
|
+
|
|
154
|
+
// Endomorphism test
|
|
155
|
+
assert(curve.endo);
|
|
156
|
+
assert.equal(
|
|
157
|
+
curve.endo.beta.fromRed().toString(16),
|
|
158
|
+
'7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee');
|
|
159
|
+
assert.equal(
|
|
160
|
+
curve.endo.lambda.toString(16),
|
|
161
|
+
'5363ad4cc05c30e0a5261c028812645a122e22ea20816678df02967c1b23bd72');
|
|
162
|
+
|
|
163
|
+
var k = new BN('1234567890123456789012345678901234', 16);
|
|
164
|
+
var split = curve._endoSplit(k);
|
|
165
|
+
|
|
166
|
+
var testK = split.k1.add(split.k2.mul(curve.endo.lambda)).umod(curve.n);
|
|
167
|
+
assert.equal(testK.toString(16), k.toString(16));
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('should compute this problematic secp256k1 multiplication', function() {
|
|
171
|
+
var curve = elliptic.curves.secp256k1.curve;
|
|
172
|
+
var g1 = curve.g; // precomputed g
|
|
173
|
+
assert(g1.precomputed);
|
|
174
|
+
var g2 = curve.point(g1.getX(), g1.getY()); // not precomputed g
|
|
175
|
+
assert(!g2.precomputed);
|
|
176
|
+
var a = new BN(
|
|
177
|
+
'6d1229a6b24c2e775c062870ad26bc261051e0198c67203167273c7c62538846', 16);
|
|
178
|
+
var p1 = g1.mul(a);
|
|
179
|
+
var p2 = g2.mul(a);
|
|
180
|
+
assert(p1.eq(p2));
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it('should not use fixed NAF when k is too large', function() {
|
|
184
|
+
var curve = elliptic.curves.secp256k1.curve;
|
|
185
|
+
var g1 = curve.g; // precomputed g
|
|
186
|
+
assert(g1.precomputed);
|
|
187
|
+
var g2 = curve.point(g1.getX(), g1.getY()); // not precomputed g
|
|
188
|
+
assert(!g2.precomputed);
|
|
189
|
+
|
|
190
|
+
var a = new BN(
|
|
191
|
+
'6d1229a6b24c2e775c062870ad26bc26' +
|
|
192
|
+
'1051e0198c67203167273c7c6253884612345678',
|
|
193
|
+
16);
|
|
194
|
+
var p1 = g1.mul(a);
|
|
195
|
+
var p2 = g2.mul(a);
|
|
196
|
+
assert(p1.eq(p2));
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it('should not fail on secp256k1 regression', function() {
|
|
200
|
+
var curve = elliptic.curves.secp256k1.curve;
|
|
201
|
+
var k1 = new BN(
|
|
202
|
+
'32efeba414cd0c830aed727749e816a01c471831536fd2fce28c56b54f5a3bb1', 16);
|
|
203
|
+
var k2 = new BN(
|
|
204
|
+
'5f2e49b5d64e53f9811545434706cde4de528af97bfd49fde1f6cf792ee37a8c', 16);
|
|
205
|
+
|
|
206
|
+
var p1 = curve.g.mul(k1);
|
|
207
|
+
var p2 = curve.g.mul(k2);
|
|
208
|
+
|
|
209
|
+
// 2 + 2 + 1 = 2 + 1 + 2
|
|
210
|
+
var two = p2.dbl();
|
|
211
|
+
var five = two.dbl().add(p2);
|
|
212
|
+
var three = two.add(p2);
|
|
213
|
+
var maybeFive = three.add(two);
|
|
214
|
+
|
|
215
|
+
assert(maybeFive.eq(five));
|
|
216
|
+
|
|
217
|
+
p1 = p1.mul(k2);
|
|
218
|
+
p2 = p2.mul(k1);
|
|
219
|
+
|
|
220
|
+
assert(p1.validate());
|
|
221
|
+
assert(p2.validate());
|
|
222
|
+
assert(p1.eq(p2));
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it('should correctly double the affine point on secp256k1', function() {
|
|
226
|
+
var bad = {
|
|
227
|
+
x: '026a2073b1ef6fab47ace18e60e728a05180a82755bbcec9a0abc08ad9f7a3d4',
|
|
228
|
+
y: '9cd8cb48c3281596139f147c1364a3ede88d3f310fdb0eb98c924e599ca1b3c9',
|
|
229
|
+
z: 'd78587ad45e4102f48b54b5d85598296e069ce6085002e169c6bad78ddc6d9bd',
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
var good = {
|
|
233
|
+
x: 'e7789226739ac2eb3c7ccb2a9a910066beeed86cdb4e0f8a7fee8eeb29dc7016',
|
|
234
|
+
y: '4b76b191fd6d47d07828ea965e275b76d0e3e0196cd5056d38384fbb819f9fcb',
|
|
235
|
+
z: 'cbf8d99056618ba132d6145b904eee1ce566e0feedb9595139c45f84e90cfa7d',
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
var curve = elliptic.curves.secp256k1.curve;
|
|
239
|
+
bad = curve.jpoint(bad.x, bad.y, bad.z);
|
|
240
|
+
good = curve.jpoint(good.x, good.y, good.z);
|
|
241
|
+
|
|
242
|
+
// They are the same points
|
|
243
|
+
assert(bad.add(good.neg()).isInfinity());
|
|
244
|
+
|
|
245
|
+
// But doubling borks them out
|
|
246
|
+
assert(bad.dbl().add(good.dbl().neg()).isInfinity());
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it('should store precomputed values correctly on negation', function() {
|
|
250
|
+
var curve = elliptic.curves.secp256k1.curve;
|
|
251
|
+
var p = curve.g.mul('2');
|
|
252
|
+
p.precompute();
|
|
253
|
+
var neg = p.neg(true);
|
|
254
|
+
var neg2 = neg.neg(true);
|
|
255
|
+
assert(p.eq(neg2));
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
it('should correctly handle scalar multiplication of zero', function() {
|
|
259
|
+
var curve = elliptic.curves.secp256k1.curve;
|
|
260
|
+
var p1 = curve.g.mul('0');
|
|
261
|
+
var p2 = p1.mul('2');
|
|
262
|
+
assert(p1.eq(p2));
|
|
263
|
+
});
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
describe('Point codec', function () {
|
|
267
|
+
function makeShortTest(definition) {
|
|
268
|
+
var curve = elliptic.curves.secp256k1.curve;
|
|
269
|
+
|
|
270
|
+
return function() {
|
|
271
|
+
var co = definition.coordinates;
|
|
272
|
+
var p = curve.point(co.x, co.y);
|
|
273
|
+
|
|
274
|
+
// Encodes as expected
|
|
275
|
+
assert.equal(p.encode('hex'), definition.encoded);
|
|
276
|
+
assert.equal(p.encodeCompressed('hex'), definition.compactEncoded);
|
|
277
|
+
|
|
278
|
+
// Decodes as expected
|
|
279
|
+
assert(curve.decodePoint(definition.encoded, 'hex').eq(p));
|
|
280
|
+
assert(curve.decodePoint(definition.compactEncoded, 'hex').eq(p));
|
|
281
|
+
assert(curve.decodePoint(definition.hybrid, 'hex').eq(p));
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function makeMontTest(definition) {
|
|
286
|
+
var curve = elliptic.curves.curve25519.curve;
|
|
287
|
+
|
|
288
|
+
return function() {
|
|
289
|
+
var co = definition.coordinates;
|
|
290
|
+
var p = curve.point(co.x, co.z);
|
|
291
|
+
var encoded = p.encode('hex');
|
|
292
|
+
var decoded = curve.decodePoint(encoded, 'hex');
|
|
293
|
+
assert(decoded.eq(p));
|
|
294
|
+
assert.equal(encoded, definition.encoded);
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
var shortPointEvenY = {
|
|
299
|
+
coordinates: {
|
|
300
|
+
x: '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',
|
|
301
|
+
y: '483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8',
|
|
302
|
+
},
|
|
303
|
+
compactEncoded:
|
|
304
|
+
'02' +
|
|
305
|
+
'79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',
|
|
306
|
+
encoded:
|
|
307
|
+
'04' +
|
|
308
|
+
'79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798' +
|
|
309
|
+
'483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8',
|
|
310
|
+
hybrid:
|
|
311
|
+
'06' +
|
|
312
|
+
'79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798' +
|
|
313
|
+
'483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8',
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
var shortPointOddY = {
|
|
317
|
+
coordinates: {
|
|
318
|
+
x: 'fff97bd5755eeea420453a14355235d382f6472f8568a18b2f057a1460297556',
|
|
319
|
+
y: 'ae12777aacfbb620f3be96017f45c560de80f0f6518fe4a03c870c36b075f297',
|
|
320
|
+
},
|
|
321
|
+
compactEncoded:
|
|
322
|
+
'03' +
|
|
323
|
+
'fff97bd5755eeea420453a14355235d382f6472f8568a18b2f057a1460297556',
|
|
324
|
+
encoded:
|
|
325
|
+
'04' +
|
|
326
|
+
'fff97bd5755eeea420453a14355235d382f6472f8568a18b2f057a1460297556' +
|
|
327
|
+
'ae12777aacfbb620f3be96017f45c560de80f0f6518fe4a03c870c36b075f297',
|
|
328
|
+
hybrid:
|
|
329
|
+
'07' +
|
|
330
|
+
'fff97bd5755eeea420453a14355235d382f6472f8568a18b2f057a1460297556' +
|
|
331
|
+
'ae12777aacfbb620f3be96017f45c560de80f0f6518fe4a03c870c36b075f297',
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
it('should throw when trying to decode random bytes', function() {
|
|
335
|
+
assert.throws(function() {
|
|
336
|
+
elliptic.curves.secp256k1.curve.decodePoint(
|
|
337
|
+
'05' +
|
|
338
|
+
'79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798');
|
|
339
|
+
});
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
it('should be able to encode/decode a short curve point with even Y',
|
|
343
|
+
makeShortTest(shortPointEvenY));
|
|
344
|
+
|
|
345
|
+
it('should be able to encode/decode a short curve point with odd Y',
|
|
346
|
+
makeShortTest(shortPointOddY));
|
|
347
|
+
|
|
348
|
+
it('should be able to encode/decode a mont curve point', makeMontTest({
|
|
349
|
+
coordinates: {
|
|
350
|
+
// curve25519.curve.g.mul(new BN('6')).getX().toString(16, 2)
|
|
351
|
+
x: '26954ccdc99ebf34f8f1dde5e6bb080685fec73640494c28f9fe0bfa8c794531',
|
|
352
|
+
z: '1',
|
|
353
|
+
},
|
|
354
|
+
encoded:
|
|
355
|
+
'26954ccdc99ebf34f8f1dde5e6bb080685fec73640494c28f9fe0bfa8c794531',
|
|
356
|
+
}));
|
|
357
|
+
});
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/* eslint-env node, mocha */
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var assert = require('assert');
|
|
5
|
+
var elliptic = require('../');
|
|
6
|
+
|
|
7
|
+
describe('ECDH', function() {
|
|
8
|
+
function test(name) {
|
|
9
|
+
it('should work with ' + name + ' curve', function() {
|
|
10
|
+
var ecdh = new elliptic.ec(name);
|
|
11
|
+
var s1 = ecdh.genKeyPair();
|
|
12
|
+
var s2 = ecdh.genKeyPair();
|
|
13
|
+
var sh1 = s1.derive(s2.getPublic());
|
|
14
|
+
var sh2 = s2.derive(s1.getPublic());
|
|
15
|
+
|
|
16
|
+
assert.equal(sh1.toString(16), sh2.toString(16));
|
|
17
|
+
|
|
18
|
+
sh1 = s1.derive(ecdh.keyFromPublic(s2.getPublic('hex'), 'hex')
|
|
19
|
+
.getPublic());
|
|
20
|
+
sh2 = s2.derive(ecdh.keyFromPublic(s1.getPublic('hex'), 'hex')
|
|
21
|
+
.getPublic());
|
|
22
|
+
assert.equal(sh1.toString(16), sh2.toString(16));
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
test('curve25519');
|
|
27
|
+
test('ed25519');
|
|
28
|
+
test('secp256k1');
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe('ECDH twist attack', () => {
|
|
32
|
+
it('should be able to prevent a twist attack for secp256k1', () => {
|
|
33
|
+
var bobEcdh = new elliptic.ec('secp256k1');
|
|
34
|
+
var malloryEcdh = new elliptic.ec('secp256k1');
|
|
35
|
+
var bob = bobEcdh.genKeyPair();
|
|
36
|
+
// This is a bad point that shouldn't be able to be passed to derive.
|
|
37
|
+
// If a bad point can be passed it's possible to perform a twist attack.
|
|
38
|
+
var mallory = malloryEcdh.keyFromPublic({ x: 14, y: 16 });
|
|
39
|
+
assert.throws(function () {
|
|
40
|
+
bob.derive(mallory.getPublic());
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
});
|