Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

Commit

Permalink
Fix JSON serde, refactored encoding tests, fix some documentation
Browse files Browse the repository at this point in the history
Signed-off-by: bytemare <[email protected]>
  • Loading branch information
bytemare committed Jun 15, 2024
1 parent ef66054 commit e29fa44
Show file tree
Hide file tree
Showing 19 changed files with 238 additions and 122 deletions.
31 changes: 16 additions & 15 deletions .github/.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ linters:
- dogsled
- dupl
- durationcheck
- err113
- errcheck
- errchkjson
- errname
- errorlint
- execinquery
#- exhaustive
#- exhaustruct
- exportloopref
- forbidigo
- forcetypeassert
- funlen
- funlencd
- gci
#- gochecknoglobals
#- gochecknoinits
Expand All @@ -33,7 +33,6 @@ linters:
- gocyclo
- godot
- godox
- goerr113
- gofmt
- gofumpt
- goheader
Expand All @@ -51,7 +50,7 @@ linters:
#- interfacebloat
#- ireturn
- lll
- logrlint
- loggercheck
- maintidx
- makezero
- misspell
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
13 changes: 5 additions & 8 deletions element.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package crypto

import (
"fmt"
"strings"

"github.com/bytemare/crypto/internal"
)
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 0 additions & 4 deletions groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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")
}
Expand Down
4 changes: 2 additions & 2 deletions internal/edwards25519/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions internal/edwards25519/scalar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions internal/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions internal/nist/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions internal/nist/scalar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions internal/ristretto/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions internal/ristretto/scalar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions internal/scalar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions internal/secp256k1/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions internal/secp256k1/scalar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 5 additions & 8 deletions scalar.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package crypto

import (
"fmt"
"strings"

"github.com/bytemare/crypto/internal"
)
Expand Down Expand Up @@ -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.
Expand Down
20 changes: 10 additions & 10 deletions tests/element_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ 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)
})
}

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)
Expand All @@ -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

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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 := ""
Expand Down Expand Up @@ -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

Expand All @@ -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()

Expand All @@ -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},
Expand All @@ -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()

Expand All @@ -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)
Expand Down
Loading

0 comments on commit e29fa44

Please sign in to comment.