Skip to content

Commit

Permalink
blake2s midstate, +10%
Browse files Browse the repository at this point in the history
Happy this algo was working well without even a test ^^

Signed-off-by: Tanguy Pruvot <[email protected]>
  • Loading branch information
tpruvot committed Mar 11, 2016
1 parent a2cba7b commit b693a9a
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 46 deletions.
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Version 1.3 (Tanguy Pruvot)
- Add decred algo
- Enhance Blake2-S

Version 1.2 (Tanguy Pruvot)
- Add cryptonight-light (Aeon)
- Add Lyra2REv2 algo (Vertcoin)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Algorithms
*__axiom__ (Axiom Shabal-256 based MemoHash)
*__bastion__ (Joincoin [J])
*__blake__ (Saffron [SFR] Blake-256)
*__blake2s__ (NevaCoin Blake2-S 256)
*__bmw__ (Midnight [MDT] BMW-256)
*__cryptonight__ (Bytecoin [BCN], Monero)
*__cryptonight-light__ (Aeon)
Expand Down Expand Up @@ -54,7 +55,6 @@ Algorithms
*__zr5__ (Ziftrcoin [ZRC])

#### Implemented, but untested
* ? blake2s
* ? hefty1 (Heavycoin)
* ? keccak (Maxcoin HelixCoin, CryptoMeth, Galleon, 365coin, Slothcoin, BitcointalkCoin)
* ? luffa (Joincoin, Doomcoin)
Expand Down
39 changes: 32 additions & 7 deletions algo/blake2.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
/**
* Blake2-S Implementation
* tpruvot@github 2015-2016
*/

#include "miner.h"

#include <string.h>
#include <stdint.h>

#include "crypto/blake2s.h"

inline void blake2s_hash(void *output, const void *input)
static __thread blake2s_state s_midstate;
static __thread blake2s_state s_ctx;
#define MIDLEN 76
#define A 64

void blake2s_hash(void *output, const void *input)
{
unsigned char hash[128] = { 0 };
uint8_t _ALIGN(A) hash[BLAKE2S_OUTBYTES];
blake2s_state blake2_ctx;

blake2s_init(&blake2_ctx, BLAKE2S_OUTBYTES);
Expand All @@ -17,10 +27,18 @@ inline void blake2s_hash(void *output, const void *input)
memcpy(output, hash, 32);
}

static void blake2s_hash_end(uint32_t *output, const uint32_t *input)
{
s_ctx.buflen = MIDLEN;
memcpy(&s_ctx, &s_midstate, 32 + 16 + MIDLEN);
blake2s_update(&s_ctx, (uint8_t*) &input[MIDLEN/4], 80 - MIDLEN);
blake2s_final(&s_ctx, (uint8_t*) output, BLAKE2S_OUTBYTES);
}

