Skip to content

Commit

Permalink
improvements from BlockchainCommons#13
Browse files Browse the repository at this point in the history
  • Loading branch information
greenaddress committed May 15, 2024
1 parent be2437d commit c936aac
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 49 deletions.
28 changes: 14 additions & 14 deletions src/bytewords.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,21 @@ static bool decode_word(const string& word, size_t word_len, uint8_t& output) {
return true;
}

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 @@ -124,19 +124,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 @@ -147,7 +147,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 @@ -186,9 +186,9 @@ string Bytewords::encode(style style, const ByteVector& bytes) {
return encode_with_separator(bytes, "-");
case minimal:
return encode_minimal(bytes);
default:
assert(false);
}
assert(false);
return string();
}

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

}
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/random-sampler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "random-sampler.hpp"
#include <numeric>
#include <algorithm>
#include <assert.h>
#include <cassert>

using namespace std;

Expand Down
4 changes: 2 additions & 2 deletions 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 Expand Up @@ -63,4 +63,4 @@ class URDecoder final {

}

#endif // BC_UR_DECODER_HPP
#endif // BC_UR_DECODER_HPP
4 changes: 2 additions & 2 deletions 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 Expand Up @@ -49,4 +49,4 @@ class UREncoder final {

}

#endif // BC_UR_ENCODER_HPP
#endif // BC_UR_ENCODER_HPP
4 changes: 2 additions & 2 deletions src/ur.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,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))
{}

bool UR::is_valid() const {
Expand Down
6 changes: 3 additions & 3 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 @@ -25,11 +25,11 @@ class UR final {
const ByteVector& cbor() const { return cbor_; }
bool is_valid() const;

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

bool operator==(const UR& lhs, const UR& rhs);

}

#endif // BC_UR_UR_HPP
#endif // BC_UR_UR_HPP
29 changes: 15 additions & 14 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ extern "C" {

}

#include <array>
#include <vector>
#include <algorithm>
#include <cctype>
Expand All @@ -26,34 +27,34 @@ namespace ur {
#define SHA256_LEN 32

ByteVector sha256(const ByteVector &buf) {
uint8_t digest[SHA256_LEN];
array<uint8_t,SHA256_LEN> digest;
mbedtls_sha256_context ctx;
mbedtls_sha256_init(&ctx);
mbedtls_sha256_starts(&ctx, 0);
mbedtls_sha256_update(&ctx, &buf[0], buf.size());
mbedtls_sha256_finish(&ctx, digest);
mbedtls_sha256_update(&ctx, buf.data(), buf.size());
mbedtls_sha256_finish(&ctx, digest.data());
mbedtls_sha256_free(&ctx);

return ByteVector(digest, digest + SHA256_LEN);
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 @@ -121,7 +122,7 @@ StringVector split(const string& s, char separator) {
}
}

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

Expand All @@ -142,12 +143,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
8 changes: 4 additions & 4 deletions src/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
#include <utility>
#include <string>
#include <array>
#include <assert.h>
#include <cassert>

#include "psram-allocator.hpp"

namespace ur {

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

ByteVector sha256(const ByteVector &buf);
ByteVector crc32_bytes(const ByteVector &buf);
Expand Down
7 changes: 3 additions & 4 deletions src/xoshiro256.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,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 @@ -151,13 +151,12 @@ 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
4 changes: 2 additions & 2 deletions src/xoshiro256.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#ifndef XOSHIRO256_HPP
#define XOSHIRO256_HPP

#include <stdint.h>
#include <cstdint>
#include <array>
#include <string>
#include "utils.hpp"
Expand All @@ -34,7 +34,7 @@ class Xoshiro256 {
void long_jump();

private:
uint64_t s[4];
std::array<uint64_t,4> s;

void set_s(const std::array<uint8_t, 32>& a);
void hash_then_set_s(const ByteVector& bytes);
Expand Down

0 comments on commit c936aac

Please sign in to comment.