Skip to content

Commit

Permalink
Fixing performance issue by removing uneeded GroupElement conversions… (
Browse files Browse the repository at this point in the history
#1056)

* Fixing performance issue by removing uneeded GroupElement conversions and hashing by std::hash()
  • Loading branch information
levonpetrosyan93 committed Jul 21, 2021
1 parent e1c5ec2 commit 7960de9
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 15 deletions.
6 changes: 6 additions & 0 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,12 @@ class CMainParams : public CChainParams {
consensus.lelantusBlacklist.insert(coin);
}

for (const auto& str : sigma::sigma_blacklist) {
GroupElement coin;
coin.deserialize(ParseHex(str).data());
consensus.sigmaBlacklist.insert(coin);
}

consensus.evoSporkKeyID = "a78fERshquPsTv2TuKMSsxTeKom56uBwLP";
consensus.nEvoSporkStartBlock = ZC_LELANTUS_STARTING_BLOCK;
consensus.nEvoSporkStopBlock = ZC_LELANTUS_STARTING_BLOCK + 24*12*365; // one year after lelantus
Expand Down
3 changes: 3 additions & 0 deletions src/consensus/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ struct Params {
// Lelantus Blacklist
std::unordered_set<secp_primitives::GroupElement> lelantusBlacklist;

// Sigma Blacklist
std::unordered_set<secp_primitives::GroupElement> sigmaBlacklist;

// The block number introducing evo sporks
int nEvoSporkStartBlock;

Expand Down
4 changes: 1 addition & 3 deletions src/lelantus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include "policy/policy.h"
#include "coins.h"
#include "batchproof_container.h"
#include "blacklists.h"

#include <atomic>
#include <sstream>
Expand Down Expand Up @@ -440,8 +439,7 @@ bool CheckLelantusJoinSplitTransaction(
BOOST_FOREACH(
const sigma::PublicCoin &pubCoinValue,
index->sigmaMintedPubCoins[denominationAndId]) {
std::vector<unsigned char> vch = pubCoinValue.getValue().getvch();
if (sigma::sigma_blacklist.count(HexStr(vch.begin(), vch.end())) > 0) {
if (::Params().GetConsensus().sigmaBlacklist.count(pubCoinValue.getValue()) > 0) {
continue;
}
lelantus::PublicCoin publicCoin(pubCoinValue.getValue() + lelantusParams->get_h1() * intDenom);
Expand Down
4 changes: 3 additions & 1 deletion src/secp256k1/include/GroupElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ class GroupElement final {

std::size_t hash() const;

std::size_t get_hash() const;

GroupElement& set_base_g();

friend class MultiExponent;
Expand All @@ -125,7 +127,7 @@ namespace std {
{
size_t operator()(const secp_primitives::GroupElement& g) const
{
return g.hash();
return g.get_hash();
}
};
} // namespace std
Expand Down
6 changes: 3 additions & 3 deletions src/secp256k1/include/Scalar.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class Scalar final {

Scalar hash(const unsigned char* data,size_t len);

std::size_t get_hash() const;

bool isMember() const;

bool isZero() const;
Expand Down Expand Up @@ -131,9 +133,7 @@ using namespace secp_primitives;
template<>
struct hash<Scalar> {
size_t operator()(const Scalar& s) const {
array<unsigned char, 32> d;
s.serialize(d.data());
return hash<string>()(string(d.begin(), d.end()));
return s.get_hash();
}
};

Expand Down
6 changes: 6 additions & 0 deletions src/secp256k1/src/cpp/GroupElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,12 @@ std::size_t GroupElement::hash() const
return std::hash<std::string>()(std::string(coord.begin(), coord.end()));
}

std::size_t GroupElement::get_hash() const {
secp256k1_fe x = reinterpret_cast<secp256k1_gej *>(g_)->x;
secp256k1_fe_normalize(&x);
return x.n[0] ^ (x.n[1] << 16);
}

const void* GroupElement::get_value() const {
return g_;
}
Expand Down
4 changes: 4 additions & 0 deletions src/secp256k1/src/cpp/Scalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ Scalar Scalar::hash(const unsigned char* data, size_t len) {
return result_;
}

std::size_t Scalar::get_hash() const {
auto scalar = reinterpret_cast<const secp256k1_scalar *>(value_);
return scalar->d[0] ^ (scalar->d[1] << 8);
}

std::string Scalar::tostring() const {
unsigned char buffer[32];
Expand Down
11 changes: 3 additions & 8 deletions src/sigma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
#include "primitives/mint_spend.h"
#include "batchproof_container.h"

#include "blacklists.h"

#include <atomic>
#include <sstream>
#include <chrono>
Expand Down Expand Up @@ -277,8 +275,7 @@ bool CheckSigmaSpendTransaction(
BOOST_FOREACH(const sigma::PublicCoin& pubCoinValue,
index->sigmaMintedPubCoins[denominationAndId]) {
if (nHeight >= params.nStartSigmaBlacklist) {
std::vector<unsigned char> vch = pubCoinValue.getValue().getvch();
if(sigma_blacklist.count(HexStr(vch.begin(), vch.end())) > 0) {
if (::Params().GetConsensus().sigmaBlacklist.count(pubCoinValue.getValue()) > 0) {
continue;
}
}
Expand Down Expand Up @@ -1069,8 +1066,7 @@ int CSigmaState::GetCoinSetForSpend(
BOOST_FOREACH(const sigma::PublicCoin& pubCoinValue,
block->sigmaMintedPubCoins[denomAndId]) {
if (chainActive.Height() >= ::Params().GetConsensus().nStartSigmaBlacklist) {
std::vector<unsigned char> vch = pubCoinValue.getValue().getvch();
if(sigma_blacklist.count(HexStr(vch.begin(), vch.end())) > 0) {
if (::Params().GetConsensus().sigmaBlacklist.count(pubCoinValue.getValue()) > 0) {
continue;
}
}
Expand Down Expand Up @@ -1112,8 +1108,7 @@ void CSigmaState::GetAnonymitySet(
BOOST_FOREACH(const sigma::PublicCoin& pubCoinValue,
block->sigmaMintedPubCoins[denomAndId]) {
if (fStartSigmaBlacklist && chainActive.Height() >= params.nStartSigmaBlacklist) {
std::vector<unsigned char> vch = pubCoinValue.getValue().getvch();
if(sigma_blacklist.count(HexStr(vch.begin(), vch.end())) > 0) {
if (::Params().GetConsensus().sigmaBlacklist.count(pubCoinValue.getValue()) > 0) {
continue;
}
}
Expand Down

0 comments on commit 7960de9

Please sign in to comment.