Skip to content

Commit

Permalink
return standard compressed encoding length for nist infinity points (#36
Browse files Browse the repository at this point in the history
)

Signed-off-by: bytemare <3641580+bytemare@users.noreply.github.com>
  • Loading branch information
bytemare committed Mar 18, 2023
1 parent 409260c commit de52f48
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
29 changes: 28 additions & 1 deletion internal/nist/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import (
"github.com/bytemare/crypto/internal"
)

const (
p256CompressedEncodingLength = 33
p384CompressedEncodingLength = 49
p521CompressedEncodingLength = 67
)

// Element implements the Element interface for group elements over NIST curves.
type Element[Point nistECPoint[Point]] struct {
p Point
Expand Down Expand Up @@ -159,13 +165,34 @@ func (e *Element[P]) Copy() internal.Element {

// Encode returns the compressed byte encoding of the element.
func (e *Element[P]) Encode() []byte {
if e.IsIdentity() {
return encodeInfinity(e)
}

return e.p.BytesCompressed()
}

func encodeInfinity[Point nistECPoint[Point]](element *Element[Point]) []byte {
_, err := element.p.BytesX()
var encodedLength int

switch err.Error()[:4] {
case "P256":
encodedLength = p256CompressedEncodingLength
case "P384":
encodedLength = p384CompressedEncodingLength
case "P521":
encodedLength = p521CompressedEncodingLength
}

return make([]byte, encodedLength)
}

// XCoordinate returns the encoded x coordinate of the element.
func (e *Element[P]) XCoordinate() []byte {
if e.IsIdentity() {
return e.new().BytesCompressed()
inf := encodeInfinity(e)
return inf[:len(inf)-1]
}

b, err := e.p.BytesX()
Expand Down
11 changes: 11 additions & 0 deletions tests/element_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package group_test

import (
"encoding/hex"
"testing"

"github.com/bytemare/crypto"
Expand Down Expand Up @@ -119,6 +120,16 @@ func TestElement_WrongInput(t *testing.T) {

func TestElement_EncodedLength(t *testing.T) {
testAll(t, func(t2 *testing.T, group *testGroup) {
id := group.id.NewElement().Identity().Encode()
if len(id) != group.elementLength {
t.Fatalf("Encode() of the identity element is expected to return %d bytes, but returned %d bytes", group.elementLength, len(id))
}

encodedID := hex.EncodeToString(id)
if encodedID != group.identity {
t.Fatalf("Encode() of the identity element is unexpected.\n\twant: %v\n\tgot : %v", group.identity, encodedID)
}

encodedElement := group.id.NewElement().Base().Multiply(group.id.NewScalar().Random()).Encode()
if len(encodedElement) != group.elementLength {
t.Fatalf("Encode() is expected to return %d bytes, but returned %d bytes", group.elementLength, encodedElement)
Expand Down
6 changes: 3 additions & 3 deletions tests/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func testGroups() []*testGroup {
"P256_XMD:SHA-256_SSWU_RO_",
"P256_XMD:SHA-256_SSWU_NU_",
"036b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296",
"00",
"000000000000000000000000000000000000000000000000000000000000000000",
33,
32,
3,
Expand All @@ -57,7 +57,7 @@ func testGroups() []*testGroup {
"P384_XMD:SHA-384_SSWU_RO_",
"P384_XMD:SHA-384_SSWU_NU_",
"03aa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7",
"00",
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
49,
48,
4,
Expand All @@ -67,7 +67,7 @@ func testGroups() []*testGroup {
"P521_XMD:SHA-512_SSWU_RO_",
"P521_XMD:SHA-512_SSWU_NU_",
"0200c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66",
"00",
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
67,
66,
5,
Expand Down

0 comments on commit de52f48

Please sign in to comment.