8000 curve: remove `_maxwellAdjust` · etherscan-io/elliptic@725bd91 · GitHub
[go: up one dir, main page]

Skip to content

Commit 725bd91

Browse files
committed
curve: remove _maxwellAdjust
See: https://git.io/vo8mB
1 parent e102a69 commit 725bd91

File tree

5 files changed

+105
-5
lines changed

5 files changed

+105
-5
lines changed

lib/elliptic/curve/base.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,9 @@ function BaseCurve(type, conf) {
3232
// Generalized Greg Maxwell's trick
3333
var adjustCount = this.n && this.p.div(this.n);
3434
if (!adjustCount || adjustCount.cmpn(100) > 0) {
35-
this._maxwellAdjust = 0;
3635
this.redN = null;
3736
} else {
38-
this._maxwellAdjust = adjustCount.toNumber();
37+
this._maxwellTrick = true;
3938
this.redN = this.n.toRed(this.red);
4039
}
4140
}

lib/elliptic/curve/edwards.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ Point.prototype.eqXToP = function eqXToP(x) {
416416

417417
var xc = x.clone();
418418
var t = this.curve.redN.redMul(this.z);
419-
for (var i = 1; i <= this.curve._maxwellAdjust; i++) {
419+
for (;;) {
420420
xc.iadd(this.curve.n);
421421
if (xc.cmp(this.curve.p) >= 0)
422422
return false;

lib/elliptic/curve/short.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ JPoint.prototype.eqXToP = function eqXToP(x) {
912912

913913
var xc = x.clone();
914914
var t = this.curve.redN.redMul(zs);
915-
for (var i = 1; i <= this.curve._maxwellAdjust; i++) {
915+
for (;;) {
916916
xc.iadd(this.curve.n);
917917
if (xc.cmp(this.curve.p) >= 0)
918918
return false;

lib/elliptic/ec/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ EC.prototype.verify = function verify(msg, signature, key, enc) {
166166
var u1 = sinv.mul(msg).umod(this.n);
167167
var u2 = sinv.mul(r).umod(this.n);
168168

169-
if (this.curve._maxwellAdjust === 0) {
169+
if (!this.curve._maxwellTrick) {
170170
var p = this.g.mulAdd(u1, key.getPublic(), u2);
171171
if (p.isInfinity())
172172
return false;

test/ecdsa-test.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,107 @@ describe('ECDSA', function() {
345345
});
346346
});
347347

348+
describe('Maxwell\'s trick', function() {
349+
var p256 = elliptic.curves.p256;
350+
assert(p256);
351+
var p384 = elliptic.curves.p384;
352+
assert(p384);
353+
354+
var msg =
355+
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855';
356+
357+
var vectors = [
358+
{
359+
curve: p256,
360+
pub: '041548fc88953e06cd34d4b300804c5322cb48c24aaaa4d0' +
361+
'7a541b0f0ccfeedeb0ae4991b90519ea405588bdf699f5e6' +
362+
'd0c6b2d5217a5c16e8371062737aa1dae1',
363+
message: msg,
364+
sig: '3006020106020104',
365+
result: true
366+
},
367+
{
368+
curve: p256,
369+
pub: '04ad8f60e4ec1ebdb6a260b559cb55b1e9d2c5ddd43a41a2' +
370+
'd11b0741ef2567d84e166737664104ebbc337af3d861d352' +
371+
'4cfbc761c12edae974a0759750c8324f9a',
372+
message: msg,
373+
sig: '3006020106020104',
374+
result: true
375+
},
376+
{
377+
curve: p256,
378+
pub: '0445bd879143a64af5746e2e82aa65fd2ea07bba4e355940' +
379+
'95a981b59984dacb219d59697387ac721b1f1eccf4b11f43' +
380+
'ddc39e8367147abab3084142ed3ea170e4',
381+
message: msg,
382+
sig: '301502104319055358e8617b0c46353d039cdaae020104',
383+
result: true
384+
},
385+
{
386+
curve: p256,
387+
pub: '040feb5df4cc78b35ec9c180cc0de5842f75f088b4845697' +
388+
'8ffa98e716d94883e1e6500b2a1f6c1d9d493428d7ae7d9a' +
389+
'8a560fff30a3d14aa160be0c5e7edcd887',
390+
message: msg,
391+
sig: '301502104319055358e8617b0c46353d039cdaae020104',
392+
result: false
393+
},
394+
{
395+
curve: p384,
396+
pub: '0425e299eea9927b39fa92417705391bf17e8110b4615e9e' +
397+
'b5da471b57be0c30e7d89dbdc3e5da4eae029b300344d385' +
398+
'1548b59ed8be668813905105e673319d59d32f574e180568' +
399+
'463c6186864888f6c0b67b304441f82aab031279e48f047c31',
400+
message: msg,
401+
sig: '3006020103020104',
402+
result: true
403+
},
404+
{
405+
curve: p384,
406+
pub: '04a328f65c22307188b4af65779c1d2ec821c6748c6bd8dc' +
407+
'0e6a008135f048f832df501f7f3f79966b03d5bef2f187ec' +
408+
'34d85f6a934af465656fb4eea8dd9176ab80fbb4a27a649f' +
409+
'526a7dfe616091b78d293552bc093dfde9b31cae69d51d3afb',
410+
message: msg,
411+
sig: '3006020103020104',
412+
result: true
413+
},
414+
{
415+
curve: p384,
416+
pub: '04242e8585eaa7a28cc6062cab4c9c5fd536f46b17be1728' +
417+
'288a2cda5951df4941aed1d712defda023d10aca1c5ee014' +
418+
'43e8beacd821f7efa27847418ab95ce2c514b2b6b395ee73' +
419+
'417c83dbcad631421f360d84d64658c98a62d685b220f5aad4',
420+
message: msg,
421+
sig: '301d0218389cb27e0bc8d21fa7e5f24cb74f58851313e696333ad68e020104',
422+
result: true
423+
},
424+
{
425+
curve: p384,
426+
pub: '04cdf865dd743fe1c23757ec5e65fd5e4038b472ded2af26' +
427+
'1e3d8343c595c8b69147df46379c7ca40e60e80170d34a11' +
428+
'88dbb2b6f7d3934c23d2f78cfb0db3f3219959fad63c9b61' +
429+
'2ef2f20d679777b84192ce86e781c14b1bbb77eacd6e0520e2',
430+
message: msg,
431+
sig: '301d0218389cb27e0bc8d21fa7e5f24cb74f58851313e696333ad68e020104',
432+
result: false
433+
}
434+
];
435+
436+
vectors.forEach(function(vector, i) {
437+
it('should pass on vector#' + i, function() {
438+
var ecdsa = new elliptic.ec(vector.curve);
439+
var key = ecdsa.keyFromPublic(vector.pub, 'hex');
440+
var msg = vector.message;
441+
var sig = vector.sig;
442+
443+
var actual = ecdsa.verify(msg, sig, key);
444+
assert.equal(actual, vector.result);
445+
});
446+
});
447+
});
448+
348449
it('should deterministically generate private key', function() {
349450
var curve = elliptic.curves.secp256k1;
350451
assert(curve);

0 commit comments

Comments
 (0)
0