From 17fa3913ef7ba5f569ebc3695fab15b10dd914f0 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Tue, 6 Jun 2017 19:21:34 +1200 Subject: Specify ECDSA constant sizes as constants --- src/pubkey.h | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'src/pubkey.h') diff --git a/src/pubkey.h b/src/pubkey.h index dbf0e23f20..6e25ad7c49 100644 --- a/src/pubkey.h +++ b/src/pubkey.h @@ -1,5 +1,6 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Copyright (c) 2009-2016 The Bitcoin Core developers +// Copyright (c) 2017 The Zcash developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -15,10 +16,12 @@ /** * secp256k1: - * const unsigned int PRIVATE_KEY_SIZE = 279; - * const unsigned int PUBLIC_KEY_SIZE = 65; - * const unsigned int SIGNATURE_SIZE = 72; - * + */ +const unsigned int PUBLIC_KEY_SIZE = 65; +const unsigned int COMPRESSED_PUBLIC_KEY_SIZE = 33; +const unsigned int SIGNATURE_SIZE = 72; +const unsigned int COMPACT_SIGNATURE_SIZE = 65; +/** * see www.keylength.com * script supports up to 75 for single byte push */ @@ -44,15 +47,15 @@ private: * Just store the serialized data. * Its length can very cheaply be computed from the first byte. */ - unsigned char vch[65]; + unsigned char vch[PUBLIC_KEY_SIZE]; //! Compute the length of a pubkey with a given first byte. unsigned int static GetLen(unsigned char chHeader) { if (chHeader == 2 || chHeader == 3) - return 33; + return COMPRESSED_PUBLIC_KEY_SIZE; if (chHeader == 4 || chHeader == 6 || chHeader == 7) - return 65; + return PUBLIC_KEY_SIZE; return 0; } @@ -127,7 +130,7 @@ public: void Unserialize(Stream& s) { unsigned int len = ::ReadCompactSize(s); - if (len <= 65) { + if (len <= PUBLIC_KEY_SIZE) { s.read((char*)vch, len); } else { // invalid pubkey, skip available data @@ -166,7 +169,7 @@ public: //! Check whether this is a compressed public key. bool IsCompressed() const { - return size() == 33; + return size() == COMPRESSED_PUBLIC_KEY_SIZE; } /** -- cgit v1.2.3 From 1ce9f0a952a3d5d9442ad8251da898d96209c16c Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Thu, 8 Jun 2017 16:07:49 +1200 Subject: Ensure that ECDSA constant sizes are correctly-sized --- src/pubkey.h | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/pubkey.h') diff --git a/src/pubkey.h b/src/pubkey.h index 6e25ad7c49..a508c238e4 100644 --- a/src/pubkey.h +++ b/src/pubkey.h @@ -48,6 +48,9 @@ private: * Its length can very cheaply be computed from the first byte. */ unsigned char vch[PUBLIC_KEY_SIZE]; + static_assert( + PUBLIC_KEY_SIZE >= COMPRESSED_PUBLIC_KEY_SIZE, + "COMPRESSED_PUBLIC_KEY_SIZE is larger than PUBLIC_KEY_SIZE"); //! Compute the length of a pubkey with a given first byte. unsigned int static GetLen(unsigned char chHeader) -- cgit v1.2.3 From 63179d028347bf3e32c7ea61386df4c44307b4a7 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Wed, 4 Oct 2017 14:41:40 +0100 Subject: Scope the ECDSA constant sizes to CPubKey / CKey classes --- src/pubkey.h | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) (limited to 'src/pubkey.h') diff --git a/src/pubkey.h b/src/pubkey.h index a508c238e4..df41eb4971 100644 --- a/src/pubkey.h +++ b/src/pubkey.h @@ -14,18 +14,6 @@ #include #include -/** - * secp256k1: - */ -const unsigned int PUBLIC_KEY_SIZE = 65; -const unsigned int COMPRESSED_PUBLIC_KEY_SIZE = 33; -const unsigned int SIGNATURE_SIZE = 72; -const unsigned int COMPACT_SIGNATURE_SIZE = 65; -/** - * see www.keylength.com - * script supports up to 75 for single byte push - */ - const unsigned int BIP32_EXTKEY_SIZE = 74; /** A reference to a CKey: the Hash160 of its serialized public key */ @@ -41,6 +29,22 @@ typedef uint256 ChainCode; /** An encapsulated public key. */ class CPubKey { +public: + /** + * secp256k1: + */ + static const unsigned int PUBLIC_KEY_SIZE = 65; + static const unsigned int COMPRESSED_PUBLIC_KEY_SIZE = 33; + static const unsigned int SIGNATURE_SIZE = 72; + static const unsigned int COMPACT_SIGNATURE_SIZE = 65; + /** + * see www.keylength.com + * script supports up to 75 for single byte push + */ + static_assert( + PUBLIC_KEY_SIZE >= COMPRESSED_PUBLIC_KEY_SIZE, + "COMPRESSED_PUBLIC_KEY_SIZE is larger than PUBLIC_KEY_SIZE"); + private: /** @@ -48,9 +52,6 @@ private: * Its length can very cheaply be computed from the first byte. */ unsigned char vch[PUBLIC_KEY_SIZE]; - static_assert( - PUBLIC_KEY_SIZE >= COMPRESSED_PUBLIC_KEY_SIZE, - "COMPRESSED_PUBLIC_KEY_SIZE is larger than PUBLIC_KEY_SIZE"); //! Compute the length of a pubkey with a given first byte. unsigned int static GetLen(unsigned char chHeader) -- cgit v1.2.3