Skip to content

Commit dd96032

Browse files
Merge #6494: depends: update 'src/dashbls' to dashpay/bls-signatures@0bb5c5b0 as efd5c56
3bbe16c build: stop tracking cmake dependency relic_conf.h.in (Kittywhiskers Van Gogh) efd5c56 Squashed 'src/dashbls/' changes from 7e747e8a07..0bb5c5b032 (Kittywhiskers Van Gogh) 257fd5e revert: stop tracking cmake dependency relic_conf.h.in (Kittywhiskers Van Gogh) Pull request description: ## Additional Information * Dependency for #6493 * Expected subtree hash `7bec74f04710e6031590283cf405e3f141bc63310cafe5e70aae9b8d4c98cbef` (see [instructions](#6323 (review)) to calculate) * Includes [bls-signatures#75](dashpay/bls-signatures#75) and [bls-signatures#106](dashpay/bls-signatures#106) ## Breaking Changes None expected. ## Checklist: - [x] I have performed a self-review of my own code **(note: N/A)** - [x] I have commented my code, particularly in hard-to-understand areas **(note: N/A)** - [x] I have added or updated relevant unit/integration/functional/e2e tests **(note: N/A)** - [x] I have made corresponding changes to the documentation **(note: N/A)** - [x] I have assigned this pull request to a milestone _(for repository code-owners and collaborators only)_ ACKs for top commit: PastaPastaPasta: utACK 3bbe16c UdjinM6: subtree looks good, utACK 3bbe16c Tree-SHA512: 3f6853f90dfe5e3040189742858b6728e4ab505513202216f1e2f7213569798d2f2e346d73ece7505f87dc2439fde4c3a51472461163fc7c21734a734cbc0bdb
2 parents a5d5400 + 3bbe16c commit dd96032

File tree

6 files changed

+90
-38
lines changed

6 files changed

+90
-38
lines changed

src/dashbls/configure.ac

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
AC_PREREQ([2.60])
2-
AC_INIT([libdashbls],[1.3.4])
2+
AC_INIT([libdashbls],[1.3.5])
33
AC_CONFIG_AUX_DIR([build-aux])
44
AC_CONFIG_MACRO_DIR([build-aux/m4])
55

