arnaucube
arnaucube/go-merkletree-iden3Public Self-hosted repository
Fix mt.Update existing leaf & GenerateCircomVerifierProof
arnaucube root@arnaucube.com
Tue, 25 Aug 2020 11:35:52 +0200
commit

cda85ddc27944db550b8b326d9a162c55db05cd3

parent

dc656fdd32fc08f845ab663d22ed2acfe2e97135

2 files changed, 22 insertions(+), 3 deletions(-)

jump to
M .github/workflows/main.yml → .github/workflows/main.yml

@@ -16,4 +16,4 @@ go-version: ${{ matrix.go-version }}

- name: Checkout code uses: actions/checkout@v2 - name: Run tests - run: go test + run: go test ./...
M merkletree.go → merkletree.go

@@ -345,6 +345,25 @@ tx.Put(k[:], v)

return k, nil } +// updateNode updates an existing node in the MT. Empty nodes are not stored +// in the tree; they are all the same and assumed to always exist. +func (mt *MerkleTree) updateNode(tx db.Tx, n *Node) (*Hash, error) { + // verify that the MerkleTree is writable + if !mt.writable { + return nil, ErrNotWritable + } + if n.Type == NodeTypeEmpty { + return n.Key() + } + k, err := n.Key() + if err != nil { + return nil, err + } + v := n.Value() + tx.Put(k[:], v) + return k, nil +} + // Get returns the value of the leaf for the given key func (mt *MerkleTree) Get(k *big.Int) (*big.Int, *big.Int, []*Hash, error) { // verfy that k is valid and fit inside the Finite Field.

@@ -437,7 +456,7 @@ cp.OldValue = n.Entry[1]

cp.Siblings = CircomSiblingsFromSiblings(siblings, mt.maxLevels) // update leaf and upload to the root newNodeLeaf := NewNodeLeaf(kHash, vHash) - _, err := mt.addNode(tx, newNodeLeaf) + _, err := mt.updateNode(tx, newNodeLeaf) if err != nil { return nil, err }

@@ -837,7 +856,7 @@ // key in the MerkleTree. If the rootKey is nil, the current merkletree root

// is used. func (mt *MerkleTree) GenerateCircomVerifierProof(k *big.Int, rootKey *Hash) (*CircomVerifierProof, error) { p, v, err := mt.GenerateProof(k, rootKey) - if err != nil || err != ErrKeyNotFound { + if err != nil && err != ErrKeyNotFound { return nil, err } var cp CircomVerifierProof