diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2013-10-01 06:12:07 +0400 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2013-10-01 06:12:07 +0400 |
commit | d24b08173c984922a2fff98d64b219875f151ed0 (patch) | |
tree | f0c22c1fa0424f867b80f8779f0745117204c088 /core/src/main/java/org/bouncycastle/math | |
parent | cf8715b17f8fca5848893e082e4b8999e0d9b67b (diff) |
Use an 8-bit precomputation table for squaring
Diffstat (limited to 'core/src/main/java/org/bouncycastle/math')
-rw-r--r-- | core/src/main/java/org/bouncycastle/math/ec/IntArray.java | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/core/src/main/java/org/bouncycastle/math/ec/IntArray.java b/core/src/main/java/org/bouncycastle/math/ec/IntArray.java index 10f709ba..7f1158f1 100644 --- a/core/src/main/java/org/bouncycastle/math/ec/IntArray.java +++ b/core/src/main/java/org/bouncycastle/math/ec/IntArray.java @@ -6,11 +6,25 @@ import java.math.BigInteger; class IntArray { - private static final int[] SQUARING_TABLE = + private static final int[] generateSquare8Table() { - 0x00000000, 0x01000000, 0x04000000, 0x05000000, 0x10000000, 0x11000000, 0x14000000, 0x15000000, - 0x40000000, 0x41000000, 0x44000000, 0x45000000, 0x50000000, 0x51000000, 0x54000000, 0x55000000 - }; + int[] t = new int[256]; + for (int i = 0; i < 256; ++i) + { + int v = 0; + for (int bit = 0; bit < 8; ++bit) + { + if ((i & (1 << bit)) != 0) + { + v ^= i << bit; + } + } + t[i] = v << 16; + } + return t; + } + + private static final int[] SQUARE8_TABLE = generateSquare8Table(); // For toString(); must have length 32 private static final String ZEROES = "00000000000000000000000000000000"; @@ -666,14 +680,7 @@ class IntArray private static int square16(int n) { - int x = SQUARING_TABLE[n & 0xF]; - x >>>= 8; n >>>= 4; - x |= SQUARING_TABLE[n & 0xF]; - x >>>= 8; n >>>= 4; - x |= SQUARING_TABLE[n & 0xF]; - x >>>= 8; n >>>= 4; - x |= SQUARING_TABLE[n]; - return x; + return (SQUARE8_TABLE[n & 0xFF] >>> 16) | SQUARE8_TABLE[n >>> 8]; } public boolean equals(Object o) |