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.

951 lines
31 KiB

  1. //nolint:gomnd,golint
  2. package test
  3. import (
  4. "bytes"
  5. "encoding/hex"
  6. "encoding/json"
  7. "fmt"
  8. "github.com/iden3/go-iden3-crypto/constants"
  9. "github.com/iden3/go-merkletree"
  10. "github.com/stretchr/testify/require"
  11. "math/big"
  12. "testing"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. var debug = false
  16. func newTestingMerkle(t *testing.T, sto merkletree.Storage, numLevels int) *merkletree.MerkleTree {
  17. mt, err := merkletree.NewMerkleTree(sto, numLevels)
  18. if err != nil {
  19. t.Fatal(err)
  20. return nil
  21. }
  22. return mt
  23. }
  24. // TestReturnKnownErrIfNotExists checks that the implementation of the
  25. // db.Storage interface returns the expected error in the case that the value
  26. // is not found
  27. func TestReturnKnownErrIfNotExists(t *testing.T, sto merkletree.Storage) {
  28. //defer sto.Close()
  29. k := []byte("key")
  30. tx, err := sto.NewTx()
  31. defer tx.Close()
  32. assert.Nil(t, err)
  33. _, err = tx.Get(k)
  34. assert.EqualError(t, err, merkletree.ErrNotFound.Error())
  35. }
  36. // TestStorageInsertGet checks that the implementation of the db.Storage
  37. // interface behaves as expected
  38. func TestStorageInsertGet(t *testing.T, sto merkletree.Storage) {
  39. defer sto.Close()
  40. value := merkletree.Hash{1, 1, 1, 1}
  41. tx, err := sto.NewTx()
  42. defer tx.Close()
  43. assert.Nil(t, err)
  44. node := merkletree.NewNodeMiddle(&value, &value)
  45. key, err := node.Key()
  46. assert.Nil(t, err)
  47. err = tx.Put(key[:], node)
  48. assert.Nil(t, err)
  49. v, err := tx.Get(key[:])
  50. assert.Nil(t, err)
  51. assert.Equal(t, value, *v.ChildL)
  52. assert.Equal(t, value, *v.ChildR)
  53. assert.Nil(t, tx.Commit())
  54. tx2, err := sto.NewTx()
  55. defer tx2.Close()
  56. assert.Nil(t, err)
  57. v, err = tx2.Get(key[:])
  58. assert.Nil(t, err)
  59. require.NotNil(t, v)
  60. assert.Equal(t, value, *v.ChildL)
  61. assert.Equal(t, value, *v.ChildR)
  62. }
  63. // TestStorageWithPrefix checks that the implementation of the db.Storage
  64. // interface behaves as expected for the WithPrefix method
  65. func TestStorageWithPrefix(t *testing.T, sto merkletree.Storage) {
  66. defer sto.Close()
  67. sto1 := sto.WithPrefix([]byte{1})
  68. sto2 := sto.WithPrefix([]byte{2})
  69. // check within tx
  70. sto1tx, err := sto1.NewTx()
  71. assert.Nil(t, err)
  72. node := merkletree.NewNodeLeaf(&merkletree.Hash{1, 2, 3}, &merkletree.Hash{4, 5, 6})
  73. k, err := node.Key()
  74. err = sto1tx.Put(k[:], node)
  75. assert.Nil(t, err)
  76. v1, err := sto1tx.Get(k[:])
  77. assert.Nil(t, err)
  78. assert.Equal(t, merkletree.Hash{4, 5, 6}, *v1.Entry[1])
  79. assert.Nil(t, sto1tx.Commit())
  80. sto2tx, err := sto2.NewTx()
  81. assert.Nil(t, err)
  82. v2, err := sto2tx.Get(k[:])
  83. assert.Equal(t, merkletree.ErrNotFound, err)
  84. err = sto2tx.Put(k[:], node)
  85. assert.Nil(t, err)
  86. v2, err = sto2tx.Get(k[:])
  87. assert.Nil(t, err)
  88. assert.Equal(t, merkletree.Hash{4, 5, 6}, *v2.Entry[1])
  89. assert.Nil(t, sto2tx.Commit())
  90. // check outside tx
  91. v1, err = sto1.Get(k[:])
  92. assert.Nil(t, err)
  93. require.NotNil(t, v1)
  94. assert.Equal(t, merkletree.Hash{4, 5, 6}, *v1.Entry[1])
  95. v2, err = sto2.Get(k[:])
  96. assert.Nil(t, err)
  97. require.NotNil(t, v2)
  98. assert.Equal(t, merkletree.Hash{4, 5, 6}, *v2.Entry[1])
  99. }
  100. // TestIterate checks that the implementation of the db.Storage interface
  101. // behaves as expected for the Iterate method
  102. func TestIterate(t *testing.T, sto merkletree.Storage) {
  103. defer sto.Close()
  104. r := []merkletree.KV{}
  105. lister := func(k []byte, v *merkletree.Node) (bool, error) {
  106. r = append(r, merkletree.KV{K: merkletree.Clone(k), V: *v})
  107. return true, nil
  108. }
  109. sto1 := sto.WithPrefix([]byte{1})
  110. err := sto1.Iterate(lister)
  111. assert.Nil(t, err)
  112. assert.Equal(t, 0, len(r))
  113. sto1tx, _ := sto1.NewTx()
  114. err = sto1tx.Put([]byte{1}, merkletree.NewNodeMiddle(&merkletree.Hash{4}, &merkletree.Hash{5}))
  115. assert.Nil(t, err)
  116. err = sto1tx.Put([]byte{2}, merkletree.NewNodeMiddle(&merkletree.Hash{5}, &merkletree.Hash{6}))
  117. assert.Nil(t, err)
  118. err = sto1tx.Put([]byte{3}, merkletree.NewNodeMiddle(&merkletree.Hash{6}, &merkletree.Hash{7}))
  119. assert.Nil(t, err)
  120. assert.Nil(t, sto1tx.Commit())
  121. sto2 := sto.WithPrefix([]byte{2})
  122. sto2tx, _ := sto2.NewTx()
  123. err = sto2tx.Put([]byte{1}, merkletree.NewNodeMiddle(&merkletree.Hash{7}, &merkletree.Hash{8}))
  124. assert.Nil(t, err)
  125. err = sto2tx.Put([]byte{2}, merkletree.NewNodeMiddle(&merkletree.Hash{8}, &merkletree.Hash{9}))
  126. assert.Nil(t, err)
  127. err = sto2tx.Put([]byte{3}, merkletree.NewNodeMiddle(&merkletree.Hash{9}, &merkletree.Hash{10}))
  128. assert.Nil(t, err)
  129. assert.Nil(t, sto2tx.Commit())
  130. r = []merkletree.KV{}
  131. err = sto1.Iterate(lister)
  132. assert.Nil(t, err)
  133. assert.Equal(t, 3, len(r))
  134. assert.Equal(t, merkletree.KV{K: []byte{1}, V: *merkletree.NewNodeMiddle(&merkletree.Hash{4}, &merkletree.Hash{5})}, r[0])
  135. assert.Equal(t, merkletree.KV{K: []byte{2}, V: *merkletree.NewNodeMiddle(&merkletree.Hash{5}, &merkletree.Hash{6})}, r[1])
  136. assert.Equal(t, merkletree.KV{K: []byte{3}, V: *merkletree.NewNodeMiddle(&merkletree.Hash{6}, &merkletree.Hash{7})}, r[2])
  137. r = []merkletree.KV{}
  138. err = sto2.Iterate(lister)
  139. assert.Nil(t, err)
  140. assert.Equal(t, 3, len(r))
  141. assert.Equal(t, merkletree.KV{K: []byte{1}, V: *merkletree.NewNodeMiddle(&merkletree.Hash{7}, &merkletree.Hash{8})}, r[0])
  142. assert.Equal(t, merkletree.KV{K: []byte{2}, V: *merkletree.NewNodeMiddle(&merkletree.Hash{8}, &merkletree.Hash{9})}, r[1])
  143. assert.Equal(t, merkletree.KV{K: []byte{3}, V: *merkletree.NewNodeMiddle(&merkletree.Hash{9}, &merkletree.Hash{10})}, r[2])
  144. }
  145. // TestConcatTx checks that the implementation of the db.Storage interface
  146. // behaves as expected
  147. func TestConcatTx(t *testing.T, sto merkletree.Storage) {
  148. k := []byte{9}
  149. sto1 := sto.WithPrefix([]byte{1})
  150. sto2 := sto.WithPrefix([]byte{2})
  151. // check within tx
  152. sto1tx, err := sto1.NewTx()
  153. if err != nil {
  154. panic(err)
  155. }
  156. err = sto1tx.Put(k, merkletree.NewNodeLeaf(&merkletree.Hash{4, 5, 6}, &merkletree.Hash{7, 8, 9}))
  157. assert.Nil(t, err)
  158. sto2tx, err := sto2.NewTx()
  159. if err != nil {
  160. panic(err)
  161. }
  162. err = sto2tx.Put(k, merkletree.NewNodeLeaf(&merkletree.Hash{8, 9}, &merkletree.Hash{10, 11}))
  163. assert.Nil(t, err)
  164. err = sto1tx.Add(sto2tx)
  165. assert.Nil(t, err)
  166. assert.Nil(t, sto1tx.Commit())
  167. // check outside tx
  168. v1, err := sto1.Get(k)
  169. require.Nil(t, err)
  170. assert.Equal(t, *merkletree.NewNodeLeaf(&merkletree.Hash{4, 5, 6}, &merkletree.Hash{7, 8, 9}), *v1)
  171. v2, err := sto2.Get(k)
  172. require.Nil(t, err)
  173. assert.Equal(t, *merkletree.NewNodeLeaf(&merkletree.Hash{8, 9}, &merkletree.Hash{10, 11}), *v2)
  174. }
  175. // TestList checks that the implementation of the db.Storage interface behaves
  176. // as expected
  177. func TestList(t *testing.T, sto merkletree.Storage) {
  178. sto1 := sto.WithPrefix([]byte{1})
  179. r1, err := sto1.List(100)
  180. assert.Nil(t, err)
  181. assert.Equal(t, 0, len(r1))
  182. sto1tx, _ := sto1.NewTx()
  183. err = sto1tx.Put([]byte{1}, merkletree.NewNodeMiddle(&merkletree.Hash{4}, &merkletree.Hash{5}))
  184. assert.Nil(t, err)
  185. err = sto1tx.Put([]byte{2}, merkletree.NewNodeMiddle(&merkletree.Hash{5}, &merkletree.Hash{6}))
  186. assert.Nil(t, err)
  187. err = sto1tx.Put([]byte{3}, merkletree.NewNodeMiddle(&merkletree.Hash{6}, &merkletree.Hash{7}))
  188. assert.Nil(t, err)
  189. assert.Nil(t, sto1tx.Commit())
  190. sto2 := sto.WithPrefix([]byte{2})
  191. sto2tx, _ := sto2.NewTx()
  192. err = sto2tx.Put([]byte{1}, merkletree.NewNodeMiddle(&merkletree.Hash{7}, &merkletree.Hash{8}))
  193. assert.Nil(t, err)
  194. err = sto2tx.Put([]byte{2}, merkletree.NewNodeMiddle(&merkletree.Hash{8}, &merkletree.Hash{9}))
  195. assert.Nil(t, err)
  196. err = sto2tx.Put([]byte{3}, merkletree.NewNodeMiddle(&merkletree.Hash{9}, &merkletree.Hash{10}))
  197. assert.Nil(t, err)
  198. assert.Nil(t, sto2tx.Commit())
  199. r, err := sto1.List(100)
  200. assert.Nil(t, err)
  201. assert.Equal(t, 3, len(r))
  202. assert.Equal(t, r[0], merkletree.KV{K: []byte{1}, V: *merkletree.NewNodeMiddle(&merkletree.Hash{4}, &merkletree.Hash{5})})
  203. assert.Equal(t, r[1], merkletree.KV{K: []byte{2}, V: *merkletree.NewNodeMiddle(&merkletree.Hash{5}, &merkletree.Hash{6})})
  204. assert.Equal(t, r[2], merkletree.KV{K: []byte{3}, V: *merkletree.NewNodeMiddle(&merkletree.Hash{6}, &merkletree.Hash{7})})
  205. r, err = sto1.List(2)
  206. assert.Nil(t, err)
  207. assert.Equal(t, 2, len(r))
  208. assert.Equal(t, r[0], merkletree.KV{K: []byte{1}, V: *merkletree.NewNodeMiddle(&merkletree.Hash{4}, &merkletree.Hash{5})})
  209. assert.Equal(t, r[1], merkletree.KV{K: []byte{2}, V: *merkletree.NewNodeMiddle(&merkletree.Hash{5}, &merkletree.Hash{6})})
  210. }
  211. //
  212. // TODO: Add tests for each storage
  213. //
  214. func TestNewTree(t *testing.T, sto merkletree.Storage) {
  215. mt, err := merkletree.NewMerkleTree(sto, 10)
  216. assert.Nil(t, err)
  217. assert.Equal(t, "0", mt.Root().String())
  218. // test vectors generated using https://github.com/iden3/circomlib smt.js
  219. err = mt.Add(big.NewInt(1), big.NewInt(2))
  220. assert.Nil(t, err)
  221. assert.Equal(t, "13578938674299138072471463694055224830892726234048532520316387704878000008795", mt.Root().BigInt().String()) //nolint:lll
  222. err = mt.Add(big.NewInt(33), big.NewInt(44))
  223. assert.Nil(t, err)
  224. assert.Equal(t, "5412393676474193513566895793055462193090331607895808993925969873307089394741", mt.Root().BigInt().String()) //nolint:lll
  225. err = mt.Add(big.NewInt(1234), big.NewInt(9876))
  226. assert.Nil(t, err)
  227. assert.Equal(t, "14204494359367183802864593755198662203838502594566452929175967972147978322084", mt.Root().BigInt().String()) //nolint:lll
  228. dbRoot, err := mt.DB().GetRoot()
  229. require.Nil(t, err)
  230. assert.Equal(t, mt.Root(), dbRoot)
  231. proof, v, err := mt.GenerateProof(big.NewInt(33), nil)
  232. assert.Nil(t, err)
  233. assert.Equal(t, big.NewInt(44), v)
  234. assert.True(t, merkletree.VerifyProof(mt.Root(), proof, big.NewInt(33), big.NewInt(44)))
  235. assert.True(t, !merkletree.VerifyProof(mt.Root(), proof, big.NewInt(33), big.NewInt(45)))
  236. }
  237. func TestAddDifferentOrder(t *testing.T, sto merkletree.Storage, sto2 merkletree.Storage) {
  238. mt1 := newTestingMerkle(t, sto, 140)
  239. defer mt1.DB().Close()
  240. for i := 0; i < 16; i++ {
  241. k := big.NewInt(int64(i))
  242. v := big.NewInt(0)
  243. if err := mt1.Add(k, v); err != nil {
  244. t.Fatal(err)
  245. }
  246. }
  247. mt2 := newTestingMerkle(t, sto2, 140)
  248. defer mt2.DB().Close()
  249. for i := 16 - 1; i >= 0; i-- {
  250. k := big.NewInt(int64(i))
  251. v := big.NewInt(0)
  252. if err := mt2.Add(k, v); err != nil {
  253. t.Fatal(err)
  254. }
  255. }
  256. assert.Equal(t, mt1.Root().Hex(), mt2.Root().Hex())
  257. assert.Equal(t, "3b89100bec24da9275c87bc188740389e1d5accfc7d88ba5688d7fa96a00d82f", mt1.Root().Hex()) //nolint:lll
  258. }
  259. func TestAddRepeatedIndex(t *testing.T, sto merkletree.Storage) {
  260. mt := newTestingMerkle(t, sto, 140)
  261. defer mt.DB().Close()
  262. k := big.NewInt(int64(3))
  263. v := big.NewInt(int64(12))
  264. if err := mt.Add(k, v); err != nil {
  265. t.Fatal(err)
  266. }
  267. err := mt.Add(k, v)
  268. assert.NotNil(t, err)
  269. assert.Equal(t, err, merkletree.ErrEntryIndexAlreadyExists)
  270. }
  271. func TestGet(t *testing.T, sto merkletree.Storage) {
  272. mt := newTestingMerkle(t, sto, 140)
  273. defer mt.DB().Close()
  274. for i := 0; i < 16; i++ {
  275. k := big.NewInt(int64(i))
  276. v := big.NewInt(int64(i * 2))
  277. if err := mt.Add(k, v); err != nil {
  278. t.Fatal(err)
  279. }
  280. }
  281. k, v, _, err := mt.Get(big.NewInt(10))
  282. assert.Nil(t, err)
  283. assert.Equal(t, big.NewInt(10), k)
  284. assert.Equal(t, big.NewInt(20), v)
  285. k, v, _, err = mt.Get(big.NewInt(15))
  286. assert.Nil(t, err)
  287. assert.Equal(t, big.NewInt(15), k)
  288. assert.Equal(t, big.NewInt(30), v)
  289. k, v, _, err = mt.Get(big.NewInt(16))
  290. assert.NotNil(t, err)
  291. assert.Equal(t, merkletree.ErrKeyNotFound, err)
  292. assert.Equal(t, "0", k.String())
  293. assert.Equal(t, "0", v.String())
  294. }
  295. func TestUpdate(t *testing.T, sto merkletree.Storage) {
  296. mt := newTestingMerkle(t, sto, 140)
  297. defer mt.DB().Close()
  298. for i := 0; i < 16; i++ {
  299. k := big.NewInt(int64(i))
  300. v := big.NewInt(int64(i * 2))
  301. if err := mt.Add(k, v); err != nil {
  302. t.Fatal(err)
  303. }
  304. }
  305. _, v, _, err := mt.Get(big.NewInt(10))
  306. assert.Nil(t, err)
  307. assert.Equal(t, big.NewInt(20), v)
  308. _, err = mt.Update(big.NewInt(10), big.NewInt(1024))
  309. assert.Nil(t, err)
  310. _, v, _, err = mt.Get(big.NewInt(10))
  311. assert.Nil(t, err)
  312. assert.Equal(t, big.NewInt(1024), v)
  313. _, err = mt.Update(big.NewInt(1000), big.NewInt(1024))
  314. assert.Equal(t, merkletree.ErrKeyNotFound, err)
  315. dbRoot, err := mt.DB().GetRoot()
  316. require.Nil(t, err)
  317. assert.Equal(t, mt.Root(), dbRoot)
  318. }
  319. func TestUpdate2(t *testing.T, sto merkletree.Storage) {
  320. mt1 := newTestingMerkle(t, sto, 140)
  321. defer mt1.DB().Close()
  322. mt2 := newTestingMerkle(t, sto, 140)
  323. defer mt2.DB().Close()
  324. err := mt1.Add(big.NewInt(1), big.NewInt(119))
  325. assert.Nil(t, err)
  326. err = mt1.Add(big.NewInt(2), big.NewInt(229))
  327. assert.Nil(t, err)
  328. err = mt1.Add(big.NewInt(9876), big.NewInt(6789))
  329. assert.Nil(t, err)
  330. err = mt2.Add(big.NewInt(1), big.NewInt(11))
  331. assert.Nil(t, err)
  332. err = mt2.Add(big.NewInt(2), big.NewInt(22))
  333. assert.Nil(t, err)
  334. err = mt2.Add(big.NewInt(9876), big.NewInt(10))
  335. assert.Nil(t, err)
  336. _, err = mt1.Update(big.NewInt(1), big.NewInt(11))
  337. assert.Nil(t, err)
  338. _, err = mt1.Update(big.NewInt(2), big.NewInt(22))
  339. assert.Nil(t, err)
  340. _, err = mt2.Update(big.NewInt(9876), big.NewInt(6789))
  341. assert.Nil(t, err)
  342. assert.Equal(t, mt1.Root(), mt2.Root())
  343. }
  344. func TestGenerateAndVerifyProof128(t *testing.T, sto merkletree.Storage) {
  345. mt, err := merkletree.NewMerkleTree(sto, 140)
  346. require.Nil(t, err)
  347. defer mt.DB().Close()
  348. for i := 0; i < 128; i++ {
  349. k := big.NewInt(int64(i))
  350. v := big.NewInt(0)
  351. if err := mt.Add(k, v); err != nil {
  352. t.Fatal(err)
  353. }
  354. }
  355. proof, v, err := mt.GenerateProof(big.NewInt(42), nil)
  356. assert.Nil(t, err)
  357. assert.Equal(t, "0", v.String())
  358. assert.True(t, merkletree.VerifyProof(mt.Root(), proof, big.NewInt(42), big.NewInt(0)))
  359. }
  360. func TestTreeLimit(t *testing.T, sto merkletree.Storage) {
  361. mt, err := merkletree.NewMerkleTree(sto, 5)
  362. require.Nil(t, err)
  363. defer mt.DB().Close()
  364. for i := 0; i < 16; i++ {
  365. err = mt.Add(big.NewInt(int64(i)), big.NewInt(int64(i)))
  366. assert.Nil(t, err)
  367. }
  368. // here the tree is full, should not allow to add more data as reaches the maximum number of levels
  369. err = mt.Add(big.NewInt(int64(16)), big.NewInt(int64(16)))
  370. assert.NotNil(t, err)
  371. assert.Equal(t, merkletree.ErrReachedMaxLevel, err)
  372. }
  373. func TestSiblingsFromProof(t *testing.T, sto merkletree.Storage) {
  374. mt, err := merkletree.NewMerkleTree(sto, 140)
  375. require.Nil(t, err)
  376. defer mt.DB().Close()
  377. for i := 0; i < 64; i++ {
  378. k := big.NewInt(int64(i))
  379. v := big.NewInt(0)
  380. if err := mt.Add(k, v); err != nil {
  381. t.Fatal(err)
  382. }
  383. }
  384. proof, _, err := mt.GenerateProof(big.NewInt(4), nil)
  385. if err != nil {
  386. t.Fatal(err)
  387. }
  388. siblings := merkletree.SiblingsFromProof(proof)
  389. assert.Equal(t, 6, len(siblings))
  390. assert.Equal(t,
  391. "d6e368bda90c5ee3e910222c1fc1c0d9e23f2d350dbc47f4a92de30f1be3c60b",
  392. siblings[0].Hex())
  393. assert.Equal(t,
  394. "9dbd03b1bcd580e0f3e6668d80d55288f04464126feb1624ec8ee30be8df9c16",
  395. siblings[1].Hex())
  396. assert.Equal(t,
  397. "de866af9545dcd1c5bb7811e7f27814918e037eb9fead40919e8f19525896e27",
  398. siblings[2].Hex())
  399. assert.Equal(t,
  400. "5f4182212a84741d1174ba7c42e369f2e3ad8ade7d04eea2d0f98e3ed8b7a317",
  401. siblings[3].Hex())
  402. assert.Equal(t,
  403. "77639098d513f7aef9730fdb1d1200401af5fe9da91b61772f4dd142ac89a122",
  404. siblings[4].Hex())
  405. assert.Equal(t,
  406. "943ee501f4ba2137c79b54af745dfc5f105f539fcc449cd2a356eb5c030e3c07",
  407. siblings[5].Hex())
  408. }
  409. func TestVerifyProofCases(t *testing.T, sto merkletree.Storage) {
  410. mt := newTestingMerkle(t, sto, 140)
  411. defer mt.DB().Close()
  412. for i := 0; i < 8; i++ {
  413. if err := mt.Add(big.NewInt(int64(i)), big.NewInt(0)); err != nil {
  414. t.Fatal(err)
  415. }
  416. }
  417. // Existence proof
  418. proof, _, err := mt.GenerateProof(big.NewInt(4), nil)
  419. if err != nil {
  420. t.Fatal(err)
  421. }
  422. assert.Equal(t, proof.Existence, true)
  423. assert.True(t, merkletree.VerifyProof(mt.Root(), proof, big.NewInt(4), big.NewInt(0)))
  424. assert.Equal(t, "0003000000000000000000000000000000000000000000000000000000000007529cbedbda2bdd25fd6455551e55245fa6dc11a9d0c27dc0cd38fca44c17e40344ad686a18ba78b502c0b6f285c5c8393bde2f7a3e2abe586515e4d84533e3037b062539bde2d80749746986cf8f0001fd2cdbf9a89fcbf981a769daef49df06", hex.EncodeToString(proof.Bytes())) //nolint:lll
  425. for i := 8; i < 32; i++ {
  426. proof, _, err = mt.GenerateProof(big.NewInt(int64(i)), nil)
  427. assert.Nil(t, err)
  428. if debug {
  429. fmt.Println(i, proof)
  430. }
  431. }
  432. // Non-existence proof, empty aux
  433. proof, _, err = mt.GenerateProof(big.NewInt(12), nil)
  434. if err != nil {
  435. t.Fatal(err)
  436. }
  437. assert.Equal(t, proof.Existence, false)
  438. // assert.True(t, proof.nodeAux == nil)
  439. assert.True(t, merkletree.VerifyProof(mt.Root(), proof, big.NewInt(12), big.NewInt(0)))
  440. assert.Equal(t, "0303000000000000000000000000000000000000000000000000000000000007529cbedbda2bdd25fd6455551e55245fa6dc11a9d0c27dc0cd38fca44c17e40344ad686a18ba78b502c0b6f285c5c8393bde2f7a3e2abe586515e4d84533e3037b062539bde2d80749746986cf8f0001fd2cdbf9a89fcbf981a769daef49df0604000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", hex.EncodeToString(proof.Bytes())) //nolint:lll
  441. // Non-existence proof, diff. node aux
  442. proof, _, err = mt.GenerateProof(big.NewInt(10), nil)
  443. if err != nil {
  444. t.Fatal(err)
  445. }
  446. assert.Equal(t, proof.Existence, false)
  447. assert.True(t, proof.NodeAux != nil)
  448. assert.True(t, merkletree.VerifyProof(mt.Root(), proof, big.NewInt(10), big.NewInt(0)))
  449. assert.Equal(t, "0303000000000000000000000000000000000000000000000000000000000007529cbedbda2bdd25fd6455551e55245fa6dc11a9d0c27dc0cd38fca44c17e4030acfcdd2617df9eb5aef744c5f2e03eb8c92c61f679007dc1f2707fd908ea41a9433745b469c101edca814c498e7f388100d497b24f1d2ac935bced3572f591d02000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", hex.EncodeToString(proof.Bytes())) //nolint:lll
  450. }
  451. func TestVerifyProofFalse(t *testing.T, sto merkletree.Storage) {
  452. mt := newTestingMerkle(t, sto, 140)
  453. defer mt.DB().Close()
  454. for i := 0; i < 8; i++ {
  455. if err := mt.Add(big.NewInt(int64(i)), big.NewInt(0)); err != nil {
  456. t.Fatal(err)
  457. }
  458. }
  459. // Invalid existence proof (node used for verification doesn't
  460. // correspond to node in the proof)
  461. proof, _, err := mt.GenerateProof(big.NewInt(int64(4)), nil)
  462. if err != nil {
  463. t.Fatal(err)
  464. }
  465. assert.Equal(t, proof.Existence, true)
  466. assert.True(t, !merkletree.VerifyProof(mt.Root(), proof, big.NewInt(int64(5)), big.NewInt(int64(5))))
  467. // Invalid non-existence proof (Non-existence proof, diff. node aux)
  468. proof, _, err = mt.GenerateProof(big.NewInt(int64(4)), nil)
  469. if err != nil {
  470. t.Fatal(err)
  471. }
  472. assert.Equal(t, proof.Existence, true)
  473. // Now we change the proof from existence to non-existence, and add e's
  474. // data as auxiliary node.
  475. proof.Existence = false
  476. proof.NodeAux = &merkletree.NodeAux{Key: merkletree.NewHashFromBigInt(big.NewInt(int64(4))),
  477. Value: merkletree.NewHashFromBigInt(big.NewInt(4))}
  478. assert.True(t, !merkletree.VerifyProof(mt.Root(), proof, big.NewInt(int64(4)), big.NewInt(0)))
  479. }
  480. func TestGraphViz(t *testing.T, sto merkletree.Storage) {
  481. mt, err := merkletree.NewMerkleTree(sto, 10)
  482. assert.Nil(t, err)
  483. _ = mt.Add(big.NewInt(1), big.NewInt(0))
  484. _ = mt.Add(big.NewInt(2), big.NewInt(0))
  485. _ = mt.Add(big.NewInt(3), big.NewInt(0))
  486. _ = mt.Add(big.NewInt(4), big.NewInt(0))
  487. _ = mt.Add(big.NewInt(5), big.NewInt(0))
  488. _ = mt.Add(big.NewInt(100), big.NewInt(0))
  489. // mt.PrintGraphViz(nil)
  490. expected := `digraph hierarchy {
  491. node [fontname=Monospace,fontsize=10,shape=box]
  492. "56332309..." -> {"18483622..." "20902180..."}
  493. "18483622..." -> {"75768243..." "16893244..."}
  494. "75768243..." -> {"empty0" "21857056..."}
  495. "empty0" [style=dashed,label=0];
  496. "21857056..." -> {"51072523..." "empty1"}
  497. "empty1" [style=dashed,label=0];
  498. "51072523..." -> {"17311038..." "empty2"}
  499. "empty2" [style=dashed,label=0];
  500. "17311038..." -> {"69499803..." "21008290..."}
  501. "69499803..." [style=filled];
  502. "21008290..." [style=filled];
  503. "16893244..." [style=filled];
  504. "20902180..." -> {"12496585..." "18055627..."}
  505. "12496585..." -> {"19374975..." "15739329..."}
  506. "19374975..." [style=filled];
  507. "15739329..." [style=filled];
  508. "18055627..." [style=filled];
  509. }
  510. `
  511. w := bytes.NewBufferString("")
  512. err = mt.GraphViz(w, nil)
  513. assert.Nil(t, err)
  514. assert.Equal(t, []byte(expected), w.Bytes())
  515. }
  516. func TestDelete(t *testing.T, sto merkletree.Storage) {
  517. mt, err := merkletree.NewMerkleTree(sto, 10)
  518. assert.Nil(t, err)
  519. assert.Equal(t, "0", mt.Root().String())
  520. // test vectors generated using https://github.com/iden3/circomlib smt.js
  521. err = mt.Add(big.NewInt(1), big.NewInt(2))
  522. assert.Nil(t, err)
  523. assert.Equal(t, "13578938674299138072471463694055224830892726234048532520316387704878000008795", mt.Root().BigInt().String()) //nolint:lll
  524. err = mt.Add(big.NewInt(33), big.NewInt(44))
  525. assert.Nil(t, err)
  526. assert.Equal(t, "5412393676474193513566895793055462193090331607895808993925969873307089394741", mt.Root().BigInt().String()) //nolint:lll
  527. err = mt.Add(big.NewInt(1234), big.NewInt(9876))
  528. assert.Nil(t, err)
  529. assert.Equal(t, "14204494359367183802864593755198662203838502594566452929175967972147978322084", mt.Root().BigInt().String()) //nolint:lll
  530. // mt.PrintGraphViz(nil)
  531. err = mt.Delete(big.NewInt(33))
  532. // mt.PrintGraphViz(nil)
  533. assert.Nil(t, err)
  534. assert.Equal(t, "15550352095346187559699212771793131433118240951738528922418613687814377955591", mt.Root().BigInt().String()) //nolint:lll
  535. err = mt.Delete(big.NewInt(1234))
  536. assert.Nil(t, err)
  537. err = mt.Delete(big.NewInt(1))
  538. assert.Nil(t, err)
  539. assert.Equal(t, "0", mt.Root().String())
  540. dbRoot, err := mt.DB().GetRoot()
  541. require.Nil(t, err)
  542. assert.Equal(t, mt.Root(), dbRoot)
  543. }
  544. func TestDelete2(t *testing.T, sto merkletree.Storage, sto2 merkletree.Storage) {
  545. mt := newTestingMerkle(t, sto, 140)
  546. defer mt.DB().Close()
  547. for i := 0; i < 8; i++ {
  548. k := big.NewInt(int64(i))
  549. v := big.NewInt(0)
  550. if err := mt.Add(k, v); err != nil {
  551. t.Fatal(err)
  552. }
  553. }
  554. expectedRoot := mt.Root()
  555. k := big.NewInt(8)
  556. v := big.NewInt(0)
  557. err := mt.Add(k, v)
  558. require.Nil(t, err)
  559. err = mt.Delete(big.NewInt(8))
  560. assert.Nil(t, err)
  561. assert.Equal(t, expectedRoot, mt.Root())
  562. mt2 := newTestingMerkle(t, sto2, 140)
  563. defer mt2.DB().Close()
  564. for i := 0; i < 8; i++ {
  565. k := big.NewInt(int64(i))
  566. v := big.NewInt(0)
  567. if err := mt2.Add(k, v); err != nil {
  568. t.Fatal(err)
  569. }
  570. }
  571. assert.Equal(t, mt2.Root(), mt.Root())
  572. }
  573. func TestDelete3(t *testing.T, sto merkletree.Storage, sto2 merkletree.Storage) {
  574. mt := newTestingMerkle(t, sto, 140)
  575. defer mt.DB().Close()
  576. err := mt.Add(big.NewInt(1), big.NewInt(1))
  577. assert.Nil(t, err)
  578. err = mt.Add(big.NewInt(2), big.NewInt(2))
  579. assert.Nil(t, err)
  580. assert.Equal(t, "19060075022714027595905950662613111880864833370144986660188929919683258088314", mt.Root().BigInt().String()) //nolint:lll
  581. err = mt.Delete(big.NewInt(1))
  582. assert.Nil(t, err)
  583. assert.Equal(t, "849831128489032619062850458217693666094013083866167024127442191257793527951", mt.Root().BigInt().String()) //nolint:lll
  584. mt2 := newTestingMerkle(t, sto2, 140)
  585. defer mt2.DB().Close()
  586. err = mt2.Add(big.NewInt(2), big.NewInt(2))
  587. assert.Nil(t, err)
  588. assert.Equal(t, mt2.Root(), mt.Root())
  589. }
  590. func TestDelete4(t *testing.T, sto merkletree.Storage, sto2 merkletree.Storage) {
  591. mt := newTestingMerkle(t, sto, 140)
  592. defer mt.DB().Close()
  593. err := mt.Add(big.NewInt(1), big.NewInt(1))
  594. assert.Nil(t, err)
  595. err = mt.Add(big.NewInt(2), big.NewInt(2))
  596. assert.Nil(t, err)
  597. err = mt.Add(big.NewInt(3), big.NewInt(3))
  598. assert.Nil(t, err)
  599. assert.Equal(t, "14109632483797541575275728657193822866549917334388996328141438956557066918117", mt.Root().BigInt().String()) //nolint:lll
  600. err = mt.Delete(big.NewInt(1))
  601. assert.Nil(t, err)
  602. assert.Equal(t, "159935162486187606489815340465698714590556679404589449576549073038844694972", mt.Root().BigInt().String()) //nolint:lll
  603. mt2 := newTestingMerkle(t, sto2, 140)
  604. defer mt2.DB().Close()
  605. err = mt2.Add(big.NewInt(2), big.NewInt(2))
  606. assert.Nil(t, err)
  607. err = mt2.Add(big.NewInt(3), big.NewInt(3))
  608. assert.Nil(t, err)
  609. assert.Equal(t, mt2.Root(), mt.Root())
  610. }
  611. func TestDelete5(t *testing.T, sto merkletree.Storage, sto2 merkletree.Storage) {
  612. mt, err := merkletree.NewMerkleTree(sto, 10)
  613. assert.Nil(t, err)
  614. err = mt.Add(big.NewInt(1), big.NewInt(2))
  615. assert.Nil(t, err)
  616. err = mt.Add(big.NewInt(33), big.NewInt(44))
  617. assert.Nil(t, err)
  618. assert.Equal(t, "5412393676474193513566895793055462193090331607895808993925969873307089394741", mt.Root().BigInt().String()) //nolint:lll
  619. err = mt.Delete(big.NewInt(1))
  620. assert.Nil(t, err)
  621. assert.Equal(t, "18869260084287237667925661423624848342947598951870765316380602291081195309822", mt.Root().BigInt().String()) //nolint:lll
  622. mt2 := newTestingMerkle(t, sto2, 140)
  623. defer mt2.DB().Close()
  624. err = mt2.Add(big.NewInt(33), big.NewInt(44))
  625. assert.Nil(t, err)
  626. assert.Equal(t, mt2.Root(), mt.Root())
  627. }
  628. func TestDeleteNonExistingKeys(t *testing.T, sto merkletree.Storage) {
  629. mt, err := merkletree.NewMerkleTree(sto, 10)
  630. assert.Nil(t, err)
  631. err = mt.Add(big.NewInt(1), big.NewInt(2))
  632. assert.Nil(t, err)
  633. err = mt.Add(big.NewInt(33), big.NewInt(44))
  634. assert.Nil(t, err)
  635. err = mt.Delete(big.NewInt(33))
  636. assert.Nil(t, err)
  637. err = mt.Delete(big.NewInt(33))
  638. assert.Equal(t, merkletree.ErrKeyNotFound, err)
  639. err = mt.Delete(big.NewInt(1))
  640. assert.Nil(t, err)
  641. assert.Equal(t, "0", mt.Root().String())
  642. err = mt.Delete(big.NewInt(33))
  643. assert.Equal(t, merkletree.ErrKeyNotFound, err)
  644. }
  645. func TestDumpLeafsImportLeafs(t *testing.T, sto merkletree.Storage, sto2 merkletree.Storage) {
  646. mt, err := merkletree.NewMerkleTree(sto, 140)
  647. require.Nil(t, err)
  648. defer mt.DB().Close()
  649. q1 := new(big.Int).Sub(constants.Q, big.NewInt(1))
  650. for i := 0; i < 10; i++ {
  651. // use numbers near under Q
  652. k := new(big.Int).Sub(q1, big.NewInt(int64(i)))
  653. v := big.NewInt(0)
  654. err = mt.Add(k, v)
  655. require.Nil(t, err)
  656. // use numbers near above 0
  657. k = big.NewInt(int64(i))
  658. err = mt.Add(k, v)
  659. require.Nil(t, err)
  660. }
  661. d, err := mt.DumpLeafs(nil)
  662. assert.Nil(t, err)
  663. mt2, err := merkletree.NewMerkleTree(sto2, 140)
  664. require.Nil(t, err)
  665. defer mt2.DB().Close()
  666. err = mt2.ImportDumpedLeafs(d)
  667. assert.Nil(t, err)
  668. assert.Equal(t, mt.Root(), mt2.Root())
  669. }
  670. func TestAddAndGetCircomProof(t *testing.T, sto merkletree.Storage) {
  671. mt, err := merkletree.NewMerkleTree(sto, 10)
  672. assert.Nil(t, err)
  673. assert.Equal(t, "0", mt.Root().String())
  674. // test vectors generated using https://github.com/iden3/circomlib smt.js
  675. cpp, err := mt.AddAndGetCircomProof(big.NewInt(1), big.NewInt(2))
  676. assert.Nil(t, err)
  677. assert.Equal(t, "0", cpp.OldRoot.String())
  678. assert.Equal(t, "13578938...", cpp.NewRoot.String())
  679. assert.Equal(t, "0", cpp.OldKey.String())
  680. assert.Equal(t, "0", cpp.OldValue.String())
  681. assert.Equal(t, "1", cpp.NewKey.String())
  682. assert.Equal(t, "2", cpp.NewValue.String())
  683. assert.Equal(t, true, cpp.IsOld0)
  684. assert.Equal(t, "[0 0 0 0 0 0 0 0 0 0 0]", fmt.Sprintf("%v", cpp.Siblings))
  685. assert.Equal(t, mt.MaxLevels()+1, len(cpp.Siblings))
  686. cpp, err = mt.AddAndGetCircomProof(big.NewInt(33), big.NewInt(44))
  687. assert.Nil(t, err)
  688. assert.Equal(t, "13578938...", cpp.OldRoot.String())
  689. assert.Equal(t, "54123936...", cpp.NewRoot.String())
  690. assert.Equal(t, "1", cpp.OldKey.String())
  691. assert.Equal(t, "2", cpp.OldValue.String())
  692. assert.Equal(t, "33", cpp.NewKey.String())
  693. assert.Equal(t, "44", cpp.NewValue.String())
  694. assert.Equal(t, false, cpp.IsOld0)
  695. assert.Equal(t, "[0 0 0 0 0 0 0 0 0 0 0]", fmt.Sprintf("%v", cpp.Siblings))
  696. assert.Equal(t, mt.MaxLevels()+1, len(cpp.Siblings))
  697. cpp, err = mt.AddAndGetCircomProof(big.NewInt(55), big.NewInt(66))
  698. assert.Nil(t, err)
  699. assert.Equal(t, "54123936...", cpp.OldRoot.String())
  700. assert.Equal(t, "50943640...", cpp.NewRoot.String())
  701. assert.Equal(t, "0", cpp.OldKey.String())
  702. assert.Equal(t, "0", cpp.OldValue.String())
  703. assert.Equal(t, "55", cpp.NewKey.String())
  704. assert.Equal(t, "66", cpp.NewValue.String())
  705. assert.Equal(t, true, cpp.IsOld0)
  706. assert.Equal(t, "[0 21312042... 0 0 0 0 0 0 0 0 0]", fmt.Sprintf("%v", cpp.Siblings))
  707. assert.Equal(t, mt.MaxLevels()+1, len(cpp.Siblings))
  708. }
  709. func TestUpdateCircomProcessorProof(t *testing.T, sto merkletree.Storage) {
  710. mt := newTestingMerkle(t, sto, 10)
  711. defer mt.DB().Close()
  712. for i := 0; i < 16; i++ {
  713. k := big.NewInt(int64(i))
  714. v := big.NewInt(int64(i * 2))
  715. if err := mt.Add(k, v); err != nil {
  716. t.Fatal(err)
  717. }
  718. }
  719. _, v, _, err := mt.Get(big.NewInt(10))
  720. assert.Nil(t, err)
  721. assert.Equal(t, big.NewInt(20), v)
  722. // test vectors generated using https://github.com/iden3/circomlib smt.js
  723. cpp, err := mt.Update(big.NewInt(10), big.NewInt(1024))
  724. assert.Nil(t, err)
  725. assert.Equal(t, "39010880...", cpp.OldRoot.String())
  726. assert.Equal(t, "18587862...", cpp.NewRoot.String())
  727. assert.Equal(t, "10", cpp.OldKey.String())
  728. assert.Equal(t, "20", cpp.OldValue.String())
  729. assert.Equal(t, "10", cpp.NewKey.String())
  730. assert.Equal(t, "1024", cpp.NewValue.String())
  731. assert.Equal(t, false, cpp.IsOld0)
  732. assert.Equal(t,
  733. "[34930557... 20201609... 18790542... 15930030... 0 0 0 0 0 0 0]",
  734. fmt.Sprintf("%v", cpp.Siblings))
  735. }
  736. func TestSmtVerifier(t *testing.T, sto merkletree.Storage) {
  737. mt, err := merkletree.NewMerkleTree(sto, 4)
  738. assert.Nil(t, err)
  739. err = mt.Add(big.NewInt(1), big.NewInt(11))
  740. assert.Nil(t, err)
  741. cvp, err := mt.GenerateSCVerifierProof(big.NewInt(1), nil)
  742. assert.Nil(t, err)
  743. jCvp, err := json.Marshal(cvp)
  744. assert.Nil(t, err)
  745. // expect siblings to be '[]', instead of 'null'
  746. expected := `{"root":"6525056641794203554583616941316772618766382307684970171204065038799368146416","siblings":[],"oldKey":"0","oldValue":"0","isOld0":false,"key":"1","value":"11","fnc":0}` //nolint:lll
  747. assert.Equal(t, expected, string(jCvp))
  748. err = mt.Add(big.NewInt(2), big.NewInt(22))
  749. assert.Nil(t, err)
  750. err = mt.Add(big.NewInt(3), big.NewInt(33))
  751. assert.Nil(t, err)
  752. err = mt.Add(big.NewInt(4), big.NewInt(44))
  753. assert.Nil(t, err)
  754. cvp, err = mt.GenerateCircomVerifierProof(big.NewInt(2), nil)
  755. assert.Nil(t, err)
  756. jCvp, err = json.Marshal(cvp)
  757. assert.Nil(t, err)
  758. // Test vectors generated using https://github.com/iden3/circomlib smt.js
  759. // Expect siblings with the extra 0 that the circom circuits need
  760. expected = `{"root":"13558168455220559042747853958949063046226645447188878859760119761585093422436","siblings":["11620130507635441932056895853942898236773847390796721536119314875877874016518","5158240518874928563648144881543092238925265313977134167935552944620041388700","0","0","0"],"oldKey":"0","oldValue":"0","isOld0":false,"key":"2","value":"22","fnc":0}` //nolint:lll
  761. assert.Equal(t, expected, string(jCvp))
  762. cvp, err = mt.GenerateSCVerifierProof(big.NewInt(2), nil)
  763. assert.Nil(t, err)
  764. jCvp, err = json.Marshal(cvp)
  765. assert.Nil(t, err)
  766. // Test vectors generated using https://github.com/iden3/circomlib smt.js
  767. // Without the extra 0 that the circom circuits need, but that are not
  768. // needed at a smart contract verification
  769. expected = `{"root":"13558168455220559042747853958949063046226645447188878859760119761585093422436","siblings":["11620130507635441932056895853942898236773847390796721536119314875877874016518","5158240518874928563648144881543092238925265313977134167935552944620041388700"],"oldKey":"0","oldValue":"0","isOld0":false,"key":"2","value":"22","fnc":0}` //nolint:lll
  770. assert.Equal(t, expected, string(jCvp))
  771. }
  772. func TestTypesMarshalers(t *testing.T, sto merkletree.Storage) {
  773. // test Hash marshalers
  774. h, err := merkletree.NewHashFromString("42")
  775. assert.Nil(t, err)
  776. s, err := json.Marshal(h)
  777. assert.Nil(t, err)
  778. var h2 *merkletree.Hash
  779. err = json.Unmarshal(s, &h2)
  780. assert.Nil(t, err)
  781. assert.Equal(t, h, h2)
  782. // create CircomProcessorProof
  783. mt := newTestingMerkle(t, sto, 10)
  784. defer mt.DB().Close()
  785. for i := 0; i < 16; i++ {
  786. k := big.NewInt(int64(i))
  787. v := big.NewInt(int64(i * 2))
  788. if err := mt.Add(k, v); err != nil {
  789. t.Fatal(err)
  790. }
  791. }
  792. _, v, _, err := mt.Get(big.NewInt(10))
  793. assert.Nil(t, err)
  794. assert.Equal(t, big.NewInt(20), v)
  795. cpp, err := mt.Update(big.NewInt(10), big.NewInt(1024))
  796. assert.Nil(t, err)
  797. // test CircomProcessorProof marshalers
  798. b, err := json.Marshal(&cpp)
  799. assert.Nil(t, err)
  800. var cpp2 *merkletree.CircomProcessorProof
  801. err = json.Unmarshal(b, &cpp2)
  802. assert.Nil(t, err)
  803. assert.Equal(t, cpp, cpp2)
  804. }