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

series of improvements to this library #13

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,13 @@ jobs:
- run: export CC="clang-10" && export CXX="clang++-10" && ./configure && make check
- run: sudo make install
- run: export CC="clang-10" && export CXX="clang++-10" && make distcheck && make distclean

build-Linux-cmake:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v2
- run: sudo apt-get install -y cmake || exit 1
- run: cmake -B build -S . -DENABLE_TESTS:BOOL=TRUE
- run: cmake --build build
- run: ctest --test-dir build || exit 1
- run: cmake --install build --prefix install-dir
97 changes: 97 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
cmake_minimum_required(VERSION 3.15)
project(bc-ur
VERSION 0.3.0
DESCRIPTION "UR reference library in C++"
LANGUAGES C CXX
)

option(ENABLE_TESTS "Build bc-ur tests" FALSE)

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

add_library(bc-ur)

set(public_headers
src/bc-ur.hpp
src/bytewords.hpp
src/cbor-lite.hpp
src/fountain-decoder.hpp
src/fountain-encoder.hpp
src/fountain-utils.hpp
src/random-sampler.hpp
src/ur.hpp
src/ur-decoder.hpp
src/ur-encoder.hpp
src/utils.hpp
src/xoshiro256.hpp
)
set(sources
src/bytewords.cpp
src/crc32.c
src/crc32.h
src/fountain-decoder.cpp
src/fountain-encoder.cpp
src/fountain-utils.cpp
src/Makefile.in
src/memzero.c
src/memzero.h
src/random-sampler.cpp
src/sha2.c
src/sha2.h
src/ur.cpp
src/ur-decoder.cpp
src/ur-encoder.cpp
src/utils.cpp
src/xoshiro256.cpp
)

target_sources(bc-ur PRIVATE ${public_headers} ${sources})
target_include_directories(bc-ur
PUBLIC
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/src"
)
set_target_properties(bc-ur PROPERTIES
PUBLIC_HEADER "${public_headers}"
VERSION ${PROJECT_VERSION}
)

if(ENABLE_TESTS)
enable_testing()
add_executable(bc-ur-test test/test.cpp test/test-utils.cpp)
target_include_directories(bc-ur-test PRIVATE test)
target_link_libraries(bc-ur-test bc-ur)
add_test(bc-ur-test ./bc-ur-test)
endif()

include(CMakePackageConfigHelpers)
include(GNUInstallDirs)
configure_package_config_file(cmake/bc-ur-config.cmake.in bc-ur-config.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/bc-ur
)
write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/bc-ur-config-version.cmake
COMPATIBILITY SameMajorVersion
)
install(TARGETS bc-ur
EXPORT "bc-ur-export"
ARCHIVE
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/bc-ur
)
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/bc-ur-config.cmake
${CMAKE_CURRENT_BINARY_DIR}/bc-ur-config-version.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/bc-ur
)
install(EXPORT "bc-ur-export"
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/bc-ur
NAMESPACE ${PROJECT_NAME}::
FILE bc-ur-targets.cmake
)

6 changes: 6 additions & 0 deletions cmake/bc-ur-config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@PACKAGE_INIT@

set_and_check(BC-UR_LIB_DIR "@PACKAGE_LIB_INSTALL_DIR@")
include("${BCUR_LIB_DIR}/cmake/bc-urTargets.cmake")
set(bc-ur_COMPONENT_FOUND TRUE)
check_required_components(bc-ur)
30 changes: 14 additions & 16 deletions src/bytewords.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ uint8_t decode_word(const string& word, size_t word_len) {
throw runtime_error("Invalid Bytewords.");
}

static int16_t* array = NULL;
static int16_t* array = nullptr;
const size_t dim = 26;

