Skip to content

Commit f029b8a

Browse files
committed
Use System.HashCode in a few places
1 parent 02e5f51 commit f029b8a

File tree

4 files changed

+107
-51
lines changed

4 files changed

+107
-51
lines changed

crypto/src/math/BigInteger.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
#if NETCOREAPP3_0_OR_GREATER
66
using System.Numerics;
77
#endif
8+
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
9+
using System.Runtime.InteropServices;
10+
#endif
811
using System.Runtime.Serialization;
912
using System.Text;
1013

@@ -1464,6 +1467,12 @@ public BigInteger Gcd(BigInteger value)
14641467

14651468
public override int GetHashCode()
14661469
{
1470+
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
1471+
HashCode hc = default;
1472+
hc.AddBytes(MemoryMarshal.AsBytes(magnitude.AsSpan()));
1473+
hc.Add(sign);
1474+
return hc.ToHashCode();
1475+
#else
14671476
int hc = magnitude.Length;
14681477
if (magnitude.Length > 0)
14691478
{
@@ -1476,6 +1485,7 @@ public override int GetHashCode()
14761485
}
14771486

14781487
return sign < 0 ? ~hc : hc;
1488+
#endif
14791489
}
14801490

14811491
// TODO Make public?

crypto/src/math/ec/ECFieldElement.cs

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -516,29 +516,24 @@ protected virtual BigInteger ModSubtract(BigInteger x1, BigInteger x2)
516516
return x3;
517517
}
518518

519-
public override bool Equals(
520-
object obj)
519+
public override bool Equals(object obj) => obj is FpFieldElement that && Equals(that);
520+
521+
public virtual bool Equals(FpFieldElement other)
521522
{
522-
if (obj == this)
523+
if (this == other)
523524
return true;
524525

525-
FpFieldElement other = obj as FpFieldElement;
526-
527-
if (other == null)
528-
return false;
529-
530-
return Equals(other);
531-
}
532-
533-
public virtual bool Equals(
534-
FpFieldElement other)
535-
{
536-
return q.Equals(other.q) && base.Equals(other);
526+
return q.Equals(other.q)
527+
&& x.Equals(other.x);
537528
}
538529

539530
public override int GetHashCode()
540531
{
541-
return q.GetHashCode() ^ base.GetHashCode();
532+
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
533+
return HashCode.Combine(q, x);
534+
#else
535+
return q.GetHashCode() ^ x.GetHashCode();
536+
#endif
542537
}
543538
}
544539

@@ -910,32 +905,26 @@ public int K3
910905
get { return this.ks.Length >= 3 ? this.ks[2] : 0; }
911906
}
912907

913-
public override bool Equals(
914-
object obj)
908+
public override bool Equals(object obj) => obj is F2mFieldElement that && Equals(that);
909+
910+
public virtual bool Equals(F2mFieldElement other)
915911
{
916-
if (obj == this)
912+
if (this == other)
917913
return true;
918914

919-
F2mFieldElement other = obj as F2mFieldElement;
920-
921-
if (other == null)
922-
return false;
923-
924-
return Equals(other);
925-
}
926-
927-
public virtual bool Equals(
928-
F2mFieldElement other)
929-
{
930-
return ((this.m == other.m)
931-
&& (this.representation == other.representation)
915+
return this.m == other.m
916+
&& this.representation == other.representation
932917
&& Arrays.AreEqual(this.ks, other.ks)
933-
&& (this.x.Equals(other.x)));
918+
&& this.x.Equals(other.x);
934919
}
935920

936921
public override int GetHashCode()
937922
{
923+
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
924+
return HashCode.Combine(x, m, Arrays.GetHashCode(ks));
925+
#else
938926
return x.GetHashCode() ^ m ^ Arrays.GetHashCode(ks);
927+
#endif
939928
}
940929
}
941930
}

crypto/src/math/ec/LongArray.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,20 +1245,7 @@ internal bool Equals(ref LongArray other)
12451245
return true;
12461246
}
12471247

1248-
public override int GetHashCode()
1249-
{
1250-
int usedLen = GetUsedLength();
1251-
int hash = 1;
1252-
for (int i = 0; i < usedLen; i++)
1253-
{
1254-
ulong mi = m_data[i];
1255-
hash *= 31;
1256-
hash ^= (int)mi;
1257-
hash *= 31;
1258-
hash ^= (int)(mi >> 32);
1259-
}
1260-
return hash;
1261-
}
1248+
public override int GetHashCode() => Arrays.GetHashCode(m_data, 0, GetUsedLength());
12621249

12631250
public LongArray Copy()
12641251
{

crypto/src/util/Arrays.cs

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Runtime.CompilerServices;
33
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
4+
using System.Runtime.InteropServices;
45
using System.Security.Cryptography;
56
#endif
67
using System.Text;
@@ -403,10 +404,13 @@ public static string ToString(
403404
public static int GetHashCode(byte[] data)
404405
{
405406
if (data == null)
406-
{
407407
return 0;
408-
}
409408

409+
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
410+
HashCode hc = default;
411+
hc.AddBytes(data);
412+
return hc.ToHashCode();
413+
#else
410414
int i = data.Length;
411415
int hc = i + 1;
412416

@@ -417,15 +421,19 @@ public static int GetHashCode(byte[] data)
417421
}
418422

419423
return hc;
424+
#endif
420425
}
421426

422427
public static int GetHashCode(byte[] data, int off, int len)
423428
{
424429
if (data == null)
425-
{
426430
return 0;
427-
}
428431

432+
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
433+
HashCode hc = default;
434+
hc.AddBytes(data.AsSpan(off, len));
435+
return hc.ToHashCode();
436+
#else
429437
int i = len;
430438
int hc = i + 1;
431439

@@ -436,13 +444,19 @@ public static int GetHashCode(byte[] data, int off, int len)
436444
}
437445

438446
return hc;
447+
#endif
439448
}
440449

