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.

117 lines
3.5 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. cryptoUtils "github.com/iden3/go-iden3-crypto/utils"
  10. )
  11. // Leaf is the data structure stored in the Leaf of the MerkleTree
  12. type Leaf struct {
  13. TokenID TokenID
  14. Nonce uint64 // max of 40 bits used
  15. Balance *big.Int // max of 192 bits used
  16. Ax *big.Int
  17. Ay *big.Int
  18. EthAddr eth.Address
  19. }
  20. // 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.
  21. func (l *Leaf) Bytes() ([32 * 5]byte, error) {
  22. var b [32 * 5]byte
  23. if l.Nonce >= uint64(math.Pow(2, 40)) {
  24. return b, fmt.Errorf("%s Nonce", ErrNumOverflow)
  25. }
  26. if len(l.Balance.Bytes()) > 24 {
  27. return b, fmt.Errorf("%s Balance", ErrNumOverflow)
  28. }
  29. var tokenIDBytes [4]byte
  30. binary.LittleEndian.PutUint32(tokenIDBytes[:], uint32(l.TokenID))
  31. var nonceBytes [8]byte
  32. binary.LittleEndian.PutUint64(nonceBytes[:], l.Nonce)
  33. copy(b[0:4], tokenIDBytes[:])
  34. copy(b[4:9], nonceBytes[:])
  35. copy(b[32:64], SwapEndianness(l.Balance.Bytes())) // SwapEndianness, as big.Int uses BigEndian
  36. copy(b[64:96], SwapEndianness(l.Ax.Bytes()))
  37. copy(b[96:128], SwapEndianness(l.Ay.Bytes()))
  38. copy(b[128:148], l.EthAddr.Bytes())
  39. return b, nil
  40. }
  41. // BigInts returns the [5]*big.Int, where each *big.Int is inside the Finite Field
  42. func (l *Leaf) BigInts() ([5]*big.Int, error) {
  43. e := [5]*big.Int{}
  44. b, err := l.Bytes()
  45. if err != nil {
  46. return e, err
  47. }
  48. e[0] = new(big.Int).SetBytes(SwapEndianness(b[0:32]))
  49. e[1] = new(big.Int).SetBytes(SwapEndianness(b[32:64]))
  50. e[2] = new(big.Int).SetBytes(SwapEndianness(b[64:96]))
  51. e[3] = new(big.Int).SetBytes(SwapEndianness(b[96:128]))
  52. e[4] = new(big.Int).SetBytes(SwapEndianness(b[128:160]))
  53. return e, nil
  54. }
  55. // LeafFromBigInts returns a Leaf from a [5]*big.Int
  56. func LeafFromBigInts(e [5]*big.Int) (*Leaf, error) {
  57. if !cryptoUtils.CheckBigIntArrayInField(e[:]) {
  58. return nil, ErrNotInFF
  59. }
  60. var b [32 * 5]byte
  61. copy(b[0:32], SwapEndianness(e[0].Bytes())) // SwapEndianness, as big.Int uses BigEndian
  62. copy(b[32:64], SwapEndianness(e[1].Bytes()))
  63. copy(b[64:96], SwapEndianness(e[2].Bytes()))
  64. copy(b[96:128], SwapEndianness(e[3].Bytes()))
  65. copy(b[128:160], SwapEndianness(e[4].Bytes()))
  66. return LeafFromBytes(b)
  67. }
  68. // LeafFromBytes returns a Leaf from a byte array
  69. func LeafFromBytes(b [32 * 5]byte) (*Leaf, error) {
  70. tokenID := binary.LittleEndian.Uint32(b[0:4])
  71. nonce := binary.LittleEndian.Uint64(b[4:12])
  72. if !bytes.Equal(b[9:12], []byte{0, 0, 0}) { // alternatively: if nonce >= uint64(math.Pow(2, 40)) {
  73. return nil, fmt.Errorf("%s Nonce", ErrNumOverflow)
  74. }
  75. balance := new(big.Int).SetBytes(SwapEndianness(b[32:56])) // b[32:56], as Balance is 192 bits (24 bytes)
  76. if !bytes.Equal(b[56:64], []byte{0, 0, 0, 0, 0, 0, 0, 0}) {
  77. return nil, fmt.Errorf("%s Balance", ErrNumOverflow)
  78. }
  79. ax := new(big.Int).SetBytes(SwapEndianness(b[64:96])) // SwapEndianness, as big.Int uses BigEndian
  80. ay := new(big.Int).SetBytes(SwapEndianness(b[96:128]))
  81. ethAddr := eth.BytesToAddress(b[128:148])
  82. if !cryptoUtils.CheckBigIntInField(balance) {
  83. return nil, ErrNotInFF
  84. }
  85. if !cryptoUtils.CheckBigIntInField(ax) {
  86. return nil, ErrNotInFF
  87. }
  88. if !cryptoUtils.CheckBigIntInField(ay) {
  89. return nil, ErrNotInFF
  90. }
  91. l := Leaf{
  92. TokenID: TokenID(tokenID),
  93. Nonce: nonce,
  94. Balance: balance,
  95. Ax: ax,
  96. Ay: ay,
  97. EthAddr: ethAddr,
  98. }
  99. return &l, nil
  100. }