14 #include <boost/test/unit_test.hpp>
24 uint256 msgHash1 =
uint256S(
"0000000000000000000000000000000000000000000000000000000000000001");
25 uint256 msgHash2 =
uint256S(
"0000000000000000000000000000000000000000000000000000000000000002");
27 auto sig1 = sk1.
Sign(msgHash1);
28 auto sig2 = sk2.
Sign(msgHash1);
45 for (
size_t i = 0; i < n; i++) {
54 std::vector<size_t> idxs;
55 for (
size_t i = 0; i < n; i++) {
59 return std::vector<size_t>(idxs.begin(), idxs.begin() + m);
90 std::vector<Member> quorum;
91 for (
const auto&
id : ids) {
92 quorum.emplace_back(
Member(
id));
103 m.contributions.InitEncrypt(N);
104 for (
size_t j = 0; j < N; j++) {
109 BOOST_CHECK(m.contributions.Encrypt(j, quorum[j].pk, plaintext, PROTOCOL_VERSION));
114 for (
size_t i = 0; i < N; i++) {
118 for (
size_t j = 0; j < N; j++) {
120 BOOST_CHECK(quorum[j].contributions.Decrypt(i, m.
sk, contribution, PROTOCOL_VERSION));
121 rcvSkContributions.emplace_back(std::move(contribution));
126 for (
size_t j = 0; j < N; j++) {
129 BOOST_CHECK(rcvSkContributions[j].GetPublicKey() == pkContribution);
130 rcvPkContributions.emplace_back(pkContribution);
139 for (
const Member& m : quorum) {
140 allSigShares.emplace_back(m.skShare.Sign(msg));
147 for (
size_t i : idxs) {
148 skShares.emplace_back(quorum[i].skShare);
149 random_ids.emplace_back(quorum[i].
id);
157 std::vector<BLSVerificationVectorPtr> v;
158 for (
const Member& m : quorum) v.emplace_back(m.vecP);
167 for (
size_t i : idxs2) {
168 sigShares.emplace_back(allSigShares[i]);
169 random_ids2.emplace_back(quorum[i].
id);
183 sigShares[0] = dummy_sig;
210 std::string message =
".mess of len 15";
214 std::string decrypted_message;
219 std::string decrypted_message2;
220 iesEnc.
Decrypt(aliceSk, decrypted_message2, PROTOCOL_VERSION);
224 decrypted_message2.clear();
227 iesEnc.
Decrypt(bobSk, decrypted_message2, PROTOCOL_VERSION);
232 decrypted_message2.clear();
234 iesEnc.
Decrypt(bobSk, decrypted_message2, PROTOCOL_VERSION);
238 template<
typename BLSKey>
248 const auto& params =
Params();
250 CBLSSecretKey sk = FromHex<CBLSSecretKey>(
"2eb071f4c520b3102e8cb9f520783da252d33993dba0313b501d69d113af9d39");
261 encodedSk.push_back(
'f');
266 encodedSk.pop_back();
267 encodedSk.pop_back();
274 const auto& params =
Params();
276 CBLSPublicKey pk = FromHex<CBLSPublicKey>(
"901138a12a352c7e30408c071b1ec097f32ab735a12c8dbb43c637612a3f805668a6bb73894982366d287cf0b02aaf5b");
287 encodedPk.push_back(
'f');
292 encodedPk.pop_back();
293 encodedPk.pop_back();
308 static void AddMessage(std::vector<Message>& vec, uint32_t sourceId, uint32_t msgId, uint32_t msgHash,
bool valid)
328 static void Verify(std::vector<Message>& vec,
bool secureVerification,
bool perMessageFallback)
332 std::set<uint32_t> expectedBadMessages;
333 std::set<uint32_t> expectedBadSources;
334 for (
auto& m : vec) {
336 expectedBadMessages.emplace(m.
msgId);
337 expectedBadSources.emplace(m.
sourceId);
343 batchVerifier.Verify();
345 BOOST_CHECK(batchVerifier.badSources == expectedBadSources);
347 if (perMessageFallback) {
348 BOOST_CHECK(batchVerifier.badMessages == expectedBadMessages);
354 static void Verify(std::vector<Message>& vec)
356 Verify(vec,
false,
false);
357 Verify(vec,
true,
false);
358 Verify(vec,
false,
true);
359 Verify(vec,
true,
true);
364 std::vector<Message> msgs;
367 AddMessage(msgs, 1, 1, 1,
true);
368 AddMessage(msgs, 2, 2, 2,
true);
369 AddMessage(msgs, 3, 3, 3,
true);
373 AddMessage(msgs, 4, 4, 4,
true);
374 AddMessage(msgs, 4, 5, 5,
true);
375 AddMessage(msgs, 4, 6, 6,
true);
379 AddMessage(msgs, 7, 7, 7,
false);
383 AddMessage(msgs, 8, 8, 7,
true);
387 AddMessage(msgs, 9, 9, 7,
true);
392 AddMessage(msgs, 1, 1, 1,
true);
393 AddMessage(msgs, 1, 2, 1,
true);
394 AddMessage(msgs, 1, 3, 1,
true);
395 AddMessage(msgs, 2, 4, 1,
true);
396 AddMessage(msgs, 2, 5, 1,
true);
397 AddMessage(msgs, 2, 6, 1,
true);
401 AddMessage(msgs, 1, 7, 1,
false);
BOOST_AUTO_TEST_CASE(bls_sig_tests)
BLSKey FromHex(const std::string &str)
std::vector< size_t > GetRandomElements(size_t m, size_t n)
std::vector< CBLSId > BLSIdVector
std::vector< CBLSPublicKey > BLSPublicKeyVector
std::shared_ptr< BLSVerificationVector > BLSVerificationVectorPtr
std::vector< CBLSSecretKey > BLSSecretKeyVector
std::vector< CBLSSignature > BLSSignatureVector
const CChainParams & Params()
Return the currently selected parameters.
CBLSPublicKey ephemeralPubKey
bool Decrypt(const CBLSSecretKey &secretKey, Object &objRet, int nVersion) const
bool Encrypt(const CBLSPublicKey &peerPubKey, const Object &obj, int nVersion)
CBLSPublicKey GetPublicKey() const
bool Recover(const std::vector< CBLSSecretKey > &keys, const std::vector< CBLSId > &ids)
CBLSSignature Sign(const uint256 &hash) const
bool VerifyInsecure(const CBLSPublicKey &pubKey, const uint256 &hash) const
bool Recover(const std::vector< CBLSSignature > &sigs, const std::vector< CBLSId > &ids)
CBLSSecretKey AggregateSecretKeys(const BLSSecretKeyVector &secKeys, size_t start=0, size_t count=0, bool parallel=true)
bool VerifyContributionShare(const CBLSId &forId, const BLSVerificationVectorPtr &vvec, const CBLSSecretKey &skContribution)
CBLSPublicKey BuildPubKeyShare(const BLSVerificationVectorPtr &vvec, const CBLSId &id)
BLSVerificationVectorPtr BuildQuorumVerificationVector(const std::vector< BLSVerificationVectorPtr > &vvecs, size_t start=0, size_t count=0, bool parallel=true)
CBLSPublicKey AggregatePublicKeys(const BLSPublicKeyVector &pubKeys, size_t start=0, size_t count=0, bool parallel=true)
bool GenerateContributions(int threshold, const BLSIdVector &ids, BLSVerificationVectorPtr &vvecRet, BLSSecretKeyVector &skShares)
BOOST_AUTO_TEST_SUITE_END()
Optional< CBLSSecretKey > DecodeSecret(const CChainParams ¶ms, const std::string &keyStr)
std::string EncodeSecret(const CChainParams ¶ms, const CBLSSecretKey &key)
Optional< CBLSPublicKey > DecodePublic(const CChainParams ¶ms, const std::string &keyStr)
std::string EncodePublic(const CChainParams ¶ms, const CBLSPublicKey &pk)
#define BOOST_FIXTURE_TEST_SUITE(a, b)
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
void GetRandBytes(unsigned char *buf, int num) noexcept
Overall design of the RNG and entropy sources.
uint256 GetRandHash() noexcept
void Shuffle(I first, I last, R &&rng)
More efficient than using std::shuffle on a FastRandomContext.
Member(const CBLSId &_id)
BLSVerificationVectorPtr vecP
CBLSIESMultiRecipientObjects< CBLSSecretKey > contributions
uint256 uint256S(const char *str)
std::vector< unsigned char > ParseHex(const char *psz)