441450
public static int GetHashCode(int[] data)
442451
{
443452
if (data == null)
444453
return 0;
445454

455+
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
456+
HashCode hc = default;
457+
hc.AddBytes(MemoryMarshal.AsBytes(data.AsSpan()));
458+
return hc.ToHashCode();
459+
#else
446460
int i = data.Length;
447461
int hc = i + 1;
448462

@@ -453,6 +467,7 @@ public static int GetHashCode(int[] data)
453467
}
454468

455469
return hc;
470+
#endif
456471
}
457472

458473
[CLSCompliant(false)]
@@ -461,6 +476,11 @@ public static int GetHashCode(ushort[] data)
461476
if (data == null)
462477
return 0;
463478

479+
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
480+
HashCode hc = default;
481+
hc.AddBytes(MemoryMarshal.AsBytes(data.AsSpan()));
482+
return hc.ToHashCode();
483+
#else
464484
int i = data.Length;
465485
int hc = i + 1;
466486

@@ -471,13 +491,19 @@ public static int GetHashCode(ushort[] data)
471491
}
472492

473493
return hc;
494+
#endif
474495
}
475496

476497
public static int GetHashCode(int[] data, int off, int len)
477498
{
478499
if (data == null)
479500
return 0;
480501

502+
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
503+
HashCode hc = default;
504+
hc.AddBytes(MemoryMarshal.AsBytes(data.AsSpan(off, len)));
505+
return hc.ToHashCode();
506+
#else
481507
int i = len;
482508
int hc = i + 1;
483509

@@ -488,6 +514,7 @@ public static int GetHashCode(int[] data, int off, int len)
488514
}
489515

490516
return hc;
517+
#endif
491518
}
492519

493520
[CLSCompliant(false)]
@@ -496,6 +523,11 @@ public static int GetHashCode(uint[] data)
496523
if (data == null)
497524
return 0;
498525

526+
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
527+
HashCode hc = default;
528+
hc.AddBytes(MemoryMarshal.AsBytes(data.AsSpan()));
529+
return hc.ToHashCode();
530+
#else
499531
int i = data.Length;
500532
int hc = i + 1;
501533

@@ -506,6 +538,7 @@ public static int GetHashCode(uint[] data)
506538
}
507539

508540
return hc;
541+
#endif
509542
}
510543

511544
[CLSCompliant(false)]
@@ -514,6 +547,11 @@ public static int GetHashCode(uint[] data, int off, int len)
514547
if (data == null)
515548
return 0;
516549

550+
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
551+
HashCode hc = default;
552+
hc.AddBytes(MemoryMarshal.AsBytes(data.AsSpan(off, len)));
553+
return hc.ToHashCode();
554+
#else
517555
int i = len;
518556
int hc = i + 1;
519557

@@ -524,6 +562,7 @@ public static int GetHashCode(uint[] data, int off, int len)
524562
}
525563

526564
return hc;
565+
#endif
527566
}
528567

529568
[CLSCompliant(false)]
@@ -532,6 +571,11 @@ public static int GetHashCode(ulong[] data)
532571
if (data == null)
533572
return 0;
534573

574+
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
575+
HashCode hc = default;
576+
hc.AddBytes(MemoryMarshal.AsBytes(data.AsSpan()));
577+
return hc.ToHashCode();
578+
#else
535579
int i = data.Length;
536580
int hc = i + 1;
537581

@@ -545,6 +589,7 @@ public static int GetHashCode(ulong[] data)
545589
}
546590

547591
return hc;
592+
#endif
548593
}
549594

550595
[CLSCompliant(false)]
@@ -553,6 +598,11 @@ public static int GetHashCode(ulong[] data, int off, int len)
553598
if (data == null)
554599
return 0;
555600

601+
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
602+
HashCode hc = default;
603+
hc.AddBytes(MemoryMarshal.AsBytes(data.AsSpan(off, len)));
604+
return hc.ToHashCode();
605+
#else
556606
int i = len;
557607
int hc = i + 1;
558608

@@ -566,6 +616,7 @@ public static int GetHashCode(ulong[] data, int off, int len)
566616
}
567617

568618
return hc;
619+
#endif
569620
}
570621

571622
public static int GetHashCode(object[] data)
@@ -574,27 +625,46 @@ public static int GetHashCode(object[] data)
574625
return 0;
575626

576627
int len = data.Length;
628+
629+
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
630+
HashCode hc = default;
631+
for (int i = 0; i < len; ++i)
632+
{
633+
hc.Add(data[i]);
634+
}
635+
return hc.ToHashCode();
636+
#else
577637
int hc = len + 1;
578638
for (int i = 0; i < len; ++i)
579639
{
580640
hc *= 257;
581641
hc ^= Objects.GetHashCode(data[i]);
582642
}
583643
return hc;
644+
#endif
584645
}
585646

586647
public static int GetHashCode(object[] data, int off, int len)
587648
{
588649
if (data == null)
589650
return 0;
590651

652+
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
653+
HashCode hc = default;
654+
for (int i = 0; i < len; ++i)
655+
{
656+
hc.Add(data[off + i]);
657+
}
658+
return hc.ToHashCode();
659+
#else
591660
int hc = len + 1;
592661
for (int i = 0; i < len; ++i)
593662
{
594663
hc *= 257;
595664
hc ^= Objects.GetHashCode(data[off + i]);
596665
}
597666
return hc;
667+
#endif
598668
}
599669

600670
public static bool[] Clone(bool[] data)

0 commit comments

Comments
 (0)