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.

129 lines
3.5 KiB

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