From 5c7c03eca137a793596c1bd5f9b978362d4fbbe5 Mon Sep 17 00:00:00 2001 From: arnaucube Date: Fri, 18 Dec 2020 11:58:02 +0100 Subject: [PATCH] Rm db.Info, rm go-iden3-core dependency Rm db.Info, rm go-iden3-core dependency, update version of go-iden3-crypto --- db/db.go | 1 - db/leveldb/leveldb.go | 38 ----------------------------------- db/memory/memory.go | 5 ----- db/pebble/pebble.go | 32 ------------------------------ go.mod | 3 +-- go.sum | 9 ++++----- merkletree.go | 46 ++++++++++++++++++------------------------- utils.go | 24 ++++++++++++++++++++++ 8 files changed, 48 insertions(+), 110 deletions(-) diff --git a/db/db.go b/db/db.go index 3d20511..f2093ff 100644 --- a/db/db.go +++ b/db/db.go @@ -19,7 +19,6 @@ type Storage interface { Get([]byte) ([]byte, error) List(int) ([]KV, error) Close() - Info() string Iterate(func([]byte, []byte) (bool, error)) error } diff --git a/db/leveldb/leveldb.go b/db/leveldb/leveldb.go index 2d25a5a..5e9f670 100644 --- a/db/leveldb/leveldb.go +++ b/db/leveldb/leveldb.go @@ -1,8 +1,6 @@ package leveldb import ( - "encoding/json" - "github.com/iden3/go-merkletree/db" log "github.com/sirupsen/logrus" "github.com/syndtr/goleveldb/leveldb" @@ -35,42 +33,6 @@ func NewLevelDbStorage(path string, errorIfMissing bool) (*Storage, error) { return &Storage{ldb, []byte{}}, nil } -type storageInfo struct { - KeyCount int - ClaimCount int -} - -// Info implements the method Info of the interface db.Storage -func (l *Storage) Info() string { - snapshot, err := l.ldb.GetSnapshot() - if err != nil { - return err.Error() - } - - keycount := 0 - claimcount := 0 - iter := snapshot.NewIterator(nil, nil) - for iter.Next() { - if iter.Value()[0] == byte(1) { - claimcount++ - } - - keycount++ - } - iter.Release() - if err := iter.Error(); err != nil { - return err.Error() - } - json, _ := json.MarshalIndent( - storageInfo{ - KeyCount: keycount, - ClaimCount: claimcount, - }, - "", " ", - ) - return string(json) -} - // WithPrefix implements the method WithPrefix of the interface db.Storage func (l *Storage) WithPrefix(prefix []byte) db.Storage { return &Storage{l.ldb, db.Concat(l.prefix, prefix)} diff --git a/db/memory/memory.go b/db/memory/memory.go index 0cd8c95..dc17a17 100644 --- a/db/memory/memory.go +++ b/db/memory/memory.go @@ -25,11 +25,6 @@ func NewMemoryStorage() *Storage { return &Storage{[]byte{}, kvmap} } -// Info implements the method Info of the interface db.Storage -func (m *Storage) Info() string { - return "in-memory" -} - // WithPrefix implements the method WithPrefix of the interface db.Storage func (m *Storage) WithPrefix(prefix []byte) db.Storage { return &Storage{db.Concat(m.prefix, prefix), m.kv} diff --git a/db/pebble/pebble.go b/db/pebble/pebble.go index 1d97f2c..a8fc6b4 100644 --- a/db/pebble/pebble.go +++ b/db/pebble/pebble.go @@ -1,8 +1,6 @@ package pebble import ( - "encoding/json" - "github.com/cockroachdb/pebble" "github.com/iden3/go-merkletree/db" log "github.com/sirupsen/logrus" @@ -32,36 +30,6 @@ func NewPebbleStorage(path string, errorIfMissing bool) (*Storage, error) { return &Storage{rdb, []byte{}}, nil } -type storageInfo struct { - KeyCount int - ClaimCount int -} - -// Info implements the method Info of the interface db.Storage -func (p *Storage) Info() string { - keycount := 0 - claimcount := 0 - err := p.Iterate(func(key []byte, value []byte) (bool, error) { - if value[0] == byte(1) { - claimcount++ - } - - keycount++ - return true, nil - }) - if err != nil { - return err.Error() - } - json, _ := json.MarshalIndent( - storageInfo{ - KeyCount: keycount, - ClaimCount: claimcount, - }, - "", " ", - ) - return string(json) -} - // WithPrefix implements the method WithPrefix of the interface db.Storage func (p *Storage) WithPrefix(prefix []byte) db.Storage { return &Storage{p.pdb, db.Concat(p.prefix, prefix)} diff --git a/go.mod b/go.mod index e1030da..2347114 100644 --- a/go.mod +++ b/go.mod @@ -4,8 +4,7 @@ go 1.14 require ( github.com/cockroachdb/pebble v0.0.0-20200814004841-77c18adb0ee3 - github.com/iden3/go-iden3-core v0.0.8 - github.com/iden3/go-iden3-crypto v0.0.6-0.20200819064831-09d161e9f670 + github.com/iden3/go-iden3-crypto v0.0.6-0.20201218111145-a2015adb2f1b github.com/sirupsen/logrus v1.5.0 github.com/stretchr/testify v1.6.1 github.com/syndtr/goleveldb v1.0.1-0.20190923125748-758128399b1d diff --git a/go.sum b/go.sum index 260a3f2..a7dbca9 100644 --- a/go.sum +++ b/go.sum @@ -132,11 +132,11 @@ github.com/huin/goutil v0.0.0-20170803182201-1ca381bf3150/go.mod h1:PpLOETDnJ0o3 github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0/go.mod h1:N0Wam8K1arqPXNWjMo21EXnBPOPp36vB07FNRdD2geA= github.com/iden3/go-circom-prover-verifier v0.0.1/go.mod h1:1FkpX4nUXxYcY2fpzqd27wHHEnWeo1v1nwDnz2TgBRo= github.com/iden3/go-circom-witnesscalc v0.0.1/go.mod h1:xjT1BlFZDBioHOlbD75SmZZLC1d1AfOycqbSa/1QRJU= -github.com/iden3/go-iden3-core v0.0.8 h1:PLw7iCiX7Pw1dqBkR+JaLQWqB5RKd+vgu25UBdvFXGQ= -github.com/iden3/go-iden3-core v0.0.8/go.mod h1:URNjIhMql6sEbWubIGrjJdw5wHCE1Pk1XghxjBOtA3s= github.com/iden3/go-iden3-crypto v0.0.5/go.mod h1:XKw1oDwYn2CIxKOtr7m/mL5jMn4mLOxAxtZBRxQBev8= -github.com/iden3/go-iden3-crypto v0.0.6-0.20200819064831-09d161e9f670 h1:gNBFu/WnRfNn+xywE04fgCWSHlb6wr0nIIll9i4R2fc= -github.com/iden3/go-iden3-crypto v0.0.6-0.20200819064831-09d161e9f670/go.mod h1:oBgthFLboAWi9feaBUFy7OxEcyn9vA1khHSL/WwWFyg= +github.com/iden3/go-iden3-crypto v0.0.6-0.20201203095229-821a601d2002 h1:f2twuL20aAqq1TlSdfQgL5r68hKjB/ioQdSctREQLuY= +github.com/iden3/go-iden3-crypto v0.0.6-0.20201203095229-821a601d2002/go.mod h1:oBgthFLboAWi9feaBUFy7OxEcyn9vA1khHSL/WwWFyg= +github.com/iden3/go-iden3-crypto v0.0.6-0.20201218111145-a2015adb2f1b h1:U2EsJRdonl0lhWvNiDscXZRDqSsPLIJHZg5DmU10IDo= +github.com/iden3/go-iden3-crypto v0.0.6-0.20201218111145-a2015adb2f1b/go.mod h1:oBgthFLboAWi9feaBUFy7OxEcyn9vA1khHSL/WwWFyg= github.com/iden3/go-wasm3 v0.0.1/go.mod h1:j+TcAB94Dfrjlu5kJt83h2OqAU+oyNUTwNZnQyII1sI= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/influxdata/influxdb v1.2.3-0.20180221223340-01288bdb0883/go.mod h1:qZna6X/4elxqT3yI9iZYdZrWWdeFOOprn86kgg4+IzY= @@ -255,7 +255,6 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20200311171314-f7b00557c8c4 h1:QmwruyY+bKbDDL0BaglrbZABEali68eoMFhTZpCjYVA= golang.org/x/crypto v0.0.0-20200311171314-f7b00557c8c4/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56/go.mod h1:JhuoJpWY28nO4Vef9tZUw9qufEGTyX1+7lmHxV5q5G4= diff --git a/merkletree.go b/merkletree.go index b75385d..5f95083 100644 --- a/merkletree.go +++ b/merkletree.go @@ -10,7 +10,6 @@ import ( "strings" "sync" - "github.com/iden3/go-iden3-core/common" cryptoUtils "github.com/iden3/go-iden3-crypto/utils" "github.com/iden3/go-merkletree/db" ) @@ -88,16 +87,16 @@ func (h Hash) Hex() string { // alternatively equivalent, but with too extra steps: // bRaw := h.BigInt().Bytes() // b := [32]byte{} - // copy(b[:], common.SwapEndianness(bRaw[:])) + // copy(b[:], SwapEndianness(bRaw[:])) // return hex.EncodeToString(b[:]) } // BigInt returns the *big.Int representation of the *Hash func (h *Hash) BigInt() *big.Int { - if new(big.Int).SetBytes(common.SwapEndianness(h[:])) == nil { + if new(big.Int).SetBytes(SwapEndianness(h[:])) == nil { return big.NewInt(0) } - return new(big.Int).SetBytes(common.SwapEndianness(h[:])) + return new(big.Int).SetBytes(SwapEndianness(h[:])) } // Bytes returns the []byte representation of the *Hash, which always is 32 @@ -105,7 +104,7 @@ func (h *Hash) BigInt() *big.Int { func (h *Hash) Bytes() []byte { bi := new(big.Int).SetBytes(h[:]).Bytes() b := [32]byte{} - copy(b[:], common.SwapEndianness(bi[:])) + copy(b[:], SwapEndianness(bi[:])) return b[:] } @@ -115,13 +114,11 @@ func (h *Hash) Bytes() []byte { // method. func NewBigIntFromHashBytes(b []byte) (*big.Int, error) { 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]) 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 } @@ -129,7 +126,7 @@ func NewBigIntFromHashBytes(b []byte) (*big.Int, error) { // NewHashFromBigInt returns a *Hash representation of the given *big.Int func NewHashFromBigInt(b *big.Int) *Hash { r := &Hash{} - copy(r[:], common.SwapEndianness(b.Bytes())) + copy(r[:], SwapEndianness(b.Bytes())) return r } @@ -138,11 +135,10 @@ func NewHashFromBigInt(b *big.Int) *Hash { // that previously has ben generated by the Hash.Bytes() method. func NewHashFromBytes(b []byte) (*Hash, error) { 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 - copy(h[:], common.SwapEndianness(b)) + copy(h[:], SwapEndianness(b)) return &h, nil } @@ -153,7 +149,7 @@ func NewHashFromHex(h string) (*Hash, error) { if err != nil { return nil, err } - return NewHashFromBytes(common.SwapEndianness(b[:])) + return NewHashFromBytes(SwapEndianness(b[:])) } // NewHashFromString returns a *Hash representation of the given decimal string @@ -327,8 +323,7 @@ func (mt *MerkleTree) pushLeaf(tx db.Tx, newLeaf *Node, oldLeaf *Node, lvl int, } var newNodeMiddle *Node 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 { return nil, err } @@ -395,12 +390,10 @@ func (mt *MerkleTree) addLeaf(tx db.Tx, newLeaf *Node, key *Hash, // right depending on path var newNodeMiddle *Node if path[lvl] { // go right - nextKey, err = mt.addLeaf(tx, newLeaf, n.ChildR, lvl+1, - path) + nextKey, err = mt.addLeaf(tx, newLeaf, n.ChildR, lvl+1, path) newNodeMiddle = NewNodeMiddle(n.ChildL, nextKey) } else { // go left - nextKey, err = mt.addLeaf(tx, newLeaf, n.ChildL, lvl+1, - path) + nextKey, err = mt.addLeaf(tx, newLeaf, n.ChildL, lvl+1, path) newNodeMiddle = NewNodeMiddle(nextKey, n.ChildR) } if err != nil { @@ -459,8 +452,7 @@ func (mt *MerkleTree) updateNode(tx db.Tx, n *Node) (*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. 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) @@ -759,7 +751,7 @@ func (mt *MerkleTree) GetNode(key *Hash) (*Node, error) { func getPath(numLevels int, k []byte) []bool { path := make([]bool, numLevels) for n := 0; n < numLevels; n++ { - path[n] = common.TestBit(k[:], uint(n)) + path[n] = TestBit(k[:], uint(n)) } return path } @@ -799,7 +791,7 @@ func NewProofFromBytes(bs []byte) (*Proof, error) { siblingBytes := bs[ElemBytesLen:] sibIdx := 0 for i := uint(0); i < p.depth; i++ { - if common.TestBitBigEndian(p.notempties[:], i) { + if TestBitBigEndian(p.notempties[:], i) { if len(siblingBytes) < (sibIdx+1)*ElemBytesLen { return nil, ErrInvalidProofBytes } @@ -852,7 +844,7 @@ func SiblingsFromProof(proof *Proof) []*Hash { sibIdx := 0 var siblings []*Hash for lvl := 0; lvl < int(proof.depth); lvl++ { - if common.TestBitBigEndian(proof.notempties[:], uint(lvl)) { + if TestBitBigEndian(proof.notempties[:], uint(lvl)) { siblings = append(siblings, proof.Siblings[sibIdx]) sibIdx++ } else { @@ -995,7 +987,7 @@ func (mt *MerkleTree) GenerateProof(k *big.Int, rootKey *Hash) (*Proof, return nil, nil, ErrInvalidNodeFound } if !bytes.Equal(siblingKey[:], HashZero[:]) { - common.SetBitBigEndian(p.notempties[:], uint(p.depth)) + SetBitBigEndian(p.notempties[:], uint(p.depth)) p.Siblings = append(p.Siblings, siblingKey) } } @@ -1042,7 +1034,7 @@ func RootFromProof(proof *Proof, k, v *big.Int) (*Hash, error) { path := getPath(int(proof.depth), kHash[:]) var siblingKey *Hash for lvl := int(proof.depth) - 1; lvl >= 0; lvl-- { - if common.TestBitBigEndian(proof.notempties[:], uint(lvl)) { + if TestBitBigEndian(proof.notempties[:], uint(lvl)) { siblingKey = proof.Siblings[sibIdx] sibIdx-- } else { diff --git a/utils.go b/utils.go index 175bdf2..af8b606 100644 --- a/utils.go +++ b/utils.go @@ -32,3 +32,27 @@ func HashElemsKey(key *big.Int, elems ...*big.Int) (*Hash, error) { } return NewHashFromBigInt(poseidonHash), nil } + +// SetBitBigEndian sets the bit n in the bitmap to 1, in Big Endian. +func SetBitBigEndian(bitmap []byte, n uint) { + bitmap[uint(len(bitmap))-n/8-1] |= 1 << (n % 8) +} + +// TestBit tests whether the bit n in bitmap is 1. +func TestBit(bitmap []byte, n uint) bool { + return bitmap[n/8]&(1<<(n%8)) != 0 +} + +// TestBitBigEndian tests whether the bit n in bitmap is 1, in Big Endian. +func TestBitBigEndian(bitmap []byte, n uint) bool { + return bitmap[uint(len(bitmap))-n/8-1]&(1<<(n%8)) != 0 +} + +// SwapEndianness swaps the order of the bytes in the slice. +func SwapEndianness(b []byte) []byte { + o := make([]byte, len(b)) + for i := range b { + o[len(b)-1-i] = b[i] + } + return o +}