Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Commit bbca22e

Browse files
committed
Initial state preprocessing refactoring #3
1 parent de4b2a2 commit bbca22e

26 files changed

+405
-72
lines changed

CMakeLists.txt

-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ if(NOT Boost_CONTAINER_FOUND OR NOT Boost_FOUND)
1717
cm_find_package(Boost COMPONENTS container)
1818
endif()
1919

20-
cm_find_package(${CMAKE_WORKSPACE_NAME}_concept_container)
2120
cm_find_package(${CMAKE_WORKSPACE_NAME}_utilities)
2221

2322
list(APPEND ${CURRENT_PROJECT_NAME}_PUBLIC_HEADERS
@@ -478,8 +477,6 @@ set_target_properties(${CMAKE_WORKSPACE_NAME}_${CURRENT_PROJECT_NAME} PROPERTIES
478477
target_link_libraries(${CMAKE_WORKSPACE_NAME}_${CURRENT_PROJECT_NAME} PUBLIC
479478
${CMAKE_WORKSPACE_NAME}::utilities
480479

481-
concept_container
482-
483480
${Boost_LIBRARIES})
484481

485482
target_include_directories(${CMAKE_WORKSPACE_NAME}_${CURRENT_PROJECT_NAME} PRIVATE
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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

include/nil/crypto3/block/aria.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#include <nil/crypto3/block/detail/aria/aria_policy.hpp>
1414

15-
#include <nil/crypto3/block/cipher_state_preprocessor.hpp>
15+
#include <nil/crypto3/block/block_state_preprocessor.hpp>
1616
#include <nil/crypto3/block/detail/stream_endian.hpp>
1717

1818
#include <nil/crypto3/block/detail/utilities/cpuid/cpuid.hpp>
@@ -60,7 +60,7 @@ namespace nil {
6060

6161
template<template<typename, typename> class Mode, std::size_t ValueBits, typename Padding>
6262
struct stream_cipher {
63-
typedef cipher_state<Mode<aria<Size>, Padding>, stream_endian::little_octet_big_bit, ValueBits,
63+
typedef block_state_preprocessor<Mode<aria<Size>, Padding>, stream_endian::little_octet_big_bit, ValueBits,
6464
policy_type::word_bits * 2> type_;
6565
#ifdef CRYPTO3_HASH_NO_HIDE_INTERNAL_TYPES
6666
typedef type_ type;

include/nil/crypto3/block/blowfish.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
#include <nil/crypto3/block/block_cipher.hpp>
1616

17-
#include <nil/crypto3/block/cipher_state_preprocessor.hpp>
17+
#include <nil/crypto3/block/detail/block_state_preprocessor.hpp>
1818
#include <nil/crypto3/block/detail/stream_endian.hpp>
1919

2020
namespace nil {
@@ -51,8 +51,8 @@ namespace nil {
5151

5252
template<template<typename, typename> class Mode, std::size_t ValueBits, typename Padding>
5353
struct stream_cipher {
54-
typedef cipher_state<Mode<blowfish, Padding>, stream_endian::little_octet_big_bit, ValueBits,
55-
policy_type::word_bits * 2> type_;
54+
typedef block_state_preprocessor<Mode<blowfish, Padding>, stream_endian::little_octet_big_bit,
55+
ValueBits, policy_type::word_bits * 2> type_;
5656
#ifdef CRYPTO3_HASH_NO_HIDE_INTERNAL_TYPES
5757
typedef type_ type;
5858
#else

include/nil/crypto3/block/camellia.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#include <nil/crypto3/block/detail/camellia/camellia_policy.hpp>
1414

15-
#include <nil/crypto3/block/cipher_state_preprocessor.hpp>
15+
#include <nil/crypto3/block/detail/block_state_preprocessor.hpp>
1616
#include <nil/crypto3/block/detail/stream_endian.hpp>
1717

1818
namespace nil {
@@ -51,8 +51,8 @@ namespace nil {
5151

5252
template<template<typename, typename> class Mode, std::size_t ValueBits, typename Padding>
5353
struct stream_cipher {
54-
typedef cipher_state<Mode<camellia<Size>, Padding>, stream_endian::little_octet_big_bit, ValueBits,
55-
policy_type::word_bits * 2> type_;
54+
typedef block_state_preprocessor<Mode<camellia<Size>, Padding>, stream_endian::little_octet_big_bit,
55+
ValueBits, policy_type::word_bits * 2> type_;
5656
#ifdef CRYPTO3_HASH_NO_HIDE_INTERNAL_TYPES
5757
typedef type_ type;
5858
#else

include/nil/crypto3/block/cast.hpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#include <nil/crypto3/block/detail/cast/cast_policy.hpp>
1414

15-
#include <nil/crypto3/block/cipher_state_preprocessor.hpp>
15+
#include <nil/crypto3/block/detail/block_state_preprocessor.hpp>
1616
#include <nil/crypto3/block/detail/stream_endian.hpp>
1717

1818
namespace nil {
@@ -54,8 +54,9 @@ namespace nil {
5454

5555
template<template<typename, typename> class Mode, std::size_t ValueBits, typename Padding>
5656
struct stream_cipher {
57-
typedef cipher_state<Mode<cast<Version, KeyBits>, Padding>, stream_endian::little_octet_big_bit,
58-
ValueBits, policy_type::word_bits * 2> type_;
57+
typedef block_state_preprocessor<Mode<cast<Version, KeyBits>, Padding>,
58+
stream_endian::little_octet_big_bit, ValueBits,
59+
policy_type::word_bits * 2> type_;
5960
#ifdef CRYPTO3_HASH_NO_HIDE_INTERNAL_TYPES
6061
typedef type_ type;
6162
#else

include/nil/crypto3/block/cipher_state.hpp

+11-7
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
#ifndef CRYPTO3_CIPHER_STATE_PREPROCESSOR_HPP
1111
#define CRYPTO3_CIPHER_STATE_PREPROCESSOR_HPP
1212

13-
#include <nil/concept_container/cached_concept_container.hpp>
13+
#include <boost/accumulators/framework/accumulator_set.hpp>
1414

15-
#include <nil/crypto3/block/cipher_state_preprocessor.hpp>
15+
#include <nil/crypto3/block/detail/cipher_state_preprocessor.hpp>
16+
#include <nil/crypto3/block/detail/block_state_preprocessor.hpp>
1617

1718
namespace nil {
1819
namespace crypto3 {
@@ -27,13 +28,16 @@ namespace nil {
2728
* @tparam ValueBits
2829
* @tparam LengthBits
2930
*/
30-
template<typename Mode, typename Endian, std::size_t ValueBits, std::size_t LengthBits> struct cipher_state
31-
: public cached_concept_container<Mode, cipher_state_preprocessor < Mode, Endian, ValueBits,
32-
LengthBits>> {
31+
template<typename ProcessingMode>
32+
struct codec_accumulator {
33+
typedef boost::accumulators::accumulator_set <block::digest<
34+
ProcessingMode::input_block_bits>, boost::accumulators::features<
35+
accumulators::tag::codec < ProcessingMode>>> type;
3336

34-
};
37+
typedef ProcessingMode mode_type;
38+
};
39+
}
3540
}
3641
}
37-
}
3842

3943
#endif //CRYPTO3_CIPHER_STATE_PREPROCESSOR_HPP

include/nil/crypto3/block/des.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#include <nil/crypto3/block/detail/des/des_functions.hpp>
1414

15-
#include <nil/crypto3/block/cipher_state_preprocessor.hpp>
15+
#include <nil/crypto3/block/detail/block_state_preprocessor.hpp>
1616
#include <nil/crypto3/block/detail/stream_endian.hpp>
1717

1818
namespace nil {

0 commit comments

Comments
 (0)