Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Asherda committed Aug 20, 2020
0 parents commit 5bd5b5c
Show file tree
Hide file tree
Showing 47 changed files with 12,823 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

*.orig
__debug_bin
.vscode
CMakeCache.txt
Makefile
.idea
cert.*
*.conf
*.cmake
*.log
*.a
CMakeFiles
CMakeCache.txt
cmake_install.cmake
100 changes: 100 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
cmake_minimum_required(VERSION 3.10)
project(verushash)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") # -Wall
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
add_library(verushash STATIC
src/haraka.c
src/haraka_portable.c
src/uint256.cpp
src/utilstrencodings.cpp
src/verus_hash.cpp
src/verus_clhash.cpp
src/verus_clhash_portable.cpp
src/ripemd160.cpp
src/sha256.cpp
compat/glibc_compat.cpp
compat/glibc_sanity.cpp
compat/glibcxx_sanity.cpp
compat/strnlen.cpp
support/cleanse.cpp
blockhash.cpp
)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=x86-64")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=x86-64 ")

# optimizations
add_definitions(-O2)


# MACOS
if(APPLE)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/compat)
endif(APPLE)

set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}/src/verus_hash.cpp PROPERTIES COMPILE_FLAGS "-m64 -mpclmul -msse2 -msse3 -mssse3 -msse4 -msse4.1 -msse4.2 -maes -g -fomit-frame-pointer")
set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}/src/verus_clhash.cpp PROPERTIES COMPILE_FLAGS "-m64 -mpclmul -msse2 -msse3 -mssse3 -msse4 -msse4.1 -msse4.2 -maes -g -fomit-frame-pointer")
set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}/src/haraka.c PROPERTIES COMPILE_FLAGS "-m64 -mpclmul -msse2 -msse3 -mssse3 -msse4 -msse4.1 -msse4.2 -maes -g -fomit-frame-pointer")

# Common
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)

# BOOST
#find_package(Threads REQUIRED COMPONENTS)
# compile boost statically
set(Boost_USE_STATIC_LIBS ON)
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
#set(BUILD_SHARED_LIBRARIES OFF)
#set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++ -static")
find_package(Boost REQUIRED COMPONENTS system)

if (Boost_FOUND)
# From the official documentation:
# Add include directories to the build. [...] If the SYSTEM option is given,
# the compiler will be told the directories are meant as system include
# directories on some platforms (signalling this setting might achieve effects
# such as the compiler skipping warnings [...])."
include_directories (SYSTEM ${Boost_INCLUDE_DIR})

# From the official documentation:
# "Specify directories in which the linker will look for libraries. [...] Note
# that this command is rarely necessary. Library locations returned by
# find_package() and find_library() are absolute paths. Pass these absolute
# library file paths directly to the target_link_libraries() command. CMake
# will ensure the linker finds them."
link_directories (${Boost_LIBRARY_DIRS})
else()
message("Boost_FOUND NOT FOUND")
endif ()

include_directories(${CMAKE_CURRENT_BINARY_DIR}/../)
find_package(PkgConfig REQUIRED)
pkg_check_modules(LIBSODIUM libsodium)

