|
|
@ -20,6 +20,8 @@ const ( |
|
|
|
proofFlagsLen = 2 |
|
|
|
// ElemBytesLen is the length of the Hash byte array
|
|
|
|
ElemBytesLen = 32 |
|
|
|
|
|
|
|
numCharPrint = 8 |
|
|
|
) |
|
|
|
|
|
|
|
var ( |
|
|
@ -60,10 +62,10 @@ type Hash [32]byte |
|
|
|
// String returns decimal representation in string format of the Hash
|
|
|
|
func (h Hash) String() string { |
|
|
|
s := h.BigInt().String() |
|
|
|
if len(s) < 8 { |
|
|
|
if len(s) < numCharPrint { |
|
|
|
return s |
|
|
|
} |
|
|
|
return s[0:8] + "..." |
|
|
|
return s[0:numCharPrint] + "..." |
|
|
|
} |
|
|
|
|
|
|
|
// Hex returns the hexadecimal representation of the Hash
|
|
|
@ -93,10 +95,10 @@ func (h *Hash) Bytes() []byte { |
|
|
|
// from a byte array that previously has ben generated by the Hash.Bytes()
|
|
|
|
// method.
|
|
|
|
func NewBigIntFromBytes(b []byte) (*big.Int, error) { |
|
|
|
if len(b) != 32 { |
|
|
|
if len(b) != ElemBytesLen { |
|
|
|
return nil, fmt.Errorf("Expected 32 bytes, found %d bytes", len(b)) |
|
|
|
} |
|
|
|
return new(big.Int).SetBytes(common.SwapEndianness(b[:32])), nil |
|
|
|
return new(big.Int).SetBytes(common.SwapEndianness(b[:ElemBytesLen])), nil |
|
|
|
} |
|
|
|
|
|
|
|
// NewHashFromBigInt returns a *Hash representation of the given *big.Int
|
|
|
@ -110,7 +112,7 @@ func NewHashFromBigInt(b *big.Int) *Hash { |
|
|
|
// in the process. This is the intended method to get a *Hash from a byte array
|
|
|
|
// that previously has ben generated by the Hash.Bytes() method.
|
|
|
|
func NewHashFromBytes(b []byte) (*Hash, error) { |
|
|
|
if len(b) != 32 { |
|
|
|
if len(b) != ElemBytesLen { |
|
|
|
return nil, fmt.Errorf("Expected 32 bytes, found %d bytes", len(b)) |
|
|
|
} |
|
|
|
var h Hash |
|
|
@ -224,7 +226,7 @@ func (mt *MerkleTree) AddAndGetCircomProof(k, v *big.Int) (*CircomProcessorProof |
|
|
|
var cp CircomProcessorProof |
|
|
|
cp.Fnc = 2 |
|
|
|
cp.OldRoot = mt.rootKey |
|
|
|
gettedK, gettedV, siblings, err := mt.Get(k) |
|
|
|
gettedK, gettedV, _, err := mt.Get(k) |
|
|
|
if err != nil && err != ErrKeyNotFound { |
|
|
|
return nil, err |
|
|
|
} |
|
|
@ -233,7 +235,7 @@ func (mt *MerkleTree) AddAndGetCircomProof(k, v *big.Int) (*CircomProcessorProof |
|
|
|
if bytes.Equal(cp.OldKey[:], HashZero[:]) { |
|
|
|
cp.IsOld0 = true |
|
|
|
} |
|
|
|
_, _, siblings, err = mt.Get(k) |
|
|
|
_, _, siblings, err := mt.Get(k) |
|
|
|
if err != nil && err != ErrKeyNotFound { |
|
|
|
return nil, err |
|
|
|
} |
|
|
@ -583,12 +585,12 @@ func (mt *MerkleTree) rmAndUpload(tx db.Tx, path []bool, kHash *Hash, siblings [ |
|
|
|
} |
|
|
|
|
|
|
|
toUpload := siblings[len(siblings)-1] |
|
|
|
if len(siblings) < 2 { |
|
|
|
if len(siblings) < 2 { //nolint:gomnd
|
|
|
|
mt.rootKey = siblings[0] |
|
|
|
mt.dbInsert(tx, rootNodeValue, DBEntryTypeRoot, mt.rootKey[:]) |
|
|
|
return tx.Commit() |
|
|
|
} |
|
|
|
for i := len(siblings) - 2; i >= 0; i-- { |
|
|
|
for i := len(siblings) - 2; i >= 0; i-- { //nolint:gomnd
|
|
|
|
if !bytes.Equal(siblings[i][:], HashZero[:]) { |
|
|
|
var newNode *Node |
|
|
|
if path[i] { |
|
|
@ -646,27 +648,6 @@ func (mt *MerkleTree) recalculatePathUntilRoot(tx db.Tx, path []bool, node *Node |
|
|
|
return nodeKey, err |
|
|
|
} |
|
|
|
|
|
|
|
// dbGet is a helper function to get the node of a key from the internal
|
|
|
|
// storage.
|
|
|
|
func (mt *MerkleTree) dbGet(k []byte) (NodeType, []byte, error) { |
|
|
|
if bytes.Equal(k, HashZero[:]) { |
|
|
|
return 0, nil, nil |
|
|
|
} |
|
|
|
|
|
|
|
value, err := mt.db.Get(k) |
|
|
|
if err != nil { |
|
|
|
return 0, nil, err |
|
|
|
} |
|
|
|
|
|
|
|
if len(value) < 2 { |
|
|
|
return 0, nil, ErrInvalidDBValue |
|
|
|
} |
|
|
|
nodeType := value[0] |
|
|
|
nodeBytes := value[1:] |
|
|
|
|
|
|
|
return NodeType(nodeType), nodeBytes, nil |
|
|
|
} |
|
|
|
|
|
|
|
// dbInsert is a helper function to insert a node into a key in an open db
|
|
|
|
// transaction.
|
|
|
|
func (mt *MerkleTree) dbInsert(tx db.Tx, k []byte, t NodeType, data []byte) { |
|
|
@ -756,7 +737,7 @@ func NewProofFromBytes(bs []byte) (*Proof, error) { |
|
|
|
func (p *Proof) Bytes() []byte { |
|
|
|
bsLen := proofFlagsLen + len(p.notempties) + ElemBytesLen*len(p.Siblings) |
|
|
|
if p.NodeAux != nil { |
|
|
|
bsLen += 2 * ElemBytesLen |
|
|
|
bsLen += 2 * ElemBytesLen //nolint:gomnd
|
|
|
|
} |
|
|
|
bs := make([]byte, bsLen) |
|
|
|
|
|
|
|