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.

114 lines
4.1 KiB

4 years ago
4 years ago
4 years ago
  1. package common
  2. import (
  3. "math/big"
  4. ethCommon "github.com/ethereum/go-ethereum/common"
  5. "github.com/iden3/go-iden3-crypto/babyjub"
  6. )
  7. const (
  8. fromBJJCompressedB = 256
  9. fromEthAddrB = 160
  10. f16B = 16
  11. tokenIDB = 32
  12. cidXB = 32
  13. )
  14. // L1Tx is a struct that represents a L1 tx
  15. type L1Tx struct {
  16. // Stored in DB: mandatory fileds
  17. TxID TxID `meddler:"tx_id"`
  18. ToForgeL1TxsNum uint32 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
  19. Position int `meddler:"position"`
  20. UserOrigin bool `meddler:"user_origin"` // true if the tx was originated by a user, false if it was aoriginated by a coordinator. Note that this differ from the spec for implementation simplification purpposes
  21. FromIdx Idx `meddler:"from_idx"` // FromIdx is used by L1Tx/Deposit to indicate the Idx receiver of the L1Tx.LoadAmount (deposit)
  22. FromEthAddr ethCommon.Address `meddler:"from_eth_addr"`
  23. FromBJJ *babyjub.PublicKey `meddler:"from_bjj"`
  24. ToIdx Idx `meddler:"to_idx"` // ToIdx is ignored in L1Tx/Deposit, but used in the L1Tx/DepositAndTransfer
  25. TokenID TokenID `meddler:"token_id"`
  26. Amount *big.Int `meddler:"amount,bigint"`
  27. LoadAmount *big.Int `meddler:"load_amount,bigint"`
  28. EthBlockNum uint64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue
  29. Type TxType `meddler:"tx_type"`
  30. BatchNum BatchNum `meddler:"-"`
  31. }
  32. // Tx returns a *Tx from the L1Tx
  33. func (tx *L1Tx) Tx() *Tx {
  34. return &Tx{
  35. TxID: tx.TxID,
  36. FromIdx: tx.FromIdx,
  37. ToIdx: tx.ToIdx,
  38. Amount: tx.Amount,
  39. Type: tx.Type,
  40. }
  41. }
  42. // Bytes encodes a L1Tx into []byte
  43. func (tx *L1Tx) Bytes(nLevels int) []byte {
  44. res := big.NewInt(0)
  45. res = res.Add(res, big.NewInt(0).Or(big.NewInt(0), tx.ToIdx.BigInt()))
  46. res = res.Add(res, big.NewInt(0).Lsh(big.NewInt(0).Or(big.NewInt(0), big.NewInt(int64(tx.TokenID))), uint(nLevels)))
  47. res = res.Add(res, big.NewInt(0).Lsh(big.NewInt(0).Or(big.NewInt(0), tx.Amount), uint(nLevels+tokenIDB)))
  48. res = res.Add(res, big.NewInt(0).Lsh(big.NewInt(0).Or(big.NewInt(0), tx.LoadAmount), uint(nLevels+tokenIDB+f16B)))
  49. res = res.Add(res, big.NewInt(0).Lsh(big.NewInt(0).Or(big.NewInt(0), tx.FromIdx.BigInt()), uint(nLevels+tokenIDB+2*f16B)))
  50. fromBJJ := big.NewInt(0)
  51. fromBJJ.SetString(tx.FromBJJ.String(), 16)
  52. fromBJJCompressed := big.NewInt(0).Or(big.NewInt(0), fromBJJ)
  53. res = res.Add(res, big.NewInt(0).Lsh(fromBJJCompressed, uint(2*nLevels+tokenIDB+2*f16B)))
  54. fromEthAddr := big.NewInt(0).Or(big.NewInt(0), tx.FromEthAddr.Hash().Big())
  55. res = res.Add(res, big.NewInt(0).Lsh(fromEthAddr, uint(fromBJJCompressedB+2*nLevels+tokenIDB+2*f16B)))
  56. return res.Bytes()
  57. }
  58. // L1TxFromBytes decodes a L1Tx from []byte
  59. func L1TxFromBytes(l1TxEncoded []byte) (*L1Tx, error) {
  60. l1Tx := &L1Tx{}
  61. var idxB uint = cidXB
  62. l1TxEncodedBI := big.NewInt(0)
  63. l1TxEncodedBI.SetBytes(l1TxEncoded)
  64. toIdx, err := IdxFromBigInt(extract(l1TxEncodedBI, 0, idxB))
  65. if err != nil {
  66. return nil, err
  67. }
  68. l1Tx.ToIdx = toIdx
  69. l1Tx.TokenID = TokenID(extract(l1TxEncodedBI, idxB, tokenIDB).Uint64())
  70. l1Tx.Amount = extract(l1TxEncodedBI, idxB+tokenIDB, f16B)
  71. l1Tx.LoadAmount = extract(l1TxEncodedBI, idxB+tokenIDB+f16B, f16B)
  72. fromIdx, err := IdxFromBigInt(extract(l1TxEncodedBI, idxB+tokenIDB+2*f16B, f16B))
  73. if err != nil {
  74. return nil, err
  75. }
  76. l1Tx.FromIdx = fromIdx
  77. var pkComp babyjub.PublicKeyComp
  78. copy(pkComp[:], extract(l1TxEncodedBI, 2*idxB+tokenIDB+2*f16B, fromBJJCompressedB).Bytes())
  79. pk, err := pkComp.Decompress()
  80. if err != nil {
  81. return nil, err
  82. }
  83. l1Tx.FromBJJ = pk
  84. l1Tx.FromEthAddr = ethCommon.BigToAddress(extract(l1TxEncodedBI, fromBJJCompressedB+2*idxB+tokenIDB+2*f16B, fromEthAddrB))
  85. return l1Tx, nil
  86. }
  87. // extract masks and shifts a bigInt
  88. func extract(num *big.Int, origin uint, len uint) *big.Int {
  89. mask := big.NewInt(0).Sub(big.NewInt(0).Lsh(big.NewInt(1), len), big.NewInt(1))
  90. return big.NewInt(0).And(big.NewInt(0).Rsh(num, origin), mask)
  91. }