From afc0e0070001ca230778c7eea6b0085fd4979645 Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Thu, 19 Sep 2019 16:08:19 +0200 Subject: [PATCH] Added zero_initialized_array template to restore functionality of fc::array --- include/fc/container/zeroed_array.hpp | 72 +++++++++++++++++++++++++++ include/fc/crypto/elliptic.hpp | 19 +++---- src/crypto/elliptic_common.cpp | 2 +- src/crypto/elliptic_secp256k1.cpp | 2 +- 4 files changed, 84 insertions(+), 11 deletions(-) create mode 100644 include/fc/container/zeroed_array.hpp diff --git a/include/fc/container/zeroed_array.hpp b/include/fc/container/zeroed_array.hpp new file mode 100644 index 0000000..a51abec --- /dev/null +++ b/include/fc/container/zeroed_array.hpp @@ -0,0 +1,72 @@ +#pragma once +/* + * Copyright (c) 2019 BitShares Blockchain Foundation, and contributors + * + * The MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#include + +namespace fc { + + template< typename T, size_t N > + class zero_initialized_array; + + template< size_t N > + class zero_initialized_array< unsigned char, N > : public std::array< unsigned char, N > { + public: + zero_initialized_array() : std::array< unsigned char, N >() { } + }; + + template + struct get_typename< zero_initialized_array > + { + static const char* name() + { + static std::string _name = std::string("zero_initialized_array<") + + std::string(fc::get_typename::name()) + + "," + fc::to_string(N) + ">"; + return _name.c_str(); + } + }; + + class variant; + template + void to_variant( const zero_initialized_array& bi, variant& v, uint32_t max_depth = 1 ) + { + to_variant( static_cast&>( bi ), v, max_depth ); + } + template + void from_variant( const variant& v, zero_initialized_array& bi, uint32_t max_depth = 1 ) + { + from_variant( v, static_cast&>( bi ), max_depth ); + } + + namespace raw { + template + inline void pack( Stream& s, const zero_initialized_array& v, uint32_t _max_depth ) { + pack( s, static_cast&>( v ), _max_depth ); + } + template + inline void unpack( Stream& s, zero_initialized_array& v, uint32_t _max_depth ) { try { + unpack( s, static_cast&>( v ), _max_depth ); + } FC_RETHROW_EXCEPTIONS( warn, "zero_initialized_array", ("length",N) ) } + } +} diff --git a/include/fc/crypto/elliptic.hpp b/include/fc/crypto/elliptic.hpp index 3784299..e47dee6 100644 --- a/include/fc/crypto/elliptic.hpp +++ b/include/fc/crypto/elliptic.hpp @@ -1,4 +1,5 @@ #pragma once +#include #include #include #include @@ -17,15 +18,15 @@ namespace fc { class private_key_impl; } - typedef fc::sha256 blind_factor_type; - typedef std::array commitment_type; - typedef std::array public_key_data; - typedef fc::sha256 private_key_secret; - typedef std::array public_key_point_data; ///< the full non-compressed version of the ECC point - typedef std::array signature; - typedef std::array compact_signature; - typedef std::vector range_proof_type; - typedef std::array extended_key_data; + typedef fc::sha256 blind_factor_type; + typedef zero_initialized_array commitment_type; + typedef zero_initialized_array public_key_data; + typedef fc::sha256 private_key_secret; + typedef zero_initialized_array public_key_point_data; ///< the full non-compressed version of the ECC point + typedef zero_initialized_array signature; + typedef zero_initialized_array compact_signature; + typedef std::vector range_proof_type; + typedef zero_initialized_array extended_key_data; /** * @class public_key diff --git a/src/crypto/elliptic_common.cpp b/src/crypto/elliptic_common.cpp index dd89c3a..2ed84a8 100644 --- a/src/crypto/elliptic_common.cpp +++ b/src/crypto/elliptic_common.cpp @@ -19,7 +19,7 @@ namespace fc { namespace ecc { namespace detail { - typedef std::array chr37; + typedef zero_initialized_array chr37; fc::sha256 _left( const fc::sha512& v ) { diff --git a/src/crypto/elliptic_secp256k1.cpp b/src/crypto/elliptic_secp256k1.cpp index b180d44..477077e 100644 --- a/src/crypto/elliptic_secp256k1.cpp +++ b/src/crypto/elliptic_secp256k1.cpp @@ -53,7 +53,7 @@ namespace fc { namespace ecc { public_key_data _key; }; - typedef std::array chr37; + typedef zero_initialized_array chr37; chr37 _derive_message( const public_key_data& key, int i ); fc::sha256 _left( const fc::sha512& v ); fc::sha256 _right( const fc::sha512& v );