|
@@ -26,8 +26,8 @@ import (
|
|
|
"testing"
|
|
"testing"
|
|
|
"time"
|
|
"time"
|
|
|
|
|
|
|
|
- "github.com/ethereum/go-ethereum/crypto/sha3"
|
|
|
|
|
"github.com/ethereum/go-ethereum/swarm/testutil"
|
|
"github.com/ethereum/go-ethereum/swarm/testutil"
|
|
|
|
|
+ "golang.org/x/crypto/sha3"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
// the actual data length generated (could be longer than max datalength of the BMT)
|
|
// the actual data length generated (could be longer than max datalength of the BMT)
|
|
@@ -44,7 +44,7 @@ var counts = []int{1, 2, 3, 4, 5, 8, 9, 15, 16, 17, 32, 37, 42, 53, 63, 64, 65,
|
|
|
|
|
|
|
|
// calculates the Keccak256 SHA3 hash of the data
|
|
// calculates the Keccak256 SHA3 hash of the data
|
|
|
func sha3hash(data ...[]byte) []byte {
|
|
func sha3hash(data ...[]byte) []byte {
|
|
|
- h := sha3.NewKeccak256()
|
|
|
|
|
|
|
+ h := sha3.NewLegacyKeccak256()
|
|
|
return doSum(h, nil, data...)
|
|
return doSum(h, nil, data...)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -121,7 +121,7 @@ func TestRefHasher(t *testing.T) {
|
|
|
t.Run(fmt.Sprintf("%d_segments_%d_bytes", segmentCount, length), func(t *testing.T) {
|
|
t.Run(fmt.Sprintf("%d_segments_%d_bytes", segmentCount, length), func(t *testing.T) {
|
|
|
data := testutil.RandomBytes(i, length)
|
|
data := testutil.RandomBytes(i, length)
|
|
|
expected := x.expected(data)
|
|
expected := x.expected(data)
|
|
|
- actual := NewRefHasher(sha3.NewKeccak256, segmentCount).Hash(data)
|
|
|
|
|
|
|
+ actual := NewRefHasher(sha3.NewLegacyKeccak256, segmentCount).Hash(data)
|
|
|
if !bytes.Equal(actual, expected) {
|
|
if !bytes.Equal(actual, expected) {
|
|
|
t.Fatalf("expected %x, got %x", expected, actual)
|
|
t.Fatalf("expected %x, got %x", expected, actual)
|
|
|
}
|
|
}
|
|
@@ -133,7 +133,7 @@ func TestRefHasher(t *testing.T) {
|
|
|
|
|
|
|
|
// tests if hasher responds with correct hash comparing the reference implementation return value
|
|
// tests if hasher responds with correct hash comparing the reference implementation return value
|
|
|
func TestHasherEmptyData(t *testing.T) {
|
|
func TestHasherEmptyData(t *testing.T) {
|
|
|
- hasher := sha3.NewKeccak256
|
|
|
|
|
|
|
+ hasher := sha3.NewLegacyKeccak256
|
|
|
var data []byte
|
|
var data []byte
|
|
|
for _, count := range counts {
|
|
for _, count := range counts {
|
|
|
t.Run(fmt.Sprintf("%d_segments", count), func(t *testing.T) {
|
|
t.Run(fmt.Sprintf("%d_segments", count), func(t *testing.T) {
|
|
@@ -153,7 +153,7 @@ func TestHasherEmptyData(t *testing.T) {
|
|
|
// tests sequential write with entire max size written in one go
|
|
// tests sequential write with entire max size written in one go
|
|
|
func TestSyncHasherCorrectness(t *testing.T) {
|
|
func TestSyncHasherCorrectness(t *testing.T) {
|
|
|
data := testutil.RandomBytes(1, BufferSize)
|
|
data := testutil.RandomBytes(1, BufferSize)
|
|
|
- hasher := sha3.NewKeccak256
|
|
|
|
|
|
|
+ hasher := sha3.NewLegacyKeccak256
|
|
|
size := hasher().Size()
|
|
size := hasher().Size()
|
|
|
|
|
|
|
|
var err error
|
|
var err error
|
|
@@ -179,7 +179,7 @@ func TestSyncHasherCorrectness(t *testing.T) {
|
|
|
// tests order-neutral concurrent writes with entire max size written in one go
|
|
// tests order-neutral concurrent writes with entire max size written in one go
|
|
|
func TestAsyncCorrectness(t *testing.T) {
|
|
func TestAsyncCorrectness(t *testing.T) {
|
|
|
data := testutil.RandomBytes(1, BufferSize)
|
|
data := testutil.RandomBytes(1, BufferSize)
|
|
|
- hasher := sha3.NewKeccak256
|
|
|
|
|
|
|
+ hasher := sha3.NewLegacyKeccak256
|
|
|
size := hasher().Size()
|
|
size := hasher().Size()
|
|
|
whs := []whenHash{first, last, random}
|
|
whs := []whenHash{first, last, random}
|
|
|
|
|
|
|
@@ -226,7 +226,7 @@ func TestHasherReuse(t *testing.T) {
|
|
|
|
|
|
|
|
// tests if bmt reuse is not corrupting result
|
|
// tests if bmt reuse is not corrupting result
|
|
|
func testHasherReuse(poolsize int, t *testing.T) {
|
|
func testHasherReuse(poolsize int, t *testing.T) {
|
|
|
- hasher := sha3.NewKeccak256
|
|
|
|
|
|
|
+ hasher := sha3.NewLegacyKeccak256
|
|
|
pool := NewTreePool(hasher, segmentCount, poolsize)
|
|
pool := NewTreePool(hasher, segmentCount, poolsize)
|
|
|
defer pool.Drain(0)
|
|
defer pool.Drain(0)
|
|
|
bmt := New(pool)
|
|
bmt := New(pool)
|
|
@@ -243,7 +243,7 @@ func testHasherReuse(poolsize int, t *testing.T) {
|
|
|
|
|
|
|
|
// Tests if pool can be cleanly reused even in concurrent use by several hasher
|
|
// Tests if pool can be cleanly reused even in concurrent use by several hasher
|
|
|
func TestBMTConcurrentUse(t *testing.T) {
|
|
func TestBMTConcurrentUse(t *testing.T) {
|
|
|
- hasher := sha3.NewKeccak256
|
|
|
|
|
|
|
+ hasher := sha3.NewLegacyKeccak256
|
|
|
pool := NewTreePool(hasher, segmentCount, PoolSize)
|
|
pool := NewTreePool(hasher, segmentCount, PoolSize)
|
|
|
defer pool.Drain(0)
|
|
defer pool.Drain(0)
|
|
|
cycles := 100
|
|
cycles := 100
|
|
@@ -277,7 +277,7 @@ LOOP:
|
|
|
// Tests BMT Hasher io.Writer interface is working correctly
|
|
// Tests BMT Hasher io.Writer interface is working correctly
|
|
|
// even multiple short random write buffers
|
|
// even multiple short random write buffers
|
|
|
func TestBMTWriterBuffers(t *testing.T) {
|
|
func TestBMTWriterBuffers(t *testing.T) {
|
|
|
- hasher := sha3.NewKeccak256
|
|
|
|
|
|
|
+ hasher := sha3.NewLegacyKeccak256
|
|
|
|
|
|
|
|
for _, count := range counts {
|
|
for _, count := range counts {
|
|
|
t.Run(fmt.Sprintf("%d_segments", count), func(t *testing.T) {
|
|
t.Run(fmt.Sprintf("%d_segments", count), func(t *testing.T) {
|
|
@@ -410,7 +410,7 @@ func BenchmarkPool(t *testing.B) {
|
|
|
// benchmarks simple sha3 hash on chunks
|
|
// benchmarks simple sha3 hash on chunks
|
|
|
func benchmarkSHA3(t *testing.B, n int) {
|
|
func benchmarkSHA3(t *testing.B, n int) {
|
|
|
data := testutil.RandomBytes(1, n)
|
|
data := testutil.RandomBytes(1, n)
|
|
|
- hasher := sha3.NewKeccak256
|
|
|
|
|
|
|
+ hasher := sha3.NewLegacyKeccak256
|
|
|
h := hasher()
|
|
h := hasher()
|
|
|
|
|
|
|
|
t.ReportAllocs()
|
|
t.ReportAllocs()
|
|
@@ -426,7 +426,7 @@ func benchmarkSHA3(t *testing.B, n int) {
|
|
|
// the premise is that this is the minimum computation needed for a BMT
|
|
// the premise is that this is the minimum computation needed for a BMT
|
|
|
// therefore this serves as a theoretical optimum for concurrent implementations
|
|
// therefore this serves as a theoretical optimum for concurrent implementations
|
|
|
func benchmarkBMTBaseline(t *testing.B, n int) {
|
|
func benchmarkBMTBaseline(t *testing.B, n int) {
|
|
|
- hasher := sha3.NewKeccak256
|
|
|
|
|
|
|
+ hasher := sha3.NewLegacyKeccak256
|
|
|
hashSize := hasher().Size()
|
|
hashSize := hasher().Size()
|
|
|
data := testutil.RandomBytes(1, hashSize)
|
|
data := testutil.RandomBytes(1, hashSize)
|
|
|
|
|
|
|
@@ -453,7 +453,7 @@ func benchmarkBMTBaseline(t *testing.B, n int) {
|
|
|
// benchmarks BMT Hasher
|
|
// benchmarks BMT Hasher
|
|
|
func benchmarkBMT(t *testing.B, n int) {
|
|
func benchmarkBMT(t *testing.B, n int) {
|
|
|
data := testutil.RandomBytes(1, n)
|
|
data := testutil.RandomBytes(1, n)
|
|
|
- hasher := sha3.NewKeccak256
|
|
|
|
|
|
|
+ hasher := sha3.NewLegacyKeccak256
|
|
|
pool := NewTreePool(hasher, segmentCount, PoolSize)
|
|
pool := NewTreePool(hasher, segmentCount, PoolSize)
|
|
|
bmt := New(pool)
|
|
bmt := New(pool)
|
|
|
|
|
|
|
@@ -467,7 +467,7 @@ func benchmarkBMT(t *testing.B, n int) {
|
|
|
// benchmarks BMT hasher with asynchronous concurrent segment/section writes
|
|
// benchmarks BMT hasher with asynchronous concurrent segment/section writes
|
|
|
func benchmarkBMTAsync(t *testing.B, n int, wh whenHash, double bool) {
|
|
func benchmarkBMTAsync(t *testing.B, n int, wh whenHash, double bool) {
|
|
|
data := testutil.RandomBytes(1, n)
|
|
data := testutil.RandomBytes(1, n)
|
|
|
- hasher := sha3.NewKeccak256
|
|
|
|
|
|
|
+ hasher := sha3.NewLegacyKeccak256
|
|
|
pool := NewTreePool(hasher, segmentCount, PoolSize)
|
|
pool := NewTreePool(hasher, segmentCount, PoolSize)
|
|
|
bmt := New(pool).NewAsyncWriter(double)
|
|
bmt := New(pool).NewAsyncWriter(double)
|
|
|
idxs, segments := splitAndShuffle(bmt.SectionSize(), data)
|
|
idxs, segments := splitAndShuffle(bmt.SectionSize(), data)
|
|
@@ -485,7 +485,7 @@ func benchmarkBMTAsync(t *testing.B, n int, wh whenHash, double bool) {
|
|
|
// benchmarks 100 concurrent bmt hashes with pool capacity
|
|
// benchmarks 100 concurrent bmt hashes with pool capacity
|
|
|
func benchmarkPool(t *testing.B, poolsize, n int) {
|
|
func benchmarkPool(t *testing.B, poolsize, n int) {
|
|
|
data := testutil.RandomBytes(1, n)
|
|
data := testutil.RandomBytes(1, n)
|
|
|
- hasher := sha3.NewKeccak256
|
|
|
|
|
|
|
+ hasher := sha3.NewLegacyKeccak256
|
|
|
pool := NewTreePool(hasher, segmentCount, poolsize)
|
|
pool := NewTreePool(hasher, segmentCount, poolsize)
|
|
|
cycles := 100
|
|
cycles := 100
|
|
|
|
|
|
|
@@ -508,7 +508,7 @@ func benchmarkPool(t *testing.B, poolsize, n int) {
|
|
|
// benchmarks the reference hasher
|
|
// benchmarks the reference hasher
|
|
|
func benchmarkRefHasher(t *testing.B, n int) {
|
|
func benchmarkRefHasher(t *testing.B, n int) {
|
|
|
data := testutil.RandomBytes(1, n)
|
|
data := testutil.RandomBytes(1, n)
|
|
|
- hasher := sha3.NewKeccak256
|
|
|
|
|
|
|
+ hasher := sha3.NewLegacyKeccak256
|
|
|
rbmt := NewRefHasher(hasher, 128)
|
|
rbmt := NewRefHasher(hasher, 128)
|
|
|
|
|
|
|
|
t.ReportAllocs()
|
|
t.ReportAllocs()
|