Skip to content

Instantly share code, notes, and snippets.

@aal89

aal89/faketypes.cs

Last active Sep 20, 2019
Embed
What would you like to do?
UInt128 and UInt256 data types in c# to compare string hashes with one and another.
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;
}
}
}
@aal89

This comment has been minimized.

Copy link
Owner Author

@aal89 aal89 commented Sep 20, 2019

Console.WriteLine("Equals");
            Console.WriteLine("");

            Console.WriteLine(new UInt128("1b78179072ffe9982a485541b54e281a") < new UInt128("1b78179072ffe9982a485541b54e281a"));
            Console.WriteLine(new UInt128("1b78179072ffe9982a485541b54e281a") > new UInt128("1b78179072ffe9982a485541b54e281a"));
            Console.WriteLine(new UInt128("1b78179072ffe9982a485541b54e281a") <= new UInt128("1b78179072ffe9982a485541b54e281a"));
            Console.WriteLine(new UInt128("1b78179072ffe9982a485541b54e281a") >= new UInt128("1b78179072ffe9982a485541b54e281a"));
            Console.WriteLine(new UInt128("1b78179072ffe9982a485541b54e281a") == new UInt128("1b78179072ffe9982a485541b54e281a"));

            Console.WriteLine("");
            Console.WriteLine("Off by one in the trailing part");
            Console.WriteLine("");

            Console.WriteLine(new UInt128("1b78179072ffe9982a485541b54e281a") < new UInt128("1b78179072ffe9982a485541b54e2819"));
            Console.WriteLine(new UInt128("1b78179072ffe9982a485541b54e281a") > new UInt128("1b78179072ffe9982a485541b54e2819"));
            Console.WriteLine(new UInt128("1b78179072ffe9982a485541b54e281a") <= new UInt128("1b78179072ffe9982a485541b54e2819"));
            Console.WriteLine(new UInt128("1b78179072ffe9982a485541b54e281a") >= new UInt128("1b78179072ffe9982a485541b54e2819"));
            Console.WriteLine(new UInt128("1b78179072ffe9982a485541b54e281a") == new UInt128("1b78179072ffe9982a485541b54e2819"));

            Console.WriteLine("");
            Console.WriteLine("Reversed");
            Console.WriteLine("");

            Console.WriteLine(new UInt128("1b78179072ffe9982a485541b54e2819") < new UInt128("1b78179072ffe9982a485541b54e281a"));
            Console.WriteLine(new UInt128("1b78179072ffe9982a485541b54e2819") > new UInt128("1b78179072ffe9982a485541b54e281a"));
            Console.WriteLine(new UInt128("1b78179072ffe9982a485541b54e2819") <= new UInt128("1b78179072ffe9982a485541b54e281a"));
            Console.WriteLine(new UInt128("1b78179072ffe9982a485541b54e2819") >= new UInt128("1b78179072ffe9982a485541b54e281a"));
            Console.WriteLine(new UInt128("1b78179072ffe9982a485541b54e2819") == new UInt128("1b78179072ffe9982a485541b54e281a"));

            Console.WriteLine("");
            Console.WriteLine("Off by one in the leading part");
            Console.WriteLine("");

            Console.WriteLine(new UInt128("1b78179072ffe9982a485541b54e281a") < new UInt128("1b78179072ffe9972a485541b54e2819"));
            Console.WriteLine(new UInt128("1b78179072ffe9982a485541b54e281a") > new UInt128("1b78179072ffe9972a485541b54e2819"));
            Console.WriteLine(new UInt128("1b78179072ffe9982a485541b54e281a") <= new UInt128("1b78179072ffe9972a485541b54e2819"));
            Console.WriteLine(new UInt128("1b78179072ffe9982a485541b54e281a") >= new UInt128("1b78179072ffe9972a485541b54e2819"));
            Console.WriteLine(new UInt128("1b78179072ffe9982a485541b54e281a") == new UInt128("1b78179072ffe9972a485541b54e2819"));

            Console.WriteLine("");
            Console.WriteLine("Reversed");
            Console.WriteLine("");

            Console.WriteLine(new UInt128("1b78179072ffe9972a485541b54e281a") < new UInt128("1b78179072ffe9982a485541b54e2819"));
            Console.WriteLine(new UInt128("1b78179072ffe9972a485541b54e281a") > new UInt128("1b78179072ffe9982a485541b54e2819"));
            Console.WriteLine(new UInt128("1b78179072ffe9972a485541b54e281a") <= new UInt128("1b78179072ffe9982a485541b54e2819"));
            Console.WriteLine(new UInt128("1b78179072ffe9972a485541b54e281a") >= new UInt128("1b78179072ffe9982a485541b54e2819"));
            Console.WriteLine(new UInt128("1b78179072ffe9972a485541b54e281a") == new UInt128("1b78179072ffe9982a485541b54e2819"));


            Console.WriteLine("");
            Console.WriteLine("Off by a lot in the trailing part");
            Console.WriteLine("");

            Console.WriteLine(new UInt128("1b78179072ffe9982a485541b54e281a") < new UInt128("1b78179072ffe9982a375541b54e181a"));
            Console.WriteLine(new UInt128("1b78179072ffe9982a485541b54e281a") > new UInt128("1b78179072ffe9982a375541b54e181a"));
            Console.WriteLine(new UInt128("1b78179072ffe9982a485541b54e281a") <= new UInt128("1b78179072ffe9982a375541b54e181a"));
            Console.WriteLine(new UInt128("1b78179072ffe9982a485541b54e281a") >= new UInt128("1b78179072ffe9982a375541b54e181a"));
            Console.WriteLine(new UInt128("1b78179072ffe9982a485541b54e281a") == new UInt128("1b78179072ffe9982a375541b54e181a"));

            Console.WriteLine("");
            Console.WriteLine("Reversed");
            Console.WriteLine("");

            Console.WriteLine(new UInt128("1b78179072ffe9982a375541b54e181a") < new UInt128("1b78179072ffe9982a485541b54e281a"));
            Console.WriteLine(new UInt128("1b78179072ffe9982a375541b54e181a") > new UInt128("1b78179072ffe9982a485541b54e281a"));
            Console.WriteLine(new UInt128("1b78179072ffe9982a375541b54e181a") <= new UInt128("1b78179072ffe9982a485541b54e281a"));
            Console.WriteLine(new UInt128("1b78179072ffe9982a375541b54e181a") >= new UInt128("1b78179072ffe9982a485541b54e281a"));
            Console.WriteLine(new UInt128("1b78179072ffe9982a375541b54e181a") == new UInt128("1b78179072ffe9982a485541b54e281a"));

            Console.WriteLine("");
            Console.WriteLine("Off by a lot in the leading part");
            Console.WriteLine("");

            Console.WriteLine(new UInt128("1b78179072ffe9982a485541b54e281a") < new UInt128("1278179022ffe8982a485541b54e281a"));
            Console.WriteLine(new UInt128("1b78179072ffe9982a485541b54e281a") > new UInt128("1278179022ffe8982a485541b54e281a"));
            Console.WriteLine(new UInt128("1b78179072ffe9982a485541b54e281a") <= new UInt128("1278179022ffe8982a485541b54e281a"));
            Console.WriteLine(new UInt128("1b78179072ffe9982a485541b54e281a") >= new UInt128("1278179022ffe8982a485541b54e281a"));
            Console.WriteLine(new UInt128("1b78179072ffe9982a485541b54e281a") == new UInt128("1278179022ffe8982a485541b54e281a"));

            Console.WriteLine("");
            Console.WriteLine("Reversed");
            Console.WriteLine("");

            Console.WriteLine(new UInt128("1278179022ffe8982a485541b54e281a") < new UInt128("1b78179072ffe9982a485541b54e281a"));
            Console.WriteLine(new UInt128("1278179022ffe8982a485541b54e281a") > new UInt128("1b78179072ffe9982a485541b54e281a"));
            Console.WriteLine(new UInt128("1278179022ffe8982a485541b54e281a") <= new UInt128("1b78179072ffe9982a485541b54e281a"));
            Console.WriteLine(new UInt128("1278179022ffe8982a485541b54e281a") >= new UInt128("1b78179072ffe9982a485541b54e281a"));
            Console.WriteLine(new UInt128("1278179022ffe8982a485541b54e281a") == new UInt128("1b78179072ffe9982a485541b54e281a"));

            Console.WriteLine("");
            Console.WriteLine("Totally different hashes");
            Console.WriteLine("");

            Console.WriteLine(new UInt128("1b78179072ffe9982a485541b54e281a") < new UInt128("e6262df3369552fe24a0f2a610ebb636"));
            Console.WriteLine(new UInt128("1b78179072ffe9982a485541b54e281a") > new UInt128("e6262df3369552fe24a0f2a610ebb636"));
            Console.WriteLine(new UInt128("1b78179072ffe9982a485541b54e281a") <= new UInt128("e6262df3369552fe24a0f2a610ebb636"));
            Console.WriteLine(new UInt128("1b78179072ffe9982a485541b54e281a") >= new UInt128("e6262df3369552fe24a0f2a610ebb636"));
            Console.WriteLine(new UInt128("1b78179072ffe9982a485541b54e281a") == new UInt128("e6262df3369552fe24a0f2a610ebb636"));

            Console.WriteLine("");
            Console.WriteLine("Reversed");
            Console.WriteLine("");

            Console.WriteLine(new UInt128("e6262df3369552fe24a0f2a610ebb636") < new UInt128("1b78179072ffe9982a485541b54e281a"));
            Console.WriteLine(new UInt128("e6262df3369552fe24a0f2a610ebb636") > new UInt128("1b78179072ffe9982a485541b54e281a"));
            Console.WriteLine(new UInt128("e6262df3369552fe24a0f2a610ebb636") <= new UInt128("1b78179072ffe9982a485541b54e281a"));
            Console.WriteLine(new UInt128("e6262df3369552fe24a0f2a610ebb636") >= new UInt128("1b78179072ffe9982a485541b54e281a"));
            Console.WriteLine(new UInt128("e6262df3369552fe24a0f2a610ebb636") == new UInt128("1b78179072ffe9982a485541b54e281a"));


            Console.WriteLine(new UInt256("e6262df1b78179072ffe9982a485541b54e281a3369552fe24a0f2a610ebb636") > new UInt256("1b78179072ffe998e6262df3369552fe24a0f2a610ebb6362a485541b54e281a"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.