Repository navigation
A negative bignum is one carry over the bytes, not bignum arithmetic - #3
Merged
Merged
Conversation
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.
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.
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 thatmrb_integer_to_bytes()andmrb_integer_from_bytes()already give us in the order the wire uses.So the five remaining calls into mruby-bigint go:
mrb_bint_sign,mrb_bint_absmrb_integer_to_bytesthrough its last argumentmrb_bint_sub(encode)|n| - 1is a borrow running backward over the bytesmrb_bint_add+mrb_bint_neg(two decode sites)n + 1is a carry, in a buffer one byte longerEach 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_mallocwith its cleanup in themrb_protect_errorwrapper goes as well —struct encode_bignum_ctxloses itsoutmember.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 testwith the repository's ownbuild_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.