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.

103 lines
3.0 KiB

  1. // Package common Float40 provides methods to work with Hermez custom half
  2. // float precision, 40 bits, codification internally called Float40 has been
  3. // adopted 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. const (
  14. // maxFloat40Value is the maximum value that the Float40 can have
  15. // (40 bits: maxFloat40Value=2**40-1)
  16. maxFloat40Value = 0xffffffffff
  17. // Float40BytesLength defines the length of the Float40 values
  18. // represented as byte arrays
  19. Float40BytesLength = 5
  20. )
  21. var (
  22. // ErrFloat40Overflow is used when a given Float40 overflows the
  23. // maximum capacity of the Float40 (2**40-1)
  24. ErrFloat40Overflow = errors.New("Float40 overflow, max value: 2**40 -1")
  25. // ErrFloat40E31 is used when the e > 31 when trying to convert a
  26. // *big.Int to Float40
  27. ErrFloat40E31 = errors.New("Float40 error, e > 31")
  28. // ErrFloat40NotEnoughPrecission is used when the given *big.Int can
  29. // not be represented as Float40 due not enough precission
  30. ErrFloat40NotEnoughPrecission = errors.New("Float40 error, not enough precission")
  31. )
  32. // Float40 represents a float in a 64 bit format
  33. type Float40 uint64
  34. // Bytes return a byte array of length 5 with the Float40 value encoded in
  35. // BigEndian
  36. func (f40 Float40) Bytes() ([]byte, error) {
  37. if f40 > maxFloat40Value {
  38. return []byte{}, tracerr.Wrap(ErrFloat40Overflow)
  39. }
  40. var f40Bytes [8]byte
  41. binary.BigEndian.PutUint64(f40Bytes[:], uint64(f40))
  42. var b [5]byte
  43. copy(b[:], f40Bytes[3:])
  44. return b[:], nil
  45. }
  46. // Float40FromBytes returns a Float40 from a byte array of 5 bytes in Bigendian
  47. // representation.
  48. func Float40FromBytes(b []byte) Float40 {
  49. var f40Bytes [8]byte
  50. copy(f40Bytes[3:], b[:])
  51. f40 := binary.BigEndian.Uint64(f40Bytes[:])
  52. return Float40(f40)
  53. }
  54. // BigInt converts the Float40 to a *big.Int v, where v = m * 10^e, being:
  55. // [ e | m ]
  56. // [ 5 bits | 35 bits ]
  57. func (f40 Float40) BigInt() (*big.Int, error) {
  58. // take the 5 used bytes (FF * 5)
  59. var f40Uint64 uint64 = uint64(f40) & 0x00_00_00_FF_FF_FF_FF_FF
  60. f40Bytes, err := f40.Bytes()
  61. if err != nil {
  62. return nil, err
  63. }
  64. e := f40Bytes[0] & 0xF8 >> 3 // take first 5 bits
  65. m := f40Uint64 & 0x07_FF_FF_FF_FF // take the others 35 bits
  66. exp := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(e)), nil)
  67. r := new(big.Int).Mul(big.NewInt(int64(m)), exp)
  68. return r, nil
  69. }
  70. // NewFloat40 encodes a *big.Int integer as a Float40, returning error in case
  71. // of loss during the encoding.
  72. func NewFloat40(f *big.Int) (Float40, error) {
  73. m := f
  74. e := big.NewInt(0)
  75. zero := big.NewInt(0)
  76. ten := big.NewInt(10)
  77. thres := big.NewInt(0x08_00_00_00_00)
  78. for new(big.Int).Mod(m, ten).Cmp(zero) == 0 && m.Cmp(thres) >= 0 {
  79. m = new(big.Int).Div(m, ten)
  80. e = new(big.Int).Add(e, big.NewInt(1))
  81. }
  82. if e.Int64() > 31 {
  83. return 0, ErrFloat40E31
  84. }
  85. if m.Cmp(thres) >= 0 {
  86. return 0, ErrFloat40NotEnoughPrecission
  87. }
  88. r := new(big.Int).Add(m,
  89. new(big.Int).Mul(e, thres))
  90. return Float40(r.Uint64()), nil
  91. }