Skip to content

Commit

Permalink
Litecoin: Scrypt n=1024 Pow hash based upon Colin Percival's Tarnsnap…
Browse files Browse the repository at this point in the history
… (2009) Modified by Artforz, coblee, pooler, wtogami, Nikolay Belikov, Adrian Gallagher
  • Loading branch information
wtogami authored and Ross Nicoll committed Sep 19, 2018
1 parent 3cff8e9 commit b506efb
Show file tree
Hide file tree
Showing 12 changed files with 577 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ crypto_libbitcoin_crypto_a_SOURCES = \
crypto/hmac_sha512.h \
crypto/ripemd160.cpp \
crypto/ripemd160.h \
crypto/scrypt.cpp \
crypto/scrypt.h \
crypto/sha1.cpp \
crypto/sha1.h \
crypto/sha256.cpp \
Expand Down
1 change: 1 addition & 0 deletions src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ BITCOIN_TESTS =\
test/script_P2SH_tests.cpp \
test/script_tests.cpp \
test/scriptnum_tests.cpp \
test/scrypt_tests.cpp \
test/serialize_tests.cpp \
test/sighash_tests.cpp \
test/sigopcount_tests.cpp \
Expand Down
11 changes: 11 additions & 0 deletions src/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ class CBlockIndex
unsigned int nBits;
unsigned int nNonce;

// Dogecoin: Keep the Scrypt hash as well as SHA256
uint256 hashBlockPoW;

//! (memory only) Sequential id assigned to distinguish order in which blocks are received.
int32_t nSequenceId;

Expand Down Expand Up @@ -226,6 +229,7 @@ class CBlockIndex
nTime = 0;
nBits = 0;
nNonce = 0;
hashBlockPoW = uint256();
}

CBlockIndex()
Expand All @@ -242,6 +246,7 @@ class CBlockIndex
nTime = block.nTime;
nBits = block.nBits;
nNonce = block.nNonce;
hashBlockPoW = block.GetPoWHash();
}

CDiskBlockPos GetBlockPos() const {
Expand Down Expand Up @@ -280,6 +285,11 @@ class CBlockIndex
return *phashBlock;
}

uint256 GetBlockPoWHash() const
{
return hashBlockPoW;
}

int64_t GetBlockTime() const
{
return (int64_t)nTime;
Expand Down Expand Up @@ -388,6 +398,7 @@ class CDiskBlockIndex : public CBlockIndex
READWRITE(nTime);
READWRITE(nBits);
READWRITE(nNonce);
READWRITE(hashBlockPoW);
}

uint256 GetBlockHash() const
Expand Down
136 changes: 136 additions & 0 deletions src/crypto/scrypt-sse2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
* Copyright 2009 Colin Percival, 2011 ArtForz, 2012-2013 pooler
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file was originally written by Colin Percival as part of the Tarsnap
* online backup system.
*/

#include "crypto/scrypt.h"
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <openssl/sha.h>

#include <emmintrin.h>

