summary refs log tree commit diff
path: root/crypto/src
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2014-02-27 13:01:12 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2014-02-27 13:01:12 +0700
commit5e1ef1cb46ed4967423d16805b9ac780c4ba9599 (patch)
tree3002a808a4d7c745e0316b0a8b2f3a2946bec0b6 /crypto/src
parentOptimize Sqrt() for custom secp384r1 (diff)
downloadBouncyCastle.NET-ed25519-5e1ef1cb46ed4967423d16805b9ac780c4ba9599.tar.xz
Equality/hashcode should ignore "excess" words
Diffstat (limited to '')
-rw-r--r--crypto/src/math/ec/custom/sec/Nat192.cs10
-rw-r--r--crypto/src/math/ec/custom/sec/Nat224.cs10
-rw-r--r--crypto/src/math/ec/custom/sec/Nat256.cs10
-rw-r--r--crypto/src/math/ec/custom/sec/SecP192K1FieldElement.cs6
-rw-r--r--crypto/src/math/ec/custom/sec/SecP192R1FieldElement.cs6
-rw-r--r--crypto/src/math/ec/custom/sec/SecP224K1FieldElement.cs8
-rw-r--r--crypto/src/math/ec/custom/sec/SecP224R1FieldElement.cs4
-rw-r--r--crypto/src/math/ec/custom/sec/SecP256K1FieldElement.cs6
-rw-r--r--crypto/src/math/ec/custom/sec/SecP256R1FieldElement.cs6
-rw-r--r--crypto/src/math/ec/custom/sec/SecP384R1FieldElement.cs4
-rw-r--r--crypto/src/math/ec/custom/sec/SecP521R1FieldElement.cs6
-rw-r--r--crypto/src/util/Arrays.cs54
12 files changed, 107 insertions, 23 deletions
diff --git a/crypto/src/math/ec/custom/sec/Nat192.cs b/crypto/src/math/ec/custom/sec/Nat192.cs
index 15053bb32..c43330862 100644
--- a/crypto/src/math/ec/custom/sec/Nat192.cs
+++ b/crypto/src/math/ec/custom/sec/Nat192.cs
@@ -215,6 +215,16 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             return pos;
         }
 
+        public static bool Eq(uint[] x, uint[] y)
+        {
+            for (int i = 5; i >= 0; --i)
+            {
+                if (x[i] != y[i])
+                    return false;
+            }
+            return true;
+        }
+
         public static uint[] FromBigInteger(BigInteger x)
         {
             if (x.SignValue < 0 || x.BitLength > 192)
diff --git a/crypto/src/math/ec/custom/sec/Nat224.cs b/crypto/src/math/ec/custom/sec/Nat224.cs
index a391fc248..357ce5c69 100644
--- a/crypto/src/math/ec/custom/sec/Nat224.cs
+++ b/crypto/src/math/ec/custom/sec/Nat224.cs
@@ -283,6 +283,16 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             return pos;
         }
 
+        public static bool Eq(uint[] x, uint[] y)
+        {
+            for (int i = 6; i >= 0; --i)
+            {
+                if (x[i] != y[i])
+                    return false;
+            }
+            return true;
+        }
+
         public static uint[] FromBigInteger(BigInteger x)
         {
             if (x.SignValue < 0 || x.BitLength > 224)
diff --git a/crypto/src/math/ec/custom/sec/Nat256.cs b/crypto/src/math/ec/custom/sec/Nat256.cs
index aa6f4e5eb..98b4b83cd 100644
--- a/crypto/src/math/ec/custom/sec/Nat256.cs
+++ b/crypto/src/math/ec/custom/sec/Nat256.cs
@@ -303,6 +303,16 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             return pos;
         }
 
+        public static bool Eq(uint[] x, uint[] y)
+        {
+            for (int i = 7; i >= 0; --i)
+            {
+                if (x[i] != y[i])
+                    return false;
+            }
+            return true;
+        }
+
         public static uint[] FromBigInteger(BigInteger x)
         {
             if (x.SignValue < 0 || x.BitLength > 256)
diff --git a/crypto/src/math/ec/custom/sec/SecP192K1FieldElement.cs b/crypto/src/math/ec/custom/sec/SecP192K1FieldElement.cs
index ba2897ae4..78886dd8c 100644
--- a/crypto/src/math/ec/custom/sec/SecP192K1FieldElement.cs
+++ b/crypto/src/math/ec/custom/sec/SecP192K1FieldElement.cs
@@ -182,7 +182,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             uint[] t2 = x3;
             SecP192K1Field.Square(t1, t2);
 
-            return Arrays.AreEqual(x1, t2) ? new SecP192K1FieldElement(t1) : null;
+            return Nat192.Eq(x1, t2) ? new SecP192K1FieldElement(t1) : null;
         }
 
         public override bool Equals(object obj)
@@ -201,12 +201,12 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
                 return true;
             if (null == other)
                 return false;
