Skip to content

Commit

Permalink
Add HashLib and methods for epochs.
Browse files Browse the repository at this point in the history
  • Loading branch information
zawawa committed Oct 16, 2017
1 parent 0230346 commit 50a8f7b
Show file tree
Hide file tree
Showing 113 changed files with 52,821 additions and 6 deletions.
162 changes: 162 additions & 0 deletions GatelessGateSharp.sln

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions GatelessGateSharp/GatelessGateSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@
<SignAssembly>false</SignAssembly>
</PropertyGroup>
<ItemGroup>
<Reference Include="HashLib, Version=2.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\HashLib.2.0.1\lib\net40\HashLib.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
Expand Down Expand Up @@ -160,6 +156,12 @@
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HashLib\HashLib.csproj">
<Project>{31880fb6-5b86-42b8-ab92-864045b5d7a2}</Project>
<Name>HashLib</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>copy $(SolutionDir)$(SolutionName)\SQLite.Interop.dll $(TargetDir)
Expand Down
748 changes: 748 additions & 0 deletions GatelessGateSharp/Miner/OpenCLEthashMiner.cs

Large diffs are not rendered by default.

37 changes: 36 additions & 1 deletion GatelessGateSharp/Stratum/NiceHashEthashStratum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using System.Net.Sockets;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

using HashLib;


namespace GatelessGateSharp
Expand Down Expand Up @@ -37,6 +37,41 @@ public Job(string aID, string aSeedhash, string aHeaderhash)
mHeaderhash = aHeaderhash;
}

public static string ByteArrayToString(byte[] ba)
{
StringBuilder hex = new StringBuilder(ba.Length * 2);
foreach (byte b in ba)
hex.AppendFormat("{0:x2}", b);
return hex.ToString();
}

public static byte[] StringToByteArray(String hex)
{
int NumberChars = hex.Length;
byte[] bytes = new byte[NumberChars / 2];
for (int i = 0; i < NumberChars; i += 2)
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
return bytes;
}

public int Epoch {
get {
byte[] seedhashArray = StringToByteArray(Seedhash);
byte[] s = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
IHash hash = HashFactory.Crypto.SHA3.CreateKeccak256();
int i;
for (i = 0; i < 2048; ++i)
{
s = hash.ComputeBytes(s).GetBytes();
if (s.SequenceEqual(seedhashArray))
break;
}
if (i >= 2048)
throw new Exception("Invalid seedhash.");
return i + 1;
}
}

