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.

297 lines
7.0 KiB

  1. package arbo
  2. import (
  3. "encoding/hex"
  4. "math/big"
  5. "testing"
  6. qt "github.com/frankban/quicktest"
  7. "github.com/iden3/go-merkletree/db/memory"
  8. )
  9. func TestVirtualTreeTestVectors(t *testing.T) {
  10. c := qt.New(t)
  11. bLen := 32
  12. keys := [][]byte{
  13. BigIntToBytes(bLen, big.NewInt(1)),
  14. BigIntToBytes(bLen, big.NewInt(33)),
  15. BigIntToBytes(bLen, big.NewInt(1234)),
  16. BigIntToBytes(bLen, big.NewInt(123456789)),
  17. }
  18. values := [][]byte{
  19. BigIntToBytes(bLen, big.NewInt(2)),
  20. BigIntToBytes(bLen, big.NewInt(44)),
  21. BigIntToBytes(bLen, big.NewInt(9876)),
  22. BigIntToBytes(bLen, big.NewInt(987654321)),
  23. }
  24. // check the root for different batches of leafs
  25. testVirtualTree(c, 10, keys[:1], values[:1])
  26. testVirtualTree(c, 10, keys[:2], values[:2])
  27. testVirtualTree(c, 10, keys[:3], values[:3])
  28. testVirtualTree(c, 10, keys[:4], values[:4])
  29. // test with hardcoded values
  30. testvectorKeys := []string{
  31. "1c7c2265e368314ca58ed2e1f33a326f1220e234a566d55c3605439dbe411642",
  32. "2c9f0a578afff5bfa4e0992a43066460faaab9e8e500db0b16647c701cdb16bf",
  33. "9cb87ec67e875c61390edcd1ab517f443591047709a4d4e45b0f9ed980857b8e",
  34. "9b4e9e92e974a589f426ceeb4cb291dc24893513fecf8e8460992dcf52621d4d",
  35. "1c45cb31f2fa39ec7b9ebf0fad40e0b8296016b5ce8844ae06ff77226379d9a5",
  36. "d8af98bbbb585129798ae54d5eabbc9d0561d583faf1663b3a3724d15bda4ec7",
  37. "3cd55dbfb8f975f20a0925dfbdabe79fa2d51dd0268afbb8ba6b01de9dfcdd3c",
  38. "5d0a9d6d9f197c091bf054fac9cb60e11ec723d6610ed8578e617b4d46cb43d5",
  39. }
  40. keys = [][]byte{}
  41. values = [][]byte{}
  42. for i := 0; i < len(testvectorKeys); i++ {
  43. key, err := hex.DecodeString(testvectorKeys[i])
  44. c.Assert(err, qt.IsNil)
  45. keys = append(keys, key)
  46. values = append(values, []byte{0})
  47. }
  48. // check the root for different batches of leafs
  49. testVirtualTree(c, 10, keys[:1], values[:1])
  50. testVirtualTree(c, 10, keys, values)
  51. }
  52. func TestVirtualTreeRandomKeys(t *testing.T) {
  53. c := qt.New(t)
  54. // test with random values
  55. nLeafs := 1024
  56. keys := make([][]byte, nLeafs)
  57. values := make([][]byte, nLeafs)
  58. for i := 0; i < nLeafs; i++ {
  59. keys[i] = randomBytes(32)
  60. values[i] = randomBytes(32)
  61. }
  62. testVirtualTree(c, 100, keys, values)
  63. }
  64. func testVirtualTree(c *qt.C, maxLevels int, keys, values [][]byte) {
  65. c.Assert(len(keys), qt.Equals, len(values))
  66. // normal tree, to have an expected root value
  67. tree, err := NewTree(memory.NewMemoryStorage(), maxLevels, HashFunctionSha256)
  68. c.Assert(err, qt.IsNil)
  69. for i := 0; i < len(keys); i++ {
  70. err := tree.Add(keys[i], values[i])
  71. c.Assert(err, qt.IsNil)
  72. }
  73. // virtual tree
  74. vTree := newVT(maxLevels, HashFunctionSha256)
  75. c.Assert(vTree.root, qt.IsNil)
  76. for i := 0; i < len(keys); i++ {
  77. err := vTree.add(0, keys[i], values[i])
  78. c.Assert(err, qt.IsNil)
  79. }
  80. // compute hashes, and check Root
  81. _, err = vTree.computeHashes()
  82. c.Assert(err, qt.IsNil)
  83. c.Assert(vTree.root.h, qt.DeepEquals, tree.root)
  84. }
  85. func TestVirtualTreeAddBatch(t *testing.T) {
  86. c := qt.New(t)
  87. nLeafs := 2000
  88. maxLevels := 100
  89. keys := make([][]byte, nLeafs)
  90. values := make([][]byte, nLeafs)
  91. for i := 0; i < nLeafs; i++ {
  92. keys[i] = randomBytes(32)
  93. values[i] = randomBytes(32)
  94. }
  95. // normal tree, to have an expected root value
  96. tree, err := NewTree(memory.NewMemoryStorage(), maxLevels, HashFunctionBlake2b)
  97. c.Assert(err, qt.IsNil)
  98. for i := 0; i < len(keys); i++ {
  99. err := tree.Add(keys[i], values[i])
  100. c.Assert(err, qt.IsNil)
  101. }
  102. // virtual tree
  103. vTree := newVT(maxLevels, HashFunctionBlake2b)
  104. c.Assert(vTree.root, qt.IsNil)
  105. invalids, err := vTree.addBatch(keys, values)
  106. c.Assert(err, qt.IsNil)
  107. c.Assert(len(invalids), qt.Equals, 0)
  108. // compute hashes, and check Root
  109. _, err = vTree.computeHashes()
  110. c.Assert(err, qt.IsNil)
  111. c.Assert(vTree.root.h, qt.DeepEquals, tree.root)
  112. }
  113. func TestGetNodesAtLevel(t *testing.T) {
  114. c := qt.New(t)
  115. tree0 := vt{
  116. params: &params{
  117. maxLevels: 100,
  118. hashFunction: HashFunctionBlake2b,
  119. emptyHash: make([]byte, HashFunctionBlake2b.Len()),
  120. },
  121. root: nil,
  122. }
  123. tree1 := vt{
  124. params: &params{
  125. maxLevels: 100,
  126. hashFunction: HashFunctionBlake2b,
  127. emptyHash: make([]byte, HashFunctionBlake2b.Len()),
  128. },
  129. root: &node{
  130. l: &node{
  131. l: &node{
  132. k: []byte{0, 0, 0, 0},
  133. v: []byte{0, 0, 0, 0},
  134. },
  135. r: &node{
  136. k: []byte{0, 0, 0, 1},
  137. v: []byte{0, 0, 0, 1},
  138. },
  139. },
  140. r: &node{
  141. l: &node{
  142. k: []byte{0, 0, 0, 2},
  143. v: []byte{0, 0, 0, 2},
  144. },
  145. r: &node{
  146. k: []byte{0, 0, 0, 3},
  147. v: []byte{0, 0, 0, 3},
  148. },
  149. },
  150. },
  151. }
  152. // tree1.printGraphviz()
  153. tree2 := vt{
  154. params: &params{
  155. maxLevels: 100,
  156. hashFunction: HashFunctionBlake2b,
  157. emptyHash: make([]byte, HashFunctionBlake2b.Len()),
  158. },
  159. root: &node{
  160. l: nil,
  161. r: &node{
  162. l: &node{
  163. l: &node{
  164. l: &node{
  165. k: []byte{0, 0, 0, 0},
  166. v: []byte{0, 0, 0, 0},
  167. },
  168. r: &node{
  169. k: []byte{0, 0, 0, 1},
  170. v: []byte{0, 0, 0, 1},
  171. },
  172. },
  173. r: &node{
  174. k: []byte{0, 0, 0, 2},
  175. v: []byte{0, 0, 0, 2},
  176. },
  177. },
  178. r: &node{
  179. k: []byte{0, 0, 0, 3},
  180. v: []byte{0, 0, 0, 3},
  181. },
  182. },
  183. },
  184. }
  185. // tree2.printGraphviz()
  186. tree3 := vt{
  187. params: &params{
  188. maxLevels: 100,
  189. hashFunction: HashFunctionBlake2b,
  190. emptyHash: make([]byte, HashFunctionBlake2b.Len()),
  191. },
  192. root: &node{
  193. l: nil,
  194. r: &node{
  195. l: &node{
  196. l: &node{
  197. l: &node{
  198. k: []byte{0, 0, 0, 0},
  199. v: []byte{0, 0, 0, 0},
  200. },
  201. r: &node{
  202. k: []byte{0, 0, 0, 1},
  203. v: []byte{0, 0, 0, 1},
  204. },
  205. },
  206. r: &node{
  207. k: []byte{0, 0, 0, 2},
  208. v: []byte{0, 0, 0, 2},
  209. },
  210. },
  211. r: nil,
  212. },
  213. },
  214. }
  215. // tree3.printGraphviz()
  216. nodes0, err := tree0.getNodesAtLevel(2)
  217. c.Assert(err, qt.IsNil)
  218. c.Assert(len(nodes0), qt.DeepEquals, 4)
  219. c.Assert("0000", qt.DeepEquals, getNotNils(nodes0))
  220. nodes1, err := tree1.getNodesAtLevel(2)
  221. c.Assert(err, qt.IsNil)
  222. c.Assert(len(nodes1), qt.DeepEquals, 4)
  223. c.Assert("1111", qt.DeepEquals, getNotNils(nodes1))
  224. nodes1, err = tree1.getNodesAtLevel(3)
  225. c.Assert(err, qt.IsNil)
  226. c.Assert(len(nodes1), qt.DeepEquals, 8)
  227. c.Assert("00000000", qt.DeepEquals, getNotNils(nodes1))
  228. nodes2, err := tree2.getNodesAtLevel(2)
  229. c.Assert(err, qt.IsNil)
  230. c.Assert(len(nodes2), qt.DeepEquals, 4)
  231. c.Assert("0011", qt.DeepEquals, getNotNils(nodes2))
  232. nodes2, err = tree2.getNodesAtLevel(3)
  233. c.Assert(err, qt.IsNil)
  234. c.Assert(len(nodes2), qt.DeepEquals, 8)
  235. c.Assert("00001100", qt.DeepEquals, getNotNils(nodes2))
  236. nodes3, err := tree3.getNodesAtLevel(2)
  237. c.Assert(err, qt.IsNil)
  238. c.Assert(len(nodes3), qt.DeepEquals, 4)
  239. c.Assert("0010", qt.DeepEquals, getNotNils(nodes3))
  240. nodes3, err = tree3.getNodesAtLevel(3)
  241. c.Assert(err, qt.IsNil)
  242. c.Assert(len(nodes3), qt.DeepEquals, 8)
  243. c.Assert("00001100", qt.DeepEquals, getNotNils(nodes3))
  244. nodes3, err = tree3.getNodesAtLevel(4)
  245. c.Assert(err, qt.IsNil)
  246. c.Assert(len(nodes3), qt.DeepEquals, 16)
  247. c.Assert("0000000011000000", qt.DeepEquals, getNotNils(nodes3))
  248. }
  249. func getNotNils(nodes []*node) string {
  250. s := ""
  251. for i := 0; i < len(nodes); i++ {
  252. if nodes[i] == nil {
  253. s += "0"
  254. } else {
  255. s += "1"
  256. }
  257. }
  258. return s
  259. }