Skip to content

Commit

Permalink
Update getNAF
Browse files Browse the repository at this point in the history
  • Loading branch information
fanatid committed Jan 10, 2016
1 parent dc2a0ef commit e8152f4
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,34 @@ exports.getNAF = function (num, w) {
var naf = []
var ws = 1 << w
var ws2m1 = (ws << 1) - 1
for (var k = num.clone(); k.cmpn(0) === 1; k.iushrn(1)) {
var z = 0
if (k.isOdd()) {
var mod = k.andln(ws2m1)
z = (mod >= ws) ? (ws - mod) : mod
k.isubn(z)

var k = num.clone()
while (!k.isZero()) {
var i = 0
for (var d = 1; (k.words[0] & d) === 0 && i < 26; ++i, d <<= 1) {
naf.push(0)
}

if (i !== 0) {
k.iushrn(i)
} else {
var mod = k.words[0] & ws2m1
if (mod >= ws) {
naf.push(ws - mod)
k.words[0] -= ws - mod
k.iushrn(1)
} else {
k.words[0] -= mod
naf.push(mod)
if (!k.isZero()) {
for (i = w - 1; i > 0; --i) {
naf.push(0)
}

k.iushrn(w)
}
}
}
naf.push(z)
}

return naf
Expand Down

0 comments on commit e8152f4

Please sign in to comment.