UNPKG

@covenance/dlc

Version:

Crypto and Bitcoin functions for Covenance DLC implementation

73 lines (63 loc) 2.6 kB
import { Point, utils } from '../src/crypto/secp256k1'; import { createAdaptorSig, verifyAdaptorSig } from '../src/crypto/counterparty'; // Number of signatures to create and verify const NUM_SIGNATURES = 10000; async function benchmark(): Promise<void> { console.log(`Benchmarking ${NUM_SIGNATURES} adaptor signatures...\n`); // Generate test keys const counterpartyPrivKey = utils.randomPrivateKey(); const counterpartyPubKey = Point.fromPrivateKey(counterpartyPrivKey); // Generate test event outcome hashes const eventOutcomeHashes = Array(NUM_SIGNATURES).fill(0).map(() => utils.randomPrivateKey() ); // Generate test CET sighashes const cetSighashes = Array(NUM_SIGNATURES).fill(0).map(() => utils.randomPrivateKey() ); // Precalculate oracle signature points const oracleSigPoints = Array(NUM_SIGNATURES).fill(0).map(() => Point.fromPrivateKey(utils.randomPrivateKey()) ); // Benchmark signature creation console.log('Creating adaptor signatures...'); const startCreate = performance.now(); const adaptorSigs = await Promise.all( eventOutcomeHashes.map(async (_, i) => { return createAdaptorSig( counterpartyPrivKey, oracleSigPoints[i], cetSighashes[i] ); }) ); const endCreate = performance.now(); const createTime = endCreate - startCreate; const signsPerSecond = (NUM_SIGNATURES / createTime) * 1000; console.log(`Created ${NUM_SIGNATURES} adaptor signatures in ${createTime}ms`); console.log(`Average time per signature: ${createTime / NUM_SIGNATURES}ms`); console.log(`Signatures per second: ${signsPerSecond.toFixed(2)}\n`); // Benchmark signature verification console.log('Verifying adaptor signatures...'); const startVerify = performance.now(); const verifyResults = await Promise.all( adaptorSigs.map(async (sig, i) => { return verifyAdaptorSig( sig, counterpartyPubKey, cetSighashes[i], oracleSigPoints[i] ); }) ); const endVerify = performance.now(); const verifyTime = endVerify - startVerify; const verificationsPerSecond = (NUM_SIGNATURES / verifyTime) * 1000; console.log(`Verified ${NUM_SIGNATURES} adaptor signatures in ${verifyTime}ms`); console.log(`Average time per verification: ${verifyTime / NUM_SIGNATURES}ms`); console.log(`Verifications per second: ${verificationsPerSecond.toFixed(2)}\n`); // Verify all signatures were valid const allValid = verifyResults.every(result => result); console.log(`All signatures valid: ${allValid}`); } benchmark().catch(console.error);