This repository has been archived by the owner on Dec 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 536
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract BLS library used in polybft to a standalone package (#1981)
* Introduce bls package * Remove MarshalMessageToBigInt (unused) * Use bls package in polybft consensus * Adopt changes from #1973 * Remove package alias for bls package * Revert GeneratePrivateKey rename * Revive Test_MakeKOSKSignature and Test_AggregatedSign * Rebase fix * Fix TestAccount * Reorder imports
- Loading branch information
1 parent
96a5d55
commit 34e08f1
Showing
45 changed files
with
310 additions
and
276 deletions.
There are no files selected for viewing
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
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
File renamed without changes.
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,66 @@ | ||
package bls | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_SingleSign(t *testing.T) { | ||
t.Parallel() | ||
|
||
validTestMsg, invalidTestMsg := testGenRandomBytes(t, messageSize), testGenRandomBytes(t, messageSize) | ||
|
||
blsKey, err := GenerateBlsKey() // structure which holds private/public key pair | ||
require.NoError(t, err) | ||
|
||
// Sign valid message | ||
signature, err := blsKey.Sign(validTestMsg, expectedDomain) | ||
require.NoError(t, err) | ||
|
||
isOk := signature.Verify(blsKey.PublicKey(), validTestMsg, expectedDomain) | ||
assert.True(t, isOk) | ||
|
||
// Verify if invalid message is signed with correct private key. Only use public key for the verification | ||
// this should fail => isOk = false | ||
isOk = signature.Verify(blsKey.PublicKey(), invalidTestMsg, unexpectedDomain) | ||
assert.False(t, isOk) | ||
} | ||
|
||
func Test_AggregatedSign(t *testing.T) { | ||
t.Parallel() | ||
|
||
validTestMsg, invalidTestMsg := testGenRandomBytes(t, messageSize), testGenRandomBytes(t, messageSize) | ||
|
||
keys, err := CreateRandomBlsKeys(participantsNumber) // create keys for validators | ||
require.NoError(t, err) | ||
|
||
pubKeys := make([]*PublicKey, len(keys)) | ||
|
||
for i, key := range keys { | ||
pubKeys[i] = key.PublicKey() | ||
} | ||
|
||
signatures := Signatures{} | ||
|
||
// test all signatures at once | ||
for i := 0; i < len(keys); i++ { | ||
sign, err := keys[i].Sign(validTestMsg, expectedDomain) | ||
require.NoError(t, err) | ||
|
||
signatures = append(signatures, sign) | ||
|
||
// verify correctness of AggregateSignature | ||
aggSig := signatures.Aggregate() | ||
|
||
isOk := aggSig.VerifyAggregated(pubKeys[:i+1], validTestMsg, expectedDomain) | ||
assert.True(t, isOk) | ||
|
||
isOk = aggSig.VerifyAggregated(pubKeys[:i+1], invalidTestMsg, expectedDomain) | ||
assert.False(t, isOk) | ||
|
||
isOk = aggSig.VerifyAggregated(pubKeys[:i+1], validTestMsg, unexpectedDomain) | ||
assert.False(t, isOk) | ||
} | ||
} |
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
Oops, something went wrong.