-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test for the first two phases of GJKR protocol
The test executes phase 1 and phase 2 and ensures the expected number of symmetric keys has been generated.
- Loading branch information
Showing
2 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |