-
Notifications
You must be signed in to change notification settings - Fork 574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clean out the BigInt interface #4056
Conversation
c67df9c
to
ff12476
Compare
1a9974e
to
79db481
Compare
@@ -60,17 +64,20 @@ class BOTAN_PUBLIC_API(2, 0) BigInt final { | |||
* Create BigInt from a word (limb) | |||
* @param n initial value of this BigInt | |||
*/ | |||
//BOTAN_DEPRECATED("Use BigInt::from_u64 instead") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Commented out on purpose? Same with BigInt(std::string_view)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No full review so far. Just a few suggestions.
// TODO this supports std::vector and secure_vector | ||
// it would be nice if this also could work with std::array as in | ||
// bn.serialize_to<std::array<uint8_t, 32>>(32); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps just add another overload like so:
template<size_t len>
std::array<uint8_t, len> serialize() const {
std::array<uint8_t, len> out;
this->serialize_to(out);
return out;
}
usage would then look like this:
bn.serialize<32>();
... this style would be similar to the implementation in the BufferSlicer
, where one would bs.take<32>()
.
For call-site readability it might make sense to rename that overload to something like serialize_as_array<len>()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My take at this point is that it's probably quite rare to know at compile time the exact size of the integer being encoded. Can always revisit this issue later, and as always hard to take it back once it is in a release, so I'm going to defer.
src/lib/math/bigint/bigint.h
Outdated
/** | ||
* Read integer value from a byte vector (big endian) | ||
* @param bytes the span of bytes to load | ||
*/ | ||
void assign_from_bytes(std::span<const uint8_t> bytes); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we even need that? Or is a simple assignment enough?
BigInt some_existing_bn;
some_existing_bn = BigInt::from_bytes(some_byte_array);
... with an appropriate move-assignment operator that might not even have a performance penalty.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See also: #4056 (comment)
Most of these are only useful internally to implement specific algorithms, and should never have been exposed to users.
Deprecate all of the existing interface.
79db481
to
e152c76
Compare
This allows us to deprecate BigInt::data and still use it in headers/inlines without triggering a warning in user code.
e152c76
to
ae49fef
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. Just some nits and a few minor suggestions.
if(base == Binary) { | ||
return BigInt::from_bytes(buf); | ||
} | ||
return BigInt::decode(buf.data(), buf.size(), base); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps BigInt::decode(std::span<const uint8_t>, ..)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For these functions using BigInt::Base
I'm inclined to not do anything with span
since already the whole set of functions and the enum itself are deprecated. We'd just be introducing new interfaces which begin life deprecated, which seems odd.
std::array<uint8_t, 56> res_bytes = {0}; | ||
res.binary_encode(res_bytes.data(), res_bytes.size()); | ||
res.serialize_to(res_bytes); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For reference: If we add a serialization overload that produces an array, this could become:
auto res_bytes = res.serialize_as_array<56>();
src/lib/asn1/ber_dec.cpp
Outdated
out.flip_sign(); | ||
} else { | ||
out = BigInt(obj.bits(), obj.length()); | ||
out.assign_from_bytes(obj.data()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no way to access a BER_Object
's data as a span?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BER_Object::data
is exactly this?
(This was just recently added in cf82307)
bb10382
to
b5498fd
Compare
Co-authored-by: René Meusel <[email protected]>
This class has grown in an unprincipled way out of accretion and it's quite a mess. Many functions are implemented which are just useful for 1 or2 algorithms. Deprecate everything along these lines.
I don't have an immediate plan for how we'll get access to the functionality later from within the library, but sine we can't remove anything until Botan 4 anyway it doesn't really matter right now.
Also add new serialization and deserialization functions based on
span
. The newserialize_to
avoids the bizarre trunction behavior ofbinary_encode
when the output buffer is too small - I'm not sure why or when that was ever useful.