From 41733b46b9d02e30489cacd87f08505d1d45d133 Mon Sep 17 00:00:00 2001 From: Dev Ojha Date: Fri, 29 Jun 2018 00:08:01 -0700 Subject: [PATCH] crypto: Abstract pubkey / signature size when known to constants (#1808) * crypto: Abstract pubkey / signature size when known to constants * Created PubKeyEd25519Size as 32 * Created PubkeySecp256k1Size as 33 * Created SignatureEd25519Size as 64 * Remove extraneous message from changelog --- CHANGELOG.md | 3 +++ crypto/pub_key.go | 16 ++++++++++------ crypto/signature.go | 4 +++- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 76fac187..f250986d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## TBD +IMPROVEMENTS: + - [crypto] Make public key size into public constants + BUG FIXES: - [rpc] limited number of HTTP/WebSocket connections (`rpc.max_open_connections`) and gRPC connections diff --git a/crypto/pub_key.go b/crypto/pub_key.go index 51ef6c54..b7f95430 100644 --- a/crypto/pub_key.go +++ b/crypto/pub_key.go @@ -40,8 +40,10 @@ type PubKey interface { var _ PubKey = PubKeyEd25519{} +const PubKeyEd25519Size = 32 + // Implements PubKeyInner -type PubKeyEd25519 [32]byte +type PubKeyEd25519 [PubKeyEd25519Size]byte // Address is the SHA256-20 of the raw pubkey bytes. func (pubKey PubKeyEd25519) Address() Address { @@ -62,15 +64,15 @@ func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool { if !ok { return false } - pubKeyBytes := [32]byte(pubKey) - sigBytes := [64]byte(sig) + pubKeyBytes := [PubKeyEd25519Size]byte(pubKey) + sigBytes := [SignatureEd25519Size]byte(sig) return ed25519.Verify(&pubKeyBytes, msg, &sigBytes) } // For use with golang/crypto/nacl/box // If error, returns nil. -func (pubKey PubKeyEd25519) ToCurve25519() *[32]byte { - keyCurve25519, pubKeyBytes := new([32]byte), [32]byte(pubKey) +func (pubKey PubKeyEd25519) ToCurve25519() *[PubKeyEd25519Size]byte { + keyCurve25519, pubKeyBytes := new([PubKeyEd25519Size]byte), [PubKeyEd25519Size]byte(pubKey) ok := extra25519.PublicKeyToCurve25519(keyCurve25519, &pubKeyBytes) if !ok { return nil @@ -94,10 +96,12 @@ func (pubKey PubKeyEd25519) Equals(other PubKey) bool { var _ PubKey = PubKeySecp256k1{} +const PubKeySecp256k1Size = 33 + // Implements PubKey. // Compressed pubkey (just the x-cord), // prefixed with 0x02 or 0x03, depending on the y-cord. -type PubKeySecp256k1 [33]byte +type PubKeySecp256k1 [PubKeySecp256k1Size]byte // Implements Bitcoin style addresses: RIPEMD160(SHA256(pubkey)) func (pubKey PubKeySecp256k1) Address() Address { diff --git a/crypto/signature.go b/crypto/signature.go index 1ffb45ea..728a2a04 100644 --- a/crypto/signature.go +++ b/crypto/signature.go @@ -25,8 +25,10 @@ type Signature interface { var _ Signature = SignatureEd25519{} +const SignatureEd25519Size = 64 + // Implements Signature -type SignatureEd25519 [64]byte +type SignatureEd25519 [SignatureEd25519Size]byte func (sig SignatureEd25519) Bytes() []byte { bz, err := cdc.MarshalBinaryBare(sig)