You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
2.7 KiB

  1. // Package common Float16 provides methods to work with Hermez custom half float
  2. // precision, 16 bits, codification internally called Float16 has been adopted
  3. // to encode large integers. This is done in order to save bits when L2
  4. // transactions are published.
  5. //nolint:gomnd
  6. package common
  7. import (
  8. "encoding/binary"
  9. "errors"
  10. "math/big"
  11. "github.com/hermeznetwork/tracerr"
  12. )
  13. var (
  14. // ErrRoundingLoss is used when converted big.Int to Float16 causes rounding loss
  15. ErrRoundingLoss = errors.New("input value causes rounding loss")
  16. )
  17. // Float16 represents a float in a 16 bit format
  18. type Float16 uint16
  19. // Bytes return a byte array of length 2 with the Float16 value encoded in BigEndian
  20. func (f16 Float16) Bytes() []byte {
  21. var b [2]byte
  22. binary.BigEndian.PutUint16(b[:], uint16(f16))
  23. return b[:]
  24. }
  25. // Float16FromBytes returns a Float16 from a byte array of 2 bytes.
  26. func Float16FromBytes(b []byte) *Float16 {
  27. f16 := Float16(binary.BigEndian.Uint16(b[:2]))
  28. return &f16
  29. }
  30. // BigInt converts the Float16 to a *big.Int integer
  31. func (f16 *Float16) BigInt() *big.Int {
  32. fl := int64(*f16)
  33. m := big.NewInt(fl & 0x3FF)
  34. e := big.NewInt(fl >> 11)
  35. e5 := (fl >> 10) & 0x01
  36. exp := big.NewInt(0).Exp(big.NewInt(10), e, nil)
  37. res := m.Mul(m, exp)
  38. if e5 != 0 && e.Cmp(big.NewInt(0)) != 0 {
  39. res.Add(res, exp.Div(exp, big.NewInt(2)))
  40. }
  41. return res
  42. }
  43. // floorFix2Float converts a fix to a float, always rounding down
  44. func floorFix2Float(_f *big.Int) Float16 {
  45. zero := big.NewInt(0)
  46. ten := big.NewInt(10)
  47. e := int64(0)
  48. m := big.NewInt(0)
  49. m.Set(_f)
  50. if m.Cmp(zero) == 0 {
  51. return 0
  52. }
  53. s := big.NewInt(0).Rsh(m, 10)
  54. for s.Cmp(zero) != 0 {
  55. m.Div(m, ten)
  56. s.Rsh(m, 10)
  57. e++
  58. }
  59. return Float16(m.Int64() | e<<11)
  60. }
  61. // NewFloat16 encodes a *big.Int integer as a Float16, returning error in
  62. // case of loss during the encoding.
  63. func NewFloat16(f *big.Int) (Float16, error) {
  64. fl1 := floorFix2Float(f)
  65. fi1 := fl1.BigInt()
  66. fl2 := fl1 | 0x400
  67. fi2 := fl2.BigInt()
  68. m3 := (fl1 & 0x3FF) + 1
  69. e3 := fl1 >> 11
  70. if m3&0x400 == 0 {
  71. m3 = 0x66
  72. e3++
  73. }
  74. fl3 := m3 + e3<<11
  75. fi3 := fl3.BigInt()
  76. res := fl1
  77. d := big.NewInt(0).Abs(fi1.Sub(fi1, f))
  78. d2 := big.NewInt(0).Abs(fi2.Sub(fi2, f))
  79. if d.Cmp(d2) == 1 {
  80. res = fl2
  81. d = d2
  82. }
  83. d3 := big.NewInt(0).Abs(fi3.Sub(fi3, f))
  84. if d.Cmp(d3) == 1 {
  85. res = fl3
  86. }
  87. // Do rounding check
  88. if res.BigInt().Cmp(f) == 0 {
  89. return res, nil
  90. }
  91. return res, tracerr.Wrap(ErrRoundingLoss)
  92. }
  93. // NewFloat16Floor encodes a big.Int integer as a Float16, rounding down in
  94. // case of loss during the encoding.
  95. func NewFloat16Floor(f *big.Int) Float16 {
  96. fl1 := floorFix2Float(f)
  97. fl2 := fl1 | 0x400
  98. fi2 := fl2.BigInt()
  99. if fi2.Cmp(f) < 1 {
  100. return fl2
  101. }
  102. return fl1
  103. }