Skip to content

Commit

Permalink
Add Phi2 Algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaolin1579 committed Jan 9, 2025
1 parent a064e67 commit aef9ec2
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/Miningcore/Crypto/Hashing/Algorithms/Phi2.cs
Original file line number Diff line number Diff line change
@@ -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<byte> data, Span<byte> result, params object[] extra)
{
Contract.Requires<ArgumentException>(result.Length >= 32);

fixed (byte* input = data)
{
fixed (byte* output = result)
{
Multihash.phi2(input, output, (uint) data.Length);
}
}
}
}
3 changes: 3 additions & 0 deletions src/Miningcore/Native/Multihash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/Native/libmultihash/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
6 changes: 6 additions & 0 deletions src/Native/libmultihash/exports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/Native/libmultihash/libmultihash.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@
<ClInclude Include="nist5.h" />
<ClInclude Include="odocrypt.h" />
<ClInclude Include="phi.h" />
<ClInclude Include="phi2.h" />
<ClInclude Include="portable_endian.h" />
<ClInclude Include="quark.h" />
<ClInclude Include="qubit.h" />
Expand Down Expand Up @@ -344,6 +345,7 @@
<ClCompile Include="exports.cpp" />
<ClCompile Include="nist5.c" />
<ClCompile Include="phi.c" />
<ClCompile Include="phi2.c" />
<ClCompile Include="quark.c" />
<ClCompile Include="qubit.c" />
<ClCompile Include="s3.c" />
Expand Down
62 changes: 62 additions & 0 deletions src/Native/libmultihash/phi2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>

#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);
}
16 changes: 16 additions & 0 deletions src/Native/libmultihash/phi2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef PHI2_H
#define PHI2_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>

void phi2_hash(const char* input, char* output, uint32_t len);

#ifdef __cplusplus
}
#endif

#endif

0 comments on commit aef9ec2

Please sign in to comment.