if(STATIC_LIBSODIUM)
if(BUILD_STATIC_EXECUTABLES)
set_target_properties(verushash PROPERTIES LINK_SEARCH_START_STATIC 1)
set_target_properties(verushash PROPERTIES LINK_SEARCH_END_STATIC 1)
endif()
set(CMAKE_FIND_LIBRARY_SUFFIXES .lib .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
endif()

find_path(SODIUM_INCLUDE_DIR sodium.h HINTS ${LIBSODIUM_INCLUDE_DIRS} /usr/local/include /opt/local/include /opt/include)
find_library(SODIUM_LIBRARY NAMES sodium HINTS ${LIBSODIUM_LIBRARY_DIRS} /usr/local/lib /opt/local/lib /opt/lib)

if(STATIC_LIBSODIUM)
set(LIBSODIUM_CFLAGS_OTHER ${LIBSODIUM_STATIC_CFLAGS_OTHER})
set(LIBSODIUM_LDFLAGS_OTHER ${LIBSODIUM_STATIC_LDFLAGS_OTHER})
endif()

target_include_directories(verushash PUBLIC ${SODIUM_INCLUDE_DIR})
target_compile_options(verushash PUBLIC ${LIBSODIUM_CFLAGS} ${LIBSODIUM_CFLAGS_OTHER})
target_link_libraries(verushash ${SODIUM_LIBRARY} ${LIBSODIUM_LDFLAGS_OTHER})

set(LIBS ${LIBS} ${Boost_LIBRARIES})

message("-- CXXFLAGS: ${CMAKE_CXX_FLAGS}")
message("-- LIBS: ${LIBS}")

target_link_libraries (verushash ${LIBS})
147 changes: 147 additions & 0 deletions blockhash.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@

// Copyright (c) 2020 Michael Toutonghi
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "include/utilstrencodings.h"
#include "solutiondata.h"

CActivationHeight CConstVerusSolutionVector::activationHeight;
uint160 ASSETCHAINS_CHAINID = uint160(ParseHex("1af5b8015c64d39ab44c60ead8317f9f5a9b6c4c"));

[[noreturn]] void new_handler_terminate()
{
// Rather than throwing std::bad-alloc if allocation fails, terminate
// immediately to (try to) avoid chain corruption.
// Since LogPrintf may itself allocate memory, set the handler directly
// to terminate first.
std::set_new_handler(std::terminate);
fputs("Error: Out of memory. Terminating.\n", stderr);

// The log was successful, terminate now.
std::terminate();
};

// checks that the solution stored data for this header matches what is expected, ensuring that the
// values in the header match the hash of the pre-header.
bool CBlockHeader::CheckNonCanonicalData() const
{
CPBaaSPreHeader pbph(*this);
CPBaaSBlockHeader pbbh1 = CPBaaSBlockHeader(ASSETCHAINS_CHAINID, pbph);
CPBaaSBlockHeader pbbh2;

int32_t idx = GetPBaaSHeader(pbbh2, ASSETCHAINS_CHAINID);
if (idx != -1)
{
if (pbbh1.hashPreHeader == pbbh2.hashPreHeader)
{
return true;
}
}
return false;
}

// checks that the solution stored data for this header matches what is expected, ensuring that the
// values in the header match the hash of the pre-header.
bool CBlockHeader::CheckNonCanonicalData(uint160 &cID) const
{
CPBaaSPreHeader pbph(*this);
CPBaaSBlockHeader pbbh1 = CPBaaSBlockHeader(cID, pbph);
CPBaaSBlockHeader pbbh2;
int32_t idx = GetPBaaSHeader(pbbh2, cID);
if (idx != -1)
{
if (pbbh1.hashPreHeader == pbbh2.hashPreHeader)
{
return true;
}
}
return false;
}

uint256 CBlockHeader::GetVerusV2Hash() const
{
if (hashPrevBlock.IsNull())
{
// always use SHA256D for genesis block
return SerializeHash(*this);
}
else
{
if (nVersion == VERUS_V2)
{
int solutionVersion = CConstVerusSolutionVector::Version(nSolution);

// in order for this to work, the PBaaS hash of the pre-header must match the header data
// otherwise, it cannot clear the canonical data and hash in a chain-independent manner
int pbaasType = CConstVerusSolutionVector::HasPBaaSHeader(nSolution);
bool debugPrint = false;

if (pbaasType != 0 && CheckNonCanonicalData())
{
CBlockHeader bh = CBlockHeader(*this);
bh.ClearNonCanonicalData();
return SerializeVerusHashV2b(bh, solutionVersion);
}
else
{
return SerializeVerusHashV2b(*this, solutionVersion);
}
}
else
{
return SerializeVerusHash(*this);
}
}
}

// returns -1 on failure, upon failure, pbbh is undefined and likely corrupted
int32_t CBlockHeader::GetPBaaSHeader(CPBaaSBlockHeader &pbh, const uint160 &cID) const
{
// find the specified PBaaS header in the solution and return its index if present
// if not present, return -1
if (nVersion == VERUS_V2)
{
// search in the solution for this header index and return it if found
CPBaaSSolutionDescriptor d = CVerusSolutionVector::solutionTools.GetDescriptor(nSolution);
if (CVerusSolutionVector::solutionTools.HasPBaaSHeader(nSolution) != 0)
{
int32_t numHeaders = d.numPBaaSHeaders;
const CPBaaSBlockHeader *ppbbh = CVerusSolutionVector::solutionTools.GetFirstPBaaSHeader(nSolution);
for (int32_t i = 0; i < numHeaders; i++)
{
if ((ppbbh + i)->chainID == cID)
{
pbh = *(ppbbh + i);
return i;
}
}
}
}
return -1;
}

CPBaaSPreHeader::CPBaaSPreHeader(const CBlockHeader &bh)
{
hashPrevBlock = bh.hashPrevBlock;
hashMerkleRoot = bh.hashMerkleRoot;
hashFinalSaplingRoot = bh.hashFinalSaplingRoot;
nNonce = bh.nNonce;
nBits = bh.nBits;
CPBaaSSolutionDescriptor descr = CConstVerusSolutionVector::GetDescriptor(bh.nSolution);
if (descr.version >= CConstVerusSolutionVector::activationHeight.ACTIVATE_PBAAS_HEADER)
{
hashPrevMMRRoot = descr.hashPrevMMRRoot;
hashBlockMMRRoot = descr.hashBlockMMRRoot;
}
}

CPBaaSBlockHeader::CPBaaSBlockHeader(const uint160 &cID, const CPBaaSPreHeader &pbph) : chainID(cID)
{
CBLAKE2bWriter hw(SER_GETHASH, 0);

// all core data besides version, and solution, which are shared across all headers
hw << pbph;

hashPreHeader = hw.GetHash();
}
47 changes: 47 additions & 0 deletions compat/byteswap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) 2014 The Bitcoin developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or https://www.opensource.org/licenses/mit-license.php .

