go-ipld-cbor icon indicating copy to clipboard operation
go-ipld-cbor copied to clipboard

when serialize Bignum, it drop the negative sign for bignum, would cause error in deserialize

Open atenjin opened this issue 5 years ago • 1 comments

marshal for bigint is like: https://github.com/ipfs/go-ipld-cbor/blob/e249008b68731703a093b723253be5175d518ed0/refmt.go#L27-L36

var BigIntAtlasEntry = atlas.BuildEntry(big.Int{}).Transform().
	TransformMarshal(atlas.MakeMarshalTransformFunc(
		func(i big.Int) ([]byte, error) {
			return i.Bytes(), nil
		})).
	TransformUnmarshal(atlas.MakeUnmarshalTransformFunc(
		func(x []byte) (big.Int, error) {
			return *big.NewInt(0).SetBytes(x), nil
		})).
	Complete()

but i.Bytes() would drop the sign for bignum!

// Bytes returns the absolute value of x as a big-endian byte slice.
func (x *Int) Bytes() []byte {
	buf := make([]byte, len(x.abs)*_S)
	return buf[x.abs.bytes(buf):]
}

and when deserialize, the bigint just fill for big.NewInt(0), it's a positive number. So the negative sign is dropped.

I think it not in expectation

atenjin avatar Dec 17 '19 04:12 atenjin

@whyrusleeping this seems a like bug... can you have a look or nominate someone familiar to do it?

hsanjuan avatar Mar 06 '20 17:03 hsanjuan