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.

132 lines
3.7 KiB

  1. package common
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "fmt"
  6. "math"
  7. "math/big"
  8. eth "github.com/ethereum/go-ethereum/common"
  9. "github.com/iden3/go-iden3-crypto/poseidon"
  10. cryptoUtils "github.com/iden3/go-iden3-crypto/utils"
  11. )
  12. const NLEAFELEMS = 4
  13. // Leaf is the data structure stored in the Leaf of the MerkleTree
  14. type Leaf struct {
  15. TokenID TokenID
  16. Nonce uint64 // max of 40 bits used
  17. Balance *big.Int // max of 192 bits used
  18. Sign bool
  19. Ay *big.Int
  20. EthAddr eth.Address
  21. }
  22. // Bytes returns the bytes representing the Leaf, 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.
  23. func (l *Leaf) Bytes() ([32 * NLEAFELEMS]byte, error) {
  24. var b [32 * NLEAFELEMS]byte
  25. if l.Nonce >= uint64(math.Pow(2, 40)) {
  26. return b, fmt.Errorf("%s Nonce", ErrNumOverflow)
  27. }
  28. if len(l.Balance.Bytes()) > 24 {
  29. return b, fmt.Errorf("%s Balance", ErrNumOverflow)
  30. }
  31. var tokenIDBytes [4]byte
  32. binary.LittleEndian.PutUint32(tokenIDBytes[:], uint32(l.TokenID))
  33. var nonceBytes [8]byte
  34. binary.LittleEndian.PutUint64(nonceBytes[:], l.Nonce)
  35. copy(b[0:4], tokenIDBytes[:])
  36. copy(b[4:9], nonceBytes[:])
  37. if l.Sign {
  38. b[10] = 1
  39. }
  40. copy(b[32:64], SwapEndianness(l.Balance.Bytes())) // SwapEndianness, as big.Int uses BigEndian
  41. copy(b[64:96], SwapEndianness(l.Ay.Bytes()))
  42. copy(b[96:116], l.EthAddr.Bytes())
  43. return b, nil
  44. }
  45. // BigInts returns the [5]*big.Int, where each *big.Int is inside the Finite Field
  46. func (l *Leaf) BigInts() ([NLEAFELEMS]*big.Int, error) {
  47. e := [NLEAFELEMS]*big.Int{}
  48. b, err := l.Bytes()
  49. if err != nil {
  50. return e, err
  51. }
  52. e[0] = new(big.Int).SetBytes(SwapEndianness(b[0:32]))
  53. e[1] = new(big.Int).SetBytes(SwapEndianness(b[32:64]))
  54. e[2] = new(big.Int).SetBytes(SwapEndianness(b[64:96]))
  55. e[3] = new(big.Int).SetBytes(SwapEndianness(b[96:128]))
  56. return e, nil
  57. }
  58. // HashValue returns the value of the Leaf, which is the Poseidon hash of its *big.Int representation
  59. func (l *Leaf) HashValue() (*big.Int, error) {
  60. toHash := [poseidon.T]*big.Int{}
  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. // LeafFromBigInts returns a Leaf from a [5]*big.Int
  70. func LeafFromBigInts(e [NLEAFELEMS]*big.Int) (*Leaf, 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 LeafFromBytes(b)
  80. }
  81. // LeafFromBytes returns a Leaf from a byte array
  82. func LeafFromBytes(b [32 * NLEAFELEMS]byte) (*Leaf, 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. if nonce >= uint64(math.Pow(2, 40)) {
  88. return nil, fmt.Errorf("%s Nonce", ErrNumOverflow)
  89. }
  90. sign := b[10] == 1
  91. balance := new(big.Int).SetBytes(SwapEndianness(b[32:56])) // b[32:56], as Balance is 192 bits (24 bytes)
  92. if !bytes.Equal(b[56:64], []byte{0, 0, 0, 0, 0, 0, 0, 0}) {
  93. return nil, fmt.Errorf("%s Balance", ErrNumOverflow)
  94. }
  95. ay := new(big.Int).SetBytes(SwapEndianness(b[64:96]))
  96. ethAddr := eth.BytesToAddress(b[96:116])
  97. if !cryptoUtils.CheckBigIntInField(balance) {
  98. return nil, ErrNotInFF
  99. }
  100. if !cryptoUtils.CheckBigIntInField(ay) {
  101. return nil, ErrNotInFF
  102. }
  103. l := Leaf{
  104. TokenID: TokenID(tokenID),
  105. Nonce: nonce,
  106. Balance: balance,
  107. Sign: sign,
  108. Ay: ay,
  109. EthAddr: ethAddr,
  110. }
  111. return &l, nil
  112. }