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.

141 lines
4.0 KiB

  1. package statedb
  2. import (
  3. "bytes"
  4. "errors"
  5. "math/big"
  6. ethCommon "github.com/ethereum/go-ethereum/common"
  7. "github.com/hermeznetwork/hermez-node/common"
  8. "github.com/hermeznetwork/hermez-node/log"
  9. "github.com/iden3/go-iden3-crypto/babyjub"
  10. "github.com/iden3/go-merkletree"
  11. )
  12. func concatEthAddrBJJ(addr *ethCommon.Address, pk *babyjub.PublicKey) []byte {
  13. pkComp := pk.Compress()
  14. var b []byte
  15. b = append(b, addr.Bytes()...)
  16. b = append(b[:], pkComp[:]...)
  17. return b
  18. }
  19. // setIdxByEthAddrBJJ stores the given Idx in the StateDB as follows:
  20. // - key: Eth Address, value: idx
  21. // - key: EthAddr & BabyJubJub PublicKey Compressed, value: idx
  22. // If Idx already exist for the given EthAddr & BJJ, the remaining Idx will be
  23. // always the smallest one.
  24. func (s *StateDB) setIdxByEthAddrBJJ(idx common.Idx, addr *ethCommon.Address, pk *babyjub.PublicKey) error {
  25. oldIdx, err := s.GetIdxByEthAddrBJJ(addr, pk)
  26. if err == nil {
  27. // EthAddr & BJJ already have an Idx
  28. // check which Idx is smaller
  29. // if new idx is smaller, store the new one
  30. // if new idx is bigger, don't store and return, as the used one will be the old
  31. if idx >= oldIdx {
  32. log.Debug("StateDB.setIdxByEthAddrBJJ: Idx not stored because there already exist a smaller Idx for the given EthAddr & BJJ")
  33. return nil
  34. }
  35. }
  36. // store idx for EthAddr & BJJ assuming that EthAddr & BJJ still don't
  37. // have an Idx stored in the DB, and if so, the already stored Idx is
  38. // bigger than the given one, so should be updated to the new one
  39. // (smaller)
  40. tx, err := s.db.NewTx()
  41. if err != nil {
  42. return err
  43. }
  44. k := concatEthAddrBJJ(addr, pk)
  45. // store Addr&BJJ-idx
  46. idxBytes, err := idx.Bytes()
  47. if err != nil {
  48. return err
  49. }
  50. err = tx.Put(k, idxBytes[:])
  51. if err != nil {
  52. return err
  53. }
  54. // store Addr-idx
  55. err = tx.Put(addr.Bytes(), idxBytes[:])
  56. if err != nil {
  57. return err
  58. }
  59. err = tx.Commit()
  60. if err != nil {
  61. return err
  62. }
  63. return nil
  64. }
  65. // GetIdxByEthAddr returns the smallest Idx in the StateDB for the given
  66. // Ethereum Address. Will return common.Idx(0) and error in case that Idx is
  67. // not found in the StateDB.
  68. func (s *StateDB) GetIdxByEthAddr(addr *ethCommon.Address) (common.Idx, error) {
  69. if addr == nil {
  70. return 0, errors.New("addr cannot be nil")
  71. }
  72. b, err := s.db.Get(addr.Bytes())
  73. if err != nil {
  74. return common.Idx(0), ErrToIdxNotFound
  75. }
  76. idx, err := common.IdxFromBytes(b)
  77. if err != nil {
  78. return common.Idx(0), ErrToIdxNotFound
  79. }
  80. return idx, nil
  81. }
  82. // GetIdxByEthAddrBJJ returns the smallest Idx in the StateDB for the given
  83. // Ethereum Address AND the given BabyJubJub PublicKey. If `addr` is the zero
  84. // address, it's ignored in the query. If `pk` is nil, it's ignored in the
  85. // query. Will return common.Idx(0) and error in case that Idx is not found in
  86. // the StateDB.
  87. func (s *StateDB) GetIdxByEthAddrBJJ(addr *ethCommon.Address, pk *babyjub.PublicKey) (common.Idx, error) {
  88. if addr == nil {
  89. return 0, errors.New("addr cannot be nil")
  90. }
  91. if !bytes.Equal(addr.Bytes(), common.EmptyAddr.Bytes()) && pk == nil {
  92. // case ToEthAddr!=0 && ToBJJ=0
  93. return s.GetIdxByEthAddr(addr)
  94. } else if !bytes.Equal(addr.Bytes(), common.EmptyAddr.Bytes()) && pk != nil {
  95. // case ToEthAddr!=0 && ToBJJ!=0
  96. k := concatEthAddrBJJ(addr, pk)
  97. b, err := s.db.Get(k)
  98. if err != nil {
  99. return common.Idx(0), ErrToIdxNotFound
  100. }
  101. idx, err := common.IdxFromBytes(b)
  102. if err != nil {
  103. return common.Idx(0), ErrToIdxNotFound
  104. }
  105. return idx, nil
  106. }
  107. // rest of cases (included case ToEthAddr==0) are not possible
  108. return common.Idx(0), ErrToIdxNotFound
  109. }
  110. func siblingsToZKInputFormat(s []*merkletree.Hash) []*big.Int {
  111. b := make([]*big.Int, len(s))
  112. for i := 0; i < len(s); i++ {
  113. b[i] = s[i].BigInt()
  114. }
  115. return b
  116. }
  117. // BJJCompressedTo256BigInts returns a [256]*big.Int array with the bit
  118. // representation of the babyjub.PublicKeyComp
  119. func BJJCompressedTo256BigInts(pkComp babyjub.PublicKeyComp) [256]*big.Int {
  120. var r [256]*big.Int
  121. b := pkComp[:]
  122. for i := 0; i < 256; i++ {
  123. if b[i/8]&(1<<(i%8)) == 0 {
  124. r[i] = big.NewInt(0)
  125. } else {
  126. r[i] = big.NewInt(1)
  127. }
  128. }
  129. return r
  130. }