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.

123 lines
2.4 KiB

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