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