static inline void xor_salsa8_sse2(__m128i B[4], const __m128i Bx[4])
{
__m128i X0, X1, X2, X3;
__m128i T;
int i;

X0 = B[0] = _mm_xor_si128(B[0], Bx[0]);
X1 = B[1] = _mm_xor_si128(B[1], Bx[1]);
X2 = B[2] = _mm_xor_si128(B[2], Bx[2]);
X3 = B[3] = _mm_xor_si128(B[3], Bx[3]);

for (i = 0; i < 8; i += 2) {
/* Operate on "columns". */
T = _mm_add_epi32(X0, X3);
X1 = _mm_xor_si128(X1, _mm_slli_epi32(T, 7));
X1 = _mm_xor_si128(X1, _mm_srli_epi32(T, 25));
T = _mm_add_epi32(X1, X0);
X2 = _mm_xor_si128(X2, _mm_slli_epi32(T, 9));
X2 = _mm_xor_si128(X2, _mm_srli_epi32(T, 23));
T = _mm_add_epi32(X2, X1);
X3 = _mm_xor_si128(X3, _mm_slli_epi32(T, 13));
X3 = _mm_xor_si128(X3, _mm_srli_epi32(T, 19));
T = _mm_add_epi32(X3, X2);
X0 = _mm_xor_si128(X0, _mm_slli_epi32(T, 18));
X0 = _mm_xor_si128(X0, _mm_srli_epi32(T, 14));

/* Rearrange data. */
X1 = _mm_shuffle_epi32(X1, 0x93);
X2 = _mm_shuffle_epi32(X2, 0x4E);
X3 = _mm_shuffle_epi32(X3, 0x39);

/* Operate on "rows". */
T = _mm_add_epi32(X0, X1);
X3 = _mm_xor_si128(X3, _mm_slli_epi32(T, 7));
X3 = _mm_xor_si128(X3, _mm_srli_epi32(T, 25));
T = _mm_add_epi32(X3, X0);
X2 = _mm_xor_si128(X2, _mm_slli_epi32(T, 9));
X2 = _mm_xor_si128(X2, _mm_srli_epi32(T, 23));
T = _mm_add_epi32(X2, X3);
X1 = _mm_xor_si128(X1, _mm_slli_epi32(T, 13));
X1 = _mm_xor_si128(X1, _mm_srli_epi32(T, 19));
T = _mm_add_epi32(X1, X2);
X0 = _mm_xor_si128(X0, _mm_slli_epi32(T, 18));
X0 = _mm_xor_si128(X0, _mm_srli_epi32(T, 14));

/* Rearrange data. */
X1 = _mm_shuffle_epi32(X1, 0x39);
X2 = _mm_shuffle_epi32(X2, 0x4E);
X3 = _mm_shuffle_epi32(X3, 0x93);
}

B[0] = _mm_add_epi32(B[0], X0);
B[1] = _mm_add_epi32(B[1], X1);
B[2] = _mm_add_epi32(B[2], X2);
B[3] = _mm_add_epi32(B[3], X3);
}

void scrypt_1024_1_1_256_sp_sse2(const char *input, char *output, char *scratchpad)
{
uint8_t B[128];
union {
__m128i i128[8];
uint32_t u32[32];
} X;
__m128i *V;
uint32_t i, j, k;

V = (__m128i *)(((uintptr_t)(scratchpad) + 63) & ~ (uintptr_t)(63));

PBKDF2_SHA256((const uint8_t *)input, 80, (const uint8_t *)input, 80, 1, B, 128);

for (k = 0; k < 2; k++) {
for (i = 0; i < 16; i++) {
X.u32[k * 16 + i] = le32dec(&B[(k * 16 + (i * 5 % 16)) * 4]);
}
}

for (i = 0; i < 1024; i++) {
for (k = 0; k < 8; k++)
V[i * 8 + k] = X.i128[k];
xor_salsa8_sse2(&X.i128[0], &X.i128[4]);
xor_salsa8_sse2(&X.i128[4], &X.i128[0]);
}
for (i = 0; i < 1024; i++) {
j = 8 * (X.u32[16] & 1023);
for (k = 0; k < 8; k++)
X.i128[k] = _mm_xor_si128(X.i128[k], V[j + k]);
xor_salsa8_sse2(&X.i128[0], &X.i128[4]);
xor_salsa8_sse2(&X.i128[4], &X.i128[0]);
}

for (k = 0; k < 2; k++) {
for (i = 0; i < 16; i++) {
le32enc(&B[(k * 16 + (i * 5 % 16)) * 4], X.u32[k * 16 + i]);
}
}

PBKDF2_SHA256((const uint8_t *)input, 80, B, 128, 1, (uint8_t *)output, 32);
}
Loading

0 comments on commit b506efb

Please sign in to comment.