2023-06-01 23:21:08 +00:00
|
|
|
package utils_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ledgerwatch/erigon/cl/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestKeccak256(t *testing.T) {
|
|
|
|
data := []byte("test data")
|
|
|
|
extras := [][]byte{
|
|
|
|
[]byte("extra1"),
|
|
|
|
[]byte("extra2"),
|
|
|
|
}
|
|
|
|
|
2023-11-22 12:24:35 +00:00
|
|
|
expectedHash := utils.Sha256(data, extras...)
|
|
|
|
hashFunc := utils.OptimizedSha256NotThreadSafe()
|
2023-06-01 23:21:08 +00:00
|
|
|
expectedOptimizedHash := hashFunc(data, extras...)
|
|
|
|
|
|
|
|
// Test Keccak256 function
|
2023-11-22 12:24:35 +00:00
|
|
|
hash := utils.Sha256(data, extras...)
|
2023-06-01 23:21:08 +00:00
|
|
|
if hash != expectedHash {
|
|
|
|
t.Errorf("Keccak256 returned an incorrect hash. Expected: %x, Got: %x", expectedHash, hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test OptimizedKeccak256 function
|
|
|
|
optimizedHash := hashFunc(data, extras...)
|
|
|
|
if optimizedHash != expectedOptimizedHash {
|
|
|
|
t.Errorf("OptimizedKeccak256 returned an incorrect hash. Expected: %x, Got: %x", expectedOptimizedHash, optimizedHash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOptimizedKeccak256NotThreadSafe(t *testing.T) {
|
|
|
|
data := []byte("test data")
|
|
|
|
extras := [][]byte{
|
|
|
|
[]byte("extra1"),
|
|
|
|
[]byte("extra2"),
|
|
|
|
}
|
|
|
|
|
2023-11-22 12:24:35 +00:00
|
|
|
expectedHash := utils.Sha256(data, extras...)
|
|
|
|
hashFunc := utils.OptimizedSha256NotThreadSafe()
|
2023-06-01 23:21:08 +00:00
|
|
|
expectedOptimizedHash := hashFunc(data, extras...)
|
|
|
|
|
|
|
|
// Test OptimizedKeccak256NotThreadSafe function
|
2023-11-22 12:24:35 +00:00
|
|
|
hash := utils.Sha256(data, extras...)
|
2023-06-01 23:21:08 +00:00
|
|
|
if hash != expectedHash {
|
|
|
|
t.Errorf("Keccak256 returned an incorrect hash. Expected: %x, Got: %x", expectedHash, hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test OptimizedKeccak256NotThreadSafe function
|
|
|
|
optimizedHash := hashFunc(data, extras...)
|
|
|
|
if optimizedHash != expectedOptimizedHash {
|
|
|
|
t.Errorf("OptimizedKeccak256NotThreadSafe returned an incorrect hash. Expected: %x, Got: %x", expectedOptimizedHash, optimizedHash)
|
|
|
|
}
|
|
|
|
}
|