From 16161c3c69ad217c9ff3440bf37884a857da0b9e Mon Sep 17 00:00:00 2001 From: William Dillon Date: Wed, 4 Jun 2025 22:03:23 -0400 Subject: [PATCH] first commit --- Mishmash.sln | 22 +++++++++++++++++ Mishmash/Mishmash.csproj | 10 ++++++++ Mishmash/Mishmash32.cs | 38 +++++++++++++++++++++++++++++ Mishmash/Mishmash64.cs | 41 +++++++++++++++++++++++++++++++ Mishmash/MishmashEngine.cs | 25 +++++++++++++++++++ Mishmash/PrimesSet.cs | 50 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 186 insertions(+) create mode 100644 Mishmash.sln create mode 100644 Mishmash/Mishmash.csproj create mode 100644 Mishmash/Mishmash32.cs create mode 100644 Mishmash/Mishmash64.cs create mode 100644 Mishmash/MishmashEngine.cs create mode 100644 Mishmash/PrimesSet.cs diff --git a/Mishmash.sln b/Mishmash.sln new file mode 100644 index 0000000..c64bf9e --- /dev/null +++ b/Mishmash.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mishmash", "Mishmash\Mishmash.csproj", "{30C18D71-CBB0-4D4F-A9C2-764047731CA7}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {30C18D71-CBB0-4D4F-A9C2-764047731CA7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {30C18D71-CBB0-4D4F-A9C2-764047731CA7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {30C18D71-CBB0-4D4F-A9C2-764047731CA7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {30C18D71-CBB0-4D4F-A9C2-764047731CA7}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/Mishmash/Mishmash.csproj b/Mishmash/Mishmash.csproj new file mode 100644 index 0000000..3d56e2b --- /dev/null +++ b/Mishmash/Mishmash.csproj @@ -0,0 +1,10 @@ + + + + netstandard2.0 + 12.0 + enable + enable + + + diff --git a/Mishmash/Mishmash32.cs b/Mishmash/Mishmash32.cs new file mode 100644 index 0000000..0aa74ac --- /dev/null +++ b/Mishmash/Mishmash32.cs @@ -0,0 +1,38 @@ +namespace Mishmash; + +public class Mishmash32 +{ + public const UInt64 UINT32_RESULTS_MASK = 0x00000000FFFFFFFF; + private UInt64 accumulator; + public Mishmash32() + { + accumulator = 0; + } + public override string ToString() + { + return accumulator.ToString("X"); + } + public UInt32 Hash32() + { + return (UInt32)(accumulator & UINT32_RESULTS_MASK); + } + public void Write(string s) + { + var (bytes, size) = MishmashEngine.StringToBytesUtf8(s); + accumulator = MishmashEngine.Engine(bytes, size, accumulator); + } + public void Write(byte[] bytes, int size) + { + accumulator = MishmashEngine.Engine(bytes, size, accumulator); + } + public byte[] Bytes() + { + var hash = Hash32(); + List results = new(); + results.Add((byte)(hash >> 24)); + results.Add((byte)((hash >> 16) & 0x000000FF)); + results.Add((byte)((hash >> 8) & 0x000000FF)); + results.Add((byte)(hash & 0x000000FF)); + return results.ToArray(); + } +} \ No newline at end of file diff --git a/Mishmash/Mishmash64.cs b/Mishmash/Mishmash64.cs new file mode 100644 index 0000000..bae41cb --- /dev/null +++ b/Mishmash/Mishmash64.cs @@ -0,0 +1,41 @@ +namespace Mishmash; + +public class Mishmash64 +{ + private UInt64 accumulator; + public Mishmash64() + { + accumulator = 0; + } + public override string ToString() + { + return accumulator.ToString("X"); + } + public UInt64 Hash64() + { + return accumulator; + } + public void Write(string s) + { + var (bytes, size) = MishmashEngine.StringToBytesUtf8(s); + accumulator = MishmashEngine.Engine(bytes, size, accumulator); + } + public void Write(byte[] bytes, int size) + { + accumulator = MishmashEngine.Engine(bytes, size, accumulator); + } + public byte[] Bytes() + { + var hash = Hash64(); + List results = new(); + results.Add((byte)((hash >> 56) & 0x00000000000000FF)); + results.Add((byte)((hash >> 48) & 0x00000000000000FF)); + results.Add((byte)((hash >> 40) & 0x00000000000000FF)); + results.Add((byte)((hash >> 32) & 0x00000000000000FF)); + results.Add((byte)((hash >> 24) & 0x00000000000000FF)); + results.Add((byte)((hash >> 16) & 0x00000000000000FF)); + results.Add((byte)((hash >> 8) & 0x00000000000000FF)); + results.Add((byte)(hash & 0x00000000000000FF)); + return results.ToArray(); + } +} \ No newline at end of file diff --git a/Mishmash/MishmashEngine.cs b/Mishmash/MishmashEngine.cs new file mode 100644 index 0000000..10ea8b4 --- /dev/null +++ b/Mishmash/MishmashEngine.cs @@ -0,0 +1,25 @@ +using System.Text; + +namespace Mishmash; + +public static class MishmashEngine +{ + public static (byte[], int) StringToBytesUtf8(string s) + { + var bytes = Encoding.UTF8.GetBytes(s); + var size = Encoding.UTF8.GetByteCount(s); + return (bytes, size); + } + public static UInt64 Engine(byte[] buffer, int length, UInt64 accumulator = 0, PrimesSet? primes = null) + { + if (primes == null) primes = new(); + foreach (var b in buffer) + { + UInt64 accumulator1 = primes.Select(accumulator) + b; + UInt64 accumulator2 = accumulator * primes.Select(b); + UInt64 accumulator3 = accumulator >> (32 + ((b & 0x1F) ^ (b >> 5))); + accumulator = accumulator1 + accumulator2 + accumulator3; + } + return accumulator; + } +} \ No newline at end of file diff --git a/Mishmash/PrimesSet.cs b/Mishmash/PrimesSet.cs new file mode 100644 index 0000000..d77131e --- /dev/null +++ b/Mishmash/PrimesSet.cs @@ -0,0 +1,50 @@ +namespace Mishmash; + + +public class PrimesSet +{ + private const UInt64 NUM_PRIMES = 0x100; + private readonly UInt32[] Primes = { + 3825240899, 3652005211, 2966014067, 3432177659, 3109134187, 3139884271, 3108258589, 2277840529, + 3748140223, 4206444373, 2684505017, 3883989821, 4076539213, 3880335997, 2603229667, 2358458953, + 4061135443, 3826856503, 2671898833, 3266747401, 3403611587, 2483486933, 3889003891, 2820911959, + 2318077829, 3470930861, 3231587809, 3225029887, 4123396483, 3422817119, 3612514831, 2170177423, + 3058754837, 3000926393, 2825656217, 3387930461, 3532314017, 3245479361, 3466327211, 4080294503, + 4252034179, 2302986211, 3394476707, 3697851029, 3957195257, 2862308587, 4285266071, 3681357247, + 3157577413, 3839398561, 3097979117, 3590787463, 3354450497, 3110291879, 3938796493, 3196834463, + 2374254481, 2702597567, 3046228397, 3461690719, 2641445467, 2401060583, 2483505539, 2775297373, + 2262447391, 3118976533, 3014355683, 3355176449, 4055753381, 2277045713, 3098402119, 3894957487, + 2770620887, 4125228329, 2575044467, 4162428989, 3294651817, 2308925797, 3698223103, 2150023273, + 3075614681, 2410764047, 3624889381, 3264455489, 3241969651, 3001767217, 3407799859, 2998917373, + 2629826653, 2714272271, 3987786247, 2880807353, 3608804803, 2231694917, 3790372403, 4156893413, + 2563320007, 2423350621, 2735169119, 4021079791, 4150641413, 2907916357, 3772971647, 2481168307, + 2842943119, 2234753693, 3966637117, 2732029457, 3207475039, 3533605151, 2349367747, 3336108011, + 2431060103, 2263416899, 2350941683, 3869512277, 3880987697, 3062735029, 2512894603, 3669845519, + 2235487739, 3201016501, 2438124943, 4170458909, 2938134889, 4231610087, 3187120061, 2378420137, + 3365835877, 3078766697, 3704906059, 3541986781, 3969072823, 3510542281, 2306290751, 3898737419, + 2898069347, 4092904481, 2484285403, 2721169823, 4293617527, 2928584759, 2213966141, 2335957513, + 3367371923, 2965261109, 4175805451, 3541995157, 2964065479, 3997902791, 3053542259, 2168926237, + 3253268639, 2620083509, 3314283407, 3873087809, 2636771209, 2737638653, 3209154931, 3414204793, + 3451689091, 2638985941, 2899591693, 2654878441, 2748067627, 3395485733, 2679070523, 3100687721, + 2520033701, 2980087373, 2873947007, 2565436501, 2400053783, 4163039563, 3517993571, 4263192407, + 3385597069, 2768101117, 3502890653, 3092130347, 3748553827, 4109944849, 2418961109, 3398621741, + 3073383031, 2167592489, 2950739053, 3529429811, 3167420899, 4254703357, 3344014309, 3725480141, + 3745944539, 3456003191, 2832137237, 4202217191, 3730577581, 2837794231, 2155546451, 2539211039, + 2256984649, 2458975411, 2986340839, 3412432363, 3596817463, 2973444983, 2409734297, 3273292601, + 3302556869, 3630727567, 3670056499, 3300959521, 3949319809, 3047032057, 3412226563, 2147483647, + 2914045411, 2882644273, 4065606553, 2735903059, 3195020617, 3887229457, 3232900987, 3409357867, + 3037985513, 3162012463, 3340137193, 2186608547, 4018093523, 4153387103, 2566863161, 3087918809, + 3332247019, 3579407009, 3082973791, 4178339461, 3269728331, 2270495261, 2400046513, 2641204147, + 2593078337, 2398468271, 3861488311, 3766456459, 2970457213, 3491800771, 3797865553, 2756555203, + 3154883449, 3782386073, 3324965471, 4088422453, 3784508591, 3903657481, 3010059277, 2936392909, + }; + public UInt32 Select(UInt64 n) + { + return Primes[n & (NUM_PRIMES - 1)]; + } + + public int Count + { + get { return (int)NUM_PRIMES; } + } +} \ No newline at end of file