This repository was archived by the owner on Jul 24, 2024. It is now read-only.
forked from cosanta/cosanta-core
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbls_tests.cpp
158 lines (131 loc) · 4.78 KB
/
bls_tests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright (c) 2019 The Dash Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <bls/bls.h>
#include <bls/bls_batchverifier.h>
#include <random.h>
#include <test/util/setup_common.h>
#include <boost/test/unit_test.hpp>
BOOST_FIXTURE_TEST_SUITE(bls_tests, BasicTestingSetup)
BOOST_AUTO_TEST_CASE(bls_sethexstr_tests)
{
CBLSSecretKey sk;
std::string strValidSecret = "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f";
// Note: invalid string passed to SetHexStr() should cause it to fail and reset key internal data
BOOST_CHECK(sk.SetHexStr(strValidSecret));
BOOST_CHECK(!sk.SetHexStr("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1g")); // non-hex
BOOST_CHECK(!sk.IsValid());
BOOST_CHECK(sk == CBLSSecretKey());
// Try few more invalid strings
BOOST_CHECK(sk.SetHexStr(strValidSecret));
BOOST_CHECK(!sk.SetHexStr("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e")); // hex but too short
BOOST_CHECK(!sk.IsValid());
BOOST_CHECK(sk.SetHexStr(strValidSecret));
BOOST_CHECK(!sk.SetHexStr("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20")); // hex but too long
BOOST_CHECK(!sk.IsValid());
}
BOOST_AUTO_TEST_CASE(bls_sig_tests)
{
CBLSSecretKey sk1, sk2;
sk1.MakeNewKey();
sk2.MakeNewKey();
uint256 msgHash1 = uint256S("0000000000000000000000000000000000000000000000000000000000000001");
uint256 msgHash2 = uint256S("0000000000000000000000000000000000000000000000000000000000000002");
auto sig1 = sk1.Sign(msgHash1);
auto sig2 = sk2.Sign(msgHash1);
BOOST_CHECK(sig1.VerifyInsecure(sk1.GetPublicKey(), msgHash1));
BOOST_CHECK(!sig1.VerifyInsecure(sk1.GetPublicKey(), msgHash2));
BOOST_CHECK(!sig2.VerifyInsecure(sk1.GetPublicKey(), msgHash1));
BOOST_CHECK(!sig2.VerifyInsecure(sk2.GetPublicKey(), msgHash2));
BOOST_CHECK(sig2.VerifyInsecure(sk2.GetPublicKey(), msgHash1));
}
struct Message
{
uint32_t sourceId;
uint32_t msgId;
uint256 msgHash;
CBLSSecretKey sk;
CBLSPublicKey pk;
CBLSSignature sig;
bool valid;
};
static void AddMessage(std::vector<Message>& vec, uint32_t sourceId, uint32_t msgId, uint32_t msgHash, bool valid)
{
Message m;
m.sourceId = sourceId;
m.msgId = msgId;
*((uint32_t*)m.msgHash.begin()) = msgHash;
m.sk.MakeNewKey();
m.pk = m.sk.GetPublicKey();
m.sig = m.sk.Sign(m.msgHash);
m.valid = valid;
if (!valid) {
CBLSSecretKey tmp;
tmp.MakeNewKey();
m.sig = tmp.Sign(m.msgHash);
}
vec.emplace_back(m);
}
static void Verify(std::vector<Message>& vec, bool secureVerification, bool perMessageFallback)
{
CBLSBatchVerifier<uint32_t, uint32_t> batchVerifier(secureVerification, perMessageFallback);
std::set<uint32_t> expectedBadMessages;
std::set<uint32_t> expectedBadSources;
for (auto& m : vec) {
if (!m.valid) {
expectedBadMessages.emplace(m.msgId);
expectedBadSources.emplace(m.sourceId);
}
batchVerifier.PushMessage(m.sourceId, m.msgId, m.msgHash, m.sig, m.pk);
}
batchVerifier.Verify();
BOOST_CHECK(batchVerifier.badSources == expectedBadSources);
if (perMessageFallback) {
BOOST_CHECK(batchVerifier.badMessages == expectedBadMessages);
} else {
BOOST_CHECK(batchVerifier.badMessages.empty());
}
}
static void Verify(std::vector<Message>& vec)
{
Verify(vec, false, false);
Verify(vec, true, false);
Verify(vec, false, true);
Verify(vec, true, true);
}
BOOST_AUTO_TEST_CASE(batch_verifier_tests)
{
std::vector<Message> msgs;
// distinct messages from distinct sources
AddMessage(msgs, 1, 1, 1, true);
AddMessage(msgs, 2, 2, 2, true);
AddMessage(msgs, 3, 3, 3, true);
Verify(msgs);
// distinct messages from same source
AddMessage(msgs, 4, 4, 4, true);
AddMessage(msgs, 4, 5, 5, true);
AddMessage(msgs, 4, 6, 6, true);
Verify(msgs);
// invalid sig
AddMessage(msgs, 7, 7, 7, false);
Verify(msgs);
// same message as before, but from another source and with valid sig
AddMessage(msgs, 8, 8, 7, true);
Verify(msgs);
// same message as before, but from another source and signed with another key
AddMessage(msgs, 9, 9, 7, true);
Verify(msgs);
msgs.clear();
// same message, signed by multiple keys
AddMessage(msgs, 1, 1, 1, true);
AddMessage(msgs, 1, 2, 1, true);
AddMessage(msgs, 1, 3, 1, true);
AddMessage(msgs, 2, 4, 1, true);
AddMessage(msgs, 2, 5, 1, true);
AddMessage(msgs, 2, 6, 1, true);
Verify(msgs);
// last message invalid from one source
AddMessage(msgs, 1, 7, 1, false);
Verify(msgs);
}
BOOST_AUTO_TEST_SUITE_END()