37 template<
bool...>
struct bool_pack;
39 using all_true = std::is_same< bool_pack<bs...,
true>, bool_pack<
true, bs...> >;
41 template<
typename Word,
size_t NumWords>
42 static void set_from_word_sequence(
const std::array<Word, NumWords>&
arr,
fixed_key<Size>& key)
44 auto itr = key._data.begin();
46 const size_t sub_word_shift = 8 *
sizeof(Word);
47 const size_t num_sub_words =
sizeof(
word_t) /
sizeof(Word);
48 auto sub_words_left = num_sub_words;
49 for(
auto&& w :
arr ) {
50 if( sub_words_left > 1 ) {
51 temp_word |=
static_cast<word_t>(w);
52 temp_word <<= sub_word_shift;
57 SYS_ASSERT( sub_words_left == 1, chain::fixed_key_type_exception,
"unexpected error in fixed_key constructor" );
58 temp_word |=
static_cast<word_t>(w);
59 sub_words_left = num_sub_words;
65 if( sub_words_left != num_sub_words ) {
66 if( sub_words_left > 1 )
67 temp_word <<= 8 * (sub_words_left-1);
94 std::copy(
arr.begin(),
arr.end(), _data.begin());
97 template<
typename Word,
size_t NumWords,
98 typename Enable =
typename std::enable_if<std::is_integral<Word>::value &&
99 !std::is_same<Word, bool>::value &&
100 sizeof(Word) <
sizeof(
word_t)>::type >
103 static_assert(
sizeof(
word_t) == (
sizeof(
word_t)/
sizeof(Word)) *
sizeof(Word),
104 "size of the backing word size is not divisible by the size of the array element" );
105 static_assert(
sizeof(Word) * NumWords <= Size,
"too many words supplied to fixed_key constructor" );
107 set_from_word_sequence(
arr, *
this);
110 template<
typename FirstWord,
typename... Rest>
113 make_from_word_sequence(
typename std::enable_if<std::is_integral<FirstWord>::value &&
114 !std::is_same<FirstWord, bool>::value &&
115 sizeof(FirstWord) <=
sizeof(
word_t) &&
116 all_true<(std::is_same<FirstWord, Rest>::value)...>
::value,
117 FirstWord>::type first_word,
120 static_assert(
sizeof(
word_t) == (
sizeof(
word_t)/
sizeof(FirstWord)) *
sizeof(FirstWord),
121 "size of the backing word size is not divisible by the size of the words supplied as arguments" );
122 static_assert(
sizeof(FirstWord) * (1 +
sizeof...(Rest)) <= Size,
"too many words supplied to make_from_word_sequence" );
125 set_from_word_sequence(std::array<FirstWord, 1+
sizeof...(Rest)>{{ first_word, rest... }},
key);
129 const auto& get_array()
const {
return _data; }
131 auto data() {
return _data.data(); }
132 auto data()
const {
return _data.data(); }
134 auto size()
const {
return _data.size(); }
136 std::array<uint8_t, Size> extract_as_byte_array()
const {
137 std::array<uint8_t, Size>
arr;
139 const size_t num_sub_words =
sizeof(
word_t);
142 auto data_itr = _data.begin();
144 for(
size_t counter = _data.size(); counter > 0; --counter, ++data_itr ) {
145 size_t sub_words_left = num_sub_words;
150 auto temp_word = *data_itr;
151 for( ; sub_words_left > 0; --sub_words_left ) {
152 *(
arr_itr + sub_words_left - 1) =
static_cast<uint8_t>(temp_word & 0xFF);