aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFelix Lange <fjl@twurst.com>2017-03-23 02:48:51 +0800
committerFelix Lange <fjl@twurst.com>2017-03-23 03:49:15 +0800
commitf1534f5797664856218c8347366488f37f1e7924 (patch)
tree6a5d4a7dcfd72489bd4f7df70f5188a636947c24
parent9a2720fb35c9802c6dd61f9a49c48620b2ba2ecb (diff)
downloaddexon-f1534f5797664856218c8347366488f37f1e7924.tar
dexon-f1534f5797664856218c8347366488f37f1e7924.tar.gz
dexon-f1534f5797664856218c8347366488f37f1e7924.tar.bz2
dexon-f1534f5797664856218c8347366488f37f1e7924.tar.lz
dexon-f1534f5797664856218c8347366488f37f1e7924.tar.xz
dexon-f1534f5797664856218c8347366488f37f1e7924.tar.zst
dexon-f1534f5797664856218c8347366488f37f1e7924.zip
trie, whisper/whisperv5: use math/rand Read function
-rw-r--r--trie/trie_test.go18
-rw-r--r--whisper/whisperv5/filter_test.go20
-rw-r--r--whisper/whisperv5/message.go18
-rw-r--r--whisper/whisperv5/message_test.go16
-rw-r--r--whisper/whisperv5/whisper_test.go5
5 files changed, 23 insertions, 54 deletions
diff --git a/trie/trie_test.go b/trie/trie_test.go
index 60307dba8..01ae3a4e7 100644
--- a/trie/trie_test.go
+++ b/trie/trie_test.go
@@ -377,7 +377,7 @@ func (randTest) Generate(r *rand.Rand, size int) reflect.Value {
if len(allKeys) < 2 || r.Intn(100) < 10 {
// new key
key := make([]byte, r.Intn(50))
- randRead(r, key)
+ r.Read(key)
allKeys = append(allKeys, key)
return key
}
@@ -401,22 +401,6 @@ func (randTest) Generate(r *rand.Rand, size int) reflect.Value {
return reflect.ValueOf(steps)
}
-// rand.Rand provides a Read method in Go 1.7 and later, but
-// we can't use it yet.
-func randRead(r *rand.Rand, b []byte) {
- pos := 0
- val := 0
- for n := 0; n < len(b); n++ {
- if pos == 0 {
- val = r.Int()
- pos = 7
- }
- b[n] = byte(val)
- val >>= 8
- pos--
- }
-}
-
func runRandTest(rt randTest) bool {
db, _ := ethdb.NewMemDatabase()
tr, _ := New(common.Hash{}, db)
diff --git a/whisper/whisperv5/filter_test.go b/whisper/whisperv5/filter_test.go
index d69fb40db..1cf85b8d7 100644
--- a/whisper/whisperv5/filter_test.go
+++ b/whisper/whisperv5/filter_test.go
@@ -18,7 +18,7 @@ package whisperv5
import (
"math/big"
- "math/rand"
+ mrand "math/rand"
"testing"
"time"
@@ -33,12 +33,12 @@ var seed int64
// reproduciblity independent of their sequence.
func InitSingleTest() {
seed = time.Now().Unix()
- rand.Seed(seed)
+ mrand.Seed(seed)
}
func InitDebugTest(i int64) {
seed = i
- rand.Seed(seed)
+ mrand.Seed(seed)
}
type FilterTestCase struct {
@@ -55,7 +55,7 @@ func generateFilter(t *testing.T, symmetric bool) (*Filter, error) {
const topicNum = 8
f.Topics = make([]TopicType, topicNum)
for i := 0; i < topicNum; i++ {
- randomize(f.Topics[i][:])
+ mrand.Read(f.Topics[i][:])
f.Topics[i][0] = 0x01
}
@@ -68,7 +68,7 @@ func generateFilter(t *testing.T, symmetric bool) (*Filter, error) {
if symmetric {
f.KeySym = make([]byte, 12)
- randomize(f.KeySym)
+ mrand.Read(f.KeySym)
f.SymKeyHash = crypto.Keccak256Hash(f.KeySym)
} else {
f.KeyAsym, err = crypto.GenerateKey()
@@ -87,7 +87,7 @@ func generateTestCases(t *testing.T, SizeTestFilters int) []FilterTestCase {
for i := 0; i < SizeTestFilters; i++ {
f, _ := generateFilter(t, true)
cases[i].f = f
- cases[i].alive = (rand.Int()&int(1) == 0)
+ cases[i].alive = (mrand.Int()&int(1) == 0)
}
return cases
}
@@ -147,7 +147,7 @@ func TestComparePubKey(t *testing.T) {
}
// generate key3 == key1
- rand.Seed(seed)
+ mrand.Seed(seed)
key3, err := crypto.GenerateKey()
if err != nil {
t.Fatalf("failed to generate third key with seed %d: %s.", seed, err)
@@ -193,7 +193,7 @@ func TestMatchEnvelope(t *testing.T) {
}
// encrypt symmetrically
- i := rand.Int() % 4
+ i := mrand.Int() % 4
fsym.Topics[i] = params.Topic
fasym.Topics[i] = params.Topic
msg = NewSentMessage(params)
@@ -544,7 +544,7 @@ func TestWatchers(t *testing.T) {
var envelopes [NumMessages]*Envelope
for i = 0; i < NumMessages; i++ {
- j = rand.Uint32() % NumFilters
+ j = mrand.Uint32() % NumFilters
e = generateCompatibeEnvelope(t, tst[j].f)
envelopes[i] = e
tst[j].msgCnt++
@@ -597,7 +597,7 @@ func TestWatchers(t *testing.T) {
envelopes[0] = e
tst[0].msgCnt++
for i = 1; i < NumMessages; i++ {
- j = rand.Uint32() % NumFilters
+ j = mrand.Uint32() % NumFilters
e = generateCompatibeEnvelope(t, tst[j].f)
envelopes[i] = e
tst[j].msgCnt++
diff --git a/whisper/whisperv5/message.go b/whisper/whisperv5/message.go
index 9677f278e..5f964b072 100644
--- a/whisper/whisperv5/message.go
+++ b/whisper/whisperv5/message.go
@@ -128,7 +128,7 @@ func (msg *SentMessage) appendPadding(params *MessageParams) {
panic("please fix the padding algorithm before releasing new version")
}
buf := make([]byte, padSize)
- randomize(buf[1:])
+ mrand.Read(buf[1:])
buf[0] = byte(padSize)
if params.Padding != nil {
copy(buf[1:], params.Padding)
@@ -365,19 +365,3 @@ func (msg *ReceivedMessage) hash() []byte {
}
return crypto.Keccak256(msg.Raw)
}
-
-// rand.Rand provides a Read method in Go 1.7 and later,
-// but we can't use it yet.
-func randomize(b []byte) {
- cnt := 0
- val := mrand.Int63()
- for n := 0; n < len(b); n++ {
- b[n] = byte(val)
- val >>= 8
- cnt++
- if cnt >= 7 {
- cnt = 0
- val = mrand.Int63()
- }
- }
-}
diff --git a/whisper/whisperv5/message_test.go b/whisper/whisperv5/message_test.go
index c6f1ca2ca..1ed7250d3 100644
--- a/whisper/whisperv5/message_test.go
+++ b/whisper/whisperv5/message_test.go
@@ -18,7 +18,7 @@ package whisperv5
import (
"bytes"
- "math/rand"
+ mrand "math/rand"
"testing"
"github.com/ethereum/go-ethereum/crypto"
@@ -34,13 +34,13 @@ func generateMessageParams() (*MessageParams, error) {
// set all the parameters except p.Dst
buf := make([]byte, 1024)
- randomize(buf)
- sz := rand.Intn(400)
+ mrand.Read(buf)
+ sz := mrand.Intn(400)
var p MessageParams
p.PoW = 0.01
p.WorkTime = 1
- p.TTL = uint32(rand.Intn(1024))
+ p.TTL = uint32(mrand.Intn(1024))
p.Payload = make([]byte, sz)
p.Padding = make([]byte, padSizeLimitUpper)
p.KeySym = make([]byte, aesKeyLength)
@@ -132,7 +132,7 @@ func TestMessageEncryption(t *testing.T) {
func TestMessageWrap(t *testing.T) {
seed = int64(1777444222)
- rand.Seed(seed)
+ mrand.Seed(seed)
target := 128.0
params, err := generateMessageParams()
@@ -168,7 +168,7 @@ func TestMessageWrap(t *testing.T) {
func TestMessageSeal(t *testing.T) {
// this test depends on deterministic choice of seed (1976726903)
seed = int64(1976726903)
- rand.Seed(seed)
+ mrand.Seed(seed)
params, err := generateMessageParams()
if err != nil {
@@ -179,8 +179,8 @@ func TestMessageSeal(t *testing.T) {
params.TTL = 1
aesnonce := make([]byte, 12)
salt := make([]byte, 12)
- randomize(aesnonce)
- randomize(salt)
+ mrand.Read(aesnonce)
+ mrand.Read(salt)
env := NewEnvelope(params.TTL, params.Topic, salt, aesnonce, msg)
if err != nil {
diff --git a/whisper/whisperv5/whisper_test.go b/whisper/whisperv5/whisper_test.go
index 312dacfc4..8d63d443c 100644
--- a/whisper/whisperv5/whisper_test.go
+++ b/whisper/whisperv5/whisper_test.go
@@ -18,6 +18,7 @@ package whisperv5
import (
"bytes"
+ mrand "math/rand"
"testing"
"time"
@@ -49,7 +50,7 @@ func TestWhisperBasic(t *testing.T) {
}
peerID := make([]byte, 64)
- randomize(peerID)
+ mrand.Read(peerID)
peer, _ := w.getPeer(peerID)
if peer != nil {
t.Fatal("found peer for random key.")
@@ -212,7 +213,7 @@ func TestWhisperSymKeyManagement(t *testing.T) {
// add existing id, nothing should change
randomKey := make([]byte, 16)
- randomize(randomKey)
+ mrand.Read(randomKey)
err = w.AddSymKey(id1, randomKey)
if err == nil {
t.Fatalf("failed AddSymKey with seed %d.", seed)