-
Notifications
You must be signed in to change notification settings - Fork 44
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
Restructure code to expose the internal hasher: #19
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
package nmt | ||
|
||
import ( | ||
"bytes" | ||
"crypto/sha256" | ||
"hash" | ||
|
||
"github.com/lazyledger/nmt/namespace" | ||
) | ||
|
||
const ( | ||
LeafPrefix = 0 | ||
NodePrefix = 1 | ||
|
||
DefaultNamespaceIDLen = 8 | ||
) | ||
|
||
// defaultHasher uses sha256 as a base-hasher, 8 bytes | ||
// for the namespace IDs and ignores the maximum possible namespace. | ||
var defaultHasher = NewNmtHasher(sha256.New(), DefaultNamespaceIDLen, true) | ||
|
||
// Sha256Namespace8FlaggedLeaf uses sha256 as a base-hasher, 8 bytes | ||
// for the namespace IDs and ignores the maximum possible namespace. | ||
// | ||
// Sha256Namespace8FlaggedLeaf(namespacedData) results in: | ||
// ns(rawData) || ns(rawData) || sha256(LeafPrefix || rawData), | ||
// where rawData is the leaf's data minus the namespace.ID prefix | ||
// (namely namespacedData[NamespaceLen:]). | ||
// | ||
// Note that different from other cryptographic hash functions, this here | ||
// makes assumptions on the input: | ||
// len(namespacedData) >= DefaultNamespaceIDLen has to hold, | ||
// as the first DefaultNamespaceIDLen bytes are interpreted as the namespace ID). | ||
// If the input does not fulfil this, we will panic. | ||
// The output will be of length 2*DefaultNamespaceIDLen+sha256.Size = 48 bytes. | ||
func Sha256Namespace8FlaggedLeaf(namespacedData []byte) []byte { | ||
return defaultHasher.HashLeaf(namespacedData) | ||
} | ||
|
||
// Sha256Namespace8FlaggedInner hashes inner nodes to: | ||
// minNID || maxNID || sha256(NodePrefix || leftRight), where leftRight consists of the full | ||
// left and right child node bytes, including their respective min and max namespace IDs. | ||
// Hence, the input has to be of size: | ||
// 48 = 32 + 8 + 8 = sha256.Size + 2*DefaultNamespaceIDLen bytes. | ||
// If the input does not fulfil this, we will panic. | ||
// The output will also be of length 2*DefaultNamespaceIDLen+sha256.Size = 48 bytes. | ||
func Sha256Namespace8FlaggedInner(leftRight []byte) []byte { | ||
const flagLen = DefaultNamespaceIDLen * 2 | ||
sha256Len := defaultHasher.Size() | ||
left := leftRight[:flagLen+sha256Len] | ||
right := leftRight[flagLen+sha256Len:] | ||
|
||
return defaultHasher.HashNode(left, right) | ||
} | ||
|
||
type Hasher struct { | ||
hash.Hash | ||
NamespaceLen namespace.IDSize | ||
|
||
ignoreMaxNs bool | ||
precomputedMaxNs namespace.ID | ||
} | ||
|
||
func (n *Hasher) IsMaxNamespaceIDIgnored() bool { | ||
return n.ignoreMaxNs | ||
} | ||
|
||
func (n *Hasher) NamespaceSize() namespace.IDSize { | ||
return n.NamespaceLen | ||
} | ||
|
||
func NewNmtHasher(baseHasher hash.Hash, nidLen namespace.IDSize, ignoreMaxNamespace bool) *Hasher { | ||
return &Hasher{ | ||
Hash: baseHasher, | ||
NamespaceLen: nidLen, | ||
ignoreMaxNs: ignoreMaxNamespace, | ||
precomputedMaxNs: bytes.Repeat([]byte{0xFF}, int(nidLen)), | ||
} | ||
} | ||
|
||
func (n *Hasher) EmptyRoot() []byte { | ||
emptyNs := bytes.Repeat([]byte{0}, int(n.NamespaceLen)) | ||
h := n.Sum(nil) | ||
digest := append(append(emptyNs, emptyNs...), h...) | ||
|
||
return digest | ||
} | ||
|
||
// HashLeaf hashes leaves to: | ||
// ns(rawData) || ns(rawData) || hash(leafPrefix || rawData), where raw data is the leaf's | ||
// data minus the namespaceID (namely leaf[NamespaceLen:]). | ||
// Hence, the input length has to be greater or equal to the | ||
// size of the underlying namespace.ID. | ||
// | ||
//Note that for leaves minNs = maxNs = ns(leaf) = leaf[:NamespaceLen]. | ||
//nolint:errcheck | ||
func (n *Hasher) HashLeaf(leaf []byte) []byte { | ||
h := n.Hash | ||
h.Reset() | ||
|
||
nID := leaf[:n.NamespaceLen] | ||
data := leaf[n.NamespaceLen:] | ||
res := append(append(make([]byte, 0), nID...), nID...) | ||
data = append([]byte{LeafPrefix}, data...) | ||
h.Write(data) | ||
return h.Sum(res) | ||
} | ||
|
||
// HashNode hashes inner nodes to: | ||
// minNID || maxNID || hash(NodePrefix || left || right), where left and right are the full | ||
// left and right child node bytes, including their respective min and max namespace IDs: | ||
// left = left.Min() || left.Max() || l.Hash(). | ||
func (n *Hasher) HashNode(l, r []byte) []byte { | ||
h := n.Hash | ||
h.Reset() | ||
|
||
// the actual hash result of the children got extended (or flagged) by their | ||
// children's minNs || maxNs; hence the flagLen = 2 * NamespaceLen: | ||
flagLen := 2 * n.NamespaceLen | ||
leftMinNs, leftMaxNs := l[:n.NamespaceLen], l[n.NamespaceLen:flagLen] | ||
rightMinNs, rightMaxNs := r[:n.NamespaceLen], r[n.NamespaceLen:flagLen] | ||
|
||
minNs := min(leftMinNs, rightMinNs) | ||
var maxNs []byte | ||
if n.ignoreMaxNs && n.precomputedMaxNs.Equal(leftMinNs) { | ||
maxNs = n.precomputedMaxNs | ||
} else if n.ignoreMaxNs && n.precomputedMaxNs.Equal(rightMinNs) { | ||
maxNs = leftMaxNs | ||
} else { | ||
maxNs = max(leftMaxNs, rightMaxNs) | ||
} | ||
|
||
res := append(append(make([]byte, 0), minNs...), maxNs...) | ||
|
||
// Note this seems a little faster than calling several Write()s on the | ||
// underlying Hash function (see: https://github.com/google/trillian/pull/1503): | ||
data := append(append(append( | ||
make([]byte, 0, 1+len(l)+len(r)), | ||
NodePrefix), | ||
l...), | ||
r...) | ||
//nolint:errcheck | ||
h.Write(data) | ||
return h.Sum(res) | ||
} | ||
|
||
func max(ns []byte, ns2 []byte) []byte { | ||
if bytes.Compare(ns, ns2) >= 0 { | ||
return ns | ||
} | ||
return ns2 | ||
} | ||
|
||
func min(ns []byte, ns2 []byte) []byte { | ||
if bytes.Compare(ns, ns2) <= 0 { | ||
return ns | ||
} | ||
return ns2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As this method is in
nmt
package, it can be namedNewHasher
.