Skip to content

Commit

Permalink
Fixed the performance issue (#523)
Browse files Browse the repository at this point in the history
* Fixed the performance issue
  • Loading branch information
a-bezrukov authored Jul 17, 2019
1 parent 5e0b843 commit 063337d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
9 changes: 2 additions & 7 deletions src/coin_containers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,10 @@ std::size_t CScalarHash::operator ()(const Scalar& bn) const noexcept {
}

std::size_t CPublicCoinHash::operator ()(const sigma::PublicCoin& coin) const noexcept {
vector<unsigned char> bnData(coin.value.memoryRequired());
coin.value.serialize(&bnData[0]);
uint256 hash = coin.getValueHash();

unsigned char hash[CSHA256::OUTPUT_SIZE];
CSHA256().Write(&bnData[0], bnData.size()).Finalize(hash);

// take the first bytes of "hash".
std::size_t result;
std::memcpy(&result, hash, sizeof(std::size_t));
std::memcpy(&result, hash.begin(), sizeof(std::size_t));
return result;
}

Expand Down
8 changes: 8 additions & 0 deletions src/sigma/coin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <openssl/rand.h>
#include <sstream>
#include "openssl_context.h"
#include "primitives/zerocoin.h"

namespace sigma {

Expand Down Expand Up @@ -173,6 +174,13 @@ const GroupElement& PublicCoin::getValue() const{
return this->value;
}

uint256 const & PublicCoin::getValueHash() const {
if(valueHash.IsNull())
valueHash = primitives::GetPubCoinValueHash(value);
return valueHash;
}


CoinDenomination PublicCoin::getDenomination() const {
return denomination;
}
Expand Down
4 changes: 3 additions & 1 deletion src/sigma/coin.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class PublicCoin {

const GroupElement& getValue() const;
CoinDenomination getDenomination() const;
uint256 const & getValueHash() const;

bool operator==(const PublicCoin& other) const;
bool operator!=(const PublicCoin& other) const;
Expand All @@ -70,9 +71,10 @@ class PublicCoin {
std::memcpy(&denomination, buffer + size, sizeof(denomination));
}

// private: TODO(martun): change back to private
private:
GroupElement value;
CoinDenomination denomination;
mutable uint256 valueHash;
};

class PrivateCoin {
Expand Down
8 changes: 4 additions & 4 deletions src/test/sigma_state_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ BOOST_AUTO_TEST_CASE(sigma_addspend)

auto coinSerial = coin.getCoinSerialNumber();
auto initSize = sigmaState->GetSpends().count(coinSerial);
sigmaState->AddSpend(coinSerial, pubcoin.denomination, 0);
sigmaState->AddSpend(coinSerial, pubcoin.getDenomination(), 0);
auto actSize = sigmaState->GetSpends().count(coinSerial);

BOOST_CHECK_MESSAGE(initSize + 1 == actSize, "Serial was not added to usedCoinSerials.");
Expand Down Expand Up @@ -351,7 +351,7 @@ BOOST_AUTO_TEST_CASE(sigma_addspend_to_mempool_coin_used)

auto coinSerial = coin.getCoinSerialNumber();

sigmaState->AddSpend(coinSerial, pubcoin.denomination, 0);
sigmaState->AddSpend(coinSerial, pubcoin.getDenomination(), 0);
BOOST_CHECK_MESSAGE(sigmaState->GetMempoolCoinSerials().size() == 0,
"Unexpected mempoolCoinSerials size before call AddSpendToMempool.");

Expand Down Expand Up @@ -471,7 +471,7 @@ BOOST_AUTO_TEST_CASE(sigma_canaddspendtomempool_used)

auto coinSerial = coin.getCoinSerialNumber();

sigmaState->AddSpend(coinSerial, pubcoin.denomination, 0);
sigmaState->AddSpend(coinSerial, pubcoin.getDenomination(), 0);

BOOST_CHECK_MESSAGE(!sigmaState->CanAddSpendToMempool(coinSerial),
"CanAddSpendToMempool return true, which means coin not in use, but should be.");
Expand Down Expand Up @@ -516,7 +516,7 @@ BOOST_AUTO_TEST_CASE(sigma_reset)
BOOST_CHECK_MESSAGE(sigmaState->GetMempoolCoinSerials().size() == 1,
"Unexpected mempoolCoinSerials size before reset.");

sigmaState->AddSpend(coinSerial, pubcoin.denomination, 0);
sigmaState->AddSpend(coinSerial, pubcoin.getDenomination(), 0);

BOOST_CHECK_MESSAGE(sigmaState->GetSpends().size() == 1,
"Unexpected usedCoinSerials size before reset.");
Expand Down
8 changes: 4 additions & 4 deletions src/zerocoin_v3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ bool GetOutPoint(COutPoint& outPoint, const sigma::PublicCoin &pubCoin) {
if(!ReadBlockFromDisk(block, mintBlock, ::Params().GetConsensus()))
LogPrintf("can't read block from disk.\n");

return GetOutPointFromBlock(outPoint, pubCoin.value, block);
return GetOutPointFromBlock(outPoint, pubCoin.getValue(), block);
}

bool GetOutPoint(COutPoint& outPoint, const GroupElement &pubCoinValue) {
Expand Down Expand Up @@ -986,9 +986,9 @@ bool CSigmaState::HasCoin(const sigma::PublicCoin& pubCoin) {

bool CSigmaState::HasCoinHash(GroupElement &pubCoinValue, const uint256 &pubCoinValueHash) {
for ( auto it = GetMints().begin(); it != GetMints().end(); ++it ){
const sigma::PublicCoin pubCoin = (*it).first;
if(primitives::GetPubCoinValueHash(pubCoin.value)==pubCoinValueHash){
pubCoinValue = pubCoin.value;
const sigma::PublicCoin & pubCoin = (*it).first;
if(pubCoin.getValueHash()==pubCoinValueHash){
pubCoinValue = pubCoin.getValue();
return true;
}
}
Expand Down

0 comments on commit 063337d

Please sign in to comment.