Skip to content

Commit d1e9cda

Browse files
authored
Merge pull request #1649 from evoskuil/master
Stub in bip342 signature operations.
2 parents caa397c + cf135f5 commit d1e9cda

File tree

20 files changed

+805
-641
lines changed

20 files changed

+805
-641
lines changed

Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ src_libbitcoin_system_la_SOURCES = \
5050
src/chain/point.cpp \
5151
src/chain/script.cpp \
5252
src/chain/transaction.cpp \
53+
src/chain/transaction_sign.cpp \
5354
src/chain/witness.cpp \
5455
src/chain/enums/opcode.cpp \
5556
src/config/base16.cpp \

builds/cmake/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,7 @@ add_library( ${CANONICAL_LIB_NAME}
491491
"../../src/chain/point.cpp"
492492
"../../src/chain/script.cpp"
493493
"../../src/chain/transaction.cpp"
494+
"../../src/chain/transaction_sign.cpp"
494495
"../../src/chain/witness.cpp"
495496
"../../src/chain/enums/opcode.cpp"
496497
"../../src/config/base16.cpp"

builds/msvc/vs2022/libbitcoin-system/libbitcoin-system.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
<ClCompile Include="..\..\..\..\src\chain\transaction.cpp">
103103
<ObjectFileName>$(IntDir)src_chain_transaction.obj</ObjectFileName>
104104
</ClCompile>
105+
<ClCompile Include="..\..\..\..\src\chain\transaction_sign.cpp" />
105106
<ClCompile Include="..\..\..\..\src\chain\witness.cpp" />
106107
<ClCompile Include="..\..\..\..\src\config\base16.cpp" />
107108
<ClCompile Include="..\..\..\..\src\config\base2.cpp" />

builds/msvc/vs2022/libbitcoin-system/libbitcoin-system.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,9 @@
279279
<ClCompile Include="..\..\..\..\src\chain\transaction.cpp">
280280
<Filter>src\chain</Filter>
281281
</ClCompile>
282+
<ClCompile Include="..\..\..\..\src\chain\transaction_sign.cpp">
283+
<Filter>src\chain</Filter>
284+
</ClCompile>
282285
<ClCompile Include="..\..\..\..\src\chain\witness.cpp">
283286
<Filter>src\chain</Filter>
284287
</ClCompile>

include/bitcoin/system/chain/enums/coverage.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ namespace chain {
3030
/// Comments from: bitcoin.org/en/developer-guide#standard-transactions
3131
enum coverage : uint8_t
3232
{
33+
/// BIP341: We define a new hashtype SIGHASH_DEFAULT (value 0x00) which
34+
/// results in signing over the whole transaction just as for SIGHASH_ALL.
35+
hash_default = 0,
36+
3337
/// The default, signs all the inputs and outputs, protecting everything
3438
/// except the signature scripts against modification.
3539
hash_all = bit_right<uint8_t>(0),

include/bitcoin/system/chain/transaction.hpp

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,18 @@ class BC_API transaction
138138
/// Assumes coinbase if prevout not populated (returns only legacy sigops).
139139
size_t signature_operations(bool bip16, bool bip141) const NOEXCEPT;
140140

141-
// signature_hash exposed for op_check_multisig caching.
141+
/// signature_hash exposed for op_check_multisig caching.
142142
hash_digest signature_hash(const input_iterator& input, const script& sub,
143143
uint64_t value, uint8_t sighash_flags, script_version version,
144144
bool bip143) const NOEXCEPT;
145145

146+
/// Not used internally.
146147
bool check_signature(const ec_signature& signature,
147148
const data_slice& public_key, const script& sub, uint32_t index,
148149
uint64_t value, uint8_t sighash_flags, script_version version,
149150
bool bip143) const NOEXCEPT;
150151

152+
/// Not used internally.
151153
bool create_endorsement(endorsement& out, const ec_secret& secret,
152154
const script& sub, uint32_t index, uint64_t value,
153155
uint8_t sighash_flags, script_version version,
@@ -233,43 +235,50 @@ class BC_API transaction
233235

234236
private:
235237
typedef struct { size_t nominal; size_t witnessed; } sizes;
238+
239+
static bool segregated(const chain::inputs& inputs) NOEXCEPT;
240+
static bool segregated(const input_cptrs& inputs) NOEXCEPT;
241+
static sizes serialized_size(const input_cptrs& inputs,
242+
const output_cptrs& outputs, bool segregated) NOEXCEPT;
243+
244+
void assign_data(reader& source, bool witness) NOEXCEPT;
245+
chain::points points() const NOEXCEPT;
246+
247+
// Signing.
248+
// ------------------------------------------------------------------------
249+
236250
typedef struct
237251
{
238252
hash_digest outputs;
239253
hash_digest points;
240254
hash_digest sequences;
241255
} sighash_cache;
242256

243-
static bool segregated(const chain::inputs& inputs) NOEXCEPT;
244-
static bool segregated(const input_cptrs& inputs) NOEXCEPT;
245-
static sizes serialized_size(const input_cptrs& inputs,
246-
const output_cptrs& outputs, bool segregated) NOEXCEPT;
257+
static inline coverage mask_sighash(uint8_t sighash_flags) NOEXCEPT;
258+
static inline bool is_sighash_valid(uint8_t sighash_flags) NOEXCEPT;
247259

248-
void assign_data(reader& source, bool witness) NOEXCEPT;
260+
hash_digest outputs_hash() const NOEXCEPT;
261+
hash_digest points_hash() const NOEXCEPT;
262+
hash_digest sequences_hash() const NOEXCEPT;
263+
void initialize_sighash_cache() const NOEXCEPT;
249264

250-
// signature hash
251-
hash_digest output_hash(const input_iterator& input) const NOEXCEPT;
252265
input_iterator input_at(uint32_t index) const NOEXCEPT;
253266
uint32_t input_index(const input_iterator& input) const NOEXCEPT;
267+
hash_digest output_hash(const input_iterator& input) const NOEXCEPT;
268+
254269
void signature_hash_single(writer& sink, const input_iterator& input,
255270
const script& sub, uint8_t sighash_flags) const NOEXCEPT;
256271
void signature_hash_none(writer& sink, const input_iterator& input,
257272
const script& sub, uint8_t sighash_flags) const NOEXCEPT;
258273
void signature_hash_all(writer& sink, const input_iterator& input,
259274
const script& sub, uint8_t sighash_flags) const NOEXCEPT;
275+
260276
hash_digest unversioned_signature_hash(const input_iterator& input,
261277
const script& sub, uint8_t sighash_flags) const NOEXCEPT;
262278
hash_digest version_0_signature_hash(const input_iterator& input,
263279
const script& sub, uint64_t value, uint8_t sighash_flags,
264280
bool bip143) const NOEXCEPT;
265281

266-
// Caching.
267-
chain::points points() const NOEXCEPT;
268-
hash_digest outputs_hash() const NOEXCEPT;
269-
hash_digest points_hash() const NOEXCEPT;
270-
hash_digest sequences_hash() const NOEXCEPT;
271-
void initialize_sighash_cache() const NOEXCEPT;
272-
273282
// Transaction should be stored as shared (adds 16 bytes).
274283
// copy: 5 * 64 + 2 = 41 bytes (vs. 16 when shared).
275284
uint32_t version_;

include/bitcoin/system/crypto/secp256k1.hpp

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -201,22 +201,6 @@ BC_API bool sign(ec_signature& out, const ec_secret& secret,
201201
BC_API bool verify_signature(const data_slice& point, const hash_digest& hash,
202202
const ec_signature& signature) NOEXCEPT;
203203

204-
/// Schnorr parse/sign/verify
205-
/// ---------------------------------------------------------------------------
206-
/// It is recommended to verify a signature after signing.
207-
208-
/// Parse Schnorr endorsement into signature hash type and Schnorr signature.
209-
BC_API bool parse_schnorr(uint8_t& sighash_flags, ec_signature& signature,
210-
const endorsement& endorsement) NOEXCEPT;
211-
212-
/// Create a Schnorr signature using a private key (simple version, no tweaks).
213-
BC_API bool sign_schnorr(ec_signature& out, const ec_secret& secret,
214-
const hash_digest& hash, const hash_digest& auxiliary) NOEXCEPT;
215-
216-
/// Verify an Schnorr signature using a potential x-only point.
217-
BC_API bool verify_schnorr(const data_slice& x_point, const hash_digest& hash,
218-
const ec_signature& signature) NOEXCEPT;
219-
220204
/// ECDSA recoverable sign/recover
221205
/// ---------------------------------------------------------------------------
222206
/// It is recommended to verify a signature after signing.
@@ -235,6 +219,28 @@ BC_API bool recover_public(ec_uncompressed& out,
235219
const recoverable_signature& recoverable,
236220
const hash_digest& hash) NOEXCEPT;
237221

222+
namespace schnorr {
223+
224+
static constexpr size_t signature_size = 64;
225+
static constexpr size_t public_key_size = 32;
226+
227+
/// Schnorr parse/sign/verify
228+
/// ---------------------------------------------------------------------------
229+
/// It is recommended to verify a signature after signing.
230+
231+
/// Parse Schnorr endorsement into signature hash type and Schnorr signature.
232+
BC_API bool parse(uint8_t& sighash_flags, ec_signature& signature,
233+
const endorsement& endorsement) NOEXCEPT;
234+
235+
/// Create a Schnorr signature using a private key (simple version, no tweaks).
236+
BC_API bool sign(ec_signature& out, const ec_secret& secret,
237+
const hash_digest& hash, const hash_digest& auxiliary) NOEXCEPT;
238+
239+
/// Verify an Schnorr signature using a potential x-only point.
240+
BC_API bool verify_signature(const data_slice& x_point,
241+
const hash_digest& hash, const ec_signature& signature) NOEXCEPT;
242+
243+
} // namespace schnorr
238244
} // namespace system
239245
} // namespace libbitcoin
240246

include/bitcoin/system/error/op_error_t.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ enum op_error_t : uint8_t
102102
op_check_sig_verify2,
103103
op_check_sig_verify3,
104104
op_check_sig_verify4,
105+
op_check_sig_verify5,
105106
op_check_sig_verify_parse,
106107
op_check_multisig_verify1,
107108
op_check_multisig_verify2,

0 commit comments

Comments
 (0)