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.3 KiB

  1. package common
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "fmt"
  6. "math/big"
  7. ethCommon "github.com/ethereum/go-ethereum/common"
  8. "github.com/iden3/go-iden3-crypto/babyjub"
  9. "github.com/iden3/go-iden3-crypto/poseidon"
  10. cryptoUtils "github.com/iden3/go-iden3-crypto/utils"
  11. )
  12. const NLEAFELEMS = 4
  13. // Account is a struct that gives information of the holdings of an address and a specific token. Is the data structure that generates the Value stored in the leaf of the MerkleTree
  14. type Account struct {
  15. TokenID TokenID
  16. Nonce uint64 // max of 40 bits used
  17. Balance *big.Int // max of 192 bits used
  18. PublicKey *babyjub.PublicKey
  19. EthAddr ethCommon.Address
  20. }
  21. func (a *Account) String() string {
  22. buf := bytes.NewBufferString("")
  23. fmt.Fprintf(buf, "PublicKey: %s..., ", a.PublicKey.String()[:10])
  24. fmt.Fprintf(buf, "EthAddr: %s..., ", a.EthAddr.String()[:10])
  25. fmt.Fprintf(buf, "TokenID: %v, ", a.TokenID)
  26. fmt.Fprintf(buf, "Nonce: %d, ", a.Nonce)
  27. fmt.Fprintf(buf, "Balance: %s, ", a.Balance.String())
  28. return buf.String()
  29. }
  30. // Bytes returns the bytes representing the Account, in a way that each BigInt is represented by 32 bytes, in spite of the BigInt could be represented in less bytes (due a small big.Int), so in this way each BigInt is always 32 bytes and can be automatically parsed from a byte array.
  31. func (a *Account) Bytes() ([32 * NLEAFELEMS]byte, error) {
  32. var b [32 * NLEAFELEMS]byte
  33. if a.Nonce > 0xffffffffff {
  34. return b, fmt.Errorf("%s Nonce", ErrNumOverflow)
  35. }
  36. if len(a.Balance.Bytes()) > 24 {
  37. return b, fmt.Errorf("%s Balance", ErrNumOverflow)
  38. }
  39. var nonceBytes [8]byte
  40. binary.LittleEndian.PutUint64(nonceBytes[:], a.Nonce)
  41. copy(b[0:4], a.TokenID.Bytes())
  42. copy(b[4:9], nonceBytes[:])
  43. if babyjub.PointCoordSign(a.PublicKey.X) {
  44. b[10] = 1
  45. }
  46. copy(b[32:64], SwapEndianness(a.Balance.Bytes())) // SwapEndianness, as big.Int uses BigEndian
  47. copy(b[64:96], SwapEndianness(a.PublicKey.Y.Bytes()))
  48. copy(b[96:116], a.EthAddr.Bytes())
  49. return b, nil
  50. }
  51. // BigInts returns the [5]*big.Int, where each *big.Int is inside the Finite Field
  52. func (a *Account) BigInts() ([NLEAFELEMS]*big.Int, error) {
  53. e := [NLEAFELEMS]*big.Int{}
  54. b, err := a.Bytes()
  55. if err != nil {
  56. return e, err
  57. }
  58. e[0] = new(big.Int).SetBytes(SwapEndianness(b[0:32]))
  59. e[1] = new(big.Int).SetBytes(SwapEndianness(b[32:64]))
  60. e[2] = new(big.Int).SetBytes(SwapEndianness(b[64:96]))
  61. e[3] = new(big.Int).SetBytes(SwapEndianness(b[96:128]))
  62. return e, nil
  63. }
  64. // HashValue returns the value of the Account, which is the Poseidon hash of its *big.Int representation
  65. func (a *Account) HashValue() (*big.Int, error) {
  66. b0 := big.NewInt(0)
  67. toHash := []*big.Int{b0, b0, b0, b0, b0, b0}
  68. lBI, err := a.BigInts()
  69. if err != nil {
  70. return nil, err
  71. }
  72. copy(toHash[:], lBI[:])
  73. v, err := poseidon.Hash(toHash)
  74. return v, err
  75. }
  76. // AccountFromBigInts returns a Account from a [5]*big.Int
  77. func AccountFromBigInts(e [NLEAFELEMS]*big.Int) (*Account, error) {
  78. if !cryptoUtils.CheckBigIntArrayInField(e[:]) {
  79. return nil, ErrNotInFF
  80. }
  81. var b [32 * NLEAFELEMS]byte
  82. copy(b[0:32], SwapEndianness(e[0].Bytes())) // SwapEndianness, as big.Int uses BigEndian
  83. copy(b[32:64], SwapEndianness(e[1].Bytes()))
  84. copy(b[64:96], SwapEndianness(e[2].Bytes()))
  85. copy(b[96:128], SwapEndianness(e[3].Bytes()))
  86. return AccountFromBytes(b)
  87. }
  88. // AccountFromBytes returns a Account from a byte array
  89. func AccountFromBytes(b [32 * NLEAFELEMS]byte) (*Account, error) {
  90. tokenID := binary.LittleEndian.Uint32(b[0:4])
  91. var nonceBytes [8]byte
  92. copy(nonceBytes[:], b[4:9])
  93. nonce := binary.LittleEndian.Uint64(nonceBytes[:])
  94. sign := b[10] == 1
  95. balance := new(big.Int).SetBytes(SwapEndianness(b[32:56])) // b[32:56], as Balance is 192 bits (24 bytes)
  96. if !bytes.Equal(b[56:64], []byte{0, 0, 0, 0, 0, 0, 0, 0}) {
  97. return nil, fmt.Errorf("%s Balance", ErrNumOverflow)
  98. }
  99. ay := new(big.Int).SetBytes(SwapEndianness(b[64:96]))
  100. pkPoint, err := babyjub.PointFromSignAndY(sign, ay)
  101. if err != nil {
  102. return nil, err
  103. }
  104. publicKey := babyjub.PublicKey(*pkPoint)
  105. ethAddr := ethCommon.BytesToAddress(b[96:116])
  106. if !cryptoUtils.CheckBigIntInField(balance) {
  107. return nil, ErrNotInFF
  108. }
  109. if !cryptoUtils.CheckBigIntInField(ay) {
  110. return nil, ErrNotInFF
  111. }
  112. a := Account{
  113. TokenID: TokenID(tokenID),
  114. Nonce: nonce,
  115. Balance: balance,
  116. PublicKey: &publicKey,
  117. EthAddr: ethAddr,
  118. }
  119. return &a, nil
  120. }