botan icon indicating copy to clipboard operation
botan copied to clipboard

Add std::array support to BigInt::serialize function

Open KaganCanSit opened this issue 2 months ago • 2 comments

This PR resolves the TODO comment in BigInt::serialize() by adding support for std::array<uint8_t, N> alongside existing std::vector and secure_vector support.

TODO Message:

// 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);

KaganCanSit avatar Nov 09 '25 18:11 KaganCanSit

Coverage Status

coverage: 91.956% (+1.3%) from 90.624% when pulling 84439abca89e06ed0053ad673bbd80467e79e644 on KaganCanSit:bigint-array-serialization into e78c62c39cd051ed45d2bb2e502faf777f58c311 on randombit:master.

coveralls avatar Nov 09 '25 19:11 coveralls

Nice, thanks!

Interesting way to detect an array! However, IIUC this would work for other tuple-types as well, no? Like std::pair<int, std::string>. That would produce very strange compiler errors for sure... But then again, the API does also allow passing a std::vector<uint32_t>, which is just as awful I guess. You could look into using the statically_spanable_range<> concept to detect the array instead.

Anyway: Could you please add a few tests that serialize a BigInt into an array? src/tests/test_bigint.cpp has a function test_bigint_serialization, please add another CHECK that uses std::array. Also check that the exception is thrown as expected if the array is too short or too long.

You're right that it will provide general support for tuple types. I've made the changes as you suggested.

Additionally, for the situation you mentioned, the API does also allow passing a std::vector<uint32_t>, which is just as awful I guess. I think we could use requires std::same_as<typename T::value_type, uint8_t. There were no issues during compilation and testing.

However, since this is a general change to the API, I haven't added it to the branch. What do you think?

KaganCanSit avatar Nov 12 '25 18:11 KaganCanSit