From 765081325adaa32b8e6b7cee6b6400a63465d5d1 Mon Sep 17 00:00:00 2001 From: Lawrence Nahum Date: Wed, 15 May 2024 16:57:34 +0200 Subject: [PATCH] remove some unnecessary wrapper functions --- src/bytewords.cpp | 9 +++++++++ src/fountain-decoder.cpp | 3 ++- src/fountain-encoder.cpp | 3 ++- src/utils.cpp | 24 ------------------------ src/utils.hpp | 4 ---- src/xoshiro256.cpp | 8 +++++++- 6 files changed, 20 insertions(+), 31 deletions(-) diff --git a/src/bytewords.cpp b/src/bytewords.cpp index e599882..d792856 100644 --- a/src/bytewords.cpp +++ b/src/bytewords.cpp @@ -9,6 +9,8 @@ #include "utils.hpp" #include #include +#include +#include namespace ur { @@ -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; diff --git a/src/fountain-decoder.cpp b/src/fountain-decoder.cpp index f191c7c..22e936a 100644 --- a/src/fountain-decoder.cpp +++ b/src/fountain-decoder.cpp @@ -11,6 +11,7 @@ #include #include #include +#include using namespace std; @@ -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 { diff --git a/src/fountain-encoder.cpp b/src/fountain-encoder.cpp index 461c666..c52e852 100644 --- a/src/fountain-encoder.cpp +++ b/src/fountain-encoder.cpp @@ -12,6 +12,7 @@ #include #include #include +#include using namespace std; namespace ur { @@ -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::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; diff --git a/src/utils.cpp b/src/utils.cpp index 0b00b0b..1c734a4 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -11,36 +11,12 @@ #include #include #include -#include -#include using namespace std; namespace ur { -#define SHA256_LEN 32 - -void sha256(const ByteVector &buf, std::array &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()}; } diff --git a/src/utils.hpp b/src/utils.hpp index 43fa2cf..cfe5190 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -23,10 +23,6 @@ using ByteVector = std::vector>; using ByteVectorVector = std::vector>; using StringVector = std::vector>; -void sha256(const ByteVector &buf, std::array &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); diff --git a/src/xoshiro256.cpp b/src/xoshiro256.cpp index 90e7a90..eb3b1fd 100644 --- a/src/xoshiro256.cpp +++ b/src/xoshiro256.cpp @@ -8,6 +8,7 @@ #include "xoshiro256.hpp" #include #include +#include /* Written in 2018 by David Blackman and Sebastiano Vigna (vigna@acm.org) @@ -55,7 +56,12 @@ void Xoshiro256::set_s(const std::array& a) { void Xoshiro256::hash_then_set_s(const ByteVector& bytes) { std::array 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); }