public bool Equals(Job aJob)
{
return (mID.Equals(aJob.mID));
Expand Down
1 change: 0 additions & 1 deletion GatelessGateSharp/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="HashLib" version="2.0.1" targetFramework="net45" />
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net45" />
</packages>
44 changes: 44 additions & 0 deletions HashLib/Checksum/Adler32.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Diagnostics;

namespace HashLib.Checksum
{
internal class Adler32 : Hash, IChecksum, IBlockHash, IHash32
{
private const uint MOD_ADLER = 65521;

private uint m_a;
private uint m_b;

public Adler32()
: base(4, 1)
{
}

public override void Initialize()
{
m_a = 1;
m_b = 0;
}

public override void TransformBytes(byte[] a_data, int a_index, int a_length)
{
Debug.Assert(a_index >= 0);
Debug.Assert(a_length >= 0);
Debug.Assert(a_index + a_length <= a_data.Length);

for (int i = a_index; a_length > 0; i++, a_length--)
{
m_a = (m_a + a_data[i]) % MOD_ADLER;
m_b = (m_b + m_a) % MOD_ADLER;

}
}

public override HashResult TransformFinal()
{
return new HashResult((m_b << 16) | m_a);

}
}
}
102 changes: 102 additions & 0 deletions HashLib/Checksum/CRC32.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System;
using System.Diagnostics;
using System.Linq;

namespace HashLib.Checksum
{
public static class CRC32Polynomials
{
public static uint IEEE_802_3 = 0xEDB88320;
public static uint Castagnoli = 0x82F63B78;
public static uint Koopman = 0xEB31D82E;
public static uint CRC_32Q = 0xD5828281;
}

internal class CRC32_IEEE : CRC32
{
public CRC32_IEEE()
: base(CRC32Polynomials.IEEE_802_3)
{
}
}

internal class CRC32_CASTAGNOLI : CRC32
{
public CRC32_CASTAGNOLI()
: base(CRC32Polynomials.Castagnoli)
{
}
}

internal class CRC32_KOOPMAN : CRC32
{
public CRC32_KOOPMAN()
: base(CRC32Polynomials.Koopman)
{
}
}

internal class CRC32_Q : CRC32
{
public CRC32_Q()
: base(CRC32Polynomials.CRC_32Q)
{
}
}

internal class CRC32 : Hash, IChecksum, IBlockHash, IHash32
{
private uint[] m_crc_tab = new uint[256];

private uint m_hash;
private uint m_initial_value;
private uint m_final_xor;

public CRC32(uint a_polynomial, uint a_initial_value = uint.MaxValue, uint a_final_xor = uint.MaxValue)
: base(4, 1)
{
m_initial_value = a_initial_value;
m_final_xor = a_final_xor;

GenerateCRCTable(a_polynomial);
}

private void GenerateCRCTable(uint a_poly32)
{
for (uint i = 0; i < 256; ++i)
{
uint crc = i;

for (int j = 0; j < 8; j++)
{
if ((crc & 1) == 1)
crc = (crc >> 1) ^ a_poly32;
else
crc = crc >> 1;
}

m_crc_tab[i] = crc;
}
}

public override void Initialize()
{
m_hash = m_initial_value;
}

public override void TransformBytes(byte[] a_data, int a_index, int a_length)
{
Debug.Assert(a_index >= 0);
Debug.Assert(a_length >= 0);
Debug.Assert(a_index + a_length <= a_data.Length);

for (int i = a_index; a_length > 0; i++, a_length--)
m_hash = (m_hash >> 8) ^ m_crc_tab[(byte)m_hash ^ a_data[i]];
}

public override HashResult TransformFinal()
{
return new HashResult(m_hash ^ m_final_xor);
}
}
}
83 changes: 83 additions & 0 deletions HashLib/Checksum/CRC64.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System;
using System.Diagnostics;

namespace HashLib.Checksum
{
public static class CRC64Polynomials
{
public static ulong ISO = 0xD800000000000000;
public static ulong ECMA_182 = 0xC96C5795D7870F42;
}

internal class CRC64_ISO : CRC64
{
public CRC64_ISO()
: base(CRC64Polynomials.ISO)
{
}
}

internal class CRC64_ECMA : CRC64
{
public CRC64_ECMA()
: base(CRC64Polynomials.ECMA_182)
{
}
}

internal class CRC64 : Hash, IChecksum, IBlockHash, IHash64
{
private ulong[] m_crc_tab = new ulong[256];

private ulong m_hash;
private ulong m_initial_value;
private ulong m_final_xor;

public CRC64(ulong a_polynomial, ulong a_initial_value = ulong.MaxValue, ulong a_final_xor = ulong.MaxValue)
: base(8, 1)
{
m_initial_value = a_initial_value;
m_final_xor = a_final_xor;

GenerateCRCTable(a_polynomial);
}

private void GenerateCRCTable(ulong a_poly64)
{
for (uint i = 0; i < 256; ++i)
{
ulong crc = i;

for (uint j = 0; j < 8; ++j)
{
if ((crc & 1) == 1)
crc = (crc >> 1) ^ a_poly64;
else
crc >>= 1;
}

m_crc_tab[i] = crc;
}
}

public override void Initialize()
{
m_hash = m_initial_value;
}

public override void TransformBytes(byte[] a_data, int a_index, int a_length)
{
Debug.Assert(a_index >= 0);
Debug.Assert(a_length >= 0);
Debug.Assert(a_index + a_length <= a_data.Length);

for (int i = a_index; a_length > 0; i++, a_length--)
m_hash = (m_hash >> 8) ^ m_crc_tab[(byte)m_hash ^ a_data[i]];
}

public override HashResult TransformFinal()
{
return new HashResult(m_hash ^ m_final_xor);
}
}
}
17 changes: 17 additions & 0 deletions HashLib/Crypto/BuildIn/MD5CryptoServiceProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace HashLib.Crypto.BuildIn
{
internal class MD5CryptoServiceProvider : HashCryptoBuildIn, IHasHMACBuildIn
{
public MD5CryptoServiceProvider()
: base(new System.Security.Cryptography.MD5CryptoServiceProvider(), 64)
{
}

public virtual System.Security.Cryptography.HMAC GetBuildHMAC()
{
return new System.Security.Cryptography.HMACMD5();
}
}
}
17 changes: 17 additions & 0 deletions HashLib/Crypto/BuildIn/RIPEMD160Managed .cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace HashLib.Crypto.BuildIn
{
internal class RIPEMD160Managed : HashCryptoBuildIn, IHasHMACBuildIn
{
public RIPEMD160Managed()
: base(new System.Security.Cryptography.RIPEMD160Managed(), 64)
{
}

public virtual System.Security.Cryptography.HMAC GetBuildHMAC()
{
return new System.Security.Cryptography.HMACRIPEMD160();
}
}
}
12 changes: 12 additions & 0 deletions HashLib/Crypto/BuildIn/SHA1Cng.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace HashLib.Crypto.BuildIn
{
internal class SHA1Cng : HashCryptoBuildIn
{
public SHA1Cng()
: base(new System.Security.Cryptography.SHA1Cng(), 64)
{
}
}
}
17 changes: 17 additions & 0 deletions HashLib/Crypto/BuildIn/SHA1CryptoServiceProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace HashLib.Crypto.BuildIn
{
internal class SHA1CryptoServiceProvider : HashCryptoBuildIn, IHasHMACBuildIn
{
public SHA1CryptoServiceProvider()
: base(new System.Security.Cryptography.SHA1CryptoServiceProvider(), 64)
{
}

public virtual System.Security.Cryptography.HMAC GetBuildHMAC()
{
return new System.Security.Cryptography.HMACSHA1(new byte[0], false);
}
}
}
17 changes: 17 additions & 0 deletions HashLib/Crypto/BuildIn/SHA1Managed.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace HashLib.Crypto.BuildIn
{
internal class SHA1Managed : HashCryptoBuildIn, IHasHMACBuildIn
{
public SHA1Managed()
: base(new System.Security.Cryptography.SHA1Managed(), 64)
{
}

public virtual System.Security.Cryptography.HMAC GetBuildHMAC()
{
return new System.Security.Cryptography.HMACSHA1(new byte[0], true);
}
}
}
Loading

0 comments on commit 50a8f7b

Please sign in to comment.