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,435 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var utils = require('../utils');
|
|
4
|
+
var BN = require('bn.js');
|
|
5
|
+
var inherits = require('inherits');
|
|
6
|
+
var Base = require('./base');
|
|
7
|
+
|
|
8
|
+
var assert = utils.assert;
|
|
9
|
+
|
|
10
|
+
function EdwardsCurve(conf) {
|
|
11
|
+
// NOTE: Important as we are creating point in Base.call()
|
|
12
|
+
this.twisted = (conf.a | 0) !== 1;
|
|
13
|
+
this.mOneA = this.twisted && (conf.a | 0) === -1;
|
|
14
|
+
this.extended = this.mOneA;
|
|
15
|
+
|
|
16
|
+
Base.call(this, 'edwards', conf);
|
|
17
|
+
|
|
18
|
+
this.a = new BN(conf.a, 16).umod(this.red.m);
|
|
19
|
+
this.a = this.a.toRed(this.red);
|
|
20
|
+
this.c = new BN(conf.c, 16).toRed(this.red);
|
|
21
|
+
this.c2 = this.c.redSqr();
|
|
22
|
+
this.d = new BN(conf.d, 16).toRed(this.red);
|
|
23
|
+
this.dd = this.d.redAdd(this.d);
|
|
24
|
+
|
|
25
|
+
assert(!this.twisted || this.c.fromRed().cmpn(1) === 0);
|
|
26
|
+
this.oneC = (conf.c | 0) === 1;
|
|
27
|
+
}
|
|
28
|
+
inherits(EdwardsCurve, Base);
|
|
29
|
+
module.exports = EdwardsCurve;
|
|
30
|
+
|
|
31
|
+
EdwardsCurve.prototype._mulA = function _mulA(num) {
|
|
32
|
+
if (this.mOneA)
|
|
33
|
+
return num.redNeg();
|
|
34
|
+
else
|
|
35
|
+
return this.a.redMul(num);
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
EdwardsCurve.prototype._mulC = function _mulC(num) {
|
|
39
|
+
if (this.oneC)
|
|
40
|
+
return num;
|
|
41
|
+
else
|
|
42
|
+
return this.c.redMul(num);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// Just for compatibility with Short curve
|
|
46
|
+
EdwardsCurve.prototype.jpoint = function jpoint(x, y, z, t) {
|
|
47
|
+
return this.point(x, y, z, t);
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
EdwardsCurve.prototype.pointFromX = function pointFromX(x, odd) {
|
|
51
|
+
x = new BN(x, 16);
|
|
52
|
+
if (!x.red)
|
|
53
|
+
x = x.toRed(this.red);
|
|
54
|
+
|
|
55
|
+
var x2 = x.redSqr();
|
|
56
|
+
var rhs = this.c2.redSub(this.a.redMul(x2));
|
|
57
|
+
var lhs = this.one.redSub(this.c2.redMul(this.d).redMul(x2));
|
|
58
|
+
|
|
59
|
+
var y2 = rhs.redMul(lhs.redInvm());
|
|
60
|
+
var y = y2.redSqrt();
|
|
61
|
+
if (y.redSqr().redSub(y2).cmp(this.zero) !== 0)
|
|
62
|
+
throw new Error('invalid point');
|
|
63
|
+
|
|
64
|
+
var isOdd = y.fromRed().isOdd();
|
|
65
|
+
if (odd && !isOdd || !odd && isOdd)
|
|
66
|
+
y = y.redNeg();
|
|
67
|
+
|
|
68
|
+
return this.point(x, y);
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
EdwardsCurve.prototype.pointFromY = function pointFromY(y, odd) {
|
|
72
|
+
y = new BN(y, 16);
|
|
73
|
+
if (!y.red)
|
|
74
|
+
y = y.toRed(this.red);
|
|
75
|
+
|
|
76
|
+
// x^2 = (y^2 - c^2) / (c^2 d y^2 - a)
|
|
77
|
+
var y2 = y.redSqr();
|
|
78
|
+
var lhs = y2.redSub(this.c2);
|
|
79
|
+
var rhs = y2.redMul(this.d).redMul(this.c2).redSub(this.a);
|
|
80
|
+
var x2 = lhs.redMul(rhs.redInvm());
|
|
81
|
+
|
|
82
|
+
if (x2.cmp(this.zero) === 0) {
|
|
83
|
+
if (odd)
|
|
84
|
+
throw new Error('invalid point');
|
|
85
|
+
else
|
|
86
|
+
return this.point(this.zero, y);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
var x = x2.redSqrt();
|
|
90
|
+
if (x.redSqr().redSub(x2).cmp(this.zero) !== 0)
|
|
91
|
+
throw new Error('invalid point');
|
|
92
|
+
|
|
93
|
+
if (x.fromRed().isOdd() !== odd)
|
|
94
|
+
x = x.redNeg();
|
|
95
|
+
|
|
96
|
+
return this.point(x, y);
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
EdwardsCurve.prototype.validate = function validate(point) {
|
|
100
|
+
if (point.isInfinity())
|
|
101
|
+
return true;
|
|
102
|
+
|
|
103
|
+
// Curve: A * X^2 + Y^2 = C^2 * (1 + D * X^2 * Y^2)
|
|
104
|
+
point.normalize();
|
|
105
|
+
|
|
106
|
+
var x2 = point.x.redSqr();
|
|
107
|
+
var y2 = point.y.redSqr();
|
|
108
|
+
var lhs = x2.redMul(this.a).redAdd(y2);
|
|
109
|
+
var rhs = this.c2.redMul(this.one.redAdd(this.d.redMul(x2).redMul(y2)));
|
|
110
|
+
|
|
111
|
+
return lhs.cmp(rhs) === 0;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
function Point(curve, x, y, z, t) {
|
|
115
|
+
Base.BasePoint.call(this, curve, 'projective');
|
|
116
|
+
if (x === null && y === null && z === null) {
|
|
117
|
+
this.x = this.curve.zero;
|
|
118
|
+
this.y = this.curve.one;
|
|
119
|
+
this.z = this.curve.one;
|
|
120
|
+
this.t = this.curve.zero;
|
|
121
|
+
this.zOne = true;
|
|
122
|
+
} else {
|
|
123
|
+
this.x = new BN(x, 16);
|
|
124
|
+
this.y = new BN(y, 16);
|
|
125
|
+
this.z = z ? new BN(z, 16) : this.curve.one;
|
|
126
|
+
this.t = t && new BN(t, 16);
|
|
127
|
+
if (!this.x.red)
|
|
128
|
+
this.x = this.x.toRed(this.curve.red);
|
|
129
|
+
if (!this.y.red)
|
|
130
|
+
this.y = this.y.toRed(this.curve.red);
|
|
131
|
+
if (!this.z.red)
|
|
132
|
+
this.z = this.z.toRed(this.curve.red);
|
|
133
|
+
if (this.t && !this.t.red)
|
|
134
|
+
this.t = this.t.toRed(this.curve.red);
|
|
135
|
+
this.zOne = this.z === this.curve.one;
|
|
136
|
+
|
|
137
|
+
// Use extended coordinates
|
|
138
|
+
if (this.curve.extended && !this.t) {
|
|
139
|
+
this.t = this.x.redMul(this.y);
|
|
140
|
+
if (!this.zOne)
|
|
141
|
+
this.t = this.t.redMul(this.z.redInvm());
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
inherits(Point, Base.BasePoint);
|
|
146
|
+
|
|
147
|
+
EdwardsCurve.prototype.pointFromJSON = function pointFromJSON(obj) {
|
|
148
|
+
return Point.fromJSON(this, obj);
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
EdwardsCurve.prototype.point = function point(x, y, z, t) {
|
|
152
|
+
return new Point(this, x, y, z, t);
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
Point.fromJSON = function fromJSON(curve, obj) {
|
|
156
|
+
return new Point(curve, obj[0], obj[1], obj[2]);
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
Point.prototype.inspect = function inspect() {
|
|
160
|
+
if (this.isInfinity())
|
|
161
|
+
return '<EC Point Infinity>';
|
|
162
|
+
return '<EC Point x: ' + this.x.fromRed().toString(16, 2) +
|
|
163
|
+
' y: ' + this.y.fromRed().toString(16, 2) +
|
|
164
|
+
' z: ' + this.z.fromRed().toString(16, 2) + '>';
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
Point.prototype.isInfinity = function isInfinity() {
|
|
168
|
+
// XXX This code assumes that zero is always zero in red
|
|
169
|
+
return this.x.cmpn(0) === 0 &&
|
|
170
|
+
(this.y.cmp(this.z) === 0 ||
|
|
171
|
+
(this.zOne && this.y.cmp(this.curve.c) === 0));
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
Point.prototype._extDbl = function _extDbl() {
|
|
175
|
+
// hyperelliptic.org/EFD/g1p/auto-twisted-extended-1.html
|
|
176
|
+
// #doubling-dbl-2008-hwcd
|
|
177
|
+
// 4M + 4S
|
|
178
|
+
|
|
179
|
+
// A = X1^2
|
|
180
|
+
var a = this.x.redSqr();
|
|
181
|
+
// B = Y1^2
|
|
182
|
+
var b = this.y.redSqr();
|
|
183
|
+
// C = 2 * Z1^2
|
|
184
|
+
var c = this.z.redSqr();
|
|
185
|
+
c = c.redIAdd(c);
|
|
186
|
+
// D = a * A
|
|
187
|
+
var d = this.curve._mulA(a);
|
|
188
|
+
// E = (X1 + Y1)^2 - A - B
|
|
189
|
+
var e = this.x.redAdd(this.y).redSqr().redISub(a).redISub(b);
|
|
190
|
+
// G = D + B
|
|
191
|
+
var g = d.redAdd(b);
|
|
192
|
+
// F = G - C
|
|
193
|
+
var f = g.redSub(c);
|
|
194
|
+
// H = D - B
|
|
195
|
+
var h = d.redSub(b);
|
|
196
|
+
// X3 = E * F
|
|
197
|
+
var nx = e.redMul(f);
|
|
198
|
+
// Y3 = G * H
|
|
199
|
+
var ny = g.redMul(h);
|
|
200
|
+
// T3 = E * H
|
|
201
|
+
var nt = e.redMul(h);
|
|
202
|
+
// Z3 = F * G
|
|
203
|
+
var nz = f.redMul(g);
|
|
204
|
+
return this.curve.point(nx, ny, nz, nt);
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
Point.prototype._projDbl = function _projDbl() {
|
|
208
|
+
// hyperelliptic.org/EFD/g1p/auto-twisted-projective.html
|
|
209
|
+
// #doubling-dbl-2008-bbjlp
|
|
210
|
+
// #doubling-dbl-2007-bl
|
|
211
|
+
// and others
|
|
212
|
+
// Generally 3M + 4S or 2M + 4S
|
|
213
|
+
|
|
214
|
+
// B = (X1 + Y1)^2
|
|
215
|
+
var b = this.x.redAdd(this.y).redSqr();
|
|
216
|
+
// C = X1^2
|
|
217
|
+
var c = this.x.redSqr();
|
|
218
|
+
// D = Y1^2
|
|
219
|
+
var d = this.y.redSqr();
|
|
220
|
+
|
|
221
|
+
var nx;
|
|
222
|
+
var ny;
|
|
223
|
+
var nz;
|
|
224
|
+
var e;
|
|
225
|
+
var h;
|
|
226
|
+
var j;
|
|
227
|
+
if (this.curve.twisted) {
|
|
228
|
+
// E = a * C
|
|
229
|
+
e = this.curve._mulA(c);
|
|
230
|
+
// F = E + D
|
|
231
|
+
var f = e.redAdd(d);
|
|
232
|
+
if (this.zOne) {
|
|
233
|
+
// X3 = (B - C - D) * (F - 2)
|
|
234
|
+
nx = b.redSub(c).redSub(d).redMul(f.redSub(this.curve.two));
|
|
235
|
+
// Y3 = F * (E - D)
|
|
236
|
+
ny = f.redMul(e.redSub(d));
|
|
237
|
+
// Z3 = F^2 - 2 * F
|
|
238
|
+
nz = f.redSqr().redSub(f).redSub(f);
|
|
239
|
+
} else {
|
|
240
|
+
// H = Z1^2
|
|
241
|
+
h = this.z.redSqr();
|
|
242
|
+
// J = F - 2 * H
|
|
243
|
+
j = f.redSub(h).redISub(h);
|
|
244
|
+
// X3 = (B-C-D)*J
|
|
245
|
+
nx = b.redSub(c).redISub(d).redMul(j);
|
|
246
|
+
// Y3 = F * (E - D)
|
|
247
|
+
ny = f.redMul(e.redSub(d));
|
|
248
|
+
// Z3 = F * J
|
|
249
|
+
nz = f.redMul(j);
|
|
250
|
+
}
|
|
251
|
+
} else {
|
|
252
|
+
// E = C + D
|
|
253
|
+
e = c.redAdd(d);
|
|
254
|
+
// H = (c * Z1)^2
|
|
255
|
+
h = this.curve._mulC(this.z).redSqr();
|
|
256
|
+
// J = E - 2 * H
|
|
257
|
+
j = e.redSub(h).redSub(h);
|
|
258
|
+
// X3 = c * (B - E) * J
|
|
259
|
+
nx = this.curve._mulC(b.redISub(e)).redMul(j);
|
|
260
|
+
// Y3 = c * E * (C - D)
|
|
261
|
+
ny = this.curve._mulC(e).redMul(c.redISub(d));
|
|
262
|
+
// Z3 = E * J
|
|
263
|
+
nz = e.redMul(j);
|
|
264
|
+
}
|
|
265
|
+
return this.curve.point(nx, ny, nz);
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
Point.prototype.dbl = function dbl() {
|
|
269
|
+
if (this.isInfinity())
|
|
270
|
+
return this;
|
|
271
|
+
|
|
272
|
+
// Double in extended coordinates
|
|
273
|
+
if (this.curve.extended)
|
|
274
|
+
return this._extDbl();
|
|
275
|
+
else
|
|
276
|
+
return this._projDbl();
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
Point.prototype._extAdd = function _extAdd(p) {
|
|
280
|
+
// hyperelliptic.org/EFD/g1p/auto-twisted-extended-1.html
|
|
281
|
+
// #addition-add-2008-hwcd-3
|
|
282
|
+
// 8M
|
|
283
|
+
|
|
284
|
+
// A = (Y1 - X1) * (Y2 - X2)
|
|
285
|
+
var a = this.y.redSub(this.x).redMul(p.y.redSub(p.x));
|
|
286
|
+
// B = (Y1 + X1) * (Y2 + X2)
|
|
287
|
+
var b = this.y.redAdd(this.x).redMul(p.y.redAdd(p.x));
|
|
288
|
+
// C = T1 * k * T2
|
|
289
|
+
var c = this.t.redMul(this.curve.dd).redMul(p.t);
|
|
290
|
+
// D = Z1 * 2 * Z2
|
|
291
|
+
var d = this.z.redMul(p.z.redAdd(p.z));
|
|
292
|
+
// E = B - A
|
|
293
|
+
var e = b.redSub(a);
|
|
294
|
+
// F = D - C
|
|
295
|
+
var f = d.redSub(c);
|
|
296
|
+
// G = D + C
|
|
297
|
+
var g = d.redAdd(c);
|
|
298
|
+
// H = B + A
|
|
299
|
+
var h = b.redAdd(a);
|
|
300
|
+
// X3 = E * F
|
|
301
|
+
var nx = e.redMul(f);
|
|
302
|
+
// Y3 = G * H
|
|
303
|
+
var ny = g.redMul(h);
|
|
304
|
+
// T3 = E * H
|
|
305
|
+
var nt = e.redMul(h);
|
|
306
|
+
// Z3 = F * G
|
|
307
|
+
var nz = f.redMul(g);
|
|
308
|
+
return this.curve.point(nx, ny, nz, nt);
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
Point.prototype._projAdd = function _projAdd(p) {
|
|
312
|
+
// hyperelliptic.org/EFD/g1p/auto-twisted-projective.html
|
|
313
|
+
// #addition-add-2008-bbjlp
|
|
314
|
+
// #addition-add-2007-bl
|
|
315
|
+
// 10M + 1S
|
|
316
|
+
|
|
317
|
+
// A = Z1 * Z2
|
|
318
|
+
var a = this.z.redMul(p.z);
|
|
319
|
+
// B = A^2
|
|
320
|
+
var b = a.redSqr();
|
|
321
|
+
// C = X1 * X2
|
|
322
|
+
var c = this.x.redMul(p.x);
|
|
323
|
+
// D = Y1 * Y2
|
|
324
|
+
var d = this.y.redMul(p.y);
|
|
325
|
+
// E = d * C * D
|
|
326
|
+
var e = this.curve.d.redMul(c).redMul(d);
|
|
327
|
+
// F = B - E
|
|
328
|
+
var f = b.redSub(e);
|
|
329
|
+
// G = B + E
|
|
330
|
+
var g = b.redAdd(e);
|
|
331
|
+
// X3 = A * F * ((X1 + Y1) * (X2 + Y2) - C - D)
|
|
332
|
+
var tmp = this.x.redAdd(this.y).redMul(p.x.redAdd(p.y)).redISub(c).redISub(d);
|
|
333
|
+
var nx = a.redMul(f).redMul(tmp);
|
|
334
|
+
var ny;
|
|
335
|
+
var nz;
|
|
336
|
+
if (this.curve.twisted) {
|
|
337
|
+
// Y3 = A * G * (D - a * C)
|
|
338
|
+
ny = a.redMul(g).redMul(d.redSub(this.curve._mulA(c)));
|
|
339
|
+
// Z3 = F * G
|
|
340
|
+
nz = f.redMul(g);
|
|
341
|
+
} else {
|
|
342
|
+
// Y3 = A * G * (D - C)
|
|
343
|
+
ny = a.redMul(g).redMul(d.redSub(c));
|
|
344
|
+
// Z3 = c * F * G
|
|
345
|
+
nz = this.curve._mulC(f).redMul(g);
|
|
346
|
+
}
|
|
347
|
+
return this.curve.point(nx, ny, nz);
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
Point.prototype.add = function add(p) {
|
|
351
|
+
if (this.isInfinity())
|
|
352
|
+
return p;
|
|
353
|
+
if (p.isInfinity())
|
|
354
|
+
return this;
|
|
355
|
+
|
|
356
|
+
if (this.curve.extended)
|
|
357
|
+
return this._extAdd(p);
|
|
358
|
+
else
|
|
359
|
+
return this._projAdd(p);
|
|
360
|
+
};
|
|
361
|
+
|
|
362
|
+
Point.prototype.mul = function mul(k) {
|
|
363
|
+
if (this._hasDoubles(k))
|
|
364
|
+
return this.curve._fixedNafMul(this, k);
|
|
365
|
+
else
|
|
366
|
+
return this.curve._wnafMul(this, k);
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
Point.prototype.mulAdd = function mulAdd(k1, p, k2) {
|
|
370
|
+
return this.curve._wnafMulAdd(1, [ this, p ], [ k1, k2 ], 2, false);
|
|
371
|
+
};
|
|
372
|
+
|
|
373
|
+
Point.prototype.jmulAdd = function jmulAdd(k1, p, k2) {
|
|
374
|
+
return this.curve._wnafMulAdd(1, [ this, p ], [ k1, k2 ], 2, true);
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
Point.prototype.normalize = function normalize() {
|
|
378
|
+
if (this.zOne)
|
|
379
|
+
return this;
|
|
380
|
+
|
|
381
|
+
// Normalize coordinates
|
|
382
|
+
var zi = this.z.redInvm();
|
|
383
|
+
this.x = this.x.redMul(zi);
|
|
384
|
+
this.y = this.y.redMul(zi);
|
|
385
|
+
if (this.t)
|
|
386
|
+
this.t = this.t.redMul(zi);
|
|
387
|
+
this.z = this.curve.one;
|
|
388
|
+
this.zOne = true;
|
|
389
|
+
return this;
|
|
390
|
+
};
|
|
391
|
+
|
|
392
|
+
Point.prototype.neg = function neg() {
|
|
393
|
+
return this.curve.point(this.x.redNeg(),
|
|
394
|
+
this.y,
|
|
395
|
+
this.z,
|
|
396
|
+
this.t && this.t.redNeg());
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
Point.prototype.getX = function getX() {
|
|
400
|
+
this.normalize();
|
|
401
|
+
return this.x.fromRed();
|
|
402
|
+
};
|
|
403
|
+
|
|
404
|
+
Point.prototype.getY = function getY() {
|
|
405
|
+
this.normalize();
|
|
406
|
+
return this.y.fromRed();
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
Point.prototype.eq = function eq(other) {
|
|
410
|
+
return this === other ||
|
|
411
|
+
this.getX().cmp(other.getX()) === 0 &&
|
|
412
|
+
this.getY().cmp(other.getY()) === 0;
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
Point.prototype.eqXToP = function eqXToP(x) {
|
|
416
|
+
var rx = x.toRed(this.curve.red).redMul(this.z);
|
|
417
|
+
if (this.x.cmp(rx) === 0)
|
|
418
|
+
return true;
|
|
419
|
+
|
|
420
|
+
var xc = x.clone();
|
|
421
|
+
var t = this.curve.redN.redMul(this.z);
|
|
422
|
+
for (;;) {
|
|
423
|
+
xc.iadd(this.curve.n);
|
|
424
|
+
if (xc.cmp(this.curve.p) >= 0)
|
|
425
|
+
return false;
|
|
426
|
+
|
|
427
|
+
rx.redIAdd(t);
|
|
428
|
+
if (this.x.cmp(rx) === 0)
|
|
429
|
+
return true;
|
|
430
|
+
}
|
|
431
|
+
};
|
|
432
|
+
|
|
433
|
+
// Compatibility with BaseCurve
|
|
434
|
+
Point.prototype.toP = Point.prototype.normalize;
|
|
435
|
+
Point.prototype.mixedAdd = Point.prototype.add;
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var BN = require('bn.js');
|
|
4
|
+
var inherits = require('inherits');
|
|
5
|
+
var Base = require('./base');
|
|
6
|
+
|
|
7
|
+
var utils = require('../utils');
|
|
8
|
+
|
|
9
|
+
function MontCurve(conf) {
|
|
10
|
+
Base.call(this, 'mont', conf);
|
|
11
|
+
|
|
12
|
+
this.a = new BN(conf.a, 16).toRed(this.red);
|
|
13
|
+
this.b = new BN(conf.b, 16).toRed(this.red);
|
|
14
|
+
this.i4 = new BN(4).toRed(this.red).redInvm();
|
|
15
|
+
this.two = new BN(2).toRed(this.red);
|
|
16
|
+
this.a24 = this.i4.redMul(this.a.redAdd(this.two));
|
|
17
|
+
}
|
|
18
|
+
inherits(MontCurve, Base);
|
|
19
|
+
module.exports = MontCurve;
|
|
20
|
+
|
|
21
|
+
MontCurve.prototype.validate = function validate(point) {
|
|
22
|
+
var x = point.normalize().x;
|
|
23
|
+
var x2 = x.redSqr();
|
|
24
|
+
var rhs = x2.redMul(x).redAdd(x2.redMul(this.a)).redAdd(x);
|
|
25
|
+
var y = rhs.redSqrt();
|
|
26
|
+
|
|
27
|
+
return y.redSqr().cmp(rhs) === 0;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
function Point(curve, x, z) {
|
|
31
|
+
Base.BasePoint.call(this, curve, 'projective');
|
|
32
|
+
if (x === null && z === null) {
|
|
33
|
+
this.x = this.curve.one;
|
|
34
|
+
this.z = this.curve.zero;
|
|
35
|
+
} else {
|
|
36
|
+
this.x = new BN(x, 16);
|
|
37
|
+
this.z = new BN(z, 16);
|
|
38
|
+
if (!this.x.red)
|
|
39
|
+
this.x = this.x.toRed(this.curve.red);
|
|
40
|
+
if (!this.z.red)
|
|
41
|
+
this.z = this.z.toRed(this.curve.red);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
inherits(Point, Base.BasePoint);
|
|
45
|
+
|
|
46
|
+
MontCurve.prototype.decodePoint = function decodePoint(bytes, enc) {
|
|
47
|
+
return this.point(utils.toArray(bytes, enc), 1);
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
MontCurve.prototype.point = function point(x, z) {
|
|
51
|
+
return new Point(this, x, z);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
MontCurve.prototype.pointFromJSON = function pointFromJSON(obj) {
|
|
55
|
+
return Point.fromJSON(this, obj);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
Point.prototype.precompute = function precompute() {
|
|
59
|
+
// No-op
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
Point.prototype._encode = function _encode() {
|
|
63
|
+
return this.getX().toArray('be', this.curve.p.byteLength());
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
Point.fromJSON = function fromJSON(curve, obj) {
|
|
67
|
+
return new Point(curve, obj[0], obj[1] || curve.one);
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
Point.prototype.inspect = function inspect() {
|
|
71
|
+
if (this.isInfinity())
|
|
72
|
+
return '<EC Point Infinity>';
|
|
73
|
+
return '<EC Point x: ' + this.x.fromRed().toString(16, 2) +
|
|
74
|
+
' z: ' + this.z.fromRed().toString(16, 2) + '>';
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
Point.prototype.isInfinity = function isInfinity() {
|
|
78
|
+
// XXX This code assumes that zero is always zero in red
|
|
79
|
+
return this.z.cmpn(0) === 0;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
Point.prototype.dbl = function dbl() {
|
|
83
|
+
// http://hyperelliptic.org/EFD/g1p/auto-montgom-xz.html#doubling-dbl-1987-m-3
|
|
84
|
+
// 2M + 2S + 4A
|
|
85
|
+
|
|
86
|
+
// A = X1 + Z1
|
|
87
|
+
var a = this.x.redAdd(this.z);
|
|
88
|
+
// AA = A^2
|
|
89
|
+
var aa = a.redSqr();
|
|
90
|
+
// B = X1 - Z1
|
|
91
|
+
var b = this.x.redSub(this.z);
|
|
92
|
+
// BB = B^2
|
|
93
|
+
var bb = b.redSqr();
|
|
94
|
+
// C = AA - BB
|
|
95
|
+
var c = aa.redSub(bb);
|
|
96
|
+
// X3 = AA * BB
|
|
97
|
+
var nx = aa.redMul(bb);
|
|
98
|
+
// Z3 = C * (BB + A24 * C)
|
|
99
|
+
var nz = c.redMul(bb.redAdd(this.curve.a24.redMul(c)));
|
|
100
|
+
return this.curve.point(nx, nz);
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
Point.prototype.add = function add() {
|
|
104
|
+
throw new Error('Not supported on Montgomery curve');
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
Point.prototype.diffAdd = function diffAdd(p, diff) {
|
|
108
|
+
// http://hyperelliptic.org/EFD/g1p/auto-montgom-xz.html#diffadd-dadd-1987-m-3
|
|
109
|
+
// 4M + 2S + 6A
|
|
110
|
+
|
|
111
|
+
// A = X2 + Z2
|
|
112
|
+
var a = this.x.redAdd(this.z);
|
|
113
|
+
// B = X2 - Z2
|
|
114
|
+
var b = this.x.redSub(this.z);
|
|
115
|
+
// C = X3 + Z3
|
|
116
|
+
var c = p.x.redAdd(p.z);
|
|
117
|
+
// D = X3 - Z3
|
|
118
|
+
var d = p.x.redSub(p.z);
|
|
119
|
+
// DA = D * A
|
|
120
|
+
var da = d.redMul(a);
|
|
121
|
+
// CB = C * B
|
|
122
|
+
var cb = c.redMul(b);
|
|
123
|
+
// X5 = Z1 * (DA + CB)^2
|
|
124
|
+
var nx = diff.z.redMul(da.redAdd(cb).redSqr());
|
|
125
|
+
// Z5 = X1 * (DA - CB)^2
|
|
126
|
+
var nz = diff.x.redMul(da.redISub(cb).redSqr());
|
|
127
|
+
return this.curve.point(nx, nz);
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
Point.prototype.mul = function mul(k) {
|
|
131
|
+
var t = k.clone();
|
|
132
|
+
var a = this; // (N / 2) * Q + Q
|
|
133
|
+
var b = this.curve.point(null, null); // (N / 2) * Q
|
|
134
|
+
var c = this; // Q
|
|
135
|
+
|
|
136
|
+
for (var bits = []; t.cmpn(0) !== 0; t.iushrn(1))
|
|
137
|
+
bits.push(t.andln(1));
|
|
138
|
+
|
|
139
|
+
for (var i = bits.length - 1; i >= 0; i--) {
|
|
140
|
+
if (bits[i] === 0) {
|
|
141
|
+
// N * Q + Q = ((N / 2) * Q + Q)) + (N / 2) * Q
|
|
142
|
+
a = a.diffAdd(b, c);
|
|
143
|
+
// N * Q = 2 * ((N / 2) * Q + Q))
|
|
144
|
+
b = b.dbl();
|
|
145
|
+
} else {
|
|
146
|
+
// N * Q = ((N / 2) * Q + Q) + ((N / 2) * Q)
|
|
147
|
+
b = a.diffAdd(b, c);
|
|
148
|
+
// N * Q + Q = 2 * ((N / 2) * Q + Q)
|
|
149
|
+
a = a.dbl();
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return b;
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
Point.prototype.mulAdd = function mulAdd() {
|
|
156
|
+
throw new Error('Not supported on Montgomery curve');
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
Point.prototype.jumlAdd = function jumlAdd() {
|
|
160
|
+
throw new Error('Not supported on Montgomery curve');
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
Point.prototype.eq = function eq(other) {
|
|
164
|
+
return this.getX().cmp(other.getX()) === 0;
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
Point.prototype.normalize = function normalize() {
|
|
168
|
+
this.x = this.x.redMul(this.z.redInvm());
|
|
169
|
+
this.z = this.curve.one;
|
|
170
|
+
return this;
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
Point.prototype.getX = function getX() {
|
|
174
|
+
// Normalize coordinates
|
|
175
|
+
this.normalize();
|
|
176
|
+
|
|
177
|
+
return this.x.fromRed();
|
|
178
|
+
};
|