Skip to content

Commit

Permalink
Fix big_integer literal parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
ioxid committed Nov 4, 2024
1 parent f092a28 commit 656991a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,26 @@ namespace nil::crypto3::multiprecision::literals {
if (!is_valid_hex_digit(c)) {
throw std::invalid_argument("non hex character in literal");
}
int digit = parse_hex_digit(c);
result <<= 4;
if (bits != 0) {
bits += 4;
}
result += parse_hex_digit(c);
if (digit != 0) {
int digit = parse_hex_digit(c);
result += digit;
if (bits == 0 && digit != 0) {
if (digit >= 8) {
bits = 4;
bits += 4;
} else if (digit >= 4) {
bits = 3;
bits += 3;
} else if (digit >= 2) {
bits = 2;
bits += 2;
} else {
bits = 1;
bits += 1;
}
}
}
if (bits > Bits) {
throw "not enough bits to store literal";
throw std::invalid_argument("not enough bits to store literal");
}
return result;
}
Expand Down
6 changes: 6 additions & 0 deletions crypto3/libs/multiprecision/test/big_integer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ CRYPTO3_MP_DEFINE_BIG_INTEGER_LITERAL(32)
CRYPTO3_MP_DEFINE_BIG_INTEGER_LITERAL(33)
CRYPTO3_MP_DEFINE_BIG_INTEGER_LITERAL(36)
CRYPTO3_MP_DEFINE_BIG_INTEGER_LITERAL(37)
CRYPTO3_MP_DEFINE_BIG_INTEGER_LITERAL(85)
BOOST_MP_DEFINE_SIZED_CPP_INT_LITERAL(60)

using namespace nil::crypto3::multiprecision::literals;
Expand All @@ -32,6 +33,11 @@ BOOST_AUTO_TEST_CASE(to_string_trivial) { BOOST_CHECK_EQUAL((0x1_big_integer60).

BOOST_AUTO_TEST_CASE(to_string_small) { BOOST_CHECK_EQUAL((0x20_big_integer60).str(), "0x20"); }

BOOST_AUTO_TEST_CASE(to_string_medium) {
constexpr auto a = 0x123456789ABCDEF1234321_big_integer85;
BOOST_CHECK_EQUAL(a.str(), "0x123456789ABCDEF1234321");
}

BOOST_AUTO_TEST_CASE(ops) {
nil::crypto3::multiprecision::big_integer<60> a = 2u, b;

Expand Down

0 comments on commit 656991a

Please sign in to comment.