Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove all getters #36

Merged
merged 1 commit into from
Jul 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions merkletree/merkletree.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func (m *MerkleTree) Get(lookupIndex []byte) *AuthenticationPath {
nodePointer = m.root

authPath := &AuthenticationPath{
treeNonce: m.nonce,
lookupIndex: lookupIndex,
TreeNonce: m.nonce,
LookupIndex: lookupIndex,
}

for {
Expand All @@ -59,11 +59,11 @@ func (m *MerkleTree) Get(lookupIndex []byte) *AuthenticationPath {
}
direction := lookupIndexBits[depth]
if direction {
authPath.prunedHashes = append(authPath.prunedHashes,
authPath.PrunedTree = append(authPath.PrunedTree,
nodePointer.(*interiorNode).leftHash)
nodePointer = nodePointer.(*interiorNode).rightChild
} else {
authPath.prunedHashes = append(authPath.prunedHashes,
authPath.PrunedTree = append(authPath.PrunedTree,
nodePointer.(*interiorNode).rightHash)
nodePointer = nodePointer.(*interiorNode).leftChild
}
Expand All @@ -76,7 +76,7 @@ func (m *MerkleTree) Get(lookupIndex []byte) *AuthenticationPath {
switch nodePointer.(type) {
case *userLeafNode:
pNode := nodePointer.(*userLeafNode).Clone(nil).(*userLeafNode)
authPath.leaf = pNode
authPath.Leaf = pNode
if bytes.Equal(nodePointer.(*userLeafNode).index, lookupIndex) {
return authPath
}
Expand All @@ -85,7 +85,7 @@ func (m *MerkleTree) Get(lookupIndex []byte) *AuthenticationPath {
pNode.value = nil
return authPath
case *emptyNode:
authPath.leaf = nodePointer.(*emptyNode).Clone(nil).(*emptyNode)
authPath.Leaf = nodePointer.(*emptyNode).Clone(nil).(*emptyNode)
return authPath
}
panic(ErrorInvalidTree)
Expand Down Expand Up @@ -212,10 +212,6 @@ func (m *MerkleTree) recomputeHash() {
m.hash = m.root.Hash(m)
}

func (m *MerkleTree) GetHash() []byte {
return m.hash
}

func (m *MerkleTree) Clone() *MerkleTree {
return &MerkleTree{
nonce: m.nonce,
Expand Down
56 changes: 28 additions & 28 deletions merkletree/merkletree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@ func TestOneEntry(t *testing.T) {
}

r := m.Get(index)
if r.Leaf().Value() == nil {
if r.Leaf.Value() == nil {
t.Error("Cannot find value of key:", key)
return
}
v := r.Leaf().Value()
v := r.Leaf.Value()
if !bytes.Equal(v, val) {
t.Errorf("Value mismatch %v / %v", v, val)
}

// Check leaf node hash
h.Reset()
h.Write(r.Leaf().(*userLeafNode).salt)
h.Write(r.Leaf.(*userLeafNode).salt)
h.Write([]byte(key))
h.Write(val)
h.Read(commit[:])
Expand All @@ -77,7 +77,7 @@ func TestOneEntry(t *testing.T) {
}

r = m.Get([]byte("abc"))
if r.Leaf().Value() != nil {
if r.Leaf.Value() != nil {
t.Error("Invalid look-up operation:", key)
return
}
Expand All @@ -104,21 +104,21 @@ func TestTwoEntries(t *testing.T) {
}

ap1 := m.Get(index1)
if ap1.Leaf().Value() == nil {
if ap1.Leaf.Value() == nil {
t.Error("Cannot find key:", key1)
return
}

ap2 := m.Get(index2)
if ap2.Leaf().Value() == nil {
if ap2.Leaf.Value() == nil {
t.Error("Cannot find key:", key2)
return
}

if !bytes.Equal(ap1.Leaf().Value(), []byte("value1")) {
if !bytes.Equal(ap1.Leaf.Value(), []byte("value1")) {
t.Error(key1, "value mismatch")
}
if !bytes.Equal(ap2.Leaf().Value(), []byte("value2")) {
if !bytes.Equal(ap2.Leaf.Value(), []byte("value2")) {
t.Error(key2, "value mismatch")
}
}
Expand Down Expand Up @@ -150,42 +150,42 @@ func TestThreeEntries(t *testing.T) {
}

ap1 := m.Get(index1)
if ap1.Leaf().Value() == nil {
if ap1.Leaf.Value() == nil {
t.Error("Cannot find key:", index1)
return
}
ap2 := m.Get(index2)
if ap2.Leaf().Value() == nil {
if ap2.Leaf.Value() == nil {
t.Error("Cannot find key:", index2)
return
}
ap3 := m.Get(index3)
if ap3.Leaf().Value() == nil {
if ap3.Leaf.Value() == nil {
t.Error("Cannot find key:", index3)
return
}
/*
// since the first bit of ap2 index is false and the one of ap1 & ap3 are true
if ap2.Leaf().Level() != 1 {
if ap2.Leaf.Level() != 1 {
t.Error("Malformed tree insertion")
}

// since n1 and n3 share first 2 bits
if ap1.Leaf().Level() != 3 {
if ap1.Leaf.Level() != 3 {
t.Error("Malformed tree insertion")
}
if ap3.Leaf().Level() != 3 {
if ap3.Leaf.Level() != 3 {
t.Error("Malformed tree insertion")
}
*/

if !bytes.Equal(ap1.Leaf().Value(), []byte("value1")) {
if !bytes.Equal(ap1.Leaf.Value(), []byte("value1")) {
t.Error(key1, "value mismatch")
}
if !bytes.Equal(ap2.Leaf().Value(), []byte("value2")) {
if !bytes.Equal(ap2.Leaf.Value(), []byte("value2")) {
t.Error(key2, "value mismatch")
}
if !bytes.Equal(ap3.Leaf().Value(), []byte("value3")) {
if !bytes.Equal(ap3.Leaf.Value(), []byte("value3")) {
t.Error(key3, "value mismatch")
}
}
Expand All @@ -210,17 +210,17 @@ func TestInsertExistedKey(t *testing.T) {
}

ap := m.Get(index1)
if ap.Leaf().Value() == nil {
if ap.Leaf.Value() == nil {
t.Error("Cannot find key:", key1)
return
}

if !bytes.Equal(ap.Leaf().Value(), []byte("new value")) {
if !bytes.Equal(ap.Leaf.Value(), []byte("new value")) {
t.Error(index1, "value mismatch\n")
}

if !bytes.Equal(ap.Leaf().Value(), val2) {
t.Errorf("Value mismatch %v / %v", ap.Leaf().Value(), val2)
if !bytes.Equal(ap.Leaf.Value(), val2) {
t.Errorf("Value mismatch %v / %v", ap.Leaf.Value(), val2)
}

val3 := []byte("new value 2")
Expand All @@ -229,13 +229,13 @@ func TestInsertExistedKey(t *testing.T) {
}

ap = m.Get(index1)
if ap.Leaf().Value() == nil {
if ap.Leaf.Value() == nil {
t.Error("Cannot find key:", key1)
return
}

if !bytes.Equal(ap.Leaf().Value(), val3) {
t.Errorf("Value mismatch %v / %v", ap.Leaf().Value(), val3)
if !bytes.Equal(ap.Leaf.Value(), val3) {
t.Errorf("Value mismatch %v / %v", ap.Leaf.Value(), val3)
}
}

Expand Down Expand Up @@ -276,20 +276,20 @@ func TestTreeClone(t *testing.T) {

// lookup
ap := m2.Get(index1)
if ap.Leaf().Value() == nil {
if ap.Leaf.Value() == nil {
t.Error("Cannot find key:", key1)
return
}
if !bytes.Equal(ap.Leaf().Value(), []byte("value1")) {
if !bytes.Equal(ap.Leaf.Value(), []byte("value1")) {
t.Error(key1, "value mismatch\n")
}

ap = m2.Get(index2)
if ap.Leaf().Value() == nil {
if ap.Leaf.Value() == nil {
t.Error("Cannot find key:", key2)
return
}
if !bytes.Equal(ap.Leaf().Value(), []byte("value2")) {
if !bytes.Equal(ap.Leaf.Value(), []byte("value2")) {
t.Error(key2, "value mismatch\n")
}
}
20 changes: 10 additions & 10 deletions merkletree/pad.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (pad *PAD) signTreeRoot(m *MerkleTree, epoch uint64) {
panic(err)
}
} else {
prevStrHash = crypto.Digest(pad.latestSTR.sig)
prevStrHash = crypto.Digest(pad.latestSTR.Signature)
}
pad.latestSTR = NewSTR(pad.key, pad.policies, m, epoch, prevStrHash)
}
Expand Down Expand Up @@ -89,7 +89,7 @@ func (pad *PAD) Update(policies Policies) {
pad.loadedEpochs = append(pad.loadedEpochs[:0], pad.loadedEpochs[n:]...)
}

pad.updateInternal(policies, pad.latestSTR.epoch+1)
pad.updateInternal(policies, pad.latestSTR.Epoch+1)
}

func (pad *PAD) Set(key string, value []byte) error {
Expand All @@ -98,22 +98,22 @@ func (pad *PAD) Set(key string, value []byte) error {
}

func (pad *PAD) Lookup(key string) (*AuthenticationPath, error) {
return pad.LookupInEpoch(key, pad.latestSTR.epoch)
return pad.LookupInEpoch(key, pad.latestSTR.Epoch)
}

func (pad *PAD) LookupInEpoch(key string, epoch uint64) (*AuthenticationPath, error) {
str := pad.GetSTR(epoch)
if str == nil {
return nil, ErrorSTRNotFound
}
lookupIndex, proof := pad.computePrivateIndex(key, str.policies.vrfPrivate())
lookupIndex, proof := pad.computePrivateIndex(key, str.Policies.vrfPrivate())
ap := str.tree.Get(lookupIndex)
ap.vrfProof = proof
ap.VrfProof = proof
return ap, nil
}

func (pad *PAD) GetSTR(epoch uint64) *SignedTreeRoot {
if epoch >= pad.latestSTR.epoch {
if epoch >= pad.latestSTR.Epoch {
return pad.latestSTR
}
return pad.snapshots[epoch]
Expand All @@ -122,17 +122,17 @@ func (pad *PAD) GetSTR(epoch uint64) *SignedTreeRoot {
func (pad *PAD) TB(key string, value []byte) (*TemporaryBinding, error) {
str := pad.latestSTR
index, _ := pad.computePrivateIndex(key, pad.policies.vrfPrivate())
tb := str.sig
tb := str.Signature
tb = append(tb, index...)
tb = append(tb, value...)
sig := crypto.Sign(pad.key, tb)

err := pad.tree.Set(index, key, value)

return &TemporaryBinding{
index: index,
value: value,
sig: sig,
Index: index,
Value: value,
Signature: sig,
}, err
}

Expand Down
Loading