|
|
@ -54,7 +54,8 @@ var ( |
|
|
|
|
|
|
|
dbKeyRootNode = []byte("currentroot") |
|
|
|
// HashZero is used at Empty nodes
|
|
|
|
HashZero = Hash{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} |
|
|
|
HashZero = Hash{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} |
|
|
|
) |
|
|
|
|
|
|
|
// Hash is the generic type stored in the MerkleTree
|
|
|
@ -114,11 +115,13 @@ func (h *Hash) Bytes() []byte { |
|
|
|
// method.
|
|
|
|
func NewBigIntFromHashBytes(b []byte) (*big.Int, error) { |
|
|
|
if len(b) != ElemBytesLen { |
|
|
|
return nil, fmt.Errorf("Expected 32 bytes, found %d bytes", len(b)) |
|
|
|
return nil, fmt.Errorf("Expected 32 bytes, found %d bytes", |
|
|
|
len(b)) |
|
|
|
} |
|
|
|
bi := new(big.Int).SetBytes(b[:ElemBytesLen]) |
|
|
|
if !cryptoUtils.CheckBigIntInField(bi) { |
|
|
|
return nil, fmt.Errorf("NewBigIntFromHashBytes: Value not inside the Finite Field") |
|
|
|
return nil, |
|
|
|
fmt.Errorf("NewBigIntFromHashBytes: Value not inside the Finite Field") |
|
|
|
} |
|
|
|
return bi, nil |
|
|
|
} |
|
|
@ -135,7 +138,8 @@ func NewHashFromBigInt(b *big.Int) *Hash { |
|
|
|
// that previously has ben generated by the Hash.Bytes() method.
|
|
|
|
func NewHashFromBytes(b []byte) (*Hash, error) { |
|
|
|
if len(b) != ElemBytesLen { |
|
|
|
return nil, fmt.Errorf("Expected 32 bytes, found %d bytes", len(b)) |
|
|
|
return nil, fmt.Errorf("Expected 32 bytes, found %d bytes", |
|
|
|
len(b)) |
|
|
|
} |
|
|
|
var h Hash |
|
|
|
copy(h[:], common.SwapEndianness(b)) |
|
|
@ -281,7 +285,8 @@ func (mt *MerkleTree) Add(k, v *big.Int) error { |
|
|
|
} |
|
|
|
|
|
|
|
// AddAndGetCircomProof does an Add, and returns a CircomProcessorProof
|
|
|
|
func (mt *MerkleTree) AddAndGetCircomProof(k, v *big.Int) (*CircomProcessorProof, error) { |
|
|
|
func (mt *MerkleTree) AddAndGetCircomProof(k, |
|
|
|
v *big.Int) (*CircomProcessorProof, error) { |
|
|
|
var cp CircomProcessorProof |
|
|
|
cp.Fnc = 2 |
|
|
|
cp.OldRoot = mt.rootKey |
|
|
@ -315,45 +320,46 @@ func (mt *MerkleTree) AddAndGetCircomProof(k, v *big.Int) (*CircomProcessorProof |
|
|
|
// pushLeaf recursively pushes an existing oldLeaf down until its path diverges
|
|
|
|
// from newLeaf, at which point both leafs are stored, all while updating the
|
|
|
|
// path.
|
|
|
|
func (mt *MerkleTree) pushLeaf(tx db.Tx, newLeaf *Node, oldLeaf *Node, |
|
|
|
lvl int, pathNewLeaf []bool, pathOldLeaf []bool) (*Hash, error) { |
|
|
|
func (mt *MerkleTree) pushLeaf(tx db.Tx, newLeaf *Node, oldLeaf *Node, lvl int, |
|
|
|
pathNewLeaf []bool, pathOldLeaf []bool) (*Hash, error) { |
|
|
|
if lvl > mt.maxLevels-2 { |
|
|
|
return nil, ErrReachedMaxLevel |
|
|
|
} |
|
|
|
var newNodeMiddle *Node |
|
|
|
if pathNewLeaf[lvl] == pathOldLeaf[lvl] { // We need to go deeper!
|
|
|
|
nextKey, err := mt.pushLeaf(tx, newLeaf, oldLeaf, lvl+1, pathNewLeaf, pathOldLeaf) |
|
|
|
nextKey, err := mt.pushLeaf(tx, newLeaf, oldLeaf, lvl+1, |
|
|
|
pathNewLeaf, pathOldLeaf) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
if pathNewLeaf[lvl] { |
|
|
|
newNodeMiddle = NewNodeMiddle(&HashZero, nextKey) // go right
|
|
|
|
} else { |
|
|
|
newNodeMiddle = NewNodeMiddle(nextKey, &HashZero) // go left
|
|
|
|
if pathNewLeaf[lvl] { // go right
|
|
|
|
newNodeMiddle = NewNodeMiddle(&HashZero, nextKey) |
|
|
|
} else { // go left
|
|
|
|
newNodeMiddle = NewNodeMiddle(nextKey, &HashZero) |
|
|
|
} |
|
|
|
return mt.addNode(tx, newNodeMiddle) |
|
|
|
} else { |
|
|
|
oldLeafKey, err := oldLeaf.Key() |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
newLeafKey, err := newLeaf.Key() |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
oldLeafKey, err := oldLeaf.Key() |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
newLeafKey, err := newLeaf.Key() |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
if pathNewLeaf[lvl] { |
|
|
|
newNodeMiddle = NewNodeMiddle(oldLeafKey, newLeafKey) |
|
|
|
} else { |
|
|
|
newNodeMiddle = NewNodeMiddle(newLeafKey, oldLeafKey) |
|
|
|
} |
|
|
|
// We can add newLeaf now. We don't need to add oldLeaf because it's already in the tree.
|
|
|
|
_, err = mt.addNode(tx, newLeaf) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
return mt.addNode(tx, newNodeMiddle) |
|
|
|
if pathNewLeaf[lvl] { |
|
|
|
newNodeMiddle = NewNodeMiddle(oldLeafKey, newLeafKey) |
|
|
|
} else { |
|
|
|
newNodeMiddle = NewNodeMiddle(newLeafKey, oldLeafKey) |
|
|
|
} |
|
|
|
// We can add newLeaf now. We don't need to add oldLeaf because it's
|
|
|
|
// already in the tree.
|
|
|
|
_, err = mt.addNode(tx, newLeaf) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
return mt.addNode(tx, newNodeMiddle) |
|
|
|
} |
|
|
|
|
|
|
|
// addLeaf recursively adds a newLeaf in the MT while updating the path.
|
|
|
@ -374,23 +380,27 @@ func (mt *MerkleTree) addLeaf(tx db.Tx, newLeaf *Node, key *Hash, |
|
|
|
return mt.addNode(tx, newLeaf) |
|
|
|
case NodeTypeLeaf: |
|
|
|
nKey := n.Entry[0] |
|
|
|
// Check if leaf node found contains the leaf node we are trying to add
|
|
|
|
// Check if leaf node found contains the leaf node we are
|
|
|
|
// trying to add
|
|
|
|
newLeafKey := newLeaf.Entry[0] |
|
|
|
if bytes.Equal(nKey[:], newLeafKey[:]) { |
|
|
|
return nil, ErrEntryIndexAlreadyExists |
|
|
|
} |
|
|
|
pathOldLeaf := getPath(mt.maxLevels, nKey[:]) |
|
|
|
// We need to push newLeaf down until its path diverges from n's path
|
|
|
|
// We need to push newLeaf down until its path diverges from
|
|
|
|
// n's path
|
|
|
|
return mt.pushLeaf(tx, newLeaf, n, lvl, path, pathOldLeaf) |
|
|
|
case NodeTypeMiddle: |
|
|
|
// We need to go deeper, continue traversing the tree, left or
|
|
|
|
// right depending on path
|
|
|
|
var newNodeMiddle *Node |
|
|
|
if path[lvl] { |
|
|
|
nextKey, err = mt.addLeaf(tx, newLeaf, n.ChildR, lvl+1, path) // go right
|
|
|
|
if path[lvl] { // go right
|
|
|
|
nextKey, err = mt.addLeaf(tx, newLeaf, n.ChildR, lvl+1, |
|
|
|
path) |
|
|
|
newNodeMiddle = NewNodeMiddle(n.ChildL, nextKey) |
|
|
|
} else { |
|
|
|
nextKey, err = mt.addLeaf(tx, newLeaf, n.ChildL, lvl+1, path) // go left
|
|
|
|
} else { // go left
|
|
|
|
nextKey, err = mt.addLeaf(tx, newLeaf, n.ChildL, lvl+1, |
|
|
|
path) |
|
|
|
newNodeMiddle = NewNodeMiddle(nextKey, n.ChildR) |
|
|
|
} |
|
|
|
if err != nil { |
|
|
@ -449,7 +459,8 @@ func (mt *MerkleTree) updateNode(tx db.Tx, n *Node) (*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.
|
|
|
|
if !cryptoUtils.CheckBigIntInField(k) { |
|
|
|
return nil, nil, nil, errors.New("Key not inside the Finite Field") |
|
|
|
return nil, nil, nil, |
|
|
|
errors.New("Key not inside the Finite Field") |
|
|
|
} |
|
|
|
|
|
|
|
kHash := NewHashFromBigInt(k) |
|
|
@ -468,9 +479,8 @@ func (mt *MerkleTree) Get(k *big.Int) (*big.Int, *big.Int, []*Hash, error) { |
|
|
|
case NodeTypeLeaf: |
|
|
|
if bytes.Equal(kHash[:], n.Entry[0][:]) { |
|
|
|
return n.Entry[0].BigInt(), n.Entry[1].BigInt(), siblings, nil |
|
|
|
} else { |
|
|
|
return n.Entry[0].BigInt(), n.Entry[1].BigInt(), siblings, ErrKeyNotFound |
|
|
|
} |
|
|
|
return n.Entry[0].BigInt(), n.Entry[1].BigInt(), siblings, ErrKeyNotFound |
|
|
|
case NodeTypeMiddle: |
|
|
|
if path[i] { |
|
|
|
nextKey = n.ChildR |
|
|
@ -541,7 +551,8 @@ func (mt *MerkleTree) Update(k, v *big.Int) (*CircomProcessorProof, error) { |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
newRootKey, err := mt.recalculatePathUntilRoot(tx, path, newNodeLeaf, siblings) |
|
|
|
newRootKey, err := |
|
|
|
mt.recalculatePathUntilRoot(tx, path, newNodeLeaf, siblings) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
@ -555,9 +566,8 @@ func (mt *MerkleTree) Update(k, v *big.Int) (*CircomProcessorProof, error) { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
return &cp, nil |
|
|
|
} else { |
|
|
|
return nil, ErrKeyNotFound |
|
|
|
} |
|
|
|
return nil, ErrKeyNotFound |
|
|
|
case NodeTypeMiddle: |
|
|
|
if path[i] { |
|
|
|
nextKey = n.ChildR |
|
|
@ -619,9 +629,8 @@ func (mt *MerkleTree) Delete(k *big.Int) error { |
|
|
|
// remove and go up with the sibling
|
|
|
|
err = mt.rmAndUpload(tx, path, kHash, siblings) |
|
|
|
return err |
|
|
|
} else { |
|
|
|
return ErrKeyNotFound |
|
|
|
} |
|
|
|
return ErrKeyNotFound |
|
|
|
case NodeTypeMiddle: |
|
|
|
if path[i] { |
|
|
|
nextKey = n.ChildR |
|
|
@ -638,7 +647,8 @@ func (mt *MerkleTree) Delete(k *big.Int) error { |
|
|
|
return ErrKeyNotFound |
|
|
|
} |
|
|
|
|
|
|
|
// rmAndUpload removes the key, and goes up until the root updating all the nodes with the new values.
|
|
|
|
// rmAndUpload removes the key, and goes up until the root updating all the
|
|
|
|
// nodes with the new values.
|
|
|
|
func (mt *MerkleTree) rmAndUpload(tx db.Tx, path []bool, kHash *Hash, siblings []*Hash) error { |
|
|
|
if len(siblings) == 0 { |
|
|
|
mt.rootKey = &HashZero |
|
|
@ -671,7 +681,8 @@ func (mt *MerkleTree) rmAndUpload(tx db.Tx, path []bool, kHash *Hash, siblings [ |
|
|
|
return err |
|
|
|
} |
|
|
|
// go up until the root
|
|
|
|
newRootKey, err := mt.recalculatePathUntilRoot(tx, path, newNode, siblings[:i]) |
|
|
|
newRootKey, err := mt.recalculatePathUntilRoot(tx, path, newNode, |
|
|
|
siblings[:i]) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
@ -682,7 +693,8 @@ func (mt *MerkleTree) rmAndUpload(tx db.Tx, path []bool, kHash *Hash, siblings [ |
|
|
|
} |
|
|
|
break |
|
|
|
} |
|
|
|
// if i==0 (root position), stop and store the sibling of the deleted leaf as root
|
|
|
|
// if i==0 (root position), stop and store the sibling of the
|
|
|
|
// deleted leaf as root
|
|
|
|
if i == 0 { |
|
|
|
mt.rootKey = toUpload |
|
|
|
err := mt.dbInsert(tx, dbKeyRootNode, DBEntryTypeRoot, mt.rootKey[:]) |
|
|
@ -700,7 +712,8 @@ func (mt *MerkleTree) rmAndUpload(tx db.Tx, path []bool, kHash *Hash, siblings [ |
|
|
|
} |
|
|
|
|
|
|
|
// recalculatePathUntilRoot recalculates the nodes until the Root
|
|
|
|
func (mt *MerkleTree) recalculatePathUntilRoot(tx db.Tx, path []bool, node *Node, siblings []*Hash) (*Hash, error) { |
|
|
|
func (mt *MerkleTree) recalculatePathUntilRoot(tx db.Tx, path []bool, node *Node, |
|
|
|
siblings []*Hash) (*Hash, error) { |
|
|
|
for i := len(siblings) - 1; i >= 0; i-- { |
|
|
|
nodeKey, err := node.Key() |
|
|
|
if err != nil { |
|
|
@ -757,9 +770,11 @@ type NodeAux struct { |
|
|
|
Value *Hash |
|
|
|
} |
|
|
|
|
|
|
|
// Proof defines the required elements for a MT proof of existence or non-existence.
|
|
|
|
// Proof defines the required elements for a MT proof of existence or
|
|
|
|
// non-existence.
|
|
|
|
type Proof struct { |
|
|
|
// existence indicates wether this is a proof of existence or non-existence.
|
|
|
|
// existence indicates wether this is a proof of existence or
|
|
|
|
// non-existence.
|
|
|
|
Existence bool |
|
|
|
// depth indicates how deep in the tree the proof goes.
|
|
|
|
depth uint |
|
|
@ -872,7 +887,8 @@ type CircomProcessorProof struct { |
|
|
|
NewKey *Hash `json:"newKey"` |
|
|
|
NewValue *Hash `json:"newValue"` |
|
|
|
IsOld0 bool `json:"isOld0"` |
|
|
|
Fnc int `json:"fnc"` // 0: NOP, 1: Update, 2: Insert, 3: Delete
|
|
|
|
// 0: NOP, 1: Update, 2: Insert, 3: Delete
|
|
|
|
Fnc int `json:"fnc"` |
|
|
|
} |
|
|
|
|
|
|
|
// String returns a human readable string representation of the
|
|
|
@ -912,7 +928,8 @@ type CircomVerifierProof struct { |
|
|
|
// GenerateCircomVerifierProof returns the CircomVerifierProof for a certain
|
|
|
|
// 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) { |
|
|
|
func (mt *MerkleTree) GenerateCircomVerifierProof(k *big.Int, |
|
|
|
rootKey *Hash) (*CircomVerifierProof, error) { |
|
|
|
if rootKey == nil { |
|
|
|
rootKey = mt.Root() |
|
|
|
} |
|
|
@ -939,7 +956,8 @@ func (mt *MerkleTree) GenerateCircomVerifierProof(k *big.Int, rootKey *Hash) (*C |
|
|
|
// GenerateProof generates the proof of existence (or non-existence) of an
|
|
|
|
// Entry's hash Index for a Merkle Tree given the root.
|
|
|
|
// If the rootKey is nil, the current merkletree root is used
|
|
|
|
func (mt *MerkleTree) GenerateProof(k *big.Int, rootKey *Hash) (*Proof, *big.Int, error) { |
|
|
|
func (mt *MerkleTree) GenerateProof(k *big.Int, rootKey *Hash) (*Proof, |
|
|
|
*big.Int, error) { |
|
|
|
p := &Proof{} |
|
|
|
var siblingKey *Hash |
|
|
|
|
|
|
@ -961,11 +979,10 @@ func (mt *MerkleTree) GenerateProof(k *big.Int, rootKey *Hash) (*Proof, *big.Int |
|
|
|
if bytes.Equal(kHash[:], n.Entry[0][:]) { |
|
|
|
p.Existence = true |
|
|
|
return p, n.Entry[1].BigInt(), nil |
|
|
|
} else { |
|
|
|
// We found a leaf whose entry didn't match hIndex
|
|
|
|
p.NodeAux = &NodeAux{Key: n.Entry[0], Value: n.Entry[1]} |
|
|
|
return p, n.Entry[1].BigInt(), nil |
|
|
|
} |
|
|
|
// We found a leaf whose entry didn't match hIndex
|
|
|
|
p.NodeAux = &NodeAux{Key: n.Entry[0], Value: n.Entry[1]} |
|
|
|
return p, n.Entry[1].BigInt(), nil |
|
|
|
case NodeTypeMiddle: |
|
|
|
if path[p.depth] { |
|
|
|
nextKey = n.ChildR |
|
|
@ -1013,7 +1030,8 @@ func RootFromProof(proof *Proof, k, v *big.Int) (*Hash, error) { |
|
|
|
midKey = &HashZero |
|
|
|
} else { |
|
|
|
if bytes.Equal(kHash[:], proof.NodeAux.Key[:]) { |
|
|
|
return nil, fmt.Errorf("Non-existence proof being checked against hIndex equal to nodeAux") |
|
|
|
return nil, |
|
|
|
fmt.Errorf("Non-existence proof being checked against hIndex equal to nodeAux") |
|
|
|
} |
|
|
|
midKey, err = LeafKey(proof.NodeAux.Key, proof.NodeAux.Value) |
|
|
|
if err != nil { |
|
|
@ -1071,10 +1089,10 @@ func (mt *MerkleTree) walk(key *Hash, f func(*Node)) error { |
|
|
|
} |
|
|
|
|
|
|
|
// Walk iterates over all the branches of a MerkleTree with the given rootKey
|
|
|
|
// if rootKey is nil, it will get the current RootKey of the current state of the MerkleTree.
|
|
|
|
// For each node, it calls the f function given in the parameters.
|
|
|
|
// See some examples of the Walk function usage in the merkletree.go and
|
|
|
|
// merkletree_test.go
|
|
|
|
// if rootKey is nil, it will get the current RootKey of the current state of
|
|
|
|
// the MerkleTree. For each node, it calls the f function given in the
|
|
|
|
// parameters. See some examples of the Walk function usage in the
|
|
|
|
// merkletree.go and merkletree_test.go
|
|
|
|
func (mt *MerkleTree) Walk(rootKey *Hash, f func(*Node)) error { |
|
|
|
if rootKey == nil { |
|
|
|
rootKey = mt.Root() |
|
|
@ -1083,8 +1101,8 @@ func (mt *MerkleTree) Walk(rootKey *Hash, f func(*Node)) error { |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
// GraphViz uses Walk function to generate a string GraphViz representation of the
|
|
|
|
// tree and writes it to w
|
|
|
|
// GraphViz uses Walk function to generate a string GraphViz representation of
|
|
|
|
// the tree and writes it to w
|
|
|
|
func (mt *MerkleTree) GraphViz(w io.Writer, rootKey *Hash) error { |
|
|
|
fmt.Fprintf(w, `digraph hierarchy { |
|
|
|
node [fontname=Monospace,fontsize=10,shape=box] |
|
|
@ -1128,12 +1146,14 @@ func (mt *MerkleTree) PrintGraphViz(rootKey *Hash) error { |
|
|
|
rootKey = mt.Root() |
|
|
|
} |
|
|
|
w := bytes.NewBufferString("") |
|
|
|
fmt.Fprintf(w, "--------\nGraphViz of the MerkleTree with RootKey "+rootKey.BigInt().String()+"\n") |
|
|
|
fmt.Fprintf(w, |
|
|
|
"--------\nGraphViz of the MerkleTree with RootKey "+rootKey.BigInt().String()+"\n") |
|
|
|
err := mt.GraphViz(w, nil) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
fmt.Fprintf(w, "End of GraphViz of the MerkleTree with RootKey "+rootKey.BigInt().String()+"\n--------\n") |
|
|
|
fmt.Fprintf(w, |
|
|
|
"End of GraphViz of the MerkleTree with RootKey "+rootKey.BigInt().String()+"\n--------\n") |
|
|
|
|
|
|
|
fmt.Println(w) |
|
|
|
return nil |
|
|
|