-
Notifications
You must be signed in to change notification settings - Fork 45
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
chore: improves documentation #96
Changes from 54 commits
57481a0
d842ff6
5495171
f1f1a53
0d3d318
be869c9
69dd211
b02c28b
da16c5d
2c82fbb
12cc868
66fd0a8
256a705
810de44
3dbcd83
184a0a7
bf514d0
d659113
ef7c36f
dc838f1
28b38b3
393db51
33cf50f
ac87f1c
4466bf8
f4504d1
8310cea
2d81750
5d7a9a7
6b76848
ae4ef1b
dab15e2
52c87da
ebbadea
f91831c
3d295c9
36c4ebd
d161c16
0d497e7
b9c7736
c820e57
19d6620
7dea8b9
de897e4
82f9a1e
c60ee20
0095ca6
530a279
9effd59
8742307
e68e750
87437b1
bb85479
f53c3af
26b1ea6
04dd6f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -165,19 +165,32 @@ func (n *Hasher) HashLeaf(leaf []byte) []byte { | |
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 { | ||
// HashNode calculates a namespaced hash of a node using the | ||
// supplied left and right children. The input values, "left" and "right," | ||
// are namespaced hash values with the format "minNID || maxNID || hash." | ||
// By default, the normal namespace hash calculation is followed, | ||
// which is "res = min(left.minNID, right.minNID) || max(left.maxNID, | ||
// right.maxNID) || H(NodePrefix, left, | ||
// right)". "res" refers to the return value of the HashNode. | ||
// However, if the "ignoreMaxNs" property of the Hasher is set to true, | ||
// the calculation of the namespace ID range of the node slightly changes. | ||
// In this case, when setting the upper range, the maximum possible namespace ID | ||
// (i.e., 2^NamespaceIDSize-1) should be ignored if possible. | ||
// This is achieved by taking the maximum value among the namespace IDs | ||
// available in the range of its left and right children (i.e., | ||
// max(left.minNID, left.maxNID , right.minNID, | ||
// right.maxNID)), which is not equal to the maximum possible namespace ID value. | ||
// If such a namespace ID does not exist, the maximum NID is calculated | ||
// as normal, i.e., "res.maxNID = max(left.maxNID , right.maxNID). | ||
func (n *Hasher) HashNode(left, right []byte) []byte { | ||
h := n.baseHasher | ||
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] | ||
leftMinNs, leftMaxNs := left[:n.NamespaceLen], left[n.NamespaceLen:flagLen] | ||
rightMinNs, rightMaxNs := right[:n.NamespaceLen], right[n.NamespaceLen:flagLen] | ||
|
||
minNs := min(leftMinNs, rightMinNs) | ||
var maxNs []byte | ||
|
@@ -193,11 +206,7 @@ func (n *Hasher) HashNode(l, r []byte) []byte { | |
|
||
// 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...) | ||
data := append(append(append(make([]byte, 0, 1+len(left)+len(right)), NodePrefix), left...), right...) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [optional] I find this a lot more readable but it's subjective data := make([]byte, 0, 1+len(left)+len(right))
data = append(data, NodePrefix)
data = append(data, left...)
data = append(data, right...) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree! please see 26b1ea6 |
||
//nolint:errcheck | ||
h.Write(data) | ||
return h.Sum(res) | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,22 +4,27 @@ import "bytes" | |||||
|
||||||
type ID []byte | ||||||
|
||||||
// Less returns true if nid < ID, otherwise, false. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [optional] since both types that are being compared are of type
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed 26b1ea6 |
||||||
func (nid ID) Less(other ID) bool { | ||||||
return bytes.Compare(nid, other) < 0 | ||||||
} | ||||||
|
||||||
// Equal returns true if nid == ID, otherwise, false. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please see 26b1ea6 |
||||||
func (nid ID) Equal(other ID) bool { | ||||||
return bytes.Equal(nid, other) | ||||||
} | ||||||
|
||||||
// LessOrEqual returns true if nid <= ID, otherwise, false. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done 26b1ea6 |
||||||
func (nid ID) LessOrEqual(other ID) bool { | ||||||
return bytes.Compare(nid, other) <= 0 | ||||||
} | ||||||
|
||||||
// Size returns the byte size of the ID. | ||||||
func (nid ID) Size() IDSize { | ||||||
return IDSize(len(nid)) | ||||||
} | ||||||
|
||||||
// String stringifies the ID. | ||||||
func (nid ID) String() string { | ||||||
return string(nid) | ||||||
} |
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.
[not blocking] for my learning, what is the purpose of the
ignoreMaxNs
field? Do you think it would help readers to add docs to: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.
Disregard, I learned from below. Maybe a variation of this comment would be helpful here:
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.
Sure, done 26b1ea6