Skip to content

Commit aef9ec2

Browse files
committed
Add Phi2 Algorithm
1 parent a064e67 commit aef9ec2

File tree

7 files changed

+111
-1
lines changed

7 files changed

+111
-1
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Miningcore.Contracts;
2+
using Miningcore.Native;
3+
4+
namespace Miningcore.Crypto.Hashing.Algorithms;
5+
6+
[Identifier("phi2")]
7+
public unsafe class Phi2 : IHashAlgorithm
8+
{
9+
public void Digest(ReadOnlySpan<byte> data, Span<byte> result, params object[] extra)
10+
{
11+
Contract.Requires<ArgumentException>(result.Length >= 32);
12+
13+
fixed (byte* input = data)
14+
{
15+
fixed (byte* output = result)
16+
{
17+
Multihash.phi2(input, output, (uint) data.Length);
18+
}
19+
}
20+
}
21+
}

src/Miningcore/Native/Multihash.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public static unsafe class Multihash
3737
[DllImport("libmultihash", EntryPoint = "phi_export", CallingConvention = CallingConvention.Cdecl)]
3838
public static extern void phi(byte* input, void* output, uint inputLength);
3939

40+
[DllImport("libmultihash", EntryPoint = "phi2_export", CallingConvention = CallingConvention.Cdecl)]
41+
public static extern void phi2(byte* input, void* output, uint inputLength);
42+
4043
[DllImport("libmultihash", EntryPoint = "x11_export", CallingConvention = CallingConvention.Cdecl)]
4144
public static extern void x11(byte* input, void* output, uint inputLength);
4245

src/Native/libmultihash/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ TARGET = libmultihash.so
66

77
OBJECTS = allium.o aurum.o bcrypt.o blake.o c11.o dcrypt.o fresh.o lane.o megabtx.o memehash.o \
88
fugue.o groestl.o hefty1.o jh.o keccak.o neoscrypt.o exports.o nist5.o quark.o qubit.o s3.o scryptn.o \
9-
sha256csm.o hmq17.o phi.o \
9+
sha256csm.o hmq17.o phi.o phi2.o \
1010
sha3/aes_helper.o sha3/hamsi.o sha3/hamsi_helper.o sha3/sph_blake.o sha3/sph_bmw.o sha3/sph_cubehash.o \
1111
sha3/sph_echo.o sha3/sph_fugue.o sha3/sph_groestl.o sha3/sph_hefty1.o sha3/sph_jh.o sha3/sph_keccak.o \
1212
sha3/sph_luffa.o sha3/sph_shabal.o sha3/sph_shavite.o sha3/sph_simd.o sha3/sph_skein.o sha3/sph_whirlpool.o \

src/Native/libmultihash/exports.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
5757
#include "sha256dt.h"
5858
#include "hmq17.h"
5959
#include "phi.h"
60+
#include "phi2.h"
6061
#include "verthash/h2.h"
6162
#include "equi/equihashverify.h"
6263
#include "heavyhash/heavyhash.h"
@@ -138,6 +139,11 @@ extern "C" MODULE_API void phi_export(const char* input, char* output, uint32_t
138139
phi_hash(input, output, input_len);
139140
}
140141

142+
extern "C" MODULE_API void phi2_export(const char* input, char* output, uint32_t input_len)
143+
{
144+
phi2_hash(input, output, input_len);
145+
}
146+
141147
extern "C" MODULE_API void x11_export(const char* input, char* output, uint32_t input_len)
142148
{
143149
x11_hash(input, output, input_len);

src/Native/libmultihash/libmultihash.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@
217217
<ClInclude Include="nist5.h" />
218218
<ClInclude Include="odocrypt.h" />
219219
<ClInclude Include="phi.h" />
220+
<ClInclude Include="phi2.h" />
220221
<ClInclude Include="portable_endian.h" />
221222
<ClInclude Include="quark.h" />
222223
<ClInclude Include="qubit.h" />
@@ -344,6 +345,7 @@
344345
<ClCompile Include="exports.cpp" />
345346
<ClCompile Include="nist5.c" />
346347
<ClCompile Include="phi.c" />
348+
<ClCompile Include="phi2.c" />
347349
<ClCompile Include="quark.c" />
348350
<ClCompile Include="qubit.c" />
349351
<ClCompile Include="s3.c" />

src/Native/libmultihash/phi2.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include <stdlib.h>
2+
#include <stdint.h>
3+
#include <string.h>
4+
#include <stdio.h>
5+
6+
#include "sha3/sph_cubehash.h"
7+
#include "sha3/sph_jh.h"
8+
#include "sha3/sph_echo.h"
9+
#include "sha3/sph_skein.h"
10+
11+
#include "sha3/gost_streebog.h"
12+
13+
#include "Lyra2.h"
14+
15+
#define _ALIGN(x) __attribute__ ((aligned(x)))
16+
17+
void phi2_hash(const char* input, char* output, uint32_t len)
18+
{
19+
unsigned char _ALIGN(128) hash[64];
20+
unsigned char _ALIGN(128) hashA[64];
21+
unsigned char _ALIGN(128) hashB[64];
22+
23+
sph_cubehash512_context ctx_cubehash;
24+
sph_jh512_context ctx_jh;
25+
sph_gost512_context ctx_gost;
26+
sph_echo512_context ctx_echo;
27+
sph_skein512_context ctx_skein;
28+
29+
sph_cubehash512_init(&ctx_cubehash);
30+
sph_cubehash512(&ctx_cubehash, input, len);
31+
sph_cubehash512_close(&ctx_cubehash, (void*)hashB);
32+
33+
LYRA2(&hashA[ 0], 32, &hashB[ 0], 32, &hashB[ 0], 32, 1, 8, 8);
34+
LYRA2(&hashA[32], 32, &hashB[32], 32, &hashB[32], 32, 1, 8, 8);
35+
36+
sph_jh512_init(&ctx_jh);
37+
sph_jh512(&ctx_jh, (const void*)hashA, 64);
38+
sph_jh512_close(&ctx_jh, (void*)hash);
39+
40+
if (hash[0] & 1) {
41+
sph_gost512_init(&ctx_gost);
42+
sph_gost512(&ctx_gost, (const void*)hash, 64);
43+
sph_gost512_close(&ctx_gost, (void*)hash);
44+
} else {
45+
sph_echo512_init(&ctx_echo);
46+
sph_echo512(&ctx_echo, (const void*)hash, 64);
47+
sph_echo512_close(&ctx_echo, (void*)hash);
48+
49+
sph_echo512_init(&ctx_echo);
50+
sph_echo512(&ctx_echo, (const void*)hash, 64);
51+
sph_echo512_close(&ctx_echo, (void*)hash);
52+
}
53+
54+
sph_skein512_init(&ctx_skein);
55+
sph_skein512(&ctx_skein, (const void*)hash, 64);
56+
sph_skein512_close(&ctx_skein, (void*)hash);
57+
58+
for (int i=0; i<32; i++)
59+
hash[i] ^= hash[i+32];
60+
61+
memcpy(output, hash, 32);
62+
}

src/Native/libmultihash/phi2.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef PHI2_H
2+
#define PHI2_H
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
#include <stdint.h>
9+
10+
void phi2_hash(const char* input, char* output, uint32_t len);
11+
12+
#ifdef __cplusplus
13+
}
14+
#endif
15+
16+
#endif

0 commit comments

Comments
 (0)