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

Restructure code to expose the internal hasher: #19

Merged
merged 3 commits into from
Feb 16, 2021
Merged

Conversation

liamsi
Copy link
Member

@liamsi liamsi commented Feb 12, 2021

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.

  • 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

TODOs:

closes: #18

 - 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
Copy link

codecov bot commented Feb 12, 2021

Codecov Report

Merging #19 (6fe0334) into master (6e8a6a5) will increase coverage by 0.09%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            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              
Impacted Files Coverage Δ
hasher.go 100.00% <100.00%> (ø)
nmt.go 92.95% <100.00%> (ø)
proof.go 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6e8a6a5...b2f848e. Read the comment docs.

hasher.go Outdated
Comment on lines 20 to 51
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)
}
Copy link
Member Author

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.

liamsi added a commit to celestiaorg/celestia-core that referenced this pull request Feb 13, 2021
 - 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
liamsi added a commit to celestiaorg/celestia-core that referenced this pull request Feb 13, 2021
 - 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
@liamsi liamsi marked this pull request as ready for review February 13, 2021 22:41
Copy link
Member

@tzdybal tzdybal left a 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 {
Copy link
Member

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.

@liamsi liamsi merged commit b22170d into master Feb 16, 2021
@liamsi liamsi deleted the liamsi/expose_hasher branch February 16, 2021 21:08
liamsi added a commit to celestiaorg/celestia-core that referenced this pull request Feb 19, 2021
* 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Export nmt-specific hashers
3 participants