Skip to content

Commit

Permalink
Try hasher 256 on the metal in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
ckormanyos committed Oct 25, 2024
1 parent ff0c6df commit ae56059
Show file tree
Hide file tree
Showing 10 changed files with 852 additions and 2 deletions.
54 changes: 54 additions & 0 deletions .github/workflows/metal.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# ------------------------------------------------------------------------------
# Copyright Matt Borland 2023 - 2024.
# Copyright Christopher Kormanyos 2023 - 2024.
# Distributed under the Boost Software License,
# Version 1.0. (See accompanying file LICENSE_1_0.txt
# or copy at http://www.boost.org/LICENSE_1_0.txt)
# ------------------------------------------------------------------------------

name: metal
on:
push:
branches:
- master
- develop
- feature/**
pull_request:
types: [opened, synchronize, reopened]
jobs:
benchmark_single-stm32f429-qemu:
runs-on: ubuntu-latest
defaults:
run:
shell: bash
steps:
- uses: actions/checkout@v4
with:
fetch-depth: '0'
- name: update-tools
run: |
sudo apt install libncurses5 libpython2.7
mkdir -p emu_env && cd emu_env
wget --no-check-certificate https://armkeil.blob.core.windows.net/developer/Files/downloads/gnu-rm/10.3-2021.10/gcc-arm-none-eabi-10.3-2021.10-x86_64-linux.tar.bz2
tar -xf gcc-arm-none-eabi-10.3-2021.10-x86_64-linux.tar.bz2
wget --no-check-certificate https://github.com/xpack-dev-tools/qemu-arm-xpack/releases/download/v7.1.0-1/xpack-qemu-arm-7.1.0-1-linux-x64.tar.gz
tar -xzf xpack-qemu-arm-7.1.0-1-linux-x64.tar.gz
working-directory: ./test/metal/
- name: build benchmark_single-stm32f429
run: |
mkdir -p bin
emu_env/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-g++ -std=c++20 -Wall -Wextra -Wpedantic -Os -g -gdwarf-2 -ffunction-sections -fdata-sections -x c++ -fno-rtti -fno-use-cxa-atexit -fno-exceptions -fno-nonansi-builtins -fno-threadsafe-statics -fno-enforce-eh-specs -fno-inline-functions -mcpu=cortex-m4 -mtune=cortex-m4 -mthumb -mfloat-abi=soft -mno-unaligned-access -mno-long-calls -I../../include -DBOOST_CRYPT_DISABLE_IOSTREAM -DAPP_BENCHMARK_STANDALONE_MAIN app_benchmark_hasher.cpp ./target/micros/stm32f429/make/single/crt.cpp ./target/micros/stm32f429/make/single/mcal_gcc_cxx_completion_with_stdlib.cpp -nostartfiles -Wl,--gc-sections -Wl,-Map,./bin/app_benchmark_hasher.map -T ./target/micros/stm32f429/make/stm32f429.ld --specs=nano.specs --specs=nosys.specs -Wl,--print-memory-usage -o ./bin/app_benchmark_hasher.elf
emu_env/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-objcopy ./bin/app_benchmark_hasher.elf -O ihex ./bin/app_benchmark_hasher.hex
ls -la ./bin/app_benchmark_hasher.elf ./bin/app_benchmark_hasher.hex ./bin/app_benchmark_hasher.map
working-directory: ./test/metal/
- name: emulate-target stm32f429
run: |
./emu_env/xpack-qemu-arm-7.1.0-1/bin/qemu-system-gnuarmeclipse --verbose --mcu STM32F429ZI --nographic --gdb tcp::9999 -d unimp,guest_errors &
working-directory: ./test/metal/
- name: run-test-on-target
run: |
./emu_env/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-gdb-py ./bin/app_benchmark_hasher.elf -x ./target/build/test_app_benchmarks_emulator.py
qemu_result=$?
echo "qemu_result" "$qemu_result"
echo "qemu_result" "$qemu_result" | grep 'qemu_result 0'
working-directory: ./test/metal/
8 changes: 6 additions & 2 deletions include/boost/crypt/hash/sha256.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ auto sha256_file_impl(utility::file_reader<block_size>& reader) noexcept -> sha2

} // namespace detail

#ifndef BOOST_CRYPT_DISABLE_IOSTREAM

BOOST_CRYPT_EXPORT inline auto sha256_file(const std::string& filepath) noexcept -> sha256_hasher::return_type
{
try
Expand Down Expand Up @@ -248,7 +250,9 @@ BOOST_CRYPT_EXPORT inline auto sha256_file(const char* filepath) noexcept -> sha
}
}

#ifdef BOOST_CRYPT_HAS_STRING_VIEW
#endif // !BOOST_CRYPT_DISABLE_IOSTREAM

#if (defined(BOOST_CRYPT_HAS_STRING_VIEW) && !defined(BOOST_CRYPT_DISABLE_IOSTREAM))

BOOST_CRYPT_EXPORT inline auto sha256_file(std::string_view filepath) noexcept -> sha256_hasher::return_type
{
Expand All @@ -263,7 +267,7 @@ BOOST_CRYPT_EXPORT inline auto sha256_file(std::string_view filepath) noexcept -
}
}

#endif // BOOST_CRYPT_HAS_STRING_VIEW
#endif // BOOST_CRYPT_HAS_STRING_VIEW && !BOOST_CRYPT_DISABLE_IOSTREAM

#endif // BOOST_CRYPT_HAS_CUDA

Expand Down
55 changes: 55 additions & 0 deletions include/boost/crypt/utility/algorithm.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2024 Matt Borland
// Copyright 2024 Christopher Kormanyos
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#ifndef BOOST_CRYPT_UTILITIES_ALGORITHM_HPP
#define BOOST_CRYPT_UTILITIES_ALGORITHM_HPP

#include <boost/crypt/utility/config.hpp>

namespace boost { namespace crypt {

template<class InputIt1, class InputIt2>
BOOST_CRYPT_GPU_ENABLED constexpr auto equal(InputIt1 first1, InputIt1 last1, InputIt2 first2) -> bool
{
while(first1 != last1)
{
if(!(*first1 == *first2))
{
return false;
}

++first1;
++first2;
}

return true;
}

template<class InputIt1, class InputIt2>
BOOST_CRYPT_GPU_ENABLED constexpr auto lexicographical_compare(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2) -> bool
{
while((first1 != last1) && (first2 != last2))
{
if(*first1 < *first2)
{
return true;
}

if(*first2 < *first1)
{
return false;
}

++first1;
++first2;
}

return ((first1 == last1) && (first2 != last2));
}

} // namespace crypt
} // namespace boost

#endif // BOOST_CRYPT_UTILITIES_ALGORITHM_HPP
37 changes: 37 additions & 0 deletions include/boost/crypt/utility/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define BOOST_CRYPT_UTILITIES_ARRAY_HPP

#include <boost/crypt/utility/config.hpp>
#include <boost/crypt/utility/algorithm.hpp>
#include <boost/crypt/utility/cstdint.hpp>
#include <boost/crypt/utility/cstddef.hpp>
#include <boost/crypt/utility/type_traits.hpp>
Expand Down Expand Up @@ -126,6 +127,42 @@ BOOST_CRYPT_GPU_ENABLED constexpr auto fill_array(ForwardIter first, ForwardIter
}
}

template<typename T, size_t N>
BOOST_CRYPT_GPU_ENABLED constexpr auto operator==(const array<T, N>& left, const array<T, N>& right) -> bool
{
return boost::crypt::equal(left.begin(), left.end(), right.begin());
}

template<typename T, size_t N>
BOOST_CRYPT_GPU_ENABLED constexpr auto operator<(const array<T, N>& left, const array<T, N>& right) -> bool
{
return boost::crypt::lexicographical_compare(left.begin(), left.end(), right.begin(), right.end());
}

template<typename T, size_t N>
BOOST_CRYPT_GPU_ENABLED constexpr auto operator!=(const array<T, N>& left, const array<T, N>& right) -> bool
{
return (!(left == right));
}

template<typename T, size_t N>
BOOST_CRYPT_GPU_ENABLED constexpr auto operator>(const array<T, N>& left, const array<T, N>& right) -> bool
{
return (right < left);
}

template<typename T, size_t N>
BOOST_CRYPT_GPU_ENABLED constexpr auto operator>=(const array<T, N>& left, const array<T, N>& right) -> bool
{
return (!(left < right));
}

template<typename T, size_t N>
BOOST_CRYPT_GPU_ENABLED constexpr auto operator<=(const array<T, N>& left, const array<T, N>& right) -> bool
{
return (!(right < left));
}

template<typename T>
class tuple_size;

Expand Down
1 change: 1 addition & 0 deletions test/metal/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bin
133 changes: 133 additions & 0 deletions test/metal/app_benchmark_hasher.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2023.
// Distributed under the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
#endif

// cd /mnt/c/ChrisGitRepos/cppalliance/crypt/test/metal
// mkdir -p bin
// arm-none-eabi-g++ -std=c++17 -Wall -Wextra -Wpedantic -O0 -g -gdwarf-2 -ffunction-sections -fdata-sections -x c++ -fno-rtti -fno-use-cxa-atexit -fno-exceptions -fno-nonansi-builtins -fno-threadsafe-statics -fno-enforce-eh-specs -ftemplate-depth=128 -mcpu=cortex-m4 -mtune=cortex-m4 -mthumb -mfloat-abi=soft -mno-unaligned-access -mno-long-calls -I../../include -DBOOST_DECIMAL_DISABLE_CLIB -DAPP_BENCHMARK_STANDALONE_MAIN app_benchmark_hasher.cpp ./target/micros/stm32f429/make/single/crt.cpp ./target/micros/stm32f429/make/single/mcal_gcc_cxx_completion_with_stdlib.cpp -nostartfiles -Wl,--gc-sections -Wl,-Map,./bin/app_benchmark_hasher.map -T ./target/micros/stm32f429/make/stm32f429.ld --specs=nano.specs --specs=nosys.specs -Wl,--print-memory-usage -o ./bin/app_benchmark_hasher.elf
// arm-none-eabi-objcopy ./bin/app_benchmark_hasher.elf -O ihex ./bin/app_benchmark_hasher.hex
// ls -la ./bin/app_benchmark_hasher.elf ./bin/app_benchmark_hasher.hex ./bin/app_benchmark_hasher.map

#if !defined(BOOST_CRYPT_STANDALONE)
#define BOOST_DECIMAL_STANDALONE
#endif

#include <boost/crypt/hash/sha256.hpp>

namespace app { namespace benchmark {

namespace detail {

} // namespace detail

auto run_hasher() -> bool;

} // namespace benchmark
} // namespace app

namespace local
{

using hasher_type = boost::crypt::sha256_hasher;

} // namespace local

auto app::benchmark::run_hasher() -> bool
{
auto app_benchmark_result_is_ok = true;

// "abc"
const std::array<std::uint8_t, 3U> message =
{{
0x61U, 0x62U, 0x63U
}};

using local_hasher_type = local::hasher_type;
using local_result_type = typename local_hasher_type::return_type;

const local_result_type control =
{{
0xBAU, 0x78U, 0x16U, 0xBFU, 0x8FU, 0x01U, 0xCFU, 0xEAU,
0x41U, 0x41U, 0x40U, 0xDEU, 0x5DU, 0xAEU, 0x22U, 0x23U,
0xB0U, 0x03U, 0x61U, 0xA3U, 0x96U, 0x17U, 0x7AU, 0x9CU,
0xB4U, 0x10U, 0xFFU, 0x61U, 0xF2U, 0x00U, 0x15U, 0xADU,
}};

local_hasher_type my_hasher { };

my_hasher.init();

my_hasher.process_bytes(message.data(), message.size());

const local_result_type result = my_hasher.get_digest();

const bool result_hash_is_ok { result == control };

app_benchmark_result_is_ok = (result_hash_is_ok && app_benchmark_result_is_ok);

return app_benchmark_result_is_ok;
}

#if defined(APP_BENCHMARK_STANDALONE_MAIN)
constexpr auto app_benchmark_standalone_foodcafe = static_cast<std::uint32_t>(UINT32_C(0xF00DCAFE));

extern "C"
{
extern volatile std::uint32_t app_benchmark_standalone_result;

auto app_benchmark_run_standalone (void) -> bool;
auto app_benchmark_get_standalone_result(void) -> bool;

auto app_benchmark_run_standalone(void) -> bool
{
auto result_is_ok = true;

for(unsigned i = 0U; i < 64U; ++i)
{
result_is_ok &= app::benchmark::run_hasher();
}

app_benchmark_standalone_result =
static_cast<std::uint32_t>
(
result_is_ok ? app_benchmark_standalone_foodcafe : static_cast<std::uint32_t>(UINT32_C(0xFFFFFFFF))
);

return result_is_ok;
}

auto app_benchmark_get_standalone_result(void) -> bool
{
volatile auto result_is_ok = (app_benchmark_standalone_result == static_cast<std::uint32_t>(UINT32_C(0xF00DCAFE)));

return result_is_ok;
}
}

auto main() -> int
{
auto result_is_ok = true;

result_is_ok = (::app_benchmark_run_standalone () && result_is_ok);
result_is_ok = (::app_benchmark_get_standalone_result() && result_is_ok);

return (result_is_ok ? 0 : -1);
}

extern "C"
{
volatile std::uint32_t app_benchmark_standalone_result;
}
#endif // APP_BENCHMARK_STANDALONE_MAIN

#if defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
Loading

0 comments on commit ae56059

Please sign in to comment.