Fix BigInt/UnsignedBigInt toBytes/toBits to emit minimal-length bytes#873
Open
mwaddip wants to merge 1 commit into
Open
Fix BigInt/UnsignedBigInt toBytes/toBits to emit minimal-length bytes#873mwaddip wants to merge 1 commit into
mwaddip wants to merge 1 commit into
Conversation
BigInt and UnsignedBigInt toBytes/toBits emitted the fixed 32-byte big-endian form (to_be_bytes), but the JVM emits a minimal-length encoding: sigma's BigInt.toBytes = BigInteger.toByteArray (signed two's-complement, minimal, 0 -> [0]) and UnsignedBigInt.toBytes = BigIntegers.asUnsignedByteArray (unsigned, minimal, 0 -> []). toBits derives from the same byte sequence, so it diverged identically (256 fixed bits vs minimal x8). E.g. BigInt(127).toBytes returned 32 bytes where the JVM returns [0x7f]. Switch the four BigInt/UnsignedBigInt eval arms to to_be_vec, already the minimal form used by serialization; the fixed-width to_be_bytes is untouched. This changes only the returned value; serialization and costing are unaffected. to_bits_bigint's roundtrip reconstructs via an unsigned positional sum, valid only for non-negative values, so it is constrained to non-negative; negatives, zero, sign-pad and the unsigned arm are covered byte-exactly by new tests reconciled against the blessed v6 vectors. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
mwaddip
added a commit
to mwaddip/sigma-rust
that referenced
this pull request
Jun 3, 2026
BigInt and UnsignedBigInt toBytes/toBits emitted the fixed 32-byte big-endian form (to_be_bytes), but the JVM emits a minimal-length encoding: sigma's BigInt.toBytes = BigInteger.toByteArray (signed two's-complement, minimal, 0 -> [0]) and UnsignedBigInt.toBytes = BigIntegers.asUnsignedByteArray (unsigned, minimal, 0 -> []). toBits derives from the same byte sequence, so it diverged identically (256 fixed bits vs minimal x8). E.g. BigInt(127).toBytes returned 32 bytes where the JVM returns [0x7f]. Switch the four BigInt/UnsignedBigInt eval arms to to_be_vec, already the minimal form used by serialization; the fixed-width to_be_bytes is untouched. This changes only the returned value; serialization and costing are unaffected. to_bits_bigint's roundtrip reconstructs via an unsigned positional sum, valid only for non-negative values, so it is constrained to non-negative; negatives, zero, sign-pad and the unsigned arm are covered byte-exactly by new tests reconciled against the blessed v6 vectors. Corresponds to develop PR ergoplatform#873. Co-Authored-By: Claude Opus 4.8 (1M context) <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.
BigInt/UnsignedBigInttoBytes/toBitsemitted the fixed 32-byte form, but the JVM emits minimal-length big-endian bytes:BigInt.toBytes=BigInteger.toByteArray(signed two's-complement,0 → [0]);UnsignedBigInt.toBytes=asUnsignedByteArray(unsigned,0 → []).toBitsderives from the same bytes, so it diverged too (e.g.BigInt(127).toByteswas 32 bytes vs[0x7f]).Fix: the four eval arms now use
to_be_vec— the minimal form serialization already uses;to_be_bytesis untouched. Output-only; serialization and costing unaffected. Tests assert byte/bit-exact output against the blessed v6 vectors (zero, sign-pad, negatives, empty unsigned).