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.

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