#ifndef BITCOIN_COMPAT_BYTESWAP_H
#define BITCOIN_COMPAT_BYTESWAP_H

#if defined(HAVE_CONFIG_H)
#include "config/bitcoin-config.h"
#endif

#include <stdint.h>

#if defined(HAVE_BYTESWAP_H)
#include <byteswap.h>
#endif

#if HAVE_DECL_BSWAP_16 == 0
inline uint16_t bswap_16(uint16_t x)
{
return (x >> 8) | ((x & 0x00ff) << 8);
}
#endif // HAVE_DECL_BSWAP16

#if HAVE_DECL_BSWAP_32 == 0
inline uint32_t bswap_32(uint32_t x)
{
return (((x & 0xff000000U) >> 24) | ((x & 0x00ff0000U) >> 8) |
((x & 0x0000ff00U) << 8) | ((x & 0x000000ffU) << 24));
}
#endif // HAVE_DECL_BSWAP32

#if HAVE_DECL_BSWAP_64 == 0
inline uint64_t bswap_64(uint64_t x)
{
return (((x & 0xff00000000000000ull) >> 56)
| ((x & 0x00ff000000000000ull) >> 40)
| ((x & 0x0000ff0000000000ull) >> 24)
| ((x & 0x000000ff00000000ull) >> 8)
| ((x & 0x00000000ff000000ull) << 8)
| ((x & 0x0000000000ff0000ull) << 24)
| ((x & 0x000000000000ff00ull) << 40)
| ((x & 0x00000000000000ffull) << 56));
}
#endif // HAVE_DECL_BSWAP64

#endif // BITCOIN_COMPAT_BYTESWAP_H
Loading

0 comments on commit 5bd5b5c

Please sign in to comment.