Commit Graph

37 Commits

Author SHA1 Message Date
arnaucube
2380214412 Extend Dump methods to work with Writer&Reader
Add methods DumpWriter & ImportDumpReader to allow generating tree
dumps and reading them working with Reader & Writer, in order to write
and read them directly from a file (internally line by line).
2022-08-25 10:31:22 +02:00
arnau
96ccfdb50f Merge pull request #26 from vocdoni/feature/addbatch-bigtrees-indisk
Feature/addbatch bigtrees indisk
2021-12-17 09:42:07 +01:00
arnaucube
4b6d6efdca Add thresholdNLeafs configurable
ThresholdNLeafs defines the threshold number of leafs in the tree that
determines if AddBatch will work in memory or in disk. It is defined
when calling NewTree, and if set to 0 it will work always in disk.
2021-11-23 06:59:33 +01:00
arnaucube
17bf867f61 Add bytes-bitmap test vectors 2021-11-12 18:59:08 +01:00
arnaucube
fc45fbde3f Add Pack & Unpack siblings test vectors 2021-11-11 11:40:11 +01:00
arnaucube
6cf1e58d9f Implement addBatchInDisk for big trees
Implement addBatchInDisk for big trees, which does not puts the tree in
memory, and works directly over the db data, parallelizing for each CPU.
2021-10-25 14:55:42 +02:00
arnaucube
e8404e16f3 Fix oldLeafKeyFull size at method tree.down 2021-10-05 18:06:11 +02:00
arnaucube
30d8b42fd3 Add checks that len(key)<=maxKeyLen
Add checks that the key is not bigger than maximum key length for the tree
maxLevels size, where maximum key len = ceil(maxLevels/8).

This is because if the key bits length is bigger than the maxLevels of the
tree, two different keys that their difference is at the end, will collision in
the same leaf of the tree (at the max depth).
2021-10-04 14:33:49 +02:00
arnaucube
0921cac304 Update keyPath to be ceil(maxLevels/8) 2021-10-01 10:57:50 +02:00
arnaucube
6062549cf0 Add tree.SetRoot (& WithTx) 2021-09-27 13:46:53 +02:00
arnaucube
ed0cf70d57 Update VT goroutines errs & Update Pack&UnpackSibl
- Update VT goroutines errs to avoid race condition
- Update pack & unpack siblings to use 2-byte for full length & bitmap
bytes length
- Add check in UnpackSiblings to avoid panic
2021-09-21 17:37:11 +02:00
arnaucube
64c8f8d0bc Add missing emptyHash in Snapshot Tree 2021-09-09 11:42:41 +02:00
arnaucube
dff11f4a7f Fix level when going down & in computeHashes
Fix level when going down & in computeHashes, this affected the methods
Get, GenProof & AddBatch.
2021-08-31 11:59:38 +02:00
arnaucube
5f6c35e435 Update Snapshot & Root approach
Update Snapshot & Root approach to get the root always from the db,
except in the cases that the tree is a snapshot, in which the root will
be in memory.
In this way, when a snapshot is performed and the original tree gets
modifyed, the snapshot will still point to the old root. Also, the root
obtained from the db, uses also the db.ReadTx, so if the root is being
modifyied in the current tx (db.WriteTx), when getting the root it will
be return the lastest version that is in the tx but not yet in the db.
2021-08-13 12:38:05 +02:00
arnaucube
2514b3188f Update db usage to new dvote.db interface version 2021-08-06 18:41:13 +02:00
arnaucube
43cad713b0 Add Tree Circom Verifier Proofs
Circom Verifier Proofs allow to verify through a zkSNARK proof the
inclusion/exclusion of a leaf in a tree. This commit adds the needed
code in go to generate the circuit inputs for a CircomVerifierProof.
2021-06-16 08:59:02 +02:00
arnaucube
465f0ed172 Add Tree.Snapshot method 2021-06-14 17:51:37 +02:00
arnaucube
c6059fcb75 Migrate kv db to go.vocdoni.io/dvote/db interface
Case tree empty, AddBatch was 10.95x times faster than without AddBatch
	nCPU: 4, nLeafs: 1024, hash: Poseidon, db: memory
	dbgStats(hash: 2.047k, dbGet: 1, dbPut: 2.049k)
Case tree not empty w/ few leafs, AddBatch was 7.28x times faster than without AddBatch
	nCPU: 4, nLeafs: 1024, hash: Poseidon, db: memory
	dbgStats(hash: 2.047k, dbGet: 198, dbPut: 2.049k)
Case tree not empty w/ enough leafs, AddBatch was 5.94x times faster than without AddBatch
	nCPU: 4, nLeafs: 1024, hash: Poseidon, db: memory
	dbgStats(hash: 2.047k, dbGet: 1.000k, dbPut: 2.049k)
Case tree not empty, AddBatch was 9.27x times faster than without AddBatch
	nCPU: 4, nLeafs: 4096, hash: Poseidon, db: memory
	dbgStats(hash: 8.191k, dbGet: 1.800k, dbPut: 8.193k)
Case tree not empty & unbalanced, AddBatch was 10.67x times faster than without AddBatch
	nCPU: 4, nLeafs: 4096, hash: Poseidon, db: memory
	dbgStats(hash: 10.409k, dbGet: 2.668k, dbPut: 10.861k)
TestAddBatchBench: nCPU: 4, nLeafs: 50000, hash: Blake2b, db: badgerdb
	Add loop:	10.10829114s
	AddBatch:	732.030263ms
		dbgStats(hash: 122.518k, dbGet: 1, dbPut: 122.520k)
TestDbgStats
	add in loop in emptyTree dbgStats(hash: 141.721k, dbGet: 134.596k, dbPut: 161.721k)
	addbatch caseEmptyTree dbgStats(hash: 24.402k, dbGet: 1, dbPut: 24.404k)
	addbatch caseNotEmptyTree dbgStats(hash: 26.868k, dbGet: 2.468k, dbPut: 26.872k)
2021-06-03 18:23:45 +02:00
arnaucube
467f063129 Update check of already existing key on add
- Update check of already existing key on add
- Update docs of methods
2021-06-02 10:26:40 +02:00
arnaucube
0b2c3b07ed Update public methods signatures
- Update public methods signatures
- Remove 'lastAccess' param
- Add option to pass root for tree.Dump, Iterate, IterateWithStop,
Graphviz (and related)
- Move error messages to const defined error messages for external usage
2021-05-26 17:01:09 +02:00
arnaucube
2a57e223ef Add TestAddBatchBench
TestAddBatchBench
	nCPU: 4, nLeafs: 50000, hash: Blake2b, db: leveldb
		Add loop:	9.304195479s
		AddBatch:	817.590526ms
2021-05-20 12:04:54 +02:00
arnaucube
13378d338e Remove cropping on pow of two the kvs for AddBatch
- Remove cropping on power of two the kvs for AddBatch
  - As not needed to be power of two length with the Virtual Tree
- Fix keyValuesToKvs keyPath
2021-05-19 12:58:11 +02:00
arnaucube
03bb9f7447 AddBatch use Virtual Tree for empty trees/subtrees
- AddBatch use Virtual Tree for cases A,B,C
- ImportDump use AddBatch instead of adding one by one
- Reorg & add more virtual tree tests
2021-05-18 10:57:06 +02:00
arnaucube
6dcbbdf4a5 Replace naive AddBatch by optimized AddBatch
- Replace naive AddBatch by optimized AddBatch
- Add blake2b hash support
- Expose needed methods for external usage (ReadLeafValue,
ReadIntermediateChilds)
- Return 'value' in GenProof
2021-05-08 17:08:07 +02:00
arnaucube
890057cd82 Add AddBatch CaseD
CASE D: Already populated Tree
==============================
- Use A, B, C, D as subtree
- Sort the Keys in Buckets that share the initial part of the path
- For each subtree add there the new leafs

              R
             /  \
            /    \
           /      \
          *        *
         / |      / \
        /  |     /   \
       /   |    /     \
L:    A    B   C       D
     /\   /\  / \     / \
    ...  ... ... ... ... ...
2021-04-25 17:55:56 +02:00
arnaucube
a4ada7e2ee Add CPU parallelization to buildTreBottomUp
buildTreeBottomUp splits the key-values into n Buckets (where n is the
number of CPUs), in parallel builds a subtree for each bucket, once all
the subtrees are built, uses the subtrees roots as keys for a new tree,
which as result will have the complete Tree build from bottom to up,
where until the log2(nCPU) level it has been computed in parallel.

As result of this, the tree construction can be parallelized until
almost the top level, almost dividing the time by the number of CPUs.
2021-04-25 17:55:56 +02:00
arnaucube
6f43980c0f Add Tree.emptyHash & nLeafs methods 2021-04-25 17:55:34 +02:00
arnaucube
8ede441191 Add Graphviz generation methods 2021-04-17 09:33:00 +02:00
arnaucube
c26b23c544 Add Mutex, integrate tx into Tree struct 2021-04-12 21:26:26 +02:00
arnaucube
8af921667f Add tree.Update(k, v) method 2021-04-03 19:58:34 +02:00
arnaucube
f8ac4be02b Tests migrate from stretchr/testify to frankban/quicktest 2021-04-03 19:40:18 +02:00
arnaucube
f1665b1a15 Add Iterate, Dump, ImportDump methods to Tree 2021-04-02 15:31:38 +02:00
arnaucube
cf572f628e Add tree.AddBatch (using db.Tx) 2021-04-01 16:36:58 +02:00
arnaucube
4cd2ff6182 Add tree.Get 2021-03-31 23:05:10 +02:00
arnaucube
bde87ca844 Add proof verification 2021-03-31 20:34:36 +02:00
arnaucube
8c63b5d192 Add proof generation 2021-03-31 20:04:30 +02:00
arnaucube
43cb6041c9 Add Tree.Add compatible with circomlib 2021-03-30 22:41:34 +02:00