Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
fixed_key.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <algorithm>
5#include <type_traits>
6
9namespace sysio {
10
11 using chain::uint128_t;
12
13 template<size_t Size>
14 class fixed_key;
15
16 template<size_t Size>
17 bool operator==(const fixed_key<Size> &c1, const fixed_key<Size> &c2);
18
19 template<size_t Size>
20 bool operator!=(const fixed_key<Size> &c1, const fixed_key<Size> &c2);
21
22 template<size_t Size>
23 bool operator>(const fixed_key<Size> &c1, const fixed_key<Size> &c2);
24
25 template<size_t Size>
26 bool operator<(const fixed_key<Size> &c1, const fixed_key<Size> &c2);
27
33 template<size_t Size>
34 class fixed_key {
35 private:
36
37 template<bool...> struct bool_pack;
38 template<bool... bs>
39 using all_true = std::is_same< bool_pack<bs..., true>, bool_pack<true, bs...> >;
40
41 template<typename Word, size_t NumWords>
42 static void set_from_word_sequence(const std::array<Word, NumWords>& arr, fixed_key<Size>& key)
43 {
44 auto itr = key._data.begin();
45 word_t temp_word = 0;
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;
53 --sub_words_left;
54 continue;
55 }
56
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;
60
61 *itr = temp_word;
62 temp_word = 0;
63 ++itr;
64 }
65 if( sub_words_left != num_sub_words ) {
66 if( sub_words_left > 1 )
67 temp_word <<= 8 * (sub_words_left-1);
68 *itr = temp_word;
69 }
70 }
71
72 public:
73
75
76 static constexpr size_t num_words() { return (Size + sizeof(word_t) - 1) / sizeof(word_t); }
77 static constexpr size_t padded_bytes() { return num_words() * sizeof(word_t) - Size; }
78
84 fixed_key() : _data() {}
85
92 fixed_key(const std::array<word_t, num_words()>& arr)
93 {
94 std::copy(arr.begin(), arr.end(), _data.begin());
95 }
96
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 >
101 fixed_key(const std::array<Word, NumWords>& arr)
102 {
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" );
106
107 set_from_word_sequence(arr, *this);
108 }
109
110 template<typename FirstWord, typename... Rest>
111 static
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,
118 Rest... rest)
119 {
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" );
123
125 set_from_word_sequence(std::array<FirstWord, 1+sizeof...(Rest)>{{ first_word, rest... }}, key);
126 return key;
127 }
128
129 const auto& get_array()const { return _data; }
130
131 auto data() { return _data.data(); }
132 auto data()const { return _data.data(); }
133
134 auto size()const { return _data.size(); }
135
136 std::array<uint8_t, Size> extract_as_byte_array()const {
137 std::array<uint8_t, Size> arr;
138
139 const size_t num_sub_words = sizeof(word_t);
140
141 auto arr_itr = arr.begin();
142 auto data_itr = _data.begin();
143
144 for( size_t counter = _data.size(); counter > 0; --counter, ++data_itr ) {
145 size_t sub_words_left = num_sub_words;
146
147 if( counter == 1 ) { // If last word in _data array...
148 sub_words_left -= padded_bytes();
149 }
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);
153 temp_word >>= 8;
154 }
155 arr_itr += num_sub_words;
156 }
157
158 return arr;
159 }
160
161 // Comparison operators
162 friend bool operator== <>(const fixed_key<Size> &c1, const fixed_key<Size> &c2);
163
164 friend bool operator!= <>(const fixed_key<Size> &c1, const fixed_key<Size> &c2);
165
166 friend bool operator> <>(const fixed_key<Size> &c1, const fixed_key<Size> &c2);
167
168 friend bool operator< <>(const fixed_key<Size> &c1, const fixed_key<Size> &c2);
169
170 private:
171
172 std::array<word_t, num_words()> _data;
173 };
174
181 template<size_t Size>
182 bool operator==(const fixed_key<Size> &c1, const fixed_key<Size> &c2) {
183 return c1._data == c2._data;
184 }
185
192 template<size_t Size>
193 bool operator!=(const fixed_key<Size> &c1, const fixed_key<Size> &c2) {
194 return c1._data != c2._data;
195 }
196
203 template<size_t Size>
204 bool operator>(const fixed_key<Size> &c1, const fixed_key<Size> &c2) {
205 return c1._data > c2._data;
206 }
207
214 template<size_t Size>
215 bool operator<(const fixed_key<Size> &c1, const fixed_key<Size> &c2) {
216 return c1._data < c2._data;
217 }
219
221}
#define SYS_ASSERT(expr, exc_type, FORMAT,...)
Definition exceptions.hpp:7
friend bool operator>(const fixed_key< Size > &c1, const fixed_key< Size > &c2)
Compares two fixed_key variables c1 and c2.
fixed_key(const std::array< word_t, num_words()> &arr)
Constructor to fixed_key object from std::array of num_words() words.
Definition fixed_key.hpp:92
fixed_key()
Default constructor to fixed_key object.
Definition fixed_key.hpp:84
friend bool operator==(const fixed_key< Size > &c1, const fixed_key< Size > &c2)
Compares two fixed_key variables c1 and c2.
uint128_t word_t
Definition fixed_key.hpp:74
static constexpr size_t padded_bytes()
Definition fixed_key.hpp:77
friend bool operator!=(const fixed_key< Size > &c1, const fixed_key< Size > &c2)
Compares two fixed_key variables c1 and c2.
static constexpr size_t num_words()
Definition fixed_key.hpp:76
Defines exception's used by fc.
__uint128_t uint128_t
Definition config.hpp:8
bool operator<(const permission_level &lhs, const permission_level &rhs)
Definition action.hpp:21
bool operator==(const permission_level &lhs, const permission_level &rhs)
Definition action.hpp:13
bool operator!=(const permission_level &lhs, const permission_level &rhs)
Definition action.hpp:17
bool operator>(const permission_level &lhs, const permission_level &rhs)
Definition action.hpp:29
unsigned __int128 uint128_t
Definition types.hpp:242
fixed_key< 32 > key256
#define value
Definition pkcs11.h:157
unsigned char uint8_t
Definition stdint.h:124
uint8_t key[16]
Definition yubico_otp.c:41