From 0ac8b4649357603ffbbce5b0861a4be2a50fbd58 Mon Sep 17 00:00:00 2001 From: Eduard S Date: Tue, 14 Apr 2020 16:47:24 +0200 Subject: [PATCH] Fix linters errors --- babyjub/babyjub.go | 5 +---- babyjub/eddsa_test.go | 31 +++++++++++-------------------- babyjub/helpers.go | 5 ++++- mimc7/mimc7_test.go | 2 +- poseidon/poseidon_test.go | 4 ++-- utils/utils.go | 9 ++------- 6 files changed, 21 insertions(+), 35 deletions(-) diff --git a/babyjub/babyjub.go b/babyjub/babyjub.go index 26fa214..0566c8b 100644 --- a/babyjub/babyjub.go +++ b/babyjub/babyjub.go @@ -154,10 +154,7 @@ func (p *Point) InSubGroup() bool { // PointCoordSign returns the sign of the curve point coordinate. It returns // false if the sign is positive and false if the sign is negative. func PointCoordSign(c *big.Int) bool { - if c.Cmp(new(big.Int).Rsh(constants.Q, 1)) == 1 { - return true - } - return false + return c.Cmp(new(big.Int).Rsh(constants.Q, 1)) == 1 } func PackPoint(ay *big.Int, sign bool) [32]byte { diff --git a/babyjub/eddsa_test.go b/babyjub/eddsa_test.go index b7f68bc..467f9d5 100644 --- a/babyjub/eddsa_test.go +++ b/babyjub/eddsa_test.go @@ -1,7 +1,6 @@ package babyjub import ( - "crypto/rand" "encoding/hex" "fmt" "math/big" @@ -10,25 +9,13 @@ import ( "github.com/iden3/go-iden3-crypto/constants" "github.com/iden3/go-iden3-crypto/utils" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) -func genInputs() (*PrivateKey, *big.Int) { - k := NewRandPrivKey() - fmt.Println("k", hex.EncodeToString(k[:])) - - msgBuf := [32]byte{} - rand.Read(msgBuf[:]) - msg := utils.SetBigIntFromLEBytes(new(big.Int), msgBuf[:]) - msg.Mod(msg, constants.Q) - fmt.Println("msg", msg) - - return &k, msg -} - func TestPublicKey(t *testing.T) { var k PrivateKey - for i := 0; i < 256; i++ { - hex.Decode(k[:], []byte{byte(i)}) + for i := 0; i < 32; i++ { + k[i] = byte(i) } pk := k.Public() assert.True(t, pk.X.Cmp(constants.Q) == -1) @@ -37,7 +24,8 @@ func TestPublicKey(t *testing.T) { func TestSignVerifyMimc7(t *testing.T) { var k PrivateKey - hex.Decode(k[:], []byte("0001020304050607080900010203040506070809000102030405060708090001")) + _, err := hex.Decode(k[:], []byte("0001020304050607080900010203040506070809000102030405060708090001")) + require.Nil(t, err) msgBuf, err := hex.DecodeString("00010203040506070809") if err != nil { panic(err) @@ -81,7 +69,8 @@ func TestSignVerifyMimc7(t *testing.T) { func TestSignVerifyPoseidon(t *testing.T) { var k PrivateKey - hex.Decode(k[:], []byte("0001020304050607080900010203040506070809000102030405060708090001")) + _, err := hex.Decode(k[:], []byte("0001020304050607080900010203040506070809000102030405060708090001")) + require.Nil(t, err) msgBuf, err := hex.DecodeString("00010203040506070809") if err != nil { panic(err) @@ -125,7 +114,8 @@ func TestSignVerifyPoseidon(t *testing.T) { func TestCompressDecompress(t *testing.T) { var k PrivateKey - hex.Decode(k[:], []byte("0001020304050607080900010203040506070809000102030405060708090001")) + _, err := hex.Decode(k[:], []byte("0001020304050607080900010203040506070809000102030405060708090001")) + require.Nil(t, err) pk := k.Public() for i := 0; i < 64; i++ { msgBuf, err := hex.DecodeString(fmt.Sprintf("000102030405060708%02d", i)) @@ -144,7 +134,8 @@ func TestCompressDecompress(t *testing.T) { func BenchmarkBabyjubEddsa(b *testing.B) { var k PrivateKey - hex.Decode(k[:], []byte("0001020304050607080900010203040506070809000102030405060708090001")) + _, err := hex.Decode(k[:], []byte("0001020304050607080900010203040506070809000102030405060708090001")) + require.Nil(b, err) pk := k.Public() const n = 256 diff --git a/babyjub/helpers.go b/babyjub/helpers.go index 2392cf9..a392f88 100644 --- a/babyjub/helpers.go +++ b/babyjub/helpers.go @@ -8,6 +8,9 @@ import ( // the original blake from the SHA3 competition and not the new blake2 version. func Blake512(m []byte) []byte { h := blake512.New() - h.Write(m[:]) + _, err := h.Write(m[:]) + if err != nil { + panic(err) + } return h.Sum(nil) } diff --git a/mimc7/mimc7_test.go b/mimc7/mimc7_test.go index 55bfdf9..2d8e85c 100644 --- a/mimc7/mimc7_test.go +++ b/mimc7/mimc7_test.go @@ -87,6 +87,6 @@ func BenchmarkMIMC7(b *testing.B) { bigArray4 := []*big.Int{b12, b45, b78, b41} for i := 0; i < b.N; i++ { - Hash(bigArray4, nil) + Hash(bigArray4, nil) //nolint:errcheck } } diff --git a/poseidon/poseidon_test.go b/poseidon/poseidon_test.go index 59016cd..8b59cba 100644 --- a/poseidon/poseidon_test.go +++ b/poseidon/poseidon_test.go @@ -104,7 +104,7 @@ func BenchmarkPoseidon(b *testing.B) { bigArray4 := []*big.Int{b12, b45, b78, b41} for i := 0; i < b.N; i++ { - Hash(bigArray4) + Hash(bigArray4) //nolint:errcheck } } @@ -117,6 +117,6 @@ func BenchmarkPoseidonLarge(b *testing.B) { bigArray4 := []*big.Int{b12, b45, b78, b41} for i := 0; i < b.N; i++ { - Hash(bigArray4) + Hash(bigArray4) //nolint:errcheck } } diff --git a/utils/utils.go b/utils/utils.go index 84628cc..49020f6 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -66,9 +66,7 @@ func HexEncode(bs []byte) string { // HexDecode decodes a hex string into an array of bytes. func HexDecode(h string) ([]byte, error) { - if strings.HasPrefix(h, "0x") { - h = h[2:] - } + h = strings.TrimPrefix(h, "0x") return hex.DecodeString(h) } @@ -92,10 +90,7 @@ func HexDecodeInto(dst []byte, h []byte) error { // CheckBigIntInField checks if given *big.Int fits in a Field Q element func CheckBigIntInField(a *big.Int) bool { - if a.Cmp(constants.Q) != -1 { - return false - } - return true + return a.Cmp(constants.Q) == -1 } // CheckBigIntArrayInField checks if given *big.Int fits in a Field Q element