-
Notifications
You must be signed in to change notification settings - Fork 170
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
BM-17: Add basic functianalities of bls crypto #12
Conversation
Great work! Two quick comments:
|
Thanks, @SebastianElvis! Re
|
b41fc7b
to
9b05bdf
Compare
When we use these APIs we need to write stuff like |
Ah, you are right. Let's hear opinions from others. |
Regarding naming, here's some advice: https://go.dev/doc/effective_go#names As I understand you always evoke functions through the package name, so in this case it would be I don't know if it should be |
crypto/bls12381/bls_multisig.go
Outdated
// SignMsg signs on a msg using a bls secret key | ||
// the returned sig is compressed version with 48 byte size | ||
func SignMsg(sk *blst.SecretKey, msg []byte) []byte { | ||
return new(BlsSig).Sign(sk, msg, dst).Compress() |
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.
Shouldn't dst
be the name of the return value? I can't see how this works.
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.
It is the Domain Separation Tag for signatures on G1 (minimal-signature-size). See https://github.com/apache/incubator-milagro-crypto-rust/blob/develop/src/bls381/basic.rs.
crypto/bls12381/bls_multisig.go
Outdated
|
||
// GenerateBlsKeyPair generates a random bls key pair based on a given seed | ||
// the public key is compressed with 96 byte size | ||
func GenerateBlsKeyPair(seed []byte) (*blst.SecretKey, []byte) { |
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.
It might help to have a struct to wrap the compressed public key, to track the type later.
crypto/bls12381/bls_multisig.go
Outdated
|
||
// VerifyBlsSig verifies a bls sig over msg with a bls public key | ||
// the sig and public key are all compressed | ||
func VerifyBlsSig(sig []byte, pk []byte, msg []byte) bool { |
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.
Wrapping signatures and public keys could help make sure we're not mixing them up.
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.
So it would be something like
type Signature []byte
type PublicKey []byte
func VerifySig(sig Signature, pk PublicKey, msg []byte) bool
crypto/bls12381/bls_multisig.go
Outdated
} | ||
|
||
// AggregateBlsSigs aggregates bls sigs into a single bls signature | ||
func AggregateBlsSigs(sigs [][]byte) ([]byte, bool) { |
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.
Would it make sense to differentiate a type Sig []byte
from a type MultiSig []byte
? Same for the public key types?
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.
Can it be used to add individual signatures to an already aggregated signature? It should, right?
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.
Can it be used to add individual signatures to an already aggregated signature? It should, right?
Yes. Do I need to add a function like AggrSig(existingSig Signature, newSig Signature)? I think it relates to our previous discussion about accumulating options, right?
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.
Would it make sense to differentiate a
type Sig []byte
from atype MultiSig []byte
? Same for the public key types?
Would it be better if MultiSig and Sig are both represented as type Signature []byte
? They are essentially points on a curve. Also, we can use Verify() to verify both a single bls sig or an aggregated bls sig.
crypto/bls12381/bls_multisig.go
Outdated
} | ||
|
||
// AggregateBlsPubKeys aggregates bls public keys into a single bls public key | ||
func AggregateBlsPubKeys(pks [][]byte) ([]byte, bool) { |
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.
Can this aggregate an already aggregated key plus a non-aggregated key?
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.
@aakoshh Yes, a public key and a sig are essentially points on the curve. Do I need to add a function like AggrPK(existingPK PublicKey, newPK PublicKey)?
@SebastianElvis @aakoshh, thanks for your review. I changed the API names to a shorter version and added a wrapper for the signature and public key. Please review it again. Thanks! |
crypto/bls12381/types.go
Outdated
type Signature []byte | ||
type PublicKey []byte | ||
|
||
func (sig Signature) ToByte() []byte { |
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.
func (sig Signature) ToByte() []byte { | |
func (sig Signature) Bytes() []byte { |
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.
This seems to be more consistent with existing Go functions like String()
. Not strong objection though.
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.
Agreed. Thanks!
crypto/bls12381/types.go
Outdated
return sig | ||
} | ||
|
||
func (pk PublicKey) ToByte() []byte { |
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.
func (pk PublicKey) ToByte() []byte { | |
func (pk PublicKey) Bytes() []byte { |
LGTM except for the above two minor comments without strong objection. |
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.
Nice work! Some minor comments:
crypto/bls12381/bls.go
Outdated
|
||
// GeneKeyPair generates a random bls key pair based on a given seed | ||
// the public key is compressed with 96 byte size | ||
func GeneKeyPair(seed []byte) (*blst.SecretKey, PublicKey) { |
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.
func GeneKeyPair(seed []byte) (*blst.SecretKey, PublicKey) { | |
func GenKeyPair(seed []byte) (*blst.SecretKey, PublicKey) { |
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.
Nice catch! Thanks.
crypto/bls12381/bls.go
Outdated
sigBytes[i] = sigs[i].ToByte() | ||
} | ||
if !aggSig.AggregateCompressed(sigBytes, false) { | ||
return nil, false |
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.
Maybe it would be better if an error was returned.
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.
or an explanation why we are returning a boolean value.
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.
Agreed. Thanks!
crypto/bls12381/bls.go
Outdated
pkBytes[i] = pks[i].ToByte() | ||
} | ||
if !aggPk.AggregateCompressed(pkBytes, false) { | ||
return nil, false |
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.
Maybe an error is appropriate here as well.
crypto/bls12381/types.go
Outdated
type BlsMultiPubKey = blst.P2Aggregate | ||
|
||
// Domain Separation Tag for signatures on G1 (minimal-signature-size) | ||
var dst = []byte("BLS_SIG_BLS12381G1_XMD:SHA-256_SSWU_RO_NUL_") |
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.
Can this be a constant? Also, upper case letters might separate the confusion from the common variable name dst
referring to destination
.
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.
Nice point. Thanks!
I just found that we may not be able to have this as a constant because Go does not support const array. See https://stackoverflow.com/questions/13137463/declare-a-constant-array
@SebastianElvis @vitsalis Thanks for your comments. I added error return and addressed naming issues. |
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.
Thanks for resolving! Some extra very minor comments mostly related to capitalization of BLS
across the codebase.
Thanks @vitsalis for the follow-up comments. I capitalized "bls" in comments but when I changed |
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.
Thanks for the changes! LGTM 🚀
@gitferry when you merge a PR, please use the "Squash" functionality of Github so Look, your individual commits are all visible in the It's this button you want to click, you can switch to it with the drop down: |
Fixes https://babylon-chain.atlassian.net/browse/BM-17
This PR adds a wrapper of blst's go binding: https://github.com/supranational/blst/tree/1bd5899e4af46375d58fdfcb21c5ccf74181a35c/bindings/go
It implements basic functionalities around bls crypto as follows: