From 5e1ef1cb46ed4967423d16805b9ac780c4ba9599 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Thu, 27 Feb 2014 13:01:12 +0700 Subject: Equality/hashcode should ignore "excess" words --- crypto/src/math/ec/custom/sec/Nat192.cs | 10 ++++++++++ crypto/src/math/ec/custom/sec/Nat224.cs | 10 ++++++++++ crypto/src/math/ec/custom/sec/Nat256.cs | 10 ++++++++++ crypto/src/math/ec/custom/sec/SecP192K1FieldElement.cs | 6 +++--- crypto/src/math/ec/custom/sec/SecP192R1FieldElement.cs | 6 +++--- crypto/src/math/ec/custom/sec/SecP224K1FieldElement.cs | 8 ++++---- crypto/src/math/ec/custom/sec/SecP224R1FieldElement.cs | 4 ++-- crypto/src/math/ec/custom/sec/SecP256K1FieldElement.cs | 6 +++--- crypto/src/math/ec/custom/sec/SecP256R1FieldElement.cs | 6 +++--- crypto/src/math/ec/custom/sec/SecP384R1FieldElement.cs | 4 ++-- crypto/src/math/ec/custom/sec/SecP521R1FieldElement.cs | 6 +++--- 11 files changed, 53 insertions(+), 23 deletions(-) (limited to 'crypto/src/math/ec/custom') 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); } } } -- cgit 1.4.1