Skip to content
This repository has been archived by the owner on Aug 2, 2021. It is now read-only.

Merkle Tree Hash #48

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
3ab152e
Swarm - plan bee for content storage and distribution on web3
Dec 3, 2015
9d2605e
integrate RPC v2 via develop
zelig Dec 14, 2015
0e26569
add SwarmSwapDisabled=bbzswapdisabled SwarmPort=bzzport flags
zelig Dec 18, 2015
18cac67
network test using ethutil/swarm
zelig Dec 18, 2015
477d498
update to latest develop
zelig Feb 1, 2016
fb3b4fa
support for separate url schemes for dns enabled, immutable and raw m…
Feb 4, 2016
863fbb4
Merge pull request #2372 from ethersphere/s/3_url_schemes
zelig Mar 22, 2016
5714724
swap integration test
zelig Feb 5, 2016
7d5c213
Merge pull request #2373 from ethersphere/s/network-tests
zelig Mar 22, 2016
dbb151b
Merge pull request #2373 from ethersphere/s/network-tests
zelig Mar 22, 2016
ba49d70
Merge branch 'swarm' of github.com:ethereum/go-ethereum into swarm
Mar 22, 2016
d7db41b
Merge pull request #2373 from ethersphere/s/network-tests
zelig Mar 22, 2016
6fb86ac
Merge branch 'swarm' of github.com:ethereum/go-ethereum into swarm
Mar 22, 2016
30d31cf
Merge pull request #2373 from ethersphere/s/network-tests
zelig Mar 22, 2016
0e8684a
Merge branch 'swarm' of github.com:ethereum/go-ethereum into swarm
Mar 23, 2016
89a2678
Merge pull request #2373 from ethersphere/s/network-tests
zelig Mar 22, 2016
02d3328
Merge branch 'swarm' of github.com:ethereum/go-ethereum into swarm
Mar 23, 2016
585bdac
Merkle hash calculation for chunks and empty tests
May 3, 2016
f3c497f
Test an actual Merkle hash tree.
May 3, 2016
4880fb6
Merkle hashing of individual chunks.
May 3, 2016
d2ae0e7
Change DB retrieval integrity checks to Merkle hash.
May 5, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merkle hash calculation for chunks and empty tests
  • Loading branch information
Daniel A. Nagy committed May 3, 2016
commit 585bdac2bc37f48aa5b3b11d4184e5e98f2aa44a
27 changes: 27 additions & 0 deletions swarm/storage/binarymerkle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package storage

// This implementation does not take advantage of any paralellisms and uses
// far more memory than necessary, but it is easy to see that it is correct.
// It can be used for generating test cases for optimized implementations.

func BinaryMerkle(chunk []byte, hasher Hasher) []byte {
hash := hasher()
section := 2 * hash.Size()
l := len(chunk)
if l > section {
n := l / section
r := l - n*section
hash.Write(chunk[0:r])
next := hash.Sum(nil)
for r < l {
hash.Reset()
hash.Write(chunk[r : r+section])
next = hash.Sum(next)
r += section
}
return BinaryMerkle(next, hasher)
} else {
hash.Write(chunk)
return hash.Sum(nil)
}
}
42 changes: 42 additions & 0 deletions swarm/storage/binarymerkle_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package storage

import (
"bytes"
"testing"
)

// tests the hashing of an empty slice; not even a valid chunk
func TestEmpty(t *testing.T) {
// obtained from
// true | sha256sum
expected := []byte{
0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
}
empty := []byte{}
hasher := MakeHashFunc(NewChunkerParams().Hash)
hash := BinaryMerkle(empty, hasher)
t.Logf("Empty slice hash: %x", hash)
if !bytes.Equal(hash, expected) {
t.Errorf("Expected: %x", expected)
}
}

// tests the hashing of an empty chunk
func TestEmptyChunk(t *testing.T) {
// obtained from
// head -c8 /dev/zero |sha256sum
expected := []byte{
0xaf, 0x55, 0x70, 0xf5, 0xa1, 0x81, 0x0b, 0x7a, 0xf7, 0x8c, 0xaf, 0x4b,
0xc7, 0x0a, 0x66, 0x0f, 0x0d, 0xf5, 0x1e, 0x42, 0xba, 0xf9, 0x1d, 0x4d,
0xe5, 0xb2, 0x32, 0x8d, 0xe0, 0xe8, 0x3d, 0xfc,
}
empty := []byte{0, 0, 0, 0, 0, 0, 0, 0}
hasher := MakeHashFunc(NewChunkerParams().Hash)
hash := BinaryMerkle(empty, hasher)
t.Logf("Empty chunk hash: %x", hash)
if !bytes.Equal(hash, expected) {
t.Errorf("Expected: %x", expected)
}
}