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.

1152 lines
30 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. /*
  2. Package arbo implements a Merkle Tree compatible with the circomlib
  3. implementation of the MerkleTree, following the specification from
  4. https://docs.iden3.io/publications/pdfs/Merkle-Tree.pdf and
  5. https://eprint.iacr.org/2018/955.
  6. Allows to define which hash function to use. So for example, when working with
  7. zkSnarks the Poseidon hash function can be used, but when not, it can be used
  8. the Blake2b hash function, which has much faster computation time.
  9. */
  10. package arbo
  11. import (
  12. "bytes"
  13. "encoding/binary"
  14. "encoding/hex"
  15. "fmt"
  16. "io"
  17. "math"
  18. "sync"
  19. "go.vocdoni.io/dvote/db"
  20. )
  21. const (
  22. // PrefixValueLen defines the bytes-prefix length used for the Value
  23. // bytes representation stored in the db
  24. PrefixValueLen = 2
  25. // PrefixValueEmpty is used for the first byte of a Value to indicate
  26. // that is an Empty value
  27. PrefixValueEmpty = 0
  28. // PrefixValueLeaf is used for the first byte of a Value to indicate
  29. // that is a Leaf value
  30. PrefixValueLeaf = 1
  31. // PrefixValueIntermediate is used for the first byte of a Value to
  32. // indicate that is a Intermediate value
  33. PrefixValueIntermediate = 2
  34. // nChars is used to crop the Graphviz nodes labels
  35. nChars = 4
  36. )
  37. var (
  38. dbKeyRoot = []byte("root")
  39. dbKeyNLeafs = []byte("nleafs")
  40. emptyValue = []byte{0}
  41. // ErrKeyNotFound is used when a key is not found in the db neither in
  42. // the current db Batch.
  43. ErrKeyNotFound = fmt.Errorf("key not found")
  44. // ErrKeyAlreadyExists is used when trying to add a key as leaf to the
  45. // tree that already exists.
  46. ErrKeyAlreadyExists = fmt.Errorf("key already exists")
  47. // ErrInvalidValuePrefix is used when going down into the tree, a value
  48. // is read from the db and has an unrecognized prefix.
  49. ErrInvalidValuePrefix = fmt.Errorf("invalid value prefix")
  50. // ErrDBNoTx is used when trying to use Tree.dbPut but Tree.dbBatch==nil
  51. ErrDBNoTx = fmt.Errorf("dbPut error: no db Batch")
  52. // ErrMaxLevel indicates when going down into the tree, the max level is
  53. // reached
  54. ErrMaxLevel = fmt.Errorf("max level reached")
  55. // ErrMaxVirtualLevel indicates when going down into the tree, the max
  56. // virtual level is reached
  57. ErrMaxVirtualLevel = fmt.Errorf("max virtual level reached")
  58. // ErrSnapshotNotEditable indicates when the tree is a snapshot, thus
  59. // can not be modified
  60. ErrSnapshotNotEditable = fmt.Errorf("snapshot tree can not be edited")
  61. // ErrTreeNotEmpty indicates when the tree was expected to be empty and
  62. // it is not
  63. ErrTreeNotEmpty = fmt.Errorf("tree is not empty")
  64. )
  65. // Tree defines the struct that implements the MerkleTree functionalities
  66. type Tree struct {
  67. sync.RWMutex
  68. db db.Database
  69. maxLevels int
  70. snapshotRoot []byte
  71. hashFunction HashFunction
  72. // TODO in the methods that use it, check if emptyHash param is len>0
  73. // (check if it has been initialized)
  74. emptyHash []byte
  75. dbg *dbgStats
  76. }
  77. // NewTree returns a new Tree, if there is a Tree still in the given database, it
  78. // will load it.
  79. func NewTree(database db.Database, maxLevels int, hash HashFunction) (*Tree, error) {
  80. wTx := database.WriteTx()
  81. defer wTx.Discard()
  82. t, err := NewTreeWithTx(wTx, database, maxLevels, hash)
  83. if err != nil {
  84. return nil, err
  85. }
  86. if err = wTx.Commit(); err != nil {
  87. return nil, err
  88. }
  89. return t, nil
  90. }
  91. // NewTreeWithTx returns a new Tree using the given db.WriteTx, which will not
  92. // be ccommited inside this method, if there is a Tree still in the given
  93. // database, it will load it.
  94. func NewTreeWithTx(wTx db.WriteTx, database db.Database,
  95. maxLevels int, hash HashFunction) (*Tree, error) {
  96. t := Tree{db: database, maxLevels: maxLevels, hashFunction: hash}
  97. t.emptyHash = make([]byte, t.hashFunction.Len()) // empty
  98. _, err := wTx.Get(dbKeyRoot)
  99. if err == db.ErrKeyNotFound {
  100. // store new root 0 (empty)
  101. if err = wTx.Set(dbKeyRoot, t.emptyHash); err != nil {
  102. return nil, err
  103. }
  104. if err = t.setNLeafs(wTx, 0); err != nil {
  105. return nil, err
  106. }
  107. return &t, nil
  108. } else if err != nil {
  109. return nil, err
  110. }
  111. return &t, nil
  112. }
  113. // Root returns the root of the Tree
  114. func (t *Tree) Root() ([]byte, error) {
  115. rTx := t.db.ReadTx()
  116. defer rTx.Discard()
  117. return t.RootWithTx(rTx)
  118. }
  119. // RootWithTx returns the root of the Tree using the given db.ReadTx
  120. func (t *Tree) RootWithTx(rTx db.ReadTx) ([]byte, error) {
  121. // if snapshotRoot is defined, means that the tree is a snapshot, and
  122. // the root is not obtained from the db, but from the snapshotRoot
  123. // parameter
  124. if t.snapshotRoot != nil {
  125. return t.snapshotRoot, nil
  126. }
  127. // get db root
  128. return rTx.Get(dbKeyRoot)
  129. }
  130. func (t *Tree) setRoot(wTx db.WriteTx, root []byte) error {
  131. return wTx.Set(dbKeyRoot, root)
  132. }
  133. // HashFunction returns Tree.hashFunction
  134. func (t *Tree) HashFunction() HashFunction {
  135. return t.hashFunction
  136. }
  137. // editable returns true if the tree is editable, and false when is not
  138. // editable (because is a snapshot tree)
  139. func (t *Tree) editable() bool {
  140. return t.snapshotRoot == nil
  141. }
  142. // AddBatch adds a batch of key-values to the Tree. Returns an array containing
  143. // the indexes of the keys failed to add. Supports empty values as input
  144. // parameters, which is equivalent to 0 valued byte array.
  145. func (t *Tree) AddBatch(keys, values [][]byte) ([]int, error) {
  146. wTx := t.db.WriteTx()
  147. defer wTx.Discard()
  148. invalids, err := t.AddBatchWithTx(wTx, keys, values)
  149. if err != nil {
  150. return invalids, err
  151. }
  152. return invalids, wTx.Commit()
  153. }
  154. // AddBatchWithTx does the same than the AddBatch method, but allowing to pass
  155. // the db.WriteTx that is used. The db.WriteTx will not be committed inside
  156. // this method.
  157. func (t *Tree) AddBatchWithTx(wTx db.WriteTx, keys, values [][]byte) ([]int, error) {
  158. t.Lock()
  159. defer t.Unlock()
  160. if !t.editable() {
  161. return nil, ErrSnapshotNotEditable
  162. }
  163. vt, err := t.loadVT(wTx)
  164. if err != nil {
  165. return nil, err
  166. }
  167. e := []byte{}
  168. // equal the number of keys & values
  169. if len(keys) > len(values) {
  170. // add missing values
  171. for i := len(values); i < len(keys); i++ {
  172. values = append(values, e)
  173. }
  174. } else if len(keys) < len(values) {
  175. // crop extra values
  176. values = values[:len(keys)]
  177. }
  178. invalids, err := vt.addBatch(keys, values)
  179. if err != nil {
  180. return nil, err
  181. }
  182. // once the VirtualTree is build, compute the hashes
  183. pairs, err := vt.computeHashes()
  184. if err != nil {
  185. // currently invalids in computeHashes are not counted,
  186. // but should not be needed, as if there is an error there is
  187. // nothing stored in the db and the error is returned
  188. return nil, err
  189. }
  190. // store pairs in db
  191. for i := 0; i < len(pairs); i++ {
  192. if err := wTx.Set(pairs[i][0], pairs[i][1]); err != nil {
  193. return nil, err
  194. }
  195. }
  196. // store root (from the vt) to db
  197. if err := wTx.Set(dbKeyRoot, vt.root.h); err != nil {
  198. return nil, err
  199. }
  200. // update nLeafs
  201. if err := t.incNLeafs(wTx, len(keys)-len(invalids)); err != nil {
  202. return nil, err
  203. }
  204. return invalids, nil
  205. }
  206. // loadVT loads a new virtual tree (vt) from the current Tree, which contains
  207. // the same leafs.
  208. func (t *Tree) loadVT(rTx db.ReadTx) (vt, error) {
  209. vt := newVT(t.maxLevels, t.hashFunction)
  210. vt.params.dbg = t.dbg
  211. err := t.IterateWithTx(rTx, nil, func(k, v []byte) {
  212. if v[0] != PrefixValueLeaf {
  213. return
  214. }
  215. leafK, leafV := ReadLeafValue(v)
  216. if err := vt.add(0, leafK, leafV); err != nil {
  217. // TODO instead of panic, return this error
  218. panic(err)
  219. }
  220. })
  221. return vt, err
  222. }
  223. // Add inserts the key-value into the Tree. If the inputs come from a
  224. // *big.Int, is expected that are represented by a Little-Endian byte array
  225. // (for circom compatibility).
  226. func (t *Tree) Add(k, v []byte) error {
  227. wTx := t.db.WriteTx()
  228. defer wTx.Discard()
  229. if err := t.AddWithTx(wTx, k, v); err != nil {
  230. return err
  231. }
  232. return wTx.Commit()
  233. }
  234. // AddWithTx does the same than the Add method, but allowing to pass the
  235. // db.WriteTx that is used. The db.WriteTx will not be committed inside this
  236. // method.
  237. func (t *Tree) AddWithTx(wTx db.WriteTx, k, v []byte) error {
  238. t.Lock()
  239. defer t.Unlock()
  240. if !t.editable() {
  241. return ErrSnapshotNotEditable
  242. }
  243. root, err := t.RootWithTx(wTx)
  244. if err != nil {
  245. return err
  246. }
  247. root, err = t.add(wTx, root, 0, k, v) // add from level 0
  248. if err != nil {
  249. return err
  250. }
  251. // store root to db
  252. if err := t.setRoot(wTx, root); err != nil {
  253. return err
  254. }
  255. // update nLeafs
  256. if err = t.incNLeafs(wTx, 1); err != nil {
  257. return err
  258. }
  259. return nil
  260. }
  261. func (t *Tree) add(wTx db.WriteTx, root []byte, fromLvl int, k, v []byte) ([]byte, error) {
  262. keyPath := make([]byte, t.hashFunction.Len())
  263. copy(keyPath[:], k)
  264. path := getPath(t.maxLevels, keyPath)
  265. // go down to the leaf
  266. var siblings [][]byte
  267. _, _, siblings, err := t.down(wTx, k, root, siblings, path, fromLvl, false)
  268. if err != nil {
  269. return nil, err
  270. }
  271. leafKey, leafValue, err := t.newLeafValue(k, v)
  272. if err != nil {
  273. return nil, err
  274. }
  275. if err := wTx.Set(leafKey, leafValue); err != nil {
  276. return nil, err
  277. }
  278. // go up to the root
  279. if len(siblings) == 0 {
  280. // return the leafKey as root
  281. return leafKey, nil
  282. }
  283. root, err = t.up(wTx, leafKey, siblings, path, len(siblings)-1, fromLvl)
  284. if err != nil {
  285. return nil, err
  286. }
  287. return root, nil
  288. }
  289. // down goes down to the leaf recursively
  290. func (t *Tree) down(rTx db.ReadTx, newKey, currKey []byte, siblings [][]byte,
  291. path []bool, currLvl int, getLeaf bool) (
  292. []byte, []byte, [][]byte, error) {
  293. if currLvl > t.maxLevels-1 {
  294. return nil, nil, nil, ErrMaxLevel
  295. }
  296. var err error
  297. var currValue []byte
  298. if bytes.Equal(currKey, t.emptyHash) {
  299. // empty value
  300. return currKey, emptyValue, siblings, nil
  301. }
  302. currValue, err = rTx.Get(currKey)
  303. if err != nil {
  304. return nil, nil, nil, err
  305. }
  306. switch currValue[0] {
  307. case PrefixValueEmpty: // empty
  308. fmt.Printf("newKey: %s, currKey: %s, currLvl: %d, currValue: %s\n",
  309. hex.EncodeToString(newKey), hex.EncodeToString(currKey),
  310. currLvl, hex.EncodeToString(currValue))
  311. panic("This point should not be reached, as the 'if' above" +
  312. " should avoid reaching this point. This panic is temporary" +
  313. " for reporting purposes, will be deleted in future versions." +
  314. " Please paste this log (including the previous lines) in a" +
  315. " new issue: https://github.com/vocdoni/arbo/issues/new") // TMP
  316. case PrefixValueLeaf: // leaf
  317. if !bytes.Equal(currValue, emptyValue) {
  318. if getLeaf {
  319. return currKey, currValue, siblings, nil
  320. }
  321. oldLeafKey, _ := ReadLeafValue(currValue)
  322. if bytes.Equal(newKey, oldLeafKey) {
  323. return nil, nil, nil, ErrKeyAlreadyExists
  324. }
  325. oldLeafKeyFull := make([]byte, t.hashFunction.Len())
  326. copy(oldLeafKeyFull[:], oldLeafKey)
  327. // if currKey is already used, go down until paths diverge
  328. oldPath := getPath(t.maxLevels, oldLeafKeyFull)
  329. siblings, err = t.downVirtually(siblings, currKey, newKey, oldPath, path, currLvl)
  330. if err != nil {
  331. return nil, nil, nil, err
  332. }
  333. }
  334. return currKey, currValue, siblings, nil
  335. case PrefixValueIntermediate: // intermediate
  336. if len(currValue) != PrefixValueLen+t.hashFunction.Len()*2 {
  337. return nil, nil, nil,
  338. fmt.Errorf("intermediate value invalid length (expected: %d, actual: %d)",
  339. PrefixValueLen+t.hashFunction.Len()*2, len(currValue))
  340. }
  341. // collect siblings while going down
  342. if path[currLvl] {
  343. // right
  344. lChild, rChild := ReadIntermediateChilds(currValue)
  345. siblings = append(siblings, lChild)
  346. return t.down(rTx, newKey, rChild, siblings, path, currLvl+1, getLeaf)
  347. }
  348. // left
  349. lChild, rChild := ReadIntermediateChilds(currValue)
  350. siblings = append(siblings, rChild)
  351. return t.down(rTx, newKey, lChild, siblings, path, currLvl+1, getLeaf)
  352. default:
  353. return nil, nil, nil, ErrInvalidValuePrefix
  354. }
  355. }
  356. // downVirtually is used when in a leaf already exists, and a new leaf which
  357. // shares the path until the existing leaf is being added
  358. func (t *Tree) downVirtually(siblings [][]byte, oldKey, newKey []byte, oldPath,
  359. newPath []bool, currLvl int) ([][]byte, error) {
  360. var err error
  361. if currLvl > t.maxLevels-1 {
  362. return nil, ErrMaxVirtualLevel
  363. }
  364. if oldPath[currLvl] == newPath[currLvl] {
  365. siblings = append(siblings, t.emptyHash)
  366. siblings, err = t.downVirtually(siblings, oldKey, newKey, oldPath, newPath, currLvl+1)
  367. if err != nil {
  368. return nil, err
  369. }
  370. return siblings, nil
  371. }
  372. // reached the divergence
  373. siblings = append(siblings, oldKey)
  374. return siblings, nil
  375. }
  376. // up goes up recursively updating the intermediate nodes
  377. func (t *Tree) up(wTx db.WriteTx, key []byte, siblings [][]byte, path []bool,
  378. currLvl, toLvl int) ([]byte, error) {
  379. var k, v []byte
  380. var err error
  381. if path[currLvl+toLvl] {
  382. k, v, err = t.newIntermediate(siblings[currLvl], key)
  383. if err != nil {
  384. return nil, err
  385. }
  386. } else {
  387. k, v, err = t.newIntermediate(key, siblings[currLvl])
  388. if err != nil {
  389. return nil, err
  390. }
  391. }
  392. // store k-v to db
  393. if err = wTx.Set(k, v); err != nil {
  394. return nil, err
  395. }
  396. if currLvl == 0 {
  397. // reached the root
  398. return k, nil
  399. }
  400. return t.up(wTx, k, siblings, path, currLvl-1, toLvl)
  401. }
  402. func (t *Tree) newLeafValue(k, v []byte) ([]byte, []byte, error) {
  403. t.dbg.incHash()
  404. return newLeafValue(t.hashFunction, k, v)
  405. }
  406. // newLeafValue takes a key & value from a leaf, and computes the leaf hash,
  407. // which is used as the leaf key. And the value is the concatenation of the
  408. // inputed key & value. The output of this function is used as key-value to
  409. // store the leaf in the DB.
  410. // [ 1 byte | 1 byte | N bytes | M bytes ]
  411. // [ type of node | length of key | key | value ]
  412. func newLeafValue(hashFunc HashFunction, k, v []byte) ([]byte, []byte, error) {
  413. leafKey, err := hashFunc.Hash(k, v, []byte{1})
  414. if err != nil {
  415. return nil, nil, err
  416. }
  417. var leafValue []byte
  418. leafValue = append(leafValue, byte(PrefixValueLeaf))
  419. leafValue = append(leafValue, byte(len(k)))
  420. leafValue = append(leafValue, k...)
  421. leafValue = append(leafValue, v...)
  422. return leafKey, leafValue, nil
  423. }
  424. // ReadLeafValue reads from a byte array the leaf key & value
  425. func ReadLeafValue(b []byte) ([]byte, []byte) {
  426. if len(b) < PrefixValueLen {
  427. return []byte{}, []byte{}
  428. }
  429. kLen := b[1]
  430. if len(b) < PrefixValueLen+int(kLen) {
  431. return []byte{}, []byte{}
  432. }
  433. k := b[PrefixValueLen : PrefixValueLen+kLen]
  434. v := b[PrefixValueLen+kLen:]
  435. return k, v
  436. }
  437. func (t *Tree) newIntermediate(l, r []byte) ([]byte, []byte, error) {
  438. t.dbg.incHash()
  439. return newIntermediate(t.hashFunction, l, r)
  440. }
  441. // newIntermediate takes the left & right keys of a intermediate node, and
  442. // computes its hash. Returns the hash of the node, which is the node key, and a
  443. // byte array that contains the value (which contains the left & right child
  444. // keys) to store in the DB.
  445. // [ 1 byte | 1 byte | N bytes | N bytes ]
  446. // [ type of node | length of left key | left key | right key ]
  447. func newIntermediate(hashFunc HashFunction, l, r []byte) ([]byte, []byte, error) {
  448. b := make([]byte, PrefixValueLen+hashFunc.Len()*2)
  449. b[0] = PrefixValueIntermediate
  450. b[1] = byte(len(l))
  451. copy(b[PrefixValueLen:PrefixValueLen+hashFunc.Len()], l)
  452. copy(b[PrefixValueLen+hashFunc.Len():], r)
  453. key, err := hashFunc.Hash(l, r)
  454. if err != nil {
  455. return nil, nil, err
  456. }
  457. return key, b, nil
  458. }
  459. // ReadIntermediateChilds reads from a byte array the two childs keys
  460. func ReadIntermediateChilds(b []byte) ([]byte, []byte) {
  461. if len(b) < PrefixValueLen {
  462. return []byte{}, []byte{}
  463. }
  464. lLen := b[1]
  465. if len(b) < PrefixValueLen+int(lLen) {
  466. return []byte{}, []byte{}
  467. }
  468. l := b[PrefixValueLen : PrefixValueLen+lLen]
  469. r := b[PrefixValueLen+lLen:]
  470. return l, r
  471. }
  472. func getPath(numLevels int, k []byte) []bool {
  473. path := make([]bool, numLevels)
  474. for n := 0; n < numLevels; n++ {
  475. path[n] = k[n/8]&(1<<(n%8)) != 0
  476. }
  477. return path
  478. }
  479. // Update updates the value for a given existing key. If the given key does not
  480. // exist, returns an error.
  481. func (t *Tree) Update(k, v []byte) error {
  482. wTx := t.db.WriteTx()
  483. defer wTx.Discard()
  484. if err := t.UpdateWithTx(wTx, k, v); err != nil {
  485. return err
  486. }
  487. return wTx.Commit()
  488. }
  489. // UpdateWithTx does the same than the Update method, but allowing to pass the
  490. // db.WriteTx that is used. The db.WriteTx will not be committed inside this
  491. // method.
  492. func (t *Tree) UpdateWithTx(wTx db.WriteTx, k, v []byte) error {
  493. t.Lock()
  494. defer t.Unlock()
  495. if !t.editable() {
  496. return ErrSnapshotNotEditable
  497. }
  498. var err error
  499. keyPath := make([]byte, t.hashFunction.Len())
  500. copy(keyPath[:], k)
  501. path := getPath(t.maxLevels, keyPath)
  502. root, err := t.RootWithTx(wTx)
  503. if err != nil {
  504. return err
  505. }
  506. var siblings [][]byte
  507. _, valueAtBottom, siblings, err := t.down(wTx, k, root, siblings, path, 0, true)
  508. if err != nil {
  509. return err
  510. }
  511. oldKey, _ := ReadLeafValue(valueAtBottom)
  512. if !bytes.Equal(oldKey, k) {
  513. return ErrKeyNotFound
  514. }
  515. leafKey, leafValue, err := t.newLeafValue(k, v)
  516. if err != nil {
  517. return err
  518. }
  519. if err := wTx.Set(leafKey, leafValue); err != nil {
  520. return err
  521. }
  522. // go up to the root
  523. if len(siblings) == 0 {
  524. return t.setRoot(wTx, leafKey)
  525. }
  526. root, err = t.up(wTx, leafKey, siblings, path, len(siblings)-1, 0)
  527. if err != nil {
  528. return err
  529. }
  530. // store root to db
  531. if err := t.setRoot(wTx, root); err != nil {
  532. return err
  533. }
  534. return nil
  535. }
  536. // GenProof generates a MerkleTree proof for the given key. The leaf value is
  537. // returned, together with the packed siblings of the proof, and a boolean
  538. // parameter that indicates if the proof is of existence (true) or not (false).
  539. func (t *Tree) GenProof(k []byte) ([]byte, []byte, []byte, bool, error) {
  540. rTx := t.db.ReadTx()
  541. defer rTx.Discard()
  542. return t.GenProofWithTx(rTx, k)
  543. }
  544. // GenProofWithTx does the same than the GenProof method, but allowing to pass
  545. // the db.ReadTx that is used.
  546. func (t *Tree) GenProofWithTx(rTx db.ReadTx, k []byte) ([]byte, []byte, []byte, bool, error) {
  547. keyPath := make([]byte, t.hashFunction.Len())
  548. copy(keyPath[:], k)
  549. root, err := t.RootWithTx(rTx)
  550. if err != nil {
  551. return nil, nil, nil, false, err
  552. }
  553. path := getPath(t.maxLevels, keyPath)
  554. // go down to the leaf
  555. var siblings [][]byte
  556. _, value, siblings, err := t.down(rTx, k, root, siblings, path, 0, true)
  557. if err != nil {
  558. return nil, nil, nil, false, err
  559. }
  560. s := PackSiblings(t.hashFunction, siblings)
  561. leafK, leafV := ReadLeafValue(value)
  562. if !bytes.Equal(k, leafK) {
  563. // key not in tree, proof of non-existence
  564. return leafK, leafV, s, false, nil
  565. }
  566. return leafK, leafV, s, true, nil
  567. }
  568. // PackSiblings packs the siblings into a byte array.
  569. // [ 1 byte | L bytes | S * N bytes ]
  570. // [ bitmap length (L) | bitmap | N non-zero siblings ]
  571. // Where the bitmap indicates if the sibling is 0 or a value from the siblings
  572. // array. And S is the size of the output of the hash function used for the
  573. // Tree.
  574. func PackSiblings(hashFunc HashFunction, siblings [][]byte) []byte {
  575. var b []byte
  576. var bitmap []bool
  577. emptySibling := make([]byte, hashFunc.Len())
  578. for i := 0; i < len(siblings); i++ {
  579. if bytes.Equal(siblings[i], emptySibling) {
  580. bitmap = append(bitmap, false)
  581. } else {
  582. bitmap = append(bitmap, true)
  583. b = append(b, siblings[i]...)
  584. }
  585. }
  586. bitmapBytes := bitmapToBytes(bitmap)
  587. l := len(bitmapBytes)
  588. res := make([]byte, l+1+len(b))
  589. res[0] = byte(l) // set the bitmapBytes length
  590. copy(res[1:1+l], bitmapBytes)
  591. copy(res[1+l:], b)
  592. return res
  593. }
  594. // UnpackSiblings unpacks the siblings from a byte array.
  595. func UnpackSiblings(hashFunc HashFunction, b []byte) ([][]byte, error) {
  596. l := b[0]
  597. bitmapBytes := b[1 : 1+l]
  598. bitmap := bytesToBitmap(bitmapBytes)
  599. siblingsBytes := b[1+l:]
  600. iSibl := 0
  601. emptySibl := make([]byte, hashFunc.Len())
  602. var siblings [][]byte
  603. for i := 0; i < len(bitmap); i++ {
  604. if iSibl >= len(siblingsBytes) {
  605. break
  606. }
  607. if bitmap[i] {
  608. siblings = append(siblings, siblingsBytes[iSibl:iSibl+hashFunc.Len()])
  609. iSibl += hashFunc.Len()
  610. } else {
  611. siblings = append(siblings, emptySibl)
  612. }
  613. }
  614. return siblings, nil
  615. }
  616. func bitmapToBytes(bitmap []bool) []byte {
  617. bitmapBytesLen := int(math.Ceil(float64(len(bitmap)) / 8)) //nolint:gomnd
  618. b := make([]byte, bitmapBytesLen)
  619. for i := 0; i < len(bitmap); i++ {
  620. if bitmap[i] {
  621. b[i/8] |= 1 << (i % 8)
  622. }
  623. }
  624. return b
  625. }
  626. func bytesToBitmap(b []byte) []bool {
  627. var bitmap []bool
  628. for i := 0; i < len(b); i++ {
  629. for j := 0; j < 8; j++ {
  630. bitmap = append(bitmap, b[i]&(1<<j) > 0)
  631. }
  632. }
  633. return bitmap
  634. }
  635. // Get returns the value for a given key. If the key is not found, will return
  636. // the error ErrKeyNotFound, and in the leafK & leafV parameters will be placed
  637. // the data found in the tree in the leaf that was on the path going to the
  638. // input key.
  639. func (t *Tree) Get(k []byte) ([]byte, []byte, error) {
  640. rTx := t.db.ReadTx()
  641. defer rTx.Discard()
  642. return t.GetWithTx(rTx, k)
  643. }
  644. // GetWithTx does the same than the Get method, but allowing to pass the
  645. // db.ReadTx that is used. If the key is not found, will return the error
  646. // ErrKeyNotFound, and in the leafK & leafV parameters will be placed the data
  647. // found in the tree in the leaf that was on the path going to the input key.
  648. func (t *Tree) GetWithTx(rTx db.ReadTx, k []byte) ([]byte, []byte, error) {
  649. keyPath := make([]byte, t.hashFunction.Len())
  650. copy(keyPath[:], k)
  651. root, err := t.RootWithTx(rTx)
  652. if err != nil {
  653. return nil, nil, err
  654. }
  655. path := getPath(t.maxLevels, keyPath)
  656. // go down to the leaf
  657. var siblings [][]byte
  658. _, value, _, err := t.down(rTx, k, root, siblings, path, 0, true)
  659. if err != nil {
  660. return nil, nil, err
  661. }
  662. leafK, leafV := ReadLeafValue(value)
  663. if !bytes.Equal(k, leafK) {
  664. return leafK, leafV, ErrKeyNotFound
  665. }
  666. return leafK, leafV, nil
  667. }
  668. // CheckProof verifies the given proof. The proof verification depends on the
  669. // HashFunction passed as parameter.
  670. func CheckProof(hashFunc HashFunction, k, v, root, packedSiblings []byte) (bool, error) {
  671. siblings, err := UnpackSiblings(hashFunc, packedSiblings)
  672. if err != nil {
  673. return false, err
  674. }
  675. keyPath := make([]byte, hashFunc.Len())
  676. copy(keyPath[:], k)
  677. key, _, err := newLeafValue(hashFunc, k, v)
  678. if err != nil {
  679. return false, err
  680. }
  681. path := getPath(len(siblings), keyPath)
  682. for i := len(siblings) - 1; i >= 0; i-- {
  683. if path[i] {
  684. key, _, err = newIntermediate(hashFunc, siblings[i], key)
  685. if err != nil {
  686. return false, err
  687. }
  688. } else {
  689. key, _, err = newIntermediate(hashFunc, key, siblings[i])
  690. if err != nil {
  691. return false, err
  692. }
  693. }
  694. }
  695. if bytes.Equal(key[:], root) {
  696. return true, nil
  697. }
  698. return false, nil
  699. }
  700. func (t *Tree) incNLeafs(wTx db.WriteTx, nLeafs int) error {
  701. oldNLeafs, err := t.GetNLeafs()
  702. if err != nil {
  703. return err
  704. }
  705. newNLeafs := oldNLeafs + nLeafs
  706. return t.setNLeafs(wTx, newNLeafs)
  707. }
  708. func (t *Tree) setNLeafs(wTx db.WriteTx, nLeafs int) error {
  709. b := make([]byte, 8)
  710. binary.LittleEndian.PutUint64(b, uint64(nLeafs))
  711. if err := wTx.Set(dbKeyNLeafs, b); err != nil {
  712. return err
  713. }
  714. return nil
  715. }
  716. // GetNLeafs returns the number of Leafs of the Tree.
  717. func (t *Tree) GetNLeafs() (int, error) {
  718. rTx := t.db.ReadTx()
  719. defer rTx.Discard()
  720. return t.GetNLeafsWithTx(rTx)
  721. }
  722. // GetNLeafsWithTx does the same than the GetNLeafs method, but allowing to
  723. // pass the db.ReadTx that is used.
  724. func (t *Tree) GetNLeafsWithTx(rTx db.ReadTx) (int, error) {
  725. b, err := rTx.Get(dbKeyNLeafs)
  726. if err != nil {
  727. return 0, err
  728. }
  729. nLeafs := binary.LittleEndian.Uint64(b)
  730. return int(nLeafs), nil
  731. }
  732. // Snapshot returns a read-only copy of the Tree from the given root
  733. func (t *Tree) Snapshot(fromRoot []byte) (*Tree, error) {
  734. t.RLock()
  735. defer t.RUnlock()
  736. // allow to define which root to use
  737. if fromRoot == nil {
  738. var err error
  739. fromRoot, err = t.Root()
  740. if err != nil {
  741. return nil, err
  742. }
  743. }
  744. return &Tree{
  745. db: t.db,
  746. maxLevels: t.maxLevels,
  747. snapshotRoot: fromRoot,
  748. hashFunction: t.hashFunction,
  749. dbg: t.dbg,
  750. }, nil
  751. }
  752. // Iterate iterates through the full Tree, executing the given function on each
  753. // node of the Tree.
  754. func (t *Tree) Iterate(fromRoot []byte, f func([]byte, []byte)) error {
  755. rTx := t.db.ReadTx()
  756. defer rTx.Discard()
  757. return t.IterateWithTx(rTx, fromRoot, f)
  758. }
  759. // IterateWithTx does the same than the Iterate method, but allowing to pass
  760. // the db.ReadTx that is used.
  761. func (t *Tree) IterateWithTx(rTx db.ReadTx, fromRoot []byte, f func([]byte, []byte)) error {
  762. // allow to define which root to use
  763. if fromRoot == nil {
  764. var err error
  765. fromRoot, err = t.RootWithTx(rTx)
  766. if err != nil {
  767. return err
  768. }
  769. }
  770. return t.iter(rTx, fromRoot, f)
  771. }
  772. // IterateWithStop does the same than Iterate, but with int for the current
  773. // level, and a boolean parameter used by the passed function, is to indicate to
  774. // stop iterating on the branch when the method returns 'true'.
  775. func (t *Tree) IterateWithStop(fromRoot []byte, f func(int, []byte, []byte) bool) error {
  776. rTx := t.db.ReadTx()
  777. defer rTx.Discard()
  778. // allow to define which root to use
  779. if fromRoot == nil {
  780. var err error
  781. fromRoot, err = t.RootWithTx(rTx)
  782. if err != nil {
  783. return err
  784. }
  785. }
  786. return t.iterWithStop(rTx, fromRoot, 0, f)
  787. }
  788. // IterateWithStopWithTx does the same than the IterateWithStop method, but
  789. // allowing to pass the db.ReadTx that is used.
  790. func (t *Tree) IterateWithStopWithTx(rTx db.ReadTx, fromRoot []byte,
  791. f func(int, []byte, []byte) bool) error {
  792. // allow to define which root to use
  793. if fromRoot == nil {
  794. var err error
  795. fromRoot, err = t.RootWithTx(rTx)
  796. if err != nil {
  797. return err
  798. }
  799. }
  800. return t.iterWithStop(rTx, fromRoot, 0, f)
  801. }
  802. func (t *Tree) iterWithStop(rTx db.ReadTx, k []byte, currLevel int,
  803. f func(int, []byte, []byte) bool) error {
  804. var v []byte
  805. var err error
  806. if bytes.Equal(k, t.emptyHash) {
  807. v = t.emptyHash
  808. } else {
  809. v, err = rTx.Get(k)
  810. if err != nil {
  811. return err
  812. }
  813. }
  814. currLevel++
  815. switch v[0] {
  816. case PrefixValueEmpty:
  817. f(currLevel, k, v)
  818. case PrefixValueLeaf:
  819. f(currLevel, k, v)
  820. case PrefixValueIntermediate:
  821. stop := f(currLevel, k, v)
  822. if stop {
  823. return nil
  824. }
  825. l, r := ReadIntermediateChilds(v)
  826. if err = t.iterWithStop(rTx, l, currLevel, f); err != nil {
  827. return err
  828. }
  829. if err = t.iterWithStop(rTx, r, currLevel, f); err != nil {
  830. return err
  831. }
  832. default:
  833. return ErrInvalidValuePrefix
  834. }
  835. return nil
  836. }
  837. func (t *Tree) iter(rTx db.ReadTx, k []byte, f func([]byte, []byte)) error {
  838. f2 := func(currLvl int, k, v []byte) bool {
  839. f(k, v)
  840. return false
  841. }
  842. return t.iterWithStop(rTx, k, 0, f2)
  843. }
  844. // Dump exports all the Tree leafs in a byte array of length:
  845. // [ N * (2+len(k+v)) ]. Where N is the number of key-values, and for each k+v:
  846. // [ 1 byte | 1 byte | S bytes | len(v) bytes ]
  847. // [ len(k) | len(v) | key | value ]
  848. // Where S is the size of the output of the hash function used for the Tree.
  849. func (t *Tree) Dump(fromRoot []byte) ([]byte, error) {
  850. // allow to define which root to use
  851. if fromRoot == nil {
  852. var err error
  853. fromRoot, err = t.Root()
  854. if err != nil {
  855. return nil, err
  856. }
  857. }
  858. // WARNING current encoding only supports key & values of 255 bytes each
  859. // (due using only 1 byte for the length headers).
  860. var b []byte
  861. err := t.Iterate(fromRoot, func(k, v []byte) {
  862. if v[0] != PrefixValueLeaf {
  863. return
  864. }
  865. leafK, leafV := ReadLeafValue(v)
  866. kv := make([]byte, 2+len(leafK)+len(leafV))
  867. kv[0] = byte(len(leafK))
  868. kv[1] = byte(len(leafV))
  869. copy(kv[2:2+len(leafK)], leafK)
  870. copy(kv[2+len(leafK):], leafV)
  871. b = append(b, kv...)
  872. })
  873. return b, err
  874. }
  875. // ImportDump imports the leafs (that have been exported with the Dump method)
  876. // in the Tree.
  877. func (t *Tree) ImportDump(b []byte) error {
  878. if !t.editable() {
  879. return ErrSnapshotNotEditable
  880. }
  881. root, err := t.Root()
  882. if err != nil {
  883. return err
  884. }
  885. if !bytes.Equal(root, t.emptyHash) {
  886. return ErrTreeNotEmpty
  887. }
  888. r := bytes.NewReader(b)
  889. var keys, values [][]byte
  890. for {
  891. l := make([]byte, 2)
  892. _, err = io.ReadFull(r, l)
  893. if err == io.EOF {
  894. break
  895. } else if err != nil {
  896. return err
  897. }
  898. k := make([]byte, l[0])
  899. _, err = io.ReadFull(r, k)
  900. if err != nil {
  901. return err
  902. }
  903. v := make([]byte, l[1])
  904. _, err = io.ReadFull(r, v)
  905. if err != nil {
  906. return err
  907. }
  908. keys = append(keys, k)
  909. values = append(values, v)
  910. }
  911. if _, err = t.AddBatch(keys, values); err != nil {
  912. return err
  913. }
  914. return nil
  915. }
  916. // Graphviz iterates across the full tree to generate a string Graphviz
  917. // representation of the tree and writes it to w
  918. func (t *Tree) Graphviz(w io.Writer, fromRoot []byte) error {
  919. return t.GraphvizFirstNLevels(w, fromRoot, t.maxLevels)
  920. }
  921. // GraphvizFirstNLevels iterates across the first NLevels of the tree to
  922. // generate a string Graphviz representation of the first NLevels of the tree
  923. // and writes it to w
  924. func (t *Tree) GraphvizFirstNLevels(w io.Writer, fromRoot []byte, untilLvl int) error {
  925. fmt.Fprintf(w, `digraph hierarchy {
  926. node [fontname=Monospace,fontsize=10,shape=box]
  927. `)
  928. rTx := t.db.ReadTx()
  929. defer rTx.Discard()
  930. if fromRoot == nil {
  931. var err error
  932. fromRoot, err = t.RootWithTx(rTx)
  933. if err != nil {
  934. return err
  935. }
  936. }
  937. nEmpties := 0
  938. err := t.iterWithStop(rTx, fromRoot, 0, func(currLvl int, k, v []byte) bool {
  939. if currLvl == untilLvl {
  940. return true // to stop the iter from going down
  941. }
  942. switch v[0] {
  943. case PrefixValueEmpty:
  944. case PrefixValueLeaf:
  945. fmt.Fprintf(w, "\"%v\" [style=filled];\n", hex.EncodeToString(k[:nChars]))
  946. // key & value from the leaf
  947. kB, vB := ReadLeafValue(v)
  948. fmt.Fprintf(w, "\"%v\" -> {\"k:%v\\nv:%v\"}\n",
  949. hex.EncodeToString(k[:nChars]), hex.EncodeToString(kB[:nChars]),
  950. hex.EncodeToString(vB[:nChars]))
  951. fmt.Fprintf(w, "\"k:%v\\nv:%v\" [style=dashed]\n",
  952. hex.EncodeToString(kB[:nChars]), hex.EncodeToString(vB[:nChars]))
  953. case PrefixValueIntermediate:
  954. l, r := ReadIntermediateChilds(v)
  955. lStr := hex.EncodeToString(l[:nChars])
  956. rStr := hex.EncodeToString(r[:nChars])
  957. eStr := ""
  958. if bytes.Equal(l, t.emptyHash) {
  959. lStr = fmt.Sprintf("empty%v", nEmpties)
  960. eStr += fmt.Sprintf("\"%v\" [style=dashed,label=0];\n",
  961. lStr)
  962. nEmpties++
  963. }
  964. if bytes.Equal(r, t.emptyHash) {
  965. rStr = fmt.Sprintf("empty%v", nEmpties)
  966. eStr += fmt.Sprintf("\"%v\" [style=dashed,label=0];\n",
  967. rStr)
  968. nEmpties++
  969. }
  970. fmt.Fprintf(w, "\"%v\" -> {\"%v\" \"%v\"}\n", hex.EncodeToString(k[:nChars]),
  971. lStr, rStr)
  972. fmt.Fprint(w, eStr)
  973. default:
  974. }
  975. return false
  976. })
  977. fmt.Fprintf(w, "}\n")
  978. return err
  979. }
  980. // PrintGraphviz prints the output of Tree.Graphviz
  981. func (t *Tree) PrintGraphviz(fromRoot []byte) error {
  982. if fromRoot == nil {
  983. var err error
  984. fromRoot, err = t.Root()
  985. if err != nil {
  986. return err
  987. }
  988. }
  989. return t.PrintGraphvizFirstNLevels(fromRoot, t.maxLevels)
  990. }
  991. // PrintGraphvizFirstNLevels prints the output of Tree.GraphvizFirstNLevels
  992. func (t *Tree) PrintGraphvizFirstNLevels(fromRoot []byte, untilLvl int) error {
  993. if fromRoot == nil {
  994. var err error
  995. fromRoot, err = t.Root()
  996. if err != nil {
  997. return err
  998. }
  999. }
  1000. w := bytes.NewBufferString("")
  1001. fmt.Fprintf(w,
  1002. "--------\nGraphviz of the Tree with Root "+hex.EncodeToString(fromRoot)+":\n")
  1003. err := t.GraphvizFirstNLevels(w, fromRoot, untilLvl)
  1004. if err != nil {
  1005. fmt.Println(w)
  1006. return err
  1007. }
  1008. fmt.Fprintf(w,
  1009. "End of Graphviz of the Tree with Root "+hex.EncodeToString(fromRoot)+"\n--------\n")
  1010. fmt.Println(w)
  1011. return nil
  1012. }
  1013. // TODO circom proofs
  1014. // TODO data structure for proofs (including root, key, value, siblings,
  1015. // hashFunction) + method to verify that data structure