Last active
April 23, 2023 14:11
-
-
Save aal89/46992d72edfefb664c93aa9277d478d5 to your computer and use it in GitHub Desktop.
UInt128 and UInt256 data types in c# to compare string hashes with one and another.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Globalization; | |
using System.Text.RegularExpressions; | |
namespace FakeTypes | |
{ | |
// All types within this namespace are considered fakes. A UInt128 is a struct | |
// containing two ulongs, their primary reason for existence is for quick calculation | |
// and comparisons of hash strings as numbers. Allocation should be on the stack. | |
public struct UInt128 | |
{ | |
public readonly ulong lp; | |
public readonly ulong tp; | |
public UInt128(string HexString) | |
{ | |
if (HexString == null || HexString.Length != 32 || !new Regex("^[0-9a-fA-F]+$", RegexOptions.IgnoreCase).Match(HexString).Success) | |
throw new Exception("Invalid hex string given. Expects length to be 32."); | |
lp = UInt64.Parse(HexString.Substring(0, HexString.Length / 2), NumberStyles.HexNumber); | |
tp = UInt64.Parse(HexString.Substring(HexString.Length / 2), NumberStyles.HexNumber); | |
} | |
public static bool operator ==(UInt128 left, UInt128 right) | |
{ | |
return left.lp == right.lp && left.tp == right.tp; | |
} | |
public static bool operator !=(UInt128 left, UInt128 right) | |
{ | |
return left.lp != right.lp && left.tp != right.tp; | |
} | |
public static bool operator >(UInt128 left, UInt128 right) | |
{ | |
return left.lp > right.lp | |
|| (left.lp == right.lp && left.tp > right.tp); | |
} | |
public static bool operator <(UInt128 left, UInt128 right) | |
{ | |
return left.lp < right.lp | |
|| (left.lp == right.lp && left.tp < right.tp); | |
} | |
public static bool operator >=(UInt128 left, UInt128 right) | |
{ | |
return (left > right) || left == right; | |
} | |
public static bool operator <=(UInt128 left, UInt128 right) | |
{ | |
return (left < right) || left == right; | |
} | |
public override bool Equals(object obj) | |
{ | |
return obj != null && obj is UInt128 && (UInt128)obj == this; | |
} | |
public override int GetHashCode() | |
{ | |
return (int)(lp + tp); | |
} | |
} | |
public struct UInt256 | |
{ | |
public readonly UInt128 lp; | |
public readonly UInt128 tp; | |
public UInt256(string HexString) | |
{ | |
try | |
{ | |
lp = new UInt128(HexString.Substring(0, HexString.Length / 2)); | |
tp = new UInt128(HexString.Substring(HexString.Length / 2)); | |
} | |
catch | |
{ | |
throw new Exception("Invalid hex string given. Expects length to be 64."); | |
} | |
} | |
public static bool operator ==(UInt256 left, UInt256 right) | |
{ | |
return left.lp == right.lp && left.tp == right.tp; | |
} | |
public static bool operator !=(UInt256 left, UInt256 right) | |
{ | |
return left.lp != right.lp && left.tp != right.tp; | |
} | |
public static bool operator >(UInt256 left, UInt256 right) | |
{ | |
return left.lp > right.lp | |
|| (left.lp == right.lp && left.tp > right.tp); | |
} | |
public static bool operator <(UInt256 left, UInt256 right) | |
{ | |
return left.lp < right.lp | |
|| (left.lp == right.lp && left.tp < right.tp); | |
} | |
public static bool operator >=(UInt256 left, UInt256 right) | |
{ | |
return (left > right) || left == right; | |
} | |
public static bool operator <=(UInt256 left, UInt256 right) | |
{ | |
return (left < right) || left == right; | |
} | |
public override bool Equals(object obj) | |
{ | |
return obj != null && obj is UInt256 && (UInt256)obj == this; | |
} | |
public override int GetHashCode() | |
{ | |
return lp.GetHashCode() + tp.GetHashCode(); | |
} | |
} | |
public static class Hash | |
{ | |
private static readonly Dictionary<char, int> Hex2Dec = new Dictionary<char, int> { | |
{ '0', 0 }, | |
{ '1', 1 }, | |
{ '2', 2 }, | |
{ '3', 3 }, | |
{ '4', 4 }, | |
{ '5', 5 }, | |
{ '6', 6 }, | |
{ '7', 7 }, | |
{ '8', 8 }, | |
{ '9', 9 }, | |
{ 'a', 10 }, | |
{ 'b', 11 }, | |
{ 'c', 12 }, | |
{ 'd', 13 }, | |
{ 'e', 14 }, | |
{ 'f', 15 } | |
}; | |
public static bool LeftSmallerThan(string s1, string s2) | |
{ | |
for (int i = 0; i < 64; i++) | |
if (Hex2Dec[s1[i]] < Hex2Dec[s2[i]]) | |
return true; | |
return false; | |
} | |
} | |
} |
Author
aal89
commented
Sep 20, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment