Skip to content

A negative bignum is one carry over the bytes, not bignum arithmetic - #3

Merged
Asmod4n merged 1 commit into
mainfrom
claude/bignum-bytes-no-bint
Sep 8, 2026
Merged

Asmod4n merged 1 commit into
mainfrom
claude/bignum-bytes-no-bint

Conversation

@Asmod4n

@Asmod4n Asmod4n commented Sep 8, 2026 •

Copy link
Copy Markdown
Owner

CBOR writes a negative Integer as -1 - n (RFC 8949 3.4.3). The magnitude of the answer is one more, or one less, than the magnitude in hand, and both directions are a carry over bytes that mrb_integer_to_bytes() and mrb_integer_from_bytes() already give us in the order the wire uses.

So the five remaining calls into mruby-bigint go:

call why it is not needed
mrb_bint_sign, mrb_bint_abs the sign comes out of mrb_integer_to_bytes through its last argument
mrb_bint_sub (encode) |n| - 1 is a borrow running backward over the bytes
mrb_bint_add + mrb_bint_neg (two decode sites) n + 1 is a carry, in a buffer one byte longer

Each of them allocated an intermediate Bignum and held it against the GC. The new buffer is a String, so a raise inside the conversion frees it through the GC, and the mrb_malloc with its cleanup in the mrb_protect_error wrapper goes as well — struct encode_bignum_ctx loses its out member.

The two decode sites now share one helper, negative_from_magnitude(). The tag 3 path no longer builds the positive Integer first and then throws it away.

The wire format does not change.

Tests. rake test with the repository's own build_config.rb, on a clean build tree: 3125 total, 3101 OK, 0 KO, 0 crashes. The suite already covers both ends — the uint64 boundary, 2**64, the zero-length payload that reads as 0 and -1, and the byte order of a magnitude under tag 2 and tag 3.

CBOR writes a negative Integer as -1 - n. The magnitude of the answer is
one more, or one less, than the magnitude in hand, and both directions
are a carry over bytes that mrb_integer_to_bytes and
mrb_integer_from_bytes already give us in the order the wire uses.

So the five remaining calls into mruby-bigint go: mrb_bint_sign and
mrb_bint_abs, because the sign comes out of mrb_integer_to_bytes through
its last argument; mrb_bint_sub on the encode side, which is a borrow;
and mrb_bint_add with mrb_bint_neg on the two decode sides, which is a
carry. Each one allocated an intermediate Bignum and held it against the
GC. The buffer is now a String, so a raise inside the conversion frees
it, and the malloc with its cleanup in the protect wrapper goes as well.

The wire format does not change. The suite covers both ends: the uint64
boundary, 2**64, the zero-length payload that reads as 0 and -1, and the
byte order of a magnitude under tag 2 and tag 3.
@Asmod4n
Asmod4n merged commit 3aeb46a into main Sep 8, 2026
2 checks passed
@Asmod4n
Asmod4n deleted the claude/bignum-bytes-no-bint branch September 8, 2026 15:24
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.

2 participants