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.

128 lines
3.6 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/poseidon"
  9. cryptoUtils "github.com/iden3/go-iden3-crypto/utils"
  10. )
  11. const NLEAFELEMS = 4
  12. // Leaf is the data structure stored in the Leaf of the MerkleTree
  13. type Leaf struct {
  14. TokenID TokenID
  15. Nonce uint64 // max of 40 bits used
  16. Balance *big.Int // max of 192 bits used
  17. Sign bool
  18. Ay *big.Int
  19. EthAddr eth.Address
  20. }
  21. // 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.
  22. func (l *Leaf) 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 l.Sign {
  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.Ay.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 *Leaf) 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 Leaf, which is the Poseidon hash of its *big.Int representation
  58. func (l *Leaf) HashValue() (*big.Int, error) {
  59. toHash := [poseidon.T]*big.Int{}
  60. lBI, err := l.BigInts()
  61. if err != nil {
  62. return nil, err
  63. }
  64. copy(toHash[:], lBI[:])
  65. v, err := poseidon.Hash(toHash)
  66. return v, err
  67. }
  68. // LeafFromBigInts returns a Leaf from a [5]*big.Int
  69. func LeafFromBigInts(e [NLEAFELEMS]*big.Int) (*Leaf, error) {
  70. if !cryptoUtils.CheckBigIntArrayInField(e[:]) {
  71. return nil, ErrNotInFF
  72. }
  73. var b [32 * NLEAFELEMS]byte
  74. copy(b[0:32], SwapEndianness(e[0].Bytes())) // SwapEndianness, as big.Int uses BigEndian
  75. copy(b[32:64], SwapEndianness(e[1].Bytes()))
  76. copy(b[64:96], SwapEndianness(e[2].Bytes()))
  77. copy(b[96:128], SwapEndianness(e[3].Bytes()))
  78. return LeafFromBytes(b)
  79. }
  80. // LeafFromBytes returns a Leaf from a byte array
  81. func LeafFromBytes(b [32 * NLEAFELEMS]byte) (*Leaf, error) {
  82. tokenID := binary.LittleEndian.Uint32(b[0:4])
  83. var nonceBytes [8]byte
  84. copy(nonceBytes[:], b[4:9])
  85. nonce := binary.LittleEndian.Uint64(nonceBytes[:])
  86. sign := b[10] == 1
  87. balance := new(big.Int).SetBytes(SwapEndianness(b[32:56])) // b[32:56], as Balance is 192 bits (24 bytes)
  88. if !bytes.Equal(b[56:64], []byte{0, 0, 0, 0, 0, 0, 0, 0}) {
  89. return nil, fmt.Errorf("%s Balance", ErrNumOverflow)
  90. }
  91. ay := new(big.Int).SetBytes(SwapEndianness(b[64:96]))
  92. ethAddr := eth.BytesToAddress(b[96:116])
  93. if !cryptoUtils.CheckBigIntInField(balance) {
  94. return nil, ErrNotInFF
  95. }
  96. if !cryptoUtils.CheckBigIntInField(ay) {
  97. return nil, ErrNotInFF
  98. }
  99. l := Leaf{
  100. TokenID: TokenID(tokenID),
  101. Nonce: nonce,
  102. Balance: balance,
  103. Sign: sign,
  104. Ay: ay,
  105. EthAddr: ethAddr,
  106. }
  107. return &l, nil
  108. }