-            return Arrays.AreEqual(x, other.x);
+            return Nat192.Eq(x, other.x);
         }
 
         public override int GetHashCode()
         {
-            return Q.GetHashCode() ^ Arrays.GetHashCode(x);
+            return Q.GetHashCode() ^ Arrays.GetHashCode(x, 0, 6);
         }
     }
 }
diff --git a/crypto/src/math/ec/custom/sec/SecP192R1FieldElement.cs b/crypto/src/math/ec/custom/sec/SecP192R1FieldElement.cs
index 5bcff10d2..020c5cdbb 100644
--- a/crypto/src/math/ec/custom/sec/SecP192R1FieldElement.cs
+++ b/crypto/src/math/ec/custom/sec/SecP192R1FieldElement.cs
@@ -157,7 +157,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             SecP192R1Field.SquareN(t1, 62, t1);
             SecP192R1Field.Square(t1, t2);
 
-            return Arrays.AreEqual(x1, t2) ? new SecP192R1FieldElement(t1) : null;
+            return Nat192.Eq(x1, t2) ? new SecP192R1FieldElement(t1) : null;
         }
 
         public override bool Equals(object obj)
@@ -176,12 +176,12 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
                 return true;
             if (null == other)
                 return false;
-            return Arrays.AreEqual(x, other.x);
+            return Nat192.Eq(x, other.x);
         }
 
         public override int GetHashCode()
         {
-            return Q.GetHashCode() ^ Arrays.GetHashCode(x);
+            return Q.GetHashCode() ^ Arrays.GetHashCode(x, 0, 6);
         }
     }
 }
diff --git a/crypto/src/math/ec/custom/sec/SecP224K1FieldElement.cs b/crypto/src/math/ec/custom/sec/SecP224K1FieldElement.cs
index 123efd2ab..72ff4b099 100644
--- a/crypto/src/math/ec/custom/sec/SecP224K1FieldElement.cs
+++ b/crypto/src/math/ec/custom/sec/SecP224K1FieldElement.cs
@@ -193,7 +193,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             uint[] t2 = x84;
             SecP224K1Field.Square(t1, t2);
 
-            if (Arrays.AreEqual(x1, t2))
+            if (Nat224.Eq(x1, t2))
             {
                 return new SecP224K1FieldElement(t1);
             }
@@ -206,7 +206,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
             SecP224K1Field.Square(t1, t2);
 
-            if (Arrays.AreEqual(x1, t2))
+            if (Nat224.Eq(x1, t2))
             {
                 return new SecP224K1FieldElement(t1);
             }
@@ -230,12 +230,12 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
                 return true;
             if (null == other)
                 return false;
-            return Arrays.AreEqual(x, other.x);
+            return Nat224.Eq(x, other.x);
         }
 
         public override int GetHashCode()
         {
-            return Q.GetHashCode() ^ Arrays.GetHashCode(x);
+            return Q.GetHashCode() ^ Arrays.GetHashCode(x, 0, 7);
         }
     }
 }
diff --git a/crypto/src/math/ec/custom/sec/SecP224R1FieldElement.cs b/crypto/src/math/ec/custom/sec/SecP224R1FieldElement.cs
index 3ca6900b9..41b2de7dc 100644
--- a/crypto/src/math/ec/custom/sec/SecP224R1FieldElement.cs
+++ b/crypto/src/math/ec/custom/sec/SecP224R1FieldElement.cs
@@ -144,12 +144,12 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
                 return true;
             if (null == other)
                 return false;
-            return Arrays.AreEqual(x, other.x);
+            return Nat224.Eq(x, other.x);
         }
 
         public override int GetHashCode()
         {
-            return Q.GetHashCode() ^ Arrays.GetHashCode(x);
+            return Q.GetHashCode() ^ Arrays.GetHashCode(x, 0, 7);
         }
     }
 }
diff --git a/crypto/src/math/ec/custom/sec/SecP256K1FieldElement.cs b/crypto/src/math/ec/custom/sec/SecP256K1FieldElement.cs
index 28c883e06..d9a039a4f 100644
--- a/crypto/src/math/ec/custom/sec/SecP256K1FieldElement.cs
+++ b/crypto/src/math/ec/custom/sec/SecP256K1FieldElement.cs
@@ -183,7 +183,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             uint[] t2 = x2;
             SecP256K1Field.Square(t1, t2);
 
-            return Arrays.AreEqual(x1, t2) ? new SecP256K1FieldElement(t1) : null;
+            return Nat256.Eq(x1, t2) ? new SecP256K1FieldElement(t1) : null;
         }
 
         public override bool Equals(object obj)
@@ -202,12 +202,12 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
                 return true;
             if (null == other)
                 return false;
-            return Arrays.AreEqual(x, other.x);
+            return Nat256.Eq(x, other.x);
         }
 
         public override int GetHashCode()
         {
-            return Q.GetHashCode() ^ Arrays.GetHashCode(x);
+            return Q.GetHashCode() ^ Arrays.GetHashCode(x, 0, 8);
         }
     }
 }
