Skip to content

Commit

Permalink
make custom Batch type for secp256k1_batch
Browse files Browse the repository at this point in the history
this is a bit of a hack, we can probably be smarter using a named api struct for
the batch verification module
  • Loading branch information
josibake authored and Eunovo committed Nov 7, 2024
1 parent 3ed4aa2 commit 50f5f93
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
21 changes: 16 additions & 5 deletions src/batchverify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
#include <secp256k1_batch.h>
#include <secp256k1_schnorrsig_batch.h>

class Batch {
private:
secp256k1_batch* m_batch;
public:
Batch(secp256k1_batch* batch) : m_batch(batch) {}
secp256k1_batch* get() const { return m_batch; }
};

BatchSchnorrVerifier::BatchSchnorrVerifier() {
unsigned char rnd[16];
GetRandBytes(rnd);
Expand All @@ -20,26 +28,29 @@ BatchSchnorrVerifier::BatchSchnorrVerifier() {
// 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;
m_batch = new Batch(batch);
}

BatchSchnorrVerifier::~BatchSchnorrVerifier() {
(void)secp256k1_batch_destroy(secp256k1_context_static, m_batch);
if (m_batch) {
(void)secp256k1_batch_destroy(secp256k1_context_static, m_batch->get());
delete 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) {
if (secp256k1_batch_usable(secp256k1_context_static, m_batch->get()) == 0) {
LogPrintf("ERROR: BatchSchnorrVerifier m_batch unusable\n");
return false;
}

secp256k1_xonly_pubkey pubkey_parsed;
if (!secp256k1_xonly_pubkey_parse(secp256k1_context_static, &pubkey_parsed, pubkey.data())) return false;
return secp256k1_batch_add_schnorrsig(secp256k1_context_static, m_batch, sig.data(), sighash.begin(), 32, &pubkey_parsed);
return secp256k1_batch_add_schnorrsig(secp256k1_context_static, m_batch->get(), sig.data(), sighash.begin(), 32, &pubkey_parsed);
}

bool BatchSchnorrVerifier::Verify() {
LOCK(m_batch_mutex);
return secp256k1_batch_verify(secp256k1_context_static, m_batch);
return secp256k1_batch_verify(secp256k1_context_static, m_batch->get());
}
5 changes: 2 additions & 3 deletions src/batchverify.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
#include <pubkey.h>
#include <sync.h>

#include <secp256k1_batch.h>

class Batch;
class BatchSchnorrVerifier {
private:
secp256k1_batch* m_batch GUARDED_BY(m_batch_mutex);
Batch* m_batch GUARDED_BY(m_batch_mutex);
mutable Mutex m_batch_mutex;

public:
Expand Down

0 comments on commit 50f5f93

Please sign in to comment.