Skip to content

Limb move and byte serialization APIs#128

Merged
mmurshed merged 4 commits into
mainfrom
feature/serialization-api
Jun 12, 2026
Merged

Limb move and byte serialization APIs#128
mmurshed merged 4 commits into
mainfrom
feature/serialization-api

Conversation

@mmurshed

Copy link
Copy Markdown
Owner

Description

This PR adds native binary import/export and zero-copy limb manipulation APIs to BigInteger to avoid the overhead of decimal string parsing (Parse) and formatting (ToString).

Changed/New APIs

  1. 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.
  2. 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 a BigInteger from magnitude bytes and a sign flag.

Tests Run

We added unit tests to test_construction.cpp covering all new functionality, and verified with ctest:

cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build -j8
cd build && ctest --output-on-failure

All test suites passed:

  • mult_correctness
  • div_correctness
  • unit_tests
  • mfa_roundtrip

mmurshed and others added 4 commits June 12, 2026 15:28
…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>
@mmurshed mmurshed merged commit 7dfb87f into main Jun 12, 2026
2 of 5 checks passed
@mmurshed mmurshed deleted the feature/serialization-api branch June 12, 2026 23:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant