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.

134 lines
3.9 KiB

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