arnaucube
arnaucube/go-merkletree-iden3Public Self-hosted repository
Update golint warnings
arnaucube root@arnaucube.com
Tue, 30 Jun 2020 22:44:51 +0200
commit

28d9e9badbaca0ba2fd6618a7782c7866a29b6b8

parent

cc927fae93d8be9847d394f0447e5291b08aa550

4 files changed, 15 insertions(+), 5 deletions(-)

jump to
M README.md → README.md

@@ -1,4 +1,5 @@

-# go-merkletree +# go-merkletree [![Go Report Card](https://goreportcard.com/badge/github.com/iden3/go-merkletree)](https://goreportcard.com/report/github.com/iden3/go-merkletree) + MerkleTree compatible with version from [circomlib](https://github.com/iden3/circomlib). Adaptation of the merkletree from https://github.com/iden3/go-iden3-core/tree/v0.0.8
M merkletree.go → merkletree.go

@@ -46,26 +46,31 @@ // exists in the tree.

ErrEntryIndexAlreadyExists = errors.New("the entry index already exists in the tree") // ErrNotWritable is used when the MerkleTree is not writable and a write function is called ErrNotWritable = errors.New("Merkle Tree not writable") - - rootNodeValue = []byte("currentroot") - 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} + rootNodeValue = []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} ) +// Hash is the generic type stored in the MerkleTree type Hash [32]byte func (h Hash) String() string { return new(big.Int).SetBytes(h[:]).String() } + +// BigInt returns the *big.Int representation of the *Hash func (h *Hash) BigInt() *big.Int { return new(big.Int).SetBytes(common.SwapEndianness(h[:])) } +// NewHashFromBigInt returns a *Hash representation of the given *big.Int func NewHashFromBigInt(b *big.Int) *Hash { r := &Hash{} copy(r[:], common.SwapEndianness(b.Bytes())) return r } +// MerkleTree is the struct with the main elements of the MerkleTree type MerkleTree struct { sync.RWMutex db db.Storage

@@ -74,6 +79,7 @@ writable bool

maxLevels int } +// NewMerkleTree loads a new Merkletree. If in the sotrage already exists one will open that one, if not, will create a new one. func NewMerkleTree(storage db.Storage, maxLevels int) (*MerkleTree, error) { mt := MerkleTree{db: storage, maxLevels: maxLevels, writable: true}

@@ -96,10 +102,12 @@ copy(mt.rootKey[:], v)

return &mt, nil } +// Root returns the MerkleRoot func (mt *MerkleTree) Root() *Hash { return mt.rootKey } +// Add adds a Key & Value into the MerkleTree. Where the `k` determines the path from the Root to the Leaf. func (mt *MerkleTree) Add(k, v *big.Int) error { // verify that the MerkleTree is writable if !mt.writable {
M node.go → node.go

@@ -69,7 +69,7 @@ case NodeTypeLeaf:

if len(b) != 2*ElemBytesLen { return nil, ErrNodeDataBadSize } - n.Entry = [2]*Hash{&Hash{}, &Hash{}} + n.Entry = [2]*Hash{{}, {}} copy(n.Entry[0][:], b[0:32]) copy(n.Entry[1][:], b[32:64]) case NodeTypeEmpty:
M utils.go → utils.go

@@ -52,6 +52,7 @@ }

return NewHashFromBigInt(poseidonHash), nil } +// BigIntsToPoseidonInput takes *big.Ints and returns a fixed-length array of the size `poseidon.T` func BigIntsToPoseidonInput(bigints ...*big.Int) ([poseidon.T]*big.Int, error) { z := big.NewInt(0) b := [poseidon.T]*big.Int{z, z, z, z, z, z}