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.

94 lines
4.1 KiB

  1. # arbo [![GoDoc](https://godoc.org/github.com/vocdoni/arbo?status.svg)](https://godoc.org/github.com/vocdoni/arbo) [![Go Report Card](https://goreportcard.com/badge/github.com/vocdoni/arbo)](https://goreportcard.com/report/github.com/vocdoni/arbo) [![Test](https://github.com/vocdoni/arbo/workflows/Test/badge.svg)](https://github.com/vocdoni/arbo/actions?query=workflow%3ATest)
  2. > *arbo*: tree in Esperanto.
  3. MerkleTree implementation in Go. Compatible with the circomlib implementation of
  4. the MerkleTree, following the specification from
  5. https://docs.iden3.io/publications/pdfs/Merkle-Tree.pdf and
  6. https://eprint.iacr.org/2018/955.
  7. Allows to define which hash function to use. So for example, when working with
  8. zkSnarks the Poseidon hash function can be used, but when not, it can be used
  9. the Blake2b hash function, which has much faster computation time.
  10. ## AddBatch
  11. The method `tree.AddBatch` is designed for the cases where there is a big amount of key-values to be added in the tree. It has the following characteristics:
  12. - Makes a copy of the tree in memory (*VirtualTree*)
  13. - The *VirtualTree* does not compute any hash, only the relations between the nodes of the tree
  14. - This step (computing the *VirtualTree*) is done in parallel in each available CPU until level *log2(nCPU)*
  15. - Once the *VirtualTree* is updated with all the new leafs (key-values) in each corresponent position, it *computes all the hashes* of each node until the root
  16. - In this way, each node hash is computed only once, while when adding many key-values using `tree.Add` method, most of the intermediate nodes will be recalculated each time that a new leaf is added
  17. - This step (*computing all the hashes*) is done in parallel in each available CPU
  18. As result, the method `tree.AddBatch` goes way faster thant looping over `tree.Add`, and can compute the tree with parallelization, so as more available CPUs, faster will do the computation.
  19. ## Usage
  20. ```go
  21. // create new database
  22. database, err := db.NewBadgerDB(c.TempDir())
  23. // create new Tree with maxLevels=100 and Blake2b hash function
  24. tree, err := arbo.NewTree(database, 100, arbo.HashFunctionBlake2b)
  25. key := []byte("hello")
  26. value := []byte("world")
  27. // Add a key & value into the merkle tree
  28. err = tree.Add(key, value)
  29. // There are cases where multiple key-values (leafs) are going to be added to a
  30. // Tree, for these cases is more efficient to use:
  31. invalids, err := tree.AddBatch(keys, values)
  32. // generate the merkle proof of a leaf by it's key
  33. value, siblings, err := tree.GenProof(key)
  34. // verify the proof
  35. verified, err := arbo.CheckProof(tree.hashFunction, key, value, tree.Root(), siblings)
  36. if !verified {
  37. fmt.Println("proof could not be verified")
  38. }
  39. // get the value of a leaf assigned to a key
  40. gettedKey, gettedValue, err := tree.Get(key)
  41. // update the value of a leaf assigned to a key
  42. err = tree.Update(key, value)
  43. // dump the tree (the leafs)
  44. dump, err := tree.Dump(nil) // instead of nil, a root to start from can be used
  45. // import the dump into a tree
  46. err = tree.ImportDump(dump)
  47. // print graphviz diagram of the tree
  48. err = tree.PrintGraphviz(nil) // instead of nil, a root to start from can be used
  49. ```
  50. ### Usage with SNARKs compatibility
  51. Arbo is designed to be compatible with [circom merkle
  52. tree](https://github.com/iden3/circomlib/tree/master/circuits/smt)'s
  53. snark-friendly merkletree.
  54. The only change needed is the hash function used for the Tree, for example using
  55. the Poseidon hash function:
  56. ```go
  57. tree, err := arbo.NewTree(database, 32, arbo.HashFunctionPoseidon)
  58. ```
  59. Be aware of the characteristics of this kind of hashes, such as using values
  60. inside the finite field used by the hash, and also the computation time.
  61. The interface of arbo uses byte arrays, and for the case of these kind of hashes
  62. (that usually work directly with finite field elements), arbo expects those
  63. values to be represented by little-endian byte arrays. There is a helper method
  64. to convert a `*big.Int` to `[]byte` using little-endian:
  65. ```go
  66. bLen := tree.HashFunction().Len()
  67. kBigInt := big.NewInt(100)
  68. // convert *big.Int to byte array
  69. kBytes := arbo.BigIntToBytes(bLen, kBigInt)
  70. // convert byte array to *big.Int
  71. kBigInt2 := arbo.BytesToBigInt(kBytes)
  72. ```