Skip to content

Commit

Permalink
multiprecision: big_uint: reenable exception in import and fix (hopef…
Browse files Browse the repository at this point in the history
…ully) all usages
  • Loading branch information
ioxid committed Dec 22, 2024
1 parent e516b7b commit f713bca
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<sizeof_field_element, integral_type, endianness>(iter);
static_assert(
std::is_same_v<integral_type,
nil::crypto3::multiprecision::big_uint<254>>);
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
Expand Down Expand Up @@ -245,8 +254,16 @@ namespace nil {
chunk_type I_bit = *iter & 0x80;
chunk_type S_bit = *iter & 0x40;

static_assert(
std::is_same_v<integral_type,
nil::crypto3::multiprecision::big_uint<254>>);
static_assert(sizeof_field_element == 254);

TIter read_iter = iter;
integral_type x_1 = multiprecision::processing::read_data<sizeof_field_element, integral_type, endianness>(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<sizeof_field_element, integral_type, endianness>(read_iter);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<params_type::bit_length(), base_integral_type, endianness>(iter);
static_assert(
std::is_same_v<base_integral_type,
nil::crypto3::multiprecision::big_uint<254>>);
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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,15 @@ namespace nil {
return nil::crypto3::marshalling::status_type::success;
}

integral_type x = multiprecision::processing::read_data<sizeof_field_element, integral_type, endianness>(iter);
static_assert(
std::is_same_v<integral_type,
nil::crypto3::multiprecision::big_uint<254>>);
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>();

g1_field_value_type x_mod(x);
g1_field_value_type y2_mod = x_mod.pow(3u) + group_type::params_type::b;
Expand Down Expand Up @@ -383,7 +391,16 @@ namespace nil {

TIter read_iter = iter;

integral_type x_1 = multiprecision::processing::read_data<sizeof_field_element, integral_type, endianness>(read_iter);
static_assert(
std::is_same_v<integral_type,
nil::crypto3::multiprecision::big_uint<254>>);
static_assert(sizeof_field_element == 254);

auto x_1_raw = multiprecision::processing::read_data<
256, nil::crypto3::multiprecision::big_uint<256>, endianness>(
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<sizeof_field_element, integral_type, endianness>(read_iter);
Expand Down Expand Up @@ -448,7 +465,15 @@ namespace nil {
return nil::crypto3::marshalling::status_type::success;
}

integral_type x = multiprecision::processing::read_data<sizeof_field_element, integral_type, endianness>(iter);
static_assert(
std::is_same_v<integral_type,
nil::crypto3::multiprecision::big_uint<254>>);
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>();

g1_field_value_type x_mod(x);
g1_field_value_type y2_mod = x_mod.pow(3u) + group_type::params_type::b;
Expand Down Expand Up @@ -512,7 +537,16 @@ namespace nil {

TIter read_iter = iter;

integral_type x_1 = multiprecision::processing::read_data<sizeof_field_element, integral_type, endianness>(read_iter);
static_assert(
std::is_same_v<integral_type,
nil::crypto3::multiprecision::big_uint<254>>);
static_assert(sizeof_field_element == 254);

auto x_1_raw = multiprecision::processing::read_data<
256, nil::crypto3::multiprecision::big_uint<256>, endianness>(
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<sizeof_field_element, integral_type, endianness>(read_iter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<params_type::bit_length(), base_integral_type, endianness>(iter);
static_assert(
std::is_same_v<base_integral_type,
nil::crypto3::multiprecision::big_uint<255>>);
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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<sizeof_field_element, integral_type, endianness>(iter);
static_assert(
std::is_same_v<integral_type,
nil::crypto3::multiprecision::big_uint<254>>);
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>();

if (I_bit) {
// point at infinity
Expand Down Expand Up @@ -273,7 +281,15 @@ namespace nil {
chunk_type S_bit = *iter & 0x40;

TIter read_iter = iter;
integral_type x_1 = multiprecision::processing::read_data<sizeof_field_element, integral_type, endianness>(read_iter);
static_assert(
std::is_same_v<integral_type,
nil::crypto3::multiprecision::big_uint<254>>);
static_assert(sizeof_field_element == 254);

auto x_1_raw = multiprecision::processing::read_data<
256, nil::crypto3::multiprecision::big_uint<256>, endianness>(
iter);
integral_type x = x_1_raw.template truncate<254>();
read_iter += sizeof_field_element_chunks_count;
integral_type x_0 = multiprecision::processing::read_data<sizeof_field_element, integral_type, endianness>(read_iter);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<sizeof_field_element, integral_type, endianness>(iter);
static_assert(
std::is_same_v<integral_type,
nil::crypto3::multiprecision::big_uint<254>>);
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>();

if (I_bit) {
// point at infinity
Expand Down Expand Up @@ -283,7 +291,15 @@ namespace nil {
chunk_type S_bit = *iter & 0x40;

TIter read_iter = iter;
integral_type x_2 = multiprecision::processing::read_data<sizeof_field_element, integral_type, endianness>(read_iter);
static_assert(
std::is_same_v<integral_type,
nil::crypto3::multiprecision::big_uint<254>>);
static_assert(sizeof_field_element == 254);

auto x_2_raw = multiprecision::processing::read_data<
256, nil::crypto3::multiprecision::big_uint<256>, endianness>(
iter);
integral_type x_2 = x_2_raw.template truncate<254>();
read_iter += sizeof_field_element_chunks_count;
integral_type x_1 = multiprecision::processing::read_data<sizeof_field_element, integral_type, endianness>(read_iter);
read_iter += sizeof_field_element_chunks_count;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand Down

0 comments on commit f713bca

Please sign in to comment.