Limb move and byte serialization APIs#128
Merged
Merged
Conversation
…test coverage - Replace bool bigEndian parameter with BigInteger::ByteOrder enum for unambiguous call sites next to FromByteArray's negative flag - Document zero-copy limb ctor precondition (limbs must be canonical for the current base; no validation performed) - Drop assertion on moved-from vector state (unspecified per standard) - Add serialization tests: odd-length inputs (5/9 bytes, partial top limb), 17-byte multi-limb round trip with both endiannesses, leading/trailing zero-byte inputs, canonical (trimmed) output Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR adds native binary import/export and zero-copy limb manipulation APIs to
BigIntegerto avoid the overhead of decimal string parsing (Parse) and formatting (ToString).Changed/New APIs
Zero-Copy Limb APIs:
BigInteger(std::vector<DataT>&& aInt, bool negative): Move constructor for raw limb vectors.std::vector<DataT> ReleaseInteger(): Moves the internal limb vector out, leaving the object in a canonical zero state.Byte Serialization APIs:
std::vector<uint8_t> ToByteArray(bool bigEndian = true) const: Serializes the magnitude to a byte array (MSB-first by default).static BigInteger FromByteArray(std::span<const uint8_t> bytes, bool negative, bool bigEndian = true): Reconstructs aBigIntegerfrom magnitude bytes and a sign flag.Tests Run
We added unit tests to
test_construction.cppcovering all new functionality, and verified withctest:All test suites passed:
mult_correctnessdiv_correctnessunit_testsmfa_roundtrip