Skip to content

Commit

Permalink
Test for the first two phases of GJKR protocol
Browse files Browse the repository at this point in the history
The test executes phase 1 and phase 2 and ensures the expected number of
symmetric keys has been generated.
  • Loading branch information
pdyraga committed Oct 25, 2024
1 parent 8449f5d commit c8a5ae4
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 1 deletion.
28 changes: 27 additions & 1 deletion gjkr/member.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,30 @@ type member struct {
memberIndex memberIndex
sessionID string
group *group
evidenceLog evidenceLog
evidenceLog *evidenceLog

logger Logger
}

func newEphemeralKeyPairGeneratingMember(
index memberIndex,
sessionID string,
dishonestThreshold uint16,
groupSize uint16,
logger Logger,
) *ephemeralKeyPairGeneratingMember {
return &ephemeralKeyPairGeneratingMember{
member: &member{
memberIndex: index,
sessionID: sessionID,
group: newGroup(dishonestThreshold, groupSize),
evidenceLog: newEvidenceLog(),
logger: logger,
},
ephemeralKeyPairs: make(map[memberIndex]*ephemeral.KeyPair),
}
}

// ephemeralKeyPairGeneratingMember represents one member in a distributed key
// generating group performing ephemeral key pair generation.
//
Expand All @@ -30,6 +49,13 @@ type ephemeralKeyPairGeneratingMember struct {
ephemeralKeyPairs map[memberIndex]*ephemeral.KeyPair
}

func (e *ephemeralKeyPairGeneratingMember) next() *symmetricKeyGeneratingMember {
return &symmetricKeyGeneratingMember{
ephemeralKeyPairGeneratingMember: e,
symmetricKeys: make(map[memberIndex]ephemeral.SymmetricKey),
}
}

// symmetricKeyGeneratingMember represents one member in a distributed key
// generating group performing ephemeral symmetric key generation.
//
Expand Down
99 changes: 99 additions & 0 deletions gjkr/protocol_ecdh_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package gjkr

import (
"fmt"
"reflect"
"testing"

"threshold.network/roast/internal/testutils"
)

func TestGenerateSymmetricKeys(t *testing.T) {
groupSize := 100

members := initializeGroup(70, uint16(groupSize))
ephemeralPublicKeyMessages, symmetricKeyGeneratingMembers := executePhase1(
t,
members,
)
executePhase2(t, symmetricKeyGeneratingMembers, ephemeralPublicKeyMessages)

// Ensure that for each member, we generated the correct number of
// symmetric keys (groupSize - 1 keys)
for _, member := range symmetricKeyGeneratingMembers {
symmetricKeys := member.symmetricKeys
keySlice := reflect.ValueOf(symmetricKeys).MapKeys()
testutils.AssertIntsEqual(
t,
"number of generated symmetric keys",
groupSize-1,
len(keySlice),
)
}
}

type mockLogger struct{}

func (ml *mockLogger) Info(msg string, keyVals ...any) {
fmt.Printf("INFO: %s {%v}", msg, keyVals)
}

func (ml *mockLogger) Warn(msg string, keyVals ...any) {
fmt.Printf("WARN: %s {%v}", msg, keyVals)
}

func (ml *mockLogger) Error(msg string, keyVals ...any) {
fmt.Printf("ERROR: %s {%v}", msg, keyVals)
}

func initializeGroup(
dishonestThreshold uint16,
groupSize uint16,
) []*ephemeralKeyPairGeneratingMember {
members := make([]*ephemeralKeyPairGeneratingMember, 0)

for idx := uint16(1); idx <= groupSize; idx++ {
members = append(members, newEphemeralKeyPairGeneratingMember(
memberIndex(idx),
"test-dkg-session",
dishonestThreshold,
groupSize,
&mockLogger{},
))
}

return members
}

func executePhase1(
t *testing.T,
members []*ephemeralKeyPairGeneratingMember,
) ([]*ephemeralPublicKeyMessage, []*symmetricKeyGeneratingMember) {
messages := make([]*ephemeralPublicKeyMessage, len(members))
nextMembers := make([]*symmetricKeyGeneratingMember, len(members))

for i, member := range members {
message, err := member.generateEphemeralKeyPair()
if err != nil {
t.Fatal(err)
}
messages[i] = message

nextMembers[i] = member.next()
}

return messages, nextMembers
}

func executePhase2(
t *testing.T,
members []*symmetricKeyGeneratingMember,
messages []*ephemeralPublicKeyMessage,
) {
for _, member := range members {
err := member.generateSymmetricKeys(messages)
if err != nil {
t.Fatal(err)
}
}
}

0 comments on commit c8a5ae4

Please sign in to comment.