You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you output BigInts with std::hex via operator<<(std::ostream&, const BigInt&) (in src/lib/math/bigint/big_io.cpp) the prefix 0x is automatically added:
This is probably the reason why it was not noticed that this code from the cli outputs the prefix twice.
// src/cli/math.cpp, line: 66const Botan::BigInt p = Botan::random_prime(rng(), bits);
if(hex)
output() << "0x" << std::hex << p << "\n";
// ^^^^^^^^^^^^^^^^^^^^^elseoutput() << p << "\n";
$ botan gen_prime --hex 2
# Output: 0x0x02 and not 0x02
Nevertheless, the current implementation of BigInt makes sense in that it allows a negative sign to be placed in front. (Which of course does not happen with normal integers in the two's complement). However, there is another problem.
Output of negative BigInts
When displaying negative BigInts, the sign is output twice:
std::cout << -Botan::BigInt{10} << '\n';
// Output: --10 and not -10
This happens because both operator<<(std::ostream&, const BigInt&) and the functions called from it to_dec_string/to_hex_string (in src/lib/math/bigint/big_code.cpp) prepend a negative sign.
This leads to the output being far from the expected/normal:
std::cout << "0x" << std::hex << -Botan::BigInt{10} << '\n';
// Output: 0x--0x0A and not 0x-0A or -0x0A
My ideas for the solution
No more prefix 0x in operator<<(std::ostream&, const BigInt&).
No more sign in operator<<(std::ostream&, const BigInt&), but still in to_dec_string/to_hex_string, as some functions call to_dec_string/to_hex_string directly.
I don't know if these changes are realistic, because I haven't found a way to find all the places in the code that depend on the overloaded operator <<.
This is my first issue on GitHub. I hope I haven't missed anything obvious.
The text was updated successfully, but these errors were encountered:
Output of BigInts in hexadecimal representation
If you output BigInts with
std::hex
viaoperator<<(std::ostream&, const BigInt&)
(insrc/lib/math/bigint/big_io.cpp
) the prefix0x
is automatically added:However, this does not happen with the standard library:
This is probably the reason why it was not noticed that this code from the cli outputs the prefix twice.
$ botan gen_prime --hex 2 # Output: 0x0x02 and not 0x02
Nevertheless, the current implementation of BigInt makes sense in that it allows a negative sign to be placed in front. (Which of course does not happen with normal integers in the two's complement). However, there is another problem.
Output of negative BigInts
When displaying negative BigInts, the sign is output twice:
This happens because both
operator<<(std::ostream&, const BigInt&)
and the functions called from itto_dec_string/to_hex_string
(insrc/lib/math/bigint/big_code.cpp
) prepend a negative sign.This leads to the output being far from the expected/normal:
My ideas for the solution
operator<<(std::ostream&, const BigInt&)
.operator<<(std::ostream&, const BigInt&)
, but still into_dec_string/to_hex_string
, as some functions callto_dec_string/to_hex_string
directly.I don't know if these changes are realistic, because I haven't found a way to find all the places in the code that depend on the overloaded operator
<<
.This is my first issue on GitHub. I hope I haven't missed anything obvious.
The text was updated successfully, but these errors were encountered: