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.

301 lines
7.0 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"
  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 := db.NewBadgerDB(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. c.Assert(vTree.root.h, qt.DeepEquals, tree.root)
  86. }
  87. func TestVirtualTreeAddBatch(t *testing.T) {
  88. c := qt.New(t)
  89. nLeafs := 2000
  90. maxLevels := 100
  91. keys := make([][]byte, nLeafs)
  92. values := make([][]byte, nLeafs)
  93. for i := 0; i < nLeafs; i++ {
  94. keys[i] = randomBytes(32)
  95. values[i] = randomBytes(32)
  96. }
  97. // normal tree, to have an expected root value
  98. database, err := db.NewBadgerDB(c.TempDir())
  99. c.Assert(err, qt.IsNil)
  100. tree, err := NewTree(database, maxLevels, HashFunctionBlake2b)
  101. c.Assert(err, qt.IsNil)
  102. for i := 0; i < len(keys); i++ {
  103. err := tree.Add(keys[i], values[i])
  104. c.Assert(err, qt.IsNil)
  105. }
  106. // virtual tree
  107. vTree := newVT(maxLevels, HashFunctionBlake2b)
  108. c.Assert(vTree.root, qt.IsNil)
  109. invalids, err := vTree.addBatch(keys, values)
  110. c.Assert(err, qt.IsNil)
  111. c.Assert(len(invalids), qt.Equals, 0)
  112. // compute hashes, and check Root
  113. _, err = vTree.computeHashes()
  114. c.Assert(err, qt.IsNil)
  115. c.Assert(vTree.root.h, qt.DeepEquals, tree.root)
  116. }
  117. func TestGetNodesAtLevel(t *testing.T) {
  118. c := qt.New(t)
  119. tree0 := vt{
  120. params: &params{
  121. maxLevels: 100,
  122. hashFunction: HashFunctionBlake2b,
  123. emptyHash: make([]byte, HashFunctionBlake2b.Len()),
  124. },
  125. root: nil,
  126. }
  127. tree1 := vt{
  128. params: &params{
  129. maxLevels: 100,
  130. hashFunction: HashFunctionBlake2b,
  131. emptyHash: make([]byte, HashFunctionBlake2b.Len()),
  132. },
  133. root: &node{
  134. l: &node{
  135. l: &node{
  136. k: []byte{0, 0, 0, 0},
  137. v: []byte{0, 0, 0, 0},
  138. },
  139. r: &node{
  140. k: []byte{0, 0, 0, 1},
  141. v: []byte{0, 0, 0, 1},
  142. },
  143. },
  144. r: &node{
  145. l: &node{
  146. k: []byte{0, 0, 0, 2},
  147. v: []byte{0, 0, 0, 2},
  148. },
  149. r: &node{
  150. k: []byte{0, 0, 0, 3},
  151. v: []byte{0, 0, 0, 3},
  152. },
  153. },
  154. },
  155. }
  156. // tree1.printGraphviz()
  157. tree2 := vt{
  158. params: &params{
  159. maxLevels: 100,
  160. hashFunction: HashFunctionBlake2b,
  161. emptyHash: make([]byte, HashFunctionBlake2b.Len()),
  162. },
  163. root: &node{
  164. l: nil,
  165. r: &node{
  166. l: &node{
  167. l: &node{
  168. l: &node{
  169. k: []byte{0, 0, 0, 0},
  170. v: []byte{0, 0, 0, 0},
  171. },
  172. r: &node{
  173. k: []byte{0, 0, 0, 1},
  174. v: []byte{0, 0, 0, 1},
  175. },
  176. },
  177. r: &node{
  178. k: []byte{0, 0, 0, 2},
  179. v: []byte{0, 0, 0, 2},
  180. },
  181. },
  182. r: &node{
  183. k: []byte{0, 0, 0, 3},
  184. v: []byte{0, 0, 0, 3},
  185. },
  186. },
  187. },
  188. }
  189. // tree2.printGraphviz()
  190. tree3 := vt{
  191. params: &params{
  192. maxLevels: 100,
  193. hashFunction: HashFunctionBlake2b,
  194. emptyHash: make([]byte, HashFunctionBlake2b.Len()),
  195. },
  196. root: &node{
  197. l: nil,
  198. r: &node{
  199. l: &node{
  200. l: &node{
  201. l: &node{
  202. k: []byte{0, 0, 0, 0},
  203. v: []byte{0, 0, 0, 0},
  204. },
  205. r: &node{
  206. k: []byte{0, 0, 0, 1},
  207. v: []byte{0, 0, 0, 1},
  208. },
  209. },
  210. r: &node{
  211. k: []byte{0, 0, 0, 2},
  212. v: []byte{0, 0, 0, 2},
  213. },
  214. },
  215. r: nil,
  216. },
  217. },
  218. }
  219. // tree3.printGraphviz()
  220. nodes0, err := tree0.getNodesAtLevel(2)
  221. c.Assert(err, qt.IsNil)
  222. c.Assert(len(nodes0), qt.DeepEquals, 4)
  223. c.Assert("0000", qt.DeepEquals, getNotNils(nodes0))
  224. nodes1, err := tree1.getNodesAtLevel(2)
  225. c.Assert(err, qt.IsNil)
  226. c.Assert(len(nodes1), qt.DeepEquals, 4)
  227. c.Assert("1111", qt.DeepEquals, getNotNils(nodes1))
  228. nodes1, err = tree1.getNodesAtLevel(3)
  229. c.Assert(err, qt.IsNil)
  230. c.Assert(len(nodes1), qt.DeepEquals, 8)
  231. c.Assert("00000000", qt.DeepEquals, getNotNils(nodes1))
  232. nodes2, err := tree2.getNodesAtLevel(2)
  233. c.Assert(err, qt.IsNil)
  234. c.Assert(len(nodes2), qt.DeepEquals, 4)
  235. c.Assert("0011", qt.DeepEquals, getNotNils(nodes2))
  236. nodes2, err = tree2.getNodesAtLevel(3)
  237. c.Assert(err, qt.IsNil)
  238. c.Assert(len(nodes2), qt.DeepEquals, 8)
  239. c.Assert("00001100", qt.DeepEquals, getNotNils(nodes2))
  240. nodes3, err := tree3.getNodesAtLevel(2)
  241. c.Assert(err, qt.IsNil)
  242. c.Assert(len(nodes3), qt.DeepEquals, 4)
  243. c.Assert("0010", qt.DeepEquals, getNotNils(nodes3))
  244. nodes3, err = tree3.getNodesAtLevel(3)
  245. c.Assert(err, qt.IsNil)
  246. c.Assert(len(nodes3), qt.DeepEquals, 8)
  247. c.Assert("00001100", qt.DeepEquals, getNotNils(nodes3))
  248. nodes3, err = tree3.getNodesAtLevel(4)
  249. c.Assert(err, qt.IsNil)
  250. c.Assert(len(nodes3), qt.DeepEquals, 16)
  251. c.Assert("0000000011000000", qt.DeepEquals, getNotNils(nodes3))
  252. }
  253. func getNotNils(nodes []*node) string {
  254. s := ""
  255. for i := 0; i < len(nodes); i++ {
  256. if nodes[i] == nil {
  257. s += "0"
  258. } else {
  259. s += "1"
  260. }
  261. }
  262. return s
  263. }