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.

143 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 Nonce // 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. nonceBytes, err := a.Nonce.Bytes()
  40. if err != nil {
  41. return b, err
  42. }
  43. copy(b[0:4], a.TokenID.Bytes())
  44. copy(b[4:9], nonceBytes[:])
  45. if babyjub.PointCoordSign(a.PublicKey.X) {
  46. b[10] = 1
  47. }
  48. copy(b[32:64], SwapEndianness(a.Balance.Bytes())) // SwapEndianness, as big.Int uses BigEndian
  49. copy(b[64:96], SwapEndianness(a.PublicKey.Y.Bytes()))
  50. copy(b[96:116], a.EthAddr.Bytes())
  51. return b, nil
  52. }
  53. // BigInts returns the [5]*big.Int, where each *big.Int is inside the Finite Field
  54. func (a *Account) BigInts() ([NLEAFELEMS]*big.Int, error) {
  55. e := [NLEAFELEMS]*big.Int{}
  56. b, err := a.Bytes()
  57. if err != nil {
  58. return e, err
  59. }
  60. e[0] = new(big.Int).SetBytes(SwapEndianness(b[0:32]))
  61. e[1] = new(big.Int).SetBytes(SwapEndianness(b[32:64]))
  62. e[2] = new(big.Int).SetBytes(SwapEndianness(b[64:96]))
  63. e[3] = new(big.Int).SetBytes(SwapEndianness(b[96:128]))
  64. return e, nil
  65. }
  66. // HashValue returns the value of the Account, which is the Poseidon hash of its *big.Int representation
  67. func (a *Account) HashValue() (*big.Int, error) {
  68. b0 := big.NewInt(0)
  69. toHash := []*big.Int{b0, b0, b0, b0, b0, b0}
  70. lBI, err := a.BigInts()
  71. if err != nil {
  72. return nil, err
  73. }
  74. copy(toHash[:], lBI[:])
  75. v, err := poseidon.Hash(toHash)
  76. return v, err
  77. }
  78. // AccountFromBigInts returns a Account from a [5]*big.Int
  79. func AccountFromBigInts(e [NLEAFELEMS]*big.Int) (*Account, error) {
  80. if !cryptoUtils.CheckBigIntArrayInField(e[:]) {
  81. return nil, ErrNotInFF
  82. }
  83. var b [32 * NLEAFELEMS]byte
  84. copy(b[0:32], SwapEndianness(e[0].Bytes())) // SwapEndianness, as big.Int uses BigEndian
  85. copy(b[32:64], SwapEndianness(e[1].Bytes()))
  86. copy(b[64:96], SwapEndianness(e[2].Bytes()))
  87. copy(b[96:128], SwapEndianness(e[3].Bytes()))
  88. return AccountFromBytes(b)
  89. }
  90. // AccountFromBytes returns a Account from a byte array
  91. func AccountFromBytes(b [32 * NLEAFELEMS]byte) (*Account, error) {
  92. tokenID := binary.LittleEndian.Uint32(b[0:4])
  93. var nonceBytes5 [5]byte
  94. copy(nonceBytes5[:], b[4:9])
  95. nonce := NonceFromBytes(nonceBytes5)
  96. sign := b[10] == 1
  97. balance := new(big.Int).SetBytes(SwapEndianness(b[32:56])) // b[32:56], as Balance is 192 bits (24 bytes)
  98. if !bytes.Equal(b[56:64], []byte{0, 0, 0, 0, 0, 0, 0, 0}) {
  99. return nil, fmt.Errorf("%s Balance", ErrNumOverflow)
  100. }
  101. ay := new(big.Int).SetBytes(SwapEndianness(b[64:96]))
  102. pkPoint, err := babyjub.PointFromSignAndY(sign, ay)
  103. if err != nil {
  104. return nil, err
  105. }
  106. publicKey := babyjub.PublicKey(*pkPoint)
  107. ethAddr := ethCommon.BytesToAddress(b[96:116])
  108. if !cryptoUtils.CheckBigIntInField(balance) {
  109. return nil, ErrNotInFF
  110. }
  111. if !cryptoUtils.CheckBigIntInField(ay) {
  112. return nil, ErrNotInFF
  113. }
  114. a := Account{
  115. TokenID: TokenID(tokenID),
  116. Nonce: nonce,
  117. Balance: balance,
  118. PublicKey: &publicKey,
  119. EthAddr: ethAddr,
  120. }
  121. return &a, nil
  122. }