Skip to content

Commit

Permalink
Fix import name
Browse files Browse the repository at this point in the history
  • Loading branch information
vqhuy committed Oct 26, 2017
1 parent c5bde39 commit 47f82f9
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 119 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
30 changes: 14 additions & 16 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,25 @@ 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 provides a universal hash function which
// 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 @@ -39,15 +36,16 @@ var hashers = make(map[string]PADHasher)
// RegisterHasher registers a hasher for use.
func RegisterHasher(h string, f func() PADHasher) {
if _, ok := hashers[h]; ok {
panic(fmt.Sprintf("RegisterHasher(%v) is already registered", h))
panic(fmt.Sprintf("%s is already registered", h))
}
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
}
return nil, fmt.Errorf("Hasher(%v) is unknown hasher", h)
return nil, fmt.Errorf("%s is an unknown hasher", h)
}
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
8 changes: 4 additions & 4 deletions 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"
conikshasher "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 Expand Up @@ -83,11 +83,11 @@ func (n *interiorNode) hash(m *MerkleTree) []byte {
if n.rightHash == nil {
n.rightHash = n.rightChild.hash(m)
}
return conikshasher.New().HashInterior(n.leftHash, n.rightHash)
return chasher.New().HashInterior(n.leftHash, n.rightHash)
}

func (n *userLeafNode) hash(m *MerkleTree) []byte {
return conikshasher.New().HashLeaf(
return chasher.New().HashLeaf(
m.nonce,
n.index,
n.level,
Expand All @@ -96,7 +96,7 @@ func (n *userLeafNode) hash(m *MerkleTree) []byte {
}

func (n *emptyNode) hash(m *MerkleTree) []byte {
return conikshasher.New().HashEmpty(
return chasher.New().HashEmpty(
m.nonce,
n.index,
n.level,
Expand Down
4 changes: 2 additions & 2 deletions 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"
conikshasher "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 Expand Up @@ -64,7 +64,7 @@ func (pad *PAD) signTreeRoot(epoch uint64) {
panic(err)
}
} else {
prevHash = conikshasher.New().Digest(pad.latestSTR.Signature)
prevHash = chasher.New().Digest(pad.latestSTR.Signature)
}
pad.tree.recomputeHash()
m := pad.tree.Clone()
Expand Down
14 changes: 7 additions & 7 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"
conikshasher "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 @@ -41,13 +41,13 @@ type ProofNode struct {
func (n *ProofNode) hash(treeNonce []byte) []byte {
if n.IsEmpty {
// empty leaf node
return conikshasher.New().HashEmpty(
return chasher.New().HashEmpty(
treeNonce,
n.Index,
n.Level,
)
} else {
return conikshasher.New().HashLeaf(
return chasher.New().HashLeaf(
treeNonce,
n.Index,
n.Level,
Expand All @@ -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 All @@ -88,9 +88,9 @@ func (ap *AuthenticationPath) authPathHash() []byte {
for depth > 0 {
depth -= 1
if indexBits[depth] { // right child
hash = conikshasher.New().Digest(ap.PrunedTree[depth][:], hash)
hash = chasher.New().Digest(ap.PrunedTree[depth][:], hash)
} else {
hash = conikshasher.New().Digest(hash, ap.PrunedTree[depth][:])
hash = chasher.New().Digest(hash, ap.PrunedTree[depth][:])
}
}
return hash
Expand Down
Loading

0 comments on commit 47f82f9

Please sign in to comment.