From f259c7ebd7e2e7a77b3ba492902f30d3e581c83b Mon Sep 17 00:00:00 2001 From: Stefan Thomas Date: Sat, 21 Sep 2013 17:16:44 -0700 Subject: [PATCH] Montgomery: Fix infinite loop for leading zero inputs. Add test. --- src/js/sjcl-custom/sjcl-montgomery.js | 5 +++-- test/sjcl-montgomery.js | 8 ++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/js/sjcl-custom/sjcl-montgomery.js b/src/js/sjcl-custom/sjcl-montgomery.js index c44fa9f5..f1d66dee 100644 --- a/src/js/sjcl-custom/sjcl-montgomery.js +++ b/src/js/sjcl-custom/sjcl-montgomery.js @@ -68,10 +68,9 @@ Montgomery.prototype.reduce = function (x) while (x.limbs[j] >= radixMod) { x.limbs[j] -= radixMod; x.limbs[++j]++; } } x.trim(); - x.fullReduce(); x = x.shiftRight(this.mt * this.m.radix); if (x.greaterEquals(this.m)) x = x.sub(this.m); - return x; + return x.trim().normalize().reduce(); }; Montgomery.prototype.square = function (x) @@ -113,6 +112,8 @@ sjcl.bn.prototype.powermodMontgomery = function (e, m) var z = new Montgomery(m); + e.trim().normalize(); + // precomputation var g = new Array(), n = 3, k1 = k-1, km = (1<