diff --git a/crypto/src/math/ec/custom/sec/SecP256R1FieldElement.cs b/crypto/src/math/ec/custom/sec/SecP256R1FieldElement.cs
index ba48fcbac..b22763cfa 100644
--- a/crypto/src/math/ec/custom/sec/SecP256R1FieldElement.cs
+++ b/crypto/src/math/ec/custom/sec/SecP256R1FieldElement.cs
@@ -157,7 +157,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             SecP256R1Field.SquareN(t1, 94, t1);
             SecP256R1Field.Multiply(t1, t1, t2);
 
-            return Arrays.AreEqual(x1, t2) ? new SecP256R1FieldElement(t1) : null;
+            return Nat256.Eq(x1, t2) ? new SecP256R1FieldElement(t1) : null;
         }
 
         public override bool Equals(object obj)
@@ -176,12 +176,12 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
                 return true;
             if (null == other)
                 return false;
-            return Arrays.AreEqual(x, other.x);
+            return Nat256.Eq(x, other.x);
         }
 
         public override int GetHashCode()
         {
-            return Q.GetHashCode() ^ Arrays.GetHashCode(x);
+            return Q.GetHashCode() ^ Arrays.GetHashCode(x, 0, 8);
         }
     }
 }
diff --git a/crypto/src/math/ec/custom/sec/SecP384R1FieldElement.cs b/crypto/src/math/ec/custom/sec/SecP384R1FieldElement.cs
index 6e4fd2030..40086978d 100644
--- a/crypto/src/math/ec/custom/sec/SecP384R1FieldElement.cs
+++ b/crypto/src/math/ec/custom/sec/SecP384R1FieldElement.cs
@@ -198,12 +198,12 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
                 return true;
             if (null == other)
                 return false;
-            return Arrays.AreEqual(x, other.x);
+            return Nat.Eq(12, x, other.x);
         }
 
         public override int GetHashCode()
         {
-            return Q.GetHashCode() ^ Arrays.GetHashCode(x);
+            return Q.GetHashCode() ^ Arrays.GetHashCode(x, 0, 12);
         }
     }
 }
diff --git a/crypto/src/math/ec/custom/sec/SecP521R1FieldElement.cs b/crypto/src/math/ec/custom/sec/SecP521R1FieldElement.cs
index e47a199f3..83a615928 100644
--- a/crypto/src/math/ec/custom/sec/SecP521R1FieldElement.cs
+++ b/crypto/src/math/ec/custom/sec/SecP521R1FieldElement.cs
@@ -136,7 +136,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             SecP521R1Field.SquareN(x1, 519, t1);
             SecP521R1Field.Square(t1, t2);
 
-            return Arrays.AreEqual(x1, t2) ? new SecP521R1FieldElement(t1) : null;
+            return Nat.Eq(17, x1, t2) ? new SecP521R1FieldElement(t1) : null;
         }
 
         public override bool Equals(object obj)
@@ -155,12 +155,12 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
                 return true;
             if (null == other)
                 return false;
-            return Arrays.AreEqual(x, other.x);
+            return Nat.Eq(17, x, other.x);
         }
 
         public override int GetHashCode()
         {
-            return Q.GetHashCode() ^ Arrays.GetHashCode(x);
+            return Q.GetHashCode() ^ Arrays.GetHashCode(x, 0, 17);
         }
     }
 }
diff --git a/crypto/src/util/Arrays.cs b/crypto/src/util/Arrays.cs
index cc2025ef3..a21dd00b1 100644
--- a/crypto/src/util/Arrays.cs
+++ b/crypto/src/util/Arrays.cs
@@ -220,6 +220,25 @@ namespace Org.BouncyCastle.Utilities
             return hc;
         }
 
+        public static int GetHashCode(byte[] data, int off, int len)
+        {
+            if (data == null)
+            {
+                return 0;
+            }
+
+            int i = len;
+            int hc = i + 1;
+
+            while (--i >= 0)
+            {
+                hc *= 257;
+                hc ^= data[off + i];
+            }
+
+            return hc;
+        }
+
         public static int GetHashCode(int[] data)
         {
             if (data == null)
@@ -237,6 +256,23 @@ namespace Org.BouncyCastle.Utilities
             return hc;
         }
 
+        public static int GetHashCode(int[] data, int off, int len)
+        {
+            if (data == null)
+                return 0;
+
+            int i = len;
+            int hc = i + 1;
+
+            while (--i >= 0)
+            {
+                hc *= 257;
+                hc ^= data[off + i];
+            }
+
+            return hc;
+        }
+
         [CLSCompliantAttribute(false)]
         public static int GetHashCode(uint[] data)
         {
@@ -255,6 +291,24 @@ namespace Org.BouncyCastle.Utilities
             return hc;
         }
 
+        [CLSCompliantAttribute(false)]
+        public static int GetHashCode(uint[] data, int off, int len)
+        {
+            if (data == null)
+                return 0;
+
+            int i = len;
+            int hc = i + 1;
+
+            while (--i >= 0)
+            {
+                hc *= 257;
+                hc ^= (int)data[off + i];
+            }
+
+            return hc;
+        }
+
         public static byte[] Clone(
             byte[] data)
         {