Skip to content

Commit

Permalink
Fix @ismail comments
Browse files Browse the repository at this point in the history
  • Loading branch information
vqhuy committed Oct 26, 2017
1 parent 299999d commit 34ac0a9
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 106 deletions.
75 changes: 0 additions & 75 deletions crypto/hasher/coniks/coniks.go

This file was deleted.

80 changes: 80 additions & 0 deletions crypto/hashers/coniks/coniks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// TODO(huyvq): Remove package import.
// Other package shouldn't need to import this package,
// instead they should insert `hashers` and import this
// package using a blank import name.
// Should be adressed in #06.

package coniks

import (
"crypto"

"github.com/coniks-sys/coniks-go/crypto/hashers"
"github.com/coniks-sys/coniks-go/utils"
)

func init() {
hashers.RegisterHasher(CONIKS_Hash_SHA512_256, New)
}

const (
// CONIKS_Hash_SHA512_256 is the identity of the hashing strategy
// specified in the Coniks paper with SHA512_256 as the hash algorithm.
CONIKS_Hash_SHA512_256 = "CONIKS_Hash_SHA512_256"

emptyIdentifier = 'E'
leafIdentifier = 'L'
)

type hasher struct {
crypto.Hash
}

// New returns an instance of CONIKS_Hash_SHA512_256.
func New() hashers.PADHasher {
return &hasher{Hash: crypto.SHA512_256}
}

func (ch *hasher) Digest(ms ...[]byte) []byte {
h := ch.New()
for _, m := range ms {
h.Write(m)
}
return h.Sum(nil)
}

func (hasher) ID() string {
return CONIKS_Hash_SHA512_256
}

func (ch *hasher) Size() int {
return ch.Size()
}

// HashInterior computes the hash of an interior node as: H(left || right).
func (ch *hasher) HashInterior(left, right []byte) []byte {
return ch.Digest(left, right)
}

// HashLeaf computes the hash of a user leaf node as:
// H(Identifier || nonce || index || level || commit).
func (ch *hasher) HashLeaf(nonce []byte, index []byte, level uint32, commit []byte) []byte {
return ch.Digest(
[]byte{leafIdentifier},
nonce,
index,
utils.UInt32ToBytes(level),
commit,
)
}

