Remove zerocash's SHA256 implementation
This commit is contained in:
parent
8466467a35
commit
d3612b1c94
|
@ -82,7 +82,6 @@ LIBZEROCASH_H = \
|
|||
zerocash/Zerocash.h \
|
||||
zerocash/ZerocashParams.h \
|
||||
zerocash/zerocash_pour_params.hpp \
|
||||
zerocash/utils/sha256.h \
|
||||
zerocash/utils/util.h
|
||||
|
||||
.PHONY: FORCE
|
||||
|
@ -360,6 +359,7 @@ zcashd_LDADD += \
|
|||
$(CRYPTO_LIBS) \
|
||||
$(MINIUPNPC_LIBS) \
|
||||
$(LIBZEROCASH) \
|
||||
$(LIBBITCOIN_CRYPTO) \
|
||||
$(LIBZEROCASH_LIBS)
|
||||
#
|
||||
|
||||
|
@ -379,6 +379,7 @@ zcash_cli_LDADD = \
|
|||
$(SSL_LIBS) \
|
||||
$(CRYPTO_LIBS) \
|
||||
$(LIBZEROCASH) \
|
||||
$(LIBBITCOIN_CRYPTO) \
|
||||
$(LIBZEROCASH_LIBS)
|
||||
#
|
||||
|
||||
|
@ -399,6 +400,7 @@ bitcoin_tx_LDADD = \
|
|||
$(LIBBITCOIN_CRYPTO) \
|
||||
$(LIBSECP256K1) \
|
||||
$(LIBZEROCASH) \
|
||||
$(LIBBITCOIN_CRYPTO) \
|
||||
$(LIBZEROCASH_LIBS)
|
||||
|
||||
bitcoin_tx_LDADD += $(BOOST_LIBS) $(CRYPTO_LIBS)
|
||||
|
@ -416,7 +418,6 @@ libzerocash_a_SOURCES = \
|
|||
zerocash/PourProver.cpp \
|
||||
zerocash/PourTransaction.cpp \
|
||||
zerocash/ZerocashParams.cpp \
|
||||
zerocash/utils/sha256.cpp \
|
||||
zerocash/utils/util.cpp
|
||||
|
||||
libzerocash_a_CPPFLAGS = -fPIC -DCURVE_ALT_BN128 -DBOOST_SPIRIT_THREADSAFE -DHAVE_BUILD_INFO -D__STDC_FORMAT_MACROS -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -std=c++11 -pipe -O2 -O0 -g -Wstack-protector -fstack-protector-all -fPIE -fvisibility=hidden -DSTATIC $(BITCOIN_INCLUDES)
|
||||
|
|
|
@ -10,6 +10,7 @@ zerocash_GenerateParamsForFiles_SOURCES = zerocash/GenerateParamsForFiles.cpp
|
|||
zerocash_GenerateParamsForFiles_LDADD = \
|
||||
$(BOOST_LIBS) \
|
||||
$(LIBZEROCASH) \
|
||||
$(LIBBITCOIN_CRYPTO) \
|
||||
$(LIBZEROCASH_LIBS)
|
||||
|
||||
# tests for our incremental merkle tree
|
||||
|
@ -17,6 +18,7 @@ zerocash_tests_merkleTest_SOURCES = zerocash/tests/merkleTest.cpp
|
|||
zerocash_tests_merkleTest_LDADD = \
|
||||
$(BOOST_LIBS) \
|
||||
$(LIBZEROCASH) \
|
||||
$(LIBBITCOIN_CRYPTO) \
|
||||
$(LIBZEROCASH_LIBS)
|
||||
|
||||
# tests for utilities that come with zerocash
|
||||
|
@ -24,6 +26,8 @@ zerocash_tests_utilTest_SOURCES = zerocash/tests/utilTest.cpp
|
|||
zerocash_tests_utilTest_LDADD = \
|
||||
$(BOOST_LIBS) \
|
||||
$(LIBZEROCASH) \
|
||||
$(LIBBITCOIN_UTIL) \
|
||||
$(LIBBITCOIN_CRYPTO) \
|
||||
$(LIBZEROCASH_LIBS)
|
||||
|
||||
# tests for libzerocash APIs
|
||||
|
@ -34,6 +38,7 @@ zerocash_tests_zerocashTest_SOURCES = \
|
|||
zerocash_tests_zerocashTest_LDADD = \
|
||||
$(BOOST_LIBS) \
|
||||
$(LIBZEROCASH) \
|
||||
$(LIBBITCOIN_CRYPTO) \
|
||||
$(LIBZEROCASH_LIBS)
|
||||
|
||||
# tests for our zkSNARK circuit
|
||||
|
@ -42,5 +47,6 @@ zerocash_tests_test_zerocash_pour_ppzksnark_SOURCES = zerocash/tests/test_zeroca
|
|||
zerocash_tests_test_zerocash_pour_ppzksnark_LDADD = \
|
||||
$(BOOST_LIBS) \
|
||||
$(LIBZEROCASH) \
|
||||
$(LIBBITCOIN_CRYPTO) \
|
||||
$(LIBZEROCASH_LIBS)
|
||||
|
||||
|
|
|
@ -263,17 +263,16 @@ namespace libzerocash {
|
|||
|
||||
// Standard constructor
|
||||
//
|
||||
IncrementalMerkleNode::IncrementalMerkleNode(uint32_t depth, uint32_t height) : left(NULL), right(NULL), value(SHA256_BLOCK_SIZE * 8, 0), nodeDepth(depth), treeHeight(height),
|
||||
IncrementalMerkleNode::IncrementalMerkleNode(uint32_t depth, uint32_t height) : left(NULL), right(NULL), value(CSHA256::OUTPUT_SIZE * 8, 0), nodeDepth(depth), treeHeight(height),
|
||||
subtreeFull(false), subtreePruned(false)
|
||||
{
|
||||
sha256_init(&ctx256);
|
||||
|
||||
}
|
||||
|
||||
// Copy constructor
|
||||
//
|
||||
IncrementalMerkleNode::IncrementalMerkleNode(const IncrementalMerkleNode& toCopy) : left(NULL), right(NULL), value(SHA256_BLOCK_SIZE * 8, 0)
|
||||
IncrementalMerkleNode::IncrementalMerkleNode(const IncrementalMerkleNode& toCopy) : left(NULL), right(NULL), value(CSHA256::OUTPUT_SIZE * 8, 0)
|
||||
{
|
||||
sha256_init(&ctx256);
|
||||
this->nodeDepth = toCopy.nodeDepth;
|
||||
this->subtreePruned = toCopy.subtreePruned;
|
||||
this->subtreeFull = toCopy.subtreeFull;
|
||||
|
@ -379,7 +378,7 @@ namespace libzerocash {
|
|||
|
||||
// Make sure there is a value on the right. If not we put the 'null' hash (0) into that element.
|
||||
if (this->right == NULL) {
|
||||
witness.at(nodeDepth).resize(SHA256_BLOCK_SIZE * 8);
|
||||
witness.at(nodeDepth).resize(CSHA256::OUTPUT_SIZE * 8);
|
||||
std::fill (witness.at(nodeDepth).begin(), witness.at(nodeDepth).end(), false);
|
||||
} else {
|
||||
this->right->getValue(witness.at(nodeDepth));
|
||||
|
@ -453,8 +452,8 @@ namespace libzerocash {
|
|||
|
||||
// Obtain the hash of the two subtrees and hash the
|
||||
// concatenation of the two.
|
||||
std::vector<bool> hash(SHA256_BLOCK_SIZE * 8);
|
||||
std::vector<bool> zero(SHA256_BLOCK_SIZE * 8);
|
||||
std::vector<bool> hash(CSHA256::OUTPUT_SIZE * 8);
|
||||
std::vector<bool> zero(CSHA256::OUTPUT_SIZE * 8);
|
||||
std::fill (zero.begin(), zero.end(), false);
|
||||
|
||||
// The following code is ugly and should be refactored. It runs
|
||||
|
@ -464,19 +463,19 @@ namespace libzerocash {
|
|||
if (VectorIsZero(this->left->getValue())) {
|
||||
hash = zero;
|
||||
} else {
|
||||
hashVectors(&ctx256, this->left->getValue(), zero, hash);
|
||||
hashVectors(this->left->getValue(), zero, hash);
|
||||
}
|
||||
} else if (!(this->left) && this->right) {
|
||||
if (VectorIsZero(this->right->getValue())) {
|
||||
hash = zero;
|
||||
} else {
|
||||
hashVectors(&ctx256, zero, this->left->getValue(), hash);
|
||||
hashVectors(zero, this->left->getValue(), hash);
|
||||
}
|
||||
} else if (this->left && this->right) {
|
||||
if (VectorIsZero(this->left->getValue()) && VectorIsZero(this->right->getValue())) {
|
||||
hash = zero;
|
||||
} else {
|
||||
hashVectors(&ctx256, this->left->getValue(), this->right->getValue(), hash);
|
||||
hashVectors(this->left->getValue(), this->right->getValue(), hash);
|
||||
}
|
||||
} else {
|
||||
hash = zero;
|
||||
|
@ -521,7 +520,7 @@ namespace libzerocash {
|
|||
|
||||
// Otherwise: Add our left child hash to the tree.
|
||||
rep.hashList.at(this->nodeDepth) = true;
|
||||
std::vector<unsigned char> hash(SHA256_BLOCK_SIZE, 0);
|
||||
std::vector<unsigned char> hash(CSHA256::OUTPUT_SIZE, 0);
|
||||
convertVectorToBytesVector(this->left->getValue(), hash);
|
||||
rep.hashVec.push_back(hash);
|
||||
|
||||
|
@ -574,7 +573,7 @@ namespace libzerocash {
|
|||
this->left = new IncrementalMerkleNode(this->nodeDepth + 1, this->treeHeight);
|
||||
|
||||
// Fill the left node with the value and mark it full/pruned
|
||||
std::vector<bool> hash(SHA256_BLOCK_SIZE * 8, 0);
|
||||
std::vector<bool> hash(CSHA256::OUTPUT_SIZE * 8, 0);
|
||||
convertBytesVectorToVector(rep.hashVec.at(pos), hash);
|
||||
this->left->value = hash;
|
||||
this->left->subtreePruned = this->left->subtreeFull = true;
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
#ifndef INCREMENTALMERKLETREE_H_
|
||||
#define INCREMENTALMERKLETREE_H_
|
||||
|
||||
#include "utils/sha256.h"
|
||||
#include "crypto/sha256.h"
|
||||
|
||||
#include "Zerocash.h"
|
||||
#include <vector>
|
||||
|
@ -59,7 +59,7 @@ private:
|
|||
|
||||
class IncrementalMerkleNode {
|
||||
public:
|
||||
SHA256_CTX_mod ctx256;
|
||||
CSHA256 ctx256;
|
||||
IncrementalMerkleNode* left;
|
||||
IncrementalMerkleNode* right;
|
||||
std::vector<bool> value;
|
||||
|
|
|
@ -3,17 +3,39 @@
|
|||
#include <boost/test/included/unit_test.hpp>
|
||||
|
||||
#include "zerocash/utils/util.h"
|
||||
#include "zerocash/utils/sha256.h"
|
||||
#include "crypto/sha256.h"
|
||||
|
||||
#define SHA256_PREIMAGE_BYTES 3
|
||||
const unsigned char sha256_preimage[SHA256_PREIMAGE_BYTES] = { 'a', 'b', 'c' };
|
||||
#include "uint256.h"
|
||||
#include "utilstrencodings.h"
|
||||
|
||||
#define SHA256_PREIMAGE_BYTES 64
|
||||
const unsigned char sha256_preimage[SHA256_PREIMAGE_BYTES] = { 'a', 'b', 'c', 'd',
|
||||
'a', 'b', 'c', 'd',
|
||||
'a', 'b', 'c', 'd',
|
||||
'a', 'b', 'c', 'd',
|
||||
'a', 'b', 'c', 'd',
|
||||
'a', 'b', 'c', 'd',
|
||||
'a', 'b', 'c', 'd',
|
||||
'a', 'b', 'c', 'd',
|
||||
'a', 'b', 'c', 'd',
|
||||
'a', 'b', 'c', 'd',
|
||||
'a', 'b', 'c', 'd',
|
||||
'a', 'b', 'c', 'd',
|
||||
'a', 'b', 'c', 'd',
|
||||
'a', 'b', 'c', 'd',
|
||||
'a', 'b', 'c', 'd',
|
||||
'a', 'b', 'c', 'd'
|
||||
};
|
||||
/* This is the SHA256 hash of "abc" according to the modified implementation of
|
||||
* SHA256 included in libzerocash. */
|
||||
const unsigned char sha256_hash[32] = { 0x6a, 0x09, 0xe6, 0x67, 0xbb, 0x67, 0xae,
|
||||
0x85, 0x3c, 0x6e, 0xf3, 0x72, 0xa5, 0x4f,
|
||||
0xf5, 0x3a, 0x51, 0x0e, 0x52, 0x7f, 0x9b,
|
||||
0x05, 0x68, 0x8c, 0x1f, 0x83, 0xd9, 0xab,
|
||||
0x5b, 0xe0, 0xcd, 0x19 };
|
||||
const unsigned char sha256_hash[32] = { 0x86, 0x7d, 0x98, 0x11,
|
||||
0x86, 0x2d, 0xbd, 0xab,
|
||||
0x2f, 0x8f, 0xa3, 0x43,
|
||||
0xe3, 0xe8, 0x41, 0xdf,
|
||||
0x7d, 0xb2, 0xde, 0xd4,
|
||||
0x33, 0x17, 0x28, 0x00,
|
||||
0xb0, 0x36, 0x9e, 0x87,
|
||||
0x41, 0xec, 0x70, 0xda };
|
||||
|
||||
BOOST_AUTO_TEST_CASE( testGetRandBytes ) {
|
||||
unsigned char bytes1[32];
|
||||
|
@ -348,15 +370,15 @@ BOOST_AUTO_TEST_CASE( testConcatenateThreeByteVectors ) {
|
|||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( testSHA256ModifiedTestVectors ) {
|
||||
unsigned char actual_hash[32];
|
||||
libzerocash::sha256(sha256_preimage, actual_hash, 3);
|
||||
BOOST_CHECK( memcmp(sha256_hash, actual_hash, 32) == 0 );
|
||||
uint256 actual_hash;
|
||||
libzerocash::sha256(sha256_preimage, actual_hash.begin(), SHA256_PREIMAGE_BYTES);
|
||||
BOOST_CHECK_MESSAGE( memcmp(sha256_hash, actual_hash.begin(), 32) == 0, HexStr(actual_hash.begin(), actual_hash.end()));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( testSHA256ModifiedTestVectorsCTX ) {
|
||||
unsigned char actual_hash[32];
|
||||
SHA256_CTX_mod ctx256;
|
||||
libzerocash::sha256(&ctx256, sha256_preimage, actual_hash, 3);
|
||||
CSHA256 ctx256;
|
||||
libzerocash::sha256(ctx256, sha256_preimage, actual_hash, SHA256_PREIMAGE_BYTES);
|
||||
BOOST_CHECK( memcmp(sha256_hash, actual_hash, 32) == 0 );
|
||||
}
|
||||
|
||||
|
@ -372,17 +394,15 @@ BOOST_AUTO_TEST_CASE( testSHA256TestVectors ) {
|
|||
0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00,
|
||||
0x15, 0xad };
|
||||
unsigned char actual_hash[32];
|
||||
SHA256_CTX_mod ctx256;
|
||||
sha256_init(&ctx256);
|
||||
sha256_update(&ctx256, preimage, 3);
|
||||
sha256_length_padding(&ctx256);
|
||||
sha256_final_no_padding(&ctx256, actual_hash);
|
||||
CSHA256 ctx256;
|
||||
ctx256.Write(preimage, 3);
|
||||
ctx256.Finalize(actual_hash);
|
||||
|
||||
BOOST_CHECK( memcmp(expected_hash, actual_hash, 32) == 0 );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( testHashBoolVectorToBoolVectorCTX ) {
|
||||
SHA256_CTX_mod ctx256;
|
||||
CSHA256 ctx256;
|
||||
|
||||
std::vector<bool> preimage(SHA256_PREIMAGE_BYTES * 8);
|
||||
libzerocash::convertBytesToVector(sha256_preimage, preimage);
|
||||
|
@ -392,13 +412,13 @@ BOOST_AUTO_TEST_CASE( testHashBoolVectorToBoolVectorCTX ) {
|
|||
|
||||
// TODO: evaluate whether this should be a necessary precondition.
|
||||
std::vector<bool> actual(32*8);
|
||||
libzerocash::hashVector(&ctx256, preimage, actual);
|
||||
libzerocash::hashVector(ctx256, preimage, actual);
|
||||
|
||||
BOOST_CHECK( expected == actual );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( testHashByteVectorToByteVectorCTX ) {
|
||||
SHA256_CTX_mod ctx256;
|
||||
CSHA256 ctx256;
|
||||
|
||||
std::vector<unsigned char> preimage(SHA256_PREIMAGE_BYTES);
|
||||
libzerocash::convertBytesToBytesVector(sha256_preimage, preimage);
|
||||
|
@ -408,7 +428,7 @@ BOOST_AUTO_TEST_CASE( testHashByteVectorToByteVectorCTX ) {
|
|||
|
||||
// TODO: evaluate whether this should be a necessary precondition.
|
||||
std::vector<unsigned char> actual(32);
|
||||
libzerocash::hashVector(&ctx256, preimage, actual);
|
||||
libzerocash::hashVector(ctx256, preimage, actual);
|
||||
|
||||
BOOST_CHECK( expected == actual );
|
||||
}
|
||||
|
@ -443,7 +463,7 @@ BOOST_AUTO_TEST_CASE( testHashByteVectorToByteVector ) {
|
|||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( testHashBoolVectorsCTX ) {
|
||||
SHA256_CTX_mod ctx256;
|
||||
CSHA256 ctx256;
|
||||
|
||||
std::vector<bool> preimage1(8);
|
||||
libzerocash::convertBytesToVector(sha256_preimage, preimage1);
|
||||
|
@ -456,13 +476,13 @@ BOOST_AUTO_TEST_CASE( testHashBoolVectorsCTX ) {
|
|||
|
||||
// TODO: evaluate whether this should be a necessary precondition.
|
||||
std::vector<bool> actual(32 * 8);
|
||||
libzerocash::hashVectors(&ctx256, preimage1, preimage2, actual);
|
||||
libzerocash::hashVectors(ctx256, preimage1, preimage2, actual);
|
||||
|
||||
BOOST_CHECK( expected == actual );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( testHashByteVectorsCTX ) {
|
||||
SHA256_CTX_mod ctx256;
|
||||
CSHA256 ctx256;
|
||||
|
||||
std::vector<unsigned char> preimage1(1);
|
||||
libzerocash::convertBytesToBytesVector(sha256_preimage, preimage1);
|
||||
|
@ -475,7 +495,7 @@ BOOST_AUTO_TEST_CASE( testHashByteVectorsCTX ) {
|
|||
|
||||
// TODO: evaluate whether this should be a necessary precondition.
|
||||
std::vector<unsigned char> actual(32);
|
||||
libzerocash::hashVectors(&ctx256, preimage1, preimage2, actual);
|
||||
libzerocash::hashVectors(ctx256, preimage1, preimage2, actual);
|
||||
|
||||
BOOST_CHECK( expected == actual );
|
||||
}
|
||||
|
|
|
@ -511,12 +511,12 @@ BOOST_AUTO_TEST_CASE( MerkleTreeSimpleTest ) {
|
|||
}
|
||||
cout << "\n" << endl;
|
||||
|
||||
vector<bool> wit1(SHA256_BLOCK_SIZE * 8);
|
||||
vector<bool> wit2(SHA256_BLOCK_SIZE * 8);
|
||||
vector<bool> wit3(SHA256_BLOCK_SIZE * 8);
|
||||
vector<bool> inter_1(SHA256_BLOCK_SIZE * 8);
|
||||
vector<bool> inter_2(SHA256_BLOCK_SIZE * 8);
|
||||
std::vector<bool> zeros(SHA256_BLOCK_SIZE * 8, 0);
|
||||
vector<bool> wit1(CSHA256::OUTPUT_SIZE * 8);
|
||||
vector<bool> wit2(CSHA256::OUTPUT_SIZE * 8);
|
||||
vector<bool> wit3(CSHA256::OUTPUT_SIZE * 8);
|
||||
vector<bool> inter_1(CSHA256::OUTPUT_SIZE * 8);
|
||||
vector<bool> inter_2(CSHA256::OUTPUT_SIZE * 8);
|
||||
std::vector<bool> zeros(CSHA256::OUTPUT_SIZE * 8, 0);
|
||||
|
||||
wit1 = coinValues.at(2);
|
||||
libzerocash::hashVectors(coinValues.at(0), coinValues.at(1), wit2);
|
||||
|
|
|
@ -1,163 +0,0 @@
|
|||
/*********************************************************************
|
||||
* Filename: sha256.c
|
||||
* Author: Brad Conte (brad AT bradconte.com)
|
||||
* Copyright:
|
||||
* Disclaimer: This code is presented "as is" without any guarantees.
|
||||
* Details: Implementation of the SHA-256 hashing algorithm.
|
||||
SHA-256 is one of the three algorithms in the SHA2
|
||||
specification. The others, SHA-384 and SHA-512, are not
|
||||
offered in this implementation.
|
||||
Algorithm specification can be found here:
|
||||
* http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf
|
||||
This implementation uses little endian byte order.
|
||||
*********************************************************************/
|
||||
|
||||
/*************************** HEADER FILES ***************************/
|
||||
#include <stdlib.h>
|
||||
#include <memory.h>
|
||||
#include "sha256.h"
|
||||
|
||||
/****************************** MACROS ******************************/
|
||||
#define ROTLEFT(a,b) (((a) << (b)) | ((a) >> (32-(b))))
|
||||
#define ROTRIGHT(a,b) (((a) >> (b)) | ((a) << (32-(b))))
|
||||
|
||||
#define CH(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
|
||||
#define MAJ(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
|
||||
#define EP0(x) (ROTRIGHT(x,2) ^ ROTRIGHT(x,13) ^ ROTRIGHT(x,22))
|
||||
#define EP1(x) (ROTRIGHT(x,6) ^ ROTRIGHT(x,11) ^ ROTRIGHT(x,25))
|
||||
#define SIG0(x) (ROTRIGHT(x,7) ^ ROTRIGHT(x,18) ^ ((x) >> 3))
|
||||
#define SIG1(x) (ROTRIGHT(x,17) ^ ROTRIGHT(x,19) ^ ((x) >> 10))
|
||||
|
||||
/**************************** VARIABLES *****************************/
|
||||
static const uint32_t k[64] = {
|
||||
0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
|
||||
0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
|
||||
0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
|
||||
0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
|
||||
0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
|
||||
0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
|
||||
0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
|
||||
0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
|
||||
};
|
||||
|
||||
/*********************** FUNCTION DEFINITIONS ***********************/
|
||||
void sha256_transform(SHA256_CTX_mod *ctx, const uint8_t data[])
|
||||
{
|
||||
uint32_t a, b, c, d, e, f, g, h, i, j, t1, t2, m[64];
|
||||
|
||||
for (i = 0, j = 0; i < 16; ++i, j += 4)
|
||||
m[i] = (data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | (data[j + 3]);
|
||||
for ( ; i < 64; ++i)
|
||||
m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16];
|
||||
|
||||
a = ctx->state[0];
|
||||
b = ctx->state[1];
|
||||
c = ctx->state[2];
|
||||
d = ctx->state[3];
|
||||
e = ctx->state[4];
|
||||
f = ctx->state[5];
|
||||
g = ctx->state[6];
|
||||
h = ctx->state[7];
|
||||
|
||||
for (i = 0; i < 64; ++i) {
|
||||
t1 = h + EP1(e) + CH(e,f,g) + k[i] + m[i];
|
||||
t2 = EP0(a) + MAJ(a,b,c);
|
||||
h = g;
|
||||
g = f;
|
||||
f = e;
|
||||
e = d + t1;
|
||||
d = c;
|
||||
c = b;
|
||||
b = a;
|
||||
a = t1 + t2;
|
||||
}
|
||||
|
||||
ctx->state[0] += a;
|
||||
ctx->state[1] += b;
|
||||
ctx->state[2] += c;
|
||||
ctx->state[3] += d;
|
||||
ctx->state[4] += e;
|
||||
ctx->state[5] += f;
|
||||
ctx->state[6] += g;
|
||||
ctx->state[7] += h;
|
||||
}
|
||||
|
||||
void sha256_init(SHA256_CTX_mod *ctx)
|
||||
{
|
||||
ctx->datalen = 0;
|
||||
ctx->bitlen = 0;
|
||||
ctx->state[0] = 0x6a09e667;
|
||||
ctx->state[1] = 0xbb67ae85;
|
||||
ctx->state[2] = 0x3c6ef372;
|
||||
ctx->state[3] = 0xa54ff53a;
|
||||
ctx->state[4] = 0x510e527f;
|
||||
ctx->state[5] = 0x9b05688c;
|
||||
ctx->state[6] = 0x1f83d9ab;
|
||||
ctx->state[7] = 0x5be0cd19;
|
||||
}
|
||||
|
||||
void sha256_update(SHA256_CTX_mod *ctx, const uint8_t data[], size_t len)
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < len; ++i) {
|
||||
ctx->data[ctx->datalen] = data[i];
|
||||
ctx->datalen++;
|
||||
if (ctx->datalen == 64) {
|
||||
sha256_transform(ctx, ctx->data);
|
||||
ctx->bitlen += 512;
|
||||
ctx->datalen = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Applies the length padding, which libzerocash does not use. Call before
|
||||
* sha256_final_no_padding() to get the test-vector compliant SHA256 hash. */
|
||||
void sha256_length_padding(SHA256_CTX_mod *ctx)
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
i = ctx->datalen;
|
||||
|
||||
// Pad whatever data is left in the buffer.
|
||||
if (ctx->datalen < 56) {
|
||||
ctx->data[i++] = 0x80;
|
||||
while (i < 56)
|
||||
ctx->data[i++] = 0x00;
|
||||
}
|
||||
else {
|
||||
ctx->data[i++] = 0x80;
|
||||
while (i < 64)
|
||||
ctx->data[i++] = 0x00;
|
||||
sha256_transform(ctx, ctx->data);
|
||||
memset(ctx->data, 0, 56);
|
||||
}
|
||||
|
||||
// Append to the padding the total message's length in bits and transform.
|
||||
ctx->bitlen += ctx->datalen * 8;
|
||||
ctx->data[63] = ctx->bitlen;
|
||||
ctx->data[62] = ctx->bitlen >> 8;
|
||||
ctx->data[61] = ctx->bitlen >> 16;
|
||||
ctx->data[60] = ctx->bitlen >> 24;
|
||||
ctx->data[59] = ctx->bitlen >> 32;
|
||||
ctx->data[58] = ctx->bitlen >> 40;
|
||||
ctx->data[57] = ctx->bitlen >> 48;
|
||||
ctx->data[56] = ctx->bitlen >> 56;
|
||||
sha256_transform(ctx, ctx->data);
|
||||
}
|
||||
|
||||
void sha256_final_no_padding(SHA256_CTX_mod *ctx, uint8_t hash[])
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < 4; ++i) {
|
||||
hash[i] = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff;
|
||||
hash[i + 4] = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff;
|
||||
hash[i + 8] = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff;
|
||||
hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0x000000ff;
|
||||
hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0x000000ff;
|
||||
hash[i + 20] = (ctx->state[5] >> (24 - i * 8)) & 0x000000ff;
|
||||
hash[i + 24] = (ctx->state[6] >> (24 - i * 8)) & 0x000000ff;
|
||||
hash[i + 28] = (ctx->state[7] >> (24 - i * 8)) & 0x000000ff;
|
||||
}
|
||||
}
|
|
@ -1,32 +0,0 @@
|
|||
/*********************************************************************
|
||||
* Filename: sha256.h
|
||||
* Author: Brad Conte (brad AT bradconte.com)
|
||||
* Copyright:
|
||||
* Disclaimer: This code is presented "as is" without any guarantees.
|
||||
* Details: Defines the API for the corresponding SHA1 implementation.
|
||||
*********************************************************************/
|
||||
|
||||
#ifndef SHA256H_H
|
||||
#define SHA256H_H
|
||||
|
||||
/*************************** HEADER FILES ***************************/
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/****************************** MACROS ******************************/
|
||||
#define SHA256_BLOCK_SIZE 32 // SHA256 outputs a 32 byte digest
|
||||
|
||||
typedef struct {
|
||||
uint8_t data[64];
|
||||
uint32_t datalen;
|
||||
unsigned long long bitlen;
|
||||
uint32_t state[8];
|
||||
} SHA256_CTX_mod;
|
||||
|
||||
/*********************** FUNCTION DECLARATIONS **********************/
|
||||
void sha256_init(SHA256_CTX_mod *ctx);
|
||||
void sha256_update(SHA256_CTX_mod *ctx, const uint8_t data[], size_t len);
|
||||
void sha256_length_padding(SHA256_CTX_mod *ctx);
|
||||
void sha256_final_no_padding(SHA256_CTX_mod *ctx, uint8_t hash[]);
|
||||
|
||||
#endif // SHA256H_H
|
|
@ -203,68 +203,65 @@ void concatenateVectors(const std::vector<unsigned char>& A, const std::vector<u
|
|||
}
|
||||
|
||||
void sha256(const unsigned char* input, unsigned char* hash, int len) {
|
||||
SHA256_CTX_mod ctx256;
|
||||
|
||||
sha256_init(&ctx256);
|
||||
sha256_update(&ctx256, input, len);
|
||||
sha256_final_no_padding(&ctx256, hash);
|
||||
CSHA256 hasher;
|
||||
hasher.Write(input, len);
|
||||
hasher.FinalizeNoPadding(hash);
|
||||
}
|
||||
|
||||
void sha256(SHA256_CTX_mod* ctx256, const unsigned char* input, unsigned char* hash, int len) {
|
||||
sha256_init(ctx256);
|
||||
sha256_update(ctx256, input, len);
|
||||
sha256_final_no_padding(ctx256, hash);
|
||||
void sha256(CSHA256& hasher, const unsigned char* input, unsigned char* hash, int len) {
|
||||
hasher.Write(input, len);
|
||||
hasher.FinalizeNoPadding(hash);
|
||||
}
|
||||
|
||||
void hashVector(SHA256_CTX_mod* ctx256, const std::vector<bool> input, std::vector<bool>& output) {
|
||||
void hashVector(CSHA256& hasher, const std::vector<bool> input, std::vector<bool>& output) {
|
||||
int size = int(input.size() / 8);
|
||||
unsigned char bytes[size];
|
||||
convertVectorToBytes(input, bytes);
|
||||
|
||||
unsigned char hash[SHA256_BLOCK_SIZE];
|
||||
sha256(ctx256, bytes, hash, (int)size);
|
||||
unsigned char hash[CSHA256::OUTPUT_SIZE];
|
||||
sha256(hasher, bytes, hash, (int)size);
|
||||
|
||||
convertBytesToVector(hash, output);
|
||||
}
|
||||
|
||||
void hashVector(SHA256_CTX_mod* ctx256, const std::vector<unsigned char> input, std::vector<unsigned char>& output) {
|
||||
void hashVector(CSHA256& hasher, const std::vector<unsigned char> input, std::vector<unsigned char>& output) {
|
||||
int size = int(input.size());
|
||||
unsigned char bytes[size];
|
||||
convertBytesVectorToBytes(input, bytes);
|
||||
|
||||
unsigned char hash[SHA256_BLOCK_SIZE];
|
||||
sha256(ctx256, bytes, hash, (int)size);
|
||||
unsigned char hash[CSHA256::OUTPUT_SIZE];
|
||||
sha256(hasher, bytes, hash, (int)size);
|
||||
|
||||
convertBytesToBytesVector(hash, output);
|
||||
}
|
||||
|
||||
void hashVector(const std::vector<bool> input, std::vector<bool>& output) {
|
||||
SHA256_CTX_mod ctx256;
|
||||
CSHA256 hasher;
|
||||
|
||||
int size = int(input.size() / 8);
|
||||
unsigned char bytes[size];
|
||||
convertVectorToBytes(input, bytes);
|
||||
|
||||
unsigned char hash[SHA256_BLOCK_SIZE];
|
||||
sha256(&ctx256, bytes, hash, (int)size);
|
||||
unsigned char hash[CSHA256::OUTPUT_SIZE];
|
||||
sha256(hasher, bytes, hash, (int)size);
|
||||
|
||||
convertBytesToVector(hash, output);
|
||||
}
|
||||
|
||||
void hashVector(const std::vector<unsigned char> input, std::vector<unsigned char>& output) {
|
||||
SHA256_CTX_mod ctx256;
|
||||
CSHA256 hasher;
|
||||
|
||||
int size = int(input.size());
|
||||
unsigned char bytes[size];
|
||||
convertBytesVectorToBytes(input, bytes);
|
||||
|
||||
unsigned char hash[SHA256_BLOCK_SIZE];
|
||||
sha256(&ctx256, bytes, hash, (int)size);
|
||||
unsigned char hash[CSHA256::OUTPUT_SIZE];
|
||||
sha256(hasher, bytes, hash, (int)size);
|
||||
|
||||
convertBytesToBytesVector(hash, output);
|
||||
}
|
||||
|
||||
void hashVectors(SHA256_CTX_mod* ctx256, const std::vector<bool> left, const std::vector<bool> right, std::vector<bool>& output) {
|
||||
void hashVectors(CSHA256& hasher, const std::vector<bool> left, const std::vector<bool> right, std::vector<bool>& output) {
|
||||
std::vector<bool> concat;
|
||||
concatenateVectors(left, right, concat);
|
||||
|
||||
|
@ -272,13 +269,13 @@ void hashVectors(SHA256_CTX_mod* ctx256, const std::vector<bool> left, const std
|
|||
unsigned char bytes[size];
|
||||
convertVectorToBytes(concat, bytes);
|
||||
|
||||
unsigned char hash[SHA256_BLOCK_SIZE];
|
||||
sha256(ctx256, bytes, hash, (int)size);
|
||||
unsigned char hash[CSHA256::OUTPUT_SIZE];
|
||||
sha256(hasher, bytes, hash, (int)size);
|
||||
|
||||
convertBytesToVector(hash, output);
|
||||
}
|
||||
|
||||
void hashVectors(SHA256_CTX_mod* ctx256, const std::vector<unsigned char> left, const std::vector<unsigned char> right, std::vector<unsigned char>& output) {
|
||||
void hashVectors(CSHA256& hasher, const std::vector<unsigned char> left, const std::vector<unsigned char> right, std::vector<unsigned char>& output) {
|
||||
std::vector<unsigned char> concat;
|
||||
concatenateVectors(left, right, concat);
|
||||
|
||||
|
@ -286,23 +283,21 @@ void hashVectors(SHA256_CTX_mod* ctx256, const std::vector<unsigned char> left,
|
|||
unsigned char bytes[size];
|
||||
convertBytesVectorToBytes(concat, bytes);
|
||||
|
||||
unsigned char hash[SHA256_BLOCK_SIZE];
|
||||
sha256(ctx256, bytes, hash, (int)size);
|
||||
unsigned char hash[CSHA256::OUTPUT_SIZE];
|
||||
sha256(hasher, bytes, hash, (int)size);
|
||||
|
||||
convertBytesToBytesVector(hash, output);
|
||||
}
|
||||
|
||||
void hashVectors(const std::vector<bool> left, const std::vector<bool> right, std::vector<bool>& output) {
|
||||
std::cout << std::endl;
|
||||
|
||||
std::vector<bool> concat;
|
||||
std::vector<bool> concat;
|
||||
concatenateVectors(left, right, concat);
|
||||
|
||||
int size = int(concat.size() / 8);
|
||||
unsigned char bytes[size];
|
||||
convertVectorToBytes(concat, bytes);
|
||||
|
||||
unsigned char hash[SHA256_BLOCK_SIZE];
|
||||
unsigned char hash[CSHA256::OUTPUT_SIZE];
|
||||
sha256(bytes, hash, (int)size);
|
||||
|
||||
convertBytesToVector(hash, output);
|
||||
|
@ -316,7 +311,7 @@ void hashVectors(const std::vector<unsigned char> left, const std::vector<unsign
|
|||
unsigned char bytes[size];
|
||||
convertBytesVectorToBytes(concat, bytes);
|
||||
|
||||
unsigned char hash[SHA256_BLOCK_SIZE];
|
||||
unsigned char hash[CSHA256::OUTPUT_SIZE];
|
||||
sha256(bytes, hash, (int)size);
|
||||
|
||||
convertBytesToBytesVector(hash, output);
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
#include <vector>
|
||||
#include <cstdint>
|
||||
|
||||
#include "sha256.h"
|
||||
#include "crypto/sha256.h"
|
||||
|
||||
namespace libzerocash {
|
||||
|
||||
|
@ -60,19 +60,19 @@ void concatenateVectors(const std::vector<unsigned char>& A, const std::vector<u
|
|||
|
||||
void sha256(const unsigned char* input, unsigned char* hash, int len);
|
||||
|
||||
void sha256(SHA256_CTX_mod* ctx256, const unsigned char* input, unsigned char* hash, int len);
|
||||
void sha256(CSHA256& ctx256, const unsigned char* input, unsigned char* hash, int len);
|
||||
|
||||
void hashVector(SHA256_CTX_mod* ctx256, const std::vector<bool> input, std::vector<bool>& output);
|
||||
void hashVector(CSHA256& ctx256, const std::vector<bool> input, std::vector<bool>& output);
|
||||
|
||||
void hashVector(SHA256_CTX_mod* ctx256, const std::vector<unsigned char> input, std::vector<unsigned char>& output);
|
||||
void hashVector(CSHA256& ctx256, const std::vector<unsigned char> input, std::vector<unsigned char>& output);
|
||||
|
||||
void hashVector(const std::vector<bool> input, std::vector<bool>& output);
|
||||
|
||||
void hashVector(const std::vector<unsigned char> input, std::vector<unsigned char>& output);
|
||||
|
||||
void hashVectors(SHA256_CTX_mod* ctx256, const std::vector<bool> left, const std::vector<bool> right, std::vector<bool>& output);
|
||||
void hashVectors(CSHA256& ctx256, const std::vector<bool> left, const std::vector<bool> right, std::vector<bool>& output);
|
||||
|
||||
void hashVectors(SHA256_CTX_mod* ctx256, const std::vector<unsigned char> left, const std::vector<unsigned char> right, std::vector<unsigned char>& output);
|
||||
void hashVectors(CSHA256& ctx256, const std::vector<unsigned char> left, const std::vector<unsigned char> right, std::vector<unsigned char>& output);
|
||||
|
||||
void hashVectors(const std::vector<bool> left, const std::vector<bool> right, std::vector<bool>& output);
|
||||
|
||||
|
|
Loading…
Reference in New Issue