From 3bb9fcc71eaeac41130074acfd59c3c1f872d85d Mon Sep 17 00:00:00 2001 From: carsenk Date: Sat, 18 Jan 2020 17:05:26 -0700 Subject: [PATCH] Fixes for Tx Null Data, Segfault Launch Fix, Ring Sig Tests --- src/main.h | 4 +- src/test/ringsig_tests.cpp | 178 +++++++++++++++++++++++++++++++++++++ src/txdb-leveldb.cpp | 2 +- src/wallet.cpp | 2 +- 4 files changed, 182 insertions(+), 4 deletions(-) create mode 100644 src/test/ringsig_tests.cpp diff --git a/src/main.h b/src/main.h index 5149275b..bf757dfb 100644 --- a/src/main.h +++ b/src/main.h @@ -69,9 +69,9 @@ static const unsigned int MAX_BLOCK_SIGOPS = MAX_BLOCK_SIZE/50; static const unsigned int MAX_TX_SIGOPS = MAX_BLOCK_SIGOPS/5; //static const unsigned int MAX_ORPHAN_TRANSACTIONS = MAX_BLOCK_SIZE/100; deprecated /** Default for -maxorphantx, maximum number of orphan transactions kept in memory */ -static const unsigned int DEFAULT_MAX_ORPHAN_TRANSACTIONS = 10000; //Was 100, lets handle 10k +static const unsigned int DEFAULT_MAX_ORPHAN_TRANSACTIONS = 100; //Was 10k, lets handle 100 /** Default for -maxorphanblocks, maximum number of orphan blocks kept in memory */ -static const unsigned int DEFAULT_MAX_ORPHAN_BLOCKS = 1000; //Default 750, Lets handle 1000 +static const unsigned int DEFAULT_MAX_ORPHAN_BLOCKS = 750; //Default 750, Lets handle 1000 maybe? static const unsigned int MAX_INV_SZ = 50000; static const int64_t MIN_TX_FEE = 1000; static const int64_t MIN_TX_FEE_ANON = 10000; diff --git a/src/test/ringsig_tests.cpp b/src/test/ringsig_tests.cpp new file mode 100644 index 00000000..21790c2f --- /dev/null +++ b/src/test/ringsig_tests.cpp @@ -0,0 +1,178 @@ +#include + +#include + +#include +#include +#include +#include +#include +#include + +#include + +#include "ringsig.h" +#include "main.h" + +using namespace boost::chrono; + +// test_shadow --log_level=all --run_test=ringsig_tests + +clock_t totalGenerate; +clock_t totalVerify; +clock_t start, stop; + +void testRingSigs(int nRingSize) +{ + uint8_t *pPubkeys = (uint8_t*) malloc(sizeof(uint8_t) * EC_COMPRESSED_SIZE * nRingSize); + uint8_t *pSigc = (uint8_t*) malloc(sizeof(uint8_t) * EC_SECRET_SIZE * nRingSize); + uint8_t *pSigr = (uint8_t*) malloc(sizeof(uint8_t) * EC_SECRET_SIZE * nRingSize); + + BOOST_REQUIRE(NULL != pPubkeys); + BOOST_REQUIRE(NULL != pSigc); + BOOST_REQUIRE(NULL != pSigr); + + CKey key[nRingSize]; + for (int i = 0; i < nRingSize; ++i) + { + key[i].MakeNewKey(true); + + CPubKey pk = key[i].GetPubKey(); + + memcpy(&pPubkeys[i * EC_COMPRESSED_SIZE], pk.begin(), EC_COMPRESSED_SIZE); + }; + + uint256 preimage; + BOOST_CHECK(1 == RAND_bytes((uint8_t*) preimage.begin(), 32)); + //BOOST_MESSAGE("Txn preimage: " << HexStr(preimage)); + + //BOOST_MESSAGE("nRingSize: " << nRingSize); + int iSender = GetRandInt(nRingSize); + //BOOST_MESSAGE("sender: " << iSender); + + ec_secret sSpend; + ec_point pkSpend; + ec_point keyImage; + + memcpy(&sSpend.e[0], key[iSender].begin(), EC_SECRET_SIZE); + + BOOST_REQUIRE(0 == SecretToPublicKey(sSpend, pkSpend)); + + BOOST_REQUIRE(0 == generateKeyImage(pkSpend, sSpend, keyImage)); + + start = clock(); + BOOST_REQUIRE(0 == generateRingSignature(keyImage, preimage, nRingSize, iSender, sSpend, pPubkeys, pSigc, pSigr)); + stop = clock(); + totalGenerate += stop - start; + + start = clock(); + BOOST_REQUIRE(0 == verifyRingSignature(keyImage, preimage, nRingSize, pPubkeys, pSigc, pSigr)); + stop = clock(); + totalVerify += stop - start; + + int sigSize = EC_COMPRESSED_SIZE + EC_SECRET_SIZE + (EC_SECRET_SIZE + EC_SECRET_SIZE + EC_COMPRESSED_SIZE) * nRingSize; + + BOOST_MESSAGE("nRingSize " << nRingSize << ", sigSize: " << bytesReadable(sigSize)); + + if (pPubkeys) + free(pPubkeys); + if (pSigc) + free(pSigc); + if (pSigr) + free(pSigr); +}; + +void testRingSigABs(int nRingSize) +{ + uint8_t *pPubkeys = (uint8_t*) malloc(sizeof(uint8_t) * EC_COMPRESSED_SIZE * nRingSize); + uint8_t *pSigS = (uint8_t*) malloc(sizeof(uint8_t) * EC_SECRET_SIZE * nRingSize); + + BOOST_CHECK(NULL != pPubkeys); + BOOST_CHECK(NULL != pSigS); + + CKey key[nRingSize]; + for (int i = 0; i < nRingSize; ++i) + { + key[i].MakeNewKey(true); + + CPubKey pk = key[i].GetPubKey(); + + memcpy(&pPubkeys[i * EC_COMPRESSED_SIZE], pk.begin(), EC_COMPRESSED_SIZE); + }; + + uint256 preimage; + BOOST_CHECK(1 == RAND_bytes((uint8_t*) preimage.begin(), 32)); + //BOOST_MESSAGE("Txn preimage: " << HexStr(preimage)); + + int iSender = GetRandInt(nRingSize); + //BOOST_MESSAGE("sender: " << iSender); + + ec_point pSigC; + + ec_secret sSpend; + ec_point pkSpend; + ec_point keyImage; + + memcpy(&sSpend.e[0], key[iSender].begin(), EC_SECRET_SIZE); + + BOOST_CHECK(0 == SecretToPublicKey(sSpend, pkSpend)); + + BOOST_REQUIRE(0 == generateKeyImage(pkSpend, sSpend, keyImage)); + + start = clock(); + BOOST_REQUIRE(0 == generateRingSignatureAB(keyImage, preimage, nRingSize, iSender, sSpend, pPubkeys, pSigC, pSigS)); + stop = clock(); + totalGenerate += stop - start; + + start = clock(); + BOOST_REQUIRE(0 == verifyRingSignatureAB(keyImage, preimage, nRingSize, pPubkeys, pSigC, pSigS)); + stop = clock(); + totalVerify += stop - start; + + int sigSize = EC_COMPRESSED_SIZE + EC_SECRET_SIZE + EC_SECRET_SIZE + (EC_SECRET_SIZE + EC_COMPRESSED_SIZE) * nRingSize; + + BOOST_MESSAGE("nRingSize " << nRingSize << ", sigSize: " << bytesReadable(sigSize)); + + if (pPubkeys) + free(pPubkeys); + if (pSigS) + free(pSigS); + +}; + +BOOST_AUTO_TEST_SUITE(ringsig_tests) + +BOOST_AUTO_TEST_CASE(ringsig) +{ + BOOST_REQUIRE(0 == initialiseRingSigs()); + + BOOST_MESSAGE("testRingSigs"); + + for (int k = 1; k < 4; ++k) + { + //BOOST_MESSAGE("ringSize " << (k % 126 + 2)); + testRingSigs(k % 126 + 2); + }; + //testRingSigs(16); + + BOOST_MESSAGE("totalGenerate " << (double(totalGenerate) / CLOCKS_PER_SEC)); + BOOST_MESSAGE("totalVerify " << (double(totalVerify) / CLOCKS_PER_SEC)); + + totalGenerate = 0; + totalVerify = 0; + BOOST_MESSAGE("testRingSigABs"); + + for (int k = 0; k < 32; ++k) + { + //BOOST_MESSAGE("ringSize " << (k % 126 + 2)); + //testRingSigABs(k % 126 + 2); + }; + //testRingSigABs(16); + + BOOST_MESSAGE("totalGenerate " << (double(totalGenerate) / CLOCKS_PER_SEC)); + BOOST_MESSAGE("totalVerify " << (double(totalVerify) / CLOCKS_PER_SEC)); + + BOOST_CHECK(0 == finaliseRingSigs()); +} + +BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/src/txdb-leveldb.cpp b/src/txdb-leveldb.cpp index c365bf94..53ea38bf 100644 --- a/src/txdb-leveldb.cpp +++ b/src/txdb-leveldb.cpp @@ -79,7 +79,7 @@ CTxDB::CTxDB(const char* pszMode) bool fCreate = strchr(pszMode, 'c'); options = GetOptions(); - options.create_if_missing = fCreate; + options.create_if_missing = true; //fCreate options.filter_policy = leveldb::NewBloomFilterPolicy(10); init_blockindex(options); // Init directory diff --git a/src/wallet.cpp b/src/wallet.cpp index f630c1ae..a9eba783 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -980,7 +980,7 @@ bool CWallet::IsChange(const CTxOut& txout) const if (::IsMine(*this, txout.scriptPubKey)) { CTxDestination address; - if (!ExtractDestination(txout.scriptPubKey, address)) + if (!ExtractDestination(txout.scriptPubKey, address) && txout.scriptPubKey[0] != OP_RETURN) //Fix Null TX Data return true; LOCK(cs_wallet);