int scanhash_blake2s(int thr_id, struct work *work, uint32_t max_nonce, uint64_t *hashes_done)
{
uint32_t _ALIGN(128) hash32[8];
uint32_t _ALIGN(128) endiandata[20];
uint32_t _ALIGN(A) vhashcpu[8];
uint32_t _ALIGN(A) endiandata[20];
uint32_t *pdata = work->data;
uint32_t *ptarget = work->target;

Expand All @@ -33,11 +51,18 @@ int scanhash_blake2s(int thr_id, struct work *work, uint32_t max_nonce, uint64_t
be32enc(&endiandata[i], pdata[i]);
}

// midstate
blake2s_init(&s_midstate, BLAKE2S_OUTBYTES);
blake2s_update(&s_midstate, (uint8_t*) endiandata, MIDLEN);
memcpy(&s_ctx, &s_midstate, sizeof(blake2s_state));

do {
be32enc(&endiandata[19], n);
blake2s_hash(hash32, endiandata);
if (hash32[7] < Htarg && fulltest(hash32, ptarget)) {
work_set_target_ratio(work, hash32);
blake2s_hash_end(vhashcpu, endiandata);

//blake2s_hash(vhashcpu, endiandata);
if (vhashcpu[7] < Htarg && fulltest(vhashcpu, ptarget)) {
work_set_target_ratio(work, vhashcpu);
*hashes_done = n - first_nonce + 1;
pdata[19] = n;
return 1;
Expand Down
5 changes: 3 additions & 2 deletions cpu-miner.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ BOOL WINAPI ConsoleHandler(DWORD);

#define LP_SCANTIME 60

#ifndef min
#define min(a,b) (a>b ? b : a)
#define max(a,b) (a<b ? b : a)
#endif

enum workio_commands {
WC_GET_WORK,
Expand Down Expand Up @@ -267,7 +269,7 @@ Options:\n\
axiom Shabal-256 MemoHash\n\
blake Blake-256 14-rounds (SFR)\n\
blakecoin Blake-256 single sha256 merkle\n\
blake2s Blake-2 S\n\
blake2s Blake2-S (256)\n\
bmw BMW 256\n\
c11/flax C11\n\
cryptolight Cryptonight-light\n\
Expand Down Expand Up @@ -2059,7 +2061,6 @@ static void *miner_thread(void *userdata)
break;
case ALGO_BLAKE:
case ALGO_BLAKECOIN:
case ALGO_BLAKE2S:
case ALGO_DECRED:
case ALGO_VANILLA:
max64 = 0x3fffffLL;
Expand Down
76 changes: 40 additions & 36 deletions crypto/blake2s.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
#define ALIGN(x) __attribute__((aligned(x)))
#endif

#if defined(_MSC_VER) || defined(__x86_64__) || defined(__x86__)
#define NATIVE_LITTLE_ENDIAN
#endif

/* blake2-impl.h */

static inline uint32_t load32(const void *src)
Expand Down Expand Up @@ -87,46 +91,46 @@ static inline void secure_zero_memory(void *v, size_t n)

/* blake2.h */

#if defined(__cplusplus)
extern "C" {
#endif

enum blake2s_constant
{
BLAKE2S_BLOCKBYTES = 64,
BLAKE2S_OUTBYTES = 32,
BLAKE2S_KEYBYTES = 32,
BLAKE2S_SALTBYTES = 8,
BLAKE2S_PERSONALBYTES = 8
};
enum blake2s_constant
{
BLAKE2S_BLOCKBYTES = 64,
BLAKE2S_OUTBYTES = 32,
BLAKE2S_KEYBYTES = 32,
BLAKE2S_SALTBYTES = 8,
BLAKE2S_PERSONALBYTES = 8
};

#pragma pack(push, 1)
typedef struct __blake2s_param
{
uint8_t digest_length; // 1
uint8_t key_length; // 2
uint8_t fanout; // 3
uint8_t depth; // 4
uint32_t leaf_length; // 8
uint8_t node_offset[6];// 14
uint8_t node_depth; // 15
uint8_t inner_length; // 16
// uint8_t reserved[0];
uint8_t salt[BLAKE2S_SALTBYTES]; // 24
uint8_t personal[BLAKE2S_PERSONALBYTES]; // 32
} blake2s_param;

ALIGN( 64 ) typedef struct __blake2s_state
{
uint32_t h[8];
uint32_t t[2];
uint32_t f[2];
uint8_t buf[2 * BLAKE2S_BLOCKBYTES];
size_t buflen;
uint8_t last_node;
} blake2s_state ;
typedef struct __blake2s_param
{
uint8_t digest_length; // 1
uint8_t key_length; // 2
uint8_t fanout; // 3
uint8_t depth; // 4
uint32_t leaf_length; // 8
uint8_t node_offset[6];// 14
uint8_t node_depth; // 15
uint8_t inner_length; // 16
// uint8_t reserved[0];
uint8_t salt[BLAKE2S_SALTBYTES]; // 24
uint8_t personal[BLAKE2S_PERSONALBYTES]; // 32
} blake2s_param;

ALIGN( 64 ) typedef struct __blake2s_state
{
uint32_t h[8];
uint32_t t[2];
uint32_t f[2];
uint8_t buf[2 * BLAKE2S_BLOCKBYTES];
size_t buflen;
uint8_t last_node;
} blake2s_state;
#pragma pack(pop)

#if defined(__cplusplus)
extern "C" {
#endif

int blake2s_compress( blake2s_state *S, const uint8_t block[BLAKE2S_BLOCKBYTES] );

// Streaming API
Expand Down

0 comments on commit b693a9a

Please sign in to comment.