Skip to content

Commit e181dbe

Browse files
committed
Add comments
1 parent a3603ac commit e181dbe

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/key.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,22 @@
1717
static secp256k1_context* secp256k1_context_sign = NULL;
1818

1919
/** These functions are taken from the libsecp256k1 distribution and are very ugly. */
20+
21+
/**
22+
* This parses a format loosely based on a DER encoding of the ECPrivateKey type from
23+
* section C.4 of SEC 1 <http://www.secg.org/sec1-v2.pdf>, with the following caveats:
24+
*
25+
* * The octet-length of the SEQUENCE must be encoded as 1 or 2 octets. It is not
26+
* required to be encoded as one octet if it is less than 256, as DER would require.
27+
* * The octet-length of the SEQUENCE must not be greater than the remaining
28+
* length of the key encoding, but need not match it (i.e. the encoding may contain
29+
* junk after the encoded SEQUENCE).
30+
* * The privateKey OCTET STRING is zero-filled on the left to 32 octets.
31+
* * Anything after the encoding of the privateKey OCTET STRING is ignored, whether
32+
* or not it is validly encoded DER.
33+
*
34+
* out32 must point to an output buffer of length at least 32 bytes.
35+
*/
2036
static int ec_privkey_import_der(const secp256k1_context* ctx, unsigned char *out32, const unsigned char *privkey, size_t privkeylen) {
2137
const unsigned char *end = privkey + privkeylen;
2238
size_t lenb = 0;
@@ -66,6 +82,13 @@ static int ec_privkey_import_der(const secp256k1_context* ctx, unsigned char *ou
6682
return 1;
6783
}
6884

85+
/**
86+
* This serializes to a DER encoding of the ECPrivateKey type from section C.4 of SEC 1
87+
* <http://www.secg.org/sec1-v2.pdf>. The optional parameters and publicKey fields are
88+
* included.
89+
*
90+
* key32 must point to a 32-byte raw private key.
91+
*/
6992
static int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *privkey, size_t *privkeylen, const unsigned char *key32, int compressed) {
7093
secp256k1_pubkey pubkey;
7194
size_t pubkeylen = 0;

src/script/interpreter.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,13 @@ bool static IsLowDERSignature(const valtype &vchSig, ScriptError* serror) {
174174
if (!IsValidSignatureEncoding(vchSig)) {
175175
return set_error(serror, SCRIPT_ERR_SIG_DER);
176176
}
177+
// https://bitcoin.stackexchange.com/a/12556:
178+
// Also note that inside transaction signatures, an extra hashtype byte
179+
// follows the actual signature data.
177180
std::vector<unsigned char> vchSigCopy(vchSig.begin(), vchSig.begin() + vchSig.size() - 1);
181+
// If the S value is above the order of the curve divided by two, its
182+
// complement modulo the order could have been used instead, which is
183+
// one byte shorter when encoded correctly.
178184
if (!CPubKey::CheckLowS(vchSigCopy)) {
179185
return set_error(serror, SCRIPT_ERR_SIG_HIGH_S);
180186
}

0 commit comments

Comments
 (0)