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.

162 lines
5.7 KiB

5 years ago
  1. package merkletree
  2. /*
  3. This is just an example of basic tests for the iden3-merkletree-specification.
  4. The methods and variables names can be different.
  5. */
  6. import (
  7. "strconv"
  8. "testing"
  9. common3 "github.com/iden3/go-iden3/common"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. type testBytesLeaf struct {
  13. data []byte
  14. indexLength uint32
  15. }
  16. func (c testBytesLeaf) Bytes() (b []byte) {
  17. return c.data
  18. }
  19. func (c testBytesLeaf) IndexLength() uint32 {
  20. return c.indexLength
  21. }
  22. func (c testBytesLeaf) Hi() Hash {
  23. h := HashBytes(c.Bytes()[:c.IndexLength()])
  24. return h
  25. }
  26. func newTestBytesLeaf(data string, indexLength uint32) testBytesLeaf {
  27. return testBytesLeaf{
  28. data: []byte(data),
  29. indexLength: indexLength,
  30. }
  31. }
  32. // Test to check the iden3-merkletree-specification
  33. func TestIden3MerkletreeSpecification(t *testing.T) {
  34. h := HashBytes([]byte("test")).Hex()
  35. assert.Equal(t, "0x9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658", h)
  36. h = HashBytes([]byte("authorizeksign")).Hex()
  37. assert.Equal(t, "0x353f867ef725411de05e3d4b0a01c37cf7ad24bcc213141a05ed7726d7932a1f", h)
  38. mt := newTestingMerkle(t, 140)
  39. defer mt.storage.Close()
  40. // empty tree
  41. assert.Equal(t, "0x0000000000000000000000000000000000000000000000000000000000000000", mt.Root().Hex())
  42. // add leaf
  43. leaf := testBytesLeaf{
  44. data: []byte("this is a test leaf"),
  45. indexLength: 15,
  46. }
  47. assert.Nil(t, mt.Add(leaf))
  48. assert.Equal(t, "0xb4fdf8a653198f0e179ccb3af7e4fc09d76247f479d6cfc95cd92d6fda589f27", mt.Root().Hex())
  49. // proof with only one leaf in the MerkleTree
  50. proof, err := mt.GenerateProof(leaf.Hi())
  51. assert.Nil(t, err)
  52. assert.Equal(t, "0x0000000000000000000000000000000000000000000000000000000000000000", common3.BytesToHex(proof))
  53. // add a second leaf
  54. leaf2 := testBytesLeaf{
  55. data: []byte("this is a second test leaf"),
  56. indexLength: 15,
  57. }
  58. err = mt.Add(leaf2)
  59. assert.Nil(t, err)
  60. assert.Equal(t, "0x8ac95e9c8a6fbd40bb21de7895ee35f9c8f30ca029dbb0972c02344f49462e82", mt.Root().Hex())
  61. // proof of the second leaf, with two leafs in the MerkleTree
  62. proof2, err := mt.GenerateProof(leaf2.Hi())
  63. assert.Nil(t, err)
  64. assert.Equal(t, "0x0000000000000000000000000000000000000000000000000000000000000001fd8e1a60cdb23c0c7b2cf8462c99fafd905054dccb0ed75e7c8a7d6806749b6b", common3.BytesToHex(proof2))
  65. // proof of emptyLeaf
  66. leaf3 := testBytesLeaf{
  67. data: []byte("this is a third test leaf"),
  68. indexLength: 15,
  69. }
  70. proof3, err := mt.GenerateProof(leaf3.Hi())
  71. assert.Nil(t, err)
  72. assert.Equal(t, "0x000000000000000000000000000000000000000000000000000000000000000389741fa23da77c259781ad8f4331a5a7d793eef1db7e5200ddfc8e5f5ca7ce2bfd8e1a60cdb23c0c7b2cf8462c99fafd905054dccb0ed75e7c8a7d6806749b6b", common3.BytesToHex(proof3))
  73. // getLeafByHi/GetValueInPos
  74. bytesInHi, err := mt.GetValueInPos(leaf2.Hi())
  75. assert.Nil(t, err)
  76. assert.Equal(t, leaf2.Bytes(), bytesInHi)
  77. // check proof
  78. rootBytes, err := common3.HexToBytes("0x7d7c5e8f4b3bf434f3d9d223359c4415e2764dd38de2e025fbf986e976a7ed3d")
  79. assert.Nil(t, err)
  80. mp, err := common3.HexToBytes("0x0000000000000000000000000000000000000000000000000000000000000002d45aada6eec346222eaa6b5d3a9260e08c9b62fcf63c72bc05df284de07e6a52")
  81. assert.Nil(t, err)
  82. hiBytes, err := common3.HexToBytes("0x786677808ba77bdd9090a969f1ef2cbd1ac5aecd9e654f340500159219106878")
  83. assert.Nil(t, err)
  84. htBytes, err := common3.HexToBytes("0x786677808ba77bdd9090a969f1ef2cbd1ac5aecd9e654f340500159219106878")
  85. assert.Nil(t, err)
  86. var root, hi, ht Hash
  87. copy(root[:], rootBytes)
  88. copy(hi[:], hiBytes)
  89. copy(ht[:], htBytes)
  90. verified := CheckProof(root, mp, hi, ht, 140)
  91. assert.True(t, verified)
  92. // check proof of empty
  93. rootBytes, err = common3.HexToBytes("0x8f021d00c39dcd768974ddfe0d21f5d13f7215bea28db1f1cb29842b111332e7")
  94. assert.Nil(t, err)
  95. mp, err = common3.HexToBytes("0x0000000000000000000000000000000000000000000000000000000000000004bf8e980d2ed328ae97f65c30c25520aeb53ff837579e392ea1464934c7c1feb9")
  96. assert.Nil(t, err)
  97. hiBytes, err = common3.HexToBytes("0xa69792a4cff51f40b7a1f7ae596c6ded4aba241646a47538898f17f2a8dff647")
  98. assert.Nil(t, err)
  99. htBytes, err = common3.HexToBytes("0x0000000000000000000000000000000000000000000000000000000000000000")
  100. assert.Nil(t, err)
  101. copy(root[:], rootBytes)
  102. copy(hi[:], hiBytes)
  103. copy(ht[:], htBytes)
  104. verified = CheckProof(root, mp, hi, ht, 140)
  105. assert.True(t, verified)
  106. // check the proof generated in the previous steps
  107. verified = CheckProof(mt.Root(), proof2, leaf2.Hi(), HashBytes(leaf2.Bytes()), 140)
  108. assert.True(t, verified)
  109. // check proof of no existence (emptyLeaf), as we are prooving an empty leaf, the Ht is an empty value (0x000...0)
  110. verified = CheckProof(mt.Root(), proof3, leaf3.Hi(), EmptyNodeValue, 140)
  111. assert.True(t, verified)
  112. // add leafs in different orders
  113. mt1 := newTestingMerkle(t, 140)
  114. defer mt.storage.Close()
  115. mt1.Add(newTestBytesLeaf("0 this is a test leaf", 15))
  116. mt1.Add(newTestBytesLeaf("1 this is a test leaf", 15))
  117. mt1.Add(newTestBytesLeaf("2 this is a test leaf", 15))
  118. mt1.Add(newTestBytesLeaf("3 this is a test leaf", 15))
  119. mt1.Add(newTestBytesLeaf("4 this is a test leaf", 15))
  120. mt2 := newTestingMerkle(t, 140)
  121. defer mt.storage.Close()
  122. mt2.Add(newTestBytesLeaf("2 this is a test leaf", 15))
  123. mt2.Add(newTestBytesLeaf("1 this is a test leaf", 15))
  124. mt2.Add(newTestBytesLeaf("0 this is a test leaf", 15))
  125. mt2.Add(newTestBytesLeaf("3 this is a test leaf", 15))
  126. mt2.Add(newTestBytesLeaf("4 this is a test leaf", 15))
  127. assert.Equal(t, mt1.Root().Hex(), mt2.Root().Hex())
  128. // adding 1000 leafs
  129. mt1000 := newTestingMerkle(t, 140)
  130. defer mt.storage.Close()
  131. numToAdd := 1000
  132. for i := 0; i < numToAdd; i++ {
  133. leaf := newTestBytesLeaf(strconv.Itoa(i)+" this is a test leaf", 15)
  134. mt1000.Add(leaf)
  135. }
  136. assert.Equal(t, "0x6e2da580b2920cd78ed8d4e4bf41e209dfc99ef28bc19560042f0ac803e0d6f7", mt1000.Root().Hex())
  137. }