From bdb9324a4e3b7f8b4672ba042d9a22bf702d0db0 Mon Sep 17 00:00:00 2001 From: Andrey Nefedov Date: Sat, 21 Dec 2024 18:52:31 +0000 Subject: [PATCH] multiprecision: big_uint: reenable exception in import and fix (hopefully) all usages --- .../algebra/processing/alt_bn128.hpp | 21 +++++++++- .../algebra/processing/babyjubjub.hpp | 11 ++++- .../marshalling/algebra/processing/bls12.hpp | 42 +++++++++++++++++-- .../algebra/processing/ed25519.hpp | 11 ++++- .../marshalling/algebra/processing/mnt4.hpp | 20 ++++++++- .../marshalling/algebra/processing/mnt6.hpp | 20 ++++++++- .../nil/crypto3/multiprecision/big_uint.hpp | 3 +- 7 files changed, 112 insertions(+), 16 deletions(-) diff --git a/crypto3/libs/marshalling/algebra/include/nil/crypto3/marshalling/algebra/processing/alt_bn128.hpp b/crypto3/libs/marshalling/algebra/include/nil/crypto3/marshalling/algebra/processing/alt_bn128.hpp index 0ec9ce28b2..fcc16be928 100644 --- a/crypto3/libs/marshalling/algebra/include/nil/crypto3/marshalling/algebra/processing/alt_bn128.hpp +++ b/crypto3/libs/marshalling/algebra/include/nil/crypto3/marshalling/algebra/processing/alt_bn128.hpp @@ -186,7 +186,16 @@ namespace nil { chunk_type I_bit = *iter & 0x80; chunk_type S_bit = *iter & 0x40; - integral_type x = multiprecision::processing::read_data(iter); + static_assert( + std::is_same_v>); + static_assert(sizeof_field_element == 254); + + auto x_raw = multiprecision::processing::read_data< + 256, nil::crypto3::multiprecision::big_uint<256>, endianness>( + iter); + integral_type x = + x_raw.template truncate<254>(); // remove I_bit and S_bit if (I_bit) { // point at infinity @@ -245,8 +254,16 @@ namespace nil { chunk_type I_bit = *iter & 0x80; chunk_type S_bit = *iter & 0x40; + static_assert( + std::is_same_v>); + static_assert(sizeof_field_element == 254); + TIter read_iter = iter; - integral_type x_1 = multiprecision::processing::read_data(read_iter); + auto x_1_raw = multiprecision::processing::read_data< + 256, nil::crypto3::multiprecision::big_uint<256>, endianness>( + read_iter); + integral_type x_1 = x_1_raw.template truncate<254>(); read_iter += sizeof_field_element_chunks_count; integral_type x_0 = multiprecision::processing::read_data(read_iter); diff --git a/crypto3/libs/marshalling/algebra/include/nil/crypto3/marshalling/algebra/processing/babyjubjub.hpp b/crypto3/libs/marshalling/algebra/include/nil/crypto3/marshalling/algebra/processing/babyjubjub.hpp index bf7075bb67..138232a35d 100644 --- a/crypto3/libs/marshalling/algebra/include/nil/crypto3/marshalling/algebra/processing/babyjubjub.hpp +++ b/crypto3/libs/marshalling/algebra/include/nil/crypto3/marshalling/algebra/processing/babyjubjub.hpp @@ -145,8 +145,15 @@ namespace nil { (params_type::bit_length() / 8 + (params_type::bit_length() % 8 ? 1 : 0)), "wrong size"); - base_integral_type y = - multiprecision::processing::read_data(iter); + static_assert( + std::is_same_v>); + static_assert(params_type::bit_length() == 254); + + auto y_raw = multiprecision::processing::read_data< + 256, nil::crypto3::multiprecision::big_uint<256>, endianness>( + iter); + base_integral_type y = y_raw.template truncate<254>(); bool sign = *(iter + encoded_size - 1) & (1 << 6); auto decoded_point_affine = diff --git a/crypto3/libs/marshalling/algebra/include/nil/crypto3/marshalling/algebra/processing/bls12.hpp b/crypto3/libs/marshalling/algebra/include/nil/crypto3/marshalling/algebra/processing/bls12.hpp index 6d2711adc7..88e3caaee4 100644 --- a/crypto3/libs/marshalling/algebra/include/nil/crypto3/marshalling/algebra/processing/bls12.hpp +++ b/crypto3/libs/marshalling/algebra/include/nil/crypto3/marshalling/algebra/processing/bls12.hpp @@ -319,7 +319,15 @@ namespace nil { return nil::crypto3::marshalling::status_type::success; } - integral_type x = multiprecision::processing::read_data(iter); + static_assert( + std::is_same_v>); + static_assert(sizeof_field_element == 381); + + auto x_raw = multiprecision::processing::read_data< + 384, nil::crypto3::multiprecision::big_uint<384>, endianness>( + iter); + integral_type x = x_raw.template truncate<381>(); g1_field_value_type x_mod(x); g1_field_value_type y2_mod = x_mod.pow(3u) + group_type::params_type::b; @@ -383,7 +391,16 @@ namespace nil { TIter read_iter = iter; - integral_type x_1 = multiprecision::processing::read_data(read_iter); + static_assert( + std::is_same_v>); + static_assert(sizeof_field_element == 384); + + auto x_1_raw = multiprecision::processing::read_data< + 384, nil::crypto3::multiprecision::big_uint<384>, endianness>( + iter); + integral_type x_1 = x_1_raw.template truncate<381>(); + read_iter += sizeof_field_element_chunks_count; integral_type x_0 = multiprecision::processing::read_data(read_iter); @@ -448,7 +465,15 @@ namespace nil { return nil::crypto3::marshalling::status_type::success; } - integral_type x = multiprecision::processing::read_data(iter); + static_assert( + std::is_same_v>); + static_assert(sizeof_field_element == 377); + + auto x_raw = multiprecision::processing::read_data< + 384, nil::crypto3::multiprecision::big_uint<384>, endianness>( + iter); + integral_type x = x_raw.template truncate<377>(); g1_field_value_type x_mod(x); g1_field_value_type y2_mod = x_mod.pow(3u) + group_type::params_type::b; @@ -512,7 +537,16 @@ namespace nil { TIter read_iter = iter; - integral_type x_1 = multiprecision::processing::read_data(read_iter); + static_assert( + std::is_same_v>); + static_assert(sizeof_field_element == 384); + + auto x_1_raw = multiprecision::processing::read_data< + 384, nil::crypto3::multiprecision::big_uint<384>, endianness>( + iter); + integral_type x_1 = x_1_raw.template truncate<377>(); + read_iter += sizeof_field_element_chunks_count; integral_type x_0 = multiprecision::processing::read_data(read_iter); diff --git a/crypto3/libs/marshalling/algebra/include/nil/crypto3/marshalling/algebra/processing/ed25519.hpp b/crypto3/libs/marshalling/algebra/include/nil/crypto3/marshalling/algebra/processing/ed25519.hpp index 23b1e94808..1a37783358 100644 --- a/crypto3/libs/marshalling/algebra/include/nil/crypto3/marshalling/algebra/processing/ed25519.hpp +++ b/crypto3/libs/marshalling/algebra/include/nil/crypto3/marshalling/algebra/processing/ed25519.hpp @@ -136,8 +136,15 @@ namespace nil { (params_type::bit_length() / 8 + (params_type::bit_length() % 8 ? 1 : 0)), "wrong size"); - base_integral_type y = - multiprecision::processing::read_data(iter); + static_assert( + std::is_same_v>); + static_assert(params_type::bit_length() == 255); + + auto y_raw = multiprecision::processing::read_data< + 256, nil::crypto3::multiprecision::big_uint<256>, endianness>( + iter); + base_integral_type y = y_raw.template truncate<255>(); bool sign = *(iter + encoded_size - 1) & (1 << 7); auto decoded_point_affine = diff --git a/crypto3/libs/marshalling/algebra/include/nil/crypto3/marshalling/algebra/processing/mnt4.hpp b/crypto3/libs/marshalling/algebra/include/nil/crypto3/marshalling/algebra/processing/mnt4.hpp index 4be174a646..3854036481 100644 --- a/crypto3/libs/marshalling/algebra/include/nil/crypto3/marshalling/algebra/processing/mnt4.hpp +++ b/crypto3/libs/marshalling/algebra/include/nil/crypto3/marshalling/algebra/processing/mnt4.hpp @@ -211,7 +211,15 @@ namespace nil { chunk_type I_bit = *iter & 0x80; chunk_type S_bit = *iter & 0x40; - integral_type x = multiprecision::processing::read_data(iter); + static_assert( + std::is_same_v>); + static_assert(sizeof_field_element == 298); + + auto x_raw = multiprecision::processing::read_data< + 304, nil::crypto3::multiprecision::big_uint<304>, endianness>( + iter); + integral_type x = x_raw.template truncate<298>(); if (I_bit) { // point at infinity @@ -273,7 +281,15 @@ namespace nil { chunk_type S_bit = *iter & 0x40; TIter read_iter = iter; - integral_type x_1 = multiprecision::processing::read_data(read_iter); + static_assert( + std::is_same_v>); + static_assert(sizeof_field_element == 304); + + auto x_1_raw = multiprecision::processing::read_data< + 304, nil::crypto3::multiprecision::big_uint<304>, endianness>( + iter); + integral_type x_1 = x_1_raw.template truncate<298>(); read_iter += sizeof_field_element_chunks_count; integral_type x_0 = multiprecision::processing::read_data(read_iter); diff --git a/crypto3/libs/marshalling/algebra/include/nil/crypto3/marshalling/algebra/processing/mnt6.hpp b/crypto3/libs/marshalling/algebra/include/nil/crypto3/marshalling/algebra/processing/mnt6.hpp index 322b24acd4..9db8e5ff81 100644 --- a/crypto3/libs/marshalling/algebra/include/nil/crypto3/marshalling/algebra/processing/mnt6.hpp +++ b/crypto3/libs/marshalling/algebra/include/nil/crypto3/marshalling/algebra/processing/mnt6.hpp @@ -221,7 +221,15 @@ namespace nil { chunk_type I_bit = *iter & 0x80; chunk_type S_bit = *iter & 0x40; - integral_type x = multiprecision::processing::read_data(iter); + static_assert( + std::is_same_v>); + static_assert(sizeof_field_element == 298); + + auto x_raw = multiprecision::processing::read_data< + 304, nil::crypto3::multiprecision::big_uint<304>, endianness>( + iter); + integral_type x = x_raw.template truncate<298>(); if (I_bit) { // point at infinity @@ -283,7 +291,15 @@ namespace nil { chunk_type S_bit = *iter & 0x40; TIter read_iter = iter; - integral_type x_2 = multiprecision::processing::read_data(read_iter); + static_assert( + std::is_same_v>); + static_assert(sizeof_field_element == 304); + + auto x_2_raw = multiprecision::processing::read_data< + 304, nil::crypto3::multiprecision::big_uint<304>, endianness>( + iter); + integral_type x_2 = x_2_raw.template truncate<298>(); read_iter += sizeof_field_element_chunks_count; integral_type x_1 = multiprecision::processing::read_data(read_iter); read_iter += sizeof_field_element_chunks_count; diff --git a/crypto3/libs/multiprecision/include/nil/crypto3/multiprecision/big_uint.hpp b/crypto3/libs/multiprecision/include/nil/crypto3/multiprecision/big_uint.hpp index c151140543..8b758716ae 100644 --- a/crypto3/libs/multiprecision/include/nil/crypto3/multiprecision/big_uint.hpp +++ b/crypto3/libs/multiprecision/include/nil/crypto3/multiprecision/big_uint.hpp @@ -1214,8 +1214,7 @@ namespace nil::crypto3::multiprecision { } if (normalize()) { - // TODO(ioxid): this throws right now - // throw std::overflow_error("import_bits: overflow"); + throw std::overflow_error("import_bits: overflow"); } }