-
Notifications
You must be signed in to change notification settings - Fork 28
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
Introduce BLS12-381 Signature Key #87
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
There are no files selected for viewing
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
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,142 @@ | ||
package bls | ||
|
||
import ( | ||
"bytes" | ||
"crypto/subtle" | ||
"fmt" | ||
|
||
"github.com/herumi/bls-eth-go-binary/bls" | ||
amino "github.com/tendermint/go-amino" | ||
"github.com/tendermint/tendermint/crypto" | ||
"github.com/tendermint/tendermint/crypto/tmhash" | ||
) | ||
|
||
var _ crypto.PrivKey = PrivKeyBLS12{} | ||
|
||
const ( | ||
PrivKeyAminoName = "tendermint/PrivKeyBLS12" | ||
PubKeyAminoName = "tendermint/PubKeyBLS12" | ||
PrivKeyBLS12Size = 32 | ||
PubKeyBLS12Size = 48 | ||
SignatureSize = 96 | ||
) | ||
|
||
var cdc = amino.NewCodec() | ||
|
||
func init() { | ||
cdc.RegisterInterface((*crypto.PubKey)(nil), nil) | ||
cdc.RegisterConcrete(PubKeyBLS12{}, | ||
PubKeyAminoName, nil) | ||
|
||
cdc.RegisterInterface((*crypto.PrivKey)(nil), nil) | ||
cdc.RegisterConcrete(PrivKeyBLS12{}, | ||
PrivKeyAminoName, nil) | ||
|
||
err := bls.Init(bls.BLS12_381) | ||
if err != nil { | ||
panic(fmt.Sprintf("ERROR: %s", err)) | ||
} | ||
err = bls.SetETHmode(bls.EthModeLatest) | ||
if err != nil { | ||
panic(fmt.Sprintf("ERROR: %s", err)) | ||
} | ||
} | ||
|
||
// PrivKeyBLS12 implements crypto.PrivKey. | ||
type PrivKeyBLS12 [PrivKeyBLS12Size]byte | ||
|
||
// GenPrivKey generates a new BLS12-381 private key. | ||
func GenPrivKey() PrivKeyBLS12 { | ||
sigKey := bls.SecretKey{} | ||
sigKey.SetByCSPRNG() | ||
sigKeyBinary := PrivKeyBLS12{} | ||
copy(sigKeyBinary[:], sigKey.Serialize()) | ||
return sigKeyBinary | ||
} | ||
|
||
// Bytes marshals the privkey using amino encoding. | ||
func (privKey PrivKeyBLS12) Bytes() []byte { | ||
return cdc.MustMarshalBinaryBare(privKey) | ||
} | ||
|
||
// Sign produces a signature on the provided message. | ||
func (privKey PrivKeyBLS12) Sign(msg []byte) ([]byte, error) { | ||
blsKey := bls.SecretKey{} | ||
err := blsKey.Deserialize(privKey[:]) | ||
if err != nil { | ||
panic(fmt.Sprintf("Failed to copy the private key: %s", err)) | ||
} | ||
sign := blsKey.SignByte(msg) | ||
return sign.Serialize(), nil | ||
} | ||
|
||
// PubKey gets the corresponding public key from the private key. | ||
func (privKey PrivKeyBLS12) PubKey() crypto.PubKey { | ||
blsKey := bls.SecretKey{} | ||
err := blsKey.Deserialize(privKey[:]) | ||
if err != nil { | ||
panic(fmt.Sprintf("Failed to copy the private key: %s", err)) | ||
} | ||
pubKey := blsKey.GetPublicKey() | ||
pubKeyBinary := PubKeyBLS12{} | ||
copy(pubKeyBinary[:], pubKey.Serialize()) | ||
return pubKeyBinary | ||
} | ||
|
||
// Equals - you probably don't need to use this. | ||
// Runs in constant time based on length of the keys. | ||
func (privKey PrivKeyBLS12) Equals(other crypto.PrivKey) bool { | ||
if otherEd, ok := other.(PrivKeyBLS12); ok { | ||
return subtle.ConstantTimeCompare(privKey[:], otherEd[:]) == 1 | ||
} | ||
return false | ||
} | ||
|
||
var _ crypto.PubKey = PubKeyBLS12{} | ||
|
||
// PubKeyBLS12 implements crypto.PubKey for the BLS12-381 signature scheme. | ||
type PubKeyBLS12 [PubKeyBLS12Size]byte | ||
|
||
// Address is the SHA256-20 of the raw pubkey bytes. | ||
func (pubKey PubKeyBLS12) Address() crypto.Address { | ||
return tmhash.SumTruncated(pubKey[:]) | ||
} | ||
|
||
// Bytes marshals the PubKey using amino encoding. | ||
func (pubKey PubKeyBLS12) Bytes() []byte { | ||
bz, err := cdc.MarshalBinaryBare(pubKey) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return bz | ||
} | ||
|
||
func (pubKey PubKeyBLS12) VerifyBytes(msg []byte, sig []byte) bool { | ||
// make sure we use the same algorithm to sign | ||
if len(sig) != SignatureSize { | ||
return false | ||
} | ||
blsPubKey := bls.PublicKey{} | ||
err := blsPubKey.Deserialize(pubKey[:]) | ||
if err != nil { | ||
panic(fmt.Sprintf("Failed to copy the public key: [%X] %s", pubKey[:], err)) | ||
} | ||
blsSign := bls.Sign{} | ||
err = blsSign.Deserialize(sig) | ||
if err != nil { | ||
return false | ||
} | ||
return blsSign.VerifyByte(&blsPubKey, msg) | ||
} | ||
|
||
func (pubKey PubKeyBLS12) String() string { | ||
return fmt.Sprintf("PubKeyBLS12{%X}", pubKey[:]) | ||
} | ||
|
||
// nolint: golint | ||
func (pubKey PubKeyBLS12) Equals(other crypto.PubKey) bool { | ||
if otherEd, ok := other.(PubKeyBLS12); ok { | ||
return bytes.Equal(pubKey[:], otherEd[:]) | ||
} | ||
return false | ||
} |
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.
Is there a reason why you used this instead of
bytes.Equal()
?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.
I'm sure that this is a defensive measure against a side-channel attack (timing attack).
The
bytes.Equals()
is more efficient because it returns early as soon as it finds a difference byte, but such behaviour can be a vulnerability in a security library. The difference in the response time of such a function provides a hint to the attacker who measures it to determine if the input prefix matches.As you can see by searching the Tendermint repository in
ConstantTimeCompare()
, theEquals()
for all ofed25519
,secp256k1
andsr25519
private key implementations in Tendermint use like this.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.
I learned from you. :)