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

Replace Double-SHA256 with blake2b and implement domain seperation #1245

Merged
merged 9 commits into from
Dec 21, 2020

Conversation

elichai
Copy link
Member

@elichai elichai commented Dec 17, 2020

This solves the following issues: #1204, #1188, #1186.

We chose Blake2b because of its speeds (See #1188 (comment)) we use blake2b's MAC for domain separation because golang x/crypto currently doesn't expose the personalization feature (See golang/go#32447).

We also fix the merkle tree bug by doing 2 things:

  1. Domain seperation for txs and for merkle branches.
  2. If there's an odd number of leafs/branches instead of duplicating we append zeros.

There's now a new file called domains.go that contains all the constructors for the domain separated writers. which should be the only way to hash in kaspad.
if we ever need to hash something new we need to write a new constructor with the appropriate domain

@ci-dag
Copy link

ci-dag commented Dec 17, 2020

Rebuild after update in master

@ci-dag
Copy link

ci-dag commented Dec 20, 2020

Rebuild after update in master

2 similar comments
@ci-dag
Copy link

ci-dag commented Dec 20, 2020

Rebuild after update in master

@ci-dag
Copy link

ci-dag commented Dec 20, 2020

Rebuild after update in master

payloadHash = *hashes.HashData(payload)
writer := hashes.NewPayloadHashWriter()
writer.InfallibleWrite(payload)
payloadHash = *writer.Finalize()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar sets of three lines keep appearing in the code.
I would've preferred if you could bundle them together into convenience methods and used those instead.

err = serialization.WriteElement(writer, timestamp)
writer := hashes.NewPoWHashWriter()
writer.InfallibleWrite(prePowHash[:])
err := serialization.WriteElement(writer, timestamp)
if err != nil {
panic(errors.Wrap(err, "this should never happen. SHA256's digest should never return an error"))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update this error message.

if err != nil {
panic(errors.Wrap(err, "this should never happen. SHA256's digest should never return an error"))
}
writer.InfallibleWrite(zeroes[:])
err = serialization.WriteElement(writer, nonce)
if err != nil {
panic(errors.Wrap(err, "this should never happen. SHA256's digest should never return an error"))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update this error message.

@@ -90,12 +82,12 @@ func merkleRoot(hashes []*externalapi.DomainHash) *externalapi.DomainHash {
merkles[offset] = nil

// When there is no right child, the parent is generated by
// hashing the concatenation of the left child with itself.
// hashing the concatenation of the left child with zeros.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are the benefits of this over concatenating the child with itself?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This prevents having 2 separate inclusion proofs for the same thing in the same merkle tree

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And it is also the standard for how merkle trees are built, see: https://tools.ietf.org/html/rfc6962#section-2.1

buf, err := vm.dstack.PopByteArray()
if err != nil {
return err
}

vm.dstack.PushByteArray(hashes.HashData(buf)[:])
hash := sha256.Sum256(buf)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No :)

@ci-dag
Copy link

ci-dag commented Dec 21, 2020

Rebuild after update in master

@stasatdaglabs stasatdaglabs merged commit 45edacf into v0.8.4-dev Dec 21, 2020
@stasatdaglabs stasatdaglabs deleted the blake2b branch December 21, 2020 10:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants