Commit Graph

64 Commits

Author SHA1 Message Date
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
arnau
f09b0b0392 Merge pull request #14 from vocdoni/fix/missing-emptyHash-in-snapshottree
Add missing emptyHash in Snapshot Tree
2021-09-09 12:29:59 +02:00
arnaucube
64c8f8d0bc Add missing emptyHash in Snapshot Tree 2021-09-09 11:42:41 +02:00
arnau
de5914f453 Merge pull request #11 from vocdoni/fix/incNLeafs
Fix: Use GetNLeafsWithTx in incNLeafs
2021-08-31 16:15:31 +02:00
Eduard S
baa1d7af48 Fix: Use GetNLeafsWithTx in incNLeafs 2021-08-31 14:40:35 +02:00
arnau
4836f19755 Merge pull request #9 from vocdoni/fix/level
Fix level when going down & in computeHashes
2021-08-31 12:37:21 +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
arnau
61b823d444 Merge pull request #8 from vocdoni/fix/vt-addbatch
Fix/vt addbatch
2021-08-30 12:42:59 +02:00
arnaucube
01408fba86 Use Prefix consts instead of hardcoded values in newLeafValue & newIntermediate methods 2021-08-30 11:27:59 +02:00
arnaucube
1638860da0 Fix VirtualTree.addBatch bucket levels 2021-08-29 23:38:41 +02:00
arnaucube
bc23beb813 Add NewTreeWithTx function 2021-08-25 10:50:41 +02:00
arnaucube
5b1a40f7f4 Small update on errors of UpdateWithTx, GenProofWithTx, ImportDump 2021-08-24 14:51:16 +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
abc04a5868 Add AddBatch explanation in README.md 2021-07-28 10:05:36 +02:00
arnaucube
01ec075b35 Update missing errors management 2021-07-21 11:57:18 +02:00
arnaucube
df896f6e96 Add option to input empty values at AddBatch
Add option to input empty values at AddBatch & different number of keys
& values.
2021-07-02 19:08:46 +02:00
arnaucube
a8c7ea9808 Add circom test w/ circuit for CircomVerifierProof
Add circom test with circuit for CircomVerifierProofs, which allows to
automatically check that the data generated from arbo matches the circom
circuit of a SMTVerifierProof.
Added also GHA workflow to test the circuits with the output of arbo
code.
2021-06-16 09:25:04 +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
f10265ed40 Migrate repository to github.com/vocdoni/arbo 2021-06-08 11:27:04 +02:00
arnaucube
f2d5037862 Add Usage section to README.md 2021-06-04 19:03:25 +02:00
arnaucube
2c62f31446 Update upFromNodes function for unbalanced tree
- Update upFromNodes function for unbalanced tree case
- Add AddBatchTestVector2 & 3 with some edge cases
- Add checkRoots test method, which stores the Dump of the tree to file for after-debug
2021-06-03 18:27:46 +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
f24fb28266 Small update addbatch_test.go org 2021-05-26 09:30:57 +02:00
arnaucube
d16ebd0c80 Rm functions related to Old AddBatch 2021-05-24 22:26:50 +02:00
arnaucube
f31ed1d2d1 AddBatch implement using VirtualTree.AddBatch
Reduces the num of hashes, dbGet and dbPut on cases D & E.

Current benchmarks:
```
CASE A, AddBatch was 9.101855 times faster than without AddBatch
	nCPU: 4, nLeafs: 1024, hash: Poseidon, db: memory
	dbgStats(hash: 2.047k, dbGet: 1, dbPut: 2.049k)
CASE B, AddBatch was 8.244078 times faster than without AddBatch
	nCPU: 4, nLeafs: 1024, hash: Poseidon, db: memory
	dbgStats(hash: 2.047k, dbGet: 198, dbPut: 2.049k)
CASE C, AddBatch was 8.511131 times faster than without AddBatch
	nCPU: 4, nLeafs: 1024, hash: Poseidon, db: memory
	dbgStats(hash: 2.047k, dbGet: 202, dbPut: 2.049k)
CASE D, AddBatch was 8.565696 times faster than without AddBatch
	nCPU: 4, nLeafs: 4096, hash: Poseidon, db: memory
	dbgStats(hash: 8.191k, dbGet: 1.800k, dbPut: 8.193k)
CASE E, AddBatch was 8.970056 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: leveldb
	Add loop:	10.616145533s
		dbgStats(hash: 824.884k, dbGet: 788.975k, dbPut: 924.884k)
	AddBatch:	942.34815ms
		dbgStats(hash: 122.051k, dbGet: 1, dbPut: 122.053k)
TestDbgStats
	add in loop    dbgStats(hash: 141.911k, dbGet: 134.650k, dbPut: 161.911k)
	addbatch caseA dbgStats(hash: 24.522k, dbGet: 1, dbPut: 24.524k)
	addbatch caseD dbgStats(hash: 26.985k, dbGet: 2.465k, dbPut: 26.989k)
```
2021-05-24 17:05:03 +02:00
arnaucube
d09bd605bb Implement VirtualTree.addBatch with cpu parallelization 2021-05-23 21:31:33 +02:00
arnaucube
4167583b8d Add dbgStats metrics
Add dbgStats metrics to analyze number of Hashes, db Gets, and db Puts.