src/dashbls/include/dashbls/elements.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class G1Element {
5959
GTElement Pair(const G2Element &b) const;
6060
uint32_t GetFingerprint(bool fLegacy = false) const;
6161
std::vector<uint8_t> Serialize(bool fLegacy = false) const;
62+
std::array<uint8_t, SIZE> SerializeToArray(bool fLegacy = false) const;
6263
G1Element Copy();
6364

6465
friend bool operator==(const G1Element &a, const G1Element &b);
@@ -102,6 +103,7 @@ class G2Element {
102103
G2Element Negate() const;
103104
GTElement Pair(const G1Element &a) const;
104105
std::vector<uint8_t> Serialize(bool fLegacy = false) const;
106+
std::array<uint8_t, G2Element::SIZE> SerializeToArray(bool fLegacy = false) const;
105107
G2Element Copy();
106108

107109
friend bool operator==(G2Element const &a, G2Element const &b);
@@ -127,6 +129,7 @@ class GTElement {
127129

128130
void Serialize(uint8_t *buffer) const;
129131
std::vector<uint8_t> Serialize() const;
132+
std::array<uint8_t, SIZE> SerializeToArray() const;
130133

131134
friend bool operator==(GTElement const &a, GTElement const &b);
132135
friend bool operator!=(GTElement const &a, GTElement const &b);

src/dashbls/include/dashbls/privatekey.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class PrivateKey {
8282
// Serialize the key into bytes
8383
void Serialize(uint8_t *buffer) const;
8484
std::vector<uint8_t> Serialize(bool fLegacy = false) const;
85+
std::array<uint8_t, PrivateKey::PRIVATE_KEY_SIZE> SerializeToArray(bool fLegacy = false) const;
8586

8687
G2Element SignG2(
8788
const uint8_t *msg,

src/dashbls/src/elements.cpp

+24-4
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,16 @@ uint32_t G1Element::GetFingerprint(const bool fLegacy) const
171171
}
172172

173173
std::vector<uint8_t> G1Element::Serialize(const bool fLegacy) const {
174+
const auto arr = G1Element::SerializeToArray(fLegacy);
175+
return std::vector<uint8_t>{arr.begin(), arr.end()};
176+
}
177+
178+
std::array<uint8_t, G1Element::SIZE> G1Element::SerializeToArray(const bool fLegacy) const {
174179
uint8_t buffer[G1Element::SIZE + 1];
175180
g1_write_bin(buffer, G1Element::SIZE + 1, p, 1);
176181

182+
std::array<uint8_t, G1Element::SIZE> result{};
177183
if (buffer[0] == 0x00) { // infinity
178-
std::vector<uint8_t> result(G1Element::SIZE, 0);
179184
result[0] = 0xc0;
180185
return result;
181186
}
@@ -187,7 +192,9 @@ std::vector<uint8_t> G1Element::Serialize(const bool fLegacy) const {
187192
if (!fLegacy) {
188193
buffer[1] |= 0x80; // indicate compression
189194
}
190-
return std::vector<uint8_t>(buffer + 1, buffer + 1 + G1Element::SIZE);
195+
196+
std::copy_n(buffer + 1, G1Element::SIZE, result.begin());
197+
return result;
191198
}
192199

193200
bool operator==(const G1Element & a, const G1Element &b)
@@ -386,11 +393,18 @@ G2Element G2Element::Negate() const
386393
GTElement G2Element::Pair(const G1Element& a) const { return a & (*this); }
387394

388395
std::vector<uint8_t> G2Element::Serialize(const bool fLegacy) const {
396+
const auto arr = G2Element::SerializeToArray(fLegacy);
397+
return std::vector<uint8_t>{arr.begin(), arr.end()};
398+
}
399+
400+
std::array<uint8_t, G2Element::SIZE> G2Element::SerializeToArray(const bool fLegacy) const {
389401
uint8_t buffer[G2Element::SIZE + 1];
390402
g2_write_bin(buffer, G2Element::SIZE + 1, (g2_st*)q, 1);
391403

404+
std::array<uint8_t, G2Element::SIZE> result{};
405+
392406
if (buffer[0] == 0x00) { // infinity
393-
std::vector<uint8_t> result(G2Element::SIZE, 0);
407+
result.fill(0);
394408
result[0] = 0xc0;
395409
return result;
396410
}
@@ -410,7 +424,6 @@ std::vector<uint8_t> G2Element::Serialize(const bool fLegacy) const {
410424
}
411425
}
412426

413-
std::vector<uint8_t> result(G2Element::SIZE, 0);
414427
if (fLegacy) {
415428
std::memcpy(result.data(), buffer + 1, G2Element::SIZE);
416429
} else {
@@ -551,4 +564,11 @@ std::vector<uint8_t> GTElement::Serialize() const
551564
return data;
552565
}
553566

567+
std::array<uint8_t, GTElement::SIZE> GTElement::SerializeToArray() const
568+
{
569+
std::array<uint8_t, GTElement::SIZE> data{};
570+
Serialize(data.data());
571+
return data;
572+
}
573+
554574
} // end namespace bls

src/dashbls/src/privatekey.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,13 @@ std::vector<uint8_t> PrivateKey::Serialize(const bool fLegacy) const
284284
return data;
285285
}
286286

287+
std::array<uint8_t, PrivateKey::PRIVATE_KEY_SIZE> PrivateKey::SerializeToArray(bool fLegacy) const
288+
{
289+
std::array<uint8_t, PRIVATE_KEY_SIZE> data{};
290+
Serialize(data.data());
291+
return data;
292+
}
293+
287294
G2Element PrivateKey::SignG2(
288295
const uint8_t *msg,
289296
size_t len,

src/dashbls/src/test-bench.cpp

+54-33
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,21 @@ using namespace bls;
3131
void benchSigs() {
3232
string testName = "Signing";
3333
const int numIters = 5000;
34-
PrivateKey sk = AugSchemeMPL().KeyGen(getRandomSeed());
34+
PrivateKey sk = BasicSchemeMPL().KeyGen(getRandomSeed());
3535
vector<uint8_t> message1 = sk.GetG1Element().Serialize();
3636

3737
auto start = startStopwatch();
3838

3939
for (int i = 0; i < numIters; i++) {
40-
AugSchemeMPL().Sign(sk, message1);
40+
BasicSchemeMPL().Sign(sk, message1);
4141
}
4242
endStopwatch(testName, start, numIters);
4343
}
4444

4545
void benchVerification() {
4646
string testName = "Verification";
47-
const int numIters = 10000;
48-
PrivateKey sk = AugSchemeMPL().KeyGen(getRandomSeed());
47+
const int numIters = 1000;
48+
PrivateKey sk = BasicSchemeMPL().KeyGen(getRandomSeed());
4949
G1Element pk = sk.GetG1Element();
5050

5151
std::vector<G2Element> sigs;
@@ -54,42 +54,44 @@ void benchVerification() {
5454
uint8_t message[4];
5555
Util::IntToFourBytes(message, i);
5656
vector<uint8_t> messageBytes(message, message + 4);
57-
sigs.push_back(AugSchemeMPL().Sign(sk, messageBytes));
57+
sigs.push_back(BasicSchemeMPL().Sign(sk, messageBytes));
5858
}
5959

6060
auto start = startStopwatch();
6161
for (int i = 0; i < numIters; i++) {
6262
uint8_t message[4];
6363
Util::IntToFourBytes(message, i);
6464
vector<uint8_t> messageBytes(message, message + 4);
65-
bool ok = AugSchemeMPL().Verify(pk, messageBytes, sigs[i]);
65+
bool ok = BasicSchemeMPL().Verify(pk, messageBytes, sigs[i]);
6666
ASSERT(ok);
6767
}
6868
endStopwatch(testName, start, numIters);
6969
}
7070

7171
void benchBatchVerification() {
72-
const int numIters = 100000;
72+
const int numIters = 10000;
7373

7474
vector<vector<uint8_t>> sig_bytes;
7575
vector<vector<uint8_t>> pk_bytes;
7676
vector<vector<uint8_t>> ms;
7777

78+
auto start = startStopwatch();
7879
for (int i = 0; i < numIters; i++) {
7980
uint8_t message[4];
8081
Util::IntToFourBytes(message, i);
8182
vector<uint8_t> messageBytes(message, message + 4);
82-
PrivateKey sk = AugSchemeMPL().KeyGen(getRandomSeed());
83+
PrivateKey sk = BasicSchemeMPL().KeyGen(getRandomSeed());
8384
G1Element pk = sk.GetG1Element();
84-
sig_bytes.push_back(AugSchemeMPL().Sign(sk, messageBytes).Serialize());
85+
sig_bytes.push_back(BasicSchemeMPL().Sign(sk, messageBytes).Serialize());
8586
pk_bytes.push_back(pk.Serialize());
8687
ms.push_back(messageBytes);
8788
}
89+
endStopwatch("Batch verification preparation", start, numIters);
8890

8991
vector<G1Element> pks;
9092
pks.reserve(numIters);
9193

92-
auto start = startStopwatch();
94+
start = startStopwatch();
9395
for (auto const& pk : pk_bytes) {
9496
pks.emplace_back(G1Element::FromBytes(Bytes(pk)));
9597
}
@@ -105,52 +107,71 @@ void benchBatchVerification() {
105107
endStopwatch("Signature validation", start, numIters);
106108

107109
start = startStopwatch();
108-
G2Element aggSig = AugSchemeMPL().Aggregate(sigs);
110+
G2Element aggSig = BasicSchemeMPL().Aggregate(sigs);
109111
endStopwatch("Aggregation", start, numIters);
110112

111113
start = startStopwatch();
112-
bool ok = AugSchemeMPL().AggregateVerify(pks, ms, aggSig);
114+
bool ok = BasicSchemeMPL().AggregateVerify(pks, ms, aggSig);
113115
ASSERT(ok);
114116
endStopwatch("Batch verification", start, numIters);
115117
}
116118

117-
void benchFastAggregateVerification() {
118-
const int numIters = 5000;
119+
void benchSerialize() {
120+
const int numIters = 5000000;
121+
PrivateKey sk = BasicSchemeMPL().KeyGen(getRandomSeed());
122+
G1Element pk = sk.GetG1Element();
123+
vector<uint8_t> message = sk.GetG1Element().Serialize();
124+
G2Element sig = BasicSchemeMPL().Sign(sk, message);
119125

120-
vector<G2Element> sigs;
121-
vector<G1Element> pks;
122-
vector<uint8_t> message = {1, 2, 3, 4, 5, 6, 7, 8};
123-
vector<G2Element> pops;
126+
auto start = startStopwatch();
127+
for (int i = 0; i < numIters; i++) {
128+
sk.Serialize();
129+
}
130+
endStopwatch("Serialize PrivateKey", start, numIters);
124131

132+
start = startStopwatch();
125133
for (int i = 0; i < numIters; i++) {
126-
PrivateKey sk = PopSchemeMPL().KeyGen(getRandomSeed());
127-
G1Element pk = sk.GetG1Element();
128-
sigs.push_back(PopSchemeMPL().Sign(sk, message));
129-
pops.push_back(PopSchemeMPL().PopProve(sk));
130-
pks.push_back(pk);
134+
pk.Serialize();
131135
}
136+
endStopwatch("Serialize G1Element", start, numIters);
132137

133-
auto start = startStopwatch();
134-
G2Element aggSig = PopSchemeMPL().Aggregate(sigs);
135-
endStopwatch("PopScheme Aggregation", start, numIters);
138+
start = startStopwatch();
139+
for (int i = 0; i < numIters; i++) {
140+
sig.Serialize();
141+
}
142+
endStopwatch("Serialize G2Element", start, numIters);
143+
}
144+
145+
void benchSerializeToArray() {
146+
const int numIters = 5000000;
147+
PrivateKey sk = BasicSchemeMPL().KeyGen(getRandomSeed());
148+
G1Element pk = sk.GetG1Element();
149+
vector<uint8_t> message = sk.GetG1Element().Serialize();
150+
G2Element sig = BasicSchemeMPL().Sign(sk, message);
136151

152+
auto start = startStopwatch();
153+
for (int i = 0; i < numIters; i++) {
154+
sk.SerializeToArray();
155+
}
156+
endStopwatch("SerializeToArray PrivateKey", start, numIters);
137157

138158
start = startStopwatch();
139159
for (int i = 0; i < numIters; i++) {
140-
bool ok = PopSchemeMPL().PopVerify(pks[i], pops[i]);
141-
ASSERT(ok);
160+
pk.SerializeToArray();
142161
}
143-
endStopwatch("PopScheme Proofs verification", start, numIters);
162+
endStopwatch("SerializeToArray G1Element", start, numIters);
144163

145164
start = startStopwatch();
146-
bool ok = PopSchemeMPL().FastAggregateVerify(pks, message, aggSig);
147-
ASSERT(ok);
148-
endStopwatch("PopScheme verification", start, numIters);
165+
for (int i = 0; i < numIters; i++) {
166+
sig.SerializeToArray();
167+
}
168+
endStopwatch("SerializeToArray G2Element", start, numIters);
149169
}
150170

151171
int main(int argc, char* argv[]) {
152172
benchSigs();
153173
benchVerification();
154174
benchBatchVerification();
155-
benchFastAggregateVerification();
175+
benchSerialize();
176+
benchSerializeToArray();
156177
}

0 commit comments

Comments
 (0)