Skip to content

Commit acb6a7c

Browse files
committed
make custom Batch type for secp256k1_batch
this is a bit of a hack, we can probably be smarter using a named api struct for the batch verification module
1 parent 3475a07 commit acb6a7c

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

src/batchverify.cpp

+16-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212
#include <secp256k1_batch.h>
1313
#include <secp256k1_schnorrsig_batch.h>
1414

15+
class Batch {
16+
private:
17+
secp256k1_batch* m_batch;
18+
public:
19+
Batch(secp256k1_batch* batch) : m_batch(batch) {}
20+
secp256k1_batch* get() const { return m_batch; }
21+
};
22+
1523
BatchSchnorrVerifier::BatchSchnorrVerifier() {
1624
unsigned char rnd[16];
1725
GetRandBytes(rnd);
@@ -20,26 +28,29 @@ BatchSchnorrVerifier::BatchSchnorrVerifier() {
2028
// still efficient.
2129
const size_t max_batch_size{106};
2230
secp256k1_batch* batch{secp256k1_batch_create(secp256k1_context_static, max_batch_size, rnd)};
23-
m_batch = batch;
31+
m_batch = new Batch(batch);
2432
}
2533

2634
BatchSchnorrVerifier::~BatchSchnorrVerifier() {
27-
(void)secp256k1_batch_destroy(secp256k1_context_static, m_batch);
35+
if (m_batch) {
36+
(void)secp256k1_batch_destroy(secp256k1_context_static, m_batch->get());
37+
delete m_batch;
38+
}
2839
}
2940

3041
bool BatchSchnorrVerifier::Add(const Span<const unsigned char> sig, const XOnlyPubKey& pubkey, const uint256& sighash) {
3142
LOCK(m_batch_mutex);
32-
if (secp256k1_batch_usable(secp256k1_context_static, m_batch) == 0) {
43+
if (secp256k1_batch_usable(secp256k1_context_static, m_batch->get()) == 0) {
3344
LogPrintf("ERROR: BatchSchnorrVerifier m_batch unusable\n");
3445
return false;
3546
}
3647

3748
secp256k1_xonly_pubkey pubkey_parsed;
3849
if (!secp256k1_xonly_pubkey_parse(secp256k1_context_static, &pubkey_parsed, pubkey.data())) return false;
39-
return secp256k1_batch_add_schnorrsig(secp256k1_context_static, m_batch, sig.data(), sighash.begin(), 32, &pubkey_parsed);
50+
return secp256k1_batch_add_schnorrsig(secp256k1_context_static, m_batch->get(), sig.data(), sighash.begin(), 32, &pubkey_parsed);
4051
}
4152

4253
bool BatchSchnorrVerifier::Verify() {
4354
LOCK(m_batch_mutex);
44-
return secp256k1_batch_verify(secp256k1_context_static, m_batch);
55+
return secp256k1_batch_verify(secp256k1_context_static, m_batch->get());
4556
}

src/batchverify.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
#include <pubkey.h>
99
#include <sync.h>
1010

11-
#include <secp256k1_batch.h>
12-
11+
class Batch;
1312
class BatchSchnorrVerifier {
1413
private:
15-
secp256k1_batch* m_batch GUARDED_BY(m_batch_mutex);
14+
Batch* m_batch GUARDED_BY(m_batch_mutex);
1615
mutable Mutex m_batch_mutex;
1716

1817
public:

0 commit comments

Comments
 (0)