Skip to content
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

[crypto]: add bls #675

Merged
merged 20 commits into from
Jan 15, 2024
Merged

[crypto]: add bls #675

merged 20 commits into from
Jan 15, 2024

Conversation

wlawt
Copy link
Contributor

@wlawt wlawt commented Jan 11, 2024

Closes #676 and #677

Add support for BLS by using the BLS library that exists in AvalancheGo. This would let developers integrate BLS, if they choose to, into their VM.

Overview:

  • Introduces BLS into the crypto package
  • The BLS package is a wrapper of a selection of functions from the AvalancheGo library that aligns with the existing function signatures that exist for ed25519 and secp256r1
  • Example of integration into the MorpheusVM

Test Plan:

  • Unit tests for the crypto bls package
    • Run go test within bls package
  • Integration test in MorpheusVM sending between ed25519 and bls
    • Run ./scripts/tests.integration.sh within examples/morpheusvm
  • Manual testing with CLI interacting with MorpheusVM
    • Run ./scripts/build.sh
    • Generate a BLS key
    • Send funds to the BLS address
    • Send funds from the BLS address to any other wallet

Future TODOs:

  • Possibly replace usage of avalanchego/utils/crypto throughout the repo with this internal package
  • Add developer docs to show how to integrate own crypto into their own HyperVM

@wlawt wlawt linked an issue Jan 14, 2024 that may be closed by this pull request
@wlawt wlawt marked this pull request as ready for review January 14, 2024 22:10
@wlawt wlawt requested a review from patrick-ogrady as a code owner January 14, 2024 22:10
@wlawt
Copy link
Contributor Author

wlawt commented Jan 14, 2024

Related: #613

var d BLS

signer := bls.SerializePublicKey(&d.Signer)
p.UnpackFixedBytes(bls.PublicKeyLen*2, &signer)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this *2?

Copy link
Contributor Author

@wlawt wlawt Jan 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bls.PublicKeyLen is defined to be BLST_P1_COMPRESS_BYTES which is 48 bytes. When we serialize the public key this has a length of BLST_P1_SERIALIZE_BYTES which is twice of PublicKeyLen.

When just unpacking the bytes after the first 48 are all 0-filled. This led to an invalid signature error when calling AsyncVerify.

I'll also make a note of this in the code

Copy link
Contributor

@patrick-ogrady patrick-ogrady Jan 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should make a new const of the actual bytes it take to serialize (that's the value we care about)...we need to align BLSSize with the actual marshal/unmarshal size or else it won't be big enough.

You are packing bls.PublicKeyLen + bls.SignatureLen but unpacking bls.PublicKeyLen*2 + bls.SignatureLen

)

type BLS struct {
Signer bls.PublicKey `json:"signer"`
Copy link
Contributor

@patrick-ogrady patrick-ogrady Jan 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the AvalancheGo/our library operates over pointers, I would recommend using them here instead of dereferencing things.

func UnmarshalBLS(p *codec.Packer, _ *warp.Message) (chain.Auth, error) {
var d BLS

signer := bls.SerializePublicKey(&d.Signer)
Copy link
Contributor

@patrick-ogrady patrick-ogrady Jan 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just create a byte array of the size you are expecting and pass that into Deserialize* instead of doing this (which is going to be much less efficient because it will try to create a compressed version of empty bytes that we then just rewrite later)

@@ -148,22 +148,22 @@ func (d *BLS) Refund(
var _ chain.AuthFactory = (*BLSFactory)(nil)

type BLSFactory struct {
priv bls.PrivateKey
priv *bls.PrivateKey `json:"priv,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private fields aren't marshaled in json, so I think this is unnecessary?

Copy link
Contributor

@patrick-ogrady patrick-ogrady left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

@patrick-ogrady patrick-ogrady merged commit f1e018e into ava-labs:main Jan 15, 2024
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

[examples] add bls into morpheusVM [crypto] support bls option
2 participants