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

Commit 5e4361a

Browse files
author
Ilias Khairullin
committed
Rfc6979 generator fixed. #3
1 parent d8040fd commit 5e4361a

File tree

1 file changed

+16
-45
lines changed

1 file changed

+16
-45
lines changed

include/nil/crypto3/random/rfc6979.hpp

+16-45
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ namespace nil {
124124
// TODO: local_char_bits is supposed to equal chunk_size from import_bits call in marshalling
125125
constexpr std::size_t local_char_bits = 8;
126126
constexpr std::size_t adjustment_shift = modulus_octets * local_char_bits - modulus_bits;
127-
constexpr std::size_t bits_remain = local_char_bits - adjustment_shift;
128127
constexpr std::size_t chunk_size = std::numeric_limits<ValueType>::digits;
129128
using bitset_repr_type = std::bitset<chunk_size>;
130129

@@ -158,12 +157,9 @@ namespace nil {
158157
return result;
159158
}
160159

161-
template<
162-
typename InputRange,
163-
typename std::enable_if<
164-
std::is_same<std::uint8_t,
165-
typename std::iterator_traits<typename InputRange::iterator>::value_type>::value,
166-
bool>::type = true>
160+
template<typename InputRange,
161+
typename ValueType = typename std::iterator_traits<typename InputRange::iterator>::value_type,
162+
typename std::enable_if<std::is_same<std::uint8_t, ValueType>::value, bool>::type = true>
167163
static inline integral_type bits2int(const InputRange& range) {
168164
integral_type result;
169165
if (modulus_bits < range.size() * 8) {
@@ -173,11 +169,17 @@ namespace nil {
173169
marshalling_integral_value_be.template read(it, modulus_octets);
174170
result = marshalling_integral_value_be.value();
175171
} else {
176-
// TODO: check correctness of this case
177-
marshalling_integral_value_le_type marshalling_integral_value_le;
178-
auto it = range.crbegin();
179-
marshalling_integral_value_le.template read(it, range.size());
180-
result = marshalling_integral_value_le.value();
172+
// TODO: creating copy of input range of modulus_octets size is a bottleneck:
173+
// extend marshaling interface by function supporting initialization from container which
174+
// length is less than modulus_octets
175+
// TODO: check need for adjust_bitstring call
176+
modulus_octets_container_type range_padded;
177+
range_padded.fill(0);
178+
std::copy(std::crbegin(range), std::crend(range), std::rbegin(range_padded));
179+
marshalling_integral_value_be_type marshalling_integral_value_be;
180+
auto it = std::cbegin(range_padded);
181+
marshalling_integral_value_be.template read(it, range_padded.size());
182+
result = marshalling_integral_value_be.value();
181183
}
182184
return result;
183185
}
@@ -203,7 +205,8 @@ namespace nil {
203205
compute<hmac_policy>(std::array<std::uint8_t, 1> {0}, acc_d);
204206
compute<hmac_policy>(int2octets_x, acc_d);
205207
compute<hmac_policy>(bits2octets_h1, acc_d);
206-
Key = key_type(::nil::crypto3::accumulators::extract::mac<mac::computation_policy<hmac_policy>>(acc_d));
208+
Key = key_type(
209+
::nil::crypto3::accumulators::extract::mac<mac::computation_policy<hmac_policy>>(acc_d));
207210

208211
// e.
209212
V = compute<hmac_policy>(V, Key);
@@ -249,38 +252,6 @@ namespace nil {
249252
} while (true);
250253
}
251254

252-
// inline void discard(std::size_t n) {
253-
// if (n > 0 && !cached) {
254-
// operator()();
255-
// }
256-
// }
257-
//
258-
// inline bool operator==(const hash& other) const {
259-
// return state == other.state;
260-
// }
261-
//
262-
// inline bool operator!=(const hash& other) const {
263-
// return !(*this == other);
264-
// }
265-
//
266-
// /** Writes a rfc6979 to a @c std::ostream */
267-
// template<class CharT, class Traits>
268-
// friend std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
269-
// const algebraic_engine& e) {
270-
// os << e.state;
271-
// return os;
272-
// }
273-
//
274-
// /** Reads a rfc6979 from a @c std::istream */
275-
// template<class CharT, class Traits>
276-
// friend std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits>& is,
277-
// algebraic_engine& e) {
278-
// input_type x;
279-
// is >> x;
280-
// e.seed(x);
281-
// return is;
282-
// }
283-
284255
protected:
285256
digest_type V;
286257
digest_type K;

0 commit comments

Comments
 (0)