// Since the first and last letters of each Byteword are unique,
// we can use them as indexes into a two-dimensional lookup table.
// This table is generated lazily.
if(array == NULL) {
if(array == nullptr) {
const size_t array_len = dim * dim;
array = (int16_t*)malloc(array_len * sizeof(int16_t));
for(size_t i = 0; i < array_len; i++) {
Expand Down Expand Up @@ -69,21 +69,21 @@ uint8_t decode_word(const string& word, size_t word_len) {
return value;
}

static const string get_word(uint8_t index) {
auto p = &bytewords[index * 4];
return string(p, p + 4);
static string get_word(uint8_t index) {
const auto *p = &bytewords[index * 4];
return {p, p + 4};
}

static const string get_minimal_word(uint8_t index) {
static string get_minimal_word(uint8_t index) {
string word;
word.reserve(2);
auto p = &bytewords[index * 4];
const auto *p = &bytewords[index * 4];
word.push_back(*p);
word.push_back(*(p + 3));
return word;
}

static const string encode(const ByteVector& buf, const string& separator) {
static string encode(const ByteVector& buf, const string& separator) {
auto len = buf.size();
StringVector words;
words.reserve(len);
Expand All @@ -94,19 +94,19 @@ static const string encode(const ByteVector& buf, const string& separator) {
return join(words, separator);
}

static const ByteVector add_crc(const ByteVector& buf) {
static ByteVector add_crc(const ByteVector& buf) {
auto crc_buf = crc32_bytes(buf);
auto result = buf;
append(result, crc_buf);
return result;
}

static const string encode_with_separator(const ByteVector& buf, const string& separator) {
static string encode_with_separator(const ByteVector& buf, const string& separator) {
auto crc_buf = add_crc(buf);
return encode(crc_buf, separator);
}

static const string encode_minimal(const ByteVector& buf) {
static string encode_minimal(const ByteVector& buf) {
string result;
auto crc_buf = add_crc(buf);
auto len = crc_buf.size();
Expand All @@ -117,7 +117,7 @@ static const string encode_minimal(const ByteVector& buf) {
return result;
}

static const ByteVector _decode(const string& s, char separator, size_t word_len) {
static ByteVector _decode(const string& s, char separator, size_t word_len) {
StringVector words;
if(word_len == 4) {
words = split(s, separator);
Expand Down Expand Up @@ -148,9 +148,8 @@ string Bytewords::encode(style style, const ByteVector& bytes) {
return encode_with_separator(bytes, "-");
case minimal:
return encode_minimal(bytes);
default:
assert(false);
}
assert(false);
}

ByteVector Bytewords::decode(style style, const string& string) {
Expand All @@ -161,9 +160,8 @@ ByteVector Bytewords::decode(style style, const string& string) {
return _decode(string, '-', 4);
case minimal:
return _decode(string, 0, 2);
default:
assert(false);
}
assert(false);
}

}
2 changes: 1 addition & 1 deletion src/fountain-encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//

#include "fountain-encoder.hpp"
#include <assert.h>
#include <cassert>
#include <cmath>
#include <optional>
#include <vector>
Expand Down
2 changes: 1 addition & 1 deletion src/ur-decoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

namespace ur {

class URDecoder final {
class URDecoder {
public:
typedef std::optional<std::variant<UR, std::exception> > Result;

Expand Down
2 changes: 1 addition & 1 deletion src/ur-encoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace ur {

class UREncoder final {
class UREncoder {
public:
// Encode a single-part UR.
static std::string encode(const UR& ur);
Expand Down
4 changes: 2 additions & 2 deletions src/ur.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ using namespace std;

namespace ur {

UR::UR(const std::string &type, const ByteVector &cbor)
: type_(type), cbor_(cbor)
UR::UR(std::string type, ByteVector cbor)
: type_(std::move(type)), cbor_(std::move(cbor))
{
if (!is_ur_type(type)) {
throw invalid_type();
Expand Down
4 changes: 2 additions & 2 deletions src/ur.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace ur {

class UR final {
class UR {
private:
std::string type_;
ByteVector cbor_;
Expand All @@ -24,7 +24,7 @@ class UR final {
const std::string& type() const { return type_; }
const ByteVector& cbor() const { return cbor_; }

UR(const std::string& type, const ByteVector& cbor);
UR(std::string type, ByteVector cbor);
};

bool operator==(const UR& lhs, const UR& rhs);
Expand Down
29 changes: 15 additions & 14 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ extern "C" {

}

#include <array>
#include <vector>
#include <sstream>
#include <algorithm>
Expand All @@ -24,28 +25,28 @@ using namespace std;
namespace ur {

ByteVector sha256(const ByteVector &buf) {
uint8_t digest[SHA256_DIGEST_LENGTH];
sha256_Raw(&buf[0], buf.size(), digest);
return ByteVector(digest, digest + SHA256_DIGEST_LENGTH);
array<uint8_t,SHA256_DIGEST_LENGTH> digest;
sha256_Raw(buf.data(), buf.size(), digest.data());
return {digest.begin(), digest.end()};
}

ByteVector crc32_bytes(const ByteVector &buf) {
uint32_t checksum = ur_crc32n(&buf[0], buf.size());
auto cbegin = (uint8_t*)&checksum;
auto cend = cbegin + sizeof(uint32_t);
return ByteVector(cbegin, cend);
uint32_t checksum = ur_crc32n(buf.data(), buf.size());
auto *cbegin = (uint8_t*)&checksum;
auto *cend = cbegin + sizeof(uint32_t);
return {cbegin, cend};
}

uint32_t crc32_int(const ByteVector &buf) {
return ur_crc32(&buf[0], buf.size());
return ur_crc32(buf.data(), buf.size());
}

ByteVector string_to_bytes(const string& s) {
return ByteVector(s.begin(), s.end());
return {s.begin(), s.end()};
}

string data_to_hex(const ByteVector& in) {
auto hex = "0123456789abcdef";
const string hex = "0123456789abcdef";
string result;
for(auto c: in) {
result.append(1, hex[(c >> 4) & 0xF]);
Expand Down Expand Up @@ -81,7 +82,7 @@ uint32_t bytes_to_int(const ByteVector& in) {
string join(const StringVector &strings, const string &separator) {
ostringstream result;
bool first = true;
for(auto s: strings) {
for(const auto& s: strings) {
if(!first) {
result << separator;
}
Expand All @@ -104,7 +105,7 @@ StringVector split(const string& s, char separator) {
}
}

if(buf != "") {
if(!buf.empty()) {
result.push_back(buf);
}

Expand All @@ -125,12 +126,12 @@ string take_first(const string &s, size_t count) {
auto first = s.begin();
auto c = min(s.size(), count);
auto last = first + c;
return string(first, last);
return {first, last};
}

string drop_first(const string& s, size_t count) {
if(count >= s.length()) { return ""; }
return string(s.begin() + count, s.end());
return {s.begin() + count, s.end()};
}

void xor_into(ByteVector& target, const ByteVector& source) {
Expand Down
4 changes: 2 additions & 2 deletions src/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

namespace ur {

typedef std::vector<uint8_t> ByteVector;
typedef std::vector<std::string> StringVector;
using ByteVector = std::vector<uint8_t>;
using StringVector = std::vector<std::string>;

ByteVector sha256(const ByteVector &buf);
ByteVector crc32_bytes(const ByteVector &buf);
Expand Down
7 changes: 4 additions & 3 deletions src/xoshiro256.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "xoshiro256.hpp"
#include <limits>
#include <cstring>

/* Written in 2018 by David Blackman and Sebastiano Vigna ([email protected])

Expand Down Expand Up @@ -129,7 +130,7 @@ void Xoshiro256::jump() {
uint64_t s3 = 0;
for(int i = 0; i < sizeof JUMP / sizeof *JUMP; i++)
for(int b = 0; b < 64; b++) {
if (JUMP[i] & UINT64_C(1) << b) {
if ((JUMP[i] & UINT64_C(1) << b) != 0u) {
s0 ^= s[0];
s1 ^= s[1];
s2 ^= s[2];
Expand All @@ -150,13 +151,13 @@ void Xoshiro256::jump() {
subsequences for parallel distributed computations. */

void Xoshiro256::long_jump() {
static const uint64_t LONG_JUMP[] = { 0x76e15d3efefdcbbf, 0xc5004e441c522fb3, 0x77710069854ee241, 0x39109bb02acbe635 };
static const std::array<uint64_t,4> LONG_JUMP = { 0x76e15d3efefdcbbf, 0xc5004e441c522fb3, 0x77710069854ee241, 0x39109bb02acbe635 };

uint64_t s0 = 0;
uint64_t s1 = 0;
uint64_t s2 = 0;
uint64_t s3 = 0;
for(int i = 0; i < sizeof LONG_JUMP / sizeof *LONG_JUMP; i++)
for(int i = 0; i < LONG_JUMP.size(); i++)
for(int b = 0; b < 64; b++) {
if (LONG_JUMP[i] & UINT64_C(1) << b) {
s0 ^= s[0];
Expand Down
Loading