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.

329 lines
7.8 KiB

  1. package arbo
  2. import (
  3. "encoding/hex"
  4. "math/big"
  5. "testing"
  6. qt "github.com/frankban/quicktest"
  7. "go.vocdoni.io/dvote/db/badgerdb"
  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. database, err := badgerdb.New(badgerdb.Options{Path: c.TempDir()})
  68. c.Assert(err, qt.IsNil)
  69. tree, err := NewTree(database, maxLevels, HashFunctionSha256)
  70. c.Assert(err, qt.IsNil)
  71. for i := 0; i < len(keys); i++ {
  72. err := tree.Add(keys[i], values[i])
  73. c.Assert(err, qt.IsNil)
  74. }
  75. // virtual tree
  76. vTree := newVT(maxLevels, HashFunctionSha256)
  77. c.Assert(vTree.root, qt.IsNil)
  78. for i := 0; i < len(keys); i++ {
  79. err := vTree.add(0, keys[i], values[i])
  80. c.Assert(err, qt.IsNil)
  81. }
  82. // compute hashes, and check Root
  83. _, err = vTree.computeHashes()
  84. c.Assert(err, qt.IsNil)
  85. root, err := tree.Root()
  86. c.Assert(err, qt.IsNil)
  87. c.Assert(vTree.root.h, qt.DeepEquals, root)
  88. }
  89. func TestVirtualTreeAddBatch(t *testing.T) {
  90. c := qt.New(t)
  91. nLeafs := 2000
  92. maxLevels := 100
  93. keys := make([][]byte, nLeafs)
  94. values := make([][]byte, nLeafs)
  95. for i := 0; i < nLeafs; i++ {
  96. keys[i] = randomBytes(32)
  97. values[i] = randomBytes(32)
  98. }
  99. // normal tree, to have an expected root value
  100. database, err := badgerdb.New(badgerdb.Options{Path: c.TempDir()})
  101. c.Assert(err, qt.IsNil)
  102. tree, err := NewTree(database, maxLevels, HashFunctionBlake2b)
  103. c.Assert(err, qt.IsNil)
  104. for i := 0; i < len(keys); i++ {
  105. err := tree.Add(keys[i], values[i])
  106. c.Assert(err, qt.IsNil)
  107. }
  108. // virtual tree
  109. vTree := newVT(maxLevels, HashFunctionBlake2b)
  110. c.Assert(vTree.root, qt.IsNil)
  111. invalids, err := vTree.addBatch(keys, values)
  112. c.Assert(err, qt.IsNil)
  113. c.Assert(len(invalids), qt.Equals, 0)
  114. // compute hashes, and check Root
  115. _, err = vTree.computeHashes()
  116. c.Assert(err, qt.IsNil)
  117. root, err := tree.Root()
  118. c.Assert(err, qt.IsNil)
  119. c.Assert(vTree.root.h, qt.DeepEquals, root)
  120. }
  121. func TestVirtualTreeAddBatchFullyUsed(t *testing.T) {
  122. c := qt.New(t)
  123. vTree1 := newVT(7, HashFunctionPoseidon) // used for add one by one
  124. vTree2 := newVT(7, HashFunctionPoseidon) // used for addBatch
  125. var keys, values [][]byte
  126. for i := 0; i < 128; i++ {
  127. k := BigIntToBytes(32, big.NewInt(int64(i)))
  128. v := k
  129. keys = append(keys, k)
  130. values = append(values, v)
  131. // add one by one expecting no error
  132. err := vTree1.add(0, k, v)
  133. c.Assert(err, qt.IsNil)
  134. }
  135. invalids, err := vTree2.addBatch(keys, values)
  136. c.Assert(err, qt.IsNil)
  137. c.Assert(0, qt.Equals, len(invalids))
  138. }
  139. func TestGetNodesAtLevel(t *testing.T) {
  140. c := qt.New(t)
  141. tree0 := vt{
  142. params: &params{
  143. maxLevels: 100,
  144. hashFunction: HashFunctionBlake2b,
  145. emptyHash: make([]byte, HashFunctionBlake2b.Len()),
  146. },
  147. root: nil,
  148. }
  149. tree1 := vt{
  150. params: &params{
  151. maxLevels: 100,
  152. hashFunction: HashFunctionBlake2b,
  153. emptyHash: make([]byte, HashFunctionBlake2b.Len()),
  154. },
  155. root: &node{
  156. l: &node{
  157. l: &node{
  158. k: []byte{0, 0, 0, 0},
  159. v: []byte{0, 0, 0, 0},
  160. },
  161. r: &node{
  162. k: []byte{0, 0, 0, 1},
  163. v: []byte{0, 0, 0, 1},
  164. },
  165. },
  166. r: &node{
  167. l: &node{
  168. k: []byte{0, 0, 0, 2},
  169. v: []byte{0, 0, 0, 2},
  170. },
  171. r: &node{
  172. k: []byte{0, 0, 0, 3},
  173. v: []byte{0, 0, 0, 3},
  174. },
  175. },
  176. },
  177. }
  178. // tree1.printGraphviz()
  179. tree2 := vt{
  180. params: &params{
  181. maxLevels: 100,
  182. hashFunction: HashFunctionBlake2b,
  183. emptyHash: make([]byte, HashFunctionBlake2b.Len()),
  184. },
  185. root: &node{
  186. l: nil,
  187. r: &node{
  188. l: &node{
  189. l: &node{
  190. l: &node{
  191. k: []byte{0, 0, 0, 0},
  192. v: []byte{0, 0, 0, 0},
  193. },
  194. r: &node{
  195. k: []byte{0, 0, 0, 1},
  196. v: []byte{0, 0, 0, 1},
  197. },
  198. },
  199. r: &node{
  200. k: []byte{0, 0, 0, 2},
  201. v: []byte{0, 0, 0, 2},
  202. },
  203. },
  204. r: &node{
  205. k: []byte{0, 0, 0, 3},
  206. v: []byte{0, 0, 0, 3},
  207. },
  208. },
  209. },
  210. }
  211. // tree2.printGraphviz()
  212. tree3 := vt{
  213. params: &params{
  214. maxLevels: 100,
  215. hashFunction: HashFunctionBlake2b,
  216. emptyHash: make([]byte, HashFunctionBlake2b.Len()),
  217. },
  218. root: &node{
  219. l: nil,
  220. r: &node{
  221. l: &node{
  222. l: &node{
  223. l: &node{
  224. k: []byte{0, 0, 0, 0},
  225. v: []byte{0, 0, 0, 0},
  226. },
  227. r: &node{
  228. k: []byte{0, 0, 0, 1},
  229. v: []byte{0, 0, 0, 1},
  230. },
  231. },
  232. r: &node{
  233. k: []byte{0, 0, 0, 2},
  234. v: []byte{0, 0, 0, 2},
  235. },
  236. },
  237. r: nil,
  238. },
  239. },
  240. }
  241. // tree3.printGraphviz()
  242. nodes0, err := tree0.getNodesAtLevel(2)
  243. c.Assert(err, qt.IsNil)
  244. c.Assert(len(nodes0), qt.DeepEquals, 4)
  245. c.Assert("0000", qt.DeepEquals, getNotNils(nodes0))
  246. nodes1, err := tree1.getNodesAtLevel(2)
  247. c.Assert(err, qt.IsNil)
  248. c.Assert(len(nodes1), qt.DeepEquals, 4)
  249. c.Assert("1111", qt.DeepEquals, getNotNils(nodes1))
  250. nodes1, err = tree1.getNodesAtLevel(3)
  251. c.Assert(err, qt.IsNil)
  252. c.Assert(len(nodes1), qt.DeepEquals, 8)
  253. c.Assert("00000000", qt.DeepEquals, getNotNils(nodes1))
  254. nodes2, err := tree2.getNodesAtLevel(2)
  255. c.Assert(err, qt.IsNil)
  256. c.Assert(len(nodes2), qt.DeepEquals, 4)
  257. c.Assert("0011", qt.DeepEquals, getNotNils(nodes2))
  258. nodes2, err = tree2.getNodesAtLevel(3)
  259. c.Assert(err, qt.IsNil)
  260. c.Assert(len(nodes2), qt.DeepEquals, 8)
  261. c.Assert("00001100", qt.DeepEquals, getNotNils(nodes2))
  262. nodes3, err := tree3.getNodesAtLevel(2)
  263. c.Assert(err, qt.IsNil)
  264. c.Assert(len(nodes3), qt.DeepEquals, 4)
  265. c.Assert("0010", qt.DeepEquals, getNotNils(nodes3))
  266. nodes3, err = tree3.getNodesAtLevel(3)
  267. c.Assert(err, qt.IsNil)
  268. c.Assert(len(nodes3), qt.DeepEquals, 8)
  269. c.Assert("00001100", qt.DeepEquals, getNotNils(nodes3))
  270. nodes3, err = tree3.getNodesAtLevel(4)
  271. c.Assert(err, qt.IsNil)
  272. c.Assert(len(nodes3), qt.DeepEquals, 16)
  273. c.Assert("0000000011000000", qt.DeepEquals, getNotNils(nodes3))
  274. }
  275. func getNotNils(nodes []*node) string {
  276. s := ""
  277. for i := 0; i < len(nodes); i++ {
  278. if nodes[i] == nil {
  279. s += "0"
  280. } else {
  281. s += "1"
  282. }
  283. }
  284. return s
  285. }