Skip to content

Fix BigInt/UnsignedBigInt toBytes/toBits to emit minimal-length bytes#873

Open
mwaddip wants to merge 1 commit into
ergoplatform:developfrom
mwaddip:fix/bigint-tobytes-minimal-length
Open

Fix BigInt/UnsignedBigInt toBytes/toBits to emit minimal-length bytes#873
mwaddip wants to merge 1 commit into
ergoplatform:developfrom
mwaddip:fix/bigint-tobytes-minimal-length

Conversation

@mwaddip

@mwaddip mwaddip commented Jun 3, 2026

Copy link
Copy Markdown

BigInt/UnsignedBigInt toBytes/toBits emitted 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 → []). toBits derives from the same bytes, so it diverged too (e.g. BigInt(127).toBytes was 32 bytes vs [0x7f]).

Fix: the four eval arms now use to_be_vec — the minimal form serialization already uses; to_be_bytes is untouched. Output-only; serialization and costing unaffected. Tests assert byte/bit-exact output against the blessed v6 vectors (zero, sign-pad, negatives, empty unsigned).

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>
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