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.

630 lines
20 KiB

4 years ago
  1. package merkletree
  2. import (
  3. "bytes"
  4. "encoding/hex"
  5. "fmt"
  6. "math/big"
  7. "testing"
  8. "github.com/iden3/go-merkletree/db/memory"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. )
  12. var debug = false
  13. type Fatalable interface {
  14. Fatal(args ...interface{})
  15. }
  16. func newTestingMerkle(f Fatalable, numLevels int) *MerkleTree {
  17. mt, err := NewMerkleTree(memory.NewMemoryStorage(), numLevels)
  18. if err != nil {
  19. f.Fatal(err)
  20. return nil
  21. }
  22. return mt
  23. }
  24. func TestHashParsers(t *testing.T) {
  25. h0 := NewHashFromBigInt(big.NewInt(0))
  26. assert.Equal(t, "0", h0.String())
  27. h1 := NewHashFromBigInt(big.NewInt(1))
  28. assert.Equal(t, "1", h1.String())
  29. h10 := NewHashFromBigInt(big.NewInt(10))
  30. assert.Equal(t, "10", h10.String())
  31. h7l := NewHashFromBigInt(big.NewInt(1234567))
  32. assert.Equal(t, "1234567", h7l.String())
  33. h8l := NewHashFromBigInt(big.NewInt(12345678))
  34. assert.Equal(t, "12345678...", h8l.String())
  35. b, ok := new(big.Int).SetString("4932297968297298434239270129193057052722409868268166443802652458940273154854", 10)
  36. assert.True(t, ok)
  37. h := NewHashFromBigInt(b)
  38. assert.Equal(t, "4932297968297298434239270129193057052722409868268166443802652458940273154854", h.BigInt().String())
  39. assert.Equal(t, "49322979...", h.String())
  40. assert.Equal(t, "0ae794eb9c3d8bbb9002e993fc2ed301dcbd2af5508ed072c375e861f1aa5b26", h.Hex())
  41. }
  42. func TestNewTree(t *testing.T) {
  43. mt, err := NewMerkleTree(memory.NewMemoryStorage(), 10)
  44. assert.Nil(t, err)
  45. assert.Equal(t, "0", mt.Root().String())
  46. // test vectors generated using https://github.com/iden3/circomlib smt.js
  47. err = mt.Add(big.NewInt(1), big.NewInt(2))
  48. assert.Nil(t, err)
  49. assert.Equal(t, "6449712043256457369579901840927028403950625973089336675272087704159094984964", mt.Root().BigInt().String())
  50. err = mt.Add(big.NewInt(33), big.NewInt(44))
  51. assert.Nil(t, err)
  52. assert.Equal(t, "11404118908468506234838877883514126008995570353394659302846433035311596046064", mt.Root().BigInt().String())
  53. err = mt.Add(big.NewInt(1234), big.NewInt(9876))
  54. assert.Nil(t, err)
  55. assert.Equal(t, "12841932325181810040554102151615400973767747666110051836366805309524360490677", mt.Root().BigInt().String())
  56. proof, v, err := mt.GenerateProof(big.NewInt(33), nil)
  57. assert.Nil(t, err)
  58. assert.Equal(t, big.NewInt(44), v)
  59. assert.True(t, VerifyProof(mt.Root(), proof, big.NewInt(33), big.NewInt(44)))
  60. assert.True(t, !VerifyProof(mt.Root(), proof, big.NewInt(33), big.NewInt(45)))
  61. }
  62. func TestAddDifferentOrder(t *testing.T) {
  63. mt1 := newTestingMerkle(t, 140)
  64. defer mt1.db.Close()
  65. for i := 0; i < 16; i++ {
  66. k := big.NewInt(int64(i))
  67. v := big.NewInt(0)
  68. if err := mt1.Add(k, v); err != nil {
  69. t.Fatal(err)
  70. }
  71. }
  72. mt2 := newTestingMerkle(t, 140)
  73. defer mt2.db.Close()
  74. for i := 16 - 1; i >= 0; i-- {
  75. k := big.NewInt(int64(i))
  76. v := big.NewInt(0)
  77. if err := mt2.Add(k, v); err != nil {
  78. t.Fatal(err)
  79. }
  80. }
  81. assert.Equal(t, mt1.Root().Hex(), mt2.Root().Hex())
  82. assert.Equal(t, "0630b27c6f8c7d36d144369ab1ac408552b544ebe96ad642bad6a94a96258e26", mt1.Root().Hex())
  83. }
  84. func TestAddRepeatedIndex(t *testing.T) {
  85. mt := newTestingMerkle(t, 140)
  86. defer mt.db.Close()
  87. k := big.NewInt(int64(3))
  88. v := big.NewInt(int64(12))
  89. if err := mt.Add(k, v); err != nil {
  90. t.Fatal(err)
  91. }
  92. err := mt.Add(k, v)
  93. assert.NotNil(t, err)
  94. assert.Equal(t, err, ErrEntryIndexAlreadyExists)
  95. }
  96. func TestGet(t *testing.T) {
  97. mt := newTestingMerkle(t, 140)
  98. defer mt.db.Close()
  99. for i := 0; i < 16; i++ {
  100. k := big.NewInt(int64(i))
  101. v := big.NewInt(int64(i * 2))
  102. if err := mt.Add(k, v); err != nil {
  103. t.Fatal(err)
  104. }
  105. }
  106. k, v, _, err := mt.Get(big.NewInt(10))
  107. assert.Nil(t, err)
  108. assert.Equal(t, big.NewInt(10), k)
  109. assert.Equal(t, big.NewInt(20), v)
  110. k, v, _, err = mt.Get(big.NewInt(15))
  111. assert.Nil(t, err)
  112. assert.Equal(t, big.NewInt(15), k)
  113. assert.Equal(t, big.NewInt(30), v)
  114. k, v, _, err = mt.Get(big.NewInt(16))
  115. assert.NotNil(t, err)
  116. assert.Equal(t, ErrKeyNotFound, err)
  117. assert.Equal(t, "0", k.String())
  118. assert.Equal(t, "0", v.String())
  119. }
  120. func TestUpdate(t *testing.T) {
  121. mt := newTestingMerkle(t, 140)
  122. defer mt.db.Close()
  123. for i := 0; i < 16; i++ {
  124. k := big.NewInt(int64(i))
  125. v := big.NewInt(int64(i * 2))
  126. if err := mt.Add(k, v); err != nil {
  127. t.Fatal(err)
  128. }
  129. }
  130. _, v, _, err := mt.Get(big.NewInt(10))
  131. assert.Nil(t, err)
  132. assert.Equal(t, big.NewInt(20), v)
  133. _, err = mt.Update(big.NewInt(10), big.NewInt(1024))
  134. assert.Nil(t, err)
  135. _, v, _, err = mt.Get(big.NewInt(10))
  136. assert.Nil(t, err)
  137. assert.Equal(t, big.NewInt(1024), v)
  138. _, err = mt.Update(big.NewInt(1000), big.NewInt(1024))
  139. assert.Equal(t, ErrKeyNotFound, err)
  140. }
  141. func TestUpdate2(t *testing.T) {
  142. mt1 := newTestingMerkle(t, 140)
  143. defer mt1.db.Close()
  144. mt2 := newTestingMerkle(t, 140)
  145. defer mt2.db.Close()
  146. err := mt1.Add(big.NewInt(1), big.NewInt(119))
  147. assert.Nil(t, err)
  148. err = mt1.Add(big.NewInt(2), big.NewInt(229))
  149. assert.Nil(t, err)
  150. err = mt1.Add(big.NewInt(9876), big.NewInt(6789))
  151. assert.Nil(t, err)
  152. err = mt2.Add(big.NewInt(1), big.NewInt(11))
  153. assert.Nil(t, err)
  154. err = mt2.Add(big.NewInt(2), big.NewInt(22))
  155. assert.Nil(t, err)
  156. err = mt2.Add(big.NewInt(9876), big.NewInt(10))
  157. assert.Nil(t, err)
  158. _, err = mt1.Update(big.NewInt(1), big.NewInt(11))
  159. assert.Nil(t, err)
  160. _, err = mt1.Update(big.NewInt(2), big.NewInt(22))
  161. assert.Nil(t, err)
  162. _, err = mt2.Update(big.NewInt(9876), big.NewInt(6789))
  163. assert.Nil(t, err)
  164. assert.Equal(t, mt1.Root(), mt2.Root())
  165. }
  166. func TestGenerateAndVerifyProof128(t *testing.T) {
  167. mt, err := NewMerkleTree(memory.NewMemoryStorage(), 140)
  168. require.Nil(t, err)
  169. defer mt.db.Close()
  170. for i := 0; i < 128; i++ {
  171. k := big.NewInt(int64(i))
  172. v := big.NewInt(0)
  173. if err := mt.Add(k, v); err != nil {
  174. t.Fatal(err)
  175. }
  176. }
  177. proof, v, err := mt.GenerateProof(big.NewInt(42), nil)
  178. assert.Nil(t, err)
  179. assert.Equal(t, "0", v.String())
  180. assert.True(t, VerifyProof(mt.Root(), proof, big.NewInt(42), big.NewInt(0)))
  181. }
  182. func TestTreeLimit(t *testing.T) {
  183. mt, err := NewMerkleTree(memory.NewMemoryStorage(), 5)
  184. require.Nil(t, err)
  185. defer mt.db.Close()
  186. for i := 0; i < 16; i++ {
  187. err = mt.Add(big.NewInt(int64(i)), big.NewInt(int64(i)))
  188. assert.Nil(t, err)
  189. }
  190. // here the tree is full, should not allow to add more data as reaches the maximum number of levels
  191. err = mt.Add(big.NewInt(int64(16)), big.NewInt(int64(16)))
  192. assert.NotNil(t, err)
  193. assert.Equal(t, ErrReachedMaxLevel, err)
  194. }
  195. func TestSiblingsFromProof(t *testing.T) {
  196. mt, err := NewMerkleTree(memory.NewMemoryStorage(), 140)
  197. require.Nil(t, err)
  198. defer mt.db.Close()
  199. for i := 0; i < 64; i++ {
  200. k := big.NewInt(int64(i))
  201. v := big.NewInt(0)
  202. if err := mt.Add(k, v); err != nil {
  203. t.Fatal(err)
  204. }
  205. }
  206. proof, _, err := mt.GenerateProof(big.NewInt(4), nil)
  207. if err != nil {
  208. t.Fatal(err)
  209. }
  210. siblings := SiblingsFromProof(proof)
  211. assert.Equal(t, 6, len(siblings))
  212. assert.Equal(t, "2f59aeef9e5b881609aa56940dba76b5cb1440a794f4eb03ad5e5958dd8b475b", siblings[0].Hex())
  213. assert.Equal(t, "2eb29ffbded0987f36a62aecddf748d2b9bf28326300bfa15e474e0a12abe8c1", siblings[1].Hex())
  214. assert.Equal(t, "0c6ee1298933d073a390cc3d267a8a4d5a7df65a126d3fdc5a16b9c28afddaf4", siblings[2].Hex())
  215. assert.Equal(t, "1575898b0b4e7802a6be130e7b76ede64fe42079b6852eba6af985bd46a34aa9", siblings[3].Hex())
  216. assert.Equal(t, "1d15b701c1fd521841120980c5cbfa86f15b1f22bf1d3079ed0d0314751d7954", siblings[4].Hex())
  217. assert.Equal(t, "1ee00f37756159cfefaa0bce02779460b449a049165f3bb9fef81105bc285d43", siblings[5].Hex())
  218. }
  219. func TestVerifyProofCases(t *testing.T) {
  220. mt := newTestingMerkle(t, 140)
  221. defer mt.DB().Close()
  222. for i := 0; i < 8; i++ {
  223. if err := mt.Add(big.NewInt(int64(i)), big.NewInt(0)); err != nil {
  224. t.Fatal(err)
  225. }
  226. }
  227. // Existence proof
  228. proof, _, err := mt.GenerateProof(big.NewInt(4), nil)
  229. if err != nil {
  230. t.Fatal(err)
  231. }
  232. assert.Equal(t, proof.Existence, true)
  233. assert.True(t, VerifyProof(mt.Root(), proof, big.NewInt(4), big.NewInt(0)))
  234. assert.Equal(t, "0003000000000000000000000000000000000000000000000000000000000007a6d6b46fefe213a6b579844a1bb7ab5c2db4a13f8662d9c5e729c36728f42730211ddfcc8d30ebd157d1d6912769b8e4abdca41e5dc2b57b026a361c091a8c14c748530e61bf8ea80c987657c3d24b134ece1ef8e2d4bd3f74437bf4392a6b1e", hex.EncodeToString(proof.Bytes()))
  235. for i := 8; i < 32; i++ {
  236. proof, _, err = mt.GenerateProof(big.NewInt(int64(i)), nil)
  237. assert.Nil(t, err)
  238. if debug {
  239. fmt.Println(i, proof)
  240. }
  241. }
  242. // Non-existence proof, empty aux
  243. proof, _, err = mt.GenerateProof(big.NewInt(12), nil)
  244. if err != nil {
  245. t.Fatal(err)
  246. }
  247. assert.Equal(t, proof.Existence, false)
  248. // assert.True(t, proof.nodeAux == nil)
  249. assert.True(t, VerifyProof(mt.Root(), proof, big.NewInt(12), big.NewInt(0)))
  250. assert.Equal(t, "0303000000000000000000000000000000000000000000000000000000000007a6d6b46fefe213a6b579844a1bb7ab5c2db4a13f8662d9c5e729c36728f42730211ddfcc8d30ebd157d1d6912769b8e4abdca41e5dc2b57b026a361c091a8c14c748530e61bf8ea80c987657c3d24b134ece1ef8e2d4bd3f74437bf4392a6b1e04000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", hex.EncodeToString(proof.Bytes()))
  251. // Non-existence proof, diff. node aux
  252. proof, _, err = mt.GenerateProof(big.NewInt(10), nil)
  253. if err != nil {
  254. t.Fatal(err)
  255. }
  256. assert.Equal(t, proof.Existence, false)
  257. assert.True(t, proof.NodeAux != nil)
  258. assert.True(t, VerifyProof(mt.Root(), proof, big.NewInt(10), big.NewInt(0)))
  259. assert.Equal(t, "0303000000000000000000000000000000000000000000000000000000000007a6d6b46fefe213a6b579844a1bb7ab5c2db4a13f8662d9c5e729c36728f42730e667e2ca15909c4a23beff18e3cc74348fbd3c1a4c765a5bbbca126c9607a42b77e008a73926f1280f8531b139dc1cacf8d83fcec31d405f5c51b7cbddfe152902000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", hex.EncodeToString(proof.Bytes()))
  260. }
  261. func TestVerifyProofFalse(t *testing.T) {
  262. mt := newTestingMerkle(t, 140)
  263. defer mt.DB().Close()
  264. for i := 0; i < 8; i++ {
  265. if err := mt.Add(big.NewInt(int64(i)), big.NewInt(0)); err != nil {
  266. t.Fatal(err)
  267. }
  268. }
  269. // Invalid existence proof (node used for verification doesn't
  270. // correspond to node in the proof)
  271. proof, _, err := mt.GenerateProof(big.NewInt(int64(4)), nil)
  272. if err != nil {
  273. t.Fatal(err)
  274. }
  275. assert.Equal(t, proof.Existence, true)
  276. assert.True(t, !VerifyProof(mt.Root(), proof, big.NewInt(int64(5)), big.NewInt(int64(5))))
  277. // Invalid non-existence proof (Non-existence proof, diff. node aux)
  278. proof, _, err = mt.GenerateProof(big.NewInt(int64(4)), nil)
  279. if err != nil {
  280. t.Fatal(err)
  281. }
  282. assert.Equal(t, proof.Existence, true)
  283. // Now we change the proof from existence to non-existence, and add e's
  284. // data as auxiliary node.
  285. proof.Existence = false
  286. proof.NodeAux = &NodeAux{Key: NewHashFromBigInt(big.NewInt(int64(4))), Value: NewHashFromBigInt(big.NewInt(4))}
  287. assert.True(t, !VerifyProof(mt.Root(), proof, big.NewInt(int64(4)), big.NewInt(0)))
  288. }
  289. func TestGraphViz(t *testing.T) {
  290. mt, err := NewMerkleTree(memory.NewMemoryStorage(), 10)
  291. assert.Nil(t, err)
  292. mt.Add(big.NewInt(1), big.NewInt(0))
  293. mt.Add(big.NewInt(2), big.NewInt(0))
  294. mt.Add(big.NewInt(3), big.NewInt(0))
  295. mt.Add(big.NewInt(4), big.NewInt(0))
  296. mt.Add(big.NewInt(5), big.NewInt(0))
  297. mt.Add(big.NewInt(100), big.NewInt(0))
  298. // mt.PrintGraphViz(nil)
  299. expected := `digraph hierarchy {
  300. node [fontname=Monospace,fontsize=10,shape=box]
  301. "16053348..." -> {"19137630..." "14119616..."}
  302. "19137630..." -> {"19543983..." "19746229..."}
  303. "19543983..." -> {"empty0" "65773153..."}
  304. "empty0" [style=dashed,label=0];
  305. "65773153..." -> {"73498412..." "empty1"}
  306. "empty1" [style=dashed,label=0];
  307. "73498412..." -> {"53169236..." "empty2"}
  308. "empty2" [style=dashed,label=0];
  309. "53169236..." -> {"73522717..." "34811870..."}
  310. "73522717..." [style=filled];
  311. "34811870..." [style=filled];
  312. "19746229..." [style=filled];
  313. "14119616..." -> {"19419204..." "15569531..."}
  314. "19419204..." -> {"78154875..." "34589916..."}
  315. "78154875..." [style=filled];
  316. "34589916..." [style=filled];
  317. "15569531..." [style=filled];
  318. }
  319. `
  320. w := bytes.NewBufferString("")
  321. mt.GraphViz(w, nil)
  322. assert.Equal(t, []byte(expected), w.Bytes())
  323. }
  324. func TestDelete(t *testing.T) {
  325. mt, err := NewMerkleTree(memory.NewMemoryStorage(), 10)
  326. assert.Nil(t, err)
  327. assert.Equal(t, "0", mt.Root().String())
  328. // test vectors generated using https://github.com/iden3/circomlib smt.js
  329. err = mt.Add(big.NewInt(1), big.NewInt(2))
  330. assert.Nil(t, err)
  331. assert.Equal(t, "6449712043256457369579901840927028403950625973089336675272087704159094984964", mt.Root().BigInt().String())
  332. err = mt.Add(big.NewInt(33), big.NewInt(44))
  333. assert.Nil(t, err)
  334. assert.Equal(t, "11404118908468506234838877883514126008995570353394659302846433035311596046064", mt.Root().BigInt().String())
  335. err = mt.Add(big.NewInt(1234), big.NewInt(9876))
  336. assert.Nil(t, err)
  337. assert.Equal(t, "12841932325181810040554102151615400973767747666110051836366805309524360490677", mt.Root().BigInt().String())
  338. // mt.PrintGraphViz(nil)
  339. err = mt.Delete(big.NewInt(33))
  340. // mt.PrintGraphViz(nil)
  341. assert.Nil(t, err)
  342. assert.Equal(t, "16195585003843604118922861401064871511855368913846540536604351220077317790615", mt.Root().BigInt().String())
  343. err = mt.Delete(big.NewInt(1234))
  344. assert.Nil(t, err)
  345. err = mt.Delete(big.NewInt(1))
  346. assert.Nil(t, err)
  347. assert.Equal(t, "0", mt.Root().String())
  348. }
  349. func TestDelete2(t *testing.T) {
  350. mt := newTestingMerkle(t, 140)
  351. defer mt.db.Close()
  352. for i := 0; i < 8; i++ {
  353. k := big.NewInt(int64(i))
  354. v := big.NewInt(0)
  355. if err := mt.Add(k, v); err != nil {
  356. t.Fatal(err)
  357. }
  358. }
  359. expectedRoot := mt.Root()
  360. k := big.NewInt(8)
  361. v := big.NewInt(0)
  362. err := mt.Add(k, v)
  363. require.Nil(t, err)
  364. err = mt.Delete(big.NewInt(8))
  365. assert.Nil(t, err)
  366. assert.Equal(t, expectedRoot, mt.Root())
  367. mt2 := newTestingMerkle(t, 140)
  368. defer mt2.db.Close()
  369. for i := 0; i < 8; i++ {
  370. k := big.NewInt(int64(i))
  371. v := big.NewInt(0)
  372. if err := mt2.Add(k, v); err != nil {
  373. t.Fatal(err)
  374. }
  375. }
  376. assert.Equal(t, mt2.Root(), mt.Root())
  377. }
  378. func TestDelete3(t *testing.T) {
  379. mt := newTestingMerkle(t, 140)
  380. defer mt.db.Close()
  381. err := mt.Add(big.NewInt(1), big.NewInt(1))
  382. assert.Nil(t, err)
  383. err = mt.Add(big.NewInt(2), big.NewInt(2))
  384. assert.Nil(t, err)
  385. assert.Equal(t, "6701939280963330813043570145125351311131831356446202146710280245621673558344", mt.Root().BigInt().String())
  386. err = mt.Delete(big.NewInt(1))
  387. assert.Nil(t, err)
  388. assert.Equal(t, "10304354743004778619823249005484018655542356856535590307973732141291410579841", mt.Root().BigInt().String())
  389. mt2 := newTestingMerkle(t, 140)
  390. defer mt2.db.Close()
  391. err = mt2.Add(big.NewInt(2), big.NewInt(2))
  392. assert.Nil(t, err)
  393. assert.Equal(t, mt2.Root(), mt.Root())
  394. }
  395. func TestDelete4(t *testing.T) {
  396. mt := newTestingMerkle(t, 140)
  397. defer mt.db.Close()
  398. err := mt.Add(big.NewInt(1), big.NewInt(1))
  399. assert.Nil(t, err)
  400. err = mt.Add(big.NewInt(2), big.NewInt(2))
  401. assert.Nil(t, err)
  402. err = mt.Add(big.NewInt(3), big.NewInt(3))
  403. assert.Nil(t, err)
  404. assert.Equal(t, "6989694633650442615746486460134957295274675622748484439660143938730686550248", mt.Root().BigInt().String())
  405. err = mt.Delete(big.NewInt(1))
  406. assert.Nil(t, err)
  407. assert.Equal(t, "1192610901536912535888866440319084773171371421781091005185759505381507049136", mt.Root().BigInt().String())
  408. mt2 := newTestingMerkle(t, 140)
  409. defer mt2.db.Close()
  410. err = mt2.Add(big.NewInt(2), big.NewInt(2))
  411. assert.Nil(t, err)
  412. err = mt2.Add(big.NewInt(3), big.NewInt(3))
  413. assert.Nil(t, err)
  414. assert.Equal(t, mt2.Root(), mt.Root())
  415. }
  416. func TestDelete5(t *testing.T) {
  417. mt, err := NewMerkleTree(memory.NewMemoryStorage(), 10)
  418. assert.Nil(t, err)
  419. err = mt.Add(big.NewInt(1), big.NewInt(2))
  420. assert.Nil(t, err)
  421. err = mt.Add(big.NewInt(33), big.NewInt(44))
  422. assert.Nil(t, err)
  423. assert.Equal(t, "11404118908468506234838877883514126008995570353394659302846433035311596046064", mt.Root().BigInt().String())
  424. err = mt.Delete(big.NewInt(1))
  425. assert.Nil(t, err)
  426. assert.Equal(t, "12802904154263054831102426711825443668153853847661287611768065280921698471037", mt.Root().BigInt().String())
  427. mt2 := newTestingMerkle(t, 140)
  428. defer mt2.db.Close()
  429. err = mt2.Add(big.NewInt(33), big.NewInt(44))
  430. assert.Nil(t, err)
  431. assert.Equal(t, mt2.Root(), mt.Root())
  432. }
  433. func TestDeleteNonExistingKeys(t *testing.T) {
  434. mt, err := NewMerkleTree(memory.NewMemoryStorage(), 10)
  435. assert.Nil(t, err)
  436. err = mt.Add(big.NewInt(1), big.NewInt(2))
  437. assert.Nil(t, err)
  438. err = mt.Add(big.NewInt(33), big.NewInt(44))
  439. assert.Nil(t, err)
  440. err = mt.Delete(big.NewInt(33))
  441. assert.Nil(t, err)
  442. err = mt.Delete(big.NewInt(33))
  443. assert.Equal(t, ErrKeyNotFound, err)
  444. err = mt.Delete(big.NewInt(1))
  445. assert.Nil(t, err)
  446. assert.Equal(t, "0", mt.Root().String())
  447. err = mt.Delete(big.NewInt(33))
  448. assert.Equal(t, ErrKeyNotFound, err)
  449. }
  450. func TestDumpLeafsImportLeafs(t *testing.T) {
  451. mt, err := NewMerkleTree(memory.NewMemoryStorage(), 140)
  452. require.Nil(t, err)
  453. defer mt.db.Close()
  454. for i := 0; i < 10; i++ {
  455. k := big.NewInt(int64(i))
  456. v := big.NewInt(0)
  457. err = mt.Add(k, v)
  458. require.Nil(t, err)
  459. }
  460. d, err := mt.DumpLeafs(nil)
  461. assert.Nil(t, err)
  462. mt2, err := NewMerkleTree(memory.NewMemoryStorage(), 140)
  463. require.Nil(t, err)
  464. defer mt2.db.Close()
  465. err = mt2.ImportDumpedLeafs(d)
  466. assert.Nil(t, err)
  467. assert.Equal(t, mt.Root(), mt2.Root())
  468. }
  469. func TestAddAndGetCircomProof(t *testing.T) {
  470. mt, err := NewMerkleTree(memory.NewMemoryStorage(), 10)
  471. assert.Nil(t, err)
  472. assert.Equal(t, "0", mt.Root().String())
  473. // test vectors generated using https://github.com/iden3/circomlib smt.js
  474. cpp, err := mt.AddAndGetCircomProof(big.NewInt(1), big.NewInt(2))
  475. assert.Nil(t, err)
  476. assert.Equal(t, "0", cpp.OldRoot.String())
  477. assert.Equal(t, "64497120...", cpp.NewRoot.String())
  478. assert.Equal(t, "0", cpp.OldKey.String())
  479. assert.Equal(t, "0", cpp.OldValue.String())
  480. assert.Equal(t, "1", cpp.NewKey.String())
  481. assert.Equal(t, "2", cpp.NewValue.String())
  482. assert.Equal(t, true, cpp.IsOld0)
  483. assert.Equal(t, "[0 0 0 0 0 0 0 0 0 0 0]", fmt.Sprintf("%v", cpp.Siblings))
  484. cpp, err = mt.AddAndGetCircomProof(big.NewInt(33), big.NewInt(44))
  485. assert.Nil(t, err)
  486. assert.Equal(t, "64497120...", cpp.OldRoot.String())
  487. assert.Equal(t, "11404118...", cpp.NewRoot.String())
  488. assert.Equal(t, "1", cpp.OldKey.String())
  489. assert.Equal(t, "2", cpp.OldValue.String())
  490. assert.Equal(t, "33", cpp.NewKey.String())
  491. assert.Equal(t, "44", cpp.NewValue.String())
  492. assert.Equal(t, false, cpp.IsOld0)
  493. assert.Equal(t, "[0 0 0 0 0 0 0 0 0 0 0]", fmt.Sprintf("%v", cpp.Siblings))
  494. cpp, err = mt.AddAndGetCircomProof(big.NewInt(55), big.NewInt(66))
  495. assert.Nil(t, err)
  496. assert.Equal(t, "11404118...", cpp.OldRoot.String())
  497. assert.Equal(t, "18284203...", cpp.NewRoot.String())
  498. assert.Equal(t, "0", cpp.OldKey.String())
  499. assert.Equal(t, "0", cpp.OldValue.String())
  500. assert.Equal(t, "55", cpp.NewKey.String())
  501. assert.Equal(t, "66", cpp.NewValue.String())
  502. assert.Equal(t, true, cpp.IsOld0)
  503. assert.Equal(t, "[0 42948778... 0 0 0 0 0 0 0 0 0]", fmt.Sprintf("%v", cpp.Siblings))
  504. // fmt.Println(cpp)
  505. }
  506. func TestUpdateCircomProcessorProof(t *testing.T) {
  507. mt := newTestingMerkle(t, 10)
  508. defer mt.db.Close()
  509. for i := 0; i < 16; i++ {
  510. k := big.NewInt(int64(i))
  511. v := big.NewInt(int64(i * 2))
  512. if err := mt.Add(k, v); err != nil {
  513. t.Fatal(err)
  514. }
  515. }
  516. _, v, _, err := mt.Get(big.NewInt(10))
  517. assert.Nil(t, err)
  518. assert.Equal(t, big.NewInt(20), v)
  519. // test vectors generated using https://github.com/iden3/circomlib smt.js
  520. cpp, err := mt.Update(big.NewInt(10), big.NewInt(1024))
  521. assert.Nil(t, err)
  522. assert.Equal(t, "14895645...", cpp.OldRoot.String())
  523. assert.Equal(t, "75223641...", cpp.NewRoot.String())
  524. assert.Equal(t, "10", cpp.OldKey.String())
  525. assert.Equal(t, "20", cpp.OldValue.String())
  526. assert.Equal(t, "10", cpp.NewKey.String())
  527. assert.Equal(t, "1024", cpp.NewValue.String())
  528. assert.Equal(t, false, cpp.IsOld0)
  529. assert.Equal(t, "[19625419... 46910949... 18399594... 20473908... 0 0 0 0 0 0 0]", fmt.Sprintf("%v", cpp.Siblings))
  530. }