Current benchmarks:
```
CASE A, AddBatch was 8.841700 times faster than without AddBatch
	nCPU: 4, nLeafs: 1024, hash: Poseidon, db: memory
	dbgStats(hash: 2.044k, dbGet: 1, dbPut: 2.049k)
CASE B, AddBatch was 7.678766 times faster than without AddBatch
	nCPU: 4, nLeafs: 1024, hash: Poseidon, db: memory
	dbgStats(hash: 2.044k, dbGet: 199, dbPut: 2.049k)
CASE C, AddBatch was 8.401087 times faster than without AddBatch
	nCPU: 4, nLeafs: 1024, hash: Poseidon, db: memory
	dbgStats(hash: 2.044k, dbGet: 207, dbPut: 2.049k)
CASE D, AddBatch was 2.466346 times faster than without AddBatch
	nCPU: 4, nLeafs: 4096, hash: Poseidon, db: memory
	dbgStats(hash: 33.884k, dbGet: 30.697k, dbPut: 33.889k)
CASE E, AddBatch was 1.958160 times faster than without AddBatch
	nCPU: 4, nLeafs: 4096, hash: Poseidon, db: memory
	dbgStats(hash: 41.419k, dbGet: 37.558k, dbPut: 41.874k)
TestAddBatchBench: nCPU: 4, nLeafs: 50000, hash: Blake2b, db: leveldb
	Add loop:	10.089858449s
		dbgStats(hash: 825.285k, dbGet: 788.869k, dbPut: 925.285k)
	AddBatch:	904.647829ms
		dbgStats(hash: 122.458k, dbGet: 1, dbPut: 122.463k)
TestDbgStats
	add in loop    dbgStats(hash: 141.915k, dbGet: 134.602k, dbPut: 161.915k)
	addbatch caseA dbgStats(hash: 24.528k, dbGet: 1, dbPut: 24.533k)
	addbatch caseD dbgStats(hash: 115.506k, dbGet: 97.482k, dbPut: 115.516k)
```
2021-05-23 16:19:04 +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
66f6ae14bb Add computeHashes at virtual tree 2021-05-17 22:16:08 +02:00
arnaucube
e13b09215e Add virtual tree graphviz printing methods 2021-05-17 21:33:12 +02:00
arnaucube
8a8ec8c49e Implement Virtual Tree construction
VirtualTree (vt) computes a tree without computing any hash. With the
idea of once all the leafs are placed in their positions, the hashes can
be computed, avoiding computing a node hash more than one time.
2021-05-17 19:32:15 +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
0eda440d93 Update CaseB to handle repeated keys cases
- Update CaseB to handle repeated keys cases
- Add test for AddBatch/CaseB with repeated keys
- AddBatch-tests abstract code reusage
2021-05-08 14:52:15 +02:00
arnaucube
0dee3bc050 AddBatch tests abstract code reusage 2021-05-03 18:03:52 +02:00
arnaucube
da268badbb Simplify cyclomatic complexity of AddBatch 2021-05-01 17:30:12 +02:00
arnaucube
f364cf6137 AddBatch: add CaseE, add parallelization on CaseB 2021-04-27 22:01:57 +02:00
arnaucube
91a98bf18d AddBatch: commit tx at end,allow batch w/ len!=2^n 2021-04-25 17:55:59 +02:00
arnaucube
1c2b7d6871 Add CPU parallelization to AddBatch CaseD
AddBatch in CaseD, is parallelized (for each CPU) until almost the top
level, almost dividing the needed time by the number of CPUs.
2021-04-25 17:55:59 +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
b3007a057e AddBatchOpt return invalid key positions 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
a3473079de Add AddBatch CaseC
CASE C: ALMOST CASE B --> if Tree has few Leafs (but numLeafs>=minLeafsThreshold)
==============================================================================
- Use A, B, G, F as Roots of subtrees
- Do CASE B for each subtree
- Then go from L to the Root

              R
             /  \
            /    \
           /      \
          *        *
         / |      / \
        /  |     /   \
       /   |    /     \
L:    A    B   G       D
              / \
             /   \
            /     \
           C      *
                 / \
                /   \
               /     \
              ...    ... (nLeafs >= minLeafsThreshold)
2021-04-25 17:55:46 +02:00
arnaucube
600fd212cc Add AddBatch CaseB
CASE B: ALMOST CASE A, Almost empty Tree --> if Tree has numLeafs < minLeafsThreshold
==============================================================================
- Get the Leafs (key & value) (iterate the tree from the current root getting
the leafs)
- Create a new empty Tree
- Do CASE A for the new Tree, giving the already existing key&values (leafs)
from the original Tree + the new key&values to be added from the AddBatch call

       R                 R
      / \               /  \
     A   *             /    \
        / \           /      \
       B   C         *        *
                    / |      / \
                   /  |     /   \
                  /   |    /     \
           L:    A    B   G       D
                         / \
                        /   \
                       /     \
                      C      *
                            / \
                           /   \
                          /     \
                         ...     ... (nLeafs < minLeafsThreshold)
2021-04-25 17:55:34 +02:00
arnaucube
6f43980c0f Add Tree.emptyHash & nLeafs methods 2021-04-25 17:55:34 +02:00