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.

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