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

Commit

Permalink
Add Group.HashFunc() to return hash function (#59)
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 authored Jun 13, 2024
1 parent 3bec1a4 commit f256838
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Group interface {
NewScalar() Scalar
NewElement() Element
Base() Element
HashFunc() crypto.Hash
HashToScalar(input, dst []byte) Scalar
HashToGroup(input, dst []byte) Element
EncodeToGroup(input, dst []byte) Element
Expand Down
6 changes: 6 additions & 0 deletions groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package crypto

import (
"crypto"
"errors"
"fmt"
"sync"
Expand Down Expand Up @@ -113,6 +114,11 @@ func checkDST(dst []byte) {
}
}

// HashFunc returns the RFC9380 associated hash function of the group.
func (g Group) HashFunc() crypto.Hash {
return g.get().HashFunc()
}

// HashToScalar returns a safe mapping of the arbitrary input to a Scalar.
// The DST must not be empty or nil, and is recommended to be longer than 16 bytes.
func (g Group) HashToScalar(input, dst []byte) *Scalar {
Expand Down
7 changes: 7 additions & 0 deletions internal/edwards25519/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
package edwards25519

import (
"crypto"

ed "filippo.io/edwards25519"

"github.com/bytemare/crypto/internal"
Expand Down Expand Up @@ -43,6 +45,11 @@ func (g Group) Base() internal.Element {
return &Element{*ed.NewGeneratorPoint()}
}

// HashFunc returns the RFC9380 associated hash function of the group.
func (g Group) HashFunc() crypto.Hash {
return crypto.SHA512
}

// HashToScalar returns a safe mapping of the arbitrary input to a Scalar.
// The DST must not be empty or nil, and is recommended to be longer than 16 bytes.
func (g Group) HashToScalar(input, dst []byte) internal.Scalar {
Expand Down
5 changes: 5 additions & 0 deletions internal/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
// Package internal defines simple and abstract APIs to group Elements and Scalars.
package internal

import "crypto"

// Group abstracts operations in a prime-order group.
type Group interface {
// NewScalar returns a new scalar set to 0.
Expand All @@ -20,6 +22,9 @@ type Group interface {
// Base returns the group's base point a.k.a. canonical generator.
Base() Element

// HashFunc returns the RFC9380 associated hash function of the group.
HashFunc() crypto.Hash

// HashToScalar returns a safe mapping of the arbitrary input to a Scalar.
// The DST must not be empty or nil, and is recommended to be longer than 16 bytes.
HashToScalar(input, dst []byte) Scalar
Expand Down
5 changes: 5 additions & 0 deletions internal/nist/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ func (g Group[P]) newPoint(p P) *Element[P] {
}
}

// HashFunc returns the RFC9380 associated hash function of the group.
func (g Group[P]) HashFunc() crypto.Hash {
return g.curve.hash
}

// HashToScalar returns a safe mapping of the arbitrary input to a Scalar.
// The DST must not be empty or nil, and is recommended to be longer than 16 bytes.
func (g Group[P]) HashToScalar(input, dst []byte) internal.Scalar {
Expand Down
5 changes: 5 additions & 0 deletions internal/ristretto/ristretto.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ func (g Group) Base() internal.Element {
return &Element{*ristretto255.NewElement().Base()}
}

// HashFunc returns the RFC9380 associated hash function of the group.
func (g Group) HashFunc() crypto.Hash {
return crypto.SHA512
}

// HashToScalar returns a safe mapping of the arbitrary input to a Scalar.
// The DST must not be empty or nil, and is recommended to be longer than 16 bytes.
func (g Group) HashToScalar(input, dst []byte) internal.Scalar {
Expand Down
7 changes: 7 additions & 0 deletions internal/secp256k1/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
package secp256k1

import (
"crypto"

"github.com/bytemare/secp256k1"

"github.com/bytemare/crypto/internal"
Expand Down Expand Up @@ -50,6 +52,11 @@ func (g Group) Base() internal.Element {
return newElement().Base()
}

// HashFunc returns the RFC9380 associated hash function of the group.
func (g Group) HashFunc() crypto.Hash {
return crypto.SHA256
}

// HashToScalar returns a safe mapping of the arbitrary input to a Scalar.
// The DST must not be empty or nil, and is recommended to be longer than 16 bytes.
func (g Group) HashToScalar(input, dst []byte) internal.Scalar {
Expand Down
8 changes: 8 additions & 0 deletions tests/groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ func TestGroup_ElementLength(t *testing.T) {
})
}

func TestHashFunc(t *testing.T) {
testAll(t, func(group *testGroup) {
if group.group.HashFunc() != group.hash {
t.Error(errExpectedEquality)
}
})
}

func TestHashToScalar(t *testing.T) {
testAll(t, func(group *testGroup) {
sv := decodeScalar(t, group.group, group.hashToCurve.hashToScalar)
Expand Down
12 changes: 10 additions & 2 deletions tests/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
package group_test

import (
"crypto"
"testing"

"github.com/bytemare/crypto"
group "github.com/bytemare/crypto"
)

func testAll(t *testing.T, f func(*testGroup)) {
Expand Down Expand Up @@ -47,7 +48,8 @@ type testGroup struct {
hashToCurve testHashToCurve
elementLength int
scalarLength int
group crypto.Group
group group.Group
hash crypto.Hash
}

var testTable = []*testGroup{
Expand Down Expand Up @@ -85,6 +87,7 @@ var testTable = []*testGroup{
32,
32,
1,
crypto.SHA512,
},
{
[15]string{
Expand Down Expand Up @@ -120,6 +123,7 @@ var testTable = []*testGroup{
33,
32,
3,
crypto.SHA256,
},
{
[15]string{
Expand Down Expand Up @@ -155,6 +159,7 @@ var testTable = []*testGroup{
49,
48,
4,
crypto.SHA384,
},
{
[15]string{
Expand Down Expand Up @@ -190,6 +195,7 @@ var testTable = []*testGroup{
67,
66,
5,
crypto.SHA512,
},
{
[15]string{
Expand Down Expand Up @@ -225,6 +231,7 @@ var testTable = []*testGroup{
32,
32,
6,
crypto.SHA512,
},
{
[15]string{
Expand Down Expand Up @@ -260,5 +267,6 @@ var testTable = []*testGroup{
33,
32,
7,
crypto.SHA256,
},
}
5 changes: 3 additions & 2 deletions tests/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"encoding/hex"
"errors"
"fmt"
"strings"
"testing"

"github.com/bytemare/crypto"
Expand Down Expand Up @@ -166,8 +167,8 @@ func testDecodingHexFails(t *testing.T, thing1, thing2 serde) {

if err := thing2.DecodeHex(string(malformed)); err == nil {
t.Fatal("expected error on malformed string")
} else {
t.Log(err)
} else if !strings.HasSuffix(err.Error(), "DecodeHex: encoding/hex: invalid byte: U+005F '_'") {
t.Fatalf("unexpected error: %q", err)
}
}

Expand Down

0 comments on commit f256838

Please sign in to comment.