From 5125c8b36acae7f8df9ce8edcd14048577c24698 Mon Sep 17 00:00:00 2001 From: Kirill Fomichev Date: Fri, 16 Feb 2018 14:02:28 +0300 Subject: [PATCH 1/3] bn: fix Red#imod --- lib/bn.js | 5 ++++- test/red-test.js | 9 +++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/bn.js b/lib/bn.js index 944259b..fe5c726 100644 --- a/lib/bn.js +++ b/lib/bn.js @@ -3169,7 +3169,10 @@ Red.prototype.imod = function imod (a) { if (this.prime) return this.prime.ireduce(a)._forceRed(this); - return a.umod(this.m)._forceRed(this); + + var mod = a.umod(this.m)._forceRed(this); + if (mod !== a) mod.copy(a); + return a; }; Red.prototype.neg = function neg (a) { diff --git a/test/red-test.js b/test/red-test.js index a50b2ec..f89f16a 100644 --- a/test/red-test.js +++ b/test/red-test.js @@ -262,4 +262,13 @@ describe('BN.js/Reduction context', function () { red.prime.split(input, output); assert.equal(input.cmp(output), 0); }); + + it('imod should change host object', function () { + var red = BN.red(new BN(13)); + var a = new BN(2).toRed(red); + var b = new BN(7).toRed(red); + var c = a.redIMul(b); + assert.equal(a.toNumber(), 1); + assert.equal(c.toNumber(), 1); + }); }); From b75f6c1eada09d2064e3071496c7dd5cba6990fc Mon Sep 17 00:00:00 2001 From: Kirill Fomichev Date: Fri, 9 Mar 2018 17:06:37 +0300 Subject: [PATCH 2/3] bn: add method move --- lib/bn.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/bn.js b/lib/bn.js index fe5c726..33f7c24 100644 --- a/lib/bn.js +++ b/lib/bn.js @@ -327,6 +327,13 @@ dest.red = this.red; }; + BN.prototype.move = function move (dest) { + dest.words = this.words; + dest.length = this.length; + dest.negative = this.negative; + dest.red = this.red; + }; + BN.prototype.clone = function clone () { var r = new BN(null); this.copy(r); @@ -3170,8 +3177,7 @@ Red.prototype.imod = function imod (a) { if (this.prime) return this.prime.ireduce(a)._forceRed(this); - var mod = a.umod(this.m)._forceRed(this); - if (mod !== a) mod.copy(a); + a.umod(this.m)._forceRed(this).move(a); return a; }; From 81593bea0d0b6fc05de52447d59b8dc16ae61e28 Mon Sep 17 00:00:00 2001 From: Kirill Fomichev Date: Wed, 6 Jun 2018 16:37:09 +0300 Subject: [PATCH 3/3] bn: rename BN#move to BN#_move --- lib/bn.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/bn.js b/lib/bn.js index 33f7c24..5eba6eb 100644 --- a/lib/bn.js +++ b/lib/bn.js @@ -327,7 +327,7 @@ dest.red = this.red; }; - BN.prototype.move = function move (dest) { + BN.prototype._move = function _move (dest) { dest.words = this.words; dest.length = this.length; dest.negative = this.negative; @@ -3177,7 +3177,7 @@ Red.prototype.imod = function imod (a) { if (this.prime) return this.prime.ireduce(a)._forceRed(this); - a.umod(this.m)._forceRed(this).move(a); + a.umod(this.m)._forceRed(this)._move(a); return a; };