From aef9ec2ec1b94705bfebc48596ce51a9b5816711 Mon Sep 17 00:00:00 2001 From: Decho Kocharin Date: Thu, 9 Jan 2025 23:45:47 +0700 Subject: [PATCH] Add Phi2 Algorithm --- .../Crypto/Hashing/Algorithms/Phi2.cs | 21 +++++++ src/Miningcore/Native/Multihash.cs | 3 + src/Native/libmultihash/Makefile | 2 +- src/Native/libmultihash/exports.cpp | 6 ++ src/Native/libmultihash/libmultihash.vcxproj | 2 + src/Native/libmultihash/phi2.c | 62 +++++++++++++++++++ src/Native/libmultihash/phi2.h | 16 +++++ 7 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 src/Miningcore/Crypto/Hashing/Algorithms/Phi2.cs create mode 100644 src/Native/libmultihash/phi2.c create mode 100644 src/Native/libmultihash/phi2.h diff --git a/src/Miningcore/Crypto/Hashing/Algorithms/Phi2.cs b/src/Miningcore/Crypto/Hashing/Algorithms/Phi2.cs new file mode 100644 index 000000000..55ebf6c3b --- /dev/null +++ b/src/Miningcore/Crypto/Hashing/Algorithms/Phi2.cs @@ -0,0 +1,21 @@ +using Miningcore.Contracts; +using Miningcore.Native; + +namespace Miningcore.Crypto.Hashing.Algorithms; + +[Identifier("phi2")] +public unsafe class Phi2 : IHashAlgorithm +{ + public void Digest(ReadOnlySpan data, Span result, params object[] extra) + { + Contract.Requires(result.Length >= 32); + + fixed (byte* input = data) + { + fixed (byte* output = result) + { + Multihash.phi2(input, output, (uint) data.Length); + } + } + } +} diff --git a/src/Miningcore/Native/Multihash.cs b/src/Miningcore/Native/Multihash.cs index 8741dde92..5945091a4 100644 --- a/src/Miningcore/Native/Multihash.cs +++ b/src/Miningcore/Native/Multihash.cs @@ -37,6 +37,9 @@ public static unsafe class Multihash [DllImport("libmultihash", EntryPoint = "phi_export", CallingConvention = CallingConvention.Cdecl)] public static extern void phi(byte* input, void* output, uint inputLength); + [DllImport("libmultihash", EntryPoint = "phi2_export", CallingConvention = CallingConvention.Cdecl)] + public static extern void phi2(byte* input, void* output, uint inputLength); + [DllImport("libmultihash", EntryPoint = "x11_export", CallingConvention = CallingConvention.Cdecl)] public static extern void x11(byte* input, void* output, uint inputLength); diff --git a/src/Native/libmultihash/Makefile b/src/Native/libmultihash/Makefile index ab1064592..e176e23bb 100644 --- a/src/Native/libmultihash/Makefile +++ b/src/Native/libmultihash/Makefile @@ -6,7 +6,7 @@ TARGET = libmultihash.so OBJECTS = allium.o aurum.o bcrypt.o blake.o c11.o dcrypt.o fresh.o lane.o megabtx.o memehash.o \ fugue.o groestl.o hefty1.o jh.o keccak.o neoscrypt.o exports.o nist5.o quark.o qubit.o s3.o scryptn.o \ - sha256csm.o hmq17.o phi.o \ + sha256csm.o hmq17.o phi.o phi2.o \ sha3/aes_helper.o sha3/hamsi.o sha3/hamsi_helper.o sha3/sph_blake.o sha3/sph_bmw.o sha3/sph_cubehash.o \ sha3/sph_echo.o sha3/sph_fugue.o sha3/sph_groestl.o sha3/sph_hefty1.o sha3/sph_jh.o sha3/sph_keccak.o \ sha3/sph_luffa.o sha3/sph_shabal.o sha3/sph_shavite.o sha3/sph_simd.o sha3/sph_skein.o sha3/sph_whirlpool.o \ diff --git a/src/Native/libmultihash/exports.cpp b/src/Native/libmultihash/exports.cpp index d7d38b743..b22d2ec43 100644 --- a/src/Native/libmultihash/exports.cpp +++ b/src/Native/libmultihash/exports.cpp @@ -57,6 +57,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include "sha256dt.h" #include "hmq17.h" #include "phi.h" +#include "phi2.h" #include "verthash/h2.h" #include "equi/equihashverify.h" #include "heavyhash/heavyhash.h" @@ -138,6 +139,11 @@ extern "C" MODULE_API void phi_export(const char* input, char* output, uint32_t phi_hash(input, output, input_len); } +extern "C" MODULE_API void phi2_export(const char* input, char* output, uint32_t input_len) +{ + phi2_hash(input, output, input_len); +} + extern "C" MODULE_API void x11_export(const char* input, char* output, uint32_t input_len) { x11_hash(input, output, input_len); diff --git a/src/Native/libmultihash/libmultihash.vcxproj b/src/Native/libmultihash/libmultihash.vcxproj index 744d29e13..fba98da4b 100644 --- a/src/Native/libmultihash/libmultihash.vcxproj +++ b/src/Native/libmultihash/libmultihash.vcxproj @@ -217,6 +217,7 @@ + @@ -344,6 +345,7 @@ + diff --git a/src/Native/libmultihash/phi2.c b/src/Native/libmultihash/phi2.c new file mode 100644 index 000000000..7f85ae91a --- /dev/null +++ b/src/Native/libmultihash/phi2.c @@ -0,0 +1,62 @@ +#include +#include +#include +#include + +#include "sha3/sph_cubehash.h" +#include "sha3/sph_jh.h" +#include "sha3/sph_echo.h" +#include "sha3/sph_skein.h" + +#include "sha3/gost_streebog.h" + +#include "Lyra2.h" + +#define _ALIGN(x) __attribute__ ((aligned(x))) + +void phi2_hash(const char* input, char* output, uint32_t len) +{ + unsigned char _ALIGN(128) hash[64]; + unsigned char _ALIGN(128) hashA[64]; + unsigned char _ALIGN(128) hashB[64]; + + sph_cubehash512_context ctx_cubehash; + sph_jh512_context ctx_jh; + sph_gost512_context ctx_gost; + sph_echo512_context ctx_echo; + sph_skein512_context ctx_skein; + + sph_cubehash512_init(&ctx_cubehash); + sph_cubehash512(&ctx_cubehash, input, len); + sph_cubehash512_close(&ctx_cubehash, (void*)hashB); + + LYRA2(&hashA[ 0], 32, &hashB[ 0], 32, &hashB[ 0], 32, 1, 8, 8); + LYRA2(&hashA[32], 32, &hashB[32], 32, &hashB[32], 32, 1, 8, 8); + + sph_jh512_init(&ctx_jh); + sph_jh512(&ctx_jh, (const void*)hashA, 64); + sph_jh512_close(&ctx_jh, (void*)hash); + + if (hash[0] & 1) { + sph_gost512_init(&ctx_gost); + sph_gost512(&ctx_gost, (const void*)hash, 64); + sph_gost512_close(&ctx_gost, (void*)hash); + } else { + sph_echo512_init(&ctx_echo); + sph_echo512(&ctx_echo, (const void*)hash, 64); + sph_echo512_close(&ctx_echo, (void*)hash); + + sph_echo512_init(&ctx_echo); + sph_echo512(&ctx_echo, (const void*)hash, 64); + sph_echo512_close(&ctx_echo, (void*)hash); + } + + sph_skein512_init(&ctx_skein); + sph_skein512(&ctx_skein, (const void*)hash, 64); + sph_skein512_close(&ctx_skein, (void*)hash); + + for (int i=0; i<32; i++) + hash[i] ^= hash[i+32]; + + memcpy(output, hash, 32); +} diff --git a/src/Native/libmultihash/phi2.h b/src/Native/libmultihash/phi2.h new file mode 100644 index 000000000..d551d2849 --- /dev/null +++ b/src/Native/libmultihash/phi2.h @@ -0,0 +1,16 @@ +#ifndef PHI2_H +#define PHI2_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +void phi2_hash(const char* input, char* output, uint32_t len); + +#ifdef __cplusplus +} +#endif + +#endif