Skip to content

Commit

Permalink
Remove use/wrapping of C includes with libsodium and relic namespace (d…
Browse files Browse the repository at this point in the history
  • Loading branch information
jonspock authored and mariano54 committed Oct 12, 2018
1 parent fb4036f commit 825428f
Show file tree
Hide file tree
Showing 19 changed files with 147 additions and 153 deletions.
2 changes: 1 addition & 1 deletion python-bindings/pythonbindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace py = pybind11;
using namespace bls;

PYBIND11_MODULE(blspy, m) {
py::class_<relic::bn_t*>(m, "bn_ptr");
py::class_<bn_t*>(m, "bn_ptr");

py::class_<AggregationInfo>(m, "AggregationInfo")
.def("from_msg_hash", [](const PublicKey &pk, const py::bytes &b) {
Expand Down
30 changes: 15 additions & 15 deletions src/aggregationinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ AggregationInfo AggregationInfo::FromMsgHash(const PublicKey &pk,
std::memcpy(mapKey, messageHash, BLS::MESSAGE_HASH_LEN);
pk.Serialize(mapKey + BLS::MESSAGE_HASH_LEN);
AggregationInfo::AggregationTree tree;
relic::bn_t *one = new relic::bn_t[1];
bn_t *one = new bn_t[1];
bn_new(*one);
bn_zero(*one);
bn_set_dig(*one, 1);
Expand All @@ -53,7 +53,7 @@ AggregationInfo AggregationInfo::FromMsg(const PublicKey &pk,
AggregationInfo AggregationInfo::FromVectors(
std::vector<PublicKey> const &pubKeys,
std::vector<uint8_t*> const &messageHashes,
std::vector<relic::bn_t*> const &exponents) {
std::vector<bn_t*> const &exponents) {
if (pubKeys.size() != messageHashes.size() || messageHashes.size() !=
exponents.size()) {
throw std::string(("Invalid input, all std::vectors must have\
Expand All @@ -65,7 +65,7 @@ AggregationInfo AggregationInfo::FromVectors(
PublicKey::PUBLIC_KEY_SIZE];
std::memcpy(mapKey, messageHashes[i], BLS::MESSAGE_HASH_LEN);
pubKeys[i].Serialize(mapKey + BLS::MESSAGE_HASH_LEN);
relic::bn_t *mapValue = new relic::bn_t[1];
bn_t *mapValue = new bn_t[1];
bn_new(*mapValue)
bn_copy(*mapValue, *exponents[i]);
tree.insert(std::make_pair(mapKey, mapValue));
Expand Down Expand Up @@ -142,7 +142,7 @@ void AggregationInfo::RemoveEntries(std::vector<uint8_t*> const &messages,
pubKeys[i].Serialize(entry + BLS::MESSAGE_HASH_LEN);
auto kv = tree.find(entry);
const uint8_t* first = kv->first;
const relic::bn_t* second = kv->second;
const bn_t* second = kv->second;
delete[] second;
tree.erase(entry);
delete[] first;
Expand All @@ -153,7 +153,7 @@ void AggregationInfo::RemoveEntries(std::vector<uint8_t*> const &messages,
SortIntoVectors(sortedMessageHashes, sortedPubKeys, tree);
}

void AggregationInfo::GetExponent(relic::bn_t *result, const uint8_t* messageHash,
void AggregationInfo::GetExponent(bn_t *result, const uint8_t* messageHash,
const PublicKey &pubKey) const {
uint8_t mapKey[BLS::MESSAGE_HASH_LEN +
PublicKey::PUBLIC_KEY_SIZE];
Expand Down Expand Up @@ -236,7 +236,7 @@ std::ostream &operator<<(std::ostream &os, AggregationInfo const &a) {
for (auto &kv : a.tree) {
os << Util::HexStr(kv.first, 80) << ".." << ":" << std::endl;
uint8_t str[RELIC_BN_BYTES * 3 + 1];
relic::bn_write_bin(str, sizeof(str), *kv.second);
bn_write_bin(str, sizeof(str), *kv.second);
os << Util::HexStr(str + RELIC_BN_BYTES * 3 + 1 - 5, 5)
<< std::endl;
}
Expand All @@ -257,11 +257,11 @@ void AggregationInfo::InsertIntoTree(AggregationInfo::AggregationTree &tree,
+ PublicKey::PUBLIC_KEY_SIZE];
std::memcpy(messageCopy, mapEntry.first, BLS::MESSAGE_HASH_LEN
+ PublicKey::PUBLIC_KEY_SIZE);
relic::bn_t * exponent = new relic::bn_t[1];
relic::bn_new(*exponent);
bn_t * exponent = new bn_t[1];
bn_new(*exponent);
bn_copy(*exponent, *mapEntry.second);
relic::bn_t ord;
relic::g1_get_ord(ord);
bn_t ord;
g1_get_ord(ord);
bn_mod(*exponent, *exponent, ord);
tree.insert(std::make_pair(messageCopy, exponent));
}
Expand Down Expand Up @@ -338,14 +338,14 @@ AggregationInfo AggregationInfo::SecureMergeInfos(

// Calculate Ts
// Each T is multiplied with an exponent in one of the collidingInfos
relic::bn_t* computedTs = new relic::bn_t[sortedCollidingInfos.size()];
bn_t* computedTs = new bn_t[sortedCollidingInfos.size()];
for (size_t i = 0; i < sortedCollidingInfos.size(); i++) {
bn_new(computedTs[i]);
}
BLS::HashPubKeys(computedTs, sortedCollidingInfos.size(), serPks, sortedKeys);

relic::bn_t ord;
relic::g1_get_ord(ord);
bn_t ord;
g1_get_ord(ord);

// Merge the trees, multiplying by the Ts, and then adding
// to total
Expand All @@ -361,15 +361,15 @@ AggregationInfo AggregationInfo::SecureMergeInfos(
std::memcpy(mapKeyCopy, mapEntry.first, BLS::MESSAGE_HASH_LEN
+ PublicKey::PUBLIC_KEY_SIZE);

relic::bn_t * exponent = new relic::bn_t[1];
bn_t * exponent = new bn_t[1];
bn_new(*exponent);
bn_copy(*exponent, *mapEntry.second);
bn_mul(*exponent, *exponent, computedTs[i]);
bn_mod(*exponent, *exponent, ord);
newTree.insert(std::make_pair(mapKeyCopy, exponent));
} else {
// This message & pk is already included. Multiply.
relic::bn_t tmp;
bn_t tmp;
bn_new(tmp);
bn_copy(tmp, *mapEntry.second);
bn_mul(tmp, tmp, computedTs[i]);
Expand Down
6 changes: 3 additions & 3 deletions src/aggregationinfo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class AggregationInfo {
static AggregationInfo FromVectors(
std::vector<PublicKey> const &pubKeys,
std::vector<uint8_t*> const &messageHashes,
std::vector<relic::bn_t*> const &exponents);
std::vector<bn_t*> const &exponents);

// Merge two AggregationInfo objects into one.
static AggregationInfo MergeInfos(std::vector<AggregationInfo>
Expand All @@ -60,7 +60,7 @@ class AggregationInfo {
std::vector<PublicKey> const &pubKeys);

// Public accessors
void GetExponent(relic::bn_t *result, const uint8_t* messageHash,
void GetExponent(bn_t *result, const uint8_t* messageHash,
const PublicKey &pubkey) const;
std::vector<PublicKey> GetPubKeys() const;
std::vector<uint8_t*> GetMessageHashes() const;
Expand All @@ -79,7 +79,7 @@ class AggregationInfo {
private:
// This is the data structure that maps messages (32) and
// public keys (48) to exponents (bn_t*).
typedef std::map<uint8_t*, relic::bn_t*,
typedef std::map<uint8_t*, bn_t*,
Util::BytesCompare80> AggregationTree;

explicit AggregationInfo(const AggregationTree& tr,
Expand Down
22 changes: 11 additions & 11 deletions src/bls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ bool BLS::Init() {
std::cout << "Must have ALLOC == AUTO";
return false;
}
relic::core_init();
if (relic::err_get_code() != STS_OK) {
core_init();
if (err_get_code() != STS_OK) {
std::cout << "core_init() failed";
return false;
}

const int r = relic::ep_param_set_any_pairf();
const int r = ep_param_set_any_pairf();
if (r != STS_OK) {
std::cout << "ep_param_set_any_pairf() failed";
return false;
}
#if BLSALLOC_SODIUM
if (libsodium::sodium_init() < 0) {
if (sodium_init() < 0) {
std::cout << "libsodium init failed";
return false;
}
Expand All @@ -51,24 +51,24 @@ bool BLS::Init() {
}

void BLS::AssertInitialized() {
if (!relic::core_get()) {
if (!core_get()) {
throw std::string("Library not initialized properly. Call BLS::Init()");
}
#if BLSALLOC_SODIUM
if (libsodium::sodium_init() < 0) {
if (sodium_init() < 0) {
throw std::string("Libsodium initialization failed.");
}
#endif
}

void BLS::Clean() {
relic::core_clean();
core_clean();
}

void BLS::HashPubKeys(relic::bn_t* output, size_t numOutputs,
void BLS::HashPubKeys(bn_t* output, size_t numOutputs,
std::vector<uint8_t*> const &serPubKeys,
std::vector<size_t> const& sortedIndices) {
relic::bn_t order;
bn_t order;

bn_new(order);
g2_get_ord(order);
Expand Down Expand Up @@ -101,10 +101,10 @@ void BLS::HashPubKeys(relic::bn_t* output, size_t numOutputs,
}

void BLS::CheckRelicErrors() {
if (!relic::core_get()) {
if (!core_get()) {
throw std::string("Library not initialized properly. Call BLS::Init()");
}
if (relic::core_get()->code != STS_OK) {
if (core_get()->code != STS_OK) {
throw std::string("Relic library error");
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/bls.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
#include "extendedprivatekey.hpp"
#include "aggregationinfo.hpp"

namespace relic {
#include "relic.h"
#include "relic_test.h"
}

#include "relic.h"
#include "relic_test.h"

namespace bls {

/*
Expand All @@ -56,7 +56,7 @@ class BLS {

// Used for secure aggregation
static void HashPubKeys(
relic::bn_t* output,
bn_t* output,
size_t numOutputs,
std::vector<uint8_t*> const &serPubKeys,
std::vector<size_t> const &sortedIndices);
Expand Down
9 changes: 4 additions & 5 deletions src/chaincode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@
#include <gmp.h>
#endif

namespace relic {
#include "relic.h"
#include "relic_test.h"
}

#include "relic.h"
#include "relic_test.h"

#include "util.hpp"
namespace bls {
Expand All @@ -51,7 +50,7 @@ class ChainCode {
// Prevent direct construction, use static constructor
ChainCode() {}

relic::bn_t chainCode;
bn_t chainCode;
};
} // end namespace bls

Expand Down
12 changes: 6 additions & 6 deletions src/extendedprivatekey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ ExtendedPrivateKey ExtendedPrivateKey::FromSeed(const uint8_t* seed,

// Hash the seed into 64 bytes, half will be sk, half will be cc
hashInput[seedLen] = 0;
relic::md_hmac(ILeft, hashInput, seedLen + 1, prefix, sizeof(prefix));
md_hmac(ILeft, hashInput, seedLen + 1, prefix, sizeof(prefix));

hashInput[seedLen] = 1;
relic::md_hmac(IRight, hashInput, seedLen + 1, prefix, sizeof(prefix));
md_hmac(IRight, hashInput, seedLen + 1, prefix, sizeof(prefix));

// Make sure private key is less than the curve order
relic::bn_t* skBn = Util::SecAlloc<relic::bn_t>(1);
relic::bn_t order;
bn_t* skBn = Util::SecAlloc<bn_t>(1);
bn_t order;
bn_new(order);
g1_get_ord(order);

Expand Down Expand Up @@ -109,13 +109,13 @@ ExtendedPrivateKey ExtendedPrivateKey::PrivateChild(uint32_t i) const {
}
hmacInput[inputLen - 1] = 0;

relic::md_hmac(ILeft, hmacInput, inputLen,
md_hmac(ILeft, hmacInput, inputLen,
hmacKey, ChainCode::CHAIN_CODE_SIZE);

// Change 1 byte to generate a different sequence for chaincode
hmacInput[inputLen - 1] = 1;

relic::md_hmac(IRight, hmacInput, inputLen,
md_hmac(IRight, hmacInput, inputLen,
hmacKey, ChainCode::CHAIN_CODE_SIZE);

PrivateKey newSk = PrivateKey::FromBytes(ILeft, true);
Expand Down
7 changes: 3 additions & 4 deletions src/extendedprivatekey.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@
#include "chaincode.hpp"
#include "extendedpublickey.hpp"

namespace relic {
#include "relic.h"
#include "relic_test.h"
}

#include "relic.h"
#include "relic_test.h"

namespace bls {
/*
Expand Down
4 changes: 2 additions & 2 deletions src/extendedpublickey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ ExtendedPublicKey ExtendedPublicKey::PublicChild(uint32_t i) const {
hmacInput[inputLen - 1] = 0;
Util::IntToFourBytes(hmacInput + PublicKey::PUBLIC_KEY_SIZE, i);

relic::md_hmac(ILeft, hmacInput, inputLen,
md_hmac(ILeft, hmacInput, inputLen,
hmacKey, ChainCode::CHAIN_CODE_SIZE);

// Change 1 byte to generate a different sequence for chaincode
hmacInput[inputLen - 1] = 1;

relic::md_hmac(IRight, hmacInput, inputLen,
md_hmac(IRight, hmacInput, inputLen,
hmacKey, ChainCode::CHAIN_CODE_SIZE);

PrivateKey leftSk = PrivateKey::FromBytes(ILeft, true);
Expand Down
8 changes: 4 additions & 4 deletions src/extendedpublickey.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
#include "publickey.hpp"
#include "chaincode.hpp"

namespace relic {
#include "relic.h"
#include "relic_test.h"
}

#include "relic.h"
#include "relic_test.h"

namespace bls {

/*
Expand Down
Loading

0 comments on commit 825428f

Please sign in to comment.