Browse Source

Fix mt.Update existing leaf & GenerateCircomVerifierProof

fix/hash-parsers
arnaucube 4 years ago
parent
commit
cda85ddc27
2 changed files with 22 additions and 3 deletions
  1. +1
    -1
      .github/workflows/main.yml
  2. +21
    -2
      merkletree.go

+ 1
- 1
.github/workflows/main.yml

@ -16,4 +16,4 @@ jobs:
- name: Checkout code - name: Checkout code
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: Run tests - name: Run tests
run: go test
run: go test ./...

+ 21
- 2
merkletree.go

@ -345,6 +345,25 @@ func (mt *MerkleTree) addNode(tx db.Tx, n *Node) (*Hash, error) {
return k, nil 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 // Get returns the value of the leaf for the given key
func (mt *MerkleTree) Get(k *big.Int) (*big.Int, *big.Int, []*Hash, error) { func (mt *MerkleTree) Get(k *big.Int) (*big.Int, *big.Int, []*Hash, error) {
// verfy that k is valid and fit inside the Finite Field. // verfy that k is valid and fit inside the Finite Field.
@ -437,7 +456,7 @@ func (mt *MerkleTree) Update(k, v *big.Int) (*CircomProcessorProof, error) {
cp.Siblings = CircomSiblingsFromSiblings(siblings, mt.maxLevels) cp.Siblings = CircomSiblingsFromSiblings(siblings, mt.maxLevels)
// update leaf and upload to the root // update leaf and upload to the root
newNodeLeaf := NewNodeLeaf(kHash, vHash) newNodeLeaf := NewNodeLeaf(kHash, vHash)
_, err := mt.addNode(tx, newNodeLeaf)
_, err := mt.updateNode(tx, newNodeLeaf)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -837,7 +856,7 @@ type CircomVerifierProof struct {
// is used. // is used.
func (mt *MerkleTree) GenerateCircomVerifierProof(k *big.Int, rootKey *Hash) (*CircomVerifierProof, error) { func (mt *MerkleTree) GenerateCircomVerifierProof(k *big.Int, rootKey *Hash) (*CircomVerifierProof, error) {
p, v, err := mt.GenerateProof(k, rootKey) p, v, err := mt.GenerateProof(k, rootKey)
if err != nil || err != ErrKeyNotFound {
if err != nil && err != ErrKeyNotFound {
return nil, err return nil, err
} }
var cp CircomVerifierProof var cp CircomVerifierProof

Loading…
Cancel
Save