Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

depends: update 'src/dashbls' to dashpay/bls-signatures@0bb5c5b0 as efd5c56 #6494

Merged
merged 4 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/dashbls/configure.ac
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
AC_PREREQ([2.60])
AC_INIT([libdashbls],[1.3.4])
AC_INIT([libdashbls],[1.3.5])
AC_CONFIG_AUX_DIR([build-aux])
AC_CONFIG_MACRO_DIR([build-aux/m4])

Expand Down
3 changes: 3 additions & 0 deletions src/dashbls/include/dashbls/elements.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class G1Element {
GTElement Pair(const G2Element &b) const;
uint32_t GetFingerprint(bool fLegacy = false) const;
std::vector<uint8_t> Serialize(bool fLegacy = false) const;
std::array<uint8_t, SIZE> SerializeToArray(bool fLegacy = false) const;
G1Element Copy();

friend bool operator==(const G1Element &a, const G1Element &b);
Expand Down Expand Up @@ -102,6 +103,7 @@ class G2Element {
G2Element Negate() const;
GTElement Pair(const G1Element &a) const;
std::vector<uint8_t> Serialize(bool fLegacy = false) const;
std::array<uint8_t, G2Element::SIZE> SerializeToArray(bool fLegacy = false) const;
G2Element Copy();

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

void Serialize(uint8_t *buffer) const;
std::vector<uint8_t> Serialize() const;
std::array<uint8_t, SIZE> SerializeToArray() const;

friend bool operator==(GTElement const &a, GTElement const &b);
friend bool operator!=(GTElement const &a, GTElement const &b);
Expand Down
1 change: 1 addition & 0 deletions src/dashbls/include/dashbls/privatekey.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class PrivateKey {
// Serialize the key into bytes
void Serialize(uint8_t *buffer) const;
std::vector<uint8_t> Serialize(bool fLegacy = false) const;
std::array<uint8_t, PrivateKey::PRIVATE_KEY_SIZE> SerializeToArray(bool fLegacy = false) const;

G2Element SignG2(
const uint8_t *msg,
Expand Down
28 changes: 24 additions & 4 deletions src/dashbls/src/elements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,16 @@ uint32_t G1Element::GetFingerprint(const bool fLegacy) const
}

std::vector<uint8_t> G1Element::Serialize(const bool fLegacy) const {
const auto arr = G1Element::SerializeToArray(fLegacy);
return std::vector<uint8_t>{arr.begin(), arr.end()};
}

std::array<uint8_t, G1Element::SIZE> G1Element::SerializeToArray(const bool fLegacy) const {
uint8_t buffer[G1Element::SIZE + 1];
g1_write_bin(buffer, G1Element::SIZE + 1, p, 1);

std::array<uint8_t, G1Element::SIZE> result{};
if (buffer[0] == 0x00) { // infinity
std::vector<uint8_t> result(G1Element::SIZE, 0);
result[0] = 0xc0;
return result;
}
Expand All @@ -187,7 +192,9 @@ std::vector<uint8_t> G1Element::Serialize(const bool fLegacy) const {
if (!fLegacy) {
buffer[1] |= 0x80; // indicate compression
}
return std::vector<uint8_t>(buffer + 1, buffer + 1 + G1Element::SIZE);

std::copy_n(buffer + 1, G1Element::SIZE, result.begin());
return result;
}

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

std::vector<uint8_t> G2Element::Serialize(const bool fLegacy) const {
const auto arr = G2Element::SerializeToArray(fLegacy);
return std::vector<uint8_t>{arr.begin(), arr.end()};
}

std::array<uint8_t, G2Element::SIZE> G2Element::SerializeToArray(const bool fLegacy) const {
uint8_t buffer[G2Element::SIZE + 1];
g2_write_bin(buffer, G2Element::SIZE + 1, (g2_st*)q, 1);

std::array<uint8_t, G2Element::SIZE> result{};

if (buffer[0] == 0x00) { // infinity
std::vector<uint8_t> result(G2Element::SIZE, 0);
result.fill(0);
result[0] = 0xc0;
return result;
}
Expand All @@ -410,7 +424,6 @@ std::vector<uint8_t> G2Element::Serialize(const bool fLegacy) const {
}
}

std::vector<uint8_t> result(G2Element::SIZE, 0);
if (fLegacy) {
std::memcpy(result.data(), buffer + 1, G2Element::SIZE);
} else {
Expand Down Expand Up @@ -551,4 +564,11 @@ std::vector<uint8_t> GTElement::Serialize() const
return data;
}

std::array<uint8_t, GTElement::SIZE> GTElement::SerializeToArray() const
{
std::array<uint8_t, GTElement::SIZE> data{};
Serialize(data.data());
return data;
}

} // end namespace bls
7 changes: 7 additions & 0 deletions src/dashbls/src/privatekey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,13 @@ std::vector<uint8_t> PrivateKey::Serialize(const bool fLegacy) const
return data;
}

std::array<uint8_t, PrivateKey::PRIVATE_KEY_SIZE> PrivateKey::SerializeToArray(bool fLegacy) const
{
std::array<uint8_t, PRIVATE_KEY_SIZE> data{};
Serialize(data.data());
return data;
}

G2Element PrivateKey::SignG2(
const uint8_t *msg,
size_t len,
Expand Down
87 changes: 54 additions & 33 deletions src/dashbls/src/test-bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@ using namespace bls;
void benchSigs() {
string testName = "Signing";
const int numIters = 5000;
PrivateKey sk = AugSchemeMPL().KeyGen(getRandomSeed());
PrivateKey sk = BasicSchemeMPL().KeyGen(getRandomSeed());
vector<uint8_t> message1 = sk.GetG1Element().Serialize();

auto start = startStopwatch();

for (int i = 0; i < numIters; i++) {
AugSchemeMPL().Sign(sk, message1);
BasicSchemeMPL().Sign(sk, message1);
}
endStopwatch(testName, start, numIters);
}

void benchVerification() {
string testName = "Verification";
const int numIters = 10000;
PrivateKey sk = AugSchemeMPL().KeyGen(getRandomSeed());
const int numIters = 1000;
PrivateKey sk = BasicSchemeMPL().KeyGen(getRandomSeed());
G1Element pk = sk.GetG1Element();

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

auto start = startStopwatch();
for (int i = 0; i < numIters; i++) {
uint8_t message[4];
Util::IntToFourBytes(message, i);
vector<uint8_t> messageBytes(message, message + 4);
bool ok = AugSchemeMPL().Verify(pk, messageBytes, sigs[i]);
bool ok = BasicSchemeMPL().Verify(pk, messageBytes, sigs[i]);
ASSERT(ok);
}
endStopwatch(testName, start, numIters);
}

void benchBatchVerification() {
const int numIters = 100000;
const int numIters = 10000;

vector<vector<uint8_t>> sig_bytes;
vector<vector<uint8_t>> pk_bytes;
vector<vector<uint8_t>> ms;

auto start = startStopwatch();
for (int i = 0; i < numIters; i++) {
uint8_t message[4];
Util::IntToFourBytes(message, i);
vector<uint8_t> messageBytes(message, message + 4);
PrivateKey sk = AugSchemeMPL().KeyGen(getRandomSeed());
PrivateKey sk = BasicSchemeMPL().KeyGen(getRandomSeed());
G1Element pk = sk.GetG1Element();
sig_bytes.push_back(AugSchemeMPL().Sign(sk, messageBytes).Serialize());
sig_bytes.push_back(BasicSchemeMPL().Sign(sk, messageBytes).Serialize());
pk_bytes.push_back(pk.Serialize());
ms.push_back(messageBytes);
}
endStopwatch("Batch verification preparation", start, numIters);

vector<G1Element> pks;
pks.reserve(numIters);

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

start = startStopwatch();
G2Element aggSig = AugSchemeMPL().Aggregate(sigs);
G2Element aggSig = BasicSchemeMPL().Aggregate(sigs);
endStopwatch("Aggregation", start, numIters);

start = startStopwatch();
bool ok = AugSchemeMPL().AggregateVerify(pks, ms, aggSig);
bool ok = BasicSchemeMPL().AggregateVerify(pks, ms, aggSig);
ASSERT(ok);
endStopwatch("Batch verification", start, numIters);
}

void benchFastAggregateVerification() {
const int numIters = 5000;
void benchSerialize() {
const int numIters = 5000000;
PrivateKey sk = BasicSchemeMPL().KeyGen(getRandomSeed());
G1Element pk = sk.GetG1Element();
vector<uint8_t> message = sk.GetG1Element().Serialize();
G2Element sig = BasicSchemeMPL().Sign(sk, message);

vector<G2Element> sigs;
vector<G1Element> pks;
vector<uint8_t> message = {1, 2, 3, 4, 5, 6, 7, 8};
vector<G2Element> pops;
auto start = startStopwatch();
for (int i = 0; i < numIters; i++) {
sk.Serialize();
}
endStopwatch("Serialize PrivateKey", start, numIters);

start = startStopwatch();
for (int i = 0; i < numIters; i++) {
PrivateKey sk = PopSchemeMPL().KeyGen(getRandomSeed());
G1Element pk = sk.GetG1Element();
sigs.push_back(PopSchemeMPL().Sign(sk, message));
pops.push_back(PopSchemeMPL().PopProve(sk));
pks.push_back(pk);
pk.Serialize();
}
endStopwatch("Serialize G1Element", start, numIters);

auto start = startStopwatch();
G2Element aggSig = PopSchemeMPL().Aggregate(sigs);
endStopwatch("PopScheme Aggregation", start, numIters);
start = startStopwatch();
for (int i = 0; i < numIters; i++) {
sig.Serialize();
}
endStopwatch("Serialize G2Element", start, numIters);
}

void benchSerializeToArray() {
const int numIters = 5000000;
PrivateKey sk = BasicSchemeMPL().KeyGen(getRandomSeed());
G1Element pk = sk.GetG1Element();
vector<uint8_t> message = sk.GetG1Element().Serialize();
G2Element sig = BasicSchemeMPL().Sign(sk, message);

auto start = startStopwatch();
for (int i = 0; i < numIters; i++) {
sk.SerializeToArray();
}
endStopwatch("SerializeToArray PrivateKey", start, numIters);

start = startStopwatch();
for (int i = 0; i < numIters; i++) {
bool ok = PopSchemeMPL().PopVerify(pks[i], pops[i]);
ASSERT(ok);
pk.SerializeToArray();
}
endStopwatch("PopScheme Proofs verification", start, numIters);
endStopwatch("SerializeToArray G1Element", start, numIters);

start = startStopwatch();
bool ok = PopSchemeMPL().FastAggregateVerify(pks, message, aggSig);
ASSERT(ok);
endStopwatch("PopScheme verification", start, numIters);
for (int i = 0; i < numIters; i++) {
sig.SerializeToArray();
}
endStopwatch("SerializeToArray G2Element", start, numIters);
}

int main(int argc, char* argv[]) {
benchSigs();
benchVerification();
benchBatchVerification();
benchFastAggregateVerification();
benchSerialize();
benchSerializeToArray();
}
Loading