Browse Source

Update golint warnings

circomproofs
arnaucube 4 years ago
parent
commit
28d9e9badb
4 changed files with 15 additions and 5 deletions
  1. +2
    -1
      README.md
  2. +11
    -3
      merkletree.go
  3. +1
    -1
      node.go
  4. +1
    -0
      utils.go

+ 2
- 1
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). 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 Adaptation of the merkletree from https://github.com/iden3/go-iden3-core/tree/v0.0.8

+ 11
- 3
merkletree.go

@ -46,26 +46,31 @@ var (
ErrEntryIndexAlreadyExists = errors.New("the entry index already 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 is used when the MerkleTree is not writable and a write function is called
ErrNotWritable = errors.New("Merkle Tree not writable") 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 type Hash [32]byte
func (h Hash) String() string { func (h Hash) String() string {
return new(big.Int).SetBytes(h[:]).String() return new(big.Int).SetBytes(h[:]).String()
} }
// BigInt returns the *big.Int representation of the *Hash
func (h *Hash) BigInt() *big.Int { func (h *Hash) BigInt() *big.Int {
return new(big.Int).SetBytes(common.SwapEndianness(h[:])) return new(big.Int).SetBytes(common.SwapEndianness(h[:]))
} }
// NewHashFromBigInt returns a *Hash representation of the given *big.Int
func NewHashFromBigInt(b *big.Int) *Hash { func NewHashFromBigInt(b *big.Int) *Hash {
r := &Hash{} r := &Hash{}
copy(r[:], common.SwapEndianness(b.Bytes())) copy(r[:], common.SwapEndianness(b.Bytes()))
return r return r
} }
// MerkleTree is the struct with the main elements of the MerkleTree
type MerkleTree struct { type MerkleTree struct {
sync.RWMutex sync.RWMutex
db db.Storage db db.Storage
@ -74,6 +79,7 @@ type MerkleTree struct {
maxLevels int 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) { func NewMerkleTree(storage db.Storage, maxLevels int) (*MerkleTree, error) {
mt := MerkleTree{db: storage, maxLevels: maxLevels, writable: true} mt := MerkleTree{db: storage, maxLevels: maxLevels, writable: true}
@ -96,10 +102,12 @@ func NewMerkleTree(storage db.Storage, maxLevels int) (*MerkleTree, error) {
return &mt, nil return &mt, nil
} }
// Root returns the MerkleRoot
func (mt *MerkleTree) Root() *Hash { func (mt *MerkleTree) Root() *Hash {
return mt.rootKey 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 { func (mt *MerkleTree) Add(k, v *big.Int) error {
// verify that the MerkleTree is writable // verify that the MerkleTree is writable
if !mt.writable { if !mt.writable {

+ 1
- 1
node.go

@ -69,7 +69,7 @@ func NewNodeFromBytes(b []byte) (*Node, error) {
if len(b) != 2*ElemBytesLen { if len(b) != 2*ElemBytesLen {
return nil, ErrNodeDataBadSize return nil, ErrNodeDataBadSize
} }
n.Entry = [2]*Hash{&Hash{}, &Hash{}}
n.Entry = [2]*Hash{{}, {}}
copy(n.Entry[0][:], b[0:32]) copy(n.Entry[0][:], b[0:32])
copy(n.Entry[1][:], b[32:64]) copy(n.Entry[1][:], b[32:64])
case NodeTypeEmpty: case NodeTypeEmpty:

+ 1
- 0
utils.go

@ -52,6 +52,7 @@ func HashElemsKey(key *big.Int, elems ...*big.Int) (*Hash, error) {
return NewHashFromBigInt(poseidonHash), nil 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) { func BigIntsToPoseidonInput(bigints ...*big.Int) ([poseidon.T]*big.Int, error) {
z := big.NewInt(0) z := big.NewInt(0)
b := [poseidon.T]*big.Int{z, z, z, z, z, z} b := [poseidon.T]*big.Int{z, z, z, z, z, z}

Loading…
Cancel
Save