Skip to content

Commit

Permalink
crypto3: rewrite usaged of import_bits and export_bits to methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ioxid committed Dec 18, 2024
1 parent c00f685 commit dd5d873
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ namespace nil {
for (std::size_t i = 0; i < length; i++) {
// Should be
// std::size_t bn_exponents_i_msb = exponents[i].data.base().msb() + 1;
// But nil::crypto3::multiprecision::msb doesn't work for zero value
// But msb doesn't work for zero value
std::size_t bn_exponents_i_msb = 1;
if (exponents[i] != field_value_type::zero()) {
bn_exponents_i_msb = exponents[i].data.base().msb() + 1;
Expand Down
4 changes: 2 additions & 2 deletions crypto3/libs/hash/include/nil/crypto3/hash/detail/h2c/ep.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ namespace nil {
for (std::size_t i = 0; i < N; i++) {
for (std::size_t j = 0; j < m; j++) {
auto elm_offset = L * (j + i * m);
import_bits(e, uniform_bytes.begin() + elm_offset,
uniform_bytes.begin() + elm_offset + L);
e.import_bits(uniform_bytes.begin() + elm_offset,
uniform_bytes.begin() + elm_offset + L);

// Sometimes hash is 512 bits, while the group element is 256 or 381 bits.
// In these cases we take the number module the modulus of the group.
Expand Down
4 changes: 2 additions & 2 deletions crypto3/libs/hash/include/nil/crypto3/hash/detail/h2c/ep2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ namespace nil {
for (std::size_t i = 0; i < N; i++) {
for (std::size_t j = 0; j < m; j++) {
auto elm_offset = L * (j + i * m);
import_bits(e, uniform_bytes.begin() + elm_offset,
uniform_bytes.begin() + elm_offset + L);
e.import_bits(uniform_bytes.begin() + elm_offset,
uniform_bytes.begin() + elm_offset + L);
// Sometimes hash is 512 bits, while the group element is 256 or 381 bits.
// In these cases we take the number module the modulus of the group.
// TODO(ioxid): this is not needed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace nil {
1 : sizeof(typename std::iterator_traits<TIter>::value_type) * 8;
std::size_t chunk_bits = sizeof(typename std::iterator_traits<TIter>::value_type) * units_bits;

export_bits(value, iter, chunk_bits, true);
value.export_bits(iter, chunk_bits, true);
}

/// @brief Write part of integral value into the output area using big
Expand All @@ -76,12 +76,12 @@ namespace nil {

if (value > 0) {
std::size_t begin_index =
chunks_count - ((nil::crypto3::multiprecision::msb(value) + 1) / chunk_bits +
(((nil::crypto3::multiprecision::msb(value) + 1) % chunk_bits) ? 1 : 0));
chunks_count - ((value.msb() + 1) / chunk_bits +
(((value.msb() + 1) % chunk_bits) ? 1 : 0));

std::fill(iter, iter + begin_index, 0);

export_bits(value, iter + begin_index, chunk_bits, true);
value.export_bits(iter + begin_index, chunk_bits, true);
} else {
std::fill(iter, iter + chunks_count, 0);
}
Expand All @@ -105,7 +105,7 @@ namespace nil {
std::size_t chunk_bits = sizeof(typename std::iterator_traits<TIter>::value_type) * units_bits;
std::size_t chunks_count = (value_size / chunk_bits) + ((value_size % chunk_bits) ? 1 : 0);

nil::crypto3::multiprecision::import_bits(serializedValue, iter, iter + chunks_count, chunk_bits, true);
serializedValue.import_bits(iter, iter + chunks_count, chunk_bits, true);
return serializedValue;
}

Expand All @@ -127,7 +127,7 @@ namespace nil {
std::size_t chunk_bits = sizeof(typename std::iterator_traits<TIter>::value_type) * units_bits;
std::size_t chunks_count = (TSize / chunk_bits) + ((TSize % chunk_bits) ? 1 : 0);

nil::crypto3::multiprecision::import_bits(serializedValue, iter, iter + chunks_count, chunk_bits, true);
serializedValue.import_bits(iter, iter + chunks_count, chunk_bits, true);
return serializedValue;
}

Expand All @@ -144,7 +144,7 @@ namespace nil {
1 : sizeof(typename std::iterator_traits<TIter>::value_type) * 8;
std::size_t chunk_bits = sizeof(typename std::iterator_traits<TIter>::value_type) * units_bits;

export_bits(value, iter, chunk_bits, false);
value.export_bits(iter, chunk_bits, false);
}

/// @brief Write integral value into the output area using big
Expand All @@ -162,14 +162,14 @@ namespace nil {
std::size_t chunks_count = (TSize / chunk_bits) + ((TSize % chunk_bits) ? 1 : 0);

if (value > 0) {
std::size_t begin_index = ((nil::crypto3::multiprecision::msb(value) + 1) / chunk_bits +
(((nil::crypto3::multiprecision::msb(value) + 1) % chunk_bits) ? 1 : 0));
std::size_t begin_index = ((value.msb() + 1) / chunk_bits +
(((value.msb() + 1) % chunk_bits) ? 1 : 0));

if (begin_index < chunks_count) {
std::fill(iter + begin_index, iter + chunks_count, 0x00);
}

export_bits(value, iter, chunk_bits, false);
value.export_bits(iter, chunk_bits, false);
} else {
std::fill(iter, iter + chunks_count, 0);
}
Expand All @@ -191,7 +191,7 @@ namespace nil {
std::size_t chunk_bits = sizeof(typename std::iterator_traits<TIter>::value_type) * units_bits;
std::size_t chunks_count = (value_size / chunk_bits) + ((value_size % chunk_bits) ? 1 : 0);

nil::crypto3::multiprecision::import_bits(serializedValue, iter, iter + chunks_count, chunk_bits, false);
serializedValue.import_bits(iter, iter + chunks_count, chunk_bits, false);
return serializedValue;
}

Expand All @@ -211,7 +211,7 @@ namespace nil {
std::size_t chunk_bits = sizeof(typename std::iterator_traits<TIter>::value_type) * units_bits;
std::size_t chunks_count = (TSize / chunk_bits) + ((TSize % chunk_bits) ? 1 : 0);

nil::crypto3::multiprecision::import_bits(serializedValue, iter, iter + chunks_count, chunk_bits, false);
serializedValue.import_bits(iter, iter + chunks_count, chunk_bits, false);
return serializedValue;
}

Expand Down
12 changes: 6 additions & 6 deletions crypto3/libs/marshalling/multiprecision/test/integral.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ void test_round_trip_fixed_precision_big_endian(T val) {

std::vector<unit_type> cv;
cv.resize(unitblob_size, 0x00);
std::size_t begin_index = cv.size() - ((nil::crypto3::multiprecision::msb(val) + 1) / units_bits +
(((nil::crypto3::multiprecision::msb(val) + 1) % units_bits) ? 1 : 0));
std::size_t begin_index =
cv.size() - ((val.msb() + 1) / units_bits + (((val.msb() + 1) % units_bits) ? 1 : 0));

export_bits(val, cv.begin() + begin_index, units_bits, true);
val.export_bits(cv.begin() + begin_index, units_bits, true);

nil::crypto3::marshalling::status_type status;
T test_val = nil::crypto3::marshalling::pack<nil::crypto3::marshalling::option::big_endian>(cv, status);
Expand All @@ -111,7 +111,7 @@ void test_round_trip_fixed_precision_little_endian(T val) {

std::vector<unit_type> cv;

export_bits(val, std::back_inserter(cv), units_bits, false);
val.export_bits(std::back_inserter(cv), units_bits, false);
cv.resize(unitblob_size, 0x00);

nil::crypto3::marshalling::status_type status;
Expand Down Expand Up @@ -148,8 +148,8 @@ void test_round_trip_non_fixed_precision(T val) {
using unit_type = OutputType;

std::vector<unit_type> cv;
export_bits(
val, std::back_inserter(cv), units_bits,
val.export_bits(
std::back_inserter(cv), units_bits,
static_cast<bool>(
std::is_same<TEndianness, nil::crypto3::marshalling::option::big_endian>::value));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,11 @@ void test_round_trip_fixed_size_container_fixed_precision_big_endian(

for (std::size_t i = 0; i < TSize; i++) {
std::size_t begin_index =
unitblob_size - ((nil::crypto3::multiprecision::msb(val_container[i]) + 1) / units_bits +
(((nil::crypto3::multiprecision::msb(val_container[i]) + 1) % units_bits) ? 1 : 0));
unitblob_size - ((val_container[i].msb() + 1) / units_bits +
(((val_container[i].msb() + 1) % units_bits) ? 1 : 0));

export_bits(val_container[i], cv.begin() + unitblob_size * i + begin_index, units_bits, true);
val_container[i].export_bits(cv.begin() + unitblob_size * i + begin_index, units_bits,
true);
}

nil::crypto3::marshalling::status_type status;
Expand Down Expand Up @@ -135,7 +136,7 @@ void test_round_trip_fixed_size_container_fixed_precision_little_endian(
cv.resize(unitblob_size * TSize, 0x00);

for (std::size_t i = 0; i < TSize; i++) {
export_bits(val_container[i], cv.begin() + unitblob_size * i, units_bits, false);
val_container[i].export_bits(cv.begin() + unitblob_size * i, units_bits, false);
}

nil::crypto3::marshalling::status_type status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ namespace nil {
>::value) {
auto integral = typename CommonDataType::field_type::integral_type(common_data.vk.constraint_system_with_params_hash.data);
std::vector<unsigned char> blob;
export_bits(integral, std::back_inserter(blob), 8);
integral.export_bits(std::back_inserter(blob), 8);
for( std::size_t i = blob.size(); i > 0; i--){
filled_constraint_system_with_params_hash.value().push_back(
nil::crypto3::marshalling::types::integral<TTypeBase, octet_type>(blob[i-1])
Expand Down Expand Up @@ -247,7 +247,7 @@ namespace nil {
blob.push_back(std::uint8_t(std::get<13>(filled_common_data.value()).value()[i].value()));
}
typename CommonDataType::field_type::integral_type newval;
import_bits(newval, blob.begin(), blob.end(), 8, false);
newval.import_bits(blob.begin(), blob.end(), 8, false);
vk.constraint_system_with_params_hash = typename CommonDataType::field_type::value_type(newval);
} else {
for( std::size_t i = 0; i < std::get<13>(filled_common_data.value()).value().size(); i++){
Expand Down
4 changes: 2 additions & 2 deletions crypto3/libs/random/include/nil/crypto3/random/hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ namespace nil {
},
seed_bytes.begin());
res = ::nil::crypto3::hash<hash_type>(seed_bytes);
::nil::crypto3::multiprecision::import_bits(
result, res.begin(), res.begin() + bincode::modulus_chunks, 8, false);
result.import_bits(res.begin(), res.begin() + bincode::modulus_chunks, 8,
false);

++iter;
} while (result >= result_type::field_type::modulus);
Expand Down

0 comments on commit dd5d873

Please sign in to comment.