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.

125 lines
3.4 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. err = tx.Put(k, idx.Bytes())
  45. if err != nil {
  46. return err
  47. }
  48. // store Addr-idx
  49. err = tx.Put(addr.Bytes(), idx.Bytes())
  50. if err != nil {
  51. return err
  52. }
  53. err = tx.Commit()
  54. if err != nil {
  55. return err
  56. }
  57. return nil
  58. }
  59. // GetIdxByEthAddr returns the smallest Idx in the StateDB for the given
  60. // Ethereum Address. Will return common.Idx(0) and error in case that Idx is
  61. // not found in the StateDB.
  62. func (s *StateDB) GetIdxByEthAddr(addr ethCommon.Address) (common.Idx, error) {
  63. b, err := s.db.Get(addr.Bytes())
  64. if err != nil {
  65. return common.Idx(0), err
  66. }
  67. idx, err := common.IdxFromBytes(b)
  68. if err != nil {
  69. return common.Idx(0), err
  70. }
  71. return idx, nil
  72. }
  73. // GetIdxByEthAddrBJJ returns the smallest Idx in the StateDB for the given
  74. // Ethereum Address AND the given BabyJubJub PublicKey. If `addr` is the zero
  75. // address, it's ignored in the query. If `pk` is nil, it's ignored in the
  76. // query. Will return common.Idx(0) and error in case that Idx is not found in
  77. // the StateDB.
  78. func (s *StateDB) GetIdxByEthAddrBJJ(addr ethCommon.Address, pk *babyjub.PublicKey) (common.Idx, error) {
  79. if pk == nil {
  80. return s.GetIdxByEthAddr(addr)
  81. }
  82. k := concatEthAddrBJJ(addr, pk)
  83. b, err := s.db.Get(k)
  84. if err != nil {
  85. return common.Idx(0), err
  86. }
  87. idx, err := common.IdxFromBytes(b)
  88. if err != nil {
  89. return common.Idx(0), err
  90. }
  91. return idx, nil
  92. }
  93. func siblingsToZKInputFormat(s []*merkletree.Hash) []*big.Int {
  94. b := make([]*big.Int, len(s))
  95. for i := 0; i < len(s); i++ {
  96. b[i] = s[i].BigInt()
  97. }
  98. return b
  99. }
  100. // BJJCompressedTo256BigInts returns a [256]*big.Int array with the bit
  101. // representation of the babyjub.PublicKeyComp
  102. func BJJCompressedTo256BigInts(pkComp babyjub.PublicKeyComp) [256]*big.Int {
  103. var r [256]*big.Int
  104. b := pkComp[:]
  105. for i := 0; i < 256; i++ {
  106. if b[i/8]&(1<<(i%8)) == 0 {
  107. r[i] = big.NewInt(0)
  108. } else {
  109. r[i] = big.NewInt(1)
  110. }
  111. }
  112. return r
  113. }