<commit_before>// Copyright (c) 2018 The Dash Core developers
// Copyright (c) 2021 The PIVX Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "bench.h"
#include "random.h"
#include "bls/bls_worker.h"
#include "utiltime.h"

#include <iostream>

CBLSWorker blsWorker;

void InitBLSTests()
{
    blsWorker.Start();
}

void CleanupBLSTests()
{
    blsWorker.Stop();
}

static void BuildTestVectors(size_t count, size_t invalidCount,
                             BLSPublicKeyVector& pubKeys, BLSSecretKeyVector& secKeys, BLSSignatureVector& sigs,
                             std::vector<uint256>& msgHashes,
                             std::vector<bool>& invalid)
{
    secKeys.resize(count);
    pubKeys.resize(count);
    sigs.resize(count);
    msgHashes.resize(count);

    invalid.resize(count);
    for (size_t i = 0; i < invalidCount; i++) {
        invalid[i] = true;
    }
    std::random_shuffle(invalid.begin(), invalid.end());

    for (size_t i = 0; i < count; i++) {
        secKeys[i].MakeNewKey();
        pubKeys[i] = secKeys[i].GetPublicKey();
        msgHashes[i] = GetRandHash();
        sigs[i] = secKeys[i].Sign(msgHashes[i]);

        if (invalid[i]) {
            CBLSSecretKey s;
            s.MakeNewKey();
            sigs[i] = s.Sign(msgHashes[i]);
        }
    }
}

static void BLSPubKeyAggregate_Normal(benchmark::State& state)
{
    CBLSSecretKey secKey1, secKey2;
    secKey1.MakeNewKey();
    secKey2.MakeNewKey();
    CBLSPublicKey pubKey1 = secKey1.GetPublicKey();
    CBLSPublicKey pubKey2 = secKey2.GetPublicKey();

    // Benchmark.
    while (state.KeepRunning()) {
        CBLSPublicKey k(pubKey1);
        k.AggregateInsecure(pubKey2);
    }
}

static void BLSSecKeyAggregate_Normal(benchmark::State& state)
{
    CBLSSecretKey secKey1, secKey2;
    secKey1.MakeNewKey();
    secKey2.MakeNewKey();
    CBLSPublicKey pubKey1 = secKey1.GetPublicKey();
    CBLSPublicKey pubKey2 = secKey2.GetPublicKey();

    // Benchmark.
    while (state.KeepRunning()) {
        CBLSSecretKey k(secKey1);
        k.AggregateInsecure(secKey2);
    }
}

static void BLSSign_Normal(benchmark::State& state)
{
    CBLSSecretKey secKey;
    secKey.MakeNewKey();
    CBLSPublicKey pubKey = secKey.GetPublicKey();

    // Benchmark.
    while (state.KeepRunning()) {
        uint256 hash = GetRandHash();
        secKey.Sign(hash);
    }
}

static void BLSVerify_Normal(benchmark::State& state)
{
    BLSPublicKeyVector pubKeys;
    BLSSecretKeyVector secKeys;
    BLSSignatureVector sigs;
    std::vector<uint256> msgHashes;
    std::vector<bool> invalid;
    BuildTestVectors(1000, 10, pubKeys, secKeys, sigs, msgHashes, invalid);

    // Benchmark.
    size_t i = 0;
    while (state.KeepRunning()) {
        bool valid = sigs[i].VerifyInsecure(pubKeys[i], msgHashes[i]);
        if (valid && invalid[i]) {
            std::cout << "expected invalid but it is valid" << std::endl;
            assert(false);
        } else if (!valid && !invalid[i]) {
            std::cout << "expected valid but it is invalid" << std::endl;
            assert(false);
        }
        i = (i + 1) % pubKeys.size();
    }<commit_msg>[Trivial] Remove unused variables in bls bench test<commit_after>// Copyright (c) 2018 The Dash Core developers
// Copyright (c) 2021 The PIVX Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "bench.h"
#include "random.h"
#include "bls/bls_worker.h"
#include "utiltime.h"

#include <iostream>

CBLSWorker blsWorker;

void InitBLSTests()
{
    blsWorker.Start();
}

void CleanupBLSTests()
{
    blsWorker.Stop();
}

static void BuildTestVectors(size_t count, size_t invalidCount,
                             BLSPublicKeyVector& pubKeys, BLSSecretKeyVector& secKeys, BLSSignatureVector& sigs,
                             std::vector<uint256>& msgHashes,
                             std::vector<bool>& invalid)
{
    secKeys.resize(count);
    pubKeys.resize(count);
    sigs.resize(count);
    msgHashes.resize(count);

    invalid.resize(count);
    for (size_t i = 0; i < invalidCount; i++) {
        invalid[i] = true;
    }
    std::random_shuffle(invalid.begin(), invalid.end());

    for (size_t i = 0; i < count; i++) {
        secKeys[i].MakeNewKey();
        pubKeys[i] = secKeys[i].GetPublicKey();
        msgHashes[i] = GetRandHash();
        sigs[i] = secKeys[i].Sign(msgHashes[i]);

        if (invalid[i]) {
            CBLSSecretKey s;
            s.MakeNewKey();
            sigs[i] = s.Sign(msgHashes[i]);
        }
    }
}

static void BLSPubKeyAggregate_Normal(benchmark::State& state)
{
    CBLSSecretKey secKey1, secKey2;
    secKey1.MakeNewKey();
    secKey2.MakeNewKey();
    CBLSPublicKey pubKey1 = secKey1.GetPublicKey();
    CBLSPublicKey pubKey2 = secKey2.GetPublicKey();

    // Benchmark.
    while (state.KeepRunning()) {
        CBLSPublicKey k(pubKey1);
        k.AggregateInsecure(pubKey2);
    }
}

static void BLSSecKeyAggregate_Normal(benchmark::State& state)
{
    CBLSSecretKey secKey1, secKey2;
    secKey1.MakeNewKey();
    secKey2.MakeNewKey();

    // Benchmark.
    while (state.KeepRunning()) {
        CBLSSecretKey k(secKey1);
        k.AggregateInsecure(secKey2);
    }
}

static void BLSSign_Normal(benchmark::State& state)
{
    CBLSSecretKey secKey;
    secKey.MakeNewKey();

    // Benchmark.
    while (state.KeepRunning()) {
        uint256 hash = GetRandHash();
        secKey.Sign(hash);
    }
}

static void BLSVerify_Normal(benchmark::State& state)
{
    BLSPublicKeyVector pubKeys;
    BLSSecretKeyVector secKeys;
    BLSSignatureVector sigs;
    std::vector<uint256> msgHashes;
    std::vector<bool> invalid;
    BuildTestVectors(1000, 10, pubKeys, secKeys, sigs, msgHashes, invalid);

    // Benchmark.
    size_t i = 0;
    while (state.KeepRunning()) {
        bool valid = sigs[i].VerifyInsecure(pubKeys[i], msgHashes[i]);
        if (valid && invalid[i]) {
            std::cout << "expected invalid but it is valid" << std::endl;
            assert(false);
        } else if (!valid && !invalid[i]) {
            std::cout << "expected valid but it is invalid" << std::endl;
            assert(false);
        }
        i = (i + 1) % pubKeys.size();
    }
{'fraction_non_alphanumeric': 0.13067292644757433, 'fraction_numerical': 0.014084507042253521, 'mean_word_length': 2.774955699940933, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4287705', 'n_tokens_mistral': 2229, 'n_tokens_neox': 2150, 'n_words': 434}