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.

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