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
base: main
Are you sure you want to change the base?
Changes from 2 commits
a244057
04f9530
f5fff43
716ca72
ff26592
ede72b9
b11081d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,19 +3,83 @@ | |
| package blsverifier | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "github.com/OffchainLabs/prysm/v6/crypto/bls" | ||
| blst "github.com/supranational/blst/bindings/go" | ||
| ) | ||
|
|
||
| func AggregatePublicKeys(publicKeys [][]byte) (bls.PublicKey, error) { | ||
| return bls.AggregatePublicKeys(publicKeys) | ||
| const ( | ||
| // PublicKeySize is the size of a compressed BLS12-381 G1 public key. | ||
| PublicKeySize = blst.BLST_P1_COMPRESS_BYTES | ||
|
dianab-cl marked this conversation as resolved.
Resolved
|
||
| // SignatureSize is the size of a compressed BLS12-381 G2 signature. | ||
| SignatureSize = blst.BLST_P2_COMPRESS_BYTES | ||
| ) | ||
|
|
||
| // dst is the Ethereum consensus proof-of-possession ciphersuite domain separation tag. | ||
| var dst = []byte("BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_") | ||
|
|
||
| // PublicKey is a compressed BLS12-381 G1 public key. | ||
| type PublicKey []byte | ||
|
dianab-cl marked this conversation as resolved.
Resolved
|
||
|
|
||
| // Marshal returns a copy of the compressed public key bytes. | ||
| func (pk PublicKey) Marshal() []byte { | ||
| return bytes.Clone(pk) | ||
| } | ||
|
|
||
| // AggregatePublicKeys validates the compressed public keys and returns their aggregate. | ||
| func AggregatePublicKeys(publicKeys [][]byte) (PublicKey, error) { | ||
| pk, err := aggregatePublicKeys(publicKeys) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return PublicKey(pk.Compress()), nil | ||
| } | ||
|
|
||
| // VerifySignature verifies a compressed signature over message against the aggregate of publicKeys. | ||
| func VerifySignature(signature []byte, message [32]byte, publicKeys [][]byte) (bool, error) { | ||
| aggregatedPublicKey, err := AggregatePublicKeys(publicKeys) | ||
| pk, err := aggregatePublicKeys(publicKeys) | ||
| if err != nil { | ||
| return false, fmt.Errorf("failed to aggregate public keys %w", err) | ||
| } | ||
| return bls.VerifySignature(signature, message, aggregatedPublicKey) | ||
|
|
||
| if len(signature) != SignatureSize { | ||
| return false, fmt.Errorf("signature must be %d bytes", SignatureSize) | ||
| } | ||
| sig := new(blst.P2Affine).Uncompress(signature) | ||
| if sig == nil { | ||
| return false, errors.New("could not unmarshal bytes into signature") | ||
| } | ||
| // Subgroup check only: an aggregate signature may be the point at infinity. | ||
| if !sig.SigValidate(false) { | ||
| return false, errors.New("signature not in group") | ||
| } | ||
|
|
||
| // Signature and key are already validated above. | ||
| return sig.Verify(false, pk, false, message[:], dst), nil | ||
| } | ||
|
|
||
| func aggregatePublicKeys(publicKeys [][]byte) (*blst.P1Affine, error) { | ||
| if len(publicKeys) == 0 { | ||
| return nil, errors.New("nil or empty public keys") | ||
| } | ||
|
|
||
| agg := new(blst.P1Aggregate) | ||
| for _, bz := range publicKeys { | ||
| if len(bz) != PublicKeySize { | ||
| return nil, fmt.Errorf("public key must be %d bytes", PublicKeySize) | ||
| } | ||
| pk := new(blst.P1Affine).Uncompress(bz) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2] Preserve caching of validated public keys Successive light-client updates commonly reuse validator keys. Prysm cached validated points, but this loop repeats decompression and subgroup validation on every query. The review benchmark reports 100 recurring keys increasing from ~0.086 ms to ~3.32 ms per aggregation (39× slower). Please preserve reuse through a bounded, concurrency-safe cache populated only after successful validation, and add a repeated-key benchmark. |
||
| if pk == nil { | ||
| return nil, errors.New("could not unmarshal bytes into public key") | ||
| } | ||
| // Rejects the point at infinity and points outside the subgroup. | ||
| if !pk.KeyValidate() { | ||
| return nil, errors.New("received an infinite public key") | ||
| } | ||
| agg.Add(pk, false) | ||
| } | ||
|
|
||
| return agg.ToAffine(), nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package blsverifier_test | ||
|
|
||
| import ( | ||
| "encoding/hex" | ||
| "encoding/json" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
|
||
| "github.com/lfdt-ibc/ibc-go/modules/light-clients/08-wasm/v11/blsverifier" | ||
| ) | ||
|
|
||
| var ( | ||
| publicKeys = [][]byte{ | ||
| mustDecodeHex("84fe268400062ae6255010a421204dad5b0a8bd93522a4abc023037fa7a769f6ef29acc14c7fae8c5c0cd1df21ae1f52"), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. // The success vectors were checked against two independent implementations: |
||
| mustDecodeHex("8b850e23cf188473e04feaa5d39f40f54b8c0e717f7fccb77d0efb5326802274bda863911efa8775a08674b887e0e766"), | ||
| mustDecodeHex("b52684603da6cb2902092310deab2ef3116a0a2b449c0f44d82d1d30bf12f7474492534f518af541ca9da110ee417132"), | ||
| } | ||
| aggregatedPublicKey = mustDecodeHex("912cf816853d0984e448f065d0bdb34b1e26be1d3c50bbc2f0d39b0ff1c2963a97b5dfd442a496763f3c6e5c54375b1e") | ||
| aggregatedSignature = mustDecodeHex("8af25b4a2786b3305f80528520544071a143e80ef4091dfca32e347eeabeef71eeb8673a78e8ff2875883ab5498d8a7b1888a80878bfd82c2d27e93ed74e979014e95d7f2ef55b1d28070a4a2e6ce9df3b8f07c6444c4799c64d654c2810c36d") | ||
| message = [32]byte{3, 1, 2, 3} | ||
| ) | ||
|
|
||
| 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"}, | ||
| } | ||
|
|
||
| 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.Marshal()) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| 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"}, | ||
| {"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) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.