diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2013-09-25 18:02:34 +0400 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2013-09-25 18:02:34 +0400 |
commit | 9e83ae1a64225bb01158d6b89cbe21f4a8cce89c (patch) | |
tree | 934f177d891adc59466c63d0ba358ccfcf29d779 /core/src/main/java/org/bouncycastle/math | |
parent | 58148f085275c7e87dbf7b37bb8de39bcfe1fb34 (diff) |
Add method generateCompactWindowNaf
Diffstat (limited to 'core/src/main/java/org/bouncycastle/math')
-rw-r--r-- | core/src/main/java/org/bouncycastle/math/ec/WNafUtil.java | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/core/src/main/java/org/bouncycastle/math/ec/WNafUtil.java b/core/src/main/java/org/bouncycastle/math/ec/WNafUtil.java index 3d556b19..834c4877 100644 --- a/core/src/main/java/org/bouncycastle/math/ec/WNafUtil.java +++ b/core/src/main/java/org/bouncycastle/math/ec/WNafUtil.java @@ -39,6 +39,64 @@ public abstract class WNafUtil return naf; } + public static int[] generateCompactWindowNaf(int width, BigInteger k) + { + if (width == 2) + { + return generateCompactNaf(k); + } + + if (width < 2 || width > 8) + { + throw new IllegalArgumentException("'width' must be in the range [2, 8]"); + } + + int[] wnaf = new int[(k.bitLength() + width - 1) / width]; + + // 2^width and a mask and sign bit set accordingly + int pow2 = 1 << width; + int mask = pow2 - 1; + int sign = pow2 >>> 1; + + boolean carry = false; + int length = 0, pos = 0; + + while (pos <= k.bitLength()) + { + if (k.testBit(pos) == carry) + { + ++pos; + continue; + } + + k = k.shiftRight(pos); + + int digit = k.intValue() & mask; + if (carry) + { + ++digit; + } + + carry = (digit & sign) != 0; + if (carry) + { + digit -= pow2; + } + + int zeroes = length > 0 ? pos - 1 : pos; + wnaf[length++] = (digit << 16) | zeroes; + pos = width; + } + + // Reduce the WNAF array to its actual length + if (wnaf.length > length) + { + wnaf = trim(wnaf, length); + } + + return wnaf; + } + public static byte[] generateNaf(BigInteger k) { BigInteger _3k = k.shiftLeft(1).add(k); |