Repository navigation
fix(light-clients/08-wasm): Replace OffchainLabs/prysm/v6 with supranational/blst #9175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dianab-cl
wants to merge
7
commits into
lfdt-ibc:main
Choose a base branch
from
dianab-cl:diana/fou-2319-fix-licensing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+339
−92
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a244057
fix: Replace OffchainLabs/prysm/v6 with supranational/blst
dianab-cl 04f9530
Apply suggestion from @dianab-cl
dianab-cl f5fff43
After code review
dianab-cl 716ca72
Update CHANGELOG.md
dianab-cl ff26592
after code review
dianab-cl ede72b9
Fix lint
dianab-cl b11081d
After code review
dianab-cl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
218 changes: 218 additions & 0 deletions
218
modules/light-clients/08-wasm/blsverifier/crypto_test.go
This file contains hidden or 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,218 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package blsverifier_test | ||
|
|
||
| import ( | ||
| "encoding/hex" | ||
| "encoding/json" | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| blst "github.com/supranational/blst/bindings/go" | ||
|
|
||
| sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
|
||
| "github.com/lfdt-ibc/ibc-go/modules/light-clients/08-wasm/v11/blsverifier" | ||
| ) | ||
|
|
||
| // The success vectors were checked against two independent implementations: | ||
| // github.com/OffchainLabs/prysm/v6@v6.1.4 crypto/bls, which this package wrapped | ||
| // before, and github.com/consensys/gnark-crypto. Both produce aggregatedPublicKey | ||
| // from publicKeys and accept aggregatedSignature over message. | ||
| // | ||
| // The not-in-subgroup vectors are on-curve points outside the prime-order | ||
| // subgroup (G1: x = 4, G2: x = 2), generated with gnark-crypto. They decode as | ||
| // curve points, so only the subgroup checks reject them. | ||
| var ( | ||
| publicKeys = [][]byte{ | ||
| mustDecodeHex("84fe268400062ae6255010a421204dad5b0a8bd93522a4abc023037fa7a769f6ef29acc14c7fae8c5c0cd1df21ae1f52"), | ||
| mustDecodeHex("8b850e23cf188473e04feaa5d39f40f54b8c0e717f7fccb77d0efb5326802274bda863911efa8775a08674b887e0e766"), | ||
| mustDecodeHex("b52684603da6cb2902092310deab2ef3116a0a2b449c0f44d82d1d30bf12f7474492534f518af541ca9da110ee417132"), | ||
| } | ||
| aggregatedPublicKey = mustDecodeHex("912cf816853d0984e448f065d0bdb34b1e26be1d3c50bbc2f0d39b0ff1c2963a97b5dfd442a496763f3c6e5c54375b1e") | ||
| aggregatedSignature = mustDecodeHex("8af25b4a2786b3305f80528520544071a143e80ef4091dfca32e347eeabeef71eeb8673a78e8ff2875883ab5498d8a7b1888a80878bfd82c2d27e93ed74e979014e95d7f2ef55b1d28070a4a2e6ce9df3b8f07c6444c4799c64d654c2810c36d") | ||
| message = [32]byte{3, 1, 2, 3} | ||
|
|
||
| g1NotInSubgroup = mustDecodeHex("800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004") | ||
| g2NotInSubgroup = mustDecodeHex("800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002") | ||
| ) | ||
|
|
||
| func mustDecodeHex(s string) []byte { | ||
| bz, err := hex.DecodeString(s) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| return bz | ||
| } | ||
|
|
||
| // negate flips the sign bit of a compressed point. | ||
| func negate(point []byte) []byte { | ||
| bz := append([]byte{}, point...) | ||
| bz[0] ^= 0x20 | ||
| return bz | ||
| } | ||
|
|
||
| func infinityPoint(size int) []byte { | ||
| bz := make([]byte, size) | ||
| bz[0] = 0xc0 | ||
| return bz | ||
| } | ||
|
|
||
| func TestAggregatePublicKeys(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| publicKeys [][]byte | ||
| expected []byte | ||
| expErrorMsg string | ||
| }{ | ||
| {"success: single key", publicKeys[:1], publicKeys[0], ""}, | ||
| {"success: multiple keys", publicKeys, aggregatedPublicKey, ""}, | ||
| {"failure: no keys", nil, nil, "nil or empty public keys"}, | ||
| {"failure: wrong length", [][]byte{publicKeys[0][:47]}, nil, "public key must be 48 bytes"}, | ||
| {"failure: not a curve point", [][]byte{make([]byte, 48)}, nil, "could not unmarshal bytes into public key"}, | ||
| {"failure: point at infinity", [][]byte{infinityPoint(48)}, nil, "received an infinite public key"}, | ||
| {"failure: point not in subgroup", [][]byte{g1NotInSubgroup}, nil, "received an infinite public key"}, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| pk, err := blsverifier.AggregatePublicKeys(tc.publicKeys) | ||
| if tc.expErrorMsg != "" { | ||
| require.ErrorContains(t, err, tc.expErrorMsg) | ||
| return | ||
| } | ||
| require.NoError(t, err) | ||
| require.Equal(t, tc.expected, pk) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestPublicKeyCache(t *testing.T) { | ||
| blsverifier.PurgePublicKeyCache() | ||
| t.Cleanup(blsverifier.PurgePublicKeyCache) | ||
|
|
||
| // Keys that fail validation are not cached, including after a valid key in the same batch. | ||
| invalidKeys := [][]byte{publicKeys[0][:47], make([]byte, 48), infinityPoint(48), g1NotInSubgroup} | ||
| for _, key := range invalidKeys { | ||
| _, err := blsverifier.AggregatePublicKeys([][]byte{publicKeys[0], key}) | ||
| require.Error(t, err) | ||
| require.Equal(t, 1, blsverifier.PublicKeyCacheLen()) | ||
| } | ||
|
|
||
| // Cached keys produce the same results as freshly validated ones. | ||
| for range 2 { | ||
| pk, err := blsverifier.AggregatePublicKeys(publicKeys) | ||
| require.NoError(t, err) | ||
| require.Equal(t, aggregatedPublicKey, pk) | ||
| require.Equal(t, len(publicKeys), blsverifier.PublicKeyCacheLen()) | ||
|
|
||
| valid, err := blsverifier.VerifySignature(aggregatedSignature, message, publicKeys) | ||
| require.NoError(t, err) | ||
| require.True(t, valid) | ||
| } | ||
|
|
||
| // A cached key is still rejected when it is not the full input. | ||
| _, err := blsverifier.AggregatePublicKeys([][]byte{publicKeys[0][:47]}) | ||
| require.ErrorContains(t, err, "public key must be 48 bytes") | ||
| } | ||
|
|
||
| func TestVerifySignature(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| signature []byte | ||
| message [32]byte | ||
| publicKeys [][]byte | ||
| expValid bool | ||
| expErrorMsg string | ||
| }{ | ||
| {"success: valid aggregate signature", aggregatedSignature, message, publicKeys, true, ""}, | ||
| {"invalid: wrong message", aggregatedSignature, [32]byte{9}, publicKeys, false, ""}, | ||
| {"invalid: missing signer", aggregatedSignature, message, publicKeys[:2], false, ""}, | ||
| {"invalid: signature at infinity", infinityPoint(96), message, publicKeys, false, ""}, | ||
| {"failure: wrong signature length", aggregatedSignature[:95], message, publicKeys, false, "signature must be 96 bytes"}, | ||
| {"failure: signature not a curve point", make([]byte, 96), message, publicKeys, false, "could not unmarshal bytes into signature"}, | ||
| {"failure: signature not in subgroup", g2NotInSubgroup, message, publicKeys, false, "signature not in group"}, | ||
| {"invalid: keys cancel to infinity", infinityPoint(96), message, [][]byte{publicKeys[0], negate(publicKeys[0])}, false, ""}, | ||
| {"failure: invalid public key", aggregatedSignature, message, [][]byte{infinityPoint(48)}, false, "failed to aggregate public keys"}, | ||
|
greptile-apps[bot] marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| valid, err := blsverifier.VerifySignature(tc.signature, tc.message, tc.publicKeys) | ||
| if tc.expErrorMsg != "" { | ||
| require.ErrorContains(t, err, tc.expErrorMsg) | ||
| } else { | ||
| require.NoError(t, err) | ||
| } | ||
| require.Equal(t, tc.expValid, valid) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestCustomQuerier(t *testing.T) { | ||
| querier := blsverifier.CustomQuerier() | ||
|
|
||
| request, err := json.Marshal(blsverifier.CustomQuery{ | ||
| Aggregate: &blsverifier.QueryAggregate{PublicKeys: publicKeys}, | ||
| }) | ||
| require.NoError(t, err) | ||
| res, err := querier(sdk.Context{}, request) | ||
| require.NoError(t, err) | ||
| expected, err := json.Marshal(aggregatedPublicKey) | ||
| require.NoError(t, err) | ||
| require.Equal(t, expected, res) | ||
|
|
||
| request, err = json.Marshal(blsverifier.CustomQuery{ | ||
| AggregateVerify: &blsverifier.QueryAggregateVerify{ | ||
| PublicKeys: publicKeys, | ||
| Signature: aggregatedSignature, | ||
| Message: message[:], | ||
| }, | ||
| }) | ||
| require.NoError(t, err) | ||
| res, err = querier(sdk.Context{}, request) | ||
| require.NoError(t, err) | ||
| require.Equal(t, []byte("true"), res) | ||
| } | ||
|
|
||
| // generatePublicKeys returns n distinct, valid compressed public keys. | ||
| func generatePublicKeys(tb testing.TB, n int) [][]byte { | ||
| tb.Helper() | ||
| keys := make([][]byte, n) | ||
| for i := range keys { | ||
| ikm := make([]byte, 32) | ||
| ikm[0], ikm[1] = byte(i), byte(i>>8) | ||
| sk := blst.KeyGen(ikm) | ||
| require.NotNil(tb, sk) | ||
| keys[i] = new(blst.P1Affine).From(sk).Compress() | ||
| } | ||
| return keys | ||
| } | ||
|
|
||
| // BenchmarkAggregatePublicKeys measures aggregation when the same keys recur across | ||
| // queries, as they do across successive light-client updates, against a cold cache. | ||
| func BenchmarkAggregatePublicKeys(b *testing.B) { | ||
| keys := generatePublicKeys(b, 100) | ||
| b.Cleanup(blsverifier.PurgePublicKeyCache) | ||
|
|
||
| b.Run(fmt.Sprintf("repeated %d keys", len(keys)), func(b *testing.B) { | ||
| blsverifier.PurgePublicKeyCache() | ||
| for b.Loop() { | ||
| if _, err := blsverifier.AggregatePublicKeys(keys); err != nil { | ||
| b.Fatal(err) | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| b.Run(fmt.Sprintf("uncached %d keys", len(keys)), func(b *testing.B) { | ||
| for range b.N { | ||
| b.StopTimer() | ||
| blsverifier.PurgePublicKeyCache() | ||
| b.StartTimer() | ||
| if _, err := blsverifier.AggregatePublicKeys(keys); err != nil { | ||
| b.Fatal(err) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
This file contains hidden or 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,18 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package blsverifier | ||
|
|
||
| // AggregatePublicKeys is a wrapper around aggregatePublicKeys to allow the function to be directly called in tests. | ||
| func AggregatePublicKeys(publicKeys [][]byte) ([]byte, error) { | ||
| return aggregatePublicKeys(publicKeys) | ||
| } | ||
|
|
||
| // PublicKeyCacheLen returns the number of cached public keys. | ||
| func PublicKeyCacheLen() int { | ||
| return publicKeyCache.Len() | ||
| } | ||
|
|
||
| // PurgePublicKeyCache removes all cached public keys. | ||
| func PurgePublicKeyCache() { | ||
| publicKeyCache.Purge() | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where do these vectors come from? If prysm or another independent implementation produced them, the success cases prove we match the old behavior; if the new code produced them, they only show it agrees with itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// The success vectors were checked against two independent implementations:
// github.com/OffchainLabs/prysm/v6@v6.1.4 crypto/bls, which this package wrapped
// before, and github.com/consensys/gnark-crypto. Both produce aggregatedPublicKey
// from publicKeys and accept aggregatedSignature over message.