-
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
Conversation
- expose NmtHasher API s.t. it can be used to construct and use NMT Hashsers externally (e.g. for IPLD multihashers) - Add two functions Sha256Namespace8FlaggedLeaf, Sha256Namespace8FlaggedInner whose signature matches regular hash functions that simplify the above even further
Codecov Report
@@ Coverage Diff @@
## master #19 +/- ##
==========================================
+ Coverage 95.30% 95.39% +0.09%
==========================================
Files 7 7
Lines 298 304 +6
==========================================
+ Hits 284 290 +6
Misses 10 10
Partials 4 4
Continue to review full report at Codecov.
|
hasher.go
Outdated
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 for the input len(namespacedData) >= DefaultNamespaceIDLen has to hold. | ||
// 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) | ||
} |
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.
Adding these 2 methods is the only real change. Everything else is moving existing code from the internal package to the main nmt one, and, deleting some no longer needed code/abstractions.
- use hasher from nmt instead of redefining the logic in multihash or here (see celestiaorg/nmt#19) - remove multihash fork and use updated go-verifcid - minor renaming and refactoring
- use hasher from nmt instead of redefining the logic in multihash or here (see celestiaorg/nmt#19) - remove multihash fork and use updated go-verifcid - minor renaming and refactoring
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.
LGTM
return n.NamespaceLen | ||
} | ||
|
||
func NewNmtHasher(baseHasher hash.Hash, nidLen namespace.IDSize, ignoreMaxNamespace bool) *Hasher { |
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 named NewHasher
.
* change plugin package * update to new rsmt2d API and update go.mod * Moved (pre)loading the plugin to init() of the plugin package itself * minor clarification with regard to the skipped test * refactor plugin to be more easily usable as a lib * Use cid.Undef and remove obsolete comment * use that lazyledger/go-ipfs fork * use ll go-verifcid and go-ipfs fork in main ll-core package too * update forks to latest version * replace deps in plugin and reorg in ll-core * bump ipfs related versions * Register custom hasher from plugin code instead of in go-multihash fork - use hasher from nmt instead of redefining the logic in multihash or here (see celestiaorg/nmt#19) - remove multihash fork and use updated go-verifcid - minor renaming and refactoring * update nmt and assert that the hashFunc is registered * small test to validate verifcid allows our hasher * actually skip test * use remote instead of local go-ipfs repo * clean up Makefile, remove set-taget, and add comment about building locally (in Readme) and remove ipfs dependency from ll-core module (this will be part of #152 instead) * review feedback: rename to mustRegisterNamespacedCodec * more review feedback: rename to mustRegisterNamespacedCodec and make sure the codec isn't already known * update nmt too * address nits: remove some unnecessary else statements * Update p2p/ipld/README.md Co-authored-by: John Adler <[email protected]> * s/defaulLength/defaultLength * Update p2p/ipld/plugin/nodes/nodes.go Co-authored-by: John Adler <[email protected]> * Deduplicate nmtHashSize * implement Copy * fix Tree implementation * Address more review comments: - rename share to namespacedLeaf - fix doc comment "When you realize you forgot to Load more https://www.youtube.com/watch?v=8yGfQak-q9M" * Add clarifying comment to `DataSquareRowOrColumnRawInputParser` and rename constant * minor doc improvement Co-authored-by: John Adler <[email protected]>
In celestiaorg/celestia-core#155 we want to register a custom multihash that matches those used by the nmt internally. Instead of duplicating the code for this (as in https://github.com/lazyledger/go-multihash/pull/1), we simply export the API to construct an nmt hasher.
Sha256Namespace8FlaggedLeaf
,Sha256Namespace8FlaggedInner
whose signature matches regular hash functions that simplify the above even furtherTODOs:
Sha256Namespace8FlaggedLeaf
,Sha256Namespace8FlaggedInner
closes: #18