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.

149 lines
4.5 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 (
  13. NLEAFELEMS = 4
  14. // maxNonceValue is the maximum value that the Account.Nonce can have (40 bits: maxNonceValue=2**40-1)
  15. maxNonceValue = 0xffffffffff
  16. // maxBalanceBytes is the maximum bytes that can use the Account.Balance *big.Int
  17. maxBalanceBytes = 24
  18. )
  19. // 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
  20. type Account struct {
  21. TokenID TokenID
  22. Nonce Nonce // max of 40 bits used
  23. Balance *big.Int // max of 192 bits used
  24. PublicKey *babyjub.PublicKey
  25. EthAddr ethCommon.Address
  26. }
  27. func (a *Account) String() string {
  28. buf := bytes.NewBufferString("")
  29. fmt.Fprintf(buf, "PublicKey: %s..., ", a.PublicKey.String()[:10])
  30. fmt.Fprintf(buf, "EthAddr: %s..., ", a.EthAddr.String()[:10])
  31. fmt.Fprintf(buf, "TokenID: %v, ", a.TokenID)
  32. fmt.Fprintf(buf, "Nonce: %d, ", a.Nonce)
  33. fmt.Fprintf(buf, "Balance: %s, ", a.Balance.String())
  34. return buf.String()
  35. }
  36. // 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.
  37. func (a *Account) Bytes() ([32 * NLEAFELEMS]byte, error) {
  38. var b [32 * NLEAFELEMS]byte
  39. if a.Nonce > maxNonceValue {
  40. return b, fmt.Errorf("%s Nonce", ErrNumOverflow)
  41. }
  42. if len(a.Balance.Bytes()) > maxBalanceBytes {
  43. return b, fmt.Errorf("%s Balance", ErrNumOverflow)
  44. }
  45. nonceBytes, err := a.Nonce.Bytes()
  46. if err != nil {
  47. return b, err
  48. }
  49. copy(b[0:4], a.TokenID.Bytes())
  50. copy(b[4:9], nonceBytes[:])
  51. if babyjub.PointCoordSign(a.PublicKey.X) {
  52. b[10] = 1
  53. }
  54. copy(b[32:64], SwapEndianness(a.Balance.Bytes())) // SwapEndianness, as big.Int uses BigEndian
  55. copy(b[64:96], SwapEndianness(a.PublicKey.Y.Bytes()))
  56. copy(b[96:116], a.EthAddr.Bytes())
  57. return b, nil
  58. }
  59. // BigInts returns the [5]*big.Int, where each *big.Int is inside the Finite Field
  60. func (a *Account) BigInts() ([NLEAFELEMS]*big.Int, error) {
  61. e := [NLEAFELEMS]*big.Int{}
  62. b, err := a.Bytes()
  63. if err != nil {
  64. return e, err
  65. }
  66. e[0] = new(big.Int).SetBytes(SwapEndianness(b[0:32]))
  67. e[1] = new(big.Int).SetBytes(SwapEndianness(b[32:64]))
  68. e[2] = new(big.Int).SetBytes(SwapEndianness(b[64:96]))
  69. e[3] = new(big.Int).SetBytes(SwapEndianness(b[96:128]))
  70. return e, nil
  71. }
  72. // HashValue returns the value of the Account, which is the Poseidon hash of its *big.Int representation
  73. func (a *Account) HashValue() (*big.Int, error) {
  74. b0 := big.NewInt(0)
  75. toHash := []*big.Int{b0, b0, b0, b0, b0, b0}
  76. lBI, err := a.BigInts()
  77. if err != nil {
  78. return nil, err
  79. }
  80. copy(toHash[:], lBI[:])
  81. v, err := poseidon.Hash(toHash)
  82. return v, err
  83. }
  84. // AccountFromBigInts returns a Account from a [5]*big.Int
  85. func AccountFromBigInts(e [NLEAFELEMS]*big.Int) (*Account, error) {
  86. if !cryptoUtils.CheckBigIntArrayInField(e[:]) {
  87. return nil, ErrNotInFF
  88. }
  89. var b [32 * NLEAFELEMS]byte
  90. copy(b[0:32], SwapEndianness(e[0].Bytes())) // SwapEndianness, as big.Int uses BigEndian
  91. copy(b[32:64], SwapEndianness(e[1].Bytes()))
  92. copy(b[64:96], SwapEndianness(e[2].Bytes()))
  93. copy(b[96:128], SwapEndianness(e[3].Bytes()))
  94. return AccountFromBytes(b)
  95. }
  96. // AccountFromBytes returns a Account from a byte array
  97. func AccountFromBytes(b [32 * NLEAFELEMS]byte) (*Account, error) {
  98. tokenID := binary.LittleEndian.Uint32(b[0:4])
  99. var nonceBytes5 [5]byte
  100. copy(nonceBytes5[:], b[4:9])
  101. nonce := NonceFromBytes(nonceBytes5)
  102. sign := b[10] == 1
  103. balance := new(big.Int).SetBytes(SwapEndianness(b[32:56])) // b[32:56], as Balance is 192 bits (24 bytes)
  104. if !bytes.Equal(b[56:64], []byte{0, 0, 0, 0, 0, 0, 0, 0}) {
  105. return nil, fmt.Errorf("%s Balance", ErrNumOverflow)
  106. }
  107. ay := new(big.Int).SetBytes(SwapEndianness(b[64:96]))
  108. pkPoint, err := babyjub.PointFromSignAndY(sign, ay)
  109. if err != nil {
  110. return nil, err
  111. }
  112. publicKey := babyjub.PublicKey(*pkPoint)
  113. ethAddr := ethCommon.BytesToAddress(b[96:116])
  114. if !cryptoUtils.CheckBigIntInField(balance) {
  115. return nil, ErrNotInFF
  116. }
  117. if !cryptoUtils.CheckBigIntInField(ay) {
  118. return nil, ErrNotInFF
  119. }
  120. a := Account{
  121. TokenID: TokenID(tokenID),
  122. Nonce: nonce,
  123. Balance: balance,
  124. PublicKey: &publicKey,
  125. EthAddr: ethAddr,
  126. }
  127. return &a, nil
  128. }