encodeBigInt() encodes a big integer as a big-endian byte slice but preserves the sign using the first byte
(i *big.Int)
| 441 | |
| 442 | // encodeBigInt() encodes a big integer as a big-endian byte slice but preserves the sign using the first byte |
| 443 | func encodeBigInt(i *big.Int) []byte { |
| 444 | x := bip.New().Set(i) |
| 445 | defer bip.Recycle(x) |
| 446 | buf := new(bytes.Buffer) |
| 447 | if x.Sign() < 0 { |
| 448 | buf.WriteByte(1) // Negative sign |
| 449 | x.Neg(i) |
| 450 | } else { |
| 451 | buf.WriteByte(0) // Positive sign |
| 452 | } |
| 453 | buf.Write(x.Bytes()) |
| 454 | return buf.Bytes() |
| 455 | } |
| 456 | |
| 457 | // decodeBigInt() decodes a big integer from a big-endian byte slice with a sign byte |
| 458 | func decodeBigInt(data []byte) *big.Int { |