mirror of
https://github.com/vocdoni/arbo.git
synced 2026-01-09 13:57:54 -05:00
named returns
This commit is contained in:
19
tree.go
19
tree.go
@@ -927,13 +927,17 @@ func (t *Tree) UpdateWithTx(wTx db.WriteTx, k, v []byte) error {
|
|||||||
// GenProof generates a MerkleTree proof for the given key. The leaf value is
|
// GenProof generates a MerkleTree proof for the given key. The leaf value is
|
||||||
// returned, together with the packed siblings of the proof, and a boolean
|
// returned, together with the packed siblings of the proof, and a boolean
|
||||||
// parameter that indicates if the proof is of existence (true) or not (false).
|
// parameter that indicates if the proof is of existence (true) or not (false).
|
||||||
func (t *Tree) GenProof(k []byte) ([]byte, []byte, []byte, bool, error) {
|
func (t *Tree) GenProof(k []byte) (
|
||||||
|
leafKey []byte, leafValue []byte, siblings []byte, existence bool, err error,
|
||||||
|
) {
|
||||||
return t.GenProofWithTx(t.treedb, k)
|
return t.GenProofWithTx(t.treedb, k)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenProofWithTx does the same than the GenProof method, but allowing to pass
|
// GenProofWithTx does the same than the GenProof method, but allowing to pass
|
||||||
// the db.ReadTx that is used.
|
// the db.ReadTx that is used.
|
||||||
func (t *Tree) GenProofWithTx(rTx db.Reader, k []byte) ([]byte, []byte, []byte, bool, error) {
|
func (t *Tree) GenProofWithTx(rTx db.Reader, k []byte) (
|
||||||
|
leafKey []byte, leafValue []byte, siblings []byte, existence bool, err error,
|
||||||
|
) {
|
||||||
keyPath, err := keyPathFromKey(t.maxLevels, k)
|
keyPath, err := keyPathFromKey(t.maxLevels, k)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, nil, false, err
|
return nil, nil, nil, false, err
|
||||||
@@ -946,24 +950,23 @@ func (t *Tree) GenProofWithTx(rTx db.Reader, k []byte) ([]byte, []byte, []byte,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// go down to the leaf
|
// go down to the leaf
|
||||||
var siblings [][]byte
|
var unpacked [][]byte
|
||||||
_, value, siblings, err := t.down(rTx, k, root, siblings, path, 0, true)
|
_, value, unpacked, err := t.down(rTx, k, root, unpacked, path, 0, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, nil, false, err
|
return nil, nil, nil, false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
s, err := PackSiblings(t.hashFunction, siblings)
|
if siblings, err = PackSiblings(t.hashFunction, unpacked); err != nil {
|
||||||
if err != nil {
|
|
||||||
return nil, nil, nil, false, err
|
return nil, nil, nil, false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
leafK, leafV := ReadLeafValue(value)
|
leafK, leafV := ReadLeafValue(value)
|
||||||
if !bytes.Equal(k, leafK) {
|
if !bytes.Equal(k, leafK) {
|
||||||
// key not in tree, proof of non-existence
|
// key not in tree, proof of non-existence
|
||||||
return leafK, leafV, s, false, nil
|
return leafK, leafV, siblings, false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return leafK, leafV, s, true, nil
|
return leafK, leafV, siblings, true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// PackSiblings packs the siblings into a byte array.
|
// PackSiblings packs the siblings into a byte array.
|
||||||
|
|||||||
21
tree_big.go
21
tree_big.go
@@ -112,7 +112,9 @@ func (t *Tree) UpdateBigInt(key *big.Int, bigints ...*big.Int) error {
|
|||||||
// node as a slice of big.Ints. It encodes the key as bytes and gets the leaf
|
// node as a slice of big.Ints. It encodes the key as bytes and gets the leaf
|
||||||
// node from the tree, then it decodes the serialized bigints of the leaf node and
|
// node from the tree, then it decodes the serialized bigints of the leaf node and
|
||||||
// returns the key and the values or an error if something fails.
|
// returns the key and the values or an error if something fails.
|
||||||
func (t *Tree) GetBigInt(k *big.Int) (*big.Int, []*big.Int, error) {
|
func (t *Tree) GetBigInt(k *big.Int) (
|
||||||
|
key *big.Int, bigints []*big.Int, err error,
|
||||||
|
) {
|
||||||
// acquire lock to wait for atomic updates to treedb and valuesdb to finish
|
// acquire lock to wait for atomic updates to treedb and valuesdb to finish
|
||||||
t.valuesdbMu.RLock()
|
t.valuesdbMu.RLock()
|
||||||
defer t.valuesdbMu.RUnlock()
|
defer t.valuesdbMu.RUnlock()
|
||||||
@@ -135,12 +137,13 @@ func (t *Tree) GetBigInt(k *big.Int) (*big.Int, []*big.Int, error) {
|
|||||||
// big.Int key into bytes and generates a proof for the key, then it returns
|
// big.Int key into bytes and generates a proof for the key, then it returns
|
||||||
// the key, the value of the leaf node, the siblings and a boolean indicating
|
// the key, the value of the leaf node, the siblings and a boolean indicating
|
||||||
// if the key exists or an error if something fails.
|
// if the key exists or an error if something fails.
|
||||||
func (t *Tree) GenProofBigInts(k *big.Int) ([]byte, []byte, []byte, bool, error) {
|
func (t *Tree) GenProofBigInts(key *big.Int) (
|
||||||
if k == nil {
|
leafKey []byte, leafValue []byte, siblings []byte, existence bool, err error,
|
||||||
|
) {
|
||||||
|
if key == nil {
|
||||||
return nil, nil, nil, false, fmt.Errorf("key cannot be nil")
|
return nil, nil, nil, false, fmt.Errorf("key cannot be nil")
|
||||||
}
|
}
|
||||||
|
bk := bigIntToLeafKey(key, t.MaxKeyLen())
|
||||||
bk := bigIntToLeafKey(k, t.MaxKeyLen())
|
|
||||||
return t.GenProof(bk)
|
return t.GenProof(bk)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -172,9 +175,11 @@ func (t *Tree) GenerateGnarkVerifierProofBigInt(k *big.Int) (*GnarkVerifierProof
|
|||||||
// into a big.Int key and a slice of big.Int values, it gets the serialized bigints
|
// into a big.Int key and a slice of big.Int values, it gets the serialized bigints
|
||||||
// from the valuesdb and checks if it matches the value of the leaf node. It
|
// from the valuesdb and checks if it matches the value of the leaf node. It
|
||||||
// returns the original key and values or an error if the values don't match.
|
// returns the original key and values or an error if the values don't match.
|
||||||
func (t *Tree) leafToBigInts(key, value, serializedBigInts []byte) (*big.Int, []*big.Int, error) {
|
func (t *Tree) leafToBigInts(bkey, value, serializedBigInts []byte) (
|
||||||
|
key *big.Int, bigints []*big.Int, err error,
|
||||||
|
) {
|
||||||
// reverse the process of bigints encoding
|
// reverse the process of bigints encoding
|
||||||
bigints := deserializeBigInts(serializedBigInts)
|
bigints = deserializeBigInts(serializedBigInts)
|
||||||
// reencode the leaf value of the tree to check if it matches the value
|
// reencode the leaf value of the tree to check if it matches the value
|
||||||
bigintsHash, err := HashBigInts(t.HashFunction(), bigints...)
|
bigintsHash, err := HashBigInts(t.HashFunction(), bigints...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -186,7 +191,7 @@ func (t *Tree) leafToBigInts(key, value, serializedBigInts []byte) (*big.Int, []
|
|||||||
return nil, nil, fmt.Errorf("LeafToBigInt: bigintsHash != value")
|
return nil, nil, fmt.Errorf("LeafToBigInt: bigintsHash != value")
|
||||||
}
|
}
|
||||||
// convert the bytes of the key to a big.Int
|
// convert the bytes of the key to a big.Int
|
||||||
return leafKeyToBigInt(key), bigints, nil
|
return leafKeyToBigInt(bkey), bigints, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// leafKeyToBigInt converts the bytes of a key into a big.Int.
|
// leafKeyToBigInt converts the bytes of a key into a big.Int.
|
||||||
|
|||||||
Reference in New Issue
Block a user