Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating linter version to v1.41 #237

Merged
merged 3 commits into from
Jul 6, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
13 changes: 7 additions & 6 deletions .etc/golangci.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
---
linters:
disable-all: true
enable:
# - lll
# - gocritic
# - gocognit
# - lll
# - gocritic
# - gocognit
# - interfacer (deprecated since v1.38.0)
# - scopelint (deprecated since v1.39.0)
# - golint (deprecated since v1.41.0)
- bodyclose
- deadcode
- depguard
Expand All @@ -14,15 +18,12 @@ linters:
- gocyclo
- gofmt
- goimports
- golint
- gosec
- gosimple
- govet
- ineffassign
- interfacer
- misspell
- nakedret
- scopelint
- staticcheck
- structcheck
- stylecheck
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Linting
uses: golangci/golangci-lint-action@v2
with:
version: v1.29
version: v1.41
args: --config=./.etc/golangci.yml ./...
- name: Setup Go-${{ matrix.GOVER }}
uses: actions/setup-go@v2
Expand Down
27 changes: 24 additions & 3 deletions pke/kyber/internal/common/field_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package common

import (
"crypto/rand"
"encoding/binary"
"flag"
mathRand "math/rand"
"testing"
)

Expand Down Expand Up @@ -35,9 +36,29 @@ func TestBarrettReduceFull(t *testing.T) {
}
}

func randSliceUint32(N uint) []uint32 {
armfazh marked this conversation as resolved.
Show resolved Hide resolved
bytes := make([]uint8, 4*N)
n, err := rand.Read(bytes)
if err != nil {
panic(err)
} else if n < len(bytes) {
panic("short read from RNG")
}
x := make([]uint32, N)
for i := range x {
x[i] = binary.LittleEndian.Uint32(bytes[4*i:])
}
return x
}

