Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/quite/humla-spongycastle.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2013-10-03 06:47:06 +0400
committerPeter Dettman <peter.dettman@bouncycastle.org>2013-10-03 06:47:06 +0400
commitdb8968be90af50ed8fdcc531ab7f750e2d9a21f6 (patch)
tree37fefa59659be3c9f04af4c8054b76fdee576061 /core/src/main/java/org/bouncycastle/math
parent164179d20cf4d2533633ad11bc04b29c8c45530e (diff)
Various small refactorings
Diffstat (limited to 'core/src/main/java/org/bouncycastle/math')
-rw-r--r--core/src/main/java/org/bouncycastle/math/ec/IntArray.java52
-rw-r--r--core/src/main/java/org/bouncycastle/math/ec/LongArray.java42
2 files changed, 71 insertions, 23 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 50a5fac9..fe5948a4 100644
--- a/core/src/main/java/org/bouncycastle/math/ec/IntArray.java
+++ b/core/src/main/java/org/bouncycastle/math/ec/IntArray.java
@@ -79,7 +79,7 @@ class IntArray
throw new IllegalArgumentException("invalid F2m field value");
}
- if (bigInt.equals(ECConstants.ZERO))
+ if (bigInt.signum() == 0)
{
m_ints = new int[] { 0 };
return;
@@ -128,8 +128,15 @@ class IntArray
public boolean isZero()
{
- return m_ints.length == 0
- || (m_ints[0] == 0 && getUsedLength() == 0);
+ int[] a = m_ints;
+ for (int i = 0; i < a.length; ++i)
+ {
+ if (a[i] != 0)
+ {
+ return false;
+ }
+ }
+ return true;
}
public int getUsedLength()
@@ -139,15 +146,18 @@ class IntArray
public int getUsedLengthFrom(int from)
{
+ int[] a = m_ints;
+ from = Math.min(from, a.length);
+
if (from < 1)
{
return 0;
}
// Check if first element will act as sentinel
- if (m_ints[0] != 0)
+ if (a[0] != 0)
{
- while (m_ints[--from] == 0)
+ while (a[--from] == 0)
{
}
return from + 1;
@@ -155,7 +165,7 @@ class IntArray
do
{
- if (m_ints[--from] != 0)
+ if (a[--from] != 0)
{
return from + 1;
}
@@ -178,19 +188,20 @@ class IntArray
}
while (w == 0);
- int t = w >>> 16, k;
+ return (i << 5) + bitLength(w);
+ }
+
+ private static int bitLength(int w)
+ {
+ int t = w >>> 16;
if (t == 0)
{
t = w >>> 8;
- k = (t == 0) ? bitLengths[w] : 8 + bitLengths[t];
- }
- else
- {
- int u = t >>> 8;
- k = (u == 0) ? 16 + bitLengths[t] : 24 + bitLengths[u];
+ return (t == 0) ? bitLengths[w] : 8 + bitLengths[t];
}
- return (i << 5) + k;
+ int u = t >>> 8;
+ return (u == 0) ? 16 + bitLengths[t] : 24 + bitLengths[u];
}
private int[] resizedInts(int newLen)
@@ -391,6 +402,11 @@ class IntArray
return result;
}
+ public boolean testBitZero()
+ {
+ return m_ints.length > 0 && (m_ints[0] & 1) != 0;
+ }
+
public boolean testBit(int n)
{
// theInt = n / 32
@@ -618,7 +634,13 @@ class IntArray
public IntArray square(int m)
{
- int len = getUsedLength(), _2len = len << 1;
+ int len = getUsedLength();
+ if (len == 0)
+ {
+ return this;
+ }
+
+ int _2len = len << 1;
int[] r = new int[_2len];
int pos = 0;
diff --git a/core/src/main/java/org/bouncycastle/math/ec/LongArray.java b/core/src/main/java/org/bouncycastle/math/ec/LongArray.java
index be1edde2..5cc48947 100644
--- a/core/src/main/java/org/bouncycastle/math/ec/LongArray.java
+++ b/core/src/main/java/org/bouncycastle/math/ec/LongArray.java
@@ -79,7 +79,7 @@ class LongArray
throw new IllegalArgumentException("invalid F2m field value");
}
- if (bigInt.equals(ECConstants.ZERO))
+ if (bigInt.signum() == 0)
{
m_ints = new long[] { 0L };
return;
@@ -128,8 +128,15 @@ class LongArray
public boolean isZero()
{
- return m_ints.length == 0
- || (m_ints[0] == 0L && getUsedLength() == 0);
+ long[] a = m_ints;
+ for (int i = 0; i < a.length; ++i)
+ {
+ if (a[i] != 0L)
+ {
+ return false;
+ }
+ }
+ return true;
}
public int getUsedLength()
@@ -139,15 +146,18 @@ class LongArray
public int getUsedLengthFrom(int from)
{
+ long[] a = m_ints;
+ from = Math.min(from, a.length);
+
if (from < 1)
{
return 0;
}
// Check if first element will act as sentinel
- if (m_ints[0] != 0)
+ if (a[0] != 0)
{
- while (m_ints[--from] == 0)
+ while (a[--from] == 0)
{
}
return from + 1;
@@ -155,7 +165,7 @@ class LongArray
do
{
- if (m_ints[--from] != 0)
+ if (a[--from] != 0)
{
return from + 1;
}
@@ -179,6 +189,11 @@ class LongArray
}
while (w == 0);
+ return (i << 6) + bitLength(w);
+ }
+
+ private static int bitLength(long w)
+ {
int u = (int)(w >>> 32), b;
if (u == 0)
{
@@ -202,7 +217,7 @@ class LongArray
k = (v == 0) ? 16 + bitLengths[t] : 24 + bitLengths[v];
}
- return (i << 6) + b + k;
+ return b + k;
}
private long[] resizedInts(int newLen)
@@ -406,6 +421,11 @@ class LongArray
return result;
}
+ public boolean testBitZero()
+ {
+ return m_ints.length > 0 && (m_ints[0] & 1L) != 0;
+ }
+
public boolean testBit(int n)
{
// theInt = n / 64
@@ -645,7 +665,13 @@ class LongArray
public LongArray square(int m)
{
- int len = getUsedLength(), _2len = len << 1;
+ int len = getUsedLength();
+ if (len == 0)
+ {
+ return this;
+ }
+
+ int _2len = len << 1;
long[] r = new long[_2len];
int pos = 0;