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.

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