func TestMontReduce(t *testing.T) {
for i := 0; i < 1000; i++ {
x := mathRand.Int31n(int32(Q)*(1<<16)) - int32(Q)*(1<<15)
N := 1000
r := randSliceUint32(uint(N))
max := uint32(Q) * (1 << 16)
mid := int32(Q) * (1 << 15)

for i := 0; i < N; i++ {
x := int32(r[i]%max) - mid
y := montReduce(x)
if modQ32(x) != modQ32(int32(y)*(1<<16)) {
t.Fatalf("%d", x)
Expand Down
13 changes: 7 additions & 6 deletions pke/kyber/internal/common/ntt_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package common

import (
mathRand "math/rand"
"testing"
)
import "testing"

func BenchmarkNTT(b *testing.B) {
var a Poly
Expand Down Expand Up @@ -34,14 +31,18 @@ func BenchmarkInvNTTGeneric(b *testing.B) {
}

func (p *Poly) Rand() {
r := randSliceUint32(uint(N))
max := uint32(Q)
for i := 0; i < N; i++ {
p[i] = int16(mathRand.Intn(int(Q)))
p[i] = int16(r[i] % max)
}
}

func (p *Poly) RandAbsLeQ() {
r := randSliceUint32(uint(N))
max := 2 * uint32(Q)
for i := 0; i < N; i++ {
p[i] = int16(mathRand.Intn(int(2*Q))) - Q
p[i] = int16(int32(r[i]%max) - int32(Q))
}
}

Expand Down
13 changes: 9 additions & 4 deletions pke/kyber/internal/common/poly_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package common

import (
cryptoRand "crypto/rand"
"crypto/rand"
"fmt"
mathRand "math/rand"
"testing"
)

func (p *Poly) RandAbsLe9Q() {
r := randSliceUint32(uint(N))
max := 9 * uint32(Q)
for i := 0; i < N; i++ {
p[i] = int16(mathRand.Intn(18*int(Q) - 9*int(Q)))
p[i] = int16(int32(r[i] % max))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change looks equivalent to me. However, the way the original code was written is weird. (Why 18 * int(Q) - 9 * int(Q) instead of just 9 * int(Q)?) This makes me think it might be worth double checking that this is the correct range.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bwesterb could you please give us some light here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's move this change forward, and do changes in other PR if bwesterb thinks it's needed.

Copy link
Member

@bwesterb bwesterb Jul 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intended code was int16(mathRand.Intn(9*int(Q)) - mathRand.Intn(18*int(Q))).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Bas, I will fix this on #240

}
}

Expand All @@ -26,7 +27,11 @@ func TestDecompressMessage(t *testing.T) {
var m, m2 [PlaintextSize]byte
var p Poly
for i := 0; i < 1000; i++ {
_, _ = cryptoRand.Read(m[:])
_, err := rand.Read(m[:])
armfazh marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
t.Error(err)
armfazh marked this conversation as resolved.
Show resolved Hide resolved
}

p.DecompressMessage(m[:])
p.CompressMessageTo(m2[:])
if m != m2 {
Expand Down
30 changes: 25 additions & 5 deletions sign/dilithium/internal/common/field_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
package common

import (
"crypto/rand"
"encoding/binary"
"flag"
"math/rand"
"testing"
)

var runVeryLongTest = flag.Bool("very-long", false, "runs very long tests")

func randSliceUint32(N uint) []uint32 {
bytes := make([]uint8, 4*N)
n, err := rand.Read(bytes)
if err != nil {
panic(err)
} else if n < len(bytes) {
panic("short read from RNG")
}
x := make([]uint32, N)
for i := range x {
x[i] = binary.LittleEndian.Uint32(bytes[4*i:])
}
return x
}

func TestModQ(t *testing.T) {
for i := 0; i < 1000; i++ {
x := rand.Uint32()
const testTimes = 1000
cjpatton marked this conversation as resolved.
Show resolved Hide resolved
r := randSliceUint32(testTimes)
for i := 0; i < testTimes; i++ {
x := r[i]
y := modQ(x)
if y > Q {
t.Fatalf("modQ(%d) > Q", x)
Expand All @@ -22,8 +40,10 @@ func TestModQ(t *testing.T) {
}

func TestReduceLe2Q(t *testing.T) {
for i := 0; i < 1000; i++ {
x := rand.Uint32()
const testTimes = 1000
r := randSliceUint32(testTimes)
for i := 0; i < testTimes; i++ {
x := r[i]
y := reduceLe2Q(x)
if y > 2*Q {
t.Fatalf("reduce_le2q(%d) > 2Q", x)
Expand Down
9 changes: 4 additions & 5 deletions sign/dilithium/internal/common/ntt_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package common

import (
"math/rand"
"testing"
)
import "testing"

func (p *Poly) RandLe2Q() {
r := randSliceUint32(N)
max := 2 * uint32(Q)
for i := uint(0); i < N; i++ {
p[i] = uint32(rand.Intn(int(2 * Q)))
p[i] = r[i] % max
}
}

Expand Down
7 changes: 5 additions & 2 deletions sign/dilithium/internal/common/pack_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package common

import (
"math/rand"
"crypto/rand"
"testing"
)

func TestPackLe16AgainstGeneric(t *testing.T) {
var p Poly
var buf1, buf2 [PolyLe16Size]byte
pp := make([]uint8, 256)

for j := 0; j < 1000; j++ {
_, _ = rand.Read(pp)
armfazh marked this conversation as resolved.
Show resolved Hide resolved
for i := 0; i < 256; i++ {
p[i] = uint32(rand.Intn(16))
p[i] = uint32(pp[i] & 0xF)
}
p.PackLe16(buf1[:])
p.packLe16Generic(buf2[:])
Expand Down
15 changes: 5 additions & 10 deletions sign/dilithium/internal/common/poly_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package common

import (
"math/rand"
"testing"
)
import "testing"

func TestExceeds(t *testing.T) {
for i := 0; i < N; i++ {
Expand Down Expand Up @@ -116,9 +113,8 @@ func TestMulHatAgainstGeneric(t *testing.T) {
func TestReduceLe2QAgainstGeneric(t *testing.T) {
for k := 0; k < 1000; k++ {
var a Poly
for j := 0; j < N; j++ {
a[j] = rand.Uint32()
}
r := randSliceUint32(N)
copy(a[:], r)
p1 := a
p2 := a
p1.reduceLe2QGeneric()
Expand All @@ -132,9 +128,8 @@ func TestReduceLe2QAgainstGeneric(t *testing.T) {
func TestNormalizeAgainstGeneric(t *testing.T) {
for k := 0; k < 1000; k++ {
var a Poly
for j := 0; j < N; j++ {
a[j] = rand.Uint32()
}
r := randSliceUint32(N)
copy(a[:], r)
p1 := a
p2 := a
p1.normalizeGeneric()
Expand Down