|
@ -54,7 +54,8 @@ var ( |
|
|
|
|
|
|
|
|
dbKeyRootNode = []byte("currentroot") |
|
|
dbKeyRootNode = []byte("currentroot") |
|
|
// HashZero is used at Empty nodes
|
|
|
// 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
|
|
|
// Hash is the generic type stored in the MerkleTree
|
|
@ -114,11 +115,13 @@ func (h *Hash) Bytes() []byte { |
|
|
// method.
|
|
|
// method.
|
|
|
func NewBigIntFromHashBytes(b []byte) (*big.Int, error) { |
|
|
func NewBigIntFromHashBytes(b []byte) (*big.Int, error) { |
|
|
if len(b) != ElemBytesLen { |
|
|
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]) |
|
|
bi := new(big.Int).SetBytes(b[:ElemBytesLen]) |
|
|
if !cryptoUtils.CheckBigIntInField(bi) { |
|
|
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 |
|
|
return bi, nil |
|
|
} |
|
|
} |
|
@ -135,7 +138,8 @@ func NewHashFromBigInt(b *big.Int) *Hash { |
|
|
// that previously has ben generated by the Hash.Bytes() method.
|
|
|
// that previously has ben generated by the Hash.Bytes() method.
|
|
|
func NewHashFromBytes(b []byte) (*Hash, error) { |
|
|
func NewHashFromBytes(b []byte) (*Hash, error) { |
|
|
if len(b) != ElemBytesLen { |
|
|
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 |
|
|
var h Hash |
|
|
copy(h[:], common.SwapEndianness(b)) |
|
|
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
|
|
|
// 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 |
|
|
var cp CircomProcessorProof |
|
|
cp.Fnc = 2 |
|
|
cp.Fnc = 2 |
|
|
cp.OldRoot = mt.rootKey |
|
|
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
|
|
|
// pushLeaf recursively pushes an existing oldLeaf down until its path diverges
|
|
|
// from newLeaf, at which point both leafs are stored, all while updating the
|
|
|
// from newLeaf, at which point both leafs are stored, all while updating the
|
|
|
// path.
|
|
|
// 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 { |
|
|
if lvl > mt.maxLevels-2 { |
|
|
return nil, ErrReachedMaxLevel |
|
|
return nil, ErrReachedMaxLevel |
|
|
} |
|
|
} |
|
|
var newNodeMiddle *Node |
|
|
var newNodeMiddle *Node |
|
|
if pathNewLeaf[lvl] == pathOldLeaf[lvl] { // We need to go deeper!
|
|
|
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 { |
|
|
if err != nil { |
|
|
return nil, err |
|
|
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) |
|
|
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.
|
|
|
// 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) |
|
|
return mt.addNode(tx, newLeaf) |
|
|
case NodeTypeLeaf: |
|
|
case NodeTypeLeaf: |
|
|
nKey := n.Entry[0] |
|
|
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] |
|
|
newLeafKey := newLeaf.Entry[0] |
|
|
if bytes.Equal(nKey[:], newLeafKey[:]) { |
|
|
if bytes.Equal(nKey[:], newLeafKey[:]) { |
|
|
return nil, ErrEntryIndexAlreadyExists |
|
|
return nil, ErrEntryIndexAlreadyExists |
|
|
} |
|
|
} |
|
|
pathOldLeaf := getPath(mt.maxLevels, nKey[:]) |
|
|
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) |
|
|
return mt.pushLeaf(tx, newLeaf, n, lvl, path, pathOldLeaf) |
|
|
case NodeTypeMiddle: |
|
|
case NodeTypeMiddle: |
|
|
// We need to go deeper, continue traversing the tree, left or
|
|
|
// We need to go deeper, continue traversing the tree, left or
|
|
|
// right depending on path
|
|
|
// right depending on path
|
|
|
var newNodeMiddle *Node |
|
|
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) |
|
|
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) |
|
|
newNodeMiddle = NewNodeMiddle(nextKey, n.ChildR) |
|
|
} |
|
|
} |
|
|
if err != nil { |
|
|
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) { |
|
|
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.
|
|
|
if !cryptoUtils.CheckBigIntInField(k) { |
|
|
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) |
|
|
kHash := NewHashFromBigInt(k) |
|
@ -468,9 +479,8 @@ func (mt *MerkleTree) Get(k *big.Int) (*big.Int, *big.Int, []*Hash, error) { |
|
|
case NodeTypeLeaf: |
|
|
case NodeTypeLeaf: |
|
|
if bytes.Equal(kHash[:], n.Entry[0][:]) { |
|
|
if bytes.Equal(kHash[:], n.Entry[0][:]) { |
|
|
return n.Entry[0].BigInt(), n.Entry[1].BigInt(), siblings, nil |
|
|
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: |
|
|
case NodeTypeMiddle: |
|
|
if path[i] { |
|
|
if path[i] { |
|
|
nextKey = n.ChildR |
|
|
nextKey = n.ChildR |
|
@ -541,7 +551,8 @@ func (mt *MerkleTree) Update(k, v *big.Int) (*CircomProcessorProof, error) { |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return nil, err |
|
|
return nil, err |
|
|
} |
|
|
} |
|
|
newRootKey, err := mt.recalculatePathUntilRoot(tx, path, newNodeLeaf, siblings) |
|
|
|
|
|
|
|
|
newRootKey, err := |
|
|
|
|
|
mt.recalculatePathUntilRoot(tx, path, newNodeLeaf, siblings) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return nil, err |
|
|
return nil, err |
|
|
} |
|
|
} |
|
@ -555,9 +566,8 @@ func (mt *MerkleTree) Update(k, v *big.Int) (*CircomProcessorProof, error) { |
|
|
return nil, err |
|
|
return nil, err |
|
|
} |
|
|
} |
|
|
return &cp, nil |
|
|
return &cp, nil |
|
|
} else { |
|
|
|
|
|
return nil, ErrKeyNotFound |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
return nil, ErrKeyNotFound |
|
|
case NodeTypeMiddle: |
|
|
case NodeTypeMiddle: |
|
|
if path[i] { |
|
|
if path[i] { |
|
|
nextKey = n.ChildR |
|
|
nextKey = n.ChildR |
|
@ -619,9 +629,8 @@ func (mt *MerkleTree) Delete(k *big.Int) error { |
|
|
// remove and go up with the sibling
|
|
|
// remove and go up with the sibling
|
|
|
err = mt.rmAndUpload(tx, path, kHash, siblings) |
|
|
err = mt.rmAndUpload(tx, path, kHash, siblings) |
|
|
return err |
|
|
return err |
|
|
} else { |
|
|
|
|
|
return ErrKeyNotFound |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
return ErrKeyNotFound |
|
|
case NodeTypeMiddle: |
|
|
case NodeTypeMiddle: |
|
|
if path[i] { |
|
|
if path[i] { |
|
|
nextKey = n.ChildR |
|
|
nextKey = n.ChildR |
|
@ -638,7 +647,8 @@ func (mt *MerkleTree) Delete(k *big.Int) error { |
|
|
return ErrKeyNotFound |
|
|
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 { |
|
|
func (mt *MerkleTree) rmAndUpload(tx db.Tx, path []bool, kHash *Hash, siblings []*Hash) error { |
|
|
if len(siblings) == 0 { |
|
|
if len(siblings) == 0 { |
|
|
mt.rootKey = &HashZero |
|
|
mt.rootKey = &HashZero |
|
@ -671,7 +681,8 @@ func (mt *MerkleTree) rmAndUpload(tx db.Tx, path []bool, kHash *Hash, siblings [ |
|
|
return err |
|
|
return err |
|
|
} |
|
|
} |
|
|
// go up until the root
|
|
|
// 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 { |
|
|
if err != nil { |
|
|
return err |
|
|
return err |
|
|
} |
|
|
} |
|
@ -682,7 +693,8 @@ func (mt *MerkleTree) rmAndUpload(tx db.Tx, path []bool, kHash *Hash, siblings [ |
|
|
} |
|
|
} |
|
|
break |
|
|
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 { |
|
|
if i == 0 { |
|
|
mt.rootKey = toUpload |
|
|
mt.rootKey = toUpload |
|
|
err := mt.dbInsert(tx, dbKeyRootNode, DBEntryTypeRoot, mt.rootKey[:]) |
|
|
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
|
|
|
// 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-- { |
|
|
for i := len(siblings) - 1; i >= 0; i-- { |
|
|
nodeKey, err := node.Key() |
|
|
nodeKey, err := node.Key() |
|
|
if err != nil { |
|
|
if err != nil { |
|
@ -757,9 +770,11 @@ type NodeAux struct { |
|
|
Value *Hash |
|
|
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 { |
|
|
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 |
|
|
Existence bool |
|
|
// depth indicates how deep in the tree the proof goes.
|
|
|
// depth indicates how deep in the tree the proof goes.
|
|
|
depth uint |
|
|
depth uint |
|
@ -872,7 +887,8 @@ type CircomProcessorProof struct { |
|
|
NewKey *Hash `json:"newKey"` |
|
|
NewKey *Hash `json:"newKey"` |
|
|
NewValue *Hash `json:"newValue"` |
|
|
NewValue *Hash `json:"newValue"` |
|
|
IsOld0 bool `json:"isOld0"` |
|
|
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
|
|
|
// String returns a human readable string representation of the
|
|
@ -912,7 +928,8 @@ type CircomVerifierProof struct { |
|
|
// GenerateCircomVerifierProof returns the CircomVerifierProof for a certain
|
|
|
// GenerateCircomVerifierProof returns the CircomVerifierProof for a certain
|
|
|
// key in the MerkleTree. If the rootKey is nil, the current merkletree root
|
|
|
// key in the MerkleTree. If the rootKey is nil, the current merkletree root
|
|
|
// 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) { |
|
|
if rootKey == nil { |
|
|
if rootKey == nil { |
|
|
rootKey = mt.Root() |
|
|
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
|
|
|
// GenerateProof generates the proof of existence (or non-existence) of an
|
|
|
// Entry's hash Index for a Merkle Tree given the root.
|
|
|
// Entry's hash Index for a Merkle Tree given the root.
|
|
|
// If the rootKey is nil, the current merkletree root is used
|
|
|
// 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{} |
|
|
p := &Proof{} |
|
|
var siblingKey *Hash |
|
|
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][:]) { |
|
|
if bytes.Equal(kHash[:], n.Entry[0][:]) { |
|
|
p.Existence = true |
|
|
p.Existence = true |
|
|
return p, n.Entry[1].BigInt(), nil |
|
|
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: |
|
|
case NodeTypeMiddle: |
|
|
if path[p.depth] { |
|
|
if path[p.depth] { |
|
|
nextKey = n.ChildR |
|
|
nextKey = n.ChildR |
|
@ -1013,7 +1030,8 @@ func RootFromProof(proof *Proof, k, v *big.Int) (*Hash, error) { |
|
|
midKey = &HashZero |
|
|
midKey = &HashZero |
|
|
} else { |
|
|
} else { |
|
|
if bytes.Equal(kHash[:], proof.NodeAux.Key[:]) { |
|
|
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) |
|
|
midKey, err = LeafKey(proof.NodeAux.Key, proof.NodeAux.Value) |
|
|
if err != nil { |
|
|
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
|
|
|
// 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 { |
|
|
func (mt *MerkleTree) Walk(rootKey *Hash, f func(*Node)) error { |
|
|
if rootKey == nil { |
|
|
if rootKey == nil { |
|
|
rootKey = mt.Root() |
|
|
rootKey = mt.Root() |
|
@ -1083,8 +1101,8 @@ func (mt *MerkleTree) Walk(rootKey *Hash, f func(*Node)) error { |
|
|
return err |
|
|
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 { |
|
|
func (mt *MerkleTree) GraphViz(w io.Writer, rootKey *Hash) error { |
|
|
fmt.Fprintf(w, `digraph hierarchy { |
|
|
fmt.Fprintf(w, `digraph hierarchy { |
|
|
node [fontname=Monospace,fontsize=10,shape=box] |
|
|
node [fontname=Monospace,fontsize=10,shape=box] |
|
@ -1128,12 +1146,14 @@ func (mt *MerkleTree) PrintGraphViz(rootKey *Hash) error { |
|
|
rootKey = mt.Root() |
|
|
rootKey = mt.Root() |
|
|
} |
|
|
} |
|
|
w := bytes.NewBufferString("") |
|
|
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) |
|
|
err := mt.GraphViz(w, nil) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return err |
|
|
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) |
|
|
fmt.Println(w) |
|
|
return nil |
|
|
return nil |
|
|