Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add built-in support for big.Int #209

Closed
x448 opened this issue Apr 3, 2020 · 0 comments · Fixed by #231
Closed

feature: add built-in support for big.Int #209

x448 opened this issue Apr 3, 2020 · 0 comments · Fixed by #231
Milestone

Comments

@x448
Copy link
Contributor

x448 commented Apr 3, 2020

Is your feature request related to a problem? Please describe.

The "Limitations" section of README.md states:

CBOR negative int (type 1) that cannot fit into Go's int64 are not supported, such as RFC 7049 example -18446744073709551616. Decoding these values returns cbor.UnmarshalTypeError like Go's encoding/json. However, this may be resolved in a future release by adding support for big.Int. Until then, users can use the API for custom encoding and decoding.

Describe the solution you'd like

Add support for encoding and decoding big.Int to handle numbers that can't fit into Go's int64.

Also add fuzzing for big.Int in fxamacker/cbor-fuzz.

Describe alternatives you've considered

Tell users to implement it via the API already provided by fxamacker/cbor.

One project using fxamacker/cbor 1.5 with big integers doesn't use Go's big.Int because they have their own big integers implementation they prefer over big.Int.

@fxamacker fxamacker added this to the v2.3.0 milestone Apr 3, 2020
fxamacker added a commit that referenced this issue May 20, 2020
By default, encode big.Int value to CBOR integer (if value fits),
otherwise encode to CBOR bignum (CBOR tag 2 or 3).

BigIntConvert is the CBOR encoding option for big.Int:

- BigIntConvertShortest: (default) encode big.Int to CBOR integer
  if value fits, otherwise encode to CBOR bignum.

- BigIntConvertNone: encode big.Int to CBOR bignum without
  converting it to another CBOR type.

Decoder can decode the following to big.Int:

- CBOR bignum (tags 2 and 3)

- CBOR integers (major type 0 and 1)

When decoding CBOR bignum to empty interface, decode to big.Int.

When decoding CBOR negative integer (major type 1) to empty interface:

- if value fits int64, decode to int64.

- if value doesn't fit int64, decode to big.Int.

Closes #209
fxamacker added a commit that referenced this issue May 20, 2020
By default, encode big.Int value to CBOR integer (if value fits),
otherwise encode to CBOR bignum (CBOR tag 2 or 3).

BigIntConvert is the CBOR encoding option for big.Int:

- BigIntConvertShortest: (default) encode big.Int to CBOR integer
  if value fits, otherwise encode to CBOR bignum.

- BigIntConvertNone: encode big.Int to CBOR bignum without
  converting it to another CBOR type.

Decoder can decode the following to big.Int:

- CBOR bignum (tags 2 and 3)

- CBOR integers (major type 0 and 1)

When decoding CBOR bignum to empty interface, decode to big.Int.

When decoding CBOR negative integer (major type 1) to empty interface:

- if value fits int64, decode to int64.

- if value doesn't fit int64, decode to big.Int.

Closes #209
@x448 x448 changed the title feature: add built-in support for big.Int so users don't have to implement it feature: add built-in support for big.Int May 21, 2020
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 a pull request may close this issue.

2 participants