Skip to content

Commit

Permalink
Fix JSON serde, refactored encoding tests, fix some documentation (#62)
Browse files Browse the repository at this point in the history
* Fix JSON serde, refactored encoding tests, fix some documentation

Signed-off-by: bytemare <3641580+bytemare@users.noreply.github.com>

---------

Signed-off-by: bytemare <3641580+bytemare@users.noreply.github.com>
  • Loading branch information
bytemare committed Jun 15, 2024
1 parent ef66054 commit 8592b9b
Show file tree
Hide file tree
Showing 19 changed files with 229 additions and 230 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
15 changes: 6 additions & 9 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,27 +149,23 @@ 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.Encode(), nil
}

// UnmarshalBinary sets e to the decoding of the byte encoded element.
func (e *Element) UnmarshalBinary(data []byte) error {
if err := e.Element.UnmarshalBinary(data); err != nil {
if err := e.Element.Decode(data); err != nil {
return fmt.Errorf("element UnmarshalBinary: %w", err)
}

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
10 changes: 0 additions & 10 deletions internal/edwards25519/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,3 @@ func (e *Element) DecodeHex(h string) error {

return e.Decode(b)
}

// MarshalBinary returns the compressed byte encoding of the element.
func (e *Element) MarshalBinary() (data []byte, err error) {
return e.Encode(), nil
}

// UnmarshalBinary sets e to the decoding of the byte encoded element.
func (e *Element) UnmarshalBinary(data []byte) error {
return e.Decode(data)
}
14 changes: 0 additions & 14 deletions internal/edwards25519/scalar.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,17 +360,3 @@ func (s *Scalar) DecodeHex(h string) error {

return s.Decode(b)
}

// MarshalBinary returns the compressed byte encoding of the scalar.
func (s *Scalar) MarshalBinary() (data []byte, err error) {
return s.Encode(), nil
}

// UnmarshalBinary sets e to the decoding of the byte encoded scalar.
func (s *Scalar) UnmarshalBinary(data []byte) error {
if err := s.Decode(data); err != nil {
return fmt.Errorf("edwards25519: %w", err)
}

return nil
}
10 changes: 0 additions & 10 deletions internal/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
// Package internal defines simple and abstract APIs to group Elements and Scalars.
package internal

import (
"encoding"
)

// Element interface abstracts common operations on an Element in a prime-order Group.
type Element interface {
// Base sets the element to the group's base point a.k.a. canonical generator.
Expand Down Expand Up @@ -62,10 +58,4 @@ type Element interface {

// DecodeHex sets e to the decoding of the hex encoded element.
DecodeHex(h string) error

// BinaryMarshaler implementation.
encoding.BinaryMarshaler

// BinaryUnmarshaler implementation.
encoding.BinaryUnmarshaler
}
10 changes: 0 additions & 10 deletions internal/nist/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,3 @@ func (e *Element[P]) DecodeHex(h string) error {

return e.Decode(b)
}

// MarshalBinary returns the compressed byte encoding of the element.
func (e *Element[P]) MarshalBinary() ([]byte, error) {
return e.Encode(), nil
}

// UnmarshalBinary sets e to the decoding of the byte encoded element.
func (e *Element[P]) UnmarshalBinary(data []byte) error {
return e.Decode(data)
}
14 changes: 0 additions & 14 deletions internal/nist/scalar.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,17 +265,3 @@ func (s *Scalar) DecodeHex(h string) error {

return s.Decode(b)
}

// MarshalBinary returns the compressed byte encoding of the scalar.
func (s *Scalar) MarshalBinary() ([]byte, error) {
return s.Encode(), nil
}

// UnmarshalBinary sets e to the decoding of the byte encoded scalar.
func (s *Scalar) UnmarshalBinary(data []byte) error {
if err := s.Decode(data); err != nil {
return fmt.Errorf("nist: %w", err)
}

return nil
}
10 changes: 0 additions & 10 deletions internal/ristretto/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,3 @@ func (e *Element) DecodeHex(h string) error {

return e.Decode(b)
}

// MarshalBinary returns the compressed byte encoding of the element.
func (e *Element) MarshalBinary() ([]byte, error) {
return e.Encode(), nil
}

// UnmarshalBinary sets e to the decoding of the byte encoded element.
func (e *Element) UnmarshalBinary(data []byte) error {
return e.Decode(data)
}
14 changes: 0 additions & 14 deletions internal/ristretto/scalar.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,17 +350,3 @@ func (s *Scalar) DecodeHex(h string) error {

return s.Decode(b)
}

// MarshalBinary returns the compressed byte encoding of the scalar.
func (s *Scalar) MarshalBinary() ([]byte, error) {
return s.Encode(), nil
}

// UnmarshalBinary sets e to the decoding of the byte encoded scalar.
func (s *Scalar) UnmarshalBinary(data []byte) error {
if err := s.Decode(data); err != nil {
return fmt.Errorf("ristretto: %w", err)
}

return nil
}
11 changes: 0 additions & 11 deletions internal/scalar.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
// Package internal defines simple and abstract APIs to group Elements and Scalars.
package internal

import (
"encoding"
)

// Scalar interface abstracts common operations on scalars in a prime-order Group.
type Scalar interface {
// Zero sets the scalar to 0, and returns it.
Expand Down Expand Up @@ -73,11 +69,4 @@ 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

// BinaryUnmarshaler recovers an element from a byte representation
// produced either by encoding.BinaryMarshaler or MarshalBinaryCompress.
encoding.BinaryUnmarshaler
}
10 changes: 0 additions & 10 deletions internal/secp256k1/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,3 @@ func (e *Element) DecodeHex(h string) error {

return e.Decode(b)
}

// MarshalBinary returns the compressed byte encoding of the element.
func (e *Element) MarshalBinary() (data []byte, err error) {
return e.Encode(), nil
}

// UnmarshalBinary sets e to the decoding of the byte encoded element.
func (e *Element) UnmarshalBinary(data []byte) error {
return e.Decode(data)
}
10 changes: 0 additions & 10 deletions internal/secp256k1/scalar.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,3 @@ func (s *Scalar) DecodeHex(h string) error {

return nil
}

// MarshalBinary returns the compressed byte encoding of the scalar.
func (s *Scalar) MarshalBinary() (data []byte, err error) {
return s.Encode(), nil
}

// UnmarshalBinary sets e to the decoding of the byte encoded scalar.
func (s *Scalar) UnmarshalBinary(data []byte) error {
return s.Decode(data)
}
15 changes: 6 additions & 9 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,27 +181,23 @@ 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.Encode(), nil
}

// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
func (s *Scalar) UnmarshalBinary(data []byte) error {
if err := s.Scalar.UnmarshalBinary(data); err != nil {
if err := s.Scalar.Decode(data); err != nil {
return fmt.Errorf("scalar UnmarshalBinary: %w", err)
}

Expand Down
Loading

0 comments on commit 8592b9b

Please sign in to comment.