Skip to content

Commit

Permalink
remove some unnecessary wrapper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
greenaddress committed May 15, 2024
1 parent de039d6 commit 7650813
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 31 deletions.
9 changes: 9 additions & 0 deletions src/bytewords.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "utils.hpp"
#include <stdexcept>
#include <algorithm>
#include <cstring>
#include <esp_crc.h>

namespace ur {

Expand Down Expand Up @@ -124,6 +126,13 @@ static string encode(const ByteVector& buf, const string& separator) {
return join(words, separator);
}

static inline ByteVector crc32_bytes(const ByteVector &buf) {
uint32_t checksum = __builtin_bswap32(esp_crc32_le(0, buf.data(), buf.size()));
ByteVector result(sizeof(checksum));
std::memcpy(result.data(), &checksum, sizeof(checksum));
return result;
}

static ByteVector add_crc(const ByteVector& buf) {
auto crc_buf = crc32_bytes(buf);
auto result = buf;
Expand Down
3 changes: 2 additions & 1 deletion src/fountain-decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <string>
#include <cmath>
#include <numeric>
#include <esp_crc.h>

using namespace std;

Expand Down Expand Up @@ -143,7 +144,7 @@ void FountainDecoder::process_simple_part(Part& p) {
auto message = join_fragments(fragments, *_expected_message_len);

// Verify the message checksum and note success or failure
auto checksum = crc32_int(message);
auto checksum = esp_crc32_le(0, message.data(), message.size());
if(checksum == _expected_checksum) {
result_ = message;
} else {
Expand Down
3 changes: 2 additions & 1 deletion src/fountain-encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <vector>
#include <limits>
#include <cbor.h>
#include <esp_crc.h>
using namespace std;

namespace ur {
Expand Down Expand Up @@ -177,7 +178,7 @@ ByteVector FountainEncoder::Part::cbor() const {
FountainEncoder::FountainEncoder(const ByteVector& message, size_t max_fragment_len, uint32_t first_seq_num, size_t min_fragment_len) {
assert(message.size() <= std::numeric_limits<uint32_t>::max());
message_len_ = message.size();
checksum_ = crc32_int(message);
checksum_ = esp_crc32_le(0, message.data(), message.size());
fragment_len_ = find_nominal_fragment_length(message_len_, min_fragment_len, max_fragment_len);
fragments_ = partition_message(message, fragment_len_);
seq_num_ = first_seq_num;
Expand Down
24 changes: 0 additions & 24 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,12 @@
#include <vector>
#include <algorithm>
#include <cctype>
#include <mbedtls/sha256.h>
#include <esp_crc.h>


using namespace std;

namespace ur {

#define SHA256_LEN 32

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

ByteVector crc32_bytes(const ByteVector &buf) {
uint32_t checksum = __builtin_bswap32(crc32_int(buf));
auto *cbegin = (uint8_t*)&checksum;
auto *cend = cbegin + sizeof(uint32_t);
return {cbegin, cend};
}

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

ByteVector string_to_bytes(const string& s) {
return {s.begin(), s.end()};
}
Expand Down
4 changes: 0 additions & 4 deletions src/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ 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>>;

void sha256(const ByteVector &buf, std::array<uint8_t, 32> &digest);
ByteVector crc32_bytes(const ByteVector &buf);
uint32_t crc32_int(const ByteVector &buf);

ByteVector string_to_bytes(const std::string& s);

std::string data_to_hex(const ByteVector& in);
Expand Down
8 changes: 7 additions & 1 deletion src/xoshiro256.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "xoshiro256.hpp"
#include <limits>
#include <cstring>
#include <mbedtls/sha256.h>

/* Written in 2018 by David Blackman and Sebastiano Vigna ([email protected])
Expand Down Expand Up @@ -55,7 +56,12 @@ void Xoshiro256::set_s(const std::array<uint8_t, 32>& a) {

void Xoshiro256::hash_then_set_s(const ByteVector& bytes) {
std::array<uint8_t, 32> a;
sha256(bytes, a);
mbedtls_sha256_context ctx;
mbedtls_sha256_init(&ctx);
mbedtls_sha256_starts(&ctx, 0);
mbedtls_sha256_update(&ctx, bytes.data(), bytes.size());
mbedtls_sha256_finish(&ctx, a.data());
mbedtls_sha256_free(&ctx);
set_s(a);
}

Expand Down

0 comments on commit 7650813

Please sign in to comment.