Remove unused code in libzerocash util.cpp
This commit is contained in:
parent
ec3bd70b6d
commit
7c1ccd9006
|
@ -52,6 +52,35 @@ BOOST_AUTO_TEST_CASE(compression)
|
|||
BOOST_CHECK_MESSAGE(digest == uint256S("d8a93718eaf9feba4362d2c091d4e58ccabe9f779957336269b4b917be9856da"),
|
||||
digest.GetHex());
|
||||
}
|
||||
|
||||
{
|
||||
unsigned char preimage[64] = { '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'
|
||||
};
|
||||
CSHA256 hasher;
|
||||
hasher.Write(&preimage[0], 64);
|
||||
|
||||
uint256 digest;
|
||||
|
||||
hasher.FinalizeNoPadding(digest.begin());
|
||||
|
||||
BOOST_CHECK_MESSAGE(digest == uint256S("da70ec41879e36b000281733d4deb27ddf41e8e343a38f2fabbd2d8611987d86"),
|
||||
digest.GetHex());
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
|
|
@ -8,49 +8,6 @@
|
|||
#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] = { 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];
|
||||
unsigned char bytes2[32];
|
||||
|
||||
memset(bytes1, 0, 32);
|
||||
memset(bytes2, 0, 32);
|
||||
|
||||
libzerocash::getRandBytes(bytes1, 32);
|
||||
libzerocash::getRandBytes(bytes2, 32);
|
||||
|
||||
BOOST_CHECK( memcmp(bytes1, bytes2, 32) != 0 );
|
||||
BOOST_CHECK( memcmp(bytes1, bytes1+16, 16) != 0 );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( testConvertVectorToInt ) {
|
||||
BOOST_CHECK(libzerocash::convertVectorToInt({0}) == 0);
|
||||
BOOST_CHECK(libzerocash::convertVectorToInt({1}) == 1);
|
||||
|
@ -74,87 +31,6 @@ BOOST_AUTO_TEST_CASE( testConvertVectorToInt ) {
|
|||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( testConvertBytesToVector ) {
|
||||
unsigned char bytes[5] = {0x00, 0x01, 0x03, 0x12, 0xFF};
|
||||
std::vector<bool> v1(5*8);
|
||||
libzerocash::convertBytesToVector(bytes, v1);
|
||||
|
||||
std::vector<bool> v2 = {
|
||||
// 0x00
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
// 0x01
|
||||
0, 0, 0, 0, 0, 0, 0, 1,
|
||||
// 0x03
|
||||
0, 0, 0, 0, 0, 0, 1, 1,
|
||||
// 0x12
|
||||
0, 0, 0, 1, 0, 0, 1, 0,
|
||||
// 0xFF
|
||||
1, 1, 1, 1, 1, 1, 1, 1
|
||||
};
|
||||
|
||||
BOOST_CHECK(v1 == v2);
|
||||
|
||||
std::vector<bool> unevensize(4);
|
||||
unsigned char abyte[1] = { 0x55 };
|
||||
libzerocash::convertBytesToVector(abyte, unevensize);
|
||||
|
||||
/* This may not be what we would expect, but this test will alert us if the
|
||||
* behavior changes. */
|
||||
v2 = { 0, 0, 0, 0 };
|
||||
BOOST_CHECK(unevensize == v2);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( testConvertVectorToBytes) {
|
||||
unsigned char bytes[5] = {0x00, 0x01, 0x03, 0x12, 0xFF};
|
||||
std::vector<bool> v = {
|
||||
// 0x00
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
// 0x01
|
||||
0, 0, 0, 0, 0, 0, 0, 1,
|
||||
// 0x03
|
||||
0, 0, 0, 0, 0, 0, 1, 1,
|
||||
// 0x12
|
||||
0, 0, 0, 1, 0, 0, 1, 0,
|
||||
// 0xFF
|
||||
1, 1, 1, 1, 1, 1, 1, 1
|
||||
};
|
||||
unsigned char output[5];
|
||||
libzerocash::convertVectorToBytes(v, output);
|
||||
BOOST_CHECK( memcmp(bytes, output, sizeof(bytes)) == 0 );
|
||||
|
||||
/* This is not necessarily the behavior one would expect, but this test will
|
||||
* notify us if it changes. */
|
||||
unsigned char onebyte[1];
|
||||
std::vector<bool> unevensize { 1, 1, 1, 1, 1, 1, 1 };
|
||||
libzerocash::convertVectorToBytes(unevensize, onebyte);
|
||||
BOOST_CHECK(onebyte[0] == 0);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( testConvertBytesToBytesVector ) {
|
||||
unsigned char bytes[16];
|
||||
for (int i = 0; i < 16; i++) {
|
||||
bytes[i] = i;
|
||||
}
|
||||
std::vector<unsigned char> v(16);
|
||||
libzerocash::convertBytesToBytesVector(bytes, v);
|
||||
for (int i = 0; i < 16; i++) {
|
||||
BOOST_CHECK(v.at(i) == bytes[i]);
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( testConvertBytesVectorToBytes ) {
|
||||
std::vector<unsigned char>v(16);
|
||||
for (int i = 0; i < 16; i++) {
|
||||
v[i] = i;
|
||||
}
|
||||
unsigned char bytes[16];
|
||||
memset(bytes, 0, 16);
|
||||
libzerocash::convertBytesVectorToBytes(v, bytes);
|
||||
for (int i = 0; i < 16; i++) {
|
||||
BOOST_CHECK(bytes[i] == v.at(i));
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( testConvertBytesVectorToVector ) {
|
||||
std::vector<unsigned char> bytes = {0x00, 0x01, 0x03, 0x12, 0xFF};
|
||||
std::vector<bool> expected_bits = {
|
||||
|
@ -174,381 +50,3 @@ BOOST_AUTO_TEST_CASE( testConvertBytesVectorToVector ) {
|
|||
BOOST_CHECK(actual_bits == expected_bits);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( testConvertVectorToBytesVector ) {
|
||||
std::vector<unsigned char> expected_bytes = {0x00, 0x01, 0x03, 0x12, 0xFF};
|
||||
std::vector<bool> bits = {
|
||||
// 0x00
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
// 0x01
|
||||
0, 0, 0, 0, 0, 0, 0, 1,
|
||||
// 0x03
|
||||
0, 0, 0, 0, 0, 0, 1, 1,
|
||||
// 0x12
|
||||
0, 0, 0, 1, 0, 0, 1, 0,
|
||||
// 0xFF
|
||||
1, 1, 1, 1, 1, 1, 1, 1
|
||||
};
|
||||
// TODO: evaluate whether initializing with 5 should be necessary.
|
||||
std::vector<unsigned char> actual_bytes(5);
|
||||
libzerocash::convertVectorToBytesVector(bits, actual_bytes);
|
||||
BOOST_CHECK(actual_bytes == expected_bytes);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( testConvertIntToBytesVector ) {
|
||||
uint64_t val;
|
||||
std::vector<unsigned char> expected;
|
||||
std::vector<unsigned char> bytes(8);
|
||||
|
||||
val = 0ULL;
|
||||
expected = { 0, 0, 0, 0, 0, 0, 0, 0 };
|
||||
libzerocash::convertIntToBytesVector(val, bytes);
|
||||
BOOST_CHECK( expected == bytes );
|
||||
|
||||
val = 1ULL;
|
||||
expected = { 0, 0, 0, 0, 0, 0, 0, 1 };
|
||||
libzerocash::convertIntToBytesVector(val, bytes);
|
||||
BOOST_CHECK( expected == bytes );
|
||||
|
||||
val = 0xffffffffffffffffULL;
|
||||
expected = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
|
||||
libzerocash::convertIntToBytesVector(val, bytes);
|
||||
BOOST_CHECK( expected == bytes );
|
||||
|
||||
val = 0x8000000080000001ULL; // sign extension
|
||||
expected = { 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x01 };
|
||||
libzerocash::convertIntToBytesVector(val, bytes);
|
||||
BOOST_CHECK( expected == bytes );
|
||||
|
||||
// The following two tests aren't necessarily desired results. They merely
|
||||
// document the behavior so that we'll be alerted if it changes in the
|
||||
// future.
|
||||
|
||||
val = 0xffffffffdeadbeefULL; // truncation
|
||||
expected = { 0xde, 0xad, 0xbe, 0xef };
|
||||
std::vector<unsigned char> small_bytes(4);
|
||||
libzerocash::convertIntToBytesVector(val, small_bytes);
|
||||
BOOST_CHECK( expected == small_bytes );
|
||||
|
||||
val = 0xf1f2f3f401020304ULL; // bytes buffer is too big
|
||||
// The first byte is 4 because `>> 64` is undefined, and that's the result
|
||||
// it has on my system (note that it's the same as the original LSB).
|
||||
expected = { 0x04, 0xf1, 0xf2, 0xf3, 0xf4, 0x01, 0x02, 0x03, 0x04 };
|
||||
std::vector<unsigned char> big_bytes(9);
|
||||
libzerocash::convertIntToBytesVector(val, big_bytes);
|
||||
BOOST_CHECK( expected == big_bytes);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( testConvertBytesVectorToInt ) {
|
||||
uint64_t val;
|
||||
uint64_t expected;
|
||||
std::vector<unsigned char> bytes;
|
||||
|
||||
bytes = { 0, 0, 0, 0, 0, 0, 0, 0 };
|
||||
expected = 0ULL;
|
||||
val = libzerocash::convertBytesVectorToInt(bytes);
|
||||
BOOST_CHECK( expected == val );
|
||||
|
||||
bytes = { 0, 0, 0, 0, 0, 0, 0, 1 };
|
||||
expected = 1ULL;
|
||||
val = libzerocash::convertBytesVectorToInt(bytes);
|
||||
BOOST_CHECK( expected == val );
|
||||
|
||||
bytes = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
|
||||
expected = 0xffffffffffffffffULL;
|
||||
val = libzerocash::convertBytesVectorToInt(bytes);
|
||||
BOOST_CHECK( expected == val );
|
||||
|
||||
bytes = { 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x01 };
|
||||
expected = 0x8000000080000001ULL;
|
||||
val = libzerocash::convertBytesVectorToInt(bytes);
|
||||
BOOST_CHECK( expected == val );
|
||||
|
||||
bytes = { 0xde, 0xad, 0xbe, 0xef }; // opposite of truncation
|
||||
expected = 0xdeadbeefULL;
|
||||
val = libzerocash::convertBytesVectorToInt(bytes);
|
||||
BOOST_CHECK( expected == val );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( testConvertIntToVector ) {
|
||||
uint64_t val;
|
||||
std::vector<bool> expected;
|
||||
std::vector<bool> vector;
|
||||
|
||||
val = 0ULL;
|
||||
expected = { 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0 };
|
||||
libzerocash::convertIntToVector(val, vector);
|
||||
BOOST_CHECK( expected == vector );
|
||||
|
||||
val = 1ULL;
|
||||
expected = { 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 1 };
|
||||
libzerocash::convertIntToVector(val, vector);
|
||||
BOOST_CHECK( expected == vector );
|
||||
|
||||
val = 0xffffffffffffffffULL;
|
||||
expected = { 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1 };
|
||||
libzerocash::convertIntToVector(val, vector);
|
||||
BOOST_CHECK( expected == vector );
|
||||
|
||||
val = 0x8000000080000001ULL; // sign extension
|
||||
expected = { 1, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
1, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 1 };
|
||||
libzerocash::convertIntToVector(val, vector);
|
||||
BOOST_CHECK( expected == vector );
|
||||
|
||||
std::vector<bool> too_big(100);
|
||||
libzerocash::convertIntToVector(0, too_big);
|
||||
BOOST_CHECK(too_big.size() == 64);
|
||||
|
||||
std::vector<bool> too_small(10);
|
||||
libzerocash::convertIntToVector(0, too_small);
|
||||
BOOST_CHECK(too_big.size() == 64);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( testConcatenateTwoBoolVectors ) {
|
||||
std::vector<bool> front = { 0, 1, 0 };
|
||||
std::vector<bool> back = { 1, 0, 1 };
|
||||
std::vector<bool> expected = { 0, 1, 0, 1, 0, 1 };
|
||||
std::vector<bool> actual;
|
||||
libzerocash::concatenateVectors(front, back, actual);
|
||||
BOOST_CHECK( expected == actual );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( testConcatenateTwoByteVectors ) {
|
||||
std::vector<unsigned char> front = { 0, 1, 2 };
|
||||
std::vector<unsigned char> back = { 3, 4, 5 };
|
||||
std::vector<unsigned char> expected = { 0, 1, 2, 3, 4, 5 };
|
||||
std::vector<unsigned char> actual;
|
||||
libzerocash::concatenateVectors(front, back, actual);
|
||||
BOOST_CHECK( expected == actual );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( testConcatenateThreeBoolVectors ) {
|
||||
std::vector<bool> front = { 0, 1, 0 };
|
||||
std::vector<bool> middle { 1, 1, 1 };
|
||||
std::vector<bool> back = { 1, 0, 1 };
|
||||
std::vector<bool> expected = { 0, 1, 0, 1, 1, 1, 1, 0, 1 };
|
||||
std::vector<bool> actual;
|
||||
libzerocash::concatenateVectors(front, middle, back, actual);
|
||||
BOOST_CHECK( expected == actual );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( testConcatenateThreeByteVectors ) {
|
||||
std::vector<unsigned char> front = { 0, 1, 0 };
|
||||
std::vector<unsigned char> middle { 1, 1, 1 };
|
||||
std::vector<unsigned char> back = { 1, 0, 1 };
|
||||
std::vector<unsigned char> expected = { 0, 1, 0, 1, 1, 1, 1, 0, 1 };
|
||||
std::vector<unsigned char> actual;
|
||||
libzerocash::concatenateVectors(front, middle, back, actual);
|
||||
BOOST_CHECK( expected == actual );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( testSHA256ModifiedTestVectors ) {
|
||||
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];
|
||||
CSHA256 ctx256;
|
||||
libzerocash::sha256(ctx256, sha256_preimage, actual_hash, SHA256_PREIMAGE_BYTES);
|
||||
BOOST_CHECK( memcmp(sha256_hash, actual_hash, 32) == 0 );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( testSHA256TestVectors ) {
|
||||
/* Tests an actual SHA256 test vector (with length padding) to make sure
|
||||
* libzerocash's implementation is actually the same as SHA256 with the
|
||||
* length padding removed. */
|
||||
unsigned char preimage[3] = { 'a', 'b', 'c' };
|
||||
unsigned char expected_hash[32] = { 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01,
|
||||
0xcf, 0xea, 0x41, 0x41, 0x40, 0xde,
|
||||
0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03,
|
||||
0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
|
||||
0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00,
|
||||
0x15, 0xad };
|
||||
unsigned char actual_hash[32];
|
||||
CSHA256 ctx256;
|
||||
ctx256.Write(preimage, 3);
|
||||
ctx256.Finalize(actual_hash);
|
||||
|
||||
BOOST_CHECK( memcmp(expected_hash, actual_hash, 32) == 0 );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( testHashBoolVectorToBoolVectorCTX ) {
|
||||
CSHA256 ctx256;
|
||||
|
||||
std::vector<bool> preimage(SHA256_PREIMAGE_BYTES * 8);
|
||||
libzerocash::convertBytesToVector(sha256_preimage, preimage);
|
||||
|
||||
std::vector<bool> expected(32*8);
|
||||
libzerocash::convertBytesToVector(sha256_hash, expected);
|
||||
|
||||
// TODO: evaluate whether this should be a necessary precondition.
|
||||
std::vector<bool> actual(32*8);
|
||||
libzerocash::hashVector(ctx256, preimage, actual);
|
||||
|
||||
BOOST_CHECK( expected == actual );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( testHashByteVectorToByteVectorCTX ) {
|
||||
CSHA256 ctx256;
|
||||
|
||||
std::vector<unsigned char> preimage(SHA256_PREIMAGE_BYTES);
|
||||
libzerocash::convertBytesToBytesVector(sha256_preimage, preimage);
|
||||
|
||||
std::vector<unsigned char> expected(32);
|
||||
libzerocash::convertBytesToBytesVector(sha256_hash, expected);
|
||||
|
||||
// TODO: evaluate whether this should be a necessary precondition.
|
||||
std::vector<unsigned char> actual(32);
|
||||
libzerocash::hashVector(ctx256, preimage, actual);
|
||||
|
||||
BOOST_CHECK( expected == actual );
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE( testHashBoolVectorToBoolVector ) {
|
||||
std::vector<bool> preimage(SHA256_PREIMAGE_BYTES * 8);
|
||||
libzerocash::convertBytesToVector(sha256_preimage, preimage);
|
||||
|
||||
std::vector<bool> expected(32*8);
|
||||
libzerocash::convertBytesToVector(sha256_hash, expected);
|
||||
|
||||
// TODO: evaluate whether this should be a necessary precondition.
|
||||
std::vector<bool> actual(32*8);
|
||||
libzerocash::hashVector(preimage, actual);
|
||||
|
||||
BOOST_CHECK( expected == actual );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( testHashByteVectorToByteVector ) {
|
||||
std::vector<unsigned char> preimage(SHA256_PREIMAGE_BYTES);
|
||||
libzerocash::convertBytesToBytesVector(sha256_preimage, preimage);
|
||||
|
||||
std::vector<unsigned char> expected(32);
|
||||
libzerocash::convertBytesToBytesVector(sha256_hash, expected);
|
||||
|
||||
// TODO: evaluate whether this should be a necessary precondition.
|
||||
std::vector<unsigned char> actual(32);
|
||||
libzerocash::hashVector(preimage, actual);
|
||||
|
||||
BOOST_CHECK( expected == actual );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( testHashBoolVectorsCTX ) {
|
||||
CSHA256 ctx256;
|
||||
|
||||
std::vector<bool> preimage1(8);
|
||||
libzerocash::convertBytesToVector(sha256_preimage, preimage1);
|
||||
|
||||
std::vector<bool> preimage2((SHA256_PREIMAGE_BYTES - 1) * 8);
|
||||
libzerocash::convertBytesToVector(sha256_preimage + 1, preimage2);
|
||||
|
||||
std::vector<bool> expected(32*8);
|
||||
libzerocash::convertBytesToVector(sha256_hash, expected);
|
||||
|
||||
// TODO: evaluate whether this should be a necessary precondition.
|
||||
std::vector<bool> actual(32 * 8);
|
||||
libzerocash::hashVectors(ctx256, preimage1, preimage2, actual);
|
||||
|
||||
BOOST_CHECK( expected == actual );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( testHashByteVectorsCTX ) {
|
||||
CSHA256 ctx256;
|
||||
|
||||
std::vector<unsigned char> preimage1(1);
|
||||
libzerocash::convertBytesToBytesVector(sha256_preimage, preimage1);
|
||||
|
||||
std::vector<unsigned char> preimage2(SHA256_PREIMAGE_BYTES - 1);
|
||||
libzerocash::convertBytesToBytesVector(sha256_preimage + 1, preimage2);
|
||||
|
||||
std::vector<unsigned char> expected(32);
|
||||
libzerocash::convertBytesToBytesVector(sha256_hash, expected);
|
||||
|
||||
// TODO: evaluate whether this should be a necessary precondition.
|
||||
std::vector<unsigned char> actual(32);
|
||||
libzerocash::hashVectors(ctx256, preimage1, preimage2, actual);
|
||||
|
||||
BOOST_CHECK( expected == actual );
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE( testHashBoolVectors ) {
|
||||
std::vector<bool> preimage1(8);
|
||||
libzerocash::convertBytesToVector(sha256_preimage, preimage1);
|
||||
|
||||
std::vector<bool> preimage2((SHA256_PREIMAGE_BYTES - 1) * 8);
|
||||
libzerocash::convertBytesToVector(sha256_preimage + 1, preimage2);
|
||||
|
||||
std::vector<bool> expected(32*8);
|
||||
libzerocash::convertBytesToVector(sha256_hash, expected);
|
||||
|
||||
// TODO: evaluate whether this should be a necessary precondition.
|
||||
std::vector<bool> actual(32 * 8);
|
||||
libzerocash::hashVectors(preimage1, preimage2, actual);
|
||||
|
||||
BOOST_CHECK( expected == actual );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( testHashByteVectors ) {
|
||||
std::vector<unsigned char> preimage1(1);
|
||||
libzerocash::convertBytesToBytesVector(sha256_preimage, preimage1);
|
||||
|
||||
std::vector<unsigned char> preimage2(SHA256_PREIMAGE_BYTES - 1);
|
||||
libzerocash::convertBytesToBytesVector(sha256_preimage + 1, preimage2);
|
||||
|
||||
std::vector<unsigned char> expected(32);
|
||||
libzerocash::convertBytesToBytesVector(sha256_hash, expected);
|
||||
|
||||
// TODO: evaluate whether this should be a necessary precondition.
|
||||
std::vector<unsigned char> actual(32);
|
||||
libzerocash::hashVectors(preimage1, preimage2, actual);
|
||||
|
||||
BOOST_CHECK( expected == actual );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( testVectorIsZero ) {
|
||||
std::vector<bool> bits;
|
||||
BOOST_CHECK( libzerocash::VectorIsZero(bits) );
|
||||
|
||||
bits = { 0 };
|
||||
BOOST_CHECK( libzerocash::VectorIsZero(bits) );
|
||||
|
||||
bits = { 0, 0 };
|
||||
BOOST_CHECK( libzerocash::VectorIsZero(bits) );
|
||||
|
||||
bits = { 1 };
|
||||
BOOST_CHECK( !libzerocash::VectorIsZero(bits) );
|
||||
|
||||
bits = { 0, 1 };
|
||||
BOOST_CHECK( !libzerocash::VectorIsZero(bits) );
|
||||
}
|
||||
|
||||
|
|
|
@ -12,142 +12,14 @@
|
|||
|
||||
namespace libzerocash {
|
||||
|
||||
void printChar(const unsigned char c) {
|
||||
for(int j = 8; j >= 0; j--) {
|
||||
std::cout << ((c >> j) & 1);
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
void printVector(const std::vector<bool>& v) {
|
||||
std::cout << v.size() << " MSB ";
|
||||
for(size_t i = 0; i < v.size(); i++) {
|
||||
std::cout << v.at(i);
|
||||
}
|
||||
std::cout << " LSB" << std::endl;
|
||||
}
|
||||
|
||||
void printVector(const std::string str, const std::vector<bool>& v) {
|
||||
std::cout << str << " " << v.size() << " MSB ";
|
||||
for(size_t i = 0; i < v.size(); i++) {
|
||||
std::cout << v.at(i);
|
||||
}
|
||||
std::cout << " LSB" << std::endl;
|
||||
}
|
||||
|
||||
void printVectorAsHex(const std::vector<bool>& v) {
|
||||
unsigned char bytes[int(v.size() / 8)];
|
||||
convertVectorToBytes(v, bytes);
|
||||
|
||||
for(int i = 0; i < int(v.size() / 8); i++) {
|
||||
std::cout << std::setw(2) << std::setfill('0') << std::hex << (int) bytes[i];
|
||||
}
|
||||
std::cout << std::dec << std::endl;
|
||||
}
|
||||
|
||||
void printVectorAsHex(const std::string str, const std::vector<bool>& v) {
|
||||
unsigned char bytes[int(v.size() / 8)];
|
||||
convertVectorToBytes(v, bytes);
|
||||
|
||||
std::cout << str << " ";
|
||||
for(int i = 0; i < int(v.size() / 8); i++) {
|
||||
std::cout << std::setw(2) << std::setfill('0') << std::hex << (int) bytes[i];
|
||||
}
|
||||
std::cout << std::dec << std::endl;
|
||||
}
|
||||
|
||||
void printBytesVector(const std::vector<unsigned char>& v) {
|
||||
std::vector<bool> boolVec(v.size() * 8);
|
||||
convertBytesVectorToVector(v, boolVec);
|
||||
printVector(boolVec);
|
||||
}
|
||||
|
||||
void printBytesVector(const std::string str, const std::vector<unsigned char>& v) {
|
||||
std::vector<bool> boolVec(v.size() * 8);
|
||||
convertBytesVectorToVector(v, boolVec);
|
||||
printVector(str, boolVec);
|
||||
}
|
||||
|
||||
void printBytesVectorAsHex(const std::vector<unsigned char>& v) {
|
||||
std::vector<bool> boolVec(v.size() * 8);
|
||||
convertBytesVectorToVector(v, boolVec);
|
||||
printVectorAsHex(boolVec);
|
||||
}
|
||||
|
||||
void printBytesVectorAsHex(const std::string str, const std::vector<unsigned char>& v) {
|
||||
std::vector<bool> boolVec(v.size() * 8);
|
||||
convertBytesVectorToVector(v, boolVec);
|
||||
printVectorAsHex(str, boolVec);
|
||||
}
|
||||
|
||||
void getRandBytes(unsigned char* bytes, int num) {
|
||||
randombytes_buf(bytes, num);
|
||||
}
|
||||
|
||||
void convertBytesToVector(const unsigned char* bytes, std::vector<bool>& v) {
|
||||
int numBytes = v.size() / 8;
|
||||
unsigned char c;
|
||||
for(int i = 0; i < numBytes; i++) {
|
||||
c = bytes[i];
|
||||
|
||||
for(int j = 0; j < 8; j++) {
|
||||
v.at((i*8)+j) = ((c >> (7-j)) & 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void convertVectorToBytes(const std::vector<bool>& v, unsigned char* bytes) {
|
||||
int numBytes = v.size() / 8;
|
||||
unsigned char c = '\0';
|
||||
|
||||
for(int i = 0; i < numBytes; i++) {
|
||||
c = '\0';
|
||||
for(int j = 0; j < 8; j++) {
|
||||
if(j == 7)
|
||||
c = ((c | v.at((i*8)+j)));
|
||||
else
|
||||
c = ((c | v.at((i*8)+j)) << 1);
|
||||
}
|
||||
bytes[i] = c;
|
||||
}
|
||||
}
|
||||
|
||||
void convertBytesToBytesVector(const unsigned char* bytes, std::vector<unsigned char>& v) {
|
||||
for(size_t i = 0; i < v.size(); i++) {
|
||||
v.at(i) = bytes[i];
|
||||
}
|
||||
}
|
||||
|
||||
void convertBytesVectorToBytes(const std::vector<unsigned char>& v, unsigned char* bytes) {
|
||||
for(size_t i = 0; i < v.size(); i++) {
|
||||
bytes[i] = v.at(i);
|
||||
}
|
||||
}
|
||||
|
||||
void convertBytesVectorToVector(const std::vector<unsigned char>& bytes, std::vector<bool>& v) {
|
||||
v.resize(bytes.size() * 8);
|
||||
unsigned char bytesArr[bytes.size()];
|
||||
convertBytesVectorToBytes(bytes, bytesArr);
|
||||
convertBytesToVector(bytesArr, v);
|
||||
}
|
||||
|
||||
void convertVectorToBytesVector(const std::vector<bool>& v, std::vector<unsigned char>& bytes) {
|
||||
unsigned char bytesArr[int(ceil(v.size() / 8.))];
|
||||
convertVectorToBytes(v, bytesArr);
|
||||
convertBytesToBytesVector(bytesArr, bytes);
|
||||
}
|
||||
|
||||
void convertIntToBytesVector(const uint64_t val_int, std::vector<unsigned char>& bytes) {
|
||||
for(size_t i = 0; i < bytes.size(); i++) {
|
||||
bytes[bytes.size()-1-i] = (val_int >> (i * 8));
|
||||
}
|
||||
}
|
||||
|
||||
void convertIntToVector(uint64_t val, std::vector<bool>& v)
|
||||
{
|
||||
v.resize(64);
|
||||
for(unsigned int i = 0; i < 64; ++i, val >>= 1) {
|
||||
v.at(63 - i) = val & 0x01;
|
||||
v.resize(bytes.size() * 8);
|
||||
unsigned char c;
|
||||
for (size_t i = 0; i < bytes.size(); i++) {
|
||||
c = bytes.at(i);
|
||||
for (size_t j = 0; j < 8; j++) {
|
||||
v.at((i*8)+j) = (c >> (7-j)) & 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -166,176 +38,5 @@ uint64_t convertVectorToInt(const std::vector<bool>& v) {
|
|||
return result;
|
||||
}
|
||||
|
||||
uint64_t convertBytesVectorToInt(const std::vector<unsigned char>& bytes) {
|
||||
uint64_t val_int = 0;
|
||||
|
||||
for(size_t i = 0; i < bytes.size(); i++) {
|
||||
val_int = val_int + (((uint64_t)bytes[i]) << ((bytes.size()-1-i) * 8));
|
||||
}
|
||||
|
||||
return val_int;
|
||||
}
|
||||
|
||||
void concatenateVectors(const std::vector<bool>& A, const std::vector<bool>& B, std::vector<bool>& result) {
|
||||
result.reserve(A.size() + B.size());
|
||||
result.insert(result.end(), A.begin(), A.end());
|
||||
result.insert(result.end(), B.begin(), B.end());
|
||||
}
|
||||
|
||||
void concatenateVectors(const std::vector<unsigned char>& A, const std::vector<unsigned char>& B, std::vector<unsigned char>& result) {
|
||||
result.reserve(A.size() + B.size());
|
||||
result.insert(result.end(), A.begin(), A.end());
|
||||
result.insert(result.end(), B.begin(), B.end());
|
||||
}
|
||||
|
||||
void concatenateVectors(const std::vector<bool>& A, const std::vector<bool>& B, const std::vector<bool>& C, std::vector<bool>& result) {
|
||||
result.reserve(A.size() + B.size() + C.size());
|
||||
result.insert(result.end(), A.begin(), A.end());
|
||||
result.insert(result.end(), B.begin(), B.end());
|
||||
result.insert(result.end(), C.begin(), C.end());
|
||||
}
|
||||
|
||||
void concatenateVectors(const std::vector<unsigned char>& A, const std::vector<unsigned char>& B, const std::vector<unsigned char>& C, std::vector<unsigned char>& result) {
|
||||
result.reserve(A.size() + B.size() + C.size());
|
||||
result.insert(result.end(), A.begin(), A.end());
|
||||
result.insert(result.end(), B.begin(), B.end());
|
||||
result.insert(result.end(), C.begin(), C.end());
|
||||
}
|
||||
|
||||
void sha256(const unsigned char* input, unsigned char* hash, int len) {
|
||||
CSHA256 hasher;
|
||||
hasher.Write(input, len);
|
||||
hasher.FinalizeNoPadding(hash);
|
||||
}
|
||||
|
||||
void sha256(CSHA256& hasher, const unsigned char* input, unsigned char* hash, int len) {
|
||||
// Ignore the one that's passed in; this optimization is useless
|
||||
// for all of our code, and this code is all being removed anyway.
|
||||
CSHA256 new_hasher;
|
||||
new_hasher.Write(input, len);
|
||||
new_hasher.FinalizeNoPadding(hash);
|
||||
}
|
||||
|
||||
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[CSHA256::OUTPUT_SIZE];
|
||||
sha256(hasher, bytes, hash, (int)size);
|
||||
|
||||
convertBytesToVector(hash, 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[CSHA256::OUTPUT_SIZE];
|
||||
sha256(hasher, bytes, hash, (int)size);
|
||||
|
||||
convertBytesToBytesVector(hash, output);
|
||||
}
|
||||
|
||||
void hashVector(const std::vector<bool> input, std::vector<bool>& output) {
|
||||
CSHA256 hasher;
|
||||
|
||||
int size = int(input.size() / 8);
|
||||
unsigned char bytes[size];
|
||||
convertVectorToBytes(input, bytes);
|
||||
|
||||
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) {
|
||||
CSHA256 hasher;
|
||||
|
||||
int size = int(input.size());
|
||||
unsigned char bytes[size];
|
||||
convertBytesVectorToBytes(input, bytes);
|
||||
|
||||
unsigned char hash[CSHA256::OUTPUT_SIZE];
|
||||
sha256(hasher, bytes, hash, (int)size);
|
||||
|
||||
convertBytesToBytesVector(hash, 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);
|
||||
|
||||
int size = int(concat.size() / 8);
|
||||
unsigned char bytes[size];
|
||||
convertVectorToBytes(concat, bytes);
|
||||
|
||||
unsigned char hash[CSHA256::OUTPUT_SIZE];
|
||||
sha256(hasher, bytes, hash, (int)size);
|
||||
|
||||
convertBytesToVector(hash, 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);
|
||||
|
||||
int size = int(concat.size());
|
||||
unsigned char bytes[size];
|
||||
convertBytesVectorToBytes(concat, bytes);
|
||||
|
||||
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::vector<bool> concat;
|
||||
concatenateVectors(left, right, concat);
|
||||
|
||||
int size = int(concat.size() / 8);
|
||||
unsigned char bytes[size];
|
||||
convertVectorToBytes(concat, bytes);
|
||||
|
||||
unsigned char hash[CSHA256::OUTPUT_SIZE];
|
||||
sha256(bytes, hash, (int)size);
|
||||
|
||||
convertBytesToVector(hash, output);
|
||||
}
|
||||
|
||||
void hashVectors(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);
|
||||
|
||||
int size = int(concat.size());
|
||||
unsigned char bytes[size];
|
||||
convertBytesVectorToBytes(concat, bytes);
|
||||
|
||||
unsigned char hash[CSHA256::OUTPUT_SIZE];
|
||||
sha256(bytes, hash, (int)size);
|
||||
|
||||
convertBytesToBytesVector(hash, output);
|
||||
}
|
||||
|
||||
bool VectorIsZero(const std::vector<bool> test) {
|
||||
// XXX: not time safe
|
||||
return (test.end() == std::find(test.begin(), test.end(), true));
|
||||
}
|
||||
|
||||
size_t countOnes(const std::vector<bool>& vec) {
|
||||
return count(vec.begin(), vec.end(), true);
|
||||
}
|
||||
|
||||
std::vector<unsigned char> vectorSlice(const std::vector<unsigned char>& vec, size_t start, size_t length) {
|
||||
std::vector<unsigned char> slice(length);
|
||||
for (size_t i = 0; i < length; i++) {
|
||||
slice.at(i) = vec.at(start + i);
|
||||
}
|
||||
return slice;
|
||||
}
|
||||
|
||||
} /* namespace libzerocash */
|
||||
|
||||
|
|
|
@ -10,80 +10,10 @@
|
|||
|
||||
namespace libzerocash {
|
||||
|
||||
void printChar(const unsigned char c);
|
||||
|
||||
void printVector(const std::vector<bool>& v);
|
||||
|
||||
void printVector(const std::string str, const std::vector<bool>& v);
|
||||
|
||||
void printVectorAsHex(const std::vector<bool>& v);
|
||||
|
||||
void printVectorAsHex(const std::string str, const std::vector<bool>& v);
|
||||
|
||||
void printBytesVector(const std::vector<unsigned char>& v);
|
||||
|
||||
void printBytesVector(const std::string str, const std::vector<unsigned char>& v);
|
||||
|
||||
void printBytesVectorAsHex(const std::vector<unsigned char>& v);
|
||||
|
||||
void printBytesVectorAsHex(const std::string str, const std::vector<unsigned char>& v);
|
||||
|
||||
void getRandBytes(unsigned char* bytes, int num);
|
||||
|
||||
void convertBytesToVector(const unsigned char* bytes, std::vector<bool>& v);
|
||||
|
||||
void convertVectorToBytes(const std::vector<bool>& v, unsigned char* bytes);
|
||||
|
||||
void convertBytesToBytesVector(const unsigned char* bytes, std::vector<unsigned char>& v);
|
||||
|
||||
void convertBytesVectorToBytes(const std::vector<unsigned char>& v, unsigned char* bytes);
|
||||
|
||||
void convertBytesVectorToVector(const std::vector<unsigned char>& bytes, std::vector<bool>& v);
|
||||
|
||||
void convertVectorToBytesVector(const std::vector<bool>& v, std::vector<unsigned char>& bytes);
|
||||
|
||||
void convertIntToBytesVector(const uint64_t val_int, std::vector<unsigned char>& bytes);
|
||||
|
||||
void convertIntToVector(uint64_t val, std::vector<bool>& v);
|
||||
|
||||
uint64_t convertVectorToInt(const std::vector<bool>& v);
|
||||
|
||||
uint64_t convertBytesVectorToInt(const std::vector<unsigned char>& bytes);
|
||||
|
||||
void concatenateVectors(const std::vector<bool>& A, const std::vector<bool>& B, std::vector<bool>& result);
|
||||
|
||||
void concatenateVectors(const std::vector<unsigned char>& A, const std::vector<unsigned char>& B, std::vector<unsigned char>& result);
|
||||
|
||||
void concatenateVectors(const std::vector<bool>& A, const std::vector<bool>& B, const std::vector<bool>& C, std::vector<bool>& result);
|
||||
|
||||
void concatenateVectors(const std::vector<unsigned char>& A, const std::vector<unsigned char>& B, const std::vector<unsigned char>& C, std::vector<unsigned char>& result);
|
||||
|
||||
void sha256(const unsigned char* input, unsigned char* hash, int len);
|
||||
|
||||
void sha256(CSHA256& ctx256, const unsigned char* input, unsigned char* hash, int len);
|
||||
|
||||
void hashVector(CSHA256& ctx256, const std::vector<bool> input, std::vector<bool>& 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(CSHA256& ctx256, const std::vector<bool> left, const std::vector<bool> right, std::vector<bool>& 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);
|
||||
|
||||
void hashVectors(const std::vector<unsigned char> left, const std::vector<unsigned char> right, std::vector<unsigned char>& output);
|
||||
|
||||
bool VectorIsZero(const std::vector<bool> test);
|
||||
|
||||
size_t countOnes(const std::vector<bool>& vec);
|
||||
|
||||
std::vector<unsigned char> vectorSlice(const std::vector<unsigned char>& vec, size_t start, size_t length);
|
||||
|
||||
} /* namespace libzerocash */
|
||||
#endif /* UTIL_H_ */
|
||||
|
||||
|
|
Loading…
Reference in New Issue