Skip to content

Commit

Permalink
*: don't use math/rand
Browse files Browse the repository at this point in the history
It's deprecated and it's not needed most of the time.

Signed-off-by: Roman Khimov <[email protected]>
  • Loading branch information
roman-khimov committed Aug 19, 2024
1 parent 070b962 commit 8d33bed
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 28 deletions.
2 changes: 1 addition & 1 deletion gf127/gf127.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/hex"
"errors"
"math/bits"
"math/rand"
"math/rand/v2"
)

// GF127 represents element of GF(2^127).
Expand Down
31 changes: 10 additions & 21 deletions tz/hash_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package tz

import (
"bytes"
"encoding/hex"
"fmt"
"io"
"math/rand"
"slices"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -104,19 +104,8 @@ func prepareArch(t testing.TB, b arch) {
}
}

func newBuffer() (data []byte) {
data = make([]byte, benchDataSize)

r := rand.New(rand.NewSource(0))
_, err := io.ReadFull(r, data)
if err != nil {
panic("cant initialize buffer")
}
return
}

func BenchmarkSum(b *testing.B) {
data := newBuffer()
data := bytes.Repeat([]byte{0x01, 0x02, 0x03, 0x04, 0x05}, benchDataSize/5)

for i := range backends {
b.Run(backends[i].Name+" digest", func(b *testing.B) {
Expand All @@ -136,24 +125,24 @@ func BenchmarkSum(b *testing.B) {
}

func TestHomomorphism(t *testing.T) {
const halfInputSize = 32
var (
c1, c2 sl2
n int
err error
h, h1, h2 [Size]byte
b []byte
)

b = make([]byte, 64)
n, err = rand.Read(b) //nolint:staticcheck // SA1019: rand.Read has been deprecated since Go 1.20 because it shouldn't be used
require.Equal(t, 64, n)
require.NoError(t, err)
b = slices.Concat(
bytes.Repeat([]byte{0x01}, halfInputSize),
bytes.Repeat([]byte{0x02}, halfInputSize),
)

// Test if our hashing is really homomorphic
h = Sum(b)
require.NotEqual(t, [64]byte{}, h)
h1 = Sum(b[:32])
h2 = Sum(b[32:])
h1 = Sum(b[:halfInputSize])
h2 = Sum(b[halfInputSize:])

err = c1.UnmarshalBinary(h1[:])
require.NoError(t, err)
Expand Down
6 changes: 0 additions & 6 deletions tz/sl2_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package tz

import (
"math/rand"
"testing"
"time"

"github.com/nspcc-dev/tzhash/gf127"
"github.com/stretchr/testify/require"
)

func init() {
rand.Seed(time.Now().UnixNano()) //nolint:staticcheck // SA1019: rand.Seed has been deprecated since Go 1.20 and an alternative has been available since Go 1.0
}

func random() (a *sl2) {
a = new(sl2)
a[0][0] = *gf127.Random()
Expand Down

0 comments on commit 8d33bed

Please sign in to comment.