|
| 1 | +//---------------------------------------------------------------------------// |
| 2 | +// Copyright (c) 2018-2019 Nil Foundation |
| 3 | +// Copyright (c) 2018-2019 Mikhail Komarov <[email protected]> |
| 4 | +// |
| 5 | +// Distributed under the Boost Software License, Version 1.0 |
| 6 | +// See accompanying file LICENSE_1_0.txt or copy at |
| 7 | +// http://www.boost.org/LICENSE_1_0.txt |
| 8 | +//---------------------------------------------------------------------------// |
| 9 | + |
| 10 | +#ifndef CRYPTO3_ACCUMULATORS_BLOCK_HPP |
| 11 | +#define CRYPTO3_ACCUMULATORS_BLOCK_HPP |
| 12 | + |
| 13 | +#include <boost/container/static_vector.hpp> |
| 14 | + |
| 15 | +#include <boost/parameter/value_type.hpp> |
| 16 | + |
| 17 | +#include <boost/accumulators/framework/accumulator_base.hpp> |
| 18 | +#include <boost/accumulators/framework/extractor.hpp> |
| 19 | +#include <boost/accumulators/framework/depends_on.hpp> |
| 20 | +#include <boost/accumulators/framework/parameters/sample.hpp> |
| 21 | + |
| 22 | +#include <nil/crypto3/block/detail/make_array.hpp> |
| 23 | +#include <nil/crypto3/block/detail/digest.hpp> |
| 24 | + |
| 25 | +namespace nil { |
| 26 | + namespace crypto3 { |
| 27 | + namespace accumulators { |
| 28 | + namespace impl { |
| 29 | + template<typename Mode> |
| 30 | + struct block_impl : boost::accumulators::accumulator_base { |
| 31 | + protected: |
| 32 | + typedef Mode mode_type; |
| 33 | + typedef typename mode_type::finalizer_type finalizer_type; |
| 34 | + |
| 35 | + typedef typename mode_type::input_block_type input_block_type; |
| 36 | + typedef typename input_block_type::value_type input_value_type; |
| 37 | + constexpr static const std::size_t input_block_bits = mode_type::input_block_bits; |
| 38 | + constexpr static const std::size_t input_value_bits = |
| 39 | + input_block_bits / std::tuple_size<input_block_type>::value; |
| 40 | + |
| 41 | + typedef typename mode_type::output_block_type output_block_type; |
| 42 | + typedef typename output_block_type::value_type output_value_type; |
| 43 | + constexpr static const std::size_t output_block_bits = mode_type::output_block_bits; |
| 44 | + constexpr static const std::size_t output_value_bits = |
| 45 | + output_block_bits / std::tuple_size<output_block_type>::value; |
| 46 | + |
| 47 | + typedef boost::container::static_vector<input_value_type, |
| 48 | + std::tuple_size<input_block_type>::value> cache_type; |
| 49 | + |
| 50 | + public: |
| 51 | + typedef block::digest<output_block_bits> result_type; |
| 52 | + |
| 53 | + template<typename Args> |
| 54 | + // The constructor takes an argument pack. |
| 55 | + block_impl(const Args &args) : seen(0) { |
| 56 | + } |
| 57 | + |
| 58 | + template<typename ArgumentPack> |
| 59 | + inline void operator()(const ArgumentPack &args) { |
| 60 | + return process(args[boost::accumulators::sample]); |
| 61 | + } |
| 62 | + |
| 63 | + template<typename ArgumentPack> |
| 64 | + inline result_type result(const ArgumentPack &args) const { |
| 65 | + result_type res = digest; |
| 66 | + |
| 67 | + if (!cache.empty()) { |
| 68 | + input_block_type ib = {0}; |
| 69 | + std::move(cache.begin(), cache.end(), ib.begin()); |
| 70 | + output_block_type ob = mode_type::process_block(ib); |
| 71 | + std::move(ob.begin(), ob.end(), std::inserter(res, res.end())); |
| 72 | + } |
| 73 | + |
| 74 | + if (seen % input_block_bits) { |
| 75 | + finalizer_type(input_block_bits - seen % input_block_bits)(res); |
| 76 | + } else { |
| 77 | + finalizer_type(0)(res); |
| 78 | + } |
| 79 | + |
| 80 | + return res; |
| 81 | + } |
| 82 | + |
| 83 | + protected: |
| 84 | + |
| 85 | + inline void process(const input_value_type &value) { |
| 86 | + if (cache.size() == cache.max_size()) { |
| 87 | + input_block_type ib = {0}; |
| 88 | + std::move(cache.begin(), cache.end(), ib.begin()); |
| 89 | + output_block_type ob = mode_type::process_block(ib); |
| 90 | + std::move(ob.begin(), ob.end(), std::inserter(digest, digest.end())); |
| 91 | + |
| 92 | + cache.clear(); |
| 93 | + } |
| 94 | + |
| 95 | + cache.push_back(value); |
| 96 | + seen += input_value_bits; |
| 97 | + } |
| 98 | + |
| 99 | + inline void process(const input_block_type &block) { |
| 100 | + output_block_type ob; |
| 101 | + if (cache.empty()) { |
| 102 | + ob = mode_type::process_block(block); |
| 103 | + } else { |
| 104 | + input_block_type b = block::make_array<std::tuple_size<input_block_type>::value>( |
| 105 | + cache.begin(), cache.end()); |
| 106 | + typename input_block_type::const_iterator itr = |
| 107 | + block.begin() + (cache.max_size() - cache.size()); |
| 108 | + |
| 109 | + std::copy(block.begin(), itr, b.end()); |
| 110 | + |
| 111 | + ob = mode_type::process_block(block); |
| 112 | + |
| 113 | + cache.clear(); |
| 114 | + cache.insert(cache.end(), itr, block.end()); |
| 115 | + } |
| 116 | + |
| 117 | + std::move(ob.begin(), ob.end(), std::inserter(digest, digest.end())); |
| 118 | + seen += input_block_bits; |
| 119 | + } |
| 120 | + |
| 121 | + std::size_t seen; |
| 122 | + cache_type cache; |
| 123 | + result_type digest; |
| 124 | + }; |
| 125 | + } |
| 126 | + |
| 127 | + namespace tag { |
| 128 | + template<typename Mode> |
| 129 | + struct block : boost::accumulators::depends_on<> { |
| 130 | + typedef Mode mode_type; |
| 131 | + |
| 132 | + /// INTERNAL ONLY |
| 133 | + /// |
| 134 | + |
| 135 | + typedef boost::mpl::always<accumulators::impl::block_impl<Mode>> impl; |
| 136 | + }; |
| 137 | + } |
| 138 | + |
| 139 | + namespace extract { |
| 140 | + template<typename Mode, typename AccumulatorSet> |
| 141 | + typename boost::mpl::apply<AccumulatorSet, tag::block<Mode> >::type::result_type block( |
| 142 | + const AccumulatorSet &acc) { |
| 143 | + return boost::accumulators::extract_result<tag::block<Mode> >(acc); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +#endif //CRYPTO3_ACCUMULATORS_BLOCK_HPP |
0 commit comments