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.

133 lines
4.0 KiB

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