From e29fa44c5ddcd4c7bf53edea8929169a480ca6b7 Mon Sep 17 00:00:00 2001 From: bytemare <3641580+bytemare@users.noreply.github.com> Date: Sat, 15 Jun 2024 20:03:07 +0200 Subject: [PATCH] Fix JSON serde, refactored encoding tests, fix some documentation Signed-off-by: bytemare <3641580+bytemare@users.noreply.github.com> --- .github/.golangci.yml | 31 ++--- element.go | 13 +-- groups.go | 4 - internal/edwards25519/element.go | 4 +- internal/edwards25519/scalar.go | 4 +- internal/element.go | 4 +- internal/nist/element.go | 4 +- internal/nist/scalar.go | 4 +- internal/ristretto/element.go | 4 +- internal/ristretto/scalar.go | 4 +- internal/scalar.go | 4 +- internal/secp256k1/element.go | 4 +- internal/secp256k1/scalar.go | 4 +- scalar.go | 13 +-- tests/element_test.go | 20 ++-- tests/encoding_test.go | 195 +++++++++++++++++++++++++------ tests/groups_test.go | 26 ++--- tests/scalar_test.go | 16 +-- tests/table_test.go | 2 +- 19 files changed, 238 insertions(+), 122 deletions(-) diff --git a/.github/.golangci.yml b/.github/.golangci.yml index 4fd1862..625f370 100644 --- a/.github/.golangci.yml +++ b/.github/.golangci.yml @@ -13,17 +13,17 @@ linters: - dogsled - dupl - durationcheck + - err113 - errcheck - errchkjson - errname - errorlint - - execinquery #- exhaustive #- exhaustruct - exportloopref - forbidigo - forcetypeassert - - funlen + - funlencd - gci #- gochecknoglobals #- gochecknoinits @@ -33,7 +33,6 @@ linters: - gocyclo - godot - godox - - goerr113 - gofmt - gofumpt - goheader @@ -51,7 +50,7 @@ linters: #- interfacebloat #- ireturn - lll - - logrlint + - loggercheck - maintidx - makezero - misspell @@ -101,7 +100,9 @@ linters-settings: errcheck: check-type-assertions: true check-blank: true - #exclude-functions: + exclude-functions: + - (*crypto/Element).MarshalBinary + - (*crypto/Scalar).MarshalBinary # - io/ioutil.ReadFile # - io.Copy(*bytes.Buffer) # - io.Copy(os.Stdout) @@ -145,16 +146,6 @@ linters-settings: simplify: true goimports: local-prefixes: github.com/bytemare/crypto - gomnd: - checks: - - argument - - condition - - return - - assign - ignored-functions: - - 'nist.setMapping' - - 'big.NewInt' - - 'hash2curve.HashToFieldXMD' gosimple: checks: [ "all" ] govet: @@ -203,6 +194,16 @@ linters-settings: tab-width: 4 misspell: locale: US + mnd: + checks: + - argument + - condition + - return + - assign + ignored-functions: + - 'nist.setMapping' + - 'big.NewInt' + - 'hash2curve.HashToFieldXMD' nlreturn: block-size: 2 prealloc: diff --git a/element.go b/element.go index 3b3a6d3..44860c1 100644 --- a/element.go +++ b/element.go @@ -11,6 +11,7 @@ package crypto import ( "fmt" + "strings" "github.com/bytemare/crypto/internal" ) @@ -148,22 +149,18 @@ func (e *Element) DecodeHex(h string) error { // MarshalJSON marshals the element into valid JSON. func (e *Element) MarshalJSON() ([]byte, error) { - return []byte(e.Hex()), nil + return []byte(fmt.Sprintf("%q", e.Hex())), nil } // UnmarshalJSON unmarshals the input into the element. func (e *Element) UnmarshalJSON(data []byte) error { - return e.DecodeHex(string(data)) + j := strings.ReplaceAll(string(data), "\"", "") + return e.DecodeHex(j) } // MarshalBinary returns the compressed byte encoding of the element. func (e *Element) MarshalBinary() ([]byte, error) { - dec, err := e.Element.MarshalBinary() - if err != nil { - return nil, fmt.Errorf("element MarshalBinary: %w", err) - } - - return dec, nil + return e.Element.MarshalBinary(), nil } // UnmarshalBinary sets e to the decoding of the byte encoded element. diff --git a/groups.go b/groups.go index ec26b3f..fd91536 100644 --- a/groups.go +++ b/groups.go @@ -163,8 +163,6 @@ func (g Group) init() { switch g { case Ristretto255Sha512: g.initGroup(ristretto.New) - case decaf448Shake256: - panic("decaf is not yet supported") case P256Sha256: g.initGroup(nist.P256) case P384Sha384: @@ -175,8 +173,6 @@ func (g Group) init() { g.initGroup(edwards25519.New) case Secp256k1: g.initGroup(secp256k1.New) - case maxID: - panic("group not recognized") default: panic("group not recognized") } diff --git a/internal/edwards25519/element.go b/internal/edwards25519/element.go index 2ac2da1..ae008fa 100644 --- a/internal/edwards25519/element.go +++ b/internal/edwards25519/element.go @@ -180,8 +180,8 @@ func (e *Element) DecodeHex(h string) error { } // MarshalBinary returns the compressed byte encoding of the element. -func (e *Element) MarshalBinary() (data []byte, err error) { - return e.Encode(), nil +func (e *Element) MarshalBinary() []byte { + return e.Encode() } // UnmarshalBinary sets e to the decoding of the byte encoded element. diff --git a/internal/edwards25519/scalar.go b/internal/edwards25519/scalar.go index 088c072..60ecb9d 100644 --- a/internal/edwards25519/scalar.go +++ b/internal/edwards25519/scalar.go @@ -362,8 +362,8 @@ func (s *Scalar) DecodeHex(h string) error { } // MarshalBinary returns the compressed byte encoding of the scalar. -func (s *Scalar) MarshalBinary() (data []byte, err error) { - return s.Encode(), nil +func (s *Scalar) MarshalBinary() []byte { + return s.Encode() } // UnmarshalBinary sets e to the decoding of the byte encoded scalar. diff --git a/internal/element.go b/internal/element.go index 6b7e72b..3cbec00 100644 --- a/internal/element.go +++ b/internal/element.go @@ -63,8 +63,8 @@ type Element interface { // DecodeHex sets e to the decoding of the hex encoded element. DecodeHex(h string) error - // BinaryMarshaler implementation. - encoding.BinaryMarshaler + // MarshalBinary returns a byte representation of the element. + MarshalBinary() []byte // BinaryUnmarshaler implementation. encoding.BinaryUnmarshaler diff --git a/internal/nist/element.go b/internal/nist/element.go index 063fe77..f68e4b3 100644 --- a/internal/nist/element.go +++ b/internal/nist/element.go @@ -237,8 +237,8 @@ func (e *Element[P]) DecodeHex(h string) error { } // MarshalBinary returns the compressed byte encoding of the element. -func (e *Element[P]) MarshalBinary() ([]byte, error) { - return e.Encode(), nil +func (e *Element[P]) MarshalBinary() []byte { + return e.Encode() } // UnmarshalBinary sets e to the decoding of the byte encoded element. diff --git a/internal/nist/scalar.go b/internal/nist/scalar.go index 6ea2fb4..6c9ce1f 100644 --- a/internal/nist/scalar.go +++ b/internal/nist/scalar.go @@ -267,8 +267,8 @@ func (s *Scalar) DecodeHex(h string) error { } // MarshalBinary returns the compressed byte encoding of the scalar. -func (s *Scalar) MarshalBinary() ([]byte, error) { - return s.Encode(), nil +func (s *Scalar) MarshalBinary() []byte { + return s.Encode() } // UnmarshalBinary sets e to the decoding of the byte encoded scalar. diff --git a/internal/ristretto/element.go b/internal/ristretto/element.go index 164d420..5c3d23d 100644 --- a/internal/ristretto/element.go +++ b/internal/ristretto/element.go @@ -186,8 +186,8 @@ func (e *Element) DecodeHex(h string) error { } // MarshalBinary returns the compressed byte encoding of the element. -func (e *Element) MarshalBinary() ([]byte, error) { - return e.Encode(), nil +func (e *Element) MarshalBinary() []byte { + return e.Encode() } // UnmarshalBinary sets e to the decoding of the byte encoded element. diff --git a/internal/ristretto/scalar.go b/internal/ristretto/scalar.go index a62fac8..905aede 100644 --- a/internal/ristretto/scalar.go +++ b/internal/ristretto/scalar.go @@ -352,8 +352,8 @@ func (s *Scalar) DecodeHex(h string) error { } // MarshalBinary returns the compressed byte encoding of the scalar. -func (s *Scalar) MarshalBinary() ([]byte, error) { - return s.Encode(), nil +func (s *Scalar) MarshalBinary() []byte { + return s.Encode() } // UnmarshalBinary sets e to the decoding of the byte encoded scalar. diff --git a/internal/scalar.go b/internal/scalar.go index 88184b5..c835f33 100644 --- a/internal/scalar.go +++ b/internal/scalar.go @@ -74,8 +74,8 @@ type Scalar interface { // DecodeHex sets s to the decoding of the hex encoded scalar. DecodeHex(h string) error - // BinaryMarshaler returns a byte representation of the element. - encoding.BinaryMarshaler + // MarshalBinary returns a byte representation of the scalar. + MarshalBinary() []byte // BinaryUnmarshaler recovers an element from a byte representation // produced either by encoding.BinaryMarshaler or MarshalBinaryCompress. diff --git a/internal/secp256k1/element.go b/internal/secp256k1/element.go index bbb26aa..7cb8ed4 100644 --- a/internal/secp256k1/element.go +++ b/internal/secp256k1/element.go @@ -151,8 +151,8 @@ func (e *Element) DecodeHex(h string) error { } // MarshalBinary returns the compressed byte encoding of the element. -func (e *Element) MarshalBinary() (data []byte, err error) { - return e.Encode(), nil +func (e *Element) MarshalBinary() []byte { + return e.Encode() } // UnmarshalBinary sets e to the decoding of the byte encoded element. diff --git a/internal/secp256k1/scalar.go b/internal/secp256k1/scalar.go index 49a57bd..cb55d2d 100644 --- a/internal/secp256k1/scalar.go +++ b/internal/secp256k1/scalar.go @@ -207,8 +207,8 @@ func (s *Scalar) DecodeHex(h string) error { } // MarshalBinary returns the compressed byte encoding of the scalar. -func (s *Scalar) MarshalBinary() (data []byte, err error) { - return s.Encode(), nil +func (s *Scalar) MarshalBinary() []byte { + return s.Encode() } // UnmarshalBinary sets e to the decoding of the byte encoded scalar. diff --git a/scalar.go b/scalar.go index b1c25e0..e8a91d0 100644 --- a/scalar.go +++ b/scalar.go @@ -11,6 +11,7 @@ package crypto import ( "fmt" + "strings" "github.com/bytemare/crypto/internal" ) @@ -180,22 +181,18 @@ func (s *Scalar) DecodeHex(h string) error { // MarshalJSON marshals the scalar into valid JSON. func (s *Scalar) MarshalJSON() ([]byte, error) { - return []byte(s.Hex()), nil + return []byte(fmt.Sprintf("%q", s.Hex())), nil } // UnmarshalJSON unmarshals the input into the scalar. func (s *Scalar) UnmarshalJSON(data []byte) error { - return s.DecodeHex(string(data)) + j := strings.ReplaceAll(string(data), "\"", "") + return s.DecodeHex(j) } // MarshalBinary implements the encoding.BinaryMarshaler interface. func (s *Scalar) MarshalBinary() ([]byte, error) { - dec, err := s.Scalar.MarshalBinary() - if err != nil { - return nil, fmt.Errorf("scalar MarshalBinary: %w", err) - } - - return dec, nil + return s.Scalar.MarshalBinary(), nil } // UnmarshalBinary implements the encoding.BinaryUnmarshaler interface. diff --git a/tests/element_test.go b/tests/element_test.go index 4cd23a7..8d74789 100644 --- a/tests/element_test.go +++ b/tests/element_test.go @@ -55,7 +55,7 @@ func testElementCopySet(t *testing.T, element, other *crypto.Element) { } func TestElementCopy(t *testing.T) { - testAll(t, func(group *testGroup) { + testAllGroups(t, func(group *testGroup) { base := group.group.Base() cpy := base.Copy() testElementCopySet(t, base, cpy) @@ -63,7 +63,7 @@ func TestElementCopy(t *testing.T) { } func TestElementSet(t *testing.T) { - testAll(t, func(group *testGroup) { + testAllGroups(t, func(group *testGroup) { base := group.group.Base() other := group.group.NewElement() other.Set(base) @@ -90,7 +90,7 @@ func TestElement_WrongInput(t *testing.T) { } } - testAll(t, func(group *testGroup) { + testAllGroups(t, func(group *testGroup) { element := group.group.NewElement() var alternativeGroup crypto.Group @@ -133,7 +133,7 @@ func TestElement_WrongInput(t *testing.T) { } func TestElement_EncodedLength(t *testing.T) { - testAll(t, func(group *testGroup) { + testAllGroups(t, func(group *testGroup) { id := group.group.NewElement().Identity().Encode() if len(id) != group.elementLength { t.Fatalf( @@ -164,7 +164,7 @@ func TestElement_EncodedLength(t *testing.T) { } func TestElement_Decode_OutOfBounds(t *testing.T) { - testAll(t, func(group *testGroup) { + testAllGroups(t, func(group *testGroup) { decodeErr := "element Decode: " unmarshallBinaryErr := "element UnmarshalBinary: " errMessage := "" @@ -221,7 +221,7 @@ func TestElement_Decode_OutOfBounds(t *testing.T) { } func TestElement_XCoordinate(t *testing.T) { - testAll(t, func(group *testGroup) { + testAllGroups(t, func(group *testGroup) { baseX := hex.EncodeToString(group.group.Base().XCoordinate()) refLen := len(baseX) / 2 // hexadecimal length is 2 times byt length @@ -239,7 +239,7 @@ func TestElement_XCoordinate(t *testing.T) { } func TestElement_Vectors_Add(t *testing.T) { - testAll(t, func(group *testGroup) { + testAllGroups(t, func(group *testGroup) { base := group.group.Base() acc := group.group.Base() @@ -264,7 +264,7 @@ func TestElement_Vectors_Add(t *testing.T) { } func TestElement_Vectors_Double(t *testing.T) { - testAll(t, func(group *testGroup) { + testAllGroups(t, func(group *testGroup) { tables := [][]int{ {1, 2, 4, 8}, {3, 6, 12}, @@ -287,7 +287,7 @@ func TestElement_Vectors_Double(t *testing.T) { } func TestElement_Vectors_Mult(t *testing.T) { - testAll(t, func(group *testGroup) { + testAllGroups(t, func(group *testGroup) { s := group.group.NewScalar() base := group.group.Base() @@ -304,7 +304,7 @@ func TestElement_Vectors_Mult(t *testing.T) { } func TestElement_Arithmetic(t *testing.T) { - testAll(t, func(group *testGroup) { + testAllGroups(t, func(group *testGroup) { elementTestEqual(t, group.group) elementTestAdd(t, group.group) elementTestDouble(t, group.group) diff --git a/tests/encoding_test.go b/tests/encoding_test.go index 3e98774..2499c17 100644 --- a/tests/encoding_test.go +++ b/tests/encoding_test.go @@ -12,8 +12,13 @@ import ( "bytes" "encoding" "encoding/hex" + "encoding/json" + "errors" + "fmt" "strings" "testing" + + "github.com/bytemare/crypto" ) type serde interface { @@ -27,63 +32,187 @@ type serde interface { encoding.BinaryUnmarshaler } -func testEncoding(t *testing.T, thing1, thing2 serde) { - // empty string - if err := thing2.DecodeHex(""); err == nil { - t.Fatal("expected error on empty string") +type ( + byteEncoder func() ([]byte, error) + byteDecoder func([]byte) error + makeEncodeTest func(t *encodingTest) *encodingTest +) + +var encodeTesters = []makeEncodeTest{ + encodeTest, + binaryTest, + hexTest, + jsonTest, +} + +func toEncoder(s serde) byteEncoder { + return func() ([]byte, error) { + return s.Encode(), nil } +} - encoded := thing1.Encode() - marshalled, _ := thing1.MarshalBinary() - hexed := thing1.Hex() +func hexToEncoder(s serde) byteEncoder { + return func() ([]byte, error) { + return []byte(s.Hex()), nil + } +} + +func hexToDecoder(s serde) byteDecoder { + return func(d []byte) error { + return s.DecodeHex(string(d)) + } +} + +type encodingTest struct { + source, receiver serde + sourceEncoder byteEncoder + receiverDecoder byteDecoder + receiverEncoder byteEncoder +} - jsoned, err := thing1.MarshalJSON() +func newEncodingTest(source, receiver serde) *encodingTest { + return &encodingTest{source: source, receiver: receiver} +} + +func encodeTest(t *encodingTest) *encodingTest { + t.sourceEncoder = toEncoder(t.source) + t.receiverDecoder = t.receiver.Decode + t.receiverEncoder = toEncoder(t.receiver) + + return t +} + +func binaryTest(t *encodingTest) *encodingTest { + t.sourceEncoder = t.source.MarshalBinary + t.receiverDecoder = t.receiver.UnmarshalBinary + t.receiverEncoder = t.receiver.MarshalBinary + + return t +} + +func hexTest(t *encodingTest) *encodingTest { + t.sourceEncoder = hexToEncoder(t.source) + t.receiverDecoder = hexToDecoder(t.receiver) + t.receiverEncoder = hexToEncoder(t.receiver) + + return t +} + +func jsonTest(t *encodingTest) *encodingTest { + t.sourceEncoder = t.source.MarshalJSON + t.receiverDecoder = t.receiver.UnmarshalJSON + t.receiverEncoder = t.receiver.MarshalJSON + + return t +} + +func (t *encodingTest) run() error { + encoded, err := t.sourceEncoder() if err != nil { - t.Fatal(err) + return err } - if !bytes.Equal(encoded, marshalled) { - t.Fatalf("Encode() and MarshalBinary() are expected to have the same output."+ - "\twant: %v\tgot : %v", encoded, marshalled) + if err = t.receiverDecoder(encoded); err != nil { + return fmt.Errorf("%v. Value: %v", err, hex.EncodeToString(encoded)) } - if hex.EncodeToString(encoded) != hexed { - t.Fatalf("Failed hex encoding, want %q, got %q", hex.EncodeToString(encoded), hexed) + encoded2, err := t.receiverEncoder() + if err != nil { + return err } - if err := thing2.Decode(nil); err == nil { - t.Fatal("expected error on Decode() with nil input") + if !bytes.Equal(encoded, encoded2) { + return fmt.Errorf("re-decoding of same source does not yield the same results.\n\twant: %v\n\tgot : %s\n", + encoded, encoded2) } - if err := thing2.Decode(encoded); err != nil { - t.Fatalf("Decode() failed on a valid encoding: %v. Value: %v", err, hex.EncodeToString(encoded)) + return nil +} + +func testScalarEncodings(g crypto.Group, f makeEncodeTest) error { + source, receiver := g.NewScalar().Random(), g.NewScalar() + t := newEncodingTest(source, receiver) + + if err := f(t).run(); err != nil { + return err } - if err := thing2.UnmarshalJSON(jsoned); err != nil { - t.Fatalf("UnmarshalJSON() failed on a valid encoding: %v", err) + if source.Equal(receiver) != 1 { + return errors.New(errExpectedEquality) } - if err := thing2.UnmarshalBinary(encoded); err != nil { - t.Fatalf("UnmarshalBinary() failed on a valid encoding: %v", err) + return nil +} + +func testElementEncodings(g crypto.Group, f makeEncodeTest) error { + source, receiver := g.Base(), g.NewElement() + t := newEncodingTest(source, receiver) + + if err := f(t).run(); err != nil { + return err } - if err := thing2.DecodeHex(hexed); err != nil { - t.Fatalf("DecodeHex() failed on valid hex encoding: %v", err) + if source.Equal(receiver) != 1 { + return errors.New(errExpectedEquality) } + + return nil } func TestEncoding(t *testing.T) { - testAll(t, func(group *testGroup) { + testAllGroups(t, func(group *testGroup) { g := group.group - scalar := g.NewScalar().Random() - testEncoding(t, scalar, g.NewScalar()) + testDecodeEmpty(t, group.group.NewScalar().Random()) + for _, tester := range encodeTesters { + if err := testScalarEncodings(g, tester); err != nil { + t.Fatal() + } + } + }) +} - scalar = g.NewScalar().Random() - element := g.Base().Multiply(scalar) - testEncoding(t, element, g.NewElement()) +func TestEncoding_Element(t *testing.T) { + testAllGroups(t, func(group *testGroup) { + g := group.group + testDecodeEmpty(t, group.group.Base()) + for _, tester := range encodeTesters { + if err := testElementEncodings(g, tester); err != nil { + t.Fatal() + } + } }) } +func testDecodeEmpty(t *testing.T, s serde) { + if err := s.Decode(nil); err == nil { + t.Fatal("expected error on Decode() with nil input") + } + + if err := s.Decode([]byte{}); err == nil { + t.Fatal("expected error on Decode() with empty input") + } + + if err := s.(encoding.BinaryUnmarshaler).UnmarshalBinary(nil); err == nil { + t.Fatal("expected error on UnmarshalBinary() with nil input") + } + + if err := s.(encoding.BinaryUnmarshaler).UnmarshalBinary([]byte{}); err == nil { + t.Fatal("expected error on UnmarshalBinary() with empty input") + } + + if err := s.DecodeHex(""); err == nil { + t.Fatal("expected error on empty string") + } + + if err := json.Unmarshal(nil, s); err == nil { + t.Fatal("expected error") + } + + if err := json.Unmarshal([]byte{}, s); err == nil { + t.Fatal("expected error") + } +} + func testDecodingHexFails(t *testing.T, thing1, thing2 serde) { // empty string if err := thing2.DecodeHex(""); err == nil { @@ -103,14 +232,10 @@ func testDecodingHexFails(t *testing.T, thing1, thing2 serde) { } func TestEncoding_Hex_Fails(t *testing.T) { - testAll(t, func(group *testGroup) { + testAllGroups(t, func(group *testGroup) { g := group.group scalar := g.NewScalar().Random() - testEncoding(t, scalar, g.NewScalar()) - - scalar = g.NewScalar().Random() element := g.Base().Multiply(scalar) - testEncoding(t, element, g.NewElement()) // Hex fails testDecodingHexFails(t, scalar, g.NewScalar()) diff --git a/tests/groups_test.go b/tests/groups_test.go index 468a7f2..92f3e60 100644 --- a/tests/groups_test.go +++ b/tests/groups_test.go @@ -20,7 +20,7 @@ import ( const consideredAvailableFmt = "%v is considered available when it must not" func TestAvailability(t *testing.T) { - testAll(t, func(group *testGroup) { + testAllGroups(t, func(group *testGroup) { if !group.group.Available() { t.Errorf("'%s' is not available, but should be", group.group.String()) } @@ -63,7 +63,7 @@ func TestNonAvailability(t *testing.T) { } func TestGroup_Base(t *testing.T) { - testAll(t, func(group *testGroup) { + testAllGroups(t, func(group *testGroup) { if group.group.Base().Hex() != group.basePoint { t.Fatalf("Got wrong base element\n\tgot : %s\n\twant: %s", group.group.Base().Hex(), @@ -84,7 +84,7 @@ func TestDST(t *testing.T) { crypto.Secp256k1: app + "-V01-CS07-", } - testAll(t, func(group *testGroup) { + testAllGroups(t, func(group *testGroup) { res := string(group.group.MakeDST(app, version)) test := tests[group.group] + group.h2c if res != test { @@ -94,7 +94,7 @@ func TestDST(t *testing.T) { } func TestGroup_String(t *testing.T) { - testAll(t, func(group *testGroup) { + testAllGroups(t, func(group *testGroup) { res := group.group.String() ref := group.h2c if res != ref { @@ -104,7 +104,7 @@ func TestGroup_String(t *testing.T) { } func TestGroup_NewScalar(t *testing.T) { - testAll(t, func(group *testGroup) { + testAllGroups(t, func(group *testGroup) { s := group.group.NewScalar().Encode() for _, b := range s { if b != 0 { @@ -115,7 +115,7 @@ func TestGroup_NewScalar(t *testing.T) { } func TestGroup_NewElement(t *testing.T) { - testAll(t, func(group *testGroup) { + testAllGroups(t, func(group *testGroup) { e := hex.EncodeToString(group.group.NewElement().Encode()) ref := group.identity @@ -126,7 +126,7 @@ func TestGroup_NewElement(t *testing.T) { } func TestGroup_ScalarLength(t *testing.T) { - testAll(t, func(group *testGroup) { + testAllGroups(t, func(group *testGroup) { if int(group.group.ScalarLength()) != group.scalarLength { t.Fatalf("expected encoded scalar length %d, but got %d", group.scalarLength, group.group.ScalarLength()) } @@ -134,7 +134,7 @@ func TestGroup_ScalarLength(t *testing.T) { } func TestGroup_ElementLength(t *testing.T) { - testAll(t, func(group *testGroup) { + testAllGroups(t, func(group *testGroup) { if group.group.ElementLength() != group.elementLength { t.Fatalf("expected encoded element length %d, but got %d", group.elementLength, group.group.ElementLength()) } @@ -142,7 +142,7 @@ func TestGroup_ElementLength(t *testing.T) { } func TestHashFunc(t *testing.T) { - testAll(t, func(group *testGroup) { + testAllGroups(t, func(group *testGroup) { if group.group.HashFunc() != group.hash { t.Error(errExpectedEquality) } @@ -150,7 +150,7 @@ func TestHashFunc(t *testing.T) { } func TestHashToScalar(t *testing.T) { - testAll(t, func(group *testGroup) { + testAllGroups(t, func(group *testGroup) { sv := decodeScalar(t, group.group, group.hashToCurve.hashToScalar) s := group.group.HashToScalar(group.hashToCurve.input, group.hashToCurve.dst) @@ -161,7 +161,7 @@ func TestHashToScalar(t *testing.T) { } func TestHashToScalar_NoDST(t *testing.T) { - testAll(t, func(group *testGroup) { + testAllGroups(t, func(group *testGroup) { data := []byte("input data") // Nil DST @@ -181,7 +181,7 @@ func TestHashToScalar_NoDST(t *testing.T) { } func TestHashToGroup(t *testing.T) { - testAll(t, func(group *testGroup) { + testAllGroups(t, func(group *testGroup) { ev := decodeElement(t, group.group, group.hashToCurve.hashToGroup) e := group.group.HashToGroup(group.hashToCurve.input, group.hashToCurve.dst) @@ -192,7 +192,7 @@ func TestHashToGroup(t *testing.T) { } func TestHashToGroup_NoDST(t *testing.T) { - testAll(t, func(group *testGroup) { + testAllGroups(t, func(group *testGroup) { data := []byte("input data") // Nil DST diff --git a/tests/scalar_test.go b/tests/scalar_test.go index 31d0661..d37a3b9 100644 --- a/tests/scalar_test.go +++ b/tests/scalar_test.go @@ -35,7 +35,7 @@ func TestScalar_WrongInput(t *testing.T) { } } - testAll(t, func(group *testGroup) { + testAllGroups(t, func(group *testGroup) { scalar := group.group.NewScalar() methods := []func(arg *crypto.Scalar) *crypto.Scalar{ scalar.Add, scalar.Subtract, scalar.Multiply, scalar.Set, @@ -100,7 +100,7 @@ func testScalarCopySet(t *testing.T, scalar, other *crypto.Scalar) { } func TestScalarCopy(t *testing.T) { - testAll(t, func(group *testGroup) { + testAllGroups(t, func(group *testGroup) { random := group.group.NewScalar().Random() cpy := random.Copy() testScalarCopySet(t, random, cpy) @@ -108,7 +108,7 @@ func TestScalarCopy(t *testing.T) { } func TestScalarSet(t *testing.T) { - testAll(t, func(group *testGroup) { + testAllGroups(t, func(group *testGroup) { random := group.group.NewScalar().Random() other := group.group.NewScalar() other.Set(random) @@ -148,7 +148,7 @@ func testScalarUInt64(t *testing.T, s *crypto.Scalar, expectedValue uint64, expe func TestScalar_UInt64(t *testing.T) { expectedError := errors.New("scalar is too big to be uint64") - testAll(t, func(group *testGroup) { + testAllGroups(t, func(group *testGroup) { // 0 testScalarUInt64(t, group.group.NewScalar(), 0, nil) @@ -169,7 +169,7 @@ func TestScalar_UInt64(t *testing.T) { } func TestScalar_SetUInt64(t *testing.T) { - testAll(t, func(group *testGroup) { + testAllGroups(t, func(group *testGroup) { s := group.group.NewScalar().SetUInt64(0) if !s.IsZero() { t.Fatal("expected 0") @@ -198,7 +198,7 @@ func TestScalar_SetUInt64(t *testing.T) { } func TestScalar_EncodedLength(t *testing.T) { - testAll(t, func(group *testGroup) { + testAllGroups(t, func(group *testGroup) { encodedScalar := group.group.NewScalar().Random().Encode() if len(encodedScalar) != group.scalarLength { t.Fatalf( @@ -211,7 +211,7 @@ func TestScalar_EncodedLength(t *testing.T) { } func TestScalar_Decode_OutOfBounds(t *testing.T) { - testAll(t, func(group *testGroup) { + testAllGroups(t, func(group *testGroup) { decodeErrPrefix := "scalar Decode: " unmarshallBinaryErrPrefix := "scalar UnmarshalBinary: " switch group.group { @@ -265,7 +265,7 @@ func TestScalar_Decode_OutOfBounds(t *testing.T) { } func TestScalar_Arithmetic(t *testing.T) { - testAll(t, func(group *testGroup) { + testAllGroups(t, func(group *testGroup) { scalarTestZero(t, group.group) scalarTestOne(t, group.group) scalarTestEqual(t, group.group) diff --git a/tests/table_test.go b/tests/table_test.go index 36fad50..3bb1252 100644 --- a/tests/table_test.go +++ b/tests/table_test.go @@ -15,7 +15,7 @@ import ( group "github.com/bytemare/crypto" ) -func testAll(t *testing.T, f func(*testGroup)) { +func testAllGroups(t *testing.T, f func(*testGroup)) { for _, test := range testTable { t.Run(test.name, func(t *testing.T) { f(test)