forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BatchVerify: Add basic schnorr sig batch verification in ConnectBlock()
- Loading branch information
Showing
8 changed files
with
132 additions
and
8 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) 2009-2010 Satoshi Nakamoto | ||
// Copyright (c) 2009-2021 The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#include <batchverify.h> | ||
#include <logging.h> | ||
#include <pubkey.h> | ||
#include <random.h> | ||
#include <sync.h> | ||
|
||
#include <secp256k1.h> | ||
#include <secp256k1_batch.h> | ||
#include <secp256k1_schnorrsig_batch.h> | ||
|
||
BatchSchnorrVerifier::BatchSchnorrVerifier() { | ||
unsigned char rnd[16]; | ||
GetRandBytes(rnd); | ||
// This is the maximum number of scalar-point pairs on the batch for which | ||
// Strauss' algorithm, which is used in the secp256k1 implementation, is | ||
// still efficient. | ||
const size_t max_batch_size{106}; | ||
secp256k1_batch* batch{secp256k1_batch_create(secp256k1_context_static, max_batch_size, rnd)}; | ||
m_batch = batch; | ||
} | ||
|
||
BatchSchnorrVerifier::~BatchSchnorrVerifier() { | ||
(void)secp256k1_batch_destroy(secp256k1_context_static, m_batch); | ||
} | ||
|
||
bool BatchSchnorrVerifier::Add(const Span<const unsigned char> sig, const XOnlyPubKey& pubkey, const uint256& sighash) { | ||
LOCK(m_batch_mutex); | ||
if (secp256k1_batch_usable(secp256k1_context_static, m_batch) == 0) { | ||
LogPrintf("ERROR: BatchSchnorrVerifier m_batch unusable\n"); | ||
return false; | ||
} | ||
|
||
secp256k1_xonly_pubkey pubkey_parsed; | ||
(void)secp256k1_xonly_pubkey_parse(secp256k1_context_static, &pubkey_parsed, pubkey.data()); | ||
|
||
return secp256k1_batch_add_schnorrsig(secp256k1_context_static, m_batch, sig.data(), sighash.begin(), 32, &pubkey_parsed); | ||
} | ||
|
||
bool BatchSchnorrVerifier::Verify() { | ||
LOCK(m_batch_mutex); | ||
return secp256k1_batch_verify(secp256k1_context_static, m_batch); | ||
} |
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,27 @@ | ||
// Copyright (c) 2009-2010 Satoshi Nakamoto | ||
// Copyright (c) 2009-2021 The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#ifndef BITCOIN_BATCHVERIFY_H | ||
#define BITCOIN_BATCHVERIFY_H | ||
|
||
#include <pubkey.h> | ||
#include <sync.h> | ||
|
||
#include <secp256k1_batch.h> | ||
|
||
class BatchSchnorrVerifier { | ||
private: | ||
secp256k1_batch* m_batch GUARDED_BY(m_batch_mutex); | ||
mutable Mutex m_batch_mutex; | ||
|
||
public: | ||
BatchSchnorrVerifier(); | ||
~BatchSchnorrVerifier(); | ||
|
||
bool Add(const Span<const unsigned char> sig, const XOnlyPubKey& pubkey, const uint256& sighash) EXCLUSIVE_LOCKS_REQUIRED(!m_batch_mutex); | ||
bool Verify() EXCLUSIVE_LOCKS_REQUIRED(!m_batch_mutex); | ||
}; | ||
|
||
#endif // BITCOIN_BATCHVERIFY_H |
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
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