Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/stackify all the things #5343

Draft
wants to merge 10 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions src/bls/bls.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class CBLSWrapper
static constexpr size_t SerSize = _SerSize;

explicit CBLSWrapper() = default;
explicit CBLSWrapper(const std::vector<unsigned char>& vecBytes) : CBLSWrapper<ImplType, _SerSize, C>()
explicit CBLSWrapper(Span<uint8_t> vecBytes) : CBLSWrapper<ImplType, _SerSize, C>()
{
SetByteVector(vecBytes);
}
Expand Down Expand Up @@ -99,18 +99,18 @@ class CBLSWrapper
*(static_cast<C*>(this)) = C();
}

void SetByteVector(const std::vector<uint8_t>& vecBytes, const bool specificLegacyScheme)
void SetByteVector(Span<uint8_t> vecBytes, const bool specificLegacyScheme)
{
if (vecBytes.size() != SerSize) {
Reset();
return;
}

if (ranges::all_of(vecBytes, [](uint8_t c) { return c == 0; })) {
if (std::all_of(vecBytes.begin(), vecBytes.end(), [](uint8_t c) { return c == 0; })) {
Reset();
} else {
try {
impl = ImplType::FromBytes(bls::Bytes(vecBytes), specificLegacyScheme);
impl = ImplType::FromBytes(bls::Bytes(vecBytes.data(), SerSize), specificLegacyScheme);
fValid = true;
} catch (...) {
Reset();
Expand All @@ -119,20 +119,20 @@ class CBLSWrapper
cachedHash.SetNull();
}

void SetByteVector(const std::vector<uint8_t>& vecBytes)
void SetByteVector(Span<uint8_t> vecBytes)
{
SetByteVector(vecBytes, bls::bls_legacy_scheme.load());
}

std::vector<uint8_t> ToByteVector(const bool specificLegacyScheme) const
std::array<uint8_t, SerSize> ToByteVector(const bool specificLegacyScheme) const
{
if (!fValid) {
return std::vector<uint8_t>(SerSize, 0);
return {};
}
return impl.Serialize(specificLegacyScheme);
return impl.SerializeToArray(specificLegacyScheme);
}

std::vector<uint8_t> ToByteVector() const
std::array<uint8_t, SerSize> ToByteVector() const
{
return ToByteVector(bls::bls_legacy_scheme.load());
}
Expand Down Expand Up @@ -185,7 +185,7 @@ class CBLSWrapper
template <typename Stream>
inline void Unserialize(Stream& s, const bool specificLegacyScheme, bool checkMalleable = true)
{
std::vector<uint8_t> vecBytes(SerSize, 0);
std::array<uint8_t, SerSize> vecBytes{};
s.read(reinterpret_cast<char*>(vecBytes.data()), SerSize);
SetByteVector(vecBytes, specificLegacyScheme);

Expand Down Expand Up @@ -228,7 +228,7 @@ class CBLSWrapper

inline std::string ToString(const bool specificLegacyScheme) const
{
std::vector<uint8_t> buf = ToByteVector(specificLegacyScheme);
auto buf = ToByteVector(specificLegacyScheme);
return HexStr(buf);
}

Expand All @@ -255,6 +255,12 @@ struct CBLSIdImplicit : public uint256
{
return {begin(), end()};
}
[[nodiscard]] std::array<uint8_t, 256 / 8> SerializeToArray(const bool fLegacy) const
{
std::array<uint8_t, 256 / 8> ret{};
std::copy(begin(), end(), ret.begin());
return ret;
}
};

class CBLSId : public CBLSWrapper<CBLSIdImplicit, BLS_CURVE_ID_SIZE, CBLSId>
Expand Down Expand Up @@ -405,7 +411,7 @@ class CBLSLazyWrapper
private:
mutable std::mutex mutex;

mutable std::vector<uint8_t> vecBytes;
mutable std::array<uint8_t, BLSObject::SerSize> vecBytes;
mutable bool bufValid{false};
mutable bool bufLegacyScheme{true};

Expand All @@ -416,7 +422,7 @@ class CBLSLazyWrapper

public:
CBLSLazyWrapper() :
vecBytes(BLSObject::SerSize, 0),
vecBytes{},
bufLegacyScheme(bls::bls_legacy_scheme.load())
{}

Expand All @@ -434,7 +440,7 @@ class CBLSLazyWrapper
if (r.bufValid) {
vecBytes = r.vecBytes;
} else {
vecBytes.resize(BLSObject::SerSize);
// vecBytes.resize(BLSObject::SerSize);
std::fill(vecBytes.begin(), vecBytes.end(), 0);
}
objInitialized = r.objInitialized;
Expand All @@ -457,7 +463,7 @@ class CBLSLazyWrapper
{
std::unique_lock<std::mutex> l(mutex);
if (!objInitialized && !bufValid) {
vecBytes.resize(BLSObject::SerSize);
// vecBytes.resize(BLSObject::SerSize);
std::fill(vecBytes.begin(), vecBytes.end(), 0);
} else if (!bufValid || (bufLegacyScheme != specificLegacyScheme)) {
vecBytes = obj.ToByteVector(specificLegacyScheme);
Expand Down Expand Up @@ -542,7 +548,7 @@ class CBLSLazyWrapper
{
std::unique_lock<std::mutex> l(mutex);
if (!objInitialized && !bufValid) {
vecBytes.resize(BLSObject::SerSize);
// vecBytes.resize(BLSObject::SerSize);
std::fill(vecBytes.begin(), vecBytes.end(), 0);
hash.SetNull();
} else if (!bufValid) {
Expand Down
12 changes: 4 additions & 8 deletions src/bls/bls_ies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ bool CBLSIESEncryptedBlob::Encrypt(size_t idx, const CBLSPublicKey& peerPubKey,
return false;
}

std::vector<unsigned char> symKey = pk.ToByteVector();
symKey.resize(32);
auto symKey = pk.ToByteVector();

uint256 iv = GetIV(idx);
return EncryptBlob(plainTextData, dataSize, data, symKey.data(), iv.begin());
Expand All @@ -63,8 +62,7 @@ bool CBLSIESEncryptedBlob::Decrypt(size_t idx, const CBLSSecretKey& secretKey, C
return false;
}

std::vector<unsigned char> symKey = pk.ToByteVector();
symKey.resize(32);
auto symKey = pk.ToByteVector();

uint256 iv = GetIV(idx);
return DecryptBlob(data.data(), data.size(), decryptedDataRet, symKey.data(), iv.begin());
Expand Down Expand Up @@ -117,8 +115,7 @@ bool CBLSIESMultiRecipientBlobs::Encrypt(size_t idx, const CBLSPublicKey& recipi
return false;
}

std::vector<uint8_t> symKey = pk.ToByteVector();
symKey.resize(32);
auto symKey = pk.ToByteVector();

return EncryptBlob(blob.data(), blob.size(), blobs[idx], symKey.data(), ivVector[idx].begin());
}
Expand All @@ -134,8 +131,7 @@ bool CBLSIESMultiRecipientBlobs::Decrypt(size_t idx, const CBLSSecretKey& sk, Bl
return false;
}

std::vector<uint8_t> symKey = pk.ToByteVector();
symKey.resize(32);
auto symKey = pk.ToByteVector();

uint256 iv = ivSeed;
for (size_t i = 0; i < idx; i++) {
Expand Down
7 changes: 5 additions & 2 deletions src/coinjoin/coinjoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <validation.h>

#include <string>
//#include <iterator>

constexpr static CAmount DEFAULT_MAX_RAW_TX_FEE{COIN / 10};

Expand Down Expand Up @@ -65,7 +66,8 @@ bool CCoinJoinQueue::Sign()
bool CCoinJoinQueue::CheckSignature(const CBLSPublicKey& blsPubKey) const
{
bool legacy_bls_scheme = !llmq::utils::IsV19Active(::ChainActive().Tip());
if (!CBLSSignature(vchSig).VerifyInsecure(blsPubKey, GetSignatureHash(legacy_bls_scheme))) {
Span<uint8_t> span(const_cast<uint8_t*>(vchSig.data()), vchSig.size());
if (!CBLSSignature(span).VerifyInsecure(blsPubKey, GetSignatureHash(legacy_bls_scheme))) {
LogPrint(BCLog::COINJOIN, "CCoinJoinQueue::CheckSignature -- VerifyInsecure() failed\n");
return false;
}
Expand Down Expand Up @@ -114,7 +116,8 @@ bool CCoinJoinBroadcastTx::Sign()
bool CCoinJoinBroadcastTx::CheckSignature(const CBLSPublicKey& blsPubKey) const
{
bool legacy_bls_scheme = !llmq::utils::IsV19Active(::ChainActive().Tip());
if (!CBLSSignature(vchSig).VerifyInsecure(blsPubKey, GetSignatureHash(legacy_bls_scheme))) {
Span<uint8_t> span(const_cast<uint8_t*>(vchSig.data()), vchSig.size());
if (!CBLSSignature(span).VerifyInsecure(blsPubKey, GetSignatureHash(legacy_bls_scheme))) {
LogPrint(BCLog::COINJOIN, "CCoinJoinBroadcastTx::CheckSignature -- VerifyInsecure() failed\n");
return false;
}
Expand Down
5 changes: 3 additions & 2 deletions src/coinjoin/coinjoin.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <version.h>

#include <utility>
#include <iterator>

class CCoinJoin;
class CConnman;
Expand Down Expand Up @@ -200,7 +201,7 @@ class CCoinJoinQueue
uint256 m_protxHash;
int64_t nTime{0};
bool fReady{false}; //ready for submit
std::vector<unsigned char> vchSig;
std::array<uint8_t, 96> vchSig;
// memory only
bool fTried{false};

Expand Down Expand Up @@ -272,7 +273,7 @@ class CCoinJoinBroadcastTx
CTransactionRef tx;
COutPoint masternodeOutpoint;
uint256 m_protxHash;
std::vector<unsigned char> vchSig;
std::array<unsigned char, 96> vchSig;
int64_t sigTime{0};

CCoinJoinBroadcastTx() :
Expand Down
3 changes: 3 additions & 0 deletions src/dashbls/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ blspy.*.so
.mypy_cache/
.pytest_chache/
.eggs/
cmake-build-debug/

js_build
node_modules
Expand Down Expand Up @@ -67,6 +68,8 @@ yarn-error.log
.vs/
out/

target

Makefile.in
/ar-lib
/mdate-sh
Expand Down
29 changes: 28 additions & 1 deletion src/dashbls/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ endif()

project(BLS)

set(BUILD_BLS_JS_BINDINGS "1" CACHE STRING "")
set(BUILD_BLS_PYTHON_BINDINGS "1" CACHE STRING "")
set(BUILD_BLS_TESTS "1" CACHE STRING "")
set(BUILD_BLS_BENCHMARKS "1" CACHE STRING "")
Expand Down Expand Up @@ -110,10 +111,36 @@ set(MI_OVERRIDE "off" CACHE STRING "")
add_subdirectory(depends/relic)
add_subdirectory(depends/mimalloc)

#message(STATUS "Patching Relic to make setjmp.h inclusion conditional")
#
#execute_process(
# COMMAND bash -c "git apply ${CMAKE_SOURCE_DIR}/setjmp_patch.diff"
# WORKING_DIRECTORY ${RELIC_SRC}
#)

add_subdirectory(src)


# Write include paths for rust binding
if(EMSCRIPTEN)
add_subdirectory(js-bindings)
file(APPEND "${CMAKE_CURRENT_LIST_DIR}/build/include_paths.txt" "${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES}/c++/v1/;")
endif()

file(APPEND "${CMAKE_CURRENT_LIST_DIR}/build/include_paths.txt" "${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES};")

if(GMP_INCLUDES)
file(APPEND "${CMAKE_CURRENT_LIST_DIR}/build/include_paths.txt" "${GMP_INCLUDES};")
endif()

# Write gmp library path for rust binding
if(GMP_LIBRARIES)
file(APPEND "${CMAKE_CURRENT_LIST_DIR}/build/gmp_libraries.txt" "${GMP_LIBRARIES}")
endif()

if(EMSCRIPTEN)
if(BUILD_BLS_JS_BINDINGS)
add_subdirectory(js-bindings)
endif()
else()
# emscripten can't build python bindings, it produces only javascript
# add_subdirectory(contrib/pybind11)
Expand Down
Loading