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

Commit d8040fd

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

File tree

2 files changed

+35
-40
lines changed

2 files changed

+35
-40
lines changed

include/nil/crypto3/random/rfc6979.hpp

+28-39
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ namespace nil {
172172
auto it = adjusted_range.cbegin();
173173
marshalling_integral_value_be.template read(it, modulus_octets);
174174
result = marshalling_integral_value_be.value();
175-
std::cout << result << std::endl;
176175
} else {
177176
// TODO: check correctness of this case
178177
marshalling_integral_value_le_type marshalling_integral_value_le;
@@ -184,79 +183,69 @@ namespace nil {
184183
}
185184

186185
template<typename InputRange>
187-
static inline modulus_octets_container_type bits2octets(const InputRange &range) {
188-
field_value_type z2(bits2int(range));
189-
return int2octets(z2);
186+
static inline modulus_octets_container_type bits2octets(const InputRange& range) {
187+
return int2octets(field_value_type(bits2int(range)));
190188
}
191189

192190
inline void seed(const result_type& x, const digest_type& h1) {
193191
// b.
194192
std::fill(V.begin(), V.end(), 1);
195193

196194
// c.
197-
digest_type K;
198195
std::fill(K.begin(), K.end(), 0);
199-
Key = key_type(K);
196+
key_type Key(K);
200197

201198
// d.
202199
internal_accumulator_type acc_d(Key);
200+
auto int2octets_x = int2octets(x);
201+
auto bits2octets_h1 = bits2octets(h1);
203202
compute<hmac_policy>(V, acc_d);
204203
compute<hmac_policy>(std::array<std::uint8_t, 1> {0}, acc_d);
205-
// int2octets(x)
206-
marshalling_field_element_be_type marshalling_field_element =
207-
::nil::crypto3::marshalling::types::fill_field_element<field_type,
208-
::nil::marshalling::option::big_endian>(
209-
x);
210-
modulus_octets_container_type modulus_octet_container;
211-
marshalling_field_element.template write(modulus_octet_container.begin(), modulus_octets);
212-
compute<hmac_policy>(modulus_octet_container, acc_d);
213-
compute<hmac_policy>(h1, acc_d);
214-
Key = key_type(
215-
::nil::crypto3::accumulators::extract::mac<mac::computation_policy<hmac_policy>>(acc_d));
204+
compute<hmac_policy>(int2octets_x, acc_d);
205+
compute<hmac_policy>(bits2octets_h1, acc_d);
206+
Key = key_type(::nil::crypto3::accumulators::extract::mac<mac::computation_policy<hmac_policy>>(acc_d));
216207

217208
// e.
218-
internal_accumulator_type acc_e(Key);
219-
compute<hmac_policy>(V, acc_e);
220-
V = ::nil::crypto3::accumulators::extract::mac<mac::computation_policy<hmac_policy>>(acc_e);
209+
V = compute<hmac_policy>(V, Key);
221210

222211
// f.
223212
internal_accumulator_type acc_f(Key);
224213
compute<hmac_policy>(V, acc_f);
225214
compute<hmac_policy>(std::array<std::uint8_t, 1> {1}, acc_f);
226-
compute<hmac_policy>(modulus_octet_container, acc_f);
227-
compute<hmac_policy>(h1, acc_f);
228-
Key = key_type(
229-
::nil::crypto3::accumulators::extract::mac<mac::computation_policy<hmac_policy>>(acc_f));
215+
compute<hmac_policy>(int2octets_x, acc_f);
216+
compute<hmac_policy>(bits2octets_h1, acc_f);
217+
K = ::nil::crypto3::accumulators::extract::mac<mac::computation_policy<hmac_policy>>(acc_f);
218+
Key = key_type(K);
230219

231220
// g.
232-
internal_accumulator_type acc_g(Key);
233-
compute<hmac_policy>(V, acc_g);
234-
V = ::nil::crypto3::accumulators::extract::mac<mac::computation_policy<hmac_policy>>(acc_g);
221+
V = compute<hmac_policy>(V, Key);
235222
}
236223

237224
inline result_type operator()() {
238225
// h.
239226
do {
240-
std::vector<std::uint8_t> T;
227+
std::array<std::uint8_t, digest_chunks * digest_octets> T;
241228

242229
// h.2.
230+
key_type Key(K);
243231
for (auto i = 0; i < digest_chunks; i++) {
244-
digest_type T_temp = compute<hmac_policy>(V, Key);
245-
std::copy(T_temp.cbegin(), T_temp.cend(), std::back_inserter(T));
232+
V = compute<hmac_policy>(V, Key);
233+
std::copy(V.cbegin(), V.cend(), T.begin() + i * digest_octets);
246234
}
247235

248236
// h.3.
249-
// TODO: use marshalling
250-
integral_type k;
251-
::nil::crypto3::multiprecision::import_bits(k, T.begin(), T.begin() + modulus_octets, 8, false);
252-
if (1 < k && k < field_type::modulus) {
237+
integral_type k = bits2int(T);
238+
if (0 < k && k < field_type::modulus) {
253239
return k;
254240
}
255241

256-
// marshalling_field_element_type marshalling_field_element;
257-
// marshalling_field_element.read(T.begin(), modulus_octets);
258-
// return crypto3::marshalling::types::construct_field_element<field_type, endianness>(
259-
// marshalling_field_element);
242+
internal_accumulator_type acc_h3(Key);
243+
compute<hmac_policy>(V, acc_h3);
244+
compute<hmac_policy>(std::array<std::uint8_t, 1> {0}, acc_h3);
245+
K = ::nil::crypto3::accumulators::extract::mac<mac::computation_policy<hmac_policy>>(acc_h3);
246+
247+
Key = key_type(K);
248+
V = compute<hmac_policy>(V, Key);
260249
} while (true);
261250
}
262251

@@ -294,7 +283,7 @@ namespace nil {
294283

295284
protected:
296285
digest_type V;
297-
key_type Key;
286+
digest_type K;
298287
};
299288
} // namespace random
300289
} // namespace crypto3

test/rfc6979.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ namespace boost {
126126

127127
BOOST_AUTO_TEST_SUITE(rfc6979_engine_tests)
128128

129-
BOOST_AUTO_TEST_CASE(mnt4_test) {
129+
// test data from https://datatracker.ietf.org/doc/html/rfc6979#appendix-A.1.2
130+
BOOST_AUTO_TEST_CASE(k_generation_ansix9t163k1) {
130131
using scalar_field_type = algebra::fields::sect_k1_scalar_field<163>;
131132
using scalar_field_value_type = typename scalar_field_type::value_type;
132133
using integral_type = typename scalar_field_type::integral_type;
@@ -166,6 +167,11 @@ BOOST_AUTO_TEST_CASE(mnt4_test) {
166167
std::vector<std::uint8_t> {0x01, 0x79, 0x5E, 0xDF, 0x0D, 0x54, 0xDB, 0x76, 0x0F, 0x15,
167168
0x6D, 0x0D, 0xAC, 0x04, 0xC0, 0x32, 0x2B, 0x3A, 0x20, 0x42, 0x24}
168169
.cbegin()));
170+
171+
auto gen = generator_type(x, h1);
172+
auto k = gen();
173+
integral_type etalon_k("3259566757037731885269073930746036563011142801435");
174+
BOOST_CHECK(k == etalon_k);
169175
}
170176

171177
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)