// HashEmpty computes the hash of an empty leaf node as:
// H(Identifier || nonce || index || level).
func (ch *hasher) HashEmpty(nonce []byte, index []byte, level uint32) []byte {
return ch.Digest(
[]byte{emptyIdentifier},
nonce,
index,
utils.UInt32ToBytes(level),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ import (
"encoding/hex"
"testing"

"github.com/coniks-sys/coniks-go/crypto/hasher"
"github.com/coniks-sys/coniks-go/crypto/hashers"
)

// h2h converts a hex string into its Hash object.
func h2h(h string) hasher.Hash {
func h2h(h string) hashers.Hash {
b, err := hex.DecodeString(h)
if err != nil {
panic("invalid hex string")
}
var ret hasher.Hash
var ret hashers.Hash
copy(ret[:], b)
return ret
}

// s2h converts a byte slice into its Hash object.
func s2h(s []byte) hasher.Hash {
var ret hasher.Hash
func s2h(s []byte) hashers.Hash {
var ret hashers.Hash
copy(ret[:], s)
return ret
}
Expand All @@ -31,7 +31,7 @@ func TestHashLeafVectors(t *testing.T) {
index []byte
depth uint32
leaf []byte
want hasher.Hash
want hashers.Hash
}{
{treeNonce: [32]byte{0}, index: []byte("foo"), depth: 128, leaf: []byte("leaf"), want: h2h("65e7f29787a6168affd016656bb1f4f03af91cf7416270f5015005f8594d3eb6")},
} {
Expand All @@ -46,7 +46,7 @@ func TestHashEmptyVectors(t *testing.T) {
treeNonce [32]byte // treeNonce is treeID in KT, it should be a 32-byte array for cross-project compatibility
index []byte
depth uint32
want hasher.Hash
want hashers.Hash
}{
{treeNonce: [32]byte{0}, index: []byte("foo"), depth: 128, want: h2h("1a6b0eb739b32a46e7d679a9be03f522e907f53423aacb82e550bf657d1afb10")},
} {
Expand Down
23 changes: 10 additions & 13 deletions crypto/hasher/hasher.go → crypto/hashers/hasher.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package hasher
package hashers

import (
"fmt"
Expand All @@ -9,28 +9,24 @@ import (
// Hash represents the output of the used hash function.
type Hash [crypto.DefaultHashSizeByte]byte

// PADHasher provides hash functions for the PAD implementations.
// PADHasher provides hash functions for the PAD implementations,
// and defines the way empty / node / leaf hashes of the underlying tree
// are constructed.
type PADHasher interface {
// ID returns the name of the cryptographic hash function.
ID() string
// Size returns the size of the hash output in bytes.
Size() int
// Digest hashes all passed byte slices. The passed slices won't be mutated.
Digest(ms ...[]byte) []byte
treeHasher
}

// treeHasher provides hash functions for tree implementations.
type treeHasher interface {
// HashInterior computes the hash of an interior node as: H(left || right)
// HashInterior computes the hash of an interior node.
HashInterior(left, right []byte) []byte

// HashLeaf computes the hash of a user leaf node as:
// H(Identifier || nonce || index || level || commit)
// HashLeaf computes the hash of a user leaf node.
HashLeaf(nonce []byte, index []byte, level uint32, data []byte) []byte

// HashEmpty computes the hash of an empty leaf node as:
// H(Identifier || nonce || index || level)
// HashEmpty computes the hash of an empty leaf node.
HashEmpty(nonce []byte, index []byte, level uint32) []byte
}

Expand All @@ -44,8 +40,9 @@ func RegisterHasher(h string, f func() PADHasher) {
hashers[h] = f()
}

// Hasher returns a PADHasher.
func Hasher(h string) (PADHasher, error) {
// NewPADHasher returns a registered PADHasher identified by the given string.
// If no such PADHasher exists, it returns an error.
func NewPADHasher(h string) (PADHasher, error) {
if f, ok := hashers[h]; ok {
return f, nil
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package hasher
package hashers

import (
"testing"
Expand All @@ -25,7 +25,7 @@ func TestGetHasher(t *testing.T) {
RegisterHasher(fakeHasherID, fakeHasher)
}

_, err := Hasher(fakeHasherID)
_, err := NewPADHasher(fakeHasherID)
if err != nil {
t.Error("Expect a hasher.")
}
Expand Down
4 changes: 2 additions & 2 deletions merkletree/merkletree.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"errors"

"github.com/coniks-sys/coniks-go/crypto"
"github.com/coniks-sys/coniks-go/crypto/hasher"
"github.com/coniks-sys/coniks-go/crypto/hashers"
"github.com/coniks-sys/coniks-go/utils"
)

Expand Down Expand Up @@ -63,7 +63,7 @@ func (m *MerkleTree) Get(lookupIndex []byte) *AuthenticationPath {
break
}
direction := lookupIndexBits[depth]
var hashArr hasher.Hash
var hashArr hashers.Hash
if direction {
copy(hashArr[:], nodePointer.(*interiorNode).leftHash)
nodePointer = nodePointer.(*interiorNode).rightChild
Expand Down
2 changes: 1 addition & 1 deletion merkletree/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package merkletree

import (
"github.com/coniks-sys/coniks-go/crypto"
chasher "github.com/coniks-sys/coniks-go/crypto/hasher/coniks"
chasher "github.com/coniks-sys/coniks-go/crypto/hashers/coniks"
"github.com/coniks-sys/coniks-go/utils"
)

Expand Down
2 changes: 1 addition & 1 deletion merkletree/pad.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"errors"

"github.com/coniks-sys/coniks-go/crypto"
chasher "github.com/coniks-sys/coniks-go/crypto/hasher/coniks"
chasher "github.com/coniks-sys/coniks-go/crypto/hashers/coniks"
"github.com/coniks-sys/coniks-go/crypto/sign"
"github.com/coniks-sys/coniks-go/crypto/vrf"
)
Expand Down
6 changes: 3 additions & 3 deletions merkletree/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"errors"

"github.com/coniks-sys/coniks-go/crypto"
"github.com/coniks-sys/coniks-go/crypto/hasher"
chasher "github.com/coniks-sys/coniks-go/crypto/hasher/coniks"
"github.com/coniks-sys/coniks-go/crypto/hashers"
chasher "github.com/coniks-sys/coniks-go/crypto/hashers/coniks"
"github.com/coniks-sys/coniks-go/utils"
)

Expand Down Expand Up @@ -74,7 +74,7 @@ const (
// equals the lookup index.
type AuthenticationPath struct {
TreeNonce []byte
PrunedTree []hasher.Hash
PrunedTree []hashers.Hash
LookupIndex []byte
VrfProof []byte
Leaf *ProofNode
Expand Down
2 changes: 1 addition & 1 deletion merkletree/str.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package merkletree
import (
"bytes"

chasher "github.com/coniks-sys/coniks-go/crypto/hasher/coniks"
chasher "github.com/coniks-sys/coniks-go/crypto/hashers/coniks"
"github.com/coniks-sys/coniks-go/crypto/sign"
"github.com/coniks-sys/coniks-go/utils"
)
Expand Down
2 changes: 1 addition & 1 deletion protocol/policy.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package protocol

import (
chasher "github.com/coniks-sys/coniks-go/crypto/hasher/coniks"
chasher "github.com/coniks-sys/coniks-go/crypto/hashers/coniks"
"github.com/coniks-sys/coniks-go/crypto/vrf"
"github.com/coniks-sys/coniks-go/merkletree"
"github.com/coniks-sys/coniks-go/utils"
Expand Down

0 comments on commit 34ac0